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>
30 #include "inspstring.h"
35 extern std::vector<Module*> modules;
36 extern std::vector<ircd_module*> factory;
42 connection::connection()
48 bool connection::CreateListener(char* newhost, int p)
50 sockaddr_in host_address;
54 struct linger linger = { 0 };
58 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
64 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
67 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
69 // attempt to increase socket sendq and recvq as high as its possible
70 // to get them on linux.
73 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
74 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
76 memset((void*)&host_address, 0, sizeof(host_address));
78 host_address.sin_family = AF_INET;
80 if (!strcmp(newhost,""))
82 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
86 inet_aton(newhost,&addy);
87 host_address.sin_addr = addy;
90 host_address.sin_port = htons(p);
92 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
97 // make the socket non-blocking
98 flags = fcntl(fd, F_GETFL, 0);
99 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
108 char* ircd_connector::GetServerIP()
113 int ircd_connector::GetServerPort()
118 bool ircd_connector::SetHostAndPort(char* newhost, int newport)
120 strncpy(this->host,newhost,160);
121 this->port = newport;
125 bool ircd_connector::SetHostAddress(char* newhost, int newport)
127 strncpy(this->host,newhost,160);
128 this->port = newport;
129 memset((void*)&addr, 0, sizeof(addr));
130 addr.sin_family = AF_INET;
131 inet_aton(host,&addr.sin_addr);
132 addr.sin_port = htons(port);
136 void ircd_connector::SetServerPort(int p)
141 void ircd_connector::AddBuffer(std::string a)
144 for (int i = 0; i < a.length(); i++)
148 std::stringstream stream(ircdbuffer);
150 log(DEBUG,"AddBuffer: %s",b.c_str());
151 ircdbuffer = stream.str();
154 bool ircd_connector::BufferIsComplete()
156 for (int i = 0; i < ircdbuffer.length(); i++)
157 if (ircdbuffer[i] == '\n')
162 void ircd_connector::ClearBuffer()
167 std::string ircd_connector::GetBuffer()
169 char* line = (char*)ircdbuffer.c_str();
171 std::stringstream* stream = new std::stringstream(std::stringstream::in | std::stringstream::out);
172 *stream << ircdbuffer;
173 stream->getline(ret,MAXBUF);
174 while ((*line != '\n') && (strlen(line)))
176 if ((*line == '\n') || (*line == '\r'))
183 bool ircd_connector::MakeOutboundConnection(char* newhost, int newport)
185 log(DEBUG,"MakeOutboundConnection: Original param: %s",newhost);
187 hostent* hoste = gethostbyname(newhost);
190 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",newhost);
191 this->SetHostAddress(newhost,newport);
192 SetHostAndPort(newhost,newport);
196 struct in_addr* ia = (in_addr*)hoste->h_addr;
197 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
198 this->SetHostAddress(inet_ntoa(*ia),newport);
199 SetHostAndPort(inet_ntoa(*ia),newport);
202 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
205 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
207 WriteOpers("connect() failed for %s",host);
208 RemoveServer(this->servername.c_str());
211 int flags = fcntl(this->fd, F_GETFL, 0);
212 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
215 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
216 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
221 WriteOpers("socket() failed!");
222 RemoveServer(this->servername.c_str());
229 bool connection::BeginLink(char* targethost, int newport, char* password, char* servername, int myport)
231 char connect[MAXBUF];
233 ircd_connector connector;
234 ircd_connector *cn = this->FindHost(servername);
239 WriteOpers("CONNECT aborted: Server %s already exists",servername);
246 if (connector.MakeOutboundConnection(targethost,newport))
248 // targethost has been turned into an ip...
249 // we dont want this as the server name.
250 connector.SetServerName(servername);
251 snprintf(connect,MAXBUF,"S %s %s %lu %lu :%s",getservername().c_str(),password,(unsigned long)myport,(unsigned long)GetRevision(),getserverdesc().c_str());
252 connector.SetState(STATE_NOAUTH_OUTBOUND);
253 connector.SetHostAndPort(targethost, newport);
254 this->connectors.push_back(connector);
255 return this->SendPacket(connect, servername);
259 connector.SetState(STATE_DISCONNECTED);
260 WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
266 bool connection::MeshCookie(char* targethost, int newport, unsigned long cookie, char* servername)
268 char connect[MAXBUF];
270 ircd_connector connector;
272 WriteOpers("Establishing meshed link to %s:%d",servername,newport);
276 if (connector.MakeOutboundConnection(targethost,newport))
278 // targethost has been turned into an ip...
279 // we dont want this as the server name.
280 connector.SetServerName(servername);
281 snprintf(connect,MAXBUF,"- %lu %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
282 connector.SetState(STATE_NOAUTH_OUTBOUND);
283 connector.SetHostAndPort(targethost, newport);
284 connector.SetState(STATE_CONNECTED);
285 this->connectors.push_back(connector);
286 return this->SendPacket(connect, servername);
290 connector.SetState(STATE_DISCONNECTED);
291 WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
297 bool connection::AddIncoming(int newfd, char* targethost, int sourceport)
299 ircd_connector connector;
301 // targethost has been turned into an ip...
302 // we dont want this as the server name.
303 connector.SetServerName(targethost);
304 connector.SetDescriptor(newfd);
305 connector.SetState(STATE_NOAUTH_INBOUND);
306 int flags = fcntl(newfd, F_GETFL, 0);
307 fcntl(newfd, F_SETFL, flags | O_NONBLOCK);
310 setsockopt(newfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
311 setsockopt(newfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
312 connector.SetHostAndPort(targethost, sourceport);
313 connector.SetState(STATE_NOAUTH_INBOUND);
314 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
315 this->connectors.push_back(connector);
319 void connection::TerminateLink(char* targethost)
321 // this locates the targethost in the connection::connectors vector of the class,
322 // and terminates it by sending it an SQUIT token and closing its descriptor.
323 // TerminateLink with a null string causes a terminate of ALL links
327 // Returns a pointer to the connector for 'host'
328 ircd_connector* connection::FindHost(std::string findhost)
330 for (int i = 0; i < this->connectors.size(); i++)
332 if (this->connectors[i].GetServerName() == findhost)
334 return &this->connectors[i];
340 std::string ircd_connector::GetServerName()
342 return this->servername;
345 std::string ircd_connector::GetDescription()
347 return this->description;
350 void ircd_connector::SetServerName(std::string serv)
352 this->servername = serv;
355 void ircd_connector::SetDescription(std::string desc)
357 this->description = desc;
361 int ircd_connector::GetDescriptor()
366 int ircd_connector::GetState()
372 void ircd_connector::SetState(int newstate)
374 this->state = newstate;
375 if (state == STATE_DISCONNECTED)
377 NetSendMyRoutingTable();
381 void ircd_connector::CloseConnection()
383 int flags = fcntl(this->fd, F_GETFL, 0);
384 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
386 flags = fcntl(this->fd, F_GETFL, 0);
387 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
390 void ircd_connector::SetDescriptor(int newfd)
395 bool connection::SendPacket(char *message, const char* sendhost)
397 if ((!message) || (!sendhost))
400 ircd_connector* cn = this->FindHost(sendhost);
402 if (!strchr(message,'\n'))
404 strlcat(message,"\n",MAXBUF);
409 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
411 if (cn->GetState() == STATE_DISCONNECTED)
413 log(DEBUG,"Main route to %s is down, seeking alternative",host);
414 // fix: can only route one hop to avoid a loop
415 if (!strncmp(message,"R ",2))
417 // this route is down, we must re-route the packet through an available point in the mesh.
418 for (int k = 0; k < this->connectors.size(); k++)
420 // search for another point in the mesh which can 'reach' where we want to go
421 for (int m = 0; m < this->connectors[k].routes.size(); m++)
423 if (!strcasecmp(this->connectors[k].routes[m].c_str(),sendhost))
425 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
427 snprintf(buffer,MAXBUF,"R %s %s",sendhost,message);
428 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
435 snprintf(buffer,MAXBUF,"& %s",sendhost);
436 NetSendToAllExcept(sendhost,buffer);
437 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",sendhost);
442 // returns false if the packet could not be sent (e.g. target host down)
443 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
445 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
446 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
447 cn->CloseConnection();
448 cn->SetState(STATE_DISCONNECTED);
449 // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
450 return this->SendPacket(message,sendhost);
456 // receives a packet from any where there is data waiting, first come, first served
457 // fills the message and host values with the host where the data came from.
459 bool connection::RecvPacket(std::deque<std::string> &messages, char* recvhost)
462 memset(data, 0, 4096);
463 for (int i = 0; i < this->connectors.size(); i++)
465 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
467 // returns false if the packet could not be sent (e.g. target host down)
469 rcvsize = recv(this->connectors[i].GetDescriptor(),data,4096,0);
470 data[rcvsize] = '\0';
475 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
476 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
477 this->connectors[i].CloseConnection();
478 this->connectors[i].SetState(STATE_DISCONNECTED);
484 this->connectors[i].AddBuffer(data);
485 if (this->connectors[i].BufferIsComplete())
487 while (this->connectors[i].BufferIsComplete())
489 std::string text = this->connectors[i].GetBuffer();
490 messages.push_back(text.c_str());
491 strlcpy(recvhost,this->connectors[i].GetServerName().c_str(),160);
492 log(DEBUG,"main: Connection::RecvPacket() %d:%s->%s",pushed++,recvhost,text.c_str());
499 // nothing new yet -- message and host will be undefined
503 long connection::GenKey()
505 return (random()*time(NULL));