Hm.
[oweals/tinc.git] / src / pokey / 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.1 2002/04/28 12:46:26 zarq 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 <unistd.h>
40 #include <sys/ioctl.h>
41 /* SunOS really wants sys/socket.h BEFORE net/if.h,
42    and FreeBSD wants these lines below the rest. */
43 #include <arpa/inet.h>
44 #include <sys/socket.h>
45 #include <net/if.h>
46
47 #include <openssl/rand.h>
48
49 #include <gtk/gtk.h>
50
51 #include <utils.h>
52 #include <xalloc.h>
53 #include <avl_tree.h>
54 #include <list.h>
55
56 #include "conf.h"
57 #include "interface.h"
58 #include "connection.h"
59 #include "meta.h"
60 #include "net.h"
61 #include "netutl.h"
62 #include "process.h"
63 #include "protocol.h"
64 #include "subnet.h"
65 #include "graph.h"
66 #include "process.h"
67 #include "route.h"
68 #include "device.h"
69 #include "event.h"
70 #include "logging.h"
71
72 #include "system.h"
73
74 int do_prune = 0;
75 int do_purge = 0;
76 int sighup = 0;
77 int sigalrm = 0;
78
79 time_t now = 0;
80
81 /*
82   put all file descriptors in an fd_set array
83 */
84 void build_fdset(fd_set *fs)
85 {
86   avl_node_t *node;
87   connection_t *c;
88   int i;
89 cp
90   FD_ZERO(fs);
91
92   for(node = connection_tree->head; node; node = node->next)
93     {
94       c = (connection_t *)node->data;
95       FD_SET(c->socket, fs);
96     }
97
98   for(i = 0; i < listen_sockets; i++)
99     {
100       FD_SET(listen_socket[i].tcp, fs);
101       FD_SET(listen_socket[i].udp, fs);
102     }
103 cp
104 }
105
106 /* Purge edges and subnets of unreachable nodes. Use carefully. */
107
108 void purge(void)
109 {
110   avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
111   node_t *n;
112   edge_t *e;
113   subnet_t *s;
114   connection_t *c;
115 cp
116   log(DEBUG_PROTOCOL, TLOG_DEBUG,
117       _("Purging unreachable nodes"));
118
119   for(nnode = node_tree->head; nnode; nnode = nnext)
120   {
121     nnext = nnode->next;
122     n = (node_t *)nnode->data;
123
124     if(!n->status.reachable)
125     {
126       if(debug_lvl >= DEBUG_SCARY_THINGS)
127         syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
128
129       for(snode = n->subnet_tree->head; snode; snode = snext)
130       {
131         snext = snode->next;
132         s = (subnet_t *)snode->data;
133
134         for(cnode = connection_tree->head; cnode; cnode = cnode->next)
135         {
136           c = (connection_t *)cnode->data;
137           if(c->status.active)
138             send_del_subnet(c, s);
139         }
140
141         subnet_del(n, s);
142       }
143
144       for(enode = n->edge_tree->head; enode; enode = enext)
145       {
146         enext = enode->next;
147         e = (edge_t *)enode->data;
148
149         for(cnode = connection_tree->head; cnode; cnode = cnode->next)
150         {
151           c = (connection_t *)cnode->data;
152           if(c->status.active)
153             send_del_edge(c, e);
154         }
155
156         edge_del(e);
157       }
158
159       node_del(n);
160     }
161   }
162 cp
163 }
164
165 /*
166   Terminate a connection:
167   - Close the socket
168   - Remove associated edge and tell other connections about it if report = 1
169   - Check if we need to retry making an outgoing connection
170   - Deactivate the host
171 */
172 void terminate_connection(connection_t *c, int report)
173 {
174   avl_node_t *node;
175   connection_t *other;
176 cp
177   if(c->status.remove)
178     return;
179
180   if(debug_lvl >= DEBUG_CONNECTIONS)
181     syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
182            c->name, c->hostname);
183
184   c->status.remove = 1;
185
186   if(c->socket)
187     close(c->socket);
188
189   if(c->edge)
190     {
191       if(report)
192         {
193           for(node = connection_tree->head; node; node = node->next)
194             {
195               other = (connection_t *)node->data;
196               if(other->status.active && other != c)
197                 send_del_edge(other, c->edge);
198             }
199         }
200
201       edge_del(c->edge);
202
203       /* Run MST and SSSP algorithms */
204
205       graph();
206     }
207
208   /* Check if this was our outgoing connection */
209
210   if(c->outgoing)
211     {
212       retry_outgoing(c->outgoing);
213       c->outgoing = NULL;
214     }
215
216   /* Deactivate */
217
218   c->status.active = 0;
219   if(c->node)
220     c->node->connection = NULL;
221   do_prune = 1;
222 cp
223 }
224
225 /*
226   Check if the other end is active.
227   If we have sent packets, but didn't receive any,
228   then possibly the other end is dead. We send a
229   PING request over the meta connection. If the other
230   end does not reply in time, we consider them dead
231   and close the connection.
232 */
233 void check_dead_connections(void)
234 {
235   avl_node_t *node, *next;
236   connection_t *c;
237 cp
238   for(node = connection_tree->head; node; node = next)
239     {
240       next = node->next;
241       c = (connection_t *)node->data;
242       if(c->last_ping_time + pingtimeout < now)
243         {
244           if(c->status.active)
245             {
246               if(c->status.pinged)
247                 {
248                   if(debug_lvl >= DEBUG_PROTOCOL)
249                     syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
250                            c->name, c->hostname);
251                   c->status.timeout = 1;
252                   terminate_connection(c, 1);
253                 }
254               else
255                 {
256                   send_ping(c);
257                 }
258             }
259           else
260             {
261               if(debug_lvl >= DEBUG_CONNECTIONS)
262                 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
263                        c->name, c->hostname);
264               terminate_connection(c, 0);
265             }
266         }
267     }
268 cp
269 }
270
271 /*
272   check all connections to see if anything
273   happened on their sockets
274 */
275 void check_network_activity(fd_set *f)
276 {
277   connection_t *c;
278   avl_node_t *node;
279   int result, i;
280   int len = sizeof(result);
281 cp
282   for(i = 0; i < listen_sockets; i++)
283     {
284       if(FD_ISSET(listen_socket[i].tcp, f))
285         handle_new_meta_connection(listen_socket[i].tcp);
286     }
287
288   for(node = connection_tree->head; node; node = node->next)
289     {
290       c = (connection_t *)node->data;
291
292       if(c->status.remove)
293         return;
294
295       if(FD_ISSET(c->socket, f))
296         {
297           if(c->status.connecting)
298             {
299               c->status.connecting = 0;
300               getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
301               if(!result)
302                 finish_connecting(c);
303               else
304                 {
305                   if(debug_lvl >= DEBUG_CONNECTIONS)
306                     syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
307                   close(c->socket);
308                   do_outgoing_connection(c);
309                   continue;
310                 }
311             }
312           if(receive_meta(c) < 0)
313             {
314               terminate_connection(c, c->status.active);
315               return;
316             }
317         }
318     }
319 cp
320 }
321
322 void prune_connections(void)
323 {
324   connection_t *c;
325   avl_node_t *node, *next;
326 cp
327   for(node = connection_tree->head; node; node = next)
328     {
329       next = node->next;
330       c = (connection_t *)node->data;
331
332       if(c->status.remove)
333         connection_del(c);
334     }
335
336   if(!connection_tree->head)
337     purge();
338 cp
339 }
340
341 /*
342   this is where it all happens...
343 */
344 void main_loop(void)
345 {
346   fd_set fset;
347   struct timeval tv;
348   int r;
349   time_t last_ping_check;
350   event_t *event;
351 cp
352   last_ping_check = now;
353
354   srand(now);
355
356   for(;;)
357     {
358       now = time(NULL);
359
360 /*       tv.tv_sec = 1 + (rand() & 7); /\* Approx. 5 seconds, randomized to prevent global synchronisation effects *\/ */
361 /*       tv.tv_usec = 0; */
362       tv.tv_sec = 0;
363       tv.tv_usec = 50000;
364
365       if(do_prune)
366         {
367           prune_connections();
368           do_prune = 0;
369         }
370
371       build_fdset(&fset);
372
373       while(gtk_events_pending()) 
374         if(gtk_main_iteration() == FALSE)
375           return;
376
377       if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
378         {
379           if(errno != EINTR) /* because of a signal */
380             {
381               syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
382               return;
383             }
384         }
385
386       if(r > 0)
387         check_network_activity(&fset);
388
389       if(do_purge)
390         {
391           purge();
392           do_purge = 0;
393         }
394
395       /* Let's check if everybody is still alive */
396
397       if(last_ping_check + pingtimeout < now)
398         {
399           check_dead_connections();
400           last_ping_check = now;
401
402           if(routing_mode== RMODE_SWITCH)
403             age_mac();
404
405           age_past_requests();
406
407           /* Should we regenerate our key? */
408
409           if(keyexpires < now)
410             {
411               if(debug_lvl >= DEBUG_STATUS)
412                 syslog(LOG_INFO, _("Regenerating symmetric key"));
413
414               RAND_pseudo_bytes(myself->key, myself->keylength);
415               send_key_changed(myself->connection, myself);
416               keyexpires = now + keylifetime;
417             }
418         }
419
420
421       while((event = get_expired_event()))
422         {
423           event->handler(event->data);
424           free(event);
425         }
426
427       if(sigalrm)
428         {
429           syslog(LOG_INFO, _("Flushing event queue"));
430
431           while(event_tree->head)
432             {
433               event = (event_t *)event_tree->head->data;
434               event->handler(event->data);
435               event_del(event);
436             }
437           sigalrm = 0;
438         }
439
440       if(sighup)
441         {
442           sighup = 0;
443           close_network_connections();
444           exit_configuration(&config_tree);
445
446           syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
447           sleep(5);
448
449           init_configuration(&config_tree);
450
451           if(read_server_config())
452             {
453               syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
454               exit(1);
455             }
456
457           if(setup_network_connections())
458             return;
459
460           continue;
461         }
462
463       if(build_graph)
464         if_build_graph();
465     }
466 cp
467 }