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