2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
23 #include "connection.h"
26 #include "meshlink_internal.h"
34 /* Needed on Mac OS/X */
36 #define SOL_TCP IPPROTO_TCP
40 #define MSG_NOSIGNAL 0
43 static const int max_connection_burst = 100;
47 static void configure_tcp(connection_t *c) {
49 int flags = fcntl(c->socket, F_GETFL);
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));
56 unsigned long arg = 1;
58 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
59 logger(c->mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
64 #if defined(SOL_TCP) && defined(TCP_NODELAY)
66 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay));
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));
75 static bool bind_to_address(meshlink_handle_t *mesh, connection_t *c) {
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) {
94 sockaddr_t sa = mesh->listen_socket[s].sa;
96 if(sa.sa.sa_family == AF_INET) {
98 } else if(sa.sa.sa_family == AF_INET6) {
102 if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
103 logger(mesh, MESHLINK_WARNING, "Can't bind outgoing socket: %s", strerror(errno));
110 int setup_listen_socket(const sockaddr_t *sa) {
115 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
118 logger(NULL, MESHLINK_ERROR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
123 fcntl(nfd, F_SETFD, FD_CLOEXEC);
126 /* Optimize TCP settings */
129 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
131 #if defined(IPV6_V6ONLY)
133 if(sa->sa.sa_family == AF_INET6) {
134 setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
138 #warning IPV6_V6ONLY not defined
141 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
143 addrstr = sockaddr2hostname(sa);
144 logger(NULL, MESHLINK_ERROR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
151 logger(NULL, MESHLINK_ERROR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
158 int setup_vpn_in_socket(meshlink_handle_t *mesh, const sockaddr_t *sa) {
163 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
166 logger(mesh, MESHLINK_ERROR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
171 fcntl(nfd, F_SETFD, FD_CLOEXEC);
176 int flags = fcntl(nfd, F_GETFL);
178 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
180 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "fcntl",
187 unsigned long arg = 1;
189 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
191 logger(mesh, MESHLINK_ERROR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
198 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
199 setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
201 #if defined(IPV6_V6ONLY)
203 if(sa->sa.sa_family == AF_INET6) {
204 setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
209 #if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
210 #define IP_DONTFRAGMENT IP_DONTFRAG
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)
218 setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
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)
226 setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
229 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
231 addrstr = sockaddr2hostname(sa);
232 logger(mesh, MESHLINK_ERROR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
238 } /* int setup_vpn_in_socket */
240 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);
248 void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
249 if(!mesh->reachable && mesh->loop.now.tv_sec < mesh->last_unreachable + mesh->dev_class_traits[mesh->devclass].fast_retry_period) {
250 outgoing->timeout = 1;
252 outgoing->timeout += 5;
255 if(outgoing->timeout > mesh->maxtimeout) {
256 outgoing->timeout = mesh->maxtimeout;
259 timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval) {
260 outgoing->timeout, prng(mesh, TIMER_FUDGE)
263 logger(mesh, MESHLINK_INFO, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
266 void finish_connecting(meshlink_handle_t *mesh, connection_t *c) {
267 logger(mesh, MESHLINK_INFO, "Connected to %s", c->name);
269 c->last_ping_time = mesh->loop.now.tv_sec;
270 c->status.connecting = false;
275 static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) {
276 if(c->outbuf.len <= c->outbuf.offset) {
280 ssize_t outlen = send(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset, MSG_NOSIGNAL);
283 if(!errno || errno == EPIPE) {
284 logger(mesh, MESHLINK_INFO, "Connection closed by %s", c->name);
285 } else if(sockwouldblock(sockerrno)) {
286 logger(mesh, MESHLINK_DEBUG, "Sending %lu bytes to %s would block", (unsigned long)(c->outbuf.len - c->outbuf.offset), c->name);
289 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));
292 terminate_connection(mesh, c, c->status.active);
296 buffer_read(&c->outbuf, outlen);
299 io_set(&mesh->loop, &c->io, IO_READ);
303 static void handle_meta_io(event_loop_t *loop, void *data, int flags) {
304 meshlink_handle_t *mesh = loop->data;
305 connection_t *c = data;
307 if(c->status.connecting) {
308 c->status.connecting = false;
311 socklen_t len = sizeof(result);
312 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
315 finish_connecting(mesh, c);
317 logger(mesh, MESHLINK_DEBUG, "Error while connecting to %s: %s", c->name, sockstrerror(result));
318 terminate_connection(mesh, c, false);
323 if(flags & IO_WRITE) {
324 handle_meta_write(mesh, c);
326 handle_meta_connection_data(mesh, c);
330 // Find edges pointing to this node, and use them to build a list of unique, known addresses.
331 static struct addrinfo *get_known_addresses(node_t *n) {
332 struct addrinfo *ai = NULL;
334 for splay_each(edge_t, e, n->edge_tree) {
341 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
342 if(!sockaddrcmp(&e->reverse->address, (sockaddr_t *)aip->ai_addr)) {
352 // Create a new struct addrinfo, and put it at the head of the list.
353 struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(e->reverse->address.sa));
357 ai->ai_family = e->reverse->address.sa.sa_family;
358 ai->ai_socktype = SOCK_STREAM;
359 ai->ai_protocol = IPPROTO_TCP;
360 ai->ai_addrlen = SALEN(e->reverse->address.sa);
361 ai->ai_addr = (struct sockaddr *)(nai + 1);
362 memcpy(ai->ai_addr, &e->reverse->address, ai->ai_addrlen);
368 // Build a list of recently seen addresses.
369 static struct addrinfo *get_recent_addresses(node_t *n) {
370 struct addrinfo *ai = NULL;
371 struct addrinfo *aip;
373 for(int i = 0; i < 5; i++) {
374 if(!n->recent[i].sa.sa_family) {
378 // Create a new struct addrinfo, and put it at the end of the list.
379 struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(n->recent[i].sa));
389 nai->ai_family = n->recent[i].sa.sa_family;
390 nai->ai_socktype = SOCK_STREAM;
391 nai->ai_protocol = IPPROTO_TCP;
392 nai->ai_addrlen = SALEN(n->recent[i].sa);
393 nai->ai_addr = (struct sockaddr *)(nai + 1);
394 memcpy(nai->ai_addr, &n->recent[i], nai->ai_addrlen);
400 // Free struct addrinfo list from get_known_addresses().
401 static void free_known_addresses(struct addrinfo *ai) {
402 for(struct addrinfo *aip = ai, *next; aip; aip = next) {
408 static struct addrinfo *get_canonical_address(node_t *n) {
409 if(!n->canonical_address) {
413 char *address = xstrdup(n->canonical_address);
414 char *port = strchr(address, ' ');
423 struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
429 static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
434 if(outgoing->state == OUTGOING_START) {
436 outgoing->state = OUTGOING_CANONICAL;
439 if(outgoing->state == OUTGOING_CANONICAL) {
441 outgoing->ai = get_canonical_address(outgoing->node);
442 outgoing->aip = outgoing->ai;
444 outgoing->aip = outgoing->aip->ai_next;
451 freeaddrinfo(outgoing->ai);
453 outgoing->aip = NULL;
454 outgoing->state = OUTGOING_RECENT;
457 if(outgoing->state == OUTGOING_RECENT) {
459 outgoing->ai = get_recent_addresses(outgoing->node);
460 outgoing->aip = outgoing->ai;
462 outgoing->aip = outgoing->aip->ai_next;
469 free_known_addresses(outgoing->ai);
471 outgoing->aip = NULL;
472 outgoing->state = OUTGOING_KNOWN;
475 if(outgoing->state == OUTGOING_KNOWN) {
477 outgoing->ai = get_known_addresses(outgoing->node);
478 outgoing->aip = outgoing->ai;
480 outgoing->aip = outgoing->aip->ai_next;
487 free_known_addresses(outgoing->ai);
489 outgoing->aip = NULL;
490 outgoing->state = OUTGOING_END;
494 outgoing->state = OUTGOING_NO_KNOWN_ADDRESSES;
500 void do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
501 struct addrinfo *proxyai = NULL;
506 if(!get_next_outgoing_address(mesh, outgoing)) {
507 if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) {
508 logger(mesh, MESHLINK_ERROR, "No known addresses for %s", outgoing->node->name);
510 logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->node->name);
511 retry_outgoing(mesh, outgoing);
517 connection_t *c = new_connection();
518 c->outgoing = outgoing;
520 memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
522 char *hostname = sockaddr2hostname(&c->address);
524 logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname);
526 if(!mesh->proxytype) {
527 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
530 proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
538 logger(mesh, MESHLINK_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport);
539 c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
543 if(c->socket == -1) {
544 logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
553 fcntl(c->socket, F_SETFD, FD_CLOEXEC);
556 #if defined(IPV6_V6ONLY)
558 if(c->address.sa.sa_family == AF_INET6) {
559 static const int option = 1;
560 setsockopt(c->socket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
565 bind_to_address(mesh, c);
569 if(!mesh->proxytype) {
570 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
572 result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
573 freeaddrinfo(proxyai);
576 if(result == -1 && !sockinprogress(sockerrno)) {
577 logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno));
583 /* Now that there is a working socket, fill in the rest and register this connection. */
585 c->status.connecting = true;
586 c->status.initiator = true;
587 c->name = xstrdup(outgoing->node->name);
588 c->last_ping_time = mesh->loop.now.tv_sec;
590 connection_add(mesh, c);
592 io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ | IO_WRITE);
595 void reset_outgoing(outgoing_t *outgoing) {
597 if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
598 free_known_addresses(outgoing->ai);
600 freeaddrinfo(outgoing->ai);
605 outgoing->aip = NULL;
606 outgoing->state = OUTGOING_START;
609 void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
610 timeout_del(&mesh->loop, &outgoing->ev);
612 if(outgoing->node->connection) {
613 logger(mesh, MESHLINK_INFO, "Already connected to %s", outgoing->node->name);
615 outgoing->node->connection->outgoing = outgoing;
619 reset_outgoing(outgoing);
621 if(outgoing->node->status.blacklisted) {
625 if(mesh->connection_try_cb) {
626 mesh->connection_try_cb(mesh, (meshlink_node_t *)outgoing->node);
629 do_outgoing_connection(mesh, outgoing);
632 /// Delayed close of a filedescriptor.
633 static void tarpit(meshlink_handle_t *mesh, int fd) {
638 if(mesh->pits[mesh->next_pit]) {
639 closesocket(mesh->pits[mesh->next_pit]);
642 mesh->pits[mesh->next_pit++] = fd;
644 if(mesh->next_pit >= (int)(sizeof mesh->pits / sizeof mesh->pits[0])) {
650 accept a new tcp connect and create a
653 void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) {
655 meshlink_handle_t *mesh = loop->data;
656 listen_socket_t *l = data;
660 socklen_t len = sizeof(sa);
662 memset(&sa, 0, sizeof(sa));
664 fd = accept(l->tcp.fd, &sa.sa, &len);
667 if(errno == EINVAL) { // TODO: check if Windows agrees
668 event_loop_stop(loop);
672 logger(mesh, MESHLINK_ERROR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
678 /* Rate limit incoming connections to max_connection_burst/second. */
680 if(mesh->loop.now.tv_sec != mesh->connection_burst_time) {
681 mesh->connection_burst_time = mesh->loop.now.tv_sec;
682 mesh->connection_burst = 0;
685 if(mesh->connection_burst >= max_connection_burst) {
690 mesh->connection_burst++;
692 // Accept the new connection
694 c = new_connection();
695 c->name = xstrdup("<unknown>");
699 c->last_ping_time = mesh->loop.now.tv_sec;
701 char *hostname = sockaddr2hostname(&sa);
702 logger(mesh, MESHLINK_INFO, "Connection from %s", hostname);
705 io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ);
709 connection_add(mesh, c);
711 c->allow_request = ID;
715 static void free_outgoing(outgoing_t *outgoing) {
716 meshlink_handle_t *mesh = outgoing->node->mesh;
718 timeout_del(&mesh->loop, &outgoing->ev);
721 if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
722 free_known_addresses(outgoing->ai);
724 freeaddrinfo(outgoing->ai);
731 void init_outgoings(meshlink_handle_t *mesh) {
732 mesh->outgoings = list_alloc((list_action_t)free_outgoing);
735 void exit_outgoings(meshlink_handle_t *mesh) {
736 if(mesh->outgoings) {
737 list_delete_list(mesh->outgoings);
738 mesh->outgoings = NULL;