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 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
191 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
193 char connect[MAXBUF];
195 ircd_connector connector;
197 WriteOpers("Establishing meshed link to %s:%d",servername,port);
201 if (connector.MakeOutboundConnection(targethost,port))
203 // targethost has been turned into an ip...
204 // we dont want this as the server name.
205 connector.SetServerName(servername);
206 sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
207 connector.SetState(STATE_NOAUTH_OUTBOUND);
208 connector.SetHostAndPort(targethost, port);
209 connector.SetState(STATE_CONNECTED);
210 this->connectors.push_back(connector);
211 return this->SendPacket(connect, servername);
215 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
221 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
223 char connect[MAXBUF];
225 ircd_connector connector;
227 // targethost has been turned into an ip...
228 // we dont want this as the server name.
229 connector.SetServerName(targethost);
230 connector.SetDescriptor(fd);
231 connector.SetState(STATE_NOAUTH_INBOUND);
232 int flags = fcntl(fd, F_GETFL, 0);
233 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
236 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
237 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
238 connector.SetHostAndPort(targethost, sourceport);
239 connector.SetState(STATE_NOAUTH_INBOUND);
240 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
241 this->connectors.push_back(connector);
245 void connection::TerminateLink(char* targethost)
247 // this locates the targethost in the connection::connectors vector of the class,
248 // and terminates it by sending it an SQUIT token and closing its descriptor.
249 // TerminateLink with a null string causes a terminate of ALL links
253 // Returns a pointer to the connector for 'host'
254 ircd_connector* connection::FindHost(std::string host)
256 for (int i = 0; i < this->connectors.size(); i++)
258 if (this->connectors[i].GetServerName() == host)
260 return &this->connectors[i];
266 std::string ircd_connector::GetServerName()
268 return this->servername;
271 std::string ircd_connector::GetDescription()
273 return this->description;
276 void ircd_connector::SetServerName(std::string serv)
278 this->servername = serv;
281 void ircd_connector::SetDescription(std::string desc)
283 this->servername = desc;
287 int ircd_connector::GetDescriptor()
292 int ircd_connector::GetState()
298 void ircd_connector::SetState(int state)
303 void ircd_connector::CloseConnection()
305 int flags = fcntl(this->fd, F_GETFL, 0);
306 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
308 flags = fcntl(this->fd, F_GETFL, 0);
309 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
312 void ircd_connector::SetDescriptor(int fd)
317 bool connection::SendPacket(char *message, const char* host)
319 ircd_connector* cn = this->FindHost(host);
320 strncat(message,"\n",MAXBUF);
324 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
326 if (cn->GetState() == STATE_DISCONNECTED)
328 log(DEBUG,"Main route to %s is down, seeking alternative",host);
329 // this route is down, we must re-route the packet through an available point in the mesh.
330 for (int k = 0; k < this->connectors.size(); k++)
332 // search for another point in the mesh which can 'reach' where we want to go
333 for (int m = 0; m < this->connectors[k].routes.size(); m++)
335 if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
337 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
339 snprintf(buffer,MAXBUF,"R %s %s",host,message);
340 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
345 log(DEBUG,"ERROR: Main route to %s is down and there are no possible routes to this server!",host);
349 // returns false if the packet could not be sent (e.g. target host down)
350 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
352 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
353 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
354 cn->CloseConnection();
355 cn->SetState(STATE_DISCONNECTED);
362 // receives a packet from any where there is data waiting, first come, first served
363 // fills the message and host values with the host where the data came from.
365 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
368 memset(data, 0, 32767);
369 for (int i = 0; i < this->connectors.size(); i++)
371 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
373 // returns false if the packet could not be sent (e.g. target host down)
375 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
380 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
381 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
382 this->connectors[i].CloseConnection();
383 this->connectors[i].SetState(STATE_DISCONNECTED);
388 char* l = strtok(data,"\n");
391 char sanitized[32767];
392 memset(sanitized, 0, 32767);
394 for (int pt = 0; pt < strlen(l); pt++)
398 sanitized[ptt++] = l[pt];
401 sanitized[ptt] = '\0';
402 if (strlen(sanitized))
404 messages.push_back(sanitized);
405 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
406 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
409 l = strtok(NULL,"\n");
415 // nothing new yet -- message and host will be undefined
419 long connection::GenKey()
422 return (random()*time(NULL));