]> git.meshlink.io Git - meshlink/blob - src/net_setup.c
Remove support for the legacy protocol.
[meshlink] / src / net_setup.c
1 /*
2     net_setup.c -- Setup.
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "cipher.h"
23 #include "conf.h"
24 #include "connection.h"
25 #include "digest.h"
26 #include "ecdsa.h"
27 #include "graph.h"
28 #include "logger.h"
29 #include "net.h"
30 #include "netutl.h"
31 #include "protocol.h"
32 #include "route.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 char *myport;
37
38 char *proxyhost;
39 char *proxyport;
40 char *proxyuser;
41 char *proxypass;
42 proxytype_t proxytype;
43 int autoconnect;
44 bool disablebuggypeers;
45
46 bool node_read_ecdsa_public_key(node_t *n) {
47         if(ecdsa_active(n->ecdsa))
48                 return true;
49
50         splay_tree_t *config_tree;
51         FILE *fp;
52         char *pubname = NULL;
53         char *p;
54
55         init_configuration(&config_tree);
56         if(!read_host_config(config_tree, n->name))
57                 goto exit;
58
59         /* First, check for simple ECDSAPublicKey statement */
60
61         if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
62                 n->ecdsa = ecdsa_set_base64_public_key(p);
63                 free(p);
64                 goto exit;
65         }
66
67         /* Else, check for ECDSAPublicKeyFile statement and read it */
68
69         if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
70                 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
71
72         fp = fopen(pubname, "r");
73
74         if(!fp)
75                 goto exit;
76
77         n->ecdsa = ecdsa_read_pem_public_key(fp);
78         fclose(fp);
79
80 exit:
81         exit_configuration(&config_tree);
82         free(pubname);
83         return n->ecdsa;
84 }
85
86 bool read_ecdsa_public_key(connection_t *c) {
87         if(ecdsa_active(c->ecdsa))
88                 return true;
89
90         FILE *fp;
91         char *fname;
92         char *p;
93
94         if(!c->config_tree) {
95                 init_configuration(&c->config_tree);
96                 if(!read_host_config(c->config_tree, c->name))
97                         return false;
98         }
99
100         /* First, check for simple ECDSAPublicKey statement */
101
102         if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
103                 c->ecdsa = ecdsa_set_base64_public_key(p);
104                 free(p);
105                 return c->ecdsa;
106         }
107
108         /* Else, check for ECDSAPublicKeyFile statement and read it */
109
110         if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
111                 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
112
113         fp = fopen(fname, "r");
114
115         if(!fp) {
116                 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
117                            fname, strerror(errno));
118                 free(fname);
119                 return false;
120         }
121
122         c->ecdsa = ecdsa_read_pem_public_key(fp);
123         fclose(fp);
124
125         if(!c->ecdsa)
126                 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
127         free(fname);
128         return c->ecdsa;
129 }
130
131 static bool read_ecdsa_private_key(void) {
132         FILE *fp;
133         char *fname;
134
135         /* Check for PrivateKeyFile statement and read it */
136
137         if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
138                 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
139
140         fp = fopen(fname, "r");
141
142         if(!fp) {
143                 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
144                 if(errno == ENOENT)
145                         logger(DEBUG_ALWAYS, LOG_INFO, "Create an ECDSA keypair with `tinc generate-ecdsa-keys'.");
146                 free(fname);
147                 return false;
148         }
149
150 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
151         struct stat s;
152
153         if(fstat(fileno(fp), &s)) {
154                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
155                 free(fname);
156                 return false;
157         }
158
159         if(s.st_mode & ~0100700)
160                 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
161 #endif
162
163         myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
164         fclose(fp);
165
166         if(!myself->connection->ecdsa)
167                 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
168         free(fname);
169         return myself->connection->ecdsa;
170 }
171
172 static bool read_invitation_key(void) {
173         FILE *fp;
174         char *fname;
175
176         if(invitation_key) {
177                 ecdsa_free(invitation_key);
178                 invitation_key = NULL;
179         }
180
181         xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase);
182
183         fp = fopen(fname, "r");
184
185         if(fp) {
186                 invitation_key = ecdsa_read_pem_private_key(fp);
187                 fclose(fp);
188                 if(!invitation_key)
189                         logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
190         }
191
192         free(fname);
193         return invitation_key;
194 }
195
196 static timeout_t keyexpire_timeout;
197
198 static void keyexpire_handler(void *data) {
199         regenerate_key();
200         timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
201 }
202
203 void regenerate_key(void) {
204         logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
205         send_key_changed();
206 }
207
208 void load_all_nodes(void) {
209         DIR *dir;
210         struct dirent *ent;
211         char *dname;
212
213         xasprintf(&dname, "%s" SLASH "hosts", confbase);
214         dir = opendir(dname);
215         if(!dir) {
216                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
217                 free(dname);
218                 return;
219         }
220
221         while((ent = readdir(dir))) {
222                 if(!check_id(ent->d_name))
223                         continue;
224
225                 node_t *n = lookup_node(ent->d_name);
226                 if(n)
227                         continue;
228
229                 n = new_node();
230                 n->name = xstrdup(ent->d_name);
231                 node_add(n);
232         }
233
234         closedir(dir);
235 }
236
237
238 char *get_name(void) {
239         char *name = NULL;
240
241         get_config_string(lookup_config(config_tree, "Name"), &name);
242
243         if(!name)
244                 return NULL;
245
246         if(*name == '$') {
247                 char *envname = getenv(name + 1);
248                 char hostname[32] = "";
249                 if(!envname) {
250                         if(strcmp(name + 1, "HOST")) {
251                                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
252                                 return false;
253                         }
254                         if(gethostname(hostname, sizeof hostname) || !*hostname) {
255                                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
256                                 return false;
257                         }
258                         hostname[31] = 0;
259                         envname = hostname;
260                 }
261                 free(name);
262                 name = xstrdup(envname);
263                 for(char *c = name; *c; c++)
264                         if(!isalnum(*c))
265                                 *c = '_';
266         }
267
268         if(!check_id(name)) {
269                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
270                 free(name);
271                 return false;
272         }
273
274         return name;
275 }
276
277 bool setup_myself_reloadable(void) {
278         char *proxy = NULL;
279         char *rmode = NULL;
280         char *fmode = NULL;
281         char *bmode = NULL;
282         char *afname = NULL;
283         char *address = NULL;
284         char *space;
285         bool choice;
286
287         get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
288         if(proxy) {
289                 if((space = strchr(proxy, ' ')))
290                         *space++ = 0;
291
292                 if(!strcasecmp(proxy, "none")) {
293                         proxytype = PROXY_NONE;
294                 } else if(!strcasecmp(proxy, "socks4")) {
295                         proxytype = PROXY_SOCKS4;
296                 } else if(!strcasecmp(proxy, "socks4a")) {
297                         proxytype = PROXY_SOCKS4A;
298                 } else if(!strcasecmp(proxy, "socks5")) {
299                         proxytype = PROXY_SOCKS5;
300                 } else if(!strcasecmp(proxy, "http")) {
301                         proxytype = PROXY_HTTP;
302                 } else if(!strcasecmp(proxy, "exec")) {
303                         proxytype = PROXY_EXEC;
304                 } else {
305                         logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
306                         return false;
307                 }
308
309                 switch(proxytype) {
310                         case PROXY_NONE:
311                         default:
312                                 break;
313
314                         case PROXY_EXEC:
315                                 if(!space || !*space) {
316                                         logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
317                                         return false;
318                                 }
319                                 proxyhost =  xstrdup(space);
320                                 break;
321
322                         case PROXY_SOCKS4:
323                         case PROXY_SOCKS4A:
324                         case PROXY_SOCKS5:
325                         case PROXY_HTTP:
326                                 proxyhost = space;
327                                 if(space && (space = strchr(space, ' ')))
328                                         *space++ = 0, proxyport = space;
329                                 if(space && (space = strchr(space, ' ')))
330                                         *space++ = 0, proxyuser = space;
331                                 if(space && (space = strchr(space, ' ')))
332                                         *space++ = 0, proxypass = space;
333                                 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
334                                         logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
335                                         return false;
336                                 }
337                                 proxyhost = xstrdup(proxyhost);
338                                 proxyport = xstrdup(proxyport);
339                                 if(proxyuser && *proxyuser)
340                                         proxyuser = xstrdup(proxyuser);
341                                 if(proxypass && *proxypass)
342                                         proxypass = xstrdup(proxypass);
343                                 break;
344                 }
345
346                 free(proxy);
347         }
348
349         if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
350                 myself->options |= OPTION_INDIRECT;
351
352         if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
353                 myself->options |= OPTION_TCPONLY;
354
355         if(myself->options & OPTION_TCPONLY)
356                 myself->options |= OPTION_INDIRECT;
357
358         get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
359         get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
360
361         memset(&localdiscovery_address, 0, sizeof localdiscovery_address);
362         if(get_config_string(lookup_config(config_tree, "LocalDiscoveryAddress"), &address)) {
363                 struct addrinfo *ai = str2addrinfo(address, myport, SOCK_DGRAM);
364                 free(address);
365                 if(!ai)
366                         return false;
367                 memcpy(&localdiscovery_address, ai->ai_addr, ai->ai_addrlen);
368         }
369
370
371         if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
372                 if(!strcasecmp(rmode, "router"))
373                         routing_mode = RMODE_ROUTER;
374                 else if(!strcasecmp(rmode, "switch"))
375                         routing_mode = RMODE_SWITCH;
376                 else if(!strcasecmp(rmode, "hub"))
377                         routing_mode = RMODE_HUB;
378                 else {
379                         logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
380                         return false;
381                 }
382                 free(rmode);
383         }
384
385         if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
386                 if(!strcasecmp(fmode, "off"))
387                         forwarding_mode = FMODE_OFF;
388                 else if(!strcasecmp(fmode, "internal"))
389                         forwarding_mode = FMODE_INTERNAL;
390                 else if(!strcasecmp(fmode, "kernel"))
391                         forwarding_mode = FMODE_KERNEL;
392                 else {
393                         logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
394                         return false;
395                 }
396                 free(fmode);
397         }
398
399         choice = true;
400         get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
401         if(choice)
402                 myself->options |= OPTION_PMTU_DISCOVERY;
403
404         choice = true;
405         get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
406         if(choice)
407                 myself->options |= OPTION_CLAMP_MSS;
408
409         get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
410         get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
411         if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
412                 if(!strcasecmp(bmode, "no"))
413                         broadcast_mode = BMODE_NONE;
414                 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
415                         broadcast_mode = BMODE_MST;
416                 else if(!strcasecmp(bmode, "direct"))
417                         broadcast_mode = BMODE_DIRECT;
418                 else {
419                         logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
420                         return false;
421                 }
422                 free(bmode);
423         }
424
425 #if !defined(SOL_IP) || !defined(IP_TOS)
426         if(priorityinheritance)
427                 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
428 #endif
429
430         if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
431                 macexpire = 600;
432
433         if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
434                 if(maxtimeout <= 0) {
435                         logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
436                         return false;
437                 }
438         } else
439                 maxtimeout = 900;
440
441         if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
442                 if(!strcasecmp(afname, "IPv4"))
443                         addressfamily = AF_INET;
444                 else if(!strcasecmp(afname, "IPv6"))
445                         addressfamily = AF_INET6;
446                 else if(!strcasecmp(afname, "any"))
447                         addressfamily = AF_UNSPEC;
448                 else {
449                         logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
450                         return false;
451                 }
452                 free(afname);
453         }
454
455         get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
456
457         if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
458                 keylifetime = 3600;
459
460         get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
461         if(autoconnect < 0)
462                 autoconnect = 0;
463
464         get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
465
466         read_invitation_key();
467
468         return true;
469 }
470
471 /*
472   Add listening sockets.
473 */
474 static bool add_listen_address(char *address, bool bindto) {
475         char *port = myport;
476
477         if(address) {
478                 char *space = strchr(address, ' ');
479                 if(space) {
480                         *space++ = 0;
481                         port = space;
482                 }
483
484                 if(!strcmp(address, "*"))
485                         *address = 0;
486         }
487
488         struct addrinfo *ai, hint = {0};
489         hint.ai_family = addressfamily;
490         hint.ai_socktype = SOCK_STREAM;
491         hint.ai_protocol = IPPROTO_TCP;
492         hint.ai_flags = AI_PASSIVE;
493
494         int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
495         free(address);
496
497         if(err || !ai) {
498                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
499                 return false;
500         }
501
502         for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
503                 // Ignore duplicate addresses
504                 bool found = false;
505
506                 for(int i = 0; i < listen_sockets; i++)
507                         if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
508                                 found = true;
509                                 break;
510                         }
511
512                 if(found)
513                         continue;
514
515                 if(listen_sockets >= MAXSOCKETS) {
516                         logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
517                         return false;
518                 }
519
520                 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
521
522                 if(tcp_fd < 0)
523                         continue;
524
525                 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
526
527                 if(tcp_fd < 0) {
528                         close(tcp_fd);
529                         continue;
530                 }
531
532                 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
533                 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
534
535                 if(debug_level >= DEBUG_CONNECTIONS) {
536                         char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
537                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
538                         free(hostname);
539                 }
540
541                 listen_socket[listen_sockets].bindto = bindto;
542                 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
543                 listen_sockets++;
544         }
545
546         freeaddrinfo(ai);
547         return true;
548 }
549
550 /*
551   Configure node_t myself and set up the local sockets (listen only)
552 */
553 bool setup_myself(void) {
554         char *name, *hostname, *cipher, *digest, *type;
555         char *address = NULL;
556         bool port_specified = false;
557
558         if(!(name = get_name())) {
559                 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
560                 return false;
561         }
562
563         myself = new_node();
564         myself->connection = new_connection();
565         myself->name = name;
566         myself->connection->name = xstrdup(name);
567         read_host_config(config_tree, name);
568
569         if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
570                 myport = xstrdup("655");
571         else
572                 port_specified = true;
573
574         myself->connection->options = 0;
575         myself->connection->protocol_major = PROT_MAJOR;
576         myself->connection->protocol_minor = PROT_MINOR;
577
578         myself->options |= PROT_MINOR << 24;
579
580         if(!read_ecdsa_private_key())
581                 return false;
582
583         /* Ensure myport is numeric */
584
585         if(!atoi(myport)) {
586                 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
587                 sockaddr_t sa;
588                 if(!ai || !ai->ai_addr)
589                         return false;
590                 free(myport);
591                 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
592                 sockaddr2str(&sa, NULL, &myport);
593         }
594
595         /* Check some options */
596
597         if(!setup_myself_reloadable())
598                 return false;
599
600         if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
601                 if(max_connection_burst <= 0) {
602                         logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
603                         return false;
604                 }
605         }
606
607         int replaywin_int;
608         if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
609                 if(replaywin_int < 0) {
610                         logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
611                         return false;
612                 }
613                 replaywin = (unsigned)replaywin_int;
614                 sptps_replaywin = replaywin;
615         }
616
617         timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
618
619         /* Compression */
620
621         if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
622                 if(myself->incompression < 0 || myself->incompression > 11) {
623                         logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
624                         return false;
625                 }
626         } else
627                 myself->incompression = 0;
628
629         myself->connection->outcompression = 0;
630
631         /* Done */
632
633         myself->nexthop = myself;
634         myself->via = myself;
635         myself->status.reachable = true;
636         myself->last_state_change = now.tv_sec;
637         node_add(myself);
638
639         graph();
640
641         if(autoconnect)
642                 load_all_nodes();
643
644         /* Open sockets */
645
646         listen_sockets = 0;
647         int cfgs = 0;
648
649         for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
650                 cfgs++;
651                 get_config_string(cfg, &address);
652                 if(!add_listen_address(address, true))
653                         return false;
654         }
655
656         for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
657                 cfgs++;
658                 get_config_string(cfg, &address);
659                 if(!add_listen_address(address, false))
660                         return false;
661         }
662
663         if(!cfgs)
664                 if(!add_listen_address(address, NULL))
665                         return false;
666
667         if(!listen_sockets) {
668                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
669                 return false;
670         }
671
672         /* If no Port option was specified, set myport to the port used by the first listening socket. */
673
674         if(!port_specified) {
675                 sockaddr_t sa;
676                 socklen_t salen = sizeof sa;
677                 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
678                         free(myport);
679                         sockaddr2str(&sa, NULL, &myport);
680                         if(!myport)
681                                 myport = xstrdup("655");
682                 }
683         }
684
685         xasprintf(&myself->hostname, "MYSELF port %s", myport);
686         myself->connection->hostname = xstrdup(myself->hostname);
687
688         /* Done. */
689
690         last_config_check = now.tv_sec;
691
692         return true;
693 }
694
695 /*
696   initialize network
697 */
698 bool setup_network(void) {
699         init_connections();
700         init_nodes();
701         init_edges();
702         init_requests();
703
704         if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
705                 if(pinginterval < 1) {
706                         pinginterval = 86400;
707                 }
708         } else
709                 pinginterval = 60;
710
711         if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
712                 pingtimeout = 5;
713         if(pingtimeout < 1 || pingtimeout > pinginterval)
714                 pingtimeout = pinginterval;
715
716         if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
717                 maxoutbufsize = 10 * MTU;
718
719         if(!setup_myself())
720                 return false;
721
722         return true;
723 }
724
725 /*
726   close all open network connections
727 */
728 void close_network_connections(void) {
729         for(list_node_t *node = connection_list->head, *next; node; node = next) {
730                 next = node->next;
731                 connection_t *c = node->data;
732                 c->outgoing = NULL;
733                 terminate_connection(c, false);
734         }
735
736         if(outgoing_list)
737                 list_delete_list(outgoing_list);
738
739         if(myself && myself->connection) {
740                 terminate_connection(myself->connection, false);
741                 free_connection(myself->connection);
742         }
743
744         for(int i = 0; i < listen_sockets; i++) {
745                 io_del(&listen_socket[i].tcp);
746                 io_del(&listen_socket[i].udp);
747                 close(listen_socket[i].tcp.fd);
748                 close(listen_socket[i].udp.fd);
749         }
750
751         exit_requests();
752         exit_edges();
753         exit_nodes();
754         exit_connections();
755
756         if(myport) free(myport);
757
758         return;
759 }