1 #include <connection.h>
6 #include <sys/utsname.h>
15 extern std::vector<Module*> modules;
16 extern std::vector<ircd_module*> factory;
28 connection::connection()
34 bool connection::CreateListener(char* host, int p)
36 sockaddr_in host_address;
40 struct linger linger = { 0 };
44 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
50 memset((void*)&host_address, 0, sizeof(host_address));
52 host_address.sin_family = AF_INET;
56 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
60 inet_aton(host,&addy);
61 host_address.sin_addr = addy;
64 host_address.sin_port = htons(p);
66 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
71 // make the socket non-blocking
72 flags = fcntl(fd, F_GETFL, 0);
73 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
77 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
80 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
82 // attempt to increase socket sendq and recvq as high as its possible
83 // to get them on linux.
86 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
87 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
94 char* ircd_connector::GetServerIP()
99 int ircd_connector::GetServerPort()
104 bool ircd_connector::SetHostAndPort(char* host, int port)
106 strncpy(this->host,host,160);
111 bool ircd_connector::SetHostAddress(char* host, int port)
113 strncpy(this->host,host,160);
115 memset((void*)&addr, 0, sizeof(addr));
116 addr.sin_family = AF_INET;
117 inet_aton(host,&addr.sin_addr);
118 addr.sin_port = htons(port);
122 void ircd_connector::SetServerPort(int p)
128 bool ircd_connector::MakeOutboundConnection(char* host, int port)
130 hostent* hoste = gethostbyname(host);
133 WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
134 this->SetHostAddress(host,port);
135 SetHostAndPort(host,port);
139 WriteOpers("Found hostname for %s",host);
140 this->SetHostAddress(hoste->h_addr,port);
141 SetHostAndPort(hoste->h_addr,port);
144 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
147 if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
149 WriteOpers("connect() failed for %s",host);
152 int flags = fcntl(this->fd, F_GETFL, 0);
153 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
156 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
157 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
162 WriteOpers("socket() failed!");
169 bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
171 char connect[MAXBUF];
173 ircd_connector connector;
177 if (connector.MakeOutboundConnection(targethost,port))
179 // targethost has been turned into an ip...
180 // we dont want this as the server name.
181 connector.SetServerName(servername);
182 sprintf(connect,"S %s %s %d :%s",getservername().c_str(),password,myport,getserverdesc().c_str());
183 connector.SetState(STATE_NOAUTH_OUTBOUND);
184 connector.SetHostAndPort(targethost, port);
185 this->connectors.push_back(connector);
186 return this->SendPacket(connect, servername);
190 connector.SetState(STATE_DISCONNECTED);
191 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
197 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
199 char connect[MAXBUF];
201 ircd_connector connector;
203 WriteOpers("Establishing meshed link to %s:%d",servername,port);
207 if (connector.MakeOutboundConnection(targethost,port))
209 // targethost has been turned into an ip...
210 // we dont want this as the server name.
211 connector.SetServerName(servername);
212 sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
213 connector.SetState(STATE_NOAUTH_OUTBOUND);
214 connector.SetHostAndPort(targethost, port);
215 connector.SetState(STATE_CONNECTED);
216 this->connectors.push_back(connector);
217 return this->SendPacket(connect, servername);
221 connector.SetState(STATE_DISCONNECTED);
222 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
228 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
230 char connect[MAXBUF];
232 ircd_connector connector;
234 // targethost has been turned into an ip...
235 // we dont want this as the server name.
236 connector.SetServerName(targethost);
237 connector.SetDescriptor(fd);
238 connector.SetState(STATE_NOAUTH_INBOUND);
239 int flags = fcntl(fd, F_GETFL, 0);
240 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
243 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
244 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
245 connector.SetHostAndPort(targethost, sourceport);
246 connector.SetState(STATE_NOAUTH_INBOUND);
247 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
248 this->connectors.push_back(connector);
252 void connection::TerminateLink(char* targethost)
254 // this locates the targethost in the connection::connectors vector of the class,
255 // and terminates it by sending it an SQUIT token and closing its descriptor.
256 // TerminateLink with a null string causes a terminate of ALL links
260 // Returns a pointer to the connector for 'host'
261 ircd_connector* connection::FindHost(std::string host)
263 for (int i = 0; i < this->connectors.size(); i++)
265 if (this->connectors[i].GetServerName() == host)
267 return &this->connectors[i];
273 std::string ircd_connector::GetServerName()
275 return this->servername;
278 std::string ircd_connector::GetDescription()
280 return this->description;
283 void ircd_connector::SetServerName(std::string serv)
285 this->servername = serv;
288 void ircd_connector::SetDescription(std::string desc)
290 this->servername = desc;
294 int ircd_connector::GetDescriptor()
299 int ircd_connector::GetState()
305 void ircd_connector::SetState(int state)
308 if (state == STATE_DISCONNECTED)
310 NetSendMyRoutingTable();
314 void ircd_connector::CloseConnection()
316 int flags = fcntl(this->fd, F_GETFL, 0);
317 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
319 flags = fcntl(this->fd, F_GETFL, 0);
320 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
323 void ircd_connector::SetDescriptor(int fd)
328 bool connection::SendPacket(char *message, const char* host)
330 ircd_connector* cn = this->FindHost(host);
332 if (!strchr(message,'\n'))
334 strncat(message,"\n",MAXBUF);
339 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
341 if (cn->GetState() == STATE_DISCONNECTED)
343 log(DEBUG,"Main route to %s is down, seeking alternative",host);
344 // this route is down, we must re-route the packet through an available point in the mesh.
345 for (int k = 0; k < this->connectors.size(); k++)
347 // search for another point in the mesh which can 'reach' where we want to go
348 for (int m = 0; m < this->connectors[k].routes.size(); m++)
350 if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
352 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
354 snprintf(buffer,MAXBUF,"R %s %s",host,message);
355 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
361 snprintf(buffer,MAXBUF,"& %s",host);
362 NetSendToAllExcept(host,buffer);
363 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
368 // returns false if the packet could not be sent (e.g. target host down)
369 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
371 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
372 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
373 cn->CloseConnection();
374 cn->SetState(STATE_DISCONNECTED);
381 // receives a packet from any where there is data waiting, first come, first served
382 // fills the message and host values with the host where the data came from.
384 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
387 memset(data, 0, 32767);
388 for (int i = 0; i < this->connectors.size(); i++)
390 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
392 // returns false if the packet could not be sent (e.g. target host down)
394 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
399 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
400 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
401 this->connectors[i].CloseConnection();
402 this->connectors[i].SetState(STATE_DISCONNECTED);
407 char* l = strtok(data,"\n");
410 char sanitized[32767];
411 memset(sanitized, 0, 32767);
413 for (int pt = 0; pt < strlen(l); pt++)
417 sanitized[ptt++] = l[pt];
420 sanitized[ptt] = '\0';
421 if (strlen(sanitized))
423 messages.push_back(sanitized);
424 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
425 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
428 l = strtok(NULL,"\n");
434 // nothing new yet -- message and host will be undefined
438 long connection::GenKey()
441 return (random()*time(NULL));