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