- Non-blocking connect()s.
[tinc] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000-2002 Guus Sliepen <guus@sliepen.warande.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: net.c,v 1.35.4.156 2002/02/18 16:25:16 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <netdb.h>
28 #include <netinet/in.h>
29 #ifdef HAVE_LINUX
30  #include <netinet/ip.h>
31  #include <netinet/tcp.h>
32 #endif
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <signal.h>
37 #include <sys/time.h>
38 #include <sys/types.h>
39 #include <syslog.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42 /* SunOS really wants sys/socket.h BEFORE net/if.h,
43    and FreeBSD wants these lines below the rest. */
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
46 #include <net/if.h>
47
48 #include <openssl/rand.h>
49
50 #include <utils.h>
51 #include <xalloc.h>
52 #include <avl_tree.h>
53 #include <list.h>
54
55 #include "conf.h"
56 #include "connection.h"
57 #include "meta.h"
58 #include "net.h"
59 #include "netutl.h"
60 #include "process.h"
61 #include "protocol.h"
62 #include "subnet.h"
63 #include "graph.h"
64 #include "process.h"
65 #include "route.h"
66 #include "device.h"
67 #include "event.h"
68
69 #include "system.h"
70
71 int do_prune = 0;
72 int do_purge = 0;
73 int sighup = 0;
74 int sigalrm = 0;
75
76 /*
77   put all file descriptors in an fd_set array
78 */
79 void build_fdset(fd_set *fs)
80 {
81   avl_node_t *node;
82   connection_t *c;
83 cp
84   FD_ZERO(fs);
85
86   for(node = connection_tree->head; node; node = node->next)
87     {
88       c = (connection_t *)node->data;
89       FD_SET(c->socket, fs);
90     }
91
92   FD_SET(tcp_socket, fs);
93   FD_SET(udp_socket, fs);
94   FD_SET(device_fd, fs);
95 cp
96 }
97
98 /* Purge edges and subnets of unreachable nodes. Use carefully. */
99
100 void purge(void)
101 {
102   avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
103   node_t *n;
104   edge_t *e;
105   subnet_t *s;
106   connection_t *c;
107 cp
108   if(debug_lvl >= DEBUG_PROTOCOL)
109     syslog(LOG_DEBUG, _("Purging unreachable nodes"));
110
111   for(nnode = node_tree->head; nnode; nnode = nnext)
112   {
113     nnext = nnode->next;
114     n = (node_t *)nnode->data;
115
116     if(!n->status.reachable)
117     {
118       if(debug_lvl >= DEBUG_SCARY_THINGS)
119         syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
120
121       for(snode = n->subnet_tree->head; snode; snode = snext)
122       {
123         snext = snode->next;
124         s = (subnet_t *)snode->data;
125
126         for(cnode = connection_tree->head; cnode; cnode = cnode->next)
127         {
128           c = (connection_t *)cnode->data;
129           if(c->status.active)
130             send_del_subnet(c, s);
131         }
132
133         subnet_del(n, s);
134       }
135
136       for(enode = n->edge_tree->head; enode; enode = enext)
137       {
138         enext = enode->next;
139         e = (edge_t *)enode->data;
140
141         for(cnode = connection_tree->head; cnode; cnode = cnode->next)
142         {
143           c = (connection_t *)cnode->data;
144           if(c->status.active)
145             send_del_edge(c, e);
146         }
147
148         edge_del(e);
149       }
150
151       node_del(n);
152     }
153   }
154 cp
155 }
156
157 /*
158   Terminate a connection:
159   - Close the socket
160   - Remove associated edge and tell other connections about it if report = 1
161   - Check if we need to retry making an outgoing connection
162   - Deactivate the host
163 */
164 void terminate_connection(connection_t *c, int report)
165 {
166   avl_node_t *node;
167   connection_t *other;
168 cp
169   if(c->status.remove)
170     return;
171
172   if(debug_lvl >= DEBUG_CONNECTIONS)
173     syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
174            c->name, c->hostname);
175
176   c->status.remove = 1;
177
178   if(c->socket)
179     close(c->socket);
180
181   if(c->edge)
182     {
183       if(report)
184         {
185           for(node = connection_tree->head; node; node = node->next)
186             {
187               other = (connection_t *)node->data;
188               if(other->status.active && other != c)
189                 send_del_edge(other, c->edge);
190             }
191         }
192
193       edge_del(c->edge);
194     }
195
196   /* Run MST and SSSP algorithms */
197
198   graph();
199
200   /* Check if this was our outgoing connection */
201
202   if(c->outgoing)
203     {
204       retry_outgoing(c->outgoing);
205       c->outgoing = NULL;
206     }
207
208   /* Deactivate */
209
210   c->status.active = 0;
211   if(c->node)
212     c->node->connection = NULL;
213   do_prune = 1;
214 cp
215 }
216
217 /*
218   Check if the other end is active.
219   If we have sent packets, but didn't receive any,
220   then possibly the other end is dead. We send a
221   PING request over the meta connection. If the other
222   end does not reply in time, we consider them dead
223   and close the connection.
224 */
225 void check_dead_connections(void)
226 {
227   time_t now;
228   avl_node_t *node, *next;
229   connection_t *c;
230 cp
231   now = time(NULL);
232
233   for(node = connection_tree->head; node; node = next)
234     {
235       next = node->next;
236       c = (connection_t *)node->data;
237       if(c->last_ping_time + pingtimeout < now)
238         {
239           if(c->status.active)
240             {
241               if(c->status.pinged)
242                 {
243                   if(debug_lvl >= DEBUG_PROTOCOL)
244                     syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
245                            c->name, c->hostname);
246                   c->status.timeout = 1;
247                   terminate_connection(c, 1);
248                 }
249               else
250                 {
251                   send_ping(c);
252                 }
253             }
254           else
255             {
256               if(debug_lvl >= DEBUG_CONNECTIONS)
257                 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
258                        c->name, c->hostname);
259               terminate_connection(c, 0);
260             }
261         }
262     }
263 cp
264 }
265
266 /*
267   check all connections to see if anything
268   happened on their sockets
269 */
270 void check_network_activity(fd_set *f)
271 {
272   connection_t *c;
273   avl_node_t *node;
274   int result;
275   int len = sizeof(result);
276   vpn_packet_t packet;
277 cp
278   if(FD_ISSET(device_fd, f))
279     {
280       if(!read_packet(&packet))
281         route_outgoing(&packet);
282     }
283
284   if(FD_ISSET(udp_socket, f))
285     handle_incoming_vpn_data();
286
287   for(node = connection_tree->head; node; node = node->next)
288     {
289       c = (connection_t *)node->data;
290
291       if(c->status.remove)
292         return;
293
294       if(FD_ISSET(c->socket, f))
295         {
296           if(c->status.connecting)
297             {
298               c->status.connecting = 0;
299               getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
300               if(!result)
301                 finish_connecting(c);
302               else
303                 {
304                   if(debug_lvl >= DEBUG_CONNECTIONS)
305                     syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
306                   close(c->socket);
307                   do_outgoing_connection(c);
308                   continue;
309                 }
310             }
311           if(receive_meta(c) < 0)
312             {
313               terminate_connection(c, c->status.active);
314               return;
315             }
316         }
317     }
318
319   if(FD_ISSET(tcp_socket, f))
320     handle_new_meta_connection();
321 cp
322 }
323
324 void prune_connections(void)
325 {
326   connection_t *c;
327   avl_node_t *node, *next;
328 cp
329   for(node = connection_tree->head; node; node = next)
330     {
331       next = node->next;
332       c = (connection_t *)node->data;
333
334       if(c->status.remove)
335         connection_del(c);
336     }
337
338   if(!connection_tree->head)
339     purge();
340 cp
341 }
342
343 /*
344   this is where it all happens...
345 */
346 void main_loop(void)
347 {
348   fd_set fset;
349   struct timeval tv;
350   int r;
351   time_t last_ping_check;
352   int t;
353   event_t *event;
354 cp
355   last_ping_check = time(NULL);
356
357   srand(time(NULL));
358
359   for(;;)
360     {
361       tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
362       tv.tv_usec = 0;
363
364       build_fdset(&fset);
365
366       if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
367         {
368           if(errno != EINTR) /* because of a signal */
369             {
370               syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
371               return;
372             }
373         }
374
375       if(r > 0)
376         check_network_activity(&fset);
377
378       if(do_prune)
379         {
380           prune_connections();
381           do_prune = 0;
382         }
383
384      if(do_purge)
385         {
386           purge();
387           do_purge = 0;
388         }
389
390       t = time(NULL);
391
392       /* Let's check if everybody is still alive */
393
394       if(last_ping_check + pingtimeout < t)
395         {
396           check_dead_connections();
397           last_ping_check = time(NULL);
398
399           /* Should we regenerate our key? */
400
401           if(keyexpires < t)
402             {
403               if(debug_lvl >= DEBUG_STATUS)
404                 syslog(LOG_INFO, _("Regenerating symmetric key"));
405
406               RAND_pseudo_bytes(myself->key, myself->keylength);
407               send_key_changed(myself->connection, myself);
408               keyexpires = time(NULL) + keylifetime;
409             }
410         }
411
412
413       while((event = get_expired_event()))
414         {
415           event->handler(event->data);
416           free(event);
417         }
418
419       if(sigalrm)
420         {
421           syslog(LOG_INFO, _("Flushing event queue"));
422
423           while(event_tree->head)
424             {
425               event = (event_t *)event_tree->head->data;
426               event->handler(event->data);
427               event_del(event);
428             }
429           sigalrm = 0;
430         }
431
432       if(sighup)
433         {
434           syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds"));
435           sighup = 0;
436           close_network_connections();
437           exit_configuration(&config_tree);
438
439           if(read_server_config())
440             {
441               syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
442               exit(1);
443             }
444
445           sleep(5);
446
447           if(setup_network_connections())
448             return;
449
450           continue;
451         }
452     }
453 cp
454 }