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 ircdbuffer = stream.str();
153 bool ircd_connector::BufferIsComplete()
155 for (int i = 0; i < ircdbuffer.length(); i++)
156 if (ircdbuffer[i] == '\n')
161 void ircd_connector::ClearBuffer()
166 std::string ircd_connector::GetBuffer()
168 char* line = (char*)ircdbuffer.c_str();
170 std::stringstream* stream = new std::stringstream(std::stringstream::in | std::stringstream::out);
171 *stream << ircdbuffer;
172 stream->getline(ret,MAXBUF);
173 while ((*line != '\n') && (strlen(line)))
175 if ((*line == '\n') || (*line == '\r'))
182 bool ircd_connector::MakeOutboundConnection(char* newhost, int newport)
184 log(DEBUG,"MakeOutboundConnection: Original param: %s",newhost);
186 hostent* hoste = gethostbyname(newhost);
189 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",newhost);
190 this->SetHostAddress(newhost,newport);
191 SetHostAndPort(newhost,newport);
195 struct in_addr* ia = (in_addr*)hoste->h_addr;
196 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
197 this->SetHostAddress(inet_ntoa(*ia),newport);
198 SetHostAndPort(inet_ntoa(*ia),newport);
201 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
204 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
206 WriteOpers("connect() failed for %s",host);
207 RemoveServer(this->servername.c_str());
210 int flags = fcntl(this->fd, F_GETFL, 0);
211 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
214 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
215 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
220 WriteOpers("socket() failed!");
221 RemoveServer(this->servername.c_str());
228 bool connection::BeginLink(char* targethost, int newport, char* password, char* servername, int myport)
230 char connect[MAXBUF];
232 ircd_connector connector;
233 ircd_connector *cn = this->FindHost(servername);
238 WriteOpers("CONNECT aborted: Server %s already exists",servername);
245 if (connector.MakeOutboundConnection(targethost,newport))
247 // targethost has been turned into an ip...
248 // we dont want this as the server name.
249 connector.SetServerName(servername);
250 snprintf(connect,MAXBUF,"S %s %s %lu %lu :%s",getservername().c_str(),password,(unsigned long)myport,(unsigned long)GetRevision(),getserverdesc().c_str());
251 connector.SetState(STATE_NOAUTH_OUTBOUND);
252 connector.SetHostAndPort(targethost, newport);
253 this->connectors.push_back(connector);
254 return this->SendPacket(connect, servername);
258 connector.SetState(STATE_DISCONNECTED);
259 WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
265 bool connection::MeshCookie(char* targethost, int newport, unsigned long cookie, char* servername)
267 char connect[MAXBUF];
269 ircd_connector connector;
271 WriteOpers("Establishing meshed link to %s:%d",servername,newport);
275 if (connector.MakeOutboundConnection(targethost,newport))
277 // targethost has been turned into an ip...
278 // we dont want this as the server name.
279 connector.SetServerName(servername);
280 snprintf(connect,MAXBUF,"- %lu %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
281 connector.SetState(STATE_NOAUTH_OUTBOUND);
282 connector.SetHostAndPort(targethost, newport);
283 connector.SetState(STATE_CONNECTED);
284 this->connectors.push_back(connector);
285 return this->SendPacket(connect, servername);
289 connector.SetState(STATE_DISCONNECTED);
290 WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
296 bool connection::AddIncoming(int newfd, char* targethost, int sourceport)
298 ircd_connector connector;
300 // targethost has been turned into an ip...
301 // we dont want this as the server name.
302 connector.SetServerName(targethost);
303 connector.SetDescriptor(newfd);
304 connector.SetState(STATE_NOAUTH_INBOUND);
305 int flags = fcntl(newfd, F_GETFL, 0);
306 fcntl(newfd, F_SETFL, flags | O_NONBLOCK);
309 setsockopt(newfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
310 setsockopt(newfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
311 connector.SetHostAndPort(targethost, sourceport);
312 connector.SetState(STATE_NOAUTH_INBOUND);
313 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
314 this->connectors.push_back(connector);
318 void connection::TerminateLink(char* targethost)
320 // this locates the targethost in the connection::connectors vector of the class,
321 // and terminates it by sending it an SQUIT token and closing its descriptor.
322 // TerminateLink with a null string causes a terminate of ALL links
326 // Returns a pointer to the connector for 'host'
327 ircd_connector* connection::FindHost(std::string findhost)
329 for (int i = 0; i < this->connectors.size(); i++)
331 if (this->connectors[i].GetServerName() == findhost)
333 return &this->connectors[i];
339 std::string ircd_connector::GetServerName()
341 return this->servername;
344 std::string ircd_connector::GetDescription()
346 return this->description;
349 void ircd_connector::SetServerName(std::string serv)
351 this->servername = serv;
354 void ircd_connector::SetDescription(std::string desc)
356 this->description = desc;
360 int ircd_connector::GetDescriptor()
365 int ircd_connector::GetState()
371 void ircd_connector::SetState(int newstate)
373 this->state = newstate;
374 if (state == STATE_DISCONNECTED)
376 NetSendMyRoutingTable();
380 void ircd_connector::CloseConnection()
382 int flags = fcntl(this->fd, F_GETFL, 0);
383 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
385 flags = fcntl(this->fd, F_GETFL, 0);
386 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
389 void ircd_connector::SetDescriptor(int newfd)
394 bool connection::SendPacket(char *message, const char* sendhost)
396 if ((!message) || (!sendhost))
399 ircd_connector* cn = this->FindHost(sendhost);
401 if (!strchr(message,'\n'))
403 strlcat(message,"\n",MAXBUF);
408 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
410 if (cn->GetState() == STATE_DISCONNECTED)
412 log(DEBUG,"Main route to %s is down, seeking alternative",host);
413 // fix: can only route one hop to avoid a loop
414 if (!strncmp(message,"R ",2))
416 // this route is down, we must re-route the packet through an available point in the mesh.
417 for (int k = 0; k < this->connectors.size(); k++)
419 // search for another point in the mesh which can 'reach' where we want to go
420 for (int m = 0; m < this->connectors[k].routes.size(); m++)
422 if (!strcasecmp(this->connectors[k].routes[m].c_str(),sendhost))
424 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
426 snprintf(buffer,MAXBUF,"R %s %s",sendhost,message);
427 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
434 snprintf(buffer,MAXBUF,"& %s",sendhost);
435 NetSendToAllExcept(sendhost,buffer);
436 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",sendhost);
441 // returns false if the packet could not be sent (e.g. target host down)
442 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
444 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
445 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
446 cn->CloseConnection();
447 cn->SetState(STATE_DISCONNECTED);
448 // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
449 return this->SendPacket(message,sendhost);
455 // receives a packet from any where there is data waiting, first come, first served
456 // fills the message and host values with the host where the data came from.
458 bool connection::RecvPacket(std::deque<std::string> &messages, char* recvhost)
461 memset(data, 0, 4096);
462 for (int i = 0; i < this->connectors.size(); i++)
464 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
466 // returns false if the packet could not be sent (e.g. target host down)
468 rcvsize = recv(this->connectors[i].GetDescriptor(),data,4096,0);
469 data[rcvsize] = '\0';
474 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
475 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
476 this->connectors[i].CloseConnection();
477 this->connectors[i].SetState(STATE_DISCONNECTED);
483 this->connectors[i].AddBuffer(data);
484 if (this->connectors[i].BufferIsComplete())
486 while (this->connectors[i].BufferIsComplete())
488 std::string text = this->connectors[i].GetBuffer();
489 messages.push_back(text.c_str());
490 strlcpy(recvhost,this->connectors[i].GetServerName().c_str(),160);
491 log(DEBUG,"main: Connection::RecvPacket() %d:%s->%s",pushed++,recvhost,text.c_str());
498 // nothing new yet -- message and host will be undefined
502 long connection::GenKey()
504 return (random()*time(NULL));