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