]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/connection.cpp
Added buffering fix
[user/henk/code/inspircd.git] / src / connection.cpp
1 #include <connection.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <sys/errno.h>
5 #include <sys/ioctl.h>
6 #include <sys/utsname.h>
7 #include <errno.h>
8 #include <vector>
9 #include "inspircd.h"
10 #include "modules.h"
11
12 extern std::vector<Module*> modules;
13 extern std::vector<ircd_module*> factory;
14
15 extern int MODCOUNT;
16
17 #define STATE_CLEAR 1
18 #define STATE_WAIT_FOR_ACK 2
19
20 packet::packet()
21 {
22         srand(time(NULL));
23         id = random();
24 }
25
26 packet::~packet()
27 {
28 }
29
30 connection::connection()
31 {
32         key = GenKey();
33         fd = 0;
34         state = STATE_CLEAR;
35         buffer.clear();
36 }
37
38
39 bool connection::CreateListener(char* host, int p)
40 {
41         sockaddr_in host_address;
42         int flags;
43         in_addr addy;
44         int on = 0;
45         struct linger linger = { 0 };
46         
47         fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
48         if (fd <= 0)
49         {
50                 return false;
51         }
52
53         memset((void*)&host_address, 0, sizeof(host_address));
54
55         host_address.sin_family = AF_INET;
56
57         if (!strcmp(host,""))
58         {
59                 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
60         }
61         else
62         {
63                 inet_aton(host,&addy);
64                 host_address.sin_addr = addy;
65         }
66
67         host_address.sin_port = htons(p);
68
69         if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
70         {
71                 return false;
72         }
73
74         // make the socket non-blocking
75         flags = fcntl(fd, F_GETFL, 0);
76         fcntl(fd, F_SETFL, flags | O_NONBLOCK);
77
78         this->port = p;
79
80         setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
81         linger.l_onoff = 1;
82         linger.l_linger = 0;
83         setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
84
85         buffer.clear();
86         
87         return true;
88 }
89
90 bool connection::BeginLink(char* targethost, int port, char* password)
91 {
92         char connect[MAXBUF];
93         
94         if (this->fd)
95         {
96                 sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
97                 this->haspassed = false;
98                 return this->SendPacket(connect, targethost, port, 0);
99         }
100         return false;
101 }
102
103 // targethost: in dot notation a.b.c.d
104 void connection::TerminateLink(char* targethost)
105 {
106 }
107
108 // host: in dot notation a.b.c.d
109 // port: host byte order
110 bool connection::SendPacket(char *message, char* host, int port, long ourkey)
111 {
112         sockaddr_in host_address;
113         in_addr addy;
114         packet p;
115
116         memset((void*)&host_address, 0, sizeof(host_address));
117
118         host_address.sin_family = AF_INET;
119         inet_aton(host,&addy);
120         host_address.sin_addr = addy;
121
122         host_address.sin_port = htons(port);
123
124         strcpy(p.data,message);
125         p.type = PT_SYN_WITH_DATA;
126         p.key = ourkey;
127
128
129         FOREACH_MOD OnPacketTransmit(p.data);
130
131         log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s:%d",p.data,host,port);
132
133         // returns false if the packet could not be sent (e.g. target host down)
134         if (sendto(this->fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
135         {
136                 log(DEBUG,"sendto() failed for Connection::SendPacket() with a packet of size %d: %s",sizeof(p),strerror(errno));
137                 return false;
138         }
139         this->state = STATE_WAIT_FOR_ACK;
140
141
142         // host_address remains unchanged. we only want to receive from where we just sent the packet to.
143         
144         // retry the packet up to 5 times
145         for (int retries = 0; retries < 5; retries++)
146         {
147                 socklen_t host_address_size;
148                 host_address.sin_family=AF_INET;
149                 host_address_size=sizeof(host_address);
150         
151                 // wait for ack, or timeout.
152                 // if reached a timeout, send again.
153                 // the packet id in the ack must match that in the original packet
154                 // this MUST operate in lock/step fashion!!!
155                 int cycles = 0;
156                 packet p2;
157                 do 
158                 {
159                         fd_set sfd;
160                         timeval tval;
161                         tval.tv_usec = 100;
162                         tval.tv_sec = 0;
163                         FD_ZERO(&sfd);
164                         FD_SET(fd,&sfd);
165                         int res = select(65535, &sfd, NULL, NULL, &tval);
166                         cycles++;
167                 }
168                 while ((recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size)<0) && (cycles < 10));
169                 
170                 if (cycles >= 10)
171                 {
172                         log(DEFAULT,"ERROR! connection::SendPacket() waited >10000 nanosecs for an ACK. Will resend up to 5 times");
173                 }
174                 else
175                 {
176                         if (p2.type != PT_ACK_ONLY)
177                         {
178                                 packet_buf pb;
179                                 pb.p.id = p.id;
180                                 pb.p.key = p.key;
181                                 pb.p.type = p.type;
182                                 strcpy(pb.host,inet_ntoa(host_address.sin_addr));
183                                 pb.port = ntohs(host_address.sin_port);
184                                 this->buffer.push_back(pb);
185                                 
186                                 log(DEFAULT,"ERROR! connection::SendPacket() received a data response and was expecting an ACK!!!");
187                                 this->state = STATE_CLEAR;
188                                 return true;
189                         }
190
191                         if (p2.id != p.id)
192                         {
193                                 log(DEFAULT,"ERROR! connection::SendPacket() received an ack for a packet it didnt send!");
194                                 this->state = STATE_CLEAR;
195                                 return false;
196                         }
197                         else
198                         {
199                                 log(DEFAULT,"Successfully received ACK");
200                                 this->state = STATE_CLEAR;
201                                 return true;
202                                 break;
203                         }
204                 }
205         }
206         log(DEFAULT,"We never received an ack. Something fishy going on, host is dead.");
207         this->state = STATE_CLEAR;
208         return false;
209
210 }
211
212 bool connection::SendSYN(char* host, int port)
213 {
214         sockaddr_in host_address;
215         in_addr addy;
216         packet p;
217
218         memset((void*)&host_address, 0, sizeof(host_address));
219
220         host_address.sin_family = AF_INET;
221         inet_aton(host,&addy);
222         host_address.sin_addr = addy;
223
224         host_address.sin_port = htons(port);
225
226         p.type = PT_SYN_ONLY;
227         p.key = key;
228         strcpy(p.data,"");
229
230         if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
231         {
232                 return false;
233         }
234         return true;
235
236 }
237
238 bool connection::SendACK(char* host, int port, int reply_id)
239 {
240         sockaddr_in host_address;
241         in_addr addy;
242         packet p;
243
244         memset((void*)&host_address, 0, sizeof(host_address));
245
246         host_address.sin_family = AF_INET;
247         inet_aton(host,&addy);
248         host_address.sin_addr = addy;
249
250         host_address.sin_port = htons(port);
251
252         p.type = PT_ACK_ONLY;
253         p.key = key;
254         p.id = reply_id;
255         strcpy(p.data,"");
256
257         if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
258         {
259                 return false;
260         }
261
262 }
263
264
265 // Generates a server key. This is pseudo-random.
266 // the server always uses the same server-key in all communications
267 // across the network. All other servers must remember the server key
268 // of servers in the network, e.g.:
269 //
270 // ServerA:  key=5555555555
271 // ServerB:  key=6666666666
272 // I am ServerC: key=77777777777
273 //
274 // If ServerC sees a packet from ServerA, and the key stored for ServerA
275 // is 0, then cache the key as the servers key.
276 // after this point, any packet from ServerA which does not contain its key,
277 // 555555555, will be silently dropped.
278 // This should prevent blind spoofing, as to fake a server you must know its
279 // assigned key, and to do that you must receive messages that are origintated
280 // from it or hack the running executable.
281 //
282 // During the AUTH phase (when server passwords are checked, the key in any
283 // packet MUST be 0). Only the initial SERVER/PASS packets may have a key
284 // of 0 (and any ACK responses to them).
285 //
286
287 long connection::GenKey()
288 {
289         srand(time(NULL));
290         return (random()*time(NULL));
291 }
292
293 // host: in dot notation a.b.c.d
294 // port: host byte order
295 bool connection::RecvPacket(char *message, char* host, int &prt, long &theirkey)
296 {
297         // returns false if no packet waiting for receive, e.g. EAGAIN or ECONNRESET
298         sockaddr_in host_address;
299         socklen_t host_address_size;
300         packet p;
301         
302         memset((void*)&host_address, 0, sizeof(host_address));
303
304         host_address.sin_family=AF_INET;
305         host_address_size=sizeof(host_address);
306
307         //int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
308         if (recvfrom(fd,&p,sizeof(p),0,(sockaddr*)&host_address,&host_address_size)<0)
309         {
310                 if (buffer.size()>0)
311                 {
312                         log(DEBUG,"Fetching a buffered packet size %d",buffer.size());
313                         strcpy(message,buffer[0].p.data);
314                         theirkey = buffer[0].p.key;
315                         strcpy(host,buffer[0].host);
316                         prt = buffer[0].port;
317                         
318                         buffer.erase(buffer.begin());
319                         
320                         return true;
321                 }
322                 return false;
323         }
324
325         log(DEBUG,"connection::RecvPacket(): received packet type %d '%s' from '%s'",p.type,p.data,inet_ntoa(host_address.sin_addr));
326
327         if (p.type == PT_SYN_ONLY)
328         {
329                 strcpy(message,p.data);
330                 strcpy(host,inet_ntoa(host_address.sin_addr));
331                 prt = ntohs(host_address.sin_port);
332                 SendACK(host,this->port,p.id);
333                 return false;
334         }
335
336         if (p.type == PT_ACK_ONLY)
337         {
338                 strcpy(message,p.data);
339                 strcpy(host,inet_ntoa(host_address.sin_addr));
340                 prt = ntohs(host_address.sin_port);
341                 this->state = STATE_CLEAR;
342                 return false;
343         }
344
345         if (p.type == PT_SYN_WITH_DATA)
346         {
347                 strcpy(message,p.data);
348                 strcpy(host,inet_ntoa(host_address.sin_addr));
349                 theirkey = p.key;
350                 prt = ntohs(host_address.sin_port); // the port we received it on
351                 SendACK(host,prt,p.id);
352
353                 if (buffer.size()>0)
354                 {
355                         log(DEBUG,"Fetching a buffered packet size %d",buffer.size());
356                         packet_buf pb;
357                         pb.p.id = p.id;
358                         pb.p.key = p.key;
359                         pb.p.type = p.type;
360                         strcpy(pb.p.data,p.data);
361                         strcpy(pb.host,inet_ntoa(host_address.sin_addr));
362                         pb.port = ntohs(host_address.sin_port);
363                         this->buffer.push_back(pb);
364
365                         strcpy(message,buffer[0].p.data);
366                         theirkey = buffer[0].p.key;
367                         strcpy(host,buffer[0].host);
368                         prt = buffer[0].port;
369                         
370                         buffer.erase(buffer.begin());
371                 }
372
373                 return true;
374         }
375
376         log(DEBUG,"connection::RecvPacket(): Invalid packet type %d (protocol error)",p.type);
377         return true;
378 }
379