1 #include <connection.h>
6 #include <sys/utsname.h>
16 extern std::vector<Module*> modules;
17 extern std::vector<ircd_module*> factory;
23 connection::connection()
29 bool connection::CreateListener(char* host, int p)
31 sockaddr_in host_address;
35 struct linger linger = { 0 };
39 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
45 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
48 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
50 // attempt to increase socket sendq and recvq as high as its possible
51 // to get them on linux.
54 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
55 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
57 memset((void*)&host_address, 0, sizeof(host_address));
59 host_address.sin_family = AF_INET;
63 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
67 inet_aton(host,&addy);
68 host_address.sin_addr = addy;
71 host_address.sin_port = htons(p);
73 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
78 // make the socket non-blocking
79 flags = fcntl(fd, F_GETFL, 0);
80 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
89 char* ircd_connector::GetServerIP()
94 int ircd_connector::GetServerPort()
99 bool ircd_connector::SetHostAndPort(char* host, int port)
101 strncpy(this->host,host,160);
106 bool ircd_connector::SetHostAddress(char* host, int port)
108 strncpy(this->host,host,160);
110 memset((void*)&addr, 0, sizeof(addr));
111 addr.sin_family = AF_INET;
112 inet_aton(host,&addr.sin_addr);
113 addr.sin_port = htons(port);
117 void ircd_connector::SetServerPort(int p)
122 bool ircd_connector::MakeOutboundConnection(char* host, int port)
124 log(DEBUG,"MakeOutboundConnection: Original param: %s",host);
125 hostent* hoste = gethostbyname(host);
128 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",host);
129 this->SetHostAddress(host,port);
130 SetHostAndPort(host,port);
134 struct in_addr* ia = (in_addr*)hoste->h_addr;
135 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
136 this->SetHostAddress(inet_ntoa(*ia),port);
137 SetHostAndPort(inet_ntoa(*ia),port);
140 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
143 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
145 WriteOpers("connect() failed for %s",host);
146 RemoveServer(this->servername.c_str());
149 int flags = fcntl(this->fd, F_GETFL, 0);
150 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
153 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
154 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
159 WriteOpers("socket() failed!");
160 RemoveServer(this->servername.c_str());
167 bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
169 char connect[MAXBUF];
171 ircd_connector connector;
172 ircd_connector *cn = this->FindHost(servername);
177 WriteOpers("CONNECT aborted: Server %s already exists",servername);
184 if (connector.MakeOutboundConnection(targethost,port))
186 // targethost has been turned into an ip...
187 // we dont want this as the server name.
188 connector.SetServerName(servername);
189 sprintf(connect,"S %s %s %d %d :%s",getservername().c_str(),password,myport,GetRevision(),getserverdesc().c_str());
190 connector.SetState(STATE_NOAUTH_OUTBOUND);
191 connector.SetHostAndPort(targethost, port);
192 this->connectors.push_back(connector);
193 return this->SendPacket(connect, servername);
197 connector.SetState(STATE_DISCONNECTED);
198 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
204 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
206 char connect[MAXBUF];
208 ircd_connector connector;
210 WriteOpers("Establishing meshed link to %s:%d",servername,port);
214 if (connector.MakeOutboundConnection(targethost,port))
216 // targethost has been turned into an ip...
217 // we dont want this as the server name.
218 connector.SetServerName(servername);
219 sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
220 connector.SetState(STATE_NOAUTH_OUTBOUND);
221 connector.SetHostAndPort(targethost, port);
222 connector.SetState(STATE_CONNECTED);
223 this->connectors.push_back(connector);
224 return this->SendPacket(connect, servername);
228 connector.SetState(STATE_DISCONNECTED);
229 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
235 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
237 char connect[MAXBUF];
239 ircd_connector connector;
241 // targethost has been turned into an ip...
242 // we dont want this as the server name.
243 connector.SetServerName(targethost);
244 connector.SetDescriptor(fd);
245 connector.SetState(STATE_NOAUTH_INBOUND);
246 int flags = fcntl(fd, F_GETFL, 0);
247 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
250 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
251 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
252 connector.SetHostAndPort(targethost, sourceport);
253 connector.SetState(STATE_NOAUTH_INBOUND);
254 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
255 this->connectors.push_back(connector);
259 void connection::TerminateLink(char* targethost)
261 // this locates the targethost in the connection::connectors vector of the class,
262 // and terminates it by sending it an SQUIT token and closing its descriptor.
263 // TerminateLink with a null string causes a terminate of ALL links
267 // Returns a pointer to the connector for 'host'
268 ircd_connector* connection::FindHost(std::string host)
270 for (int i = 0; i < this->connectors.size(); i++)
272 if (this->connectors[i].GetServerName() == host)
274 return &this->connectors[i];
280 std::string ircd_connector::GetServerName()
282 return this->servername;
285 std::string ircd_connector::GetDescription()
287 return this->description;
290 void ircd_connector::SetServerName(std::string serv)
292 this->servername = serv;
295 void ircd_connector::SetDescription(std::string desc)
297 this->description = desc;
301 int ircd_connector::GetDescriptor()
306 int ircd_connector::GetState()
312 void ircd_connector::SetState(int state)
315 if (state == STATE_DISCONNECTED)
317 NetSendMyRoutingTable();
321 void ircd_connector::CloseConnection()
323 int flags = fcntl(this->fd, F_GETFL, 0);
324 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
326 flags = fcntl(this->fd, F_GETFL, 0);
327 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
330 void ircd_connector::SetDescriptor(int fd)
335 bool connection::SendPacket(char *message, const char* host)
337 if ((!message) || (!host))
340 ircd_connector* cn = this->FindHost(host);
342 if (!strchr(message,'\n'))
344 strncat(message,"\n",MAXBUF);
349 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
351 if (cn->GetState() == STATE_DISCONNECTED)
353 log(DEBUG,"Main route to %s is down, seeking alternative",host);
354 // fix: can only route one hop to avoid a loop
355 if (strncat(message,"R ",2))
357 // this route is down, we must re-route the packet through an available point in the mesh.
358 for (int k = 0; k < this->connectors.size(); k++)
360 // search for another point in the mesh which can 'reach' where we want to go
361 for (int m = 0; m < this->connectors[k].routes.size(); m++)
363 if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
365 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
367 snprintf(buffer,MAXBUF,"R %s %s",host,message);
368 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
375 snprintf(buffer,MAXBUF,"& %s",host);
376 NetSendToAllExcept(host,buffer);
377 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
382 // returns false if the packet could not be sent (e.g. target host down)
383 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
385 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
386 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
387 cn->CloseConnection();
388 cn->SetState(STATE_DISCONNECTED);
389 // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
390 return this->SendPacket(message,host);
396 // receives a packet from any where there is data waiting, first come, first served
397 // fills the message and host values with the host where the data came from.
399 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
402 memset(data, 0, 32767);
403 for (int i = 0; i < this->connectors.size(); i++)
405 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
407 // returns false if the packet could not be sent (e.g. target host down)
409 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
414 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
415 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
416 this->connectors[i].CloseConnection();
417 this->connectors[i].SetState(STATE_DISCONNECTED);
422 char* l = strtok(data,"\n");
425 char sanitized[32767];
426 memset(sanitized, 0, 32767);
428 for (int pt = 0; pt < strlen(l); pt++)
432 sanitized[ptt++] = l[pt];
435 sanitized[ptt] = '\0';
436 if (strlen(sanitized))
438 messages.push_back(sanitized);
439 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
440 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
443 l = strtok(NULL,"\n");
449 // nothing new yet -- message and host will be undefined
453 long connection::GenKey()
456 return (random()*time(NULL));