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