]> git.meshlink.io Git - meshlink/blob - src/net_socket.c
Fix memory leaks from timers.
[meshlink] / src / net_socket.c
1 /*
2     net_socket.c -- Handle various kinds of sockets.
3     Copyright (C) 2014-2017 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 static const int max_connection_burst = 100;
44
45 /* Setup sockets */
46
47 static void configure_tcp(connection_t *c) {
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(c->mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "fcntl", strerror(errno));
53         }
54
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         }
61
62 #endif
63
64 #if defined(SOL_TCP) && defined(TCP_NODELAY)
65         int nodelay = 1;
66         setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay));
67 #endif
68
69 #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
70         int lowdelay = IPTOS_LOWDELAY;
71         setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay));
72 #endif
73 }
74
75 static bool bind_to_address(meshlink_handle_t *mesh, connection_t *c) {
76         int s = -1;
77
78         for(int i = 0; i < mesh->listen_sockets && mesh->listen_socket[i].bindto; i++) {
79                 if(mesh->listen_socket[i].sa.sa.sa_family != c->address.sa.sa_family) {
80                         continue;
81                 }
82
83                 if(s >= 0) {
84                         return false;
85                 }
86
87                 s = i;
88         }
89
90         if(s < 0) {
91                 return false;
92         }
93
94         sockaddr_t sa = mesh->listen_socket[s].sa;
95
96         if(sa.sa.sa_family == AF_INET) {
97                 sa.in.sin_port = 0;
98         } else if(sa.sa.sa_family == AF_INET6) {
99                 sa.in6.sin6_port = 0;
100         }
101
102         if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
103                 logger(mesh, MESHLINK_WARNING, "Can't bind outgoing socket: %s", strerror(errno));
104                 return false;
105         }
106
107         return true;
108 }
109
110 int setup_listen_socket(const sockaddr_t *sa) {
111         int nfd;
112         char *addrstr;
113         int option;
114
115         nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
116
117         if(nfd < 0) {
118                 logger(NULL, MESHLINK_ERROR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
119                 return -1;
120         }
121
122 #ifdef FD_CLOEXEC
123         fcntl(nfd, F_SETFD, FD_CLOEXEC);
124 #endif
125
126         /* Optimize TCP settings */
127
128         option = 1;
129         setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
130
131 #if defined(IPV6_V6ONLY)
132
133         if(sa->sa.sa_family == AF_INET6) {
134                 setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
135         }
136
137 #else
138 #warning IPV6_V6ONLY not defined
139 #endif
140
141         if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
142                 closesocket(nfd);
143                 addrstr = sockaddr2hostname(sa);
144                 logger(NULL, MESHLINK_ERROR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
145                 free(addrstr);
146                 return -1;
147         }
148
149         if(listen(nfd, 3)) {
150                 closesocket(nfd);
151                 logger(NULL, MESHLINK_ERROR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
152                 return -1;
153         }
154
155         return nfd;
156 }
157
158 int setup_vpn_in_socket(meshlink_handle_t *mesh, const sockaddr_t *sa) {
159         int nfd;
160         char *addrstr;
161         int option;
162
163         nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
164
165         if(nfd < 0) {
166                 logger(mesh, MESHLINK_ERROR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
167                 return -1;
168         }
169
170 #ifdef FD_CLOEXEC
171         fcntl(nfd, F_SETFD, FD_CLOEXEC);
172 #endif
173
174 #ifdef O_NONBLOCK
175         {
176                 int flags = fcntl(nfd, F_GETFL);
177
178                 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
179                         closesocket(nfd);
180                         logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "fcntl",
181                                strerror(errno));
182                         return -1;
183                 }
184         }
185 #elif defined(WIN32)
186         {
187                 unsigned long arg = 1;
188
189                 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
190                         closesocket(nfd);
191                         logger(mesh, MESHLINK_ERROR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
192                         return -1;
193                 }
194         }
195 #endif
196
197         option = 1;
198         setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
199         setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
200
201 #if defined(IPV6_V6ONLY)
202
203         if(sa->sa.sa_family == AF_INET6) {
204                 setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
205         }
206
207 #endif
208
209 #if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
210 #define IP_DONTFRAGMENT IP_DONTFRAG
211 #endif
212
213 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
214         option = IP_PMTUDISC_DO;
215         setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
216 #elif defined(IP_DONTFRAGMENT)
217         option = 1;
218         setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
219 #endif
220
221 #if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
222         option = IPV6_PMTUDISC_DO;
223         setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
224 #elif defined(IPV6_DONTFRAG)
225         option = 1;
226         setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
227 #endif
228
229         if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
230                 closesocket(nfd);
231                 addrstr = sockaddr2hostname(sa);
232                 logger(mesh, MESHLINK_ERROR, "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(event_loop_t *loop, void *data) {
241         meshlink_handle_t *mesh = loop->data;
242         outgoing_t *outgoing = data;
243         setup_outgoing_connection(mesh, outgoing);
244 }
245
246 void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
247         outgoing->timeout += 5;
248
249         if(outgoing->timeout > mesh->maxtimeout) {
250                 outgoing->timeout = mesh->maxtimeout;
251         }
252
253         timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval) {
254                 outgoing->timeout, rand() % 100000
255         });
256
257         logger(mesh, MESHLINK_INFO, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
258 }
259
260 void finish_connecting(meshlink_handle_t *mesh, connection_t *c) {
261         logger(mesh, MESHLINK_INFO, "Connected to %s", c->name);
262
263         c->last_ping_time = mesh->loop.now.tv_sec;
264         c->status.connecting = false;
265
266         send_id(mesh, c);
267 }
268
269 static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) {
270         if(c->outbuf.len <= c->outbuf.offset) {
271                 return;
272         }
273
274         ssize_t outlen = send(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset, MSG_NOSIGNAL);
275
276         if(outlen <= 0) {
277                 if(!errno || errno == EPIPE) {
278                         logger(mesh, MESHLINK_INFO, "Connection closed by %s", c->name);
279                 } else if(sockwouldblock(sockerrno)) {
280                         logger(mesh, MESHLINK_DEBUG, "Sending %lu bytes to %s would block", (unsigned long)(c->outbuf.len - c->outbuf.offset), c->name);
281                         return;
282                 } else {
283                         logger(mesh, MESHLINK_ERROR, "Could not send %lu bytes of data to %s: %s", (unsigned long)(c->outbuf.len - c->outbuf.offset), c->name, strerror(errno));
284                 }
285
286                 terminate_connection(mesh, c, c->status.active);
287                 return;
288         }
289
290         buffer_read(&c->outbuf, outlen);
291
292         if(!c->outbuf.len) {
293                 io_set(&mesh->loop, &c->io, IO_READ);
294         }
295 }
296
297 static void handle_meta_io(event_loop_t *loop, void *data, int flags) {
298         meshlink_handle_t *mesh = loop->data;
299         connection_t *c = data;
300
301         if(c->status.connecting) {
302                 c->status.connecting = false;
303
304                 int result;
305                 socklen_t len = sizeof(result);
306                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
307
308                 if(!result) {
309                         finish_connecting(mesh, c);
310                 } else {
311                         logger(mesh, MESHLINK_DEBUG, "Error while connecting to %s: %s", c->name, sockstrerror(result));
312                         terminate_connection(mesh, c, false);
313                         return;
314                 }
315         }
316
317         if(flags & IO_WRITE) {
318                 handle_meta_write(mesh, c);
319         } else {
320                 handle_meta_connection_data(mesh, c);
321         }
322 }
323
324 // Find edges pointing to this node, and use them to build a list of unique, known addresses.
325 static struct addrinfo *get_known_addresses(node_t *n) {
326         struct addrinfo *ai = NULL;
327
328         for splay_each(edge_t, e, n->edge_tree) {
329                 if(!e->reverse) {
330                         continue;
331                 }
332
333                 bool found = false;
334
335                 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
336                         if(!sockaddrcmp(&e->reverse->address, (sockaddr_t *)aip->ai_addr)) {
337                                 found = true;
338                                 break;
339                         }
340                 }
341
342                 if(found) {
343                         continue;
344                 }
345
346                 // Create a new struct addrinfo, and put it at the head of the list.
347                 struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(e->reverse->address.sa));
348                 nai->ai_next = ai;
349                 ai = nai;
350
351                 ai->ai_family = e->reverse->address.sa.sa_family;
352                 ai->ai_socktype = SOCK_STREAM;
353                 ai->ai_protocol = IPPROTO_TCP;
354                 ai->ai_addrlen = SALEN(e->reverse->address.sa);
355                 ai->ai_addr = (struct sockaddr *)(nai + 1);
356                 memcpy(ai->ai_addr, &e->reverse->address, ai->ai_addrlen);
357         }
358
359         return ai;
360 }
361
362 // Build a list of recently seen addresses.
363 static struct addrinfo *get_recent_addresses(node_t *n) {
364         struct addrinfo *ai = NULL;
365         struct addrinfo *aip;
366
367         for(int i = 0; i < 5; i++) {
368                 if(!n->recent[i].sa.sa_family) {
369                         break;
370                 }
371
372                 // Create a new struct addrinfo, and put it at the end of the list.
373                 struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(n->recent[i].sa));
374
375                 if(!ai) {
376                         ai = nai;
377                 } else {
378                         aip->ai_next = nai;
379                 }
380
381                 aip = nai;
382
383                 nai->ai_family = n->recent[i].sa.sa_family;
384                 nai->ai_socktype = SOCK_STREAM;
385                 nai->ai_protocol = IPPROTO_TCP;
386                 nai->ai_addrlen = SALEN(n->recent[i].sa);
387                 nai->ai_addr = (struct sockaddr *)(nai + 1);
388                 memcpy(nai->ai_addr, &n->recent[i], nai->ai_addrlen);
389         }
390
391         return ai;
392 }
393
394 // Free struct addrinfo list from get_known_addresses().
395 static void free_known_addresses(struct addrinfo *ai) {
396         for(struct addrinfo *aip = ai, *next; aip; aip = next) {
397                 next = aip->ai_next;
398                 free(aip);
399         }
400 }
401
402 static struct addrinfo *get_canonical_address(node_t *n) {
403         if(!n->canonical_address) {
404                 return false;
405         }
406
407         char *address = xstrdup(n->canonical_address);
408         char *port = strchr(address, ' ');
409
410         if(!port) {
411                 free(address);
412                 return false;
413         }
414
415         *port++ = 0;
416
417         struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
418         free(address);
419
420         return ai;
421 }
422
423 static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
424         (void)mesh;
425
426         bool start = false;
427
428         if(outgoing->state == OUTGOING_START) {
429                 start = true;
430                 outgoing->state = OUTGOING_CANONICAL;
431         }
432
433         if(outgoing->state == OUTGOING_CANONICAL) {
434                 if(!outgoing->aip) {
435                         outgoing->ai = get_canonical_address(outgoing->node);
436                         outgoing->aip = outgoing->ai;
437                 } else {
438                         outgoing->aip = outgoing->aip->ai_next;
439                 }
440
441                 if(outgoing->aip) {
442                         return true;
443                 }
444
445                 freeaddrinfo(outgoing->ai);
446                 outgoing->ai = NULL;
447                 outgoing->aip = NULL;
448                 outgoing->state = OUTGOING_RECENT;
449         }
450
451         if(outgoing->state == OUTGOING_RECENT) {
452                 if(!outgoing->aip) {
453                         outgoing->ai = get_recent_addresses(outgoing->node);
454                         outgoing->aip = outgoing->ai;
455                 } else {
456                         outgoing->aip = outgoing->aip->ai_next;
457                 }
458
459                 if(outgoing->aip) {
460                         return true;
461                 }
462
463                 free_known_addresses(outgoing->ai);
464                 outgoing->ai = NULL;
465                 outgoing->aip = NULL;
466                 outgoing->state = OUTGOING_KNOWN;
467         }
468
469         if(outgoing->state == OUTGOING_KNOWN) {
470                 if(!outgoing->aip) {
471                         outgoing->ai = get_known_addresses(outgoing->node);
472                         outgoing->aip = outgoing->ai;
473                 } else {
474                         outgoing->aip = outgoing->aip->ai_next;
475                 }
476
477                 if(outgoing->aip) {
478                         return true;
479                 }
480
481                 free_known_addresses(outgoing->ai);
482                 outgoing->ai = NULL;
483                 outgoing->aip = NULL;
484                 outgoing->state = OUTGOING_END;
485         }
486
487         if(start) {
488                 outgoing->state = OUTGOING_NO_KNOWN_ADDRESSES;
489         }
490
491         return false;
492 }
493
494 bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
495         struct addrinfo *proxyai = NULL;
496         int result;
497
498 begin:
499
500         if(!get_next_outgoing_address(mesh, outgoing)) {
501                 if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) {
502                         logger(mesh, MESHLINK_ERROR, "No known addresses for %s", outgoing->node->name);
503                 } else {
504                         logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->node->name);
505                         retry_outgoing(mesh, outgoing);
506                 }
507
508                 return false;
509         }
510
511         connection_t *c = new_connection();
512         c->outgoing = outgoing;
513
514         memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
515
516         char *hostname = sockaddr2hostname(&c->address);
517
518         logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname);
519
520         if(!mesh->proxytype) {
521                 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
522                 configure_tcp(c);
523         } else {
524                 proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
525
526                 if(!proxyai) {
527                         free_connection(c);
528                         free(hostname);
529                         goto begin;
530                 }
531
532                 logger(mesh, MESHLINK_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport);
533                 c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
534                 configure_tcp(c);
535         }
536
537         if(c->socket == -1) {
538                 logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
539                 free_connection(c);
540                 free(hostname);
541                 goto begin;
542         }
543
544         free(hostname);
545
546 #ifdef FD_CLOEXEC
547         fcntl(c->socket, F_SETFD, FD_CLOEXEC);
548 #endif
549
550 #if defined(IPV6_V6ONLY)
551
552         if(c->address.sa.sa_family == AF_INET6) {
553                 static const int option = 1;
554                 setsockopt(c->socket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
555         }
556
557 #endif
558
559         bind_to_address(mesh, c);
560
561         /* Connect */
562
563         if(!mesh->proxytype) {
564                 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
565         } else {
566                 result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
567                 freeaddrinfo(proxyai);
568         }
569
570         if(result == -1 && !sockinprogress(sockerrno)) {
571                 logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno));
572                 free_connection(c);
573
574                 goto begin;
575         }
576
577         /* Now that there is a working socket, fill in the rest and register this connection. */
578
579         c->status.connecting = true;
580         c->status.initiator = true;
581         c->name = xstrdup(outgoing->node->name);
582         c->last_ping_time = mesh->loop.now.tv_sec;
583
584         connection_add(mesh, c);
585
586         io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ | IO_WRITE);
587
588         return true;
589 }
590
591 void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
592         timeout_del(&mesh->loop, &outgoing->ev);
593
594         if(outgoing->node->connection) {
595                 logger(mesh, MESHLINK_INFO, "Already connected to %s", outgoing->node->name);
596
597                 outgoing->node->connection->outgoing = outgoing;
598                 return;
599         }
600
601
602         if(outgoing->ai) {
603                 if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
604                         free_known_addresses(outgoing->ai);
605                 } else {
606                         freeaddrinfo(outgoing->ai);
607                 }
608         }
609
610         outgoing->state = OUTGOING_START;
611
612         if(outgoing->node->status.blacklisted) {
613                 return;
614         }
615
616         if(mesh->connection_try_cb) {
617                 mesh->connection_try_cb(mesh, (meshlink_node_t *)outgoing->node);
618         }
619
620         do_outgoing_connection(mesh, outgoing);
621 }
622
623 /// Delayed close of a filedescriptor.
624 static void tarpit(meshlink_handle_t *mesh, int fd) {
625         if(!fd) {
626                 return;
627         }
628
629         if(mesh->pits[mesh->next_pit]) {
630                 closesocket(mesh->pits[mesh->next_pit]);
631         }
632
633         mesh->pits[mesh->next_pit++] = fd;
634
635         if(mesh->next_pit >= (int)(sizeof mesh->pits / sizeof mesh->pits[0])) {
636                 mesh->next_pit = 0;
637         }
638 }
639
640 /*
641   accept a new tcp connect and create a
642   new connection
643 */
644 void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) {
645         (void)flags;
646         meshlink_handle_t *mesh = loop->data;
647         listen_socket_t *l = data;
648         connection_t *c;
649         sockaddr_t sa;
650         int fd;
651         socklen_t len = sizeof(sa);
652
653         memset(&sa, 0, sizeof(sa));
654
655         fd = accept(l->tcp.fd, &sa.sa, &len);
656
657         if(fd < 0) {
658                 if(errno == EINVAL) { // TODO: check if Windows agrees
659                         event_loop_stop(loop);
660                         return;
661                 }
662
663                 logger(mesh, MESHLINK_ERROR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
664                 return;
665         }
666
667         sockaddrunmap(&sa);
668
669         /* Rate limit incoming connections to max_connection_burst/second. */
670
671         if(mesh->loop.now.tv_sec != mesh->connection_burst_time) {
672                 mesh->connection_burst_time = mesh->loop.now.tv_sec;
673                 mesh->connection_burst = 0;
674         }
675
676         if(mesh->connection_burst >= max_connection_burst) {
677                 tarpit(mesh, fd);
678                 return;
679         }
680
681         mesh->connection_burst++;
682
683         // Accept the new connection
684
685         c = new_connection();
686         c->name = xstrdup("<unknown>");
687
688         c->address = sa;
689         c->socket = fd;
690         c->last_ping_time = mesh->loop.now.tv_sec;
691
692         char *hostname = sockaddr2hostname(&sa);
693         logger(mesh, MESHLINK_INFO, "Connection from %s", hostname);
694         free(hostname);
695
696         io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ);
697
698         configure_tcp(c);
699
700         connection_add(mesh, c);
701
702         c->allow_request = ID;
703         send_id(mesh, c);
704 }
705
706 static void free_outgoing(outgoing_t *outgoing) {
707         meshlink_handle_t *mesh = outgoing->node->mesh;
708
709         timeout_del(&mesh->loop, &outgoing->ev);
710
711         if(outgoing->ai) {
712                 if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
713                         free_known_addresses(outgoing->ai);
714                 } else {
715                         freeaddrinfo(outgoing->ai);
716                 }
717         }
718
719         free(outgoing);
720 }
721
722 void init_outgoings(meshlink_handle_t *mesh) {
723         mesh->outgoings = list_alloc((list_action_t)free_outgoing);
724 }
725
726 void exit_outgoings(meshlink_handle_t *mesh) {
727         if(mesh->outgoings) {
728                 list_delete_list(mesh->outgoings);
729                 mesh->outgoings = NULL;
730         }
731 }