1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
20 #include "connection.h"
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
32 #include "inspstring.h"
33 #include "helperfuncs.h"
35 extern bool has_been_netsplit;
36 extern std::vector<Module*> modules;
37 extern std::vector<ircd_module*> factory;
45 * The InspIRCd mesh network is maintained by a tree of objects which reference *themselves*.
46 * Every local server has an array of 32 *serverrecs, known as me[]. Each of these represents
47 * a local listening port, and is not null if the user has opened a listening port on the server.
48 * It is assumed nobody will ever want to open more than 32 listening server ports at any one
49 * time (i mean come on, why would you want more, the ircd works fine with ONE).
50 * Each me[] entry has multiple classes within it of type ircd_connector. These are stored in a vector
51 * and each represents a server linked via this socket. If the connection was created outbound,
52 * the connection is bound to the default ip address by using me[defaultRoute] (defaultRoute being
53 * a global variable which indicates the default server to make connections on). If the connection
54 * was created inbound, it is attached to the port the connection came in on. There may be as many
55 * ircd_connector objects as needed in each me[] entry. Each ircd_connector implements the specifics
56 * of an ircd connection in the mesh, however each ircd may have multiple ircd_connector connections
57 * to it, to maintain the mesh link.
60 char* xsumtable = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
62 // creates a random id for a line for detection of duplicate messages
63 std::string CreateSum()
68 for(int q = 1; q < 8; q++)
69 sum[q] = xsumtable[rand()%52];
73 connection::connection()
79 ircd_connector::ircd_connector()
89 char* ircd_connector::GetServerIP()
94 int ircd_connector::GetServerPort()
99 bool ircd_connector::SetHostAndPort(char* newhost, int newport)
101 strncpy(this->host,newhost,160);
102 this->port = newport;
106 bool ircd_connector::SetHostAddress(char* newhost, int newport)
108 strncpy(this->host,newhost,160);
109 this->port = newport;
110 memset((void*)&addr, 0, sizeof(addr));
111 addr.sin_family = AF_INET;
112 inet_aton(host,&addr.sin_addr);
113 addr.sin_port = htons(port);
117 void ircd_connector::SetServerPort(int p)
122 bool ircd_connector::AddBuffer(std::string a)
125 for (int i = 0; i < a.length(); i++)
129 std::stringstream stream(ircdbuffer);
131 log(DEBUG,"AddBuffer: %s",b.c_str());
132 ircdbuffer = stream.str();
133 return (ircdbuffer.length() < 1048576);
136 bool ircd_connector::BufferIsComplete()
138 for (int i = 0; i < ircdbuffer.length(); i++)
139 if (ircdbuffer[i] == '\n')
144 void ircd_connector::ClearBuffer()
149 std::string ircd_connector::GetBuffer()
151 // Fix by Brain 28th Apr 2005
152 // seems my stringstream code isnt liked by linux
153 // EVEN THOUGH IT IS CORRECT! Fixed by using a different
154 // (SLOWER) algorithm...
155 char* line = (char*)ircdbuffer.c_str();
156 std::string ret = "";
157 while ((*line != '\n') && (strlen(line)))
162 if ((*line == '\n') || (*line == '\r'))
168 bool ircd_connector::AddWriteBuf(std::string data)
170 log(DEBUG,"connector::AddWriteBuf(%s)",data.c_str());
171 if (this->GetWriteError() != "")
173 if (this->GetState() == STATE_DISCONNECTED)
175 std::stringstream stream;
176 stream << sendq << data;
177 sendq = stream.str();
178 return (sendq.length() < 1048576);
181 bool ircd_connector::HasBufferedOutput()
183 return (sendq.length() > 0);
186 bool ircd_connector::CheckPing()
188 if (TIME > this->nextping)
192 this->AddWriteBuf("?\n");
193 this->nextping = TIME+120;
194 this->replied = false;
199 if (this->GetState() == STATE_CONNECTED)
201 this->SetWriteError("Ping timeout");
202 this->CloseConnection();
203 this->SetState(STATE_DISCONNECTED);
204 WriteOpers("*** Ping timeout on link to %s (more routes may remain)",this->GetServerName().c_str());
205 has_been_netsplit = true;
213 void ircd_connector::ResetPing()
215 log(DEBUG,"Reset ping counter");
216 this->replied = true;
217 this->nextping = TIME+120;
220 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
221 bool ircd_connector::FlushWriteBuf()
223 if (this->GetState() == STATE_NOAUTH_OUTBOUND)
225 // if the outbound socket hasnt connected yet... return true and don't
226 // actually do anything until it IS connected. This should probably
227 // have a timeout somewhere, 10 secs should suffice. ;-)
230 polls.events = POLLOUT;
231 int ret = poll(&polls,1,1);
234 // this falls through and sends any waiting data, which can put it into the
237 if ((sendq.length()) && (this->GetState() != STATE_DISCONNECTED))
239 char* tb = (char*)this->sendq.c_str();
240 int n_sent = write(this->fd,tb,this->sendq.length());
245 this->SetWriteError(strerror(errno));
250 log(DEBUG,"Wrote %d chars to socket",n_sent);
261 void ircd_connector::SetWriteError(std::string error)
263 if (this->WriteError == "")
264 this->WriteError = error;
267 std::string ircd_connector::GetWriteError()
269 return this->WriteError;
273 bool ircd_connector::MakeOutboundConnection(char* newhost, int newport)
275 log(DEBUG,"MakeOutboundConnection: Original param: %s",newhost);
277 hostent* hoste = gethostbyname(newhost);
280 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",newhost);
281 this->SetHostAddress(newhost,newport);
282 SetHostAndPort(newhost,newport);
286 struct in_addr* ia = (in_addr*)hoste->h_addr;
287 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
288 this->SetHostAddress(inet_ntoa(*ia),newport);
289 SetHostAndPort(inet_ntoa(*ia),newport);
292 this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
295 int flags = fcntl(this->fd, F_GETFL, 0);
296 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
297 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
299 if (errno != EINPROGRESS)
301 WriteOpers("connect() failed for %s",host);
302 RemoveServer(this->servername.c_str());
308 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
309 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
314 WriteOpers("socket() failed!");
315 RemoveServer(this->servername.c_str());
322 void ircd_connector::SetVersionString(std::string newversion)
324 log(DEBUG,"Set version of %s to %s",this->servername.c_str(),newversion.c_str());
325 this->version = newversion;
328 std::string ircd_connector::GetVersionString()
330 return this->version;
334 std::string ircd_connector::GetServerName()
336 return this->servername;
339 std::string ircd_connector::GetDescription()
341 return this->description;
344 void ircd_connector::SetServerName(std::string serv)
346 this->servername = serv;
349 void ircd_connector::SetDescription(std::string desc)
351 this->description = desc;
355 int ircd_connector::GetDescriptor()
360 int ircd_connector::GetState()
366 void ircd_connector::SetState(int newstate)
368 this->state = newstate;
369 if (state == STATE_DISCONNECTED)
371 NetSendMyRoutingTable();
375 void ircd_connector::CloseConnection()
377 log(DEBUG,"Closing connection");
380 this->ircdbuffer = "";
381 shutdown(this->fd,2);
385 void ircd_connector::SetDescriptor(int newfd)