]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/connection.cpp
0537fef24969c5060f587343f40c580ae9ff48c0
[user/henk/code/inspircd.git] / src / connection.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd.h"
20 #include "connection.h"
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
27 #include <vector>
28 #include <string>
29 #include <deque>
30 #include <sstream>
31 #include "modules.h"
32 #include "inspstring.h"
33 #include "helperfuncs.h"
34
35 extern bool has_been_netsplit;
36 extern std::vector<Module*> modules;
37 extern std::vector<ircd_module*> factory;
38
39 extern int MODCOUNT;
40
41 extern time_t TIME;
42
43
44 /**
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.
58  */
59
60 char* xsumtable = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
61
62 // creates a random id for a line for detection of duplicate messages
63 std::string CreateSum()
64 {
65         char sum[9];
66         sum[0] = ':';
67         sum[8] = '\0';
68         for(int q = 1; q < 8; q++)
69                 sum[q] = xsumtable[rand()%52];
70         return sum;
71 }
72
73 connection::connection()
74 {
75         fd = -1;
76 }
77
78
79 ircd_connector::ircd_connector()
80 {
81         fd = -1;
82         port = 0;
83         sendq = "";
84         WriteError = "";
85         nextping = TIME+120;
86         replied = false;
87 }
88
89 char* ircd_connector::GetServerIP()
90 {
91         return this->host;
92 }
93
94 int ircd_connector::GetServerPort()
95 {
96         return this->port;
97 }
98
99 bool ircd_connector::SetHostAndPort(char* newhost, int newport)
100 {
101         strncpy(this->host,newhost,160);
102         this->port = newport;
103         return true;
104 }
105
106 bool ircd_connector::SetHostAddress(char* newhost, int newport)
107 {
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);
114         return true;
115 }
116
117 void ircd_connector::SetServerPort(int p)
118 {
119         this->port = p;
120 }
121
122 bool ircd_connector::AddBuffer(std::string a)
123 {
124         std::string b = "";
125         for (unsigned int i = 0; i < a.length(); i++)
126                 if (a[i] != '\r')
127                         b = b + a[i];
128
129         std::stringstream stream(ircdbuffer);
130         stream << b;
131         log(DEBUG,"AddBuffer: %s",b.c_str());
132         ircdbuffer = stream.str();
133         return (ircdbuffer.length() < 1048576);
134 }
135
136 bool ircd_connector::BufferIsComplete()
137 {
138         for (unsigned int i = 0; i < ircdbuffer.length(); i++)
139                 if (ircdbuffer[i] == '\n')
140                         return true;
141         return false;
142 }
143
144 void ircd_connector::ClearBuffer()
145 {
146         ircdbuffer = "";
147 }
148
149 std::string ircd_connector::GetBuffer()
150 {
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)))
158         {
159                 ret = ret + *line;
160                 line++;
161         }
162         if ((*line == '\n') || (*line == '\r'))
163                 line++;
164         ircdbuffer = line;
165         return ret;
166 }
167
168 bool ircd_connector::AddWriteBuf(std::string data)
169 {
170         log(DEBUG,"connector::AddWriteBuf(%s)",data.c_str());
171         if (this->GetWriteError() != "")
172                 return false;
173         if (this->GetState() == STATE_DISCONNECTED)
174                 return false;
175         std::stringstream stream;
176         stream << sendq << data;
177         sendq = stream.str();
178         return (sendq.length() < 1048576);
179 }
180
181 bool ircd_connector::HasBufferedOutput()
182 {
183         return (sendq.length() > 0);
184 }
185
186 bool ircd_connector::CheckPing()
187 {
188         if (TIME > this->nextping)
189         {
190                 if (this->replied)
191                 {
192                         this->AddWriteBuf("?\n");
193                         this->nextping = TIME+120;
194                         this->replied = false;
195                         return true;
196                 }
197                 else
198                 {
199                         if (this->GetState() == STATE_CONNECTED)
200                         {
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;
206                                 return false;
207                         }
208                 }
209         }
210         return true;
211 }
212
213 void ircd_connector::ResetPing()
214 {
215         log(DEBUG,"Reset ping counter");
216         this->replied = true;
217         this->nextping = TIME+120;
218 }
219
220 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
221 bool ircd_connector::FlushWriteBuf()
222 {
223         char buffer[MAXBUF];
224         if ((this->GetState() == STATE_NOAUTH_OUTBOUND) || (this->GetState() == STATE_COOKIE_OUTBOUND))
225         {
226                 // if the outbound socket hasnt connected yet... return true and don't
227                 // actually do anything until it IS connected. This should probably
228                 // have a timeout somewhere, 10 secs should suffice. ;-)
229                 pollfd polls;
230                 polls.fd = this->fd;
231                 polls.events = POLLOUT;
232                 int ret = poll(&polls,1,1);
233                 if (ret < 1)
234                         return true;
235                 // this falls through and sends any waiting data, which can put it into the
236                 // connected state.
237                 if (this->GetState() == STATE_COOKIE_OUTBOUND)
238                 {
239                         log(DEBUG,"Moving cookie_outbound into STATE_CONNECTED state");
240                         this->SetState(STATE_CONNECTED);
241                         for (int t = 0; t < 32; t++) if (me[t]) for (unsigned int l = 0; l < me[t]->connectors.size(); l++)
242                         {
243                                 if (me[t]->connectors[l].GetDescription() != "")
244                                 {
245                                         snprintf(buffer,MAXBUF,"%s = %s :%s\r\n",CreateSum().c_str(),me[t]->connectors[l].GetServerName().c_str(),me[t]->connectors[l].GetDescription().c_str());
246                                         this->AddWriteBuf(buffer);
247                                 }
248                         }
249                 }
250                 snprintf(buffer,MAXBUF,"%s v %s %s\r\n",CreateSum().c_str(),ServerName,GetVersionString().c_str());
251                 this->AddWriteBuf(buffer);
252         }
253         if ((sendq.length()) && (this->GetState() != STATE_DISCONNECTED))
254         {
255                 char* tb = (char*)this->sendq.c_str();
256                 int n_sent = write(this->fd,tb,this->sendq.length());
257                 if (n_sent != 0)
258                 {
259                         if (n_sent == -1)
260                         {
261                                 this->SetWriteError(strerror(errno));
262                                 return false;
263                         }
264                         else
265                         {
266                                 log(DEBUG,"Wrote %d chars to socket",n_sent);
267                                 // advance the queue
268                                 tb += n_sent;
269                                 this->sendq = tb;
270                                 return true;
271                         }
272                 }
273         }
274         return true;
275 }
276
277 void ircd_connector::SetWriteError(std::string error)
278 {
279         if (this->WriteError == "")
280                 this->WriteError = error;
281 }
282
283 std::string ircd_connector::GetWriteError()
284 {
285         return this->WriteError;
286 }
287
288
289 bool ircd_connector::MakeOutboundConnection(char* newhost, int newport)
290 {
291         log(DEBUG,"MakeOutboundConnection: Original param: %s",newhost);
292         ClearBuffer();
293         hostent* hoste = gethostbyname(newhost);
294         if (!hoste)
295         {
296                 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",newhost);
297                 this->SetHostAddress(newhost,newport);
298                 SetHostAndPort(newhost,newport);
299         }
300         else
301         {
302                 struct in_addr* ia = (in_addr*)hoste->h_addr;
303                 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
304                 this->SetHostAddress(inet_ntoa(*ia),newport);
305                 SetHostAndPort(inet_ntoa(*ia),newport);
306         }
307
308         this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
309         if (this->fd >= 0)
310         {
311                 int flags = fcntl(this->fd, F_GETFL, 0);
312                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
313                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
314                 {
315                         if (errno != EINPROGRESS)
316                         {
317                                 WriteOpers("connect() failed for %s",host);
318                                 RemoveServer(this->servername.c_str());
319                                 return false;
320                         }
321                 }
322                 int sendbuf = 32768;
323                 int recvbuf = 32768;
324                 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
325                 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
326                 return true;
327         }
328         else
329         {
330                 WriteOpers("socket() failed!");
331                 RemoveServer(this->servername.c_str());
332         }
333
334         return false;
335 }
336
337
338 void ircd_connector::SetVersionString(std::string newversion)
339 {
340         log(DEBUG,"Set version of %s to %s",this->servername.c_str(),newversion.c_str());
341         this->version = newversion;
342 }
343
344 std::string ircd_connector::GetVersionString()
345 {
346         return this->version;
347 }
348
349
350 std::string ircd_connector::GetServerName()
351 {
352         return this->servername;
353 }
354
355 std::string ircd_connector::GetDescription()
356 {
357         return this->description;
358 }
359
360 void ircd_connector::SetServerName(std::string serv)
361 {
362         this->servername = serv;
363 }
364
365 void ircd_connector::SetDescription(std::string desc)
366 {
367         this->description = desc;
368 }
369
370
371 int ircd_connector::GetDescriptor()
372 {
373         return this->fd;
374 }
375
376 int ircd_connector::GetState()
377 {
378         return this->state;
379 }
380
381
382 void ircd_connector::SetState(int newstate)
383 {
384         this->state = newstate;
385         if (state == STATE_DISCONNECTED)
386         {
387                 NetSendMyRoutingTable();
388         }
389 }
390
391 void ircd_connector::CloseConnection()
392 {
393         log(DEBUG,"Closing connection");
394         // flush the queues
395         this->sendq = "";
396         this->ircdbuffer = "";
397         shutdown(this->fd,2);
398         close(this->fd);
399 }
400
401 void ircd_connector::SetDescriptor(int newfd)
402 {
403         this->fd = newfd;
404 }