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()
35 bool connection::CreateListener(char* host, int p)
37 sockaddr_in host_address;
41 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 void ircd_connector::SetServerPort(int p)
129 bool ircd_connector::MakeOutboundConnection(char* host, int port)
131 hostent* hoste = gethostbyname(host);
134 WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
135 this->SetHostAddress(host,port);
136 SetHostAndPort(host,port);
140 WriteOpers("Found hostname for %s",host);
141 this->SetHostAddress(hoste->h_addr,port);
142 SetHostAndPort(hoste->h_addr,port);
145 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
148 if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
150 WriteOpers("connect() failed for %s",host);
153 int flags = fcntl(this->fd, F_GETFL, 0);
154 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
157 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
158 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
163 WriteOpers("socket() failed!");
170 bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
172 char connect[MAXBUF];
174 ircd_connector connector;
178 if (connector.MakeOutboundConnection(targethost,port))
180 // targethost has been turned into an ip...
181 // we dont want this as the server name.
182 connector.SetServerName(servername);
183 sprintf(connect,"S %s %s %d :%s",getservername().c_str(),password,myport,getserverdesc().c_str());
184 connector.SetState(STATE_NOAUTH_OUTBOUND);
185 connector.SetHostAndPort(targethost, port);
186 this->connectors.push_back(connector);
187 return this->SendPacket(connect, servername);
191 connector.SetState(STATE_DISCONNECTED);
192 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
198 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
200 char connect[MAXBUF];
202 ircd_connector connector;
204 WriteOpers("Establishing meshed link to %s:%d",servername,port);
208 if (connector.MakeOutboundConnection(targethost,port))
210 // targethost has been turned into an ip...
211 // we dont want this as the server name.
212 connector.SetServerName(servername);
213 sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
214 connector.SetState(STATE_NOAUTH_OUTBOUND);
215 connector.SetHostAndPort(targethost, port);
216 connector.SetState(STATE_CONNECTED);
217 this->connectors.push_back(connector);
218 return this->SendPacket(connect, servername);
222 connector.SetState(STATE_DISCONNECTED);
223 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
229 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
231 char connect[MAXBUF];
233 ircd_connector connector;
235 // targethost has been turned into an ip...
236 // we dont want this as the server name.
237 connector.SetServerName(targethost);
238 connector.SetDescriptor(fd);
239 connector.SetState(STATE_NOAUTH_INBOUND);
240 int flags = fcntl(fd, F_GETFL, 0);
241 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
244 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
245 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
246 connector.SetHostAndPort(targethost, sourceport);
247 connector.SetState(STATE_NOAUTH_INBOUND);
248 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
249 this->connectors.push_back(connector);
253 void connection::TerminateLink(char* targethost)
255 // this locates the targethost in the connection::connectors vector of the class,
256 // and terminates it by sending it an SQUIT token and closing its descriptor.
257 // TerminateLink with a null string causes a terminate of ALL links
261 // Returns a pointer to the connector for 'host'
262 ircd_connector* connection::FindHost(std::string host)
264 for (int i = 0; i < this->connectors.size(); i++)
266 if (this->connectors[i].GetServerName() == host)
268 return &this->connectors[i];
274 std::string ircd_connector::GetServerName()
276 return this->servername;
279 std::string ircd_connector::GetDescription()
281 return this->description;
284 void ircd_connector::SetServerName(std::string serv)
286 this->servername = serv;
289 void ircd_connector::SetDescription(std::string desc)
291 this->servername = desc;
295 int ircd_connector::GetDescriptor()
300 int ircd_connector::GetState()
306 void ircd_connector::SetState(int state)
309 if (state == STATE_DISCONNECTED)
311 NetSendMyRoutingTable();
315 void ircd_connector::CloseConnection()
317 int flags = fcntl(this->fd, F_GETFL, 0);
318 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
320 flags = fcntl(this->fd, F_GETFL, 0);
321 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
324 void ircd_connector::SetDescriptor(int fd)
329 bool connection::SendPacket(char *message, const char* host)
331 ircd_connector* cn = this->FindHost(host);
333 if (!strchr(message,'\n'))
335 strncat(message,"\n",MAXBUF);
340 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
342 if (cn->GetState() == STATE_DISCONNECTED)
344 log(DEBUG,"Main route to %s is down, seeking alternative",host);
345 // this route is down, we must re-route the packet through an available point in the mesh.
346 for (int k = 0; k < this->connectors.size(); k++)
348 // search for another point in the mesh which can 'reach' where we want to go
349 for (int m = 0; m < this->connectors[k].routes.size(); m++)
351 if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
353 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
355 snprintf(buffer,MAXBUF,"R %s %s",host,message);
356 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
362 snprintf(buffer,MAXBUF,"& %s",host);
363 NetSendToAllExcept(host,buffer);
364 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
369 // returns false if the packet could not be sent (e.g. target host down)
370 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
372 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
373 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
374 cn->CloseConnection();
375 cn->SetState(STATE_DISCONNECTED);
382 // receives a packet from any where there is data waiting, first come, first served
383 // fills the message and host values with the host where the data came from.
385 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
388 memset(data, 0, 32767);
389 for (int i = 0; i < this->connectors.size(); i++)
391 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
393 // returns false if the packet could not be sent (e.g. target host down)
395 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
400 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
401 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
402 this->connectors[i].CloseConnection();
403 this->connectors[i].SetState(STATE_DISCONNECTED);
408 char* l = strtok(data,"\n");
411 char sanitized[32767];
412 memset(sanitized, 0, 32767);
414 for (int pt = 0; pt < strlen(l); pt++)
418 sanitized[ptt++] = l[pt];
421 sanitized[ptt] = '\0';
422 if (strlen(sanitized))
424 messages.push_back(sanitized);
425 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
426 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
429 l = strtok(NULL,"\n");
435 // nothing new yet -- message and host will be undefined
439 long connection::GenKey()
442 return (random()*time(NULL));