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>
20 #include <sys/errno.h>
21 #include <sys/ioctl.h>
22 #include <sys/utsname.h>
32 extern std::vector<Module*> modules;
33 extern std::vector<ircd_module*> factory;
39 connection::connection()
45 bool connection::CreateListener(char* host, int p)
47 sockaddr_in host_address;
51 struct linger linger = { 0 };
55 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
61 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
64 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
66 // attempt to increase socket sendq and recvq as high as its possible
67 // to get them on linux.
70 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
71 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
73 memset((void*)&host_address, 0, sizeof(host_address));
75 host_address.sin_family = AF_INET;
79 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
83 inet_aton(host,&addy);
84 host_address.sin_addr = addy;
87 host_address.sin_port = htons(p);
89 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
94 // make the socket non-blocking
95 flags = fcntl(fd, F_GETFL, 0);
96 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
105 char* ircd_connector::GetServerIP()
110 int ircd_connector::GetServerPort()
115 bool ircd_connector::SetHostAndPort(char* host, int port)
117 strncpy(this->host,host,160);
122 bool ircd_connector::SetHostAddress(char* host, int port)
124 strncpy(this->host,host,160);
126 memset((void*)&addr, 0, sizeof(addr));
127 addr.sin_family = AF_INET;
128 inet_aton(host,&addr.sin_addr);
129 addr.sin_port = htons(port);
133 void ircd_connector::SetServerPort(int p)
138 bool ircd_connector::MakeOutboundConnection(char* host, int port)
140 log(DEBUG,"MakeOutboundConnection: Original param: %s",host);
141 hostent* hoste = gethostbyname(host);
144 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",host);
145 this->SetHostAddress(host,port);
146 SetHostAndPort(host,port);
150 struct in_addr* ia = (in_addr*)hoste->h_addr;
151 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
152 this->SetHostAddress(inet_ntoa(*ia),port);
153 SetHostAndPort(inet_ntoa(*ia),port);
156 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
159 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
161 WriteOpers("connect() failed for %s",host);
162 RemoveServer(this->servername.c_str());
165 int flags = fcntl(this->fd, F_GETFL, 0);
166 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
169 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
170 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
175 WriteOpers("socket() failed!");
176 RemoveServer(this->servername.c_str());
183 bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
185 char connect[MAXBUF];
187 ircd_connector connector;
188 ircd_connector *cn = this->FindHost(servername);
193 WriteOpers("CONNECT aborted: Server %s already exists",servername);
200 if (connector.MakeOutboundConnection(targethost,port))
202 // targethost has been turned into an ip...
203 // we dont want this as the server name.
204 connector.SetServerName(servername);
205 snprintf(connect,MAXBUF,"S %s %s %d %d :%s",getservername().c_str(),password,myport,GetRevision(),getserverdesc().c_str());
206 connector.SetState(STATE_NOAUTH_OUTBOUND);
207 connector.SetHostAndPort(targethost, port);
208 this->connectors.push_back(connector);
209 return this->SendPacket(connect, servername);
213 connector.SetState(STATE_DISCONNECTED);
214 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
220 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
222 char connect[MAXBUF];
224 ircd_connector connector;
226 WriteOpers("Establishing meshed link to %s:%d",servername,port);
230 if (connector.MakeOutboundConnection(targethost,port))
232 // targethost has been turned into an ip...
233 // we dont want this as the server name.
234 connector.SetServerName(servername);
235 snprintf(connect,MAXBUF,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
236 connector.SetState(STATE_NOAUTH_OUTBOUND);
237 connector.SetHostAndPort(targethost, port);
238 connector.SetState(STATE_CONNECTED);
239 this->connectors.push_back(connector);
240 return this->SendPacket(connect, servername);
244 connector.SetState(STATE_DISCONNECTED);
245 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
251 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
253 char connect[MAXBUF];
255 ircd_connector connector;
257 // targethost has been turned into an ip...
258 // we dont want this as the server name.
259 connector.SetServerName(targethost);
260 connector.SetDescriptor(fd);
261 connector.SetState(STATE_NOAUTH_INBOUND);
262 int flags = fcntl(fd, F_GETFL, 0);
263 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
266 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
267 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
268 connector.SetHostAndPort(targethost, sourceport);
269 connector.SetState(STATE_NOAUTH_INBOUND);
270 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
271 this->connectors.push_back(connector);
275 void connection::TerminateLink(char* targethost)
277 // this locates the targethost in the connection::connectors vector of the class,
278 // and terminates it by sending it an SQUIT token and closing its descriptor.
279 // TerminateLink with a null string causes a terminate of ALL links
283 // Returns a pointer to the connector for 'host'
284 ircd_connector* connection::FindHost(std::string host)
286 for (int i = 0; i < this->connectors.size(); i++)
288 if (this->connectors[i].GetServerName() == host)
290 return &this->connectors[i];
296 std::string ircd_connector::GetServerName()
298 return this->servername;
301 std::string ircd_connector::GetDescription()
303 return this->description;
306 void ircd_connector::SetServerName(std::string serv)
308 this->servername = serv;
311 void ircd_connector::SetDescription(std::string desc)
313 this->description = desc;
317 int ircd_connector::GetDescriptor()
322 int ircd_connector::GetState()
328 void ircd_connector::SetState(int state)
331 if (state == STATE_DISCONNECTED)
333 NetSendMyRoutingTable();
337 void ircd_connector::CloseConnection()
339 int flags = fcntl(this->fd, F_GETFL, 0);
340 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
342 flags = fcntl(this->fd, F_GETFL, 0);
343 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
346 void ircd_connector::SetDescriptor(int fd)
351 bool connection::SendPacket(char *message, const char* host)
353 if ((!message) || (!host))
356 ircd_connector* cn = this->FindHost(host);
358 if (!strchr(message,'\n'))
360 strlcat(message,"\n",MAXBUF);
365 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
367 if (cn->GetState() == STATE_DISCONNECTED)
369 log(DEBUG,"Main route to %s is down, seeking alternative",host);
370 // fix: can only route one hop to avoid a loop
371 if (strlcat(message,"R ",2))
373 // this route is down, we must re-route the packet through an available point in the mesh.
374 for (int k = 0; k < this->connectors.size(); k++)
376 // search for another point in the mesh which can 'reach' where we want to go
377 for (int m = 0; m < this->connectors[k].routes.size(); m++)
379 if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
381 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
383 snprintf(buffer,MAXBUF,"R %s %s",host,message);
384 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
391 snprintf(buffer,MAXBUF,"& %s",host);
392 NetSendToAllExcept(host,buffer);
393 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
398 // returns false if the packet could not be sent (e.g. target host down)
399 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
401 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
402 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
403 cn->CloseConnection();
404 cn->SetState(STATE_DISCONNECTED);
405 // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
406 return this->SendPacket(message,host);
412 // receives a packet from any where there is data waiting, first come, first served
413 // fills the message and host values with the host where the data came from.
415 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
418 memset(data, 0, 32767);
419 for (int i = 0; i < this->connectors.size(); i++)
421 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
423 // returns false if the packet could not be sent (e.g. target host down)
425 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
430 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
431 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
432 this->connectors[i].CloseConnection();
433 this->connectors[i].SetState(STATE_DISCONNECTED);
438 char* l = strtok(data,"\n");
441 char sanitized[32767];
442 memset(sanitized, 0, 32767);
444 for (int pt = 0; pt < strlen(l); pt++)
448 sanitized[ptt++] = l[pt];
451 sanitized[ptt] = '\0';
452 if (strlen(sanitized))
454 messages.push_back(sanitized);
455 strlcpy(host,this->connectors[i].GetServerName().c_str(),160);
456 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
459 l = strtok(NULL,"\n");
465 // nothing new yet -- message and host will be undefined
469 long connection::GenKey()
472 return (random()*time(NULL));