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