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