1 #include <connection.h>
6 #include <sys/utsname.h>
15 extern std::vector<Module*> modules;
16 extern std::vector<ircd_module*> factory;
30 connection::connection()
37 bool connection::CreateListener(char* host, int p)
39 sockaddr_in host_address;
43 struct linger linger = { 0 };
45 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
51 memset((void*)&host_address, 0, sizeof(host_address));
53 host_address.sin_family = AF_INET;
57 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
61 inet_aton(host,&addy);
62 host_address.sin_addr = addy;
65 host_address.sin_port = htons(p);
67 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
72 // make the socket non-blocking
73 flags = fcntl(fd, F_GETFL, 0);
74 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
78 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
81 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
83 // attempt to increase socket sendq and recvq as high as its possible
84 // to get them on linux.
87 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
88 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
95 char* ircd_connector::GetServerIP()
100 int ircd_connector::GetServerPort()
105 bool ircd_connector::SetHostAndPort(char* host, int port)
107 strncpy(this->host,host,160);
112 bool ircd_connector::SetHostAddress(char* host, int port)
114 strncpy(this->host,host,160);
116 memset((void*)&addr, 0, sizeof(addr));
117 addr.sin_family = AF_INET;
118 inet_aton(host,&addr.sin_addr);
119 addr.sin_port = htons(port);
123 bool ircd_connector::MakeOutboundConnection(char* host, int port)
125 hostent* hoste = gethostbyname(host);
128 WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
129 this->SetHostAddress(host,port);
130 SetHostAndPort(host,port);
134 WriteOpers("Found hostname for %s",host);
135 this->SetHostAddress(hoste->h_addr,port);
136 SetHostAndPort(hoste->h_addr,port);
139 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
142 if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
144 WriteOpers("connect() failed for %s",host);
147 int flags = fcntl(this->fd, F_GETFL, 0);
148 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
151 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
152 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
157 WriteOpers("socket() failed!");
164 bool connection::BeginLink(char* targethost, int port, char* password, char* servername)
166 char connect[MAXBUF];
168 ircd_connector connector;
172 if (connector.MakeOutboundConnection(targethost,port))
174 // targethost has been turned into an ip...
175 // we dont want this as the server name.
176 connector.SetServerName(servername);
177 sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
178 connector.SetState(STATE_NOAUTH_OUTBOUND);
179 connector.SetHostAndPort(targethost, port);
180 this->connectors.push_back(connector);
181 return this->SendPacket(connect, servername);
185 connector.SetState(STATE_DISCONNECTED);
186 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
192 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
194 char connect[MAXBUF];
196 ircd_connector connector;
198 WriteOpers("Establishing meshed link to %s:%d",servername,port);
202 if (connector.MakeOutboundConnection(targethost,port))
204 // targethost has been turned into an ip...
205 // we dont want this as the server name.
206 connector.SetServerName(servername);
207 sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
208 connector.SetState(STATE_NOAUTH_OUTBOUND);
209 connector.SetHostAndPort(targethost, port);
210 connector.SetState(STATE_CONNECTED);
211 this->connectors.push_back(connector);
212 return this->SendPacket(connect, servername);
216 connector.SetState(STATE_DISCONNECTED);
217 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
223 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
225 char connect[MAXBUF];
227 ircd_connector connector;
229 // targethost has been turned into an ip...
230 // we dont want this as the server name.
231 connector.SetServerName(targethost);
232 connector.SetDescriptor(fd);
233 connector.SetState(STATE_NOAUTH_INBOUND);
234 int flags = fcntl(fd, F_GETFL, 0);
235 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
238 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
239 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
240 connector.SetHostAndPort(targethost, sourceport);
241 connector.SetState(STATE_NOAUTH_INBOUND);
242 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
243 this->connectors.push_back(connector);
247 void connection::TerminateLink(char* targethost)
249 // this locates the targethost in the connection::connectors vector of the class,
250 // and terminates it by sending it an SQUIT token and closing its descriptor.
251 // TerminateLink with a null string causes a terminate of ALL links
255 // Returns a pointer to the connector for 'host'
256 ircd_connector* connection::FindHost(std::string host)
258 for (int i = 0; i < this->connectors.size(); i++)
260 if (this->connectors[i].GetServerName() == host)
262 return &this->connectors[i];
268 std::string ircd_connector::GetServerName()
270 return this->servername;
273 std::string ircd_connector::GetDescription()
275 return this->description;
278 void ircd_connector::SetServerName(std::string serv)
280 this->servername = serv;
283 void ircd_connector::SetDescription(std::string desc)
285 this->servername = desc;
289 int ircd_connector::GetDescriptor()
294 int ircd_connector::GetState()
300 void ircd_connector::SetState(int state)
303 if (state == STATE_DISCONNECTED)
305 NetSendMyRoutingTable();
309 void ircd_connector::CloseConnection()
311 int flags = fcntl(this->fd, F_GETFL, 0);
312 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
314 flags = fcntl(this->fd, F_GETFL, 0);
315 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
318 void ircd_connector::SetDescriptor(int fd)
323 bool connection::SendPacket(char *message, const char* host)
325 ircd_connector* cn = this->FindHost(host);
327 if (!strchr(message,'\n'))
329 strncat(message,"\n",MAXBUF);
334 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
336 if (cn->GetState() == STATE_DISCONNECTED)
338 log(DEBUG,"Main route to %s is down, seeking alternative",host);
339 // this route is down, we must re-route the packet through an available point in the mesh.
340 for (int k = 0; k < this->connectors.size(); k++)
342 // search for another point in the mesh which can 'reach' where we want to go
343 for (int m = 0; m < this->connectors[k].routes.size(); m++)
345 if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
347 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
349 snprintf(buffer,MAXBUF,"R %s %s",host,message);
350 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
356 snprintf(buffer,MAXBUF,"& %s",host);
357 NetSendToAllExcept(host,buffer);
358 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
362 // returns false if the packet could not be sent (e.g. target host down)
363 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
365 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
366 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
367 cn->CloseConnection();
368 cn->SetState(STATE_DISCONNECTED);
375 // receives a packet from any where there is data waiting, first come, first served
376 // fills the message and host values with the host where the data came from.
378 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
381 memset(data, 0, 32767);
382 for (int i = 0; i < this->connectors.size(); i++)
384 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
386 // returns false if the packet could not be sent (e.g. target host down)
388 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
393 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
394 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
395 this->connectors[i].CloseConnection();
396 this->connectors[i].SetState(STATE_DISCONNECTED);
401 char* l = strtok(data,"\n");
404 char sanitized[32767];
405 memset(sanitized, 0, 32767);
407 for (int pt = 0; pt < strlen(l); pt++)
411 sanitized[ptt++] = l[pt];
414 sanitized[ptt] = '\0';
415 if (strlen(sanitized))
417 messages.push_back(sanitized);
418 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
419 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
422 l = strtok(NULL,"\n");
428 // nothing new yet -- message and host will be undefined
432 long connection::GenKey()
435 return (random()*time(NULL));