]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/connection.cpp
Fixes to ircd_connector::SetServerDesc() (was setting the server name!)
[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 connection::connection()
21 {
22         fd = 0;
23 }
24
25
26 bool connection::CreateListener(char* host, int p)
27 {
28         sockaddr_in host_address;
29         int flags;
30         in_addr addy;
31         int on = 0;
32         struct linger linger = { 0 };
33         
34         this->port = p;
35         
36         fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
37         if (fd <= 0)
38         {
39                 return false;
40         }
41
42         memset((void*)&host_address, 0, sizeof(host_address));
43
44         host_address.sin_family = AF_INET;
45
46         if (!strcmp(host,""))
47         {
48                 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
49         }
50         else
51         {
52                 inet_aton(host,&addy);
53                 host_address.sin_addr = addy;
54         }
55
56         host_address.sin_port = htons(p);
57
58         if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
59         {
60                 return false;
61         }
62
63         // make the socket non-blocking
64         flags = fcntl(fd, F_GETFL, 0);
65         fcntl(fd, F_SETFL, flags | O_NONBLOCK);
66
67         this->port = p;
68
69         setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
70         linger.l_onoff = 1;
71         linger.l_linger = 0;
72         setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
73         
74         // attempt to increase socket sendq and recvq as high as its possible
75         // to get them on linux.
76         int sendbuf = 32768;
77         int recvbuf = 32768;
78         setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
79         setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
80         
81         listen(this->fd,5);
82
83         return true;
84 }
85
86 char* ircd_connector::GetServerIP()
87 {
88         return this->host;
89 }
90
91 int ircd_connector::GetServerPort()
92 {
93         return this->port;
94 }
95
96 bool ircd_connector::SetHostAndPort(char* host, int port)
97 {
98         strncpy(this->host,host,160);
99         this->port = port;
100         return true;
101 }
102
103 bool ircd_connector::SetHostAddress(char* host, int port)
104 {
105         strncpy(this->host,host,160);
106         this->port = port;
107         memset((void*)&addr, 0, sizeof(addr));
108         addr.sin_family = AF_INET;
109         inet_aton(host,&addr.sin_addr);
110         addr.sin_port = htons(port);
111         return true;
112 }
113
114 void ircd_connector::SetServerPort(int p)
115 {
116         this->port = p;
117 }
118
119 bool ircd_connector::MakeOutboundConnection(char* host, int port)
120 {
121         log(DEBUG,"MakeOutboundConnection: Original param: %s",host);
122         hostent* hoste = gethostbyname(host);
123         if (!hoste)
124         {
125                 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",host);
126                 this->SetHostAddress(host,port);
127                 SetHostAndPort(host,port);
128         }
129         else
130         {
131                 struct in_addr* ia = (in_addr*)hoste->h_addr;
132                 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
133                 this->SetHostAddress(inet_ntoa(*ia),port);
134                 SetHostAndPort(inet_ntoa(*ia),port);
135         }
136
137         this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
138         if (this->fd >= 0)
139         {
140                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
141                 {
142                         WriteOpers("connect() failed for %s",host);
143                         return false;
144                 }
145                 int flags = fcntl(this->fd, F_GETFL, 0);
146                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
147                 int sendbuf = 32768;
148                 int recvbuf = 32768;
149                 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
150                 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
151                 return true;
152         }
153         else
154         {
155                 WriteOpers("socket() failed!");
156         }
157
158         return false;
159 }
160
161
162 bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
163 {
164         char connect[MAXBUF];
165         
166         ircd_connector connector;
167         
168         if (this->fd)
169         {
170                 if (connector.MakeOutboundConnection(targethost,port))
171                 {
172                         // targethost has been turned into an ip...
173                         // we dont want this as the server name.
174                         connector.SetServerName(servername);
175                         sprintf(connect,"S %s %s %d :%s",getservername().c_str(),password,myport,getserverdesc().c_str());
176                         connector.SetState(STATE_NOAUTH_OUTBOUND);
177                         connector.SetHostAndPort(targethost, port);
178                         this->connectors.push_back(connector);
179                         return this->SendPacket(connect, servername);
180                 }
181                 else
182                 {
183                         connector.SetState(STATE_DISCONNECTED);
184                         WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
185                 }
186         }
187         return false;
188 }
189
190 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
191 {
192         char connect[MAXBUF];
193         
194         ircd_connector connector;
195         
196         WriteOpers("Establishing meshed link to %s:%d",servername,port);
197
198         if (this->fd)
199         {
200                 if (connector.MakeOutboundConnection(targethost,port))
201                 {
202                         // targethost has been turned into an ip...
203                         // we dont want this as the server name.
204                         connector.SetServerName(servername);
205                         sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
206                         connector.SetState(STATE_NOAUTH_OUTBOUND);
207                         connector.SetHostAndPort(targethost, port);
208                         connector.SetState(STATE_CONNECTED);
209                         this->connectors.push_back(connector);
210                         return this->SendPacket(connect, servername);
211                 }
212                 else
213                 {
214                         connector.SetState(STATE_DISCONNECTED);
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->description = 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         if (state == STATE_DISCONNECTED)
302         {
303                 NetSendMyRoutingTable();
304         }
305 }
306
307 void ircd_connector::CloseConnection()
308 {
309         int flags = fcntl(this->fd, F_GETFL, 0);
310         fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
311         close(this->fd);
312         flags = fcntl(this->fd, F_GETFL, 0);
313         fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
314 }
315
316 void ircd_connector::SetDescriptor(int fd)
317 {
318         this->fd = fd;
319 }
320
321 bool connection::SendPacket(char *message, const char* host)
322 {
323         ircd_connector* cn = this->FindHost(host);
324         
325         if (!strchr(message,'\n'))
326         {
327                 strncat(message,"\n",MAXBUF);
328         }
329
330         if (cn)
331         {
332                 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
333                 
334                 if (cn->GetState() == STATE_DISCONNECTED)
335                 {
336                         log(DEBUG,"Main route to %s is down, seeking alternative",host);
337                         // this route is down, we must re-route the packet through an available point in the mesh.
338                         for (int k = 0; k < this->connectors.size(); k++)
339                         {
340                                 // search for another point in the mesh which can 'reach' where we want to go
341                                 for (int m = 0; m < this->connectors[k].routes.size(); m++)
342                                 {
343                                         if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
344                                         {
345                                                 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
346                                                 char buffer[MAXBUF];
347                                                 snprintf(buffer,MAXBUF,"R %s %s",host,message);
348                                                 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
349                                                 return true;
350                                         }
351                                 }
352                         }
353                         char buffer[MAXBUF];
354                         snprintf(buffer,MAXBUF,"& %s",host);
355                         NetSendToAllExcept(host,buffer);
356                         log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
357                         DoSplit(host);
358                         return false;
359                 }
360
361                 // returns false if the packet could not be sent (e.g. target host down)
362                 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
363                 {
364                         log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
365                         log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
366                         cn->CloseConnection();
367                         cn->SetState(STATE_DISCONNECTED);
368                         return false;
369                 }
370                 return true;
371         }
372 }
373
374 // receives a packet from any where there is data waiting, first come, first served
375 // fills the message and host values with the host where the data came from.
376
377 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
378 {
379         char data[32767];
380         memset(data, 0, 32767);
381         for (int i = 0; i < this->connectors.size(); i++)
382         {
383                 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
384                 {
385                         // returns false if the packet could not be sent (e.g. target host down)
386                         int rcvsize = 0;
387                         rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
388                         if (rcvsize == -1)
389                         {
390                                 if (errno != EAGAIN)
391                                 {
392                                         log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
393                                         log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
394                                         this->connectors[i].CloseConnection();
395                                         this->connectors[i].SetState(STATE_DISCONNECTED);
396                                 }
397                         }
398                         if (rcvsize > 0)
399                         {
400                                 char* l = strtok(data,"\n");
401                                 while (l)
402                                 {
403                                         char sanitized[32767];
404                                         memset(sanitized, 0, 32767);
405                                         int ptt = 0;
406                                         for (int pt = 0; pt < strlen(l); pt++)
407                                         {
408                                                 if (l[pt] != '\r')
409                                                 {
410                                                         sanitized[ptt++] = l[pt];
411                                                 }
412                                         }
413                                         sanitized[ptt] = '\0';
414                                         if (strlen(sanitized))
415                                         {
416                                                 messages.push_back(sanitized);
417                                                 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
418                                                 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
419                                                 
420                                         }
421                                         l = strtok(NULL,"\n");
422                                 }
423                                 return true;
424                         }
425                 }
426         }
427         // nothing new yet -- message and host will be undefined
428         return false;
429 }
430
431 long connection::GenKey()
432 {
433         srand(time(NULL));
434         return (random()*time(NULL));
435 }
436