]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/connection.cpp
Added routing table transfer between mesh nodes
[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         srand(time(NULL));
23         id = random();
24 }
25
26 packet::~packet()
27 {
28 }
29
30 connection::connection()
31 {
32         key = GenKey();
33         fd = 0;
34 }
35
36
37 bool connection::CreateListener(char* host, int p)
38 {
39         sockaddr_in host_address;
40         int flags;
41         in_addr addy;
42         int on = 0;
43         struct linger linger = { 0 };
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 bool ircd_connector::MakeOutboundConnection(char* host, int port)
124 {
125         hostent* hoste = gethostbyname(host);
126         if (!hoste)
127         {
128                 WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
129                 this->SetHostAddress(host,port);
130                 SetHostAndPort(host,port);
131         }
132         else
133         {
134                 WriteOpers("Found hostname for %s",host);
135                 this->SetHostAddress(hoste->h_addr,port);
136                 SetHostAndPort(hoste->h_addr,port);
137         }
138
139         this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
140         if (this->fd >= 0)
141         {
142                 if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
143                 {
144                         WriteOpers("connect() failed for %s",host);
145                         return false;
146                 }
147                 int flags = fcntl(this->fd, F_GETFL, 0);
148                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
149                 int sendbuf = 32768;
150                 int recvbuf = 32768;
151                 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
152                 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
153                 return true;
154         }
155         else
156         {
157                 WriteOpers("socket() failed!");
158         }
159
160         return false;
161 }
162
163
164 bool connection::BeginLink(char* targethost, int port, char* password, char* servername)
165 {
166         char connect[MAXBUF];
167         
168         ircd_connector connector;
169         
170         if (this->fd)
171         {
172                 if (connector.MakeOutboundConnection(targethost,port))
173                 {
174                         // targethost has been turned into an ip...
175                         // we dont want this as the server name.
176                         connector.SetServerName(servername);
177                         sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
178                         connector.SetState(STATE_NOAUTH_OUTBOUND);
179                         connector.SetHostAndPort(targethost, port);
180                         this->connectors.push_back(connector);
181                         return this->SendPacket(connect, servername);
182                 }
183                 else
184                 {
185                         WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
186                 }
187         }
188         return false;
189 }
190
191 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
192 {
193         char connect[MAXBUF];
194         
195         ircd_connector connector;
196         
197         WriteOpers("Establishing meshed link to %s:%d",servername,port);
198
199         if (this->fd)
200         {
201                 if (connector.MakeOutboundConnection(targethost,port))
202                 {
203                         // targethost has been turned into an ip...
204                         // we dont want this as the server name.
205                         connector.SetServerName(servername);
206                         sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
207                         connector.SetState(STATE_NOAUTH_OUTBOUND);
208                         connector.SetHostAndPort(targethost, port);
209                         connector.SetState(STATE_CONNECTED);
210                         this->connectors.push_back(connector);
211                         return this->SendPacket(connect, servername);
212                 }
213                 else
214                 {
215                         WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
216                 }
217         }
218         return false;
219 }
220
221 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
222 {
223         char connect[MAXBUF];
224         
225         ircd_connector connector;
226         
227         // targethost has been turned into an ip...
228         // we dont want this as the server name.
229         connector.SetServerName(targethost);
230         connector.SetDescriptor(fd);
231         connector.SetState(STATE_NOAUTH_INBOUND);
232         int flags = fcntl(fd, F_GETFL, 0);
233         fcntl(fd, F_SETFL, flags | O_NONBLOCK);
234         int sendbuf = 32768;
235         int recvbuf = 32768;
236         setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
237         setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
238         connector.SetHostAndPort(targethost, sourceport);
239         connector.SetState(STATE_NOAUTH_INBOUND);
240         log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
241         this->connectors.push_back(connector);
242         return true;
243 }
244
245 void connection::TerminateLink(char* targethost)
246 {
247         // this locates the targethost in the connection::connectors vector of the class,
248         // and terminates it by sending it an SQUIT token and closing its descriptor.
249         // TerminateLink with a null string causes a terminate of ALL links
250 }
251
252
253 // Returns a pointer to the connector for 'host'
254 ircd_connector* connection::FindHost(std::string host)
255 {
256         for (int i = 0; i < this->connectors.size(); i++)
257         {
258                 if (this->connectors[i].GetServerName() == host)
259                 {
260                         return &this->connectors[i];
261                 }
262         }
263         return NULL;
264 }
265
266 std::string ircd_connector::GetServerName()
267 {
268         return this->servername;
269 }
270
271 std::string ircd_connector::GetDescription()
272 {
273         return this->description;
274 }
275
276 void ircd_connector::SetServerName(std::string serv)
277 {
278         this->servername = serv;
279 }
280
281 void ircd_connector::SetDescription(std::string desc)
282 {
283         this->servername = desc;
284 }
285
286
287 int ircd_connector::GetDescriptor()
288 {
289         return this->fd;
290 }
291
292 int ircd_connector::GetState()
293 {
294         return this->state;
295 }
296
297
298 void ircd_connector::SetState(int state)
299 {
300         this->state = state;
301 }
302
303 void ircd_connector::CloseConnection()
304 {
305         int flags = fcntl(this->fd, F_GETFL, 0);
306         fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
307         close(this->fd);
308         flags = fcntl(this->fd, F_GETFL, 0);
309         fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
310 }
311
312 void ircd_connector::SetDescriptor(int fd)
313 {
314         this->fd = fd;
315 }
316
317 bool connection::SendPacket(char *message, const char* host)
318 {
319         ircd_connector* cn = this->FindHost(host);
320         strncat(message,"\n",MAXBUF);
321
322         if (cn)
323         {
324                 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
325                 
326                 if (cn->GetState() == STATE_DISCONNECTED)
327                 {
328                         log(DEBUG,"Main route to %s is down, seeking alternative",host);
329                         // this route is down, we must re-route the packet through an available point in the mesh.
330                         for (int k = 0; k < this->connectors.size(); k++)
331                         {
332                                 // search for another point in the mesh which can 'reach' where we want to go
333                                 for (int m = 0; m < this->connectors[k].routes.size(); m++)
334                                 {
335                                         if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
336                                         {
337                                                 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
338                                                 char buffer[MAXBUF];
339                                                 snprintf(buffer,MAXBUF,"R %s %s",host,message);
340                                                 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
341                                                 return true;
342                                         }
343                                 }
344                         }
345                         log(DEBUG,"ERROR: Main route to %s is down and there are no possible routes to this server!",host);
346                         return false;
347                 }
348
349                 // returns false if the packet could not be sent (e.g. target host down)
350                 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
351                 {
352                         log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
353                         log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
354                         cn->CloseConnection();
355                         cn->SetState(STATE_DISCONNECTED);
356                         return false;
357                 }
358                 return true;
359         }
360 }
361
362 // receives a packet from any where there is data waiting, first come, first served
363 // fills the message and host values with the host where the data came from.
364
365 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
366 {
367         char data[32767];
368         memset(data, 0, 32767);
369         for (int i = 0; i < this->connectors.size(); i++)
370         {
371                 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
372                 {
373                         // returns false if the packet could not be sent (e.g. target host down)
374                         int rcvsize = 0;
375                         rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
376                         if (rcvsize == -1)
377                         {
378                                 if (errno != EAGAIN)
379                                 {
380                                         log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
381                                         log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
382                                         this->connectors[i].CloseConnection();
383                                         this->connectors[i].SetState(STATE_DISCONNECTED);
384                                 }
385                         }
386                         if (rcvsize > 0)
387                         {
388                                 char* l = strtok(data,"\n");
389                                 while (l)
390                                 {
391                                         char sanitized[32767];
392                                         memset(sanitized, 0, 32767);
393                                         int ptt = 0;
394                                         for (int pt = 0; pt < strlen(l); pt++)
395                                         {
396                                                 if (l[pt] != '\r')
397                                                 {
398                                                         sanitized[ptt++] = l[pt];
399                                                 }
400                                         }
401                                         sanitized[ptt] = '\0';
402                                         if (strlen(sanitized))
403                                         {
404                                                 messages.push_back(sanitized);
405                                                 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
406                                                 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
407                                                 
408                                         }
409                                         l = strtok(NULL,"\n");
410                                 }
411                                 return true;
412                         }
413                 }
414         }
415         // nothing new yet -- message and host will be undefined
416         return false;
417 }
418
419 long connection::GenKey()
420 {
421         srand(time(NULL));
422         return (random()*time(NULL));
423 }
424