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