]> git.meshlink.io Git - meshlink/blob - src/net_socket.c
Move pinginterval pingtimeout and maxtimeout to mesh
[meshlink] / src / net_socket.c
1 /*
2     net_socket.c -- Handle various kinds of sockets.
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 "conf.h"
23 #include "connection.h"
24 #include "list.h"
25 #include "logger.h"
26 #include "meshlink_internal.h"
27 #include "meta.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "protocol.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 /* Needed on Mac OS/X */
35 #ifndef SOL_TCP
36 #define SOL_TCP IPPROTO_TCP
37 #endif
38
39 int addressfamily = AF_UNSPEC;
40 int seconds_till_retry = 5;
41 int max_connection_burst = 100;
42
43 /* Setup sockets */
44
45 static void configure_tcp(connection_t *c) {
46         int option;
47
48 #ifdef O_NONBLOCK
49         int flags = fcntl(c->socket, F_GETFL);
50
51         if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
52                 logger(DEBUG_ALWAYS, LOG_ERR, "fcntl for %s: %s", c->hostname, strerror(errno));
53         }
54 #elif defined(WIN32)
55         unsigned long arg = 1;
56
57         if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
58                 logger(DEBUG_ALWAYS, LOG_ERR, "ioctlsocket for %s: %s", c->hostname, sockstrerror(sockerrno));
59         }
60 #endif
61
62 #if defined(SOL_TCP) && defined(TCP_NODELAY)
63         option = 1;
64         setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&option, sizeof option);
65 #endif
66
67 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
68         option = IPTOS_LOWDELAY;
69         setsockopt(c->socket, SOL_IP, IP_TOS, (void *)&option, sizeof option);
70 #endif
71 }
72
73 static bool bind_to_address(connection_t *c) {
74         int s = -1;
75
76         for(int i = 0; i < mesh->listen_sockets && mesh->listen_socket[i].bindto; i++) {
77                 if(mesh->listen_socket[i].sa.sa.sa_family != c->address.sa.sa_family)
78                         continue;
79                 if(s >= 0)
80                         return false;
81                 s = i;
82         }
83
84         if(s < 0)
85                 return false;
86
87         sockaddr_t sa = mesh->listen_socket[s].sa;
88         if(sa.sa.sa_family == AF_INET)
89                 sa.in.sin_port = 0;
90         else if(sa.sa.sa_family == AF_INET6)
91                 sa.in6.sin6_port = 0;
92
93         if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
94                 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Can't bind outgoing socket: %s", strerror(errno));
95                 return false;
96         }
97
98         return true;
99 }
100
101 int setup_listen_socket(const sockaddr_t *sa) {
102         int nfd;
103         char *addrstr;
104         int option;
105         char *iface;
106
107         nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
108
109         if(nfd < 0) {
110                 logger(DEBUG_STATUS, LOG_ERR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
111                 return -1;
112         }
113
114 #ifdef FD_CLOEXEC
115         fcntl(nfd, F_SETFD, FD_CLOEXEC);
116 #endif
117
118         /* Optimize TCP settings */
119
120         option = 1;
121         setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof option);
122
123 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
124         if(sa->sa.sa_family == AF_INET6)
125                 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
126 #endif
127
128         if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
129                 closesocket(nfd);
130                 addrstr = sockaddr2hostname(sa);
131                 logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
132                 free(addrstr);
133                 return -1;
134         }
135
136         if(listen(nfd, 3)) {
137                 closesocket(nfd);
138                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
139                 return -1;
140         }
141
142         return nfd;
143 }
144
145 int setup_vpn_in_socket(const sockaddr_t *sa) {
146         int nfd;
147         char *addrstr;
148         int option;
149
150         nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
151
152         if(nfd < 0) {
153                 logger(DEBUG_ALWAYS, LOG_ERR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
154                 return -1;
155         }
156
157 #ifdef FD_CLOEXEC
158         fcntl(nfd, F_SETFD, FD_CLOEXEC);
159 #endif
160
161 #ifdef O_NONBLOCK
162         {
163                 int flags = fcntl(nfd, F_GETFL);
164
165                 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
166                         closesocket(nfd);
167                         logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl",
168                                    strerror(errno));
169                         return -1;
170                 }
171         }
172 #elif defined(WIN32)
173         {
174                 unsigned long arg = 1;
175                 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
176                         closesocket(nfd);
177                         logger(DEBUG_ALWAYS, LOG_ERR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
178                         return -1;
179                 }
180         }
181 #endif
182
183         option = 1;
184         setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof option);
185         setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof option);
186
187 #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
188         if(sa->sa.sa_family == AF_INET6)
189                 setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
190 #endif
191
192 #if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
193 #define IP_DONTFRAGMENT IP_DONTFRAG
194 #endif
195
196 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
197         if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
198                 option = IP_PMTUDISC_DO;
199                 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
200         }
201 #elif defined(IPPROTO_IP) && defined(IP_DONTFRAGMENT)
202         if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
203                 option = 1;
204                 setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
205         }
206 #else
207 #warning No way to disable IPv4 fragmentation
208 #endif
209
210 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
211         if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
212                 option = IPV6_PMTUDISC_DO;
213                 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
214         }
215 #elif defined(IPPROTO_IPV6) && defined(IPV6_DONTFRAG)
216         if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
217                 option = 1;
218                 setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
219         }
220 #else
221 #warning No way to disable IPv6 fragmentation
222 #endif
223
224         if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
225                 closesocket(nfd);
226                 addrstr = sockaddr2hostname(sa);
227                 logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
228                 free(addrstr);
229                 return -1;
230         }
231
232         return nfd;
233 } /* int setup_vpn_in_socket */
234
235 static void retry_outgoing_handler(void *data) {
236         setup_outgoing_connection(data);
237 }
238
239 void retry_outgoing(outgoing_t *outgoing) {
240         outgoing->timeout += 5;
241
242         if(outgoing->timeout > mesh->maxtimeout)
243                 outgoing->timeout = mesh->maxtimeout;
244
245         timeout_add(&outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval){outgoing->timeout, rand() % 100000});
246
247         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
248 }
249
250 void finish_connecting(connection_t *c) {
251         logger(DEBUG_CONNECTIONS, LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);
252
253         c->last_ping_time = now.tv_sec;
254         c->status.connecting = false;
255
256         send_id(c);
257 }
258
259 static void do_outgoing_pipe(connection_t *c, char *command) {
260 #ifndef HAVE_MINGW
261         int fd[2];
262
263         if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
264                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create socketpair: %s", strerror(errno));
265                 return;
266         }
267
268         if(fork()) {
269                 c->socket = fd[0];
270                 close(fd[1]);
271                 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Using proxy %s", command);
272                 return;
273         }
274
275         close(0);
276         close(1);
277         close(fd[0]);
278         dup2(fd[1], 0);
279         dup2(fd[1], 1);
280         close(fd[1]);
281
282         // Other filedescriptors should be closed automatically by CLOEXEC
283
284         char *host = NULL;
285         char *port = NULL;
286
287         sockaddr2str(&c->address, &host, &port);
288         setenv("REMOTEADDRESS", host, true);
289         setenv("REMOTEPORT", port, true);
290         setenv("NODE", c->name, true);
291         setenv("NAME", mesh->self->name, true);
292
293         int result = system(command);
294         if(result < 0)
295                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not execute %s: %s", command, strerror(errno));
296         else if(result)
297                 logger(DEBUG_ALWAYS, LOG_ERR, "%s exited with non-zero status %d", command, result);
298         exit(result);
299 #else
300         logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type exec not supported on this platform!");
301         return;
302 #endif
303 }
304
305 static void handle_meta_write(connection_t *c) {
306         if(c->outbuf.len <= c->outbuf.offset)
307                 return;
308
309         ssize_t outlen = send(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset, 0);
310         if(outlen <= 0) {
311                 if(!errno || errno == EPIPE) {
312                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)", c->name, c->hostname);
313                 } else if(sockwouldblock(sockerrno)) {
314                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Sending %d bytes to %s (%s) would block", c->outbuf.len - c->outbuf.offset, c->name, c->hostname);
315                         return;
316                 } else {
317                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not send %d bytes of data to %s (%s): %s", c->outbuf.len - c->outbuf.offset, c->name, c->hostname, strerror(errno));
318                 }
319
320                 terminate_connection(c, c->status.active);
321                 return;
322         }
323
324         buffer_read(&c->outbuf, outlen);
325         if(!c->outbuf.len)
326                 io_set(&c->io, IO_READ);
327 }
328
329 static void handle_meta_io(void *data, int flags) {
330         connection_t *c = data;
331
332         if(c->status.connecting) {
333                 c->status.connecting = false;
334
335                 int result;
336                 socklen_t len = sizeof result;
337                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
338
339                 if(!result)
340                         finish_connecting(c);
341                 else {
342                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): %s", c->name, c->hostname, sockstrerror(result));
343                         terminate_connection(c, false);
344                         return;
345                 }
346         }
347
348         if(flags & IO_WRITE)
349                 handle_meta_write(c);
350         else
351                 handle_meta_connection_data(c);
352 }
353
354 bool do_outgoing_connection(outgoing_t *outgoing) {
355         char *address, *port, *space;
356         struct addrinfo *proxyai = NULL;
357         int result;
358
359 begin:
360         if(!outgoing->ai) {
361                 if(!outgoing->cfg) {
362                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not set up a meta connection to %s", outgoing->name);
363                         retry_outgoing(outgoing);
364                         return false;
365                 }
366
367                 get_config_string(outgoing->cfg, &address);
368
369                 space = strchr(address, ' ');
370                 if(space) {
371                         port = xstrdup(space + 1);
372                         *space = 0;
373                 } else {
374                         // TODO: Only allow Address statements?
375                         if(!get_config_string(lookup_config(outgoing->config_tree, "Port"), &port))
376                                 port = xstrdup("655");
377                 }
378
379                 outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
380                 free(address);
381                 free(port);
382
383                 outgoing->aip = outgoing->ai;
384                 outgoing->cfg = lookup_config_next(outgoing->config_tree, outgoing->cfg);
385         }
386
387         if(!outgoing->aip) {
388                 if(outgoing->ai)
389                         freeaddrinfo(outgoing->ai);
390                 outgoing->ai = NULL;
391                 goto begin;
392         }
393
394         connection_t *c = new_connection();
395         c->outgoing = outgoing;
396
397         memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
398         outgoing->aip = outgoing->aip->ai_next;
399
400         c->hostname = sockaddr2hostname(&c->address);
401
402         logger(DEBUG_CONNECTIONS, LOG_INFO, "Trying to connect to %s (%s)", outgoing->name, c->hostname);
403
404         if(!mesh->proxytype) {
405                 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
406                 configure_tcp(c);
407         } else if(mesh->proxytype == PROXY_EXEC) {
408                 do_outgoing_pipe(c, mesh->proxyhost);
409         } else {
410                 proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
411                 if(!proxyai) {
412                         free_connection(c);
413                         goto begin;
414                 }
415                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport);
416                 c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
417                 configure_tcp(c);
418         }
419
420         if(c->socket == -1) {
421                 logger(DEBUG_CONNECTIONS, LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
422                 free_connection(c);
423                 goto begin;
424         }
425
426 #ifdef FD_CLOEXEC
427         fcntl(c->socket, F_SETFD, FD_CLOEXEC);
428 #endif
429
430         if(mesh->proxytype != PROXY_EXEC) {
431 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
432                 int option = 1;
433                 if(c->address.sa.sa_family == AF_INET6)
434                         setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
435 #endif
436
437                 bind_to_address(c);
438         }
439
440         /* Connect */
441
442         if(!mesh->proxytype) {
443                 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
444         } else if(mesh->proxytype == PROXY_EXEC) {
445                 result = 0;
446         } else {
447                 result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
448                 freeaddrinfo(proxyai);
449         }
450
451         if(result == -1 && !sockinprogress(sockerrno)) {
452                 logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not connect to %s (%s): %s", outgoing->name, c->hostname, sockstrerror(sockerrno));
453                 free_connection(c);
454
455                 goto begin;
456         }
457
458         /* Now that there is a working socket, fill in the rest and register this connection. */
459
460         c->status.connecting = true;
461         c->name = xstrdup(outgoing->name);
462         c->outcompression = mesh->self->connection->outcompression;
463         c->last_ping_time = now.tv_sec;
464
465         connection_add(c);
466
467         io_add(&c->io, handle_meta_io, c, c->socket, IO_READ|IO_WRITE);
468
469         return true;
470 }
471
472 // Find edges pointing to this node, and use them to build a list of unique, known addresses.
473 static struct addrinfo *get_known_addresses(node_t *n) {
474         struct addrinfo *ai = NULL;
475
476         for splay_each(edge_t, e, n->edge_tree) {
477                 if(!e->reverse)
478                         continue;
479
480                 bool found = false;
481                 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
482                         if(!sockaddrcmp(&e->reverse->address, (sockaddr_t *)aip->ai_addr)) {
483                                 found = true;
484                                 break;
485                         }
486                 }
487                 if(found)
488                         continue;
489
490                 struct addrinfo *nai = xzalloc(sizeof *nai);
491                 if(ai)
492                         ai->ai_next = nai;
493                 ai = nai;
494                 ai->ai_family = e->reverse->address.sa.sa_family;
495                 ai->ai_socktype = SOCK_STREAM;
496                 ai->ai_protocol = IPPROTO_TCP;
497                 ai->ai_addrlen = SALEN(e->reverse->address.sa);
498                 ai->ai_addr = xmalloc(ai->ai_addrlen);
499                 memcpy(ai->ai_addr, &e->reverse->address, ai->ai_addrlen);
500         }
501
502         return ai;
503 }
504
505 void setup_outgoing_connection(outgoing_t *outgoing) {
506         timeout_del(&outgoing->ev);
507
508         node_t *n = lookup_node(outgoing->name);
509
510         if(n && n->connection) {
511                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Already connected to %s", outgoing->name);
512
513                 n->connection->outgoing = outgoing;
514                 return;
515         }
516
517         init_configuration(&outgoing->config_tree);
518         read_host_config(outgoing->config_tree, outgoing->name);
519         outgoing->cfg = lookup_config(outgoing->config_tree, "Address");
520
521         if(!outgoing->cfg) {
522                 if(n)
523                         outgoing->aip = outgoing->ai = get_known_addresses(n);
524                 if(!outgoing->ai) {
525                         logger(DEBUG_ALWAYS, LOG_ERR, "No address known for %s", outgoing->name);
526                         return;
527                 }
528         }
529
530         do_outgoing_connection(outgoing);
531 }
532
533 /*
534   accept a new tcp connect and create a
535   new connection
536 */
537 void handle_new_meta_connection(void *data, int flags) {
538         listen_socket_t *l = data;
539         connection_t *c;
540         sockaddr_t sa;
541         int fd;
542         socklen_t len = sizeof sa;
543
544         fd = accept(l->tcp.fd, &sa.sa, &len);
545
546         if(fd < 0) {
547                 logger(DEBUG_ALWAYS, LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
548                 return;
549         }
550
551         sockaddrunmap(&sa);
552
553         // Check if we get many connections from the same host
554
555         static sockaddr_t prev_sa;
556         static int tarpit = -1;
557
558         if(tarpit >= 0) {
559                 closesocket(tarpit);
560                 tarpit = -1;
561         }
562
563         if(!sockaddrcmp_noport(&sa, &prev_sa)) {
564                 static int samehost_burst;
565                 static int samehost_burst_time;
566
567                 if(now.tv_sec - samehost_burst_time > samehost_burst)
568                         samehost_burst = 0;
569                 else
570                         samehost_burst -= now.tv_sec - samehost_burst_time;
571
572                 samehost_burst_time = now.tv_sec;
573                 samehost_burst++;
574
575                 if(samehost_burst > max_connection_burst) {
576                         tarpit = fd;
577                         return;
578                 }
579         }
580
581         memcpy(&prev_sa, &sa, sizeof sa);
582
583         // Check if we get many connections from different hosts
584
585         static int connection_burst;
586         static int connection_burst_time;
587
588         if(now.tv_sec - connection_burst_time > connection_burst)
589                 connection_burst = 0;
590         else
591                 connection_burst -= now.tv_sec - connection_burst_time;
592
593         connection_burst_time = now.tv_sec;
594         connection_burst++;
595
596         if(connection_burst >= max_connection_burst) {
597                 connection_burst = max_connection_burst;
598                 tarpit = fd;
599                 return;
600         }
601
602         // Accept the new connection
603
604         c = new_connection();
605         c->name = xstrdup("<unknown>");
606         c->outcompression = mesh->self->connection->outcompression;
607
608         c->address = sa;
609         c->hostname = sockaddr2hostname(&sa);
610         c->socket = fd;
611         c->last_ping_time = now.tv_sec;
612
613         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection from %s", c->hostname);
614
615         io_add(&c->io, handle_meta_io, c, c->socket, IO_READ);
616
617         configure_tcp(c);
618
619         connection_add(c);
620
621         c->allow_request = ID;
622         send_id(c);
623 }
624
625 static void free_outgoing(outgoing_t *outgoing) {
626         timeout_del(&outgoing->ev);
627
628         if(outgoing->ai)
629                 freeaddrinfo(outgoing->ai);
630
631         if(outgoing->config_tree)
632                 exit_configuration(&outgoing->config_tree);
633
634         if(outgoing->name)
635                 free(outgoing->name);
636
637         free(outgoing);
638 }
639
640 void try_outgoing_connections(void) {
641         /* If there is no outgoing list yet, create one. Otherwise, mark all outgoings as deleted. */
642
643         if(!mesh->outgoings) {
644                 mesh->outgoings = list_alloc((list_action_t)free_outgoing);
645         } else {
646                 for list_each(outgoing_t, outgoing, mesh->outgoings)
647                         outgoing->timeout = -1;
648         }
649
650         /* Make sure there is one outgoing_t in the list for each ConnectTo. */
651
652         // TODO: Drop support for ConnectTo since AutoConnect is now always on?
653         for(config_t *cfg = lookup_config(mesh->config, "ConnectTo"); cfg; cfg = lookup_config_next(mesh->config, cfg)) {
654                 char *name;
655                 get_config_string(cfg, &name);
656
657                 if(!check_id(name)) {
658                         logger(DEBUG_ALWAYS, LOG_ERR,
659                                    "Invalid name for outgoing connection in %s line %d",
660                                    cfg->file, cfg->line);
661                         free(name);
662                         continue;
663                 }
664
665                 bool found = false;
666
667                 for list_each(outgoing_t, outgoing, mesh->outgoings) {
668                         if(!strcmp(outgoing->name, name)) {
669                                 found = true;
670                                 outgoing->timeout = 0;
671                                 break;
672                         }
673                 }
674
675                 if(!found) {
676                         outgoing_t *outgoing = xzalloc(sizeof *outgoing);
677                         outgoing->name = name;
678                         list_insert_tail(mesh->outgoings, outgoing);
679                         setup_outgoing_connection(outgoing);
680                 }
681         }
682
683         /* Terminate any connections whose outgoing_t is to be deleted. */
684
685         for list_each(connection_t, c, mesh->connections) {
686                 if(c->outgoing && c->outgoing->timeout == -1) {
687                         c->outgoing = NULL;
688                         logger(DEBUG_CONNECTIONS, LOG_INFO, "No more outgoing connection to %s", c->name);
689                         terminate_connection(c, c->status.active);
690                 }
691         }
692
693         /* Delete outgoing_ts for which there is no ConnectTo. */
694
695         for list_each(outgoing_t, outgoing, mesh->outgoings)
696                 if(outgoing->timeout == -1)
697                         list_delete_node(mesh->outgoings, node);
698 }