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