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