1 #include <connection.h>
6 #include <sys/utsname.h>
15 extern std::vector<Module*> modules;
16 extern std::vector<ircd_module*> factory;
20 connection::connection()
26 bool connection::CreateListener(char* host, int p)
28 sockaddr_in host_address;
32 struct linger linger = { 0 };
36 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
42 memset((void*)&host_address, 0, sizeof(host_address));
44 host_address.sin_family = AF_INET;
48 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
52 inet_aton(host,&addy);
53 host_address.sin_addr = addy;
56 host_address.sin_port = htons(p);
58 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
63 // make the socket non-blocking
64 flags = fcntl(fd, F_GETFL, 0);
65 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
69 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
72 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
74 // attempt to increase socket sendq and recvq as high as its possible
75 // to get them on linux.
78 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
79 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
86 char* ircd_connector::GetServerIP()
91 int ircd_connector::GetServerPort()
96 bool ircd_connector::SetHostAndPort(char* host, int port)
98 strncpy(this->host,host,160);
103 bool ircd_connector::SetHostAddress(char* host, int port)
105 strncpy(this->host,host,160);
107 memset((void*)&addr, 0, sizeof(addr));
108 addr.sin_family = AF_INET;
109 inet_aton(host,&addr.sin_addr);
110 addr.sin_port = htons(port);
114 void ircd_connector::SetServerPort(int p)
120 bool ircd_connector::MakeOutboundConnection(char* host, int port)
122 hostent* hoste = gethostbyname(host);
125 WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
126 this->SetHostAddress(host,port);
127 SetHostAndPort(host,port);
131 WriteOpers("Found hostname for %s",host);
132 this->SetHostAddress(hoste->h_addr,port);
133 SetHostAndPort(hoste->h_addr,port);
136 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
139 if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
141 WriteOpers("connect() failed for %s",host);
144 int flags = fcntl(this->fd, F_GETFL, 0);
145 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
148 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
149 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
154 WriteOpers("socket() failed!");
161 bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
163 char connect[MAXBUF];
165 ircd_connector connector;
169 if (connector.MakeOutboundConnection(targethost,port))
171 // targethost has been turned into an ip...
172 // we dont want this as the server name.
173 connector.SetServerName(servername);
174 sprintf(connect,"S %s %s %d :%s",getservername().c_str(),password,myport,getserverdesc().c_str());
175 connector.SetState(STATE_NOAUTH_OUTBOUND);
176 connector.SetHostAndPort(targethost, port);
177 this->connectors.push_back(connector);
178 return this->SendPacket(connect, servername);
182 connector.SetState(STATE_DISCONNECTED);
183 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
189 bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
191 char connect[MAXBUF];
193 ircd_connector connector;
195 WriteOpers("Establishing meshed link to %s:%d",servername,port);
199 if (connector.MakeOutboundConnection(targethost,port))
201 // targethost has been turned into an ip...
202 // we dont want this as the server name.
203 connector.SetServerName(servername);
204 sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
205 connector.SetState(STATE_NOAUTH_OUTBOUND);
206 connector.SetHostAndPort(targethost, port);
207 connector.SetState(STATE_CONNECTED);
208 this->connectors.push_back(connector);
209 return this->SendPacket(connect, servername);
213 connector.SetState(STATE_DISCONNECTED);
214 WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
220 bool connection::AddIncoming(int fd, char* targethost, int sourceport)
222 char connect[MAXBUF];
224 ircd_connector connector;
226 // targethost has been turned into an ip...
227 // we dont want this as the server name.
228 connector.SetServerName(targethost);
229 connector.SetDescriptor(fd);
230 connector.SetState(STATE_NOAUTH_INBOUND);
231 int flags = fcntl(fd, F_GETFL, 0);
232 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
235 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
236 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
237 connector.SetHostAndPort(targethost, sourceport);
238 connector.SetState(STATE_NOAUTH_INBOUND);
239 log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
240 this->connectors.push_back(connector);
244 void connection::TerminateLink(char* targethost)
246 // this locates the targethost in the connection::connectors vector of the class,
247 // and terminates it by sending it an SQUIT token and closing its descriptor.
248 // TerminateLink with a null string causes a terminate of ALL links
252 // Returns a pointer to the connector for 'host'
253 ircd_connector* connection::FindHost(std::string host)
255 for (int i = 0; i < this->connectors.size(); i++)
257 if (this->connectors[i].GetServerName() == host)
259 return &this->connectors[i];
265 std::string ircd_connector::GetServerName()
267 return this->servername;
270 std::string ircd_connector::GetDescription()
272 return this->description;
275 void ircd_connector::SetServerName(std::string serv)
277 this->servername = serv;
280 void ircd_connector::SetDescription(std::string desc)
282 this->servername = desc;
286 int ircd_connector::GetDescriptor()
291 int ircd_connector::GetState()
297 void ircd_connector::SetState(int state)
300 if (state == STATE_DISCONNECTED)
302 NetSendMyRoutingTable();
306 void ircd_connector::CloseConnection()
308 int flags = fcntl(this->fd, F_GETFL, 0);
309 fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
311 flags = fcntl(this->fd, F_GETFL, 0);
312 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
315 void ircd_connector::SetDescriptor(int fd)
320 bool connection::SendPacket(char *message, const char* host)
322 ircd_connector* cn = this->FindHost(host);
324 if (!strchr(message,'\n'))
326 strncat(message,"\n",MAXBUF);
331 log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
333 if (cn->GetState() == STATE_DISCONNECTED)
335 log(DEBUG,"Main route to %s is down, seeking alternative",host);
336 // this route is down, we must re-route the packet through an available point in the mesh.
337 for (int k = 0; k < this->connectors.size(); k++)
339 // search for another point in the mesh which can 'reach' where we want to go
340 for (int m = 0; m < this->connectors[k].routes.size(); m++)
342 if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
344 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
346 snprintf(buffer,MAXBUF,"R %s %s",host,message);
347 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
353 snprintf(buffer,MAXBUF,"& %s",host);
354 NetSendToAllExcept(host,buffer);
355 log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
360 // returns false if the packet could not be sent (e.g. target host down)
361 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
363 log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
364 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
365 cn->CloseConnection();
366 cn->SetState(STATE_DISCONNECTED);
373 // receives a packet from any where there is data waiting, first come, first served
374 // fills the message and host values with the host where the data came from.
376 bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
379 memset(data, 0, 32767);
380 for (int i = 0; i < this->connectors.size(); i++)
382 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
384 // returns false if the packet could not be sent (e.g. target host down)
386 rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
391 log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
392 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
393 this->connectors[i].CloseConnection();
394 this->connectors[i].SetState(STATE_DISCONNECTED);
399 char* l = strtok(data,"\n");
402 char sanitized[32767];
403 memset(sanitized, 0, 32767);
405 for (int pt = 0; pt < strlen(l); pt++)
409 sanitized[ptt++] = l[pt];
412 sanitized[ptt] = '\0';
413 if (strlen(sanitized))
415 messages.push_back(sanitized);
416 strncpy(host,this->connectors[i].GetServerName().c_str(),160);
417 log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
420 l = strtok(NULL,"\n");
426 // nothing new yet -- message and host will be undefined
430 long connection::GenKey()
433 return (random()*time(NULL));