]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/connection.cpp
Fixes to class packet to make it work!
[user/henk/code/inspircd.git] / src / connection.cpp
1 #include <connection.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <sys/errno.h>
5 #include <sys/ioctl.h>
6 #include <sys/utsname.h>
7 #include <vector>
8 #include <string>
9 #include <deque>
10 #include "inspircd.h"
11 #include "modules.h"
12
13 using namespace std;
14
15 extern std::vector<Module*> modules;
16 extern std::vector<ircd_module*> factory;
17
18 extern int MODCOUNT;
19
20 packet::packet()
21 {
22 }
23
24 packet::~packet()
25 {
26 }
27
28 connection::connection()
29 {
30         key = GenKey();
31         fd = 0;
32 }
33
34
35 bool connection::CreateListener(char* host, int p)
36 {
37         sockaddr_in host_address;
38         int flags;
39         in_addr addy;
40         int on = 0;
41         struct linger linger = { 0 };
42         
43         this->port = p;
44         
45         fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
46         if (fd <= 0)
47         {
48                 return false;
49         }
50
51         memset((void*)&host_address, 0, sizeof(host_address));
52
53         host_address.sin_family = AF_INET;
54
55         if (!strcmp(host,""))
56         {
57                 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
58         }
59         else
60         {
61                 inet_aton(host,&addy);
62                 host_address.sin_addr = addy;
63         }
64
65         host_address.sin_port = htons(p);
66
67         if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
68         {
69                 return false;
70         }
71
72         // make the socket non-blocking
73         flags = fcntl(fd, F_GETFL, 0);
74         fcntl(fd, F_SETFL, flags | O_NONBLOCK);
75
76         this->port = p;
77
78         setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
79         linger.l_onoff = 1;
80         linger.l_linger = 0;
81         setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
82         
83         // attempt to increase socket sendq and recvq as high as its possible
84         // to get them on linux.
85         int sendbuf = 32768;
86         int recvbuf = 32768;
87         setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
88         setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
89         
90         listen(this->fd,5);
91
92         return true;
93 }
94
95 char* ircd_connector::GetServerIP()
96 {
97         return this->host;
98 }
99
100 int ircd_connector::GetServerPort()
101 {
102         return this->port;
103 }
104
105 bool ircd_connector::SetHostAndPort(char* host, int port)
106 {
107         strncpy(this->host,host,160);
108         this->port = port;
109         return true;
110 }
111
112 bool ircd_connector::SetHostAddress(char* host, int port)
113 {
114         strncpy(this->host,host,160);
115         this->port = port;
116         memset((void*)&addr, 0, sizeof(addr));
117         addr.sin_family = AF_INET;
118         inet_aton(host,&addr.sin_addr);
119         addr.sin_port = htons(port);
120         return true;
121 }
122
123 void ircd_connector::SetServerPort(int p)
124 {
125         this->port = p;
126 }
127
128
129 bool ircd_connector::MakeOutboundConnection(char* host, int port)
130 {
131         hostent* hoste = gethostbyname(host);
132         if (!hoste)
133         {
134                 WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
135                 this->SetHostAddress(host,port);
136                 SetHostAndPort(host,port);
137         }
138         else
139         {
140                 WriteOpers("Found hostname for %s",host);
141                 this->SetHostAddress(hoste->h_addr,port);
142                 SetHostAndPort(hoste->h_addr,port);
143         }
144
145         this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
146         if (this->fd >= 0)
147         {
148                 if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
149                 {
150                         WriteOpers("connect() failed for %s",host);
151                         return false;
152                 }
153                 int flags = fcntl(this->fd, F_GETFL, 0);
154                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
155                 int sendbuf = 32768;
156                 int recvbuf = 32768;
157                 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
158                 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
159                 return true;
160         }
161         else
162         {
163                 WriteOpers("socket() failed!");
164         }
165
166         return false;
167 }
168
169
170 bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
171 {
172         char connect[MAXBUF];
173         
174         ircd_connector connector;
175         
176         if (this->fd)
177         {
178                 if (connector.MakeOutboundConnection(targethost,port))
179                 {
180                         // targethost has been turned into an ip...
181                         // we dont want this as the server name.
182                         connector.SetServerName(servername);
183                         sprintf(connect,"S %s %s %d :%s",getservername().c_str(),password,myport,getserverdesc().c_str());
184                         connector.SetState(STATE_NOAUTH_OUTBOUND);
185                         connector.SetHostAndPort(targethost, port);
186                         this->connectors.push_back(connector);
187                         return this->SendPacket(connect, servername);
188                 }
189                 else
190                 {
191                         connector.SetState(STATE_DISCONNECTED);
192                         WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
193                 }
194         }
195         return false;
196 }
197
198 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
199 {
200         char connect[MAXBUF];
201         
202         ircd_connector connector;
203         
204         WriteOpers("Establishing meshed link to %s:%d",servername,port);
205
206         if (this->fd)
207         {
208                 if (connector.MakeOutboundConnection(targethost,port))
209                 {
210                         // targethost has been turned into an ip...
211                         // we dont want this as the server name.
212                         connector.SetServerName(servername);
213                         sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
214                         connector.SetState(STATE_NOAUTH_OUTBOUND);
215                         connector.SetHostAndPort(targethost, port);
216                         connector.SetState(STATE_CONNECTED);
217                         this->connectors.push_back(connector);
218                         return this->SendPacket(connect, servername);
219                 }
220                 else
221                 {
222                         connector.SetState(STATE_DISCONNECTED);
223                         WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
224                 }
225         }
226         return false;
227 }
228
229 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
230 {
231         char connect[MAXBUF];
232         
233         ircd_connector connector;
234         
235         // targethost has been turned into an ip...
236         // we dont want this as the server name.
237         connector.SetServerName(targethost);
238         connector.SetDescriptor(fd);
239         connector.SetState(STATE_NOAUTH_INBOUND);
240         int flags = fcntl(fd, F_GETFL, 0);
241         fcntl(fd, F_SETFL, flags | O_NONBLOCK);
242         int sendbuf = 32768;
243         int recvbuf = 32768;
244         setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
245         setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
246         connector.SetHostAndPort(targethost, sourceport);
247         connector.SetState(STATE_NOAUTH_INBOUND);
248         log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
249         this->connectors.push_back(connector);
250         return true;
251 }
252
253 void connection::TerminateLink(char* targethost)
254 {
255         // this locates the targethost in the connection::connectors vector of the class,
256         // and terminates it by sending it an SQUIT token and closing its descriptor.
257         // TerminateLink with a null string causes a terminate of ALL links
258 }
259
260
261 // Returns a pointer to the connector for 'host'
262 ircd_connector* connection::FindHost(std::string host)
263 {
264         for (int i = 0; i < this->connectors.size(); i++)
265         {
266                 if (this->connectors[i].GetServerName() == host)
267                 {
268                         return &this->connectors[i];
269                 }
270         }
271         return NULL;
272 }
273
274 std::string ircd_connector::GetServerName()
275 {
276         return this->servername;
277 }
278
279 std::string ircd_connector::GetDescription()
280 {
281         return this->description;
282 }
283
284 void ircd_connector::SetServerName(std::string serv)
285 {
286         this->servername = serv;
287 }
288
289 void ircd_connector::SetDescription(std::string desc)
290 {
291         this->servername = desc;
292 }
293
294
295 int ircd_connector::GetDescriptor()
296 {
297         return this->fd;
298 }
299
300 int ircd_connector::GetState()
301 {
302         return this->state;
303 }
304
305
306 void ircd_connector::SetState(int state)
307 {
308         this->state = state;
309         if (state == STATE_DISCONNECTED)
310         {
311                 NetSendMyRoutingTable();
312         }
313 }
314
315 void ircd_connector::CloseConnection()
316 {
317         int flags = fcntl(this->fd, F_GETFL, 0);
318         fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
319         close(this->fd);
320         flags = fcntl(this->fd, F_GETFL, 0);
321         fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
322 }
323
324 void ircd_connector::SetDescriptor(int fd)
325 {
326         this->fd = fd;
327 }
328
329 bool connection::SendPacket(char *message, const char* host)
330 {
331         ircd_connector* cn = this->FindHost(host);
332         
333         if (!strchr(message,'\n'))
334         {
335                 strncat(message,"\n",MAXBUF);
336         }
337
338         if (cn)
339         {
340                 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
341                 
342                 if (cn->GetState() == STATE_DISCONNECTED)
343                 {
344                         log(DEBUG,"Main route to %s is down, seeking alternative",host);
345                         // this route is down, we must re-route the packet through an available point in the mesh.
346                         for (int k = 0; k < this->connectors.size(); k++)
347                         {
348                                 // search for another point in the mesh which can 'reach' where we want to go
349                                 for (int m = 0; m < this->connectors[k].routes.size(); m++)
350                                 {
351                                         if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
352                                         {
353                                                 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
354                                                 char buffer[MAXBUF];
355                                                 snprintf(buffer,MAXBUF,"R %s %s",host,message);
356                                                 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
357                                                 return true;
358                                         }
359                                 }
360                         }
361                         char buffer[MAXBUF];
362                         snprintf(buffer,MAXBUF,"& %s",host);
363                         NetSendToAllExcept(host,buffer);
364                         log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
365                         DoSplit(host);
366                         return false;
367                 }
368
369                 // returns false if the packet could not be sent (e.g. target host down)
370                 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
371                 {
372                         log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
373                         log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
374                         cn->CloseConnection();
375                         cn->SetState(STATE_DISCONNECTED);
376                         return false;
377                 }
378                 return true;
379         }
380 }
381
382 // receives a packet from any where there is data waiting, first come, first served
383 // fills the message and host values with the host where the data came from.
384
385 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
386 {
387         char data[32767];
388         memset(data, 0, 32767);
389         for (int i = 0; i < this->connectors.size(); i++)
390         {
391                 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
392                 {
393                         // returns false if the packet could not be sent (e.g. target host down)
394                         int rcvsize = 0;
395                         rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
396                         if (rcvsize == -1)
397                         {
398                                 if (errno != EAGAIN)
399                                 {
400                                         log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
401                                         log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
402                                         this->connectors[i].CloseConnection();
403                                         this->connectors[i].SetState(STATE_DISCONNECTED);
404                                 }
405                         }
406                         if (rcvsize > 0)
407                         {
408                                 char* l = strtok(data,"\n");
409                                 while (l)
410                                 {
411                                         char sanitized[32767];
412                                         memset(sanitized, 0, 32767);
413                                         int ptt = 0;
414                                         for (int pt = 0; pt < strlen(l); pt++)
415                                         {
416                                                 if (l[pt] != '\r')
417                                                 {
418                                                         sanitized[ptt++] = l[pt];
419                                                 }
420                                         }
421                                         sanitized[ptt] = '\0';
422                                         if (strlen(sanitized))
423                                         {
424                                                 messages.push_back(sanitized);
425                                                 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
426                                                 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
427                                                 
428                                         }
429                                         l = strtok(NULL,"\n");
430                                 }
431                                 return true;
432                         }
433                 }
434         }
435         // nothing new yet -- message and host will be undefined
436         return false;
437 }
438
439 long connection::GenKey()
440 {
441         srand(time(NULL));
442         return (random()*time(NULL));
443 }
444