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 };
47 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
53 memset((void*)&host_address, 0, sizeof(host_address));
55 host_address.sin_family = AF_INET;
59 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
63 inet_aton(host,&addy);
64 host_address.sin_addr = addy;
67 host_address.sin_port = htons(p);
69 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
74 // make the socket non-blocking
75 flags = fcntl(fd, F_GETFL, 0);
76 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
80 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
83 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
85 // attempt to increase socket sendq and recvq as high as its possible
86 // to get them on linux.
89 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
90 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
97 char* ircd_connector::GetServerIP()
102 int ircd_connector::GetServerPort()
107 bool ircd_connector::SetHostAndPort(char* host, int port)
109 strncpy(this->host,host,160);
114 bool ircd_connector::SetHostAddress(char* host, int port)
116 strncpy(this->host,host,160);
118 memset((void*)&addr, 0, sizeof(addr));
119 addr.sin_family = AF_INET;
120 inet_aton(host,&addr.sin_addr);
121 addr.sin_port = htons(port);
125 void ircd_connector::SetServerPort(int p)
131 bool ircd_connector::MakeOutboundConnection(char* host, int port)
133 hostent* hoste = gethostbyname(host);
136 WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
137 this->SetHostAddress(host,port);
138 SetHostAndPort(host,port);
142 WriteOpers("Found hostname for %s",host);
143 this->SetHostAddress(hoste->h_addr,port);
144 SetHostAndPort(hoste->h_addr,port);
147 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
150 if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
152 WriteOpers("connect() failed for %s",host);
155 int flags = fcntl(this->fd, F_GETFL, 0);
156 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
159 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
160 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
165 WriteOpers("socket() failed!");
172 bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
174 char connect[MAXBUF];
176 ircd_connector connector;
180 if (connector.MakeOutboundConnection(targethost,port))
182 // targethost has been turned into an ip...
183 // we dont want this as the server name.
184 connector.SetServerName(servername);
185 sprintf(connect,"S %s %s %d :%s",getservername().c_str(),password,myport,getserverdesc().c_str());
186 connector.SetState(STATE_NOAUTH_OUTBOUND);
187 connector.SetHostAndPort(targethost, port);
188 this->connectors.push_back(connector);
189 return this->SendPacket(connect, servername);
193 connector.SetState(STATE_DISCONNECTED);
194 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
200 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
202 char connect[MAXBUF];
204 ircd_connector connector;
206 WriteOpers("Establishing meshed link to %s:%d",servername,port);
210 if (connector.MakeOutboundConnection(targethost,port))
212 // targethost has been turned into an ip...
213 // we dont want this as the server name.
214 connector.SetServerName(servername);
215 sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
216 connector.SetState(STATE_NOAUTH_OUTBOUND);
217 connector.SetHostAndPort(targethost, port);
218 connector.SetState(STATE_CONNECTED);
219 this->connectors.push_back(connector);
220 return this->SendPacket(connect, servername);
224 connector.SetState(STATE_DISCONNECTED);
225 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
231 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
233 char connect[MAXBUF];
235 ircd_connector connector;
237 // targethost has been turned into an ip...
238 // we dont want this as the server name.
239 connector.SetServerName(targethost);
240 connector.SetDescriptor(fd);
241 connector.SetState(STATE_NOAUTH_INBOUND);
242 int flags = fcntl(fd, F_GETFL, 0);
243 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
246 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
247 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
248 connector.SetHostAndPort(targethost, sourceport);
249 connector.SetState(STATE_NOAUTH_INBOUND);
250 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
251 this->connectors.push_back(connector);
255 void connection::TerminateLink(char* targethost)
257 // this locates the targethost in the connection::connectors vector of the class,
258 // and terminates it by sending it an SQUIT token and closing its descriptor.
259 // TerminateLink with a null string causes a terminate of ALL links
263 // Returns a pointer to the connector for 'host'
264 ircd_connector* connection::FindHost(std::string host)
266 for (int i = 0; i < this->connectors.size(); i++)
268 if (this->connectors[i].GetServerName() == host)
270 return &this->connectors[i];
276 std::string ircd_connector::GetServerName()
278 return this->servername;
281 std::string ircd_connector::GetDescription()
283 return this->description;
286 void ircd_connector::SetServerName(std::string serv)
288 this->servername = serv;
291 void ircd_connector::SetDescription(std::string desc)
293 this->servername = desc;
297 int ircd_connector::GetDescriptor()
302 int ircd_connector::GetState()
308 void ircd_connector::SetState(int state)
311 if (state == STATE_DISCONNECTED)
313 NetSendMyRoutingTable();
317 void ircd_connector::CloseConnection()
319 int flags = fcntl(this->fd, F_GETFL, 0);
320 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
322 flags = fcntl(this->fd, F_GETFL, 0);
323 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
326 void ircd_connector::SetDescriptor(int fd)
331 bool connection::SendPacket(char *message, const char* host)
333 ircd_connector* cn = this->FindHost(host);
335 if (!strchr(message,'\n'))
337 strncat(message,"\n",MAXBUF);
342 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
344 if (cn->GetState() == STATE_DISCONNECTED)
346 log(DEBUG,"Main route to %s is down, seeking alternative",host);
347 // this route is down, we must re-route the packet through an available point in the mesh.
348 for (int k = 0; k < this->connectors.size(); k++)
350 // search for another point in the mesh which can 'reach' where we want to go
351 for (int m = 0; m < this->connectors[k].routes.size(); m++)
353 if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
355 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
357 snprintf(buffer,MAXBUF,"R %s %s",host,message);
358 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
364 snprintf(buffer,MAXBUF,"& %s",host);
365 NetSendToAllExcept(host,buffer);
366 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
371 // returns false if the packet could not be sent (e.g. target host down)
372 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
374 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
375 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
376 cn->CloseConnection();
377 cn->SetState(STATE_DISCONNECTED);
384 // receives a packet from any where there is data waiting, first come, first served
385 // fills the message and host values with the host where the data came from.
387 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
390 memset(data, 0, 32767);
391 for (int i = 0; i < this->connectors.size(); i++)
393 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
395 // returns false if the packet could not be sent (e.g. target host down)
397 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
402 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
403 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
404 this->connectors[i].CloseConnection();
405 this->connectors[i].SetState(STATE_DISCONNECTED);
410 char* l = strtok(data,"\n");
413 char sanitized[32767];
414 memset(sanitized, 0, 32767);
416 for (int pt = 0; pt < strlen(l); pt++)
420 sanitized[ptt++] = l[pt];
423 sanitized[ptt] = '\0';
424 if (strlen(sanitized))
426 messages.push_back(sanitized);
427 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
428 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
431 l = strtok(NULL,"\n");
437 // nothing new yet -- message and host will be undefined
441 long connection::GenKey()
444 return (random()*time(NULL));