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 // Fix by Brain 28th Apr 2005
170 // seems my stringstream code isnt liked by linux
171 // EVEN THOUGH IT IS CORRECT! Fixed by using a different
172 // (SLOWER) algorithm...
173 char* line = (char*)ircdbuffer.c_str();
174 std::string ret = "";
175 while ((*line != '\n') && (strlen(line)))
180 if ((*line == '\n') || (*line == '\r'))
186 bool ircd_connector::MakeOutboundConnection(char* newhost, int newport)
188 log(DEBUG,"MakeOutboundConnection: Original param: %s",newhost);
190 hostent* hoste = gethostbyname(newhost);
193 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",newhost);
194 this->SetHostAddress(newhost,newport);
195 SetHostAndPort(newhost,newport);
199 struct in_addr* ia = (in_addr*)hoste->h_addr;
200 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
201 this->SetHostAddress(inet_ntoa(*ia),newport);
202 SetHostAndPort(inet_ntoa(*ia),newport);
205 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
208 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
210 WriteOpers("connect() failed for %s",host);
211 RemoveServer(this->servername.c_str());
214 int flags = fcntl(this->fd, F_GETFL, 0);
215 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
218 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
219 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
224 WriteOpers("socket() failed!");
225 RemoveServer(this->servername.c_str());
232 bool connection::BeginLink(char* targethost, int newport, char* password, char* servername, int myport)
234 char connect[MAXBUF];
236 ircd_connector connector;
237 ircd_connector *cn = this->FindHost(servername);
242 WriteOpers("CONNECT aborted: Server %s already exists",servername);
249 if (connector.MakeOutboundConnection(targethost,newport))
251 // targethost has been turned into an ip...
252 // we dont want this as the server name.
253 connector.SetServerName(servername);
254 snprintf(connect,MAXBUF,"S %s %s %lu %lu :%s",getservername().c_str(),password,(unsigned long)myport,(unsigned long)GetRevision(),getserverdesc().c_str());
255 connector.SetState(STATE_NOAUTH_OUTBOUND);
256 connector.SetHostAndPort(targethost, newport);
257 this->connectors.push_back(connector);
258 return this->SendPacket(connect, servername);
262 connector.SetState(STATE_DISCONNECTED);
263 WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
269 void ircd_connector::SetVersionString(std::string newversion)
271 log(DEBUG,"Set version of %s to %s",this->servername.c_str(),newversion.c_str());
272 this->version = newversion;
275 std::string ircd_connector::GetVersionString()
277 if (this->version == "")
279 return "(No version available for "+this->servername+")";
281 else return this->version;
284 bool connection::MeshCookie(char* targethost, int newport, unsigned long cookie, char* servername)
286 char connect[MAXBUF];
288 ircd_connector connector;
290 WriteOpers("Establishing meshed link to %s:%d",servername,newport);
294 if (connector.MakeOutboundConnection(targethost,newport))
296 // targethost has been turned into an ip...
297 // we dont want this as the server name.
298 connector.SetServerName(servername);
299 snprintf(connect,MAXBUF,"- %lu %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
300 connector.SetState(STATE_NOAUTH_OUTBOUND);
301 connector.SetHostAndPort(targethost, newport);
302 connector.SetState(STATE_CONNECTED);
303 this->connectors.push_back(connector);
304 return this->SendPacket(connect, servername);
308 connector.SetState(STATE_DISCONNECTED);
309 WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
315 bool connection::AddIncoming(int newfd, char* targethost, int sourceport)
317 ircd_connector connector;
319 // targethost has been turned into an ip...
320 // we dont want this as the server name.
321 connector.SetServerName(targethost);
322 connector.SetDescriptor(newfd);
323 connector.SetState(STATE_NOAUTH_INBOUND);
324 int flags = fcntl(newfd, F_GETFL, 0);
325 fcntl(newfd, F_SETFL, flags | O_NONBLOCK);
328 setsockopt(newfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
329 setsockopt(newfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
330 connector.SetHostAndPort(targethost, sourceport);
331 connector.SetState(STATE_NOAUTH_INBOUND);
332 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
333 this->connectors.push_back(connector);
337 void connection::TerminateLink(char* targethost)
339 // this locates the targethost in the connection::connectors vector of the class,
340 // and terminates it by sending it an SQUIT token and closing its descriptor.
341 // TerminateLink with a null string causes a terminate of ALL links
345 // Returns a pointer to the connector for 'host'
346 ircd_connector* connection::FindHost(std::string findhost)
348 for (int i = 0; i < this->connectors.size(); i++)
350 if (this->connectors[i].GetServerName() == findhost)
352 return &this->connectors[i];
358 std::string ircd_connector::GetServerName()
360 return this->servername;
363 std::string ircd_connector::GetDescription()
365 return this->description;
368 void ircd_connector::SetServerName(std::string serv)
370 this->servername = serv;
373 void ircd_connector::SetDescription(std::string desc)
375 this->description = desc;
379 int ircd_connector::GetDescriptor()
384 int ircd_connector::GetState()
390 void ircd_connector::SetState(int newstate)
392 this->state = newstate;
393 if (state == STATE_DISCONNECTED)
395 NetSendMyRoutingTable();
399 void ircd_connector::CloseConnection()
401 int flags = fcntl(this->fd, F_GETFL, 0);
402 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
404 flags = fcntl(this->fd, F_GETFL, 0);
405 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
408 void ircd_connector::SetDescriptor(int newfd)
413 bool connection::SendPacket(char *message, const char* sendhost)
415 if ((!message) || (!sendhost))
418 ircd_connector* cn = this->FindHost(sendhost);
420 if (!strchr(message,'\n'))
422 strlcat(message,"\n",MAXBUF);
427 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
429 if (cn->GetState() == STATE_DISCONNECTED)
431 log(DEBUG,"\n\n\n\nMain route to %s is down, seeking alternative\n\n\n\n",sendhost);
432 // fix: can only route one hop to avoid a loop
433 if (strncmp(message,"R ",2))
435 log(DEBUG,"Not a double reroute");
436 // this route is down, we must re-route the packet through an available point in the mesh.
437 for (int k = 0; k < this->connectors.size(); k++)
439 log(DEBUG,"Check connector %d: %s",k,this->connectors[k].GetServerName().c_str());
440 // search for another point in the mesh which can 'reach' where we want to go
441 for (int m = 0; m < this->connectors[k].routes.size(); m++)
443 log(DEBUG,"Check connector %d: %s route %s",k,this->connectors[k].GetServerName().c_str(),this->connectors[k].routes[m].c_str());
444 if (!strcasecmp(this->connectors[k].routes[m].c_str(),sendhost))
446 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
448 snprintf(buffer,MAXBUF,"R %s %s",sendhost,message);
449 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
456 snprintf(buffer,MAXBUF,"& %s",sendhost);
457 NetSendToAllExcept(sendhost,buffer);
458 log(DEBUG,"\n\nThere are no routes to %s, we're gonna boot the server off!\n\n",sendhost);
463 // returns false if the packet could not be sent (e.g. target host down)
464 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
466 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
467 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
468 cn->CloseConnection();
469 cn->SetState(STATE_DISCONNECTED);
470 // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
471 return this->SendPacket(message,sendhost);
477 // receives a packet from any where there is data waiting, first come, first served
478 // fills the message and host values with the host where the data came from.
480 bool connection::RecvPacket(std::deque<std::string> &messages, char* recvhost)
483 memset(data, 0, 4096);
484 for (int i = 0; i < this->connectors.size(); i++)
486 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
488 // returns false if the packet could not be sent (e.g. target host down)
491 // check if theres any data on this socket
492 // if not, continue onwards to the next.
494 polls.fd = this->connectors[i].GetDescriptor();
495 polls.events = POLLIN;
496 int ret = poll(&polls,1,1);
497 if (ret <= 0) continue;
499 rcvsize = recv(this->connectors[i].GetDescriptor(),data,4096,0);
500 data[rcvsize] = '\0';
505 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
506 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
507 this->connectors[i].CloseConnection();
508 this->connectors[i].SetState(STATE_DISCONNECTED);
514 this->connectors[i].AddBuffer(data);
515 if (this->connectors[i].BufferIsComplete())
517 while (this->connectors[i].BufferIsComplete())
519 std::string text = this->connectors[i].GetBuffer();
522 messages.push_back(text.c_str());
523 strlcpy(recvhost,this->connectors[i].GetServerName().c_str(),160);
524 log(DEBUG,"main: Connection::RecvPacket() %d:%s->%s",pushed++,recvhost,text.c_str());
532 // nothing new yet -- message and host will be undefined
536 long connection::GenKey()
538 return (random()*time(NULL));