]> git.meshlink.io Git - meshlink/blob - src/net_socket.c
Stop using the global variable mesh in most of the rest of the code.
[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(meshlink_handle_t *mesh, 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(meshlink_handle_t *mesh, 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(event_loop_t *loop, void *data) {
236         meshlink_handle_t *mesh = loop->data;
237         outgoing_t *outgoing = data;
238         setup_outgoing_connection(mesh, outgoing);
239 }
240
241 void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
242         outgoing->timeout += 5;
243
244         if(outgoing->timeout > mesh->maxtimeout)
245                 outgoing->timeout = mesh->maxtimeout;
246
247         timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval){outgoing->timeout, rand() % 100000});
248
249         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
250 }
251
252 void finish_connecting(meshlink_handle_t *mesh, connection_t *c) {
253         logger(DEBUG_CONNECTIONS, LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);
254
255         c->last_ping_time = mesh->loop.now.tv_sec;
256         c->status.connecting = false;
257
258         send_id(mesh, c);
259 }
260
261 static void do_outgoing_pipe(meshlink_handle_t *mesh, connection_t *c, char *command) {
262 #ifndef HAVE_MINGW
263         int fd[2];
264
265         if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
266                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create socketpair: %s", strerror(errno));
267                 return;
268         }
269
270         if(fork()) {
271                 c->socket = fd[0];
272                 close(fd[1]);
273                 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Using proxy %s", command);
274                 return;
275         }
276
277         close(0);
278         close(1);
279         close(fd[0]);
280         dup2(fd[1], 0);
281         dup2(fd[1], 1);
282         close(fd[1]);
283
284         // Other filedescriptors should be closed automatically by CLOEXEC
285
286         char *host = NULL;
287         char *port = NULL;
288
289         sockaddr2str(&c->address, &host, &port);
290         setenv("REMOTEADDRESS", host, true);
291         setenv("REMOTEPORT", port, true);
292         setenv("NODE", c->name, true);
293         setenv("NAME", mesh->self->name, true);
294
295         int result = system(command);
296         if(result < 0)
297                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not execute %s: %s", command, strerror(errno));
298         else if(result)
299                 logger(DEBUG_ALWAYS, LOG_ERR, "%s exited with non-zero status %d", command, result);
300         exit(result);
301 #else
302         logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type exec not supported on this platform!");
303         return;
304 #endif
305 }
306
307 static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) {
308         if(c->outbuf.len <= c->outbuf.offset)
309                 return;
310
311         ssize_t outlen = send(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset, 0);
312         if(outlen <= 0) {
313                 if(!errno || errno == EPIPE) {
314                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)", c->name, c->hostname);
315                 } else if(sockwouldblock(sockerrno)) {
316                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Sending %d bytes to %s (%s) would block", c->outbuf.len - c->outbuf.offset, c->name, c->hostname);
317                         return;
318                 } else {
319                         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));
320                 }
321
322                 terminate_connection(mesh, c, c->status.active);
323                 return;
324         }
325
326         buffer_read(&c->outbuf, outlen);
327         if(!c->outbuf.len)
328                 io_set(&mesh->loop, &c->io, IO_READ);
329 }
330
331 static void handle_meta_io(event_loop_t *loop, void *data, int flags) {
332         meshlink_handle_t *mesh = loop->data;
333         connection_t *c = data;
334
335         if(c->status.connecting) {
336                 c->status.connecting = false;
337
338                 int result;
339                 socklen_t len = sizeof result;
340                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
341
342                 if(!result)
343                         finish_connecting(mesh, c);
344                 else {
345                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): %s", c->name, c->hostname, sockstrerror(result));
346                         terminate_connection(mesh, c, false);
347                         return;
348                 }
349         }
350
351         if(flags & IO_WRITE)
352                 handle_meta_write(mesh, c);
353         else
354                 handle_meta_connection_data(mesh, c);
355 }
356
357 bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
358         char *address, *port, *space;
359         struct addrinfo *proxyai = NULL;
360         int result;
361
362 begin:
363         if(!outgoing->ai) {
364                 if(!outgoing->cfg) {
365                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not set up a meta connection to %s", outgoing->name);
366                         retry_outgoing(mesh, outgoing);
367                         return false;
368                 }
369
370                 get_config_string(outgoing->cfg, &address);
371
372                 space = strchr(address, ' ');
373                 if(space) {
374                         port = xstrdup(space + 1);
375                         *space = 0;
376                 } else {
377                         // TODO: Only allow Address statements?
378                         if(!get_config_string(lookup_config(outgoing->config_tree, "Port"), &port))
379                                 port = xstrdup("655");
380                 }
381
382                 outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
383                 free(address);
384                 free(port);
385
386                 outgoing->aip = outgoing->ai;
387                 outgoing->cfg = lookup_config_next(outgoing->config_tree, outgoing->cfg);
388         }
389
390         if(!outgoing->aip) {
391                 if(outgoing->ai)
392                         freeaddrinfo(outgoing->ai);
393                 outgoing->ai = NULL;
394                 goto begin;
395         }
396
397         connection_t *c = new_connection();
398         c->outgoing = outgoing;
399
400         memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
401         outgoing->aip = outgoing->aip->ai_next;
402
403         c->hostname = sockaddr2hostname(&c->address);
404
405         logger(DEBUG_CONNECTIONS, LOG_INFO, "Trying to connect to %s (%s)", outgoing->name, c->hostname);
406
407         if(!mesh->proxytype) {
408                 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
409                 configure_tcp(c);
410         } else if(mesh->proxytype == PROXY_EXEC) {
411                 do_outgoing_pipe(mesh, c, mesh->proxyhost);
412         } else {
413                 proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
414                 if(!proxyai) {
415                         free_connection(c);
416                         goto begin;
417                 }
418                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport);
419                 c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
420                 configure_tcp(c);
421         }
422
423         if(c->socket == -1) {
424                 logger(DEBUG_CONNECTIONS, LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
425                 free_connection(c);
426                 goto begin;
427         }
428
429 #ifdef FD_CLOEXEC
430         fcntl(c->socket, F_SETFD, FD_CLOEXEC);
431 #endif
432
433         if(mesh->proxytype != PROXY_EXEC) {
434 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
435                 int option = 1;
436                 if(c->address.sa.sa_family == AF_INET6)
437                         setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
438 #endif
439
440                 bind_to_address(mesh, c);
441         }
442
443         /* Connect */
444
445         if(!mesh->proxytype) {
446                 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
447         } else if(mesh->proxytype == PROXY_EXEC) {
448                 result = 0;
449         } else {
450                 result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
451                 freeaddrinfo(proxyai);
452         }
453
454         if(result == -1 && !sockinprogress(sockerrno)) {
455                 logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not connect to %s (%s): %s", outgoing->name, c->hostname, sockstrerror(sockerrno));
456                 free_connection(c);
457
458                 goto begin;
459         }
460
461         /* Now that there is a working socket, fill in the rest and register this connection. */
462
463         c->status.connecting = true;
464         c->name = xstrdup(outgoing->name);
465         c->outcompression = mesh->self->connection->outcompression;
466         c->last_ping_time = mesh->loop.now.tv_sec;
467
468         connection_add(mesh, c);
469
470         io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ|IO_WRITE);
471
472         return true;
473 }
474
475 // Find edges pointing to this node, and use them to build a list of unique, known addresses.
476 static struct addrinfo *get_known_addresses(node_t *n) {
477         struct addrinfo *ai = NULL;
478
479         for splay_each(edge_t, e, n->edge_tree) {
480                 if(!e->reverse)
481                         continue;
482
483                 bool found = false;
484                 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
485                         if(!sockaddrcmp(&e->reverse->address, (sockaddr_t *)aip->ai_addr)) {
486                                 found = true;
487                                 break;
488                         }
489                 }
490                 if(found)
491                         continue;
492
493                 struct addrinfo *nai = xzalloc(sizeof *nai);
494                 if(ai)
495                         ai->ai_next = nai;
496                 ai = nai;
497                 ai->ai_family = e->reverse->address.sa.sa_family;
498                 ai->ai_socktype = SOCK_STREAM;
499                 ai->ai_protocol = IPPROTO_TCP;
500                 ai->ai_addrlen = SALEN(e->reverse->address.sa);
501                 ai->ai_addr = xmalloc(ai->ai_addrlen);
502                 memcpy(ai->ai_addr, &e->reverse->address, ai->ai_addrlen);
503         }
504
505         return ai;
506 }
507
508 void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
509         timeout_del(&mesh->loop, &outgoing->ev);
510
511         node_t *n = lookup_node(mesh, outgoing->name);
512
513         if(n && n->connection) {
514                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Already connected to %s", outgoing->name);
515
516                 n->connection->outgoing = outgoing;
517                 return;
518         }
519
520         init_configuration(&outgoing->config_tree);
521         read_host_config(mesh, outgoing->config_tree, outgoing->name);
522         outgoing->cfg = lookup_config(outgoing->config_tree, "Address");
523
524         if(!outgoing->cfg) {
525                 if(n)
526                         outgoing->aip = outgoing->ai = get_known_addresses(n);
527                 if(!outgoing->ai) {
528                         logger(DEBUG_ALWAYS, LOG_ERR, "No address known for %s", outgoing->name);
529                         return;
530                 }
531         }
532
533         do_outgoing_connection(mesh, outgoing);
534 }
535
536 /*
537   accept a new tcp connect and create a
538   new connection
539 */
540 void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) {
541         meshlink_handle_t *mesh = loop->data;
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(mesh->loop.now.tv_sec - samehost_burst_time > samehost_burst)
572                         samehost_burst = 0;
573                 else
574                         samehost_burst -= mesh->loop.now.tv_sec - samehost_burst_time;
575
576                 samehost_burst_time = mesh->loop.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(mesh->loop.now.tv_sec - connection_burst_time > connection_burst)
593                 connection_burst = 0;
594         else
595                 connection_burst -= mesh->loop.now.tv_sec - connection_burst_time;
596
597         connection_burst_time = mesh->loop.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 = mesh->loop.now.tv_sec;
616
617         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection from %s", c->hostname);
618
619         io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ);
620
621         configure_tcp(c);
622
623         connection_add(mesh, c);
624
625         c->allow_request = ID;
626         send_id(mesh, c);
627 }
628
629 static void free_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
630         timeout_del(&mesh->loop, &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(meshlink_handle_t *mesh) {
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(mesh, 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(mesh, 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 }