]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/servers.cpp
*** empty log message ***
[user/henk/code/inspircd.git] / src / servers.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_config.h" 
20 #include "servers.h"
21 #include "inspircd.h"
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <poll.h>
25 #include <sys/errno.h>
26 #include <sys/ioctl.h>
27 #include <sys/utsname.h>
28 #include <vector>
29 #include <string>
30 #include <deque>
31 #include <sstream>
32 #include <map>
33 #include "inspstring.h"
34 #include "helperfuncs.h"
35 #include "connection.h"
36
37 extern time_t TIME;
38 extern int MaxConn;
39
40 std::deque<std::string> xsums;
41
42 serverrec::serverrec()
43 {
44         strlcpy(name,"",256);
45         pingtime = 0;
46         lastping = TIME;
47         usercount_i = usercount = opercount = version = 0;
48         hops_away = 1;
49         signon = TIME;
50         jupiter = false;
51         fd = 0;
52         sync_soon = false;
53         strlcpy(nickserv,"",NICKMAX);
54         connectors.clear();
55 }
56
57  
58 serverrec::~serverrec()
59 {
60 }
61
62 serverrec::serverrec(char* n, long ver, bool jupe)
63 {
64         strlcpy(name,n,256);
65         lastping = TIME;
66         usercount_i = usercount = opercount = 0;
67         version = ver;
68         hops_away = 1;
69         signon = TIME;
70         jupiter = jupe;
71         fd = 0;
72         sync_soon = false;
73         strlcpy(nickserv,"",NICKMAX);
74         connectors.clear();
75 }
76
77 bool serverrec::CreateListener(char* newhost, int p)
78 {
79         sockaddr_in host_address;
80         int flags;
81         in_addr addy;
82         int on = 0;
83         struct linger linger = { 0 };
84
85         this->port = p;
86
87         fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
88         if (fd <= 0)
89         {
90                 return false;
91         }
92
93         setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
94         linger.l_onoff = 1;
95         linger.l_linger = 1;
96         setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
97
98         // attempt to increase socket sendq and recvq as high as its possible
99         // to get them on linux.
100         int sendbuf = 32768;
101         int recvbuf = 32768;
102         setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
103         setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
104
105         memset((void*)&host_address, 0, sizeof(host_address));
106
107         host_address.sin_family = AF_INET;
108
109         if (!strcmp(newhost,""))
110         {
111                 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
112         }
113         else
114         {
115                 inet_aton(newhost,&addy);
116                 host_address.sin_addr = addy;
117         }
118
119         host_address.sin_port = htons(p);
120
121         if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
122         {
123                 return false;
124         }
125
126         // make the socket non-blocking
127         flags = fcntl(fd, F_GETFL, 0);
128         fcntl(fd, F_SETFL, flags | O_NONBLOCK);
129
130         this->port = p;
131
132         listen(this->fd, MaxConn);
133
134         return true;
135 }
136
137
138 bool serverrec::BeginLink(char* targethost, int newport, char* password, char* servername, int myport)
139 {
140         char connect[MAXBUF];
141
142         ircd_connector connector;
143         ircd_connector *cn = this->FindHost(servername);
144
145
146         if (cn)
147         {
148                 WriteOpers("CONNECT aborted: Server %s already exists",servername);
149                 return false;
150         }
151
152
153         if (this->fd)
154         {
155                 if (connector.MakeOutboundConnection(targethost,newport))
156                 {
157                         // targethost has been turned into an ip...
158                         // we dont want this as the server name.
159                         connector.SetServerName(servername);
160                         snprintf(connect,MAXBUF,"S %s %s %lu %lu :%s",getservername().c_str(),password,(unsigned long)myport,(unsigned long)GetRevision(),getserverdesc().c_str());
161                         connector.SetState(STATE_NOAUTH_OUTBOUND);
162                         connector.SetHostAndPort(targethost, newport);
163                         this->connectors.push_back(connector);
164                         return this->SendPacket(connect, servername);
165                 }
166                 else
167                 {
168                         connector.SetState(STATE_DISCONNECTED);
169                         WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
170                 }
171         }
172         return false;
173 }
174
175
176 bool serverrec::MeshCookie(char* targethost, int newport, unsigned long cookie, char* servername)
177 {
178         char connect[MAXBUF];
179
180         ircd_connector connector;
181
182         WriteOpers("Establishing meshed link to %s:%d",servername,newport);
183
184         if (this->fd)
185         {
186                 if (connector.MakeOutboundConnection(targethost,newport))
187                 {
188                         // targethost has been turned into an ip...
189                         // we dont want this as the server name.
190                         connector.SetServerName(servername);
191                         snprintf(connect,MAXBUF,"- %lu %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
192                         connector.SetState(STATE_NOAUTH_OUTBOUND);
193                         connector.SetHostAndPort(targethost, newport);
194                         connector.SetState(STATE_CONNECTED);
195                         this->connectors.push_back(connector);
196                         return this->SendPacket(connect, servername);
197                 }
198                 else
199                 {
200                         connector.SetState(STATE_DISCONNECTED);
201                         WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
202                 }
203         }
204         return false;
205 }
206
207 bool serverrec::AddIncoming(int newfd, char* targethost, int sourceport)
208 {
209         ircd_connector connector;
210
211         // targethost has been turned into an ip...
212         // we dont want this as the server name.
213         connector.SetServerName(targethost);
214         connector.SetDescriptor(newfd);
215         connector.SetState(STATE_NOAUTH_INBOUND);
216         int flags = fcntl(newfd, F_GETFL, 0);
217         fcntl(newfd, F_SETFL, flags | O_NONBLOCK);
218         int sendbuf = 32768;
219         int recvbuf = 32768;
220         setsockopt(newfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
221         setsockopt(newfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
222         connector.SetHostAndPort(targethost, sourceport);
223         connector.SetState(STATE_NOAUTH_INBOUND);
224         log(DEBUG,"serverrec::AddIncoming() Added connection: %s:%d",targethost,sourceport);
225         this->connectors.push_back(connector);
226         return true;
227 }
228
229 void serverrec::TerminateLink(char* targethost)
230 {
231         // this locates the targethost in the serverrec::connectors vector of the class,
232         // and terminates it by sending it an SQUIT token and closing its descriptor.
233         // TerminateLink with a null string causes a terminate of ALL links
234 }
235
236 // Returns a pointer to the connector for 'host'
237 ircd_connector* serverrec::FindHost(std::string findhost)
238 {
239         for (int i = 0; i < this->connectors.size(); i++)
240         {
241                 if (this->connectors[i].GetServerName() == findhost)
242                 {
243                         return &this->connectors[i];
244                 }
245         }
246         return NULL;
247 }
248
249 bool serverrec::SendPacket(char *message, const char* sendhost)
250 {
251         if ((!message) || (!sendhost))
252                 return true;
253
254         ircd_connector* cn = this->FindHost(sendhost);
255
256         if (!strchr(message,'\n'))
257         {
258                 strlcat(message,"\n",MAXBUF);
259         }
260
261         if (cn)
262         {
263                 log(DEBUG,"main: serverrec::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
264
265                 if (cn->GetState() == STATE_DISCONNECTED)
266                 {
267                         log(DEBUG,"\n\n\n\nMain route to %s is down, seeking alternative\n\n\n\n",sendhost);
268                         // fix: can only route one hop to avoid a loop
269                         if (strncmp(message,"R ",2))
270                         {
271                                 log(DEBUG,"Not a double reroute");
272                                 // this route is down, we must re-route the packet through an available point in the mesh.
273                                 for (int k = 0; k < this->connectors.size(); k++)
274                                 {
275                                         log(DEBUG,"Check connector %d: %s",k,this->connectors[k].GetServerName().c_str());
276                                         // search for another point in the mesh which can 'reach' where we want to go
277                                         for (int m = 0; m < this->connectors[k].routes.size(); m++)
278                                         {
279                                                 if (!strcasecmp(this->connectors[k].routes[m].c_str(),sendhost))
280                                                 {
281                                                         log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
282                                                         char buffer[MAXBUF];
283                                                         snprintf(buffer,MAXBUF,"R %s %s",sendhost,message);
284                                                         this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
285                                                         return true;
286                                                 }
287                                         }
288                                 }
289                         }
290                         char buffer[MAXBUF];
291                         snprintf(buffer,MAXBUF,"& %s",sendhost);
292                         NetSendToAllExcept(sendhost,buffer);
293                         log(DEBUG,"\n\nThere are no routes to %s, we're gonna boot the server off!\n\n",sendhost);
294                         DoSplit(sendhost);
295                         return false;
296                 }
297
298                 // returns false if the packet could not be sent (e.g. target host down)
299                 if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
300                 {
301                         log(DEBUG,"send() failed for serverrec::SendPacket(): %s",strerror(errno));
302                         log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
303                         cn->CloseConnection();
304                         cn->SetState(STATE_DISCONNECTED);
305                         // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
306                         return this->SendPacket(message,sendhost);
307                 }
308                 return true;
309         }
310 }
311
312 bool already_have_sum(std::string sum)
313 {
314         for (int i = 0; i < xsums.size(); i++)
315         {
316                 if (xsums[i] == sum)
317                 {
318                         return true;
319                 }
320         }
321         if (xsums.size() >= 128)
322         {
323                 xsums.pop_front();
324         }
325         xsums.push_back(sum);
326         return false;
327 }
328
329 // receives a packet from any where there is data waiting, first come, first served
330 // fills the message and host values with the host where the data came from.
331
332 bool serverrec::RecvPacket(std::deque<std::string> &messages, char* recvhost,std::deque<std::string> &sums)
333 {
334         char data[65536];
335         memset(data, 0, 65536);
336         for (int i = 0; i < this->connectors.size(); i++)
337         {
338                 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
339                 {
340                         // returns false if the packet could not be sent (e.g. target host down)
341                         int rcvsize = 0;
342
343                         // check if theres any data on this socket
344                         // if not, continue onwards to the next.
345                         pollfd polls;
346                         polls.fd = this->connectors[i].GetDescriptor();
347                         polls.events = POLLIN;
348                         int ret = poll(&polls,1,1);
349                         if (ret <= 0) continue;
350
351                         rcvsize = recv(this->connectors[i].GetDescriptor(),data,65000,0);
352                         data[rcvsize] = '\0';
353                         if (rcvsize == -1)
354                         {
355                                 if (errno != EAGAIN)
356                                 {
357                                         log(DEBUG,"recv() failed for serverrec::RecvPacket(): %s",strerror(errno));
358                                         log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
359                                         this->connectors[i].CloseConnection();
360                                         this->connectors[i].SetState(STATE_DISCONNECTED);
361                                 }
362                         }
363                         int pushed = 0;
364                         if (rcvsize > 0)
365                         {
366                                 this->connectors[i].AddBuffer(data);
367                                 if (this->connectors[i].BufferIsComplete())
368                                 {
369                                         while (this->connectors[i].BufferIsComplete())
370                                         {
371                                                 std::string text = this->connectors[i].GetBuffer();
372                                                 if (text != "")
373                                                 {
374                                                         if ((text[0] == ':') && (text.find(" ") != std::string::npos))
375                                                         {
376                                                                 std::string orig = text;
377                                                                 log(DEBUG,"Original: %s",text.c_str());
378                                                                 std::string sum = text.substr(1,text.find(" ")-1);
379                                                                 text = text.substr(text.find(" ")+1,text.length());
380                                                                 std::string possible_token = text.substr(1,text.find(" ")-1);
381                                                                 if (possible_token.length() > 1)
382                                                                 {
383                                                                         sums.push_back("*");
384                                                                         text = orig;
385                                                                         log(DEBUG,"Non-mesh, non-tokenized string passed up the chain");
386                                                                 }
387                                                                 else
388                                                                 {
389                                                                         log(DEBUG,"Packet sum: '%s'",sum.c_str());
390                                                                         if ((already_have_sum(sum)) && (sum != "*"))
391                                                                         {
392                                                                                 // we don't accept dupes
393                                                                                 continue;
394                                                                         }
395                                                                         sums.push_back(sum.c_str());
396                                                                 }
397                                                         }
398                                                         else sums.push_back("*");
399                                                         messages.push_back(text.c_str());
400                                                         strlcpy(recvhost,this->connectors[i].GetServerName().c_str(),160);
401                                                         log(DEBUG,"serverrec::RecvPacket() %d:%s->%s",pushed++,recvhost,text.c_str());
402                                                 }
403                                         }
404                                         return true;
405                                 }
406                         }
407                 }
408         }
409         // nothing new yet -- message and host will be undefined
410         return false;
411 }
412