1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #include <connection.h>
21 #include <sys/errno.h>
22 #include <sys/ioctl.h>
23 #include <sys/utsname.h>
29 #include "inspstring.h"
34 extern std::vector<Module*> modules;
35 extern std::vector<ircd_module*> factory;
41 connection::connection()
47 bool connection::CreateListener(char* host, int p)
49 sockaddr_in host_address;
53 struct linger linger = { 0 };
57 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
63 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
66 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
68 // attempt to increase socket sendq and recvq as high as its possible
69 // to get them on linux.
72 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
73 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
75 memset((void*)&host_address, 0, sizeof(host_address));
77 host_address.sin_family = AF_INET;
81 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
85 inet_aton(host,&addy);
86 host_address.sin_addr = addy;
89 host_address.sin_port = htons(p);
91 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
96 // make the socket non-blocking
97 flags = fcntl(fd, F_GETFL, 0);
98 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
107 char* ircd_connector::GetServerIP()
112 int ircd_connector::GetServerPort()
117 bool ircd_connector::SetHostAndPort(char* host, int port)
119 strncpy(this->host,host,160);
124 bool ircd_connector::SetHostAddress(char* host, int port)
126 strncpy(this->host,host,160);
128 memset((void*)&addr, 0, sizeof(addr));
129 addr.sin_family = AF_INET;
130 inet_aton(host,&addr.sin_addr);
131 addr.sin_port = htons(port);
135 void ircd_connector::SetServerPort(int p)
140 void ircd_connector::AddBuffer(std::string a)
142 for (int i = 0; i < a.length(); i++)
144 ircdbuffer = ircdbuffer + a[i];
147 bool ircd_connector::BufferIsComplete()
149 for (int i = 0; i < ircdbuffer.length(); i++)
150 if (ircdbuffer[i] == '\n')
155 void ircd_connector::ClearBuffer()
157 while ((ircdbuffer != "") && (ircdbuffer[0] != '\n'))
159 ircdbuffer.erase(ircdbuffer.begin());
161 if (ircdbuffer != "")
162 ircdbuffer.erase(ircdbuffer.begin());
165 std::string ircd_connector::GetBuffer()
169 while (ircdbuffer[t] != '\n')
171 z = z + ircdbuffer[t];
177 bool ircd_connector::MakeOutboundConnection(char* host, int port)
179 log(DEBUG,"MakeOutboundConnection: Original param: %s",host);
181 hostent* hoste = gethostbyname(host);
184 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",host);
185 this->SetHostAddress(host,port);
186 SetHostAndPort(host,port);
190 struct in_addr* ia = (in_addr*)hoste->h_addr;
191 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
192 this->SetHostAddress(inet_ntoa(*ia),port);
193 SetHostAndPort(inet_ntoa(*ia),port);
196 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
199 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
201 WriteOpers("connect() failed for %s",host);
202 RemoveServer(this->servername.c_str());
205 int flags = fcntl(this->fd, F_GETFL, 0);
206 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
209 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
210 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
215 WriteOpers("socket() failed!");
216 RemoveServer(this->servername.c_str());
223 bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
225 char connect[MAXBUF];
227 ircd_connector connector;
228 ircd_connector *cn = this->FindHost(servername);
233 WriteOpers("CONNECT aborted: Server %s already exists",servername);
240 if (connector.MakeOutboundConnection(targethost,port))
242 // targethost has been turned into an ip...
243 // we dont want this as the server name.
244 connector.SetServerName(servername);
245 snprintf(connect,MAXBUF,"S %s %s %d %d :%s",getservername().c_str(),password,myport,GetRevision(),getserverdesc().c_str());
246 connector.SetState(STATE_NOAUTH_OUTBOUND);
247 connector.SetHostAndPort(targethost, port);
248 this->connectors.push_back(connector);
249 return this->SendPacket(connect, servername);
253 connector.SetState(STATE_DISCONNECTED);
254 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
260 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
262 char connect[MAXBUF];
264 ircd_connector connector;
266 WriteOpers("Establishing meshed link to %s:%d",servername,port);
270 if (connector.MakeOutboundConnection(targethost,port))
272 // targethost has been turned into an ip...
273 // we dont want this as the server name.
274 connector.SetServerName(servername);
275 snprintf(connect,MAXBUF,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
276 connector.SetState(STATE_NOAUTH_OUTBOUND);
277 connector.SetHostAndPort(targethost, port);
278 connector.SetState(STATE_CONNECTED);
279 this->connectors.push_back(connector);
280 return this->SendPacket(connect, servername);
284 connector.SetState(STATE_DISCONNECTED);
285 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
291 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
293 char connect[MAXBUF];
295 ircd_connector connector;
297 // targethost has been turned into an ip...
298 // we dont want this as the server name.
299 connector.SetServerName(targethost);
300 connector.SetDescriptor(fd);
301 connector.SetState(STATE_NOAUTH_INBOUND);
302 int flags = fcntl(fd, F_GETFL, 0);
303 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
306 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
307 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
308 connector.SetHostAndPort(targethost, sourceport);
309 connector.SetState(STATE_NOAUTH_INBOUND);
310 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
311 this->connectors.push_back(connector);
315 void connection::TerminateLink(char* targethost)
317 // this locates the targethost in the connection::connectors vector of the class,
318 // and terminates it by sending it an SQUIT token and closing its descriptor.
319 // TerminateLink with a null string causes a terminate of ALL links
323 // Returns a pointer to the connector for 'host'
324 ircd_connector* connection::FindHost(std::string host)
326 for (int i = 0; i < this->connectors.size(); i++)
328 if (this->connectors[i].GetServerName() == host)
330 return &this->connectors[i];
336 std::string ircd_connector::GetServerName()
338 return this->servername;
341 std::string ircd_connector::GetDescription()
343 return this->description;
346 void ircd_connector::SetServerName(std::string serv)
348 this->servername = serv;
351 void ircd_connector::SetDescription(std::string desc)
353 this->description = desc;
357 int ircd_connector::GetDescriptor()
362 int ircd_connector::GetState()
368 void ircd_connector::SetState(int state)
371 if (state == STATE_DISCONNECTED)
373 NetSendMyRoutingTable();
377 void ircd_connector::CloseConnection()
379 int flags = fcntl(this->fd, F_GETFL, 0);
380 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
382 flags = fcntl(this->fd, F_GETFL, 0);
383 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
386 void ircd_connector::SetDescriptor(int fd)
391 bool connection::SendPacket(char *message, const char* host)
393 if ((!message) || (!host))
396 ircd_connector* cn = this->FindHost(host);
398 if (!strchr(message,'\n'))
400 strlcat(message,"\n",MAXBUF);
405 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
407 if (cn->GetState() == STATE_DISCONNECTED)
409 log(DEBUG,"Main route to %s is down, seeking alternative",host);
410 // fix: can only route one hop to avoid a loop
411 if (strlcat(message,"R ",2))
413 // this route is down, we must re-route the packet through an available point in the mesh.
414 for (int k = 0; k < this->connectors.size(); k++)
416 // search for another point in the mesh which can 'reach' where we want to go
417 for (int m = 0; m < this->connectors[k].routes.size(); m++)
419 if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
421 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
423 snprintf(buffer,MAXBUF,"R %s %s",host,message);
424 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
431 snprintf(buffer,MAXBUF,"& %s",host);
432 NetSendToAllExcept(host,buffer);
433 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
438 // returns false if the packet could not be sent (e.g. target host down)
439 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
441 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
442 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
443 cn->CloseConnection();
444 cn->SetState(STATE_DISCONNECTED);
445 // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
446 return this->SendPacket(message,host);
452 // receives a packet from any where there is data waiting, first come, first served
453 // fills the message and host values with the host where the data came from.
455 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
458 memset(data, 0, 4096);
459 for (int i = 0; i < this->connectors.size(); i++)
461 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
463 // returns false if the packet could not be sent (e.g. target host down)
465 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32,0);
466 data[rcvsize] == '\0';
471 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
472 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
473 this->connectors[i].CloseConnection();
474 this->connectors[i].SetState(STATE_DISCONNECTED);
479 this->connectors[i].AddBuffer(data);
480 if (this->connectors[i].BufferIsComplete())
482 messages.push_back(this->connectors[i].GetBuffer().c_str());
483 strlcpy(host,this->connectors[i].GetServerName().c_str(),160);
484 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",this->connectors[i].GetBuffer().c_str(),host);
485 this->connectors[i].ClearBuffer();
491 // nothing new yet -- message and host will be undefined
495 long connection::GenKey()
497 return (random()*time(NULL));