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* newhost, 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;
79 if (!strcmp(newhost,""))
81 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
85 inet_aton(newhost,&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* newhost, int newport)
119 strncpy(this->host,newhost,160);
120 this->port = newport;
124 bool ircd_connector::SetHostAddress(char* newhost, int newport)
126 strncpy(this->host,newhost,160);
127 this->port = newport;
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* newhost, int newport)
179 log(DEBUG,"MakeOutboundConnection: Original param: %s",newhost);
181 hostent* hoste = gethostbyname(newhost);
184 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",newhost);
185 this->SetHostAddress(newhost,newport);
186 SetHostAndPort(newhost,newport);
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),newport);
193 SetHostAndPort(inet_ntoa(*ia),newport);
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 newport, 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,newport))
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 %lu %lu :%s",getservername().c_str(),password,(unsigned long)myport,(unsigned long)GetRevision(),getserverdesc().c_str());
246 connector.SetState(STATE_NOAUTH_OUTBOUND);
247 connector.SetHostAndPort(targethost, newport);
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,newport);
260 bool connection::MeshCookie(char* targethost, int newport, unsigned long cookie, char* servername)
262 char connect[MAXBUF];
264 ircd_connector connector;
266 WriteOpers("Establishing meshed link to %s:%d",servername,newport);
270 if (connector.MakeOutboundConnection(targethost,newport))
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,"- %lu %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
276 connector.SetState(STATE_NOAUTH_OUTBOUND);
277 connector.SetHostAndPort(targethost, newport);
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,newport);
291 bool connection::AddIncoming(int newfd, char* targethost, int sourceport)
293 ircd_connector connector;
295 // targethost has been turned into an ip...
296 // we dont want this as the server name.
297 connector.SetServerName(targethost);
298 connector.SetDescriptor(newfd);
299 connector.SetState(STATE_NOAUTH_INBOUND);
300 int flags = fcntl(newfd, F_GETFL, 0);
301 fcntl(newfd, F_SETFL, flags | O_NONBLOCK);
304 setsockopt(newfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
305 setsockopt(newfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
306 connector.SetHostAndPort(targethost, sourceport);
307 connector.SetState(STATE_NOAUTH_INBOUND);
308 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
309 this->connectors.push_back(connector);
313 void connection::TerminateLink(char* targethost)
315 // this locates the targethost in the connection::connectors vector of the class,
316 // and terminates it by sending it an SQUIT token and closing its descriptor.
317 // TerminateLink with a null string causes a terminate of ALL links
321 // Returns a pointer to the connector for 'host'
322 ircd_connector* connection::FindHost(std::string findhost)
324 for (int i = 0; i < this->connectors.size(); i++)
326 if (this->connectors[i].GetServerName() == findhost)
328 return &this->connectors[i];
334 std::string ircd_connector::GetServerName()
336 return this->servername;
339 std::string ircd_connector::GetDescription()
341 return this->description;
344 void ircd_connector::SetServerName(std::string serv)
346 this->servername = serv;
349 void ircd_connector::SetDescription(std::string desc)
351 this->description = desc;
355 int ircd_connector::GetDescriptor()
360 int ircd_connector::GetState()
366 void ircd_connector::SetState(int newstate)
368 this->state = newstate;
369 if (state == STATE_DISCONNECTED)
371 NetSendMyRoutingTable();
375 void ircd_connector::CloseConnection()
377 int flags = fcntl(this->fd, F_GETFL, 0);
378 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
380 flags = fcntl(this->fd, F_GETFL, 0);
381 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
384 void ircd_connector::SetDescriptor(int newfd)
389 bool connection::SendPacket(char *message, const char* sendhost)
391 if ((!message) || (!sendhost))
394 ircd_connector* cn = this->FindHost(sendhost);
396 if (!strchr(message,'\n'))
398 strlcat(message,"\n",MAXBUF);
403 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
405 if (cn->GetState() == STATE_DISCONNECTED)
407 log(DEBUG,"Main route to %s is down, seeking alternative",host);
408 // fix: can only route one hop to avoid a loop
409 if (strncmp(message,"R ",2))
411 // this route is down, we must re-route the packet through an available point in the mesh.
412 for (int k = 0; k < this->connectors.size(); k++)
414 // search for another point in the mesh which can 'reach' where we want to go
415 for (int m = 0; m < this->connectors[k].routes.size(); m++)
417 if (!strcasecmp(this->connectors[k].routes[m].c_str(),sendhost))
419 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
421 snprintf(buffer,MAXBUF,"R %s %s",sendhost,message);
422 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
429 snprintf(buffer,MAXBUF,"& %s",sendhost);
430 NetSendToAllExcept(sendhost,buffer);
431 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",sendhost);
436 // returns false if the packet could not be sent (e.g. target host down)
437 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
439 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
440 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
441 cn->CloseConnection();
442 cn->SetState(STATE_DISCONNECTED);
443 // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
444 return this->SendPacket(message,sendhost);
450 // receives a packet from any where there is data waiting, first come, first served
451 // fills the message and host values with the host where the data came from.
453 bool connection::RecvPacket(std::deque<std::string> &messages, char* recvhost)
456 memset(data, 0, 4096);
457 for (int i = 0; i < this->connectors.size(); i++)
459 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
461 // returns false if the packet could not be sent (e.g. target host down)
463 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32,0);
464 data[rcvsize] = '\0';
469 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
470 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
471 this->connectors[i].CloseConnection();
472 this->connectors[i].SetState(STATE_DISCONNECTED);
477 this->connectors[i].AddBuffer(data);
478 if (this->connectors[i].BufferIsComplete())
480 messages.push_back(this->connectors[i].GetBuffer().c_str());
481 strlcpy(host,this->connectors[i].GetServerName().c_str(),160);
482 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",this->connectors[i].GetBuffer().c_str(),recvhost);
483 this->connectors[i].ClearBuffer();
489 // nothing new yet -- message and host will be undefined
493 long connection::GenKey()
495 return (random()*time(NULL));