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 int addressfamily = AF_UNSPEC;
44 int seconds_till_retry = 5;
45 int max_connection_burst = 100;
49 static void configure_tcp(connection_t *c) {
51 int flags = fcntl(c->socket, F_GETFL);
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));
58 unsigned long arg = 1;
60 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
61 logger(c->mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
66 #if defined(SOL_TCP) && defined(TCP_NODELAY)
68 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay));
71 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
72 int lowdelay = IPTOS_LOWDELAY;
73 setsockopt(c->socket, SOL_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay));
77 static bool bind_to_address(meshlink_handle_t *mesh, connection_t *c) {
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) {
96 sockaddr_t sa = mesh->listen_socket[s].sa;
98 if(sa.sa.sa_family == AF_INET) {
100 } else if(sa.sa.sa_family == AF_INET6) {
101 sa.in6.sin6_port = 0;
104 if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
105 logger(mesh, MESHLINK_WARNING, "Can't bind outgoing socket: %s", strerror(errno));
112 int setup_listen_socket(const sockaddr_t *sa) {
117 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
120 logger(NULL, MESHLINK_ERROR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
125 fcntl(nfd, F_SETFD, FD_CLOEXEC);
128 /* Optimize TCP settings */
131 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
133 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
135 if(sa->sa.sa_family == AF_INET6) {
136 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
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(IPPROTO_IPV6) && 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(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
215 if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
216 option = IP_PMTUDISC_DO;
217 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
220 #elif defined(IPPROTO_IP) && defined(IP_DONTFRAGMENT)
222 if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
224 setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
228 #warning No way to disable IPv4 fragmentation
231 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
233 if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
234 option = IPV6_PMTUDISC_DO;
235 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
238 #elif defined(IPPROTO_IPV6) && defined(IPV6_DONTFRAG)
240 if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
242 setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
246 #warning No way to disable IPv6 fragmentation
249 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
251 addrstr = sockaddr2hostname(sa);
252 logger(mesh, MESHLINK_ERROR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
258 } /* int setup_vpn_in_socket */
260 static void retry_outgoing_handler(event_loop_t *loop, void *data) {
261 meshlink_handle_t *mesh = loop->data;
262 outgoing_t *outgoing = data;
263 setup_outgoing_connection(mesh, outgoing);
266 void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
267 outgoing->timeout += 5;
269 if(outgoing->timeout > mesh->maxtimeout) {
270 outgoing->timeout = mesh->maxtimeout;
273 timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval) {
274 outgoing->timeout, rand() % 100000
277 logger(mesh, MESHLINK_INFO, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
280 void finish_connecting(meshlink_handle_t *mesh, connection_t *c) {
281 logger(mesh, MESHLINK_INFO, "Connected to %s", c->name);
283 c->last_ping_time = mesh->loop.now.tv_sec;
284 c->status.connecting = false;
289 static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) {
290 if(c->outbuf.len <= c->outbuf.offset) {
294 ssize_t outlen = send(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset, MSG_NOSIGNAL);
297 if(!errno || errno == EPIPE) {
298 logger(mesh, MESHLINK_INFO, "Connection closed by %s", c->name);
299 } else if(sockwouldblock(sockerrno)) {
300 logger(mesh, MESHLINK_DEBUG, "Sending %lu bytes to %s would block", (unsigned long)(c->outbuf.len - c->outbuf.offset), c->name);
303 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));
306 terminate_connection(mesh, c, c->status.active);
310 buffer_read(&c->outbuf, outlen);
313 io_set(&mesh->loop, &c->io, IO_READ);
317 static void handle_meta_io(event_loop_t *loop, void *data, int flags) {
318 meshlink_handle_t *mesh = loop->data;
319 connection_t *c = data;
321 if(c->status.connecting) {
322 c->status.connecting = false;
325 socklen_t len = sizeof(result);
326 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
329 finish_connecting(mesh, c);
331 logger(mesh, MESHLINK_DEBUG, "Error while connecting to %s: %s", c->name, sockstrerror(result));
332 terminate_connection(mesh, c, false);
337 if(flags & IO_WRITE) {
338 handle_meta_write(mesh, c);
340 handle_meta_connection_data(mesh, c);
344 // Find edges pointing to this node, and use them to build a list of unique, known addresses.
345 static struct addrinfo *get_known_addresses(node_t *n) {
346 struct addrinfo *ai = NULL;
348 for splay_each(edge_t, e, n->edge_tree) {
355 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
356 if(!sockaddrcmp(&e->reverse->address, (sockaddr_t *)aip->ai_addr)) {
366 // Create a new struct addrinfo, and put it at the head of the list.
367 struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(e->reverse->address.sa));
371 ai->ai_family = e->reverse->address.sa.sa_family;
372 ai->ai_socktype = SOCK_STREAM;
373 ai->ai_protocol = IPPROTO_TCP;
374 ai->ai_addrlen = SALEN(e->reverse->address.sa);
375 ai->ai_addr = (struct sockaddr *)(nai + 1);
376 memcpy(ai->ai_addr, &e->reverse->address, ai->ai_addrlen);
382 // Free struct addrinfo list from get_known_addresses().
383 static void free_known_addresses(struct addrinfo *ai) {
384 for(struct addrinfo *aip = ai, *next; aip; aip = next) {
390 bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
391 char *address, *port, *space;
392 struct addrinfo *proxyai = NULL;
397 if(!outgoing->ai && !outgoing->nai) {
399 logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->name);
400 retry_outgoing(mesh, outgoing);
404 get_config_string(outgoing->cfg, &address);
406 space = strchr(address, ' ');
409 port = xstrdup(space + 1);
412 // TODO: Only allow Address statements?
413 if(!get_config_string(lookup_config(outgoing->config_tree, "Port"), &port)) {
414 logger(mesh, MESHLINK_ERROR, "No Port known for %s", outgoing->name);
415 retry_outgoing(mesh, outgoing);
420 outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
424 outgoing->aip = outgoing->ai;
425 outgoing->cfg = lookup_config_next(outgoing->config_tree, outgoing->cfg);
430 freeaddrinfo(outgoing->ai);
436 free_known_addresses(outgoing->nai);
439 outgoing->nai = NULL;
444 connection_t *c = new_connection();
445 c->outgoing = outgoing;
447 memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
448 outgoing->aip = outgoing->aip->ai_next;
450 char *hostname = sockaddr2hostname(&c->address);
452 logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->name, hostname);
454 if(!mesh->proxytype) {
455 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
458 proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
466 logger(mesh, MESHLINK_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport);
467 c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
471 if(c->socket == -1) {
472 logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, c->hostname, sockstrerror(sockerrno));
481 fcntl(c->socket, F_SETFD, FD_CLOEXEC);
484 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
486 if(c->address.sa.sa_family == AF_INET6) {
487 static const int option = 1;
488 setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
493 bind_to_address(mesh, c);
497 if(!mesh->proxytype) {
498 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
500 result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
501 freeaddrinfo(proxyai);
504 if(result == -1 && !sockinprogress(sockerrno)) {
505 logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->name, sockstrerror(sockerrno));
511 /* Now that there is a working socket, fill in the rest and register this connection. */
513 c->status.connecting = true;
514 c->name = xstrdup(outgoing->name);
515 c->outcompression = mesh->self->connection->outcompression;
516 c->last_ping_time = mesh->loop.now.tv_sec;
518 connection_add(mesh, c);
520 io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ | IO_WRITE);
525 void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
526 bool blacklisted = false;
527 timeout_del(&mesh->loop, &outgoing->ev);
529 node_t *n = lookup_node(mesh, outgoing->name);
531 if(n && n->connection) {
532 logger(mesh, MESHLINK_INFO, "Already connected to %s", outgoing->name);
534 n->connection->outgoing = outgoing;
538 exit_configuration(&outgoing->config_tree); // discard old configuration if present
539 init_configuration(&outgoing->config_tree);
540 read_host_config(mesh, outgoing->config_tree, outgoing->name);
541 outgoing->cfg = lookup_config(outgoing->config_tree, "Address");
543 get_config_bool(lookup_config(outgoing->config_tree, "blacklisted"), &blacklisted);
551 outgoing->aip = outgoing->nai = get_known_addresses(n);
555 logger(mesh, MESHLINK_ERROR, "No address known for %s", outgoing->name);
560 do_outgoing_connection(mesh, outgoing);
564 accept a new tcp connect and create a
567 void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) {
569 meshlink_handle_t *mesh = loop->data;
570 listen_socket_t *l = data;
574 socklen_t len = sizeof(sa);
576 fd = accept(l->tcp.fd, &sa.sa, &len);
579 if(errno == EINVAL) { // TODO: check if Windows agrees
580 event_loop_stop(loop);
584 logger(mesh, MESHLINK_ERROR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
590 // Check if we get many connections from the same host
592 static sockaddr_t prev_sa;
593 static int tarpit = -1;
600 if(!sockaddrcmp_noport(&sa, &prev_sa)) {
601 static int samehost_burst;
602 static int samehost_burst_time;
604 if(mesh->loop.now.tv_sec - samehost_burst_time > samehost_burst) {
607 samehost_burst -= mesh->loop.now.tv_sec - samehost_burst_time;
610 samehost_burst_time = mesh->loop.now.tv_sec;
613 if(samehost_burst > max_connection_burst) {
619 memcpy(&prev_sa, &sa, sizeof(sa));
621 // Check if we get many connections from different hosts
623 static int connection_burst;
624 static int connection_burst_time;
626 if(mesh->loop.now.tv_sec - connection_burst_time > connection_burst) {
627 connection_burst = 0;
629 connection_burst -= mesh->loop.now.tv_sec - connection_burst_time;
632 connection_burst_time = mesh->loop.now.tv_sec;
635 if(connection_burst >= max_connection_burst) {
636 connection_burst = max_connection_burst;
641 // Accept the new connection
643 c = new_connection();
644 c->name = xstrdup("<unknown>");
645 c->outcompression = mesh->self->connection->outcompression;
649 c->last_ping_time = mesh->loop.now.tv_sec;
651 char *hostname = sockaddr2hostname(&sa);
652 logger(mesh, MESHLINK_INFO, "Connection from %s", c->hostname);
655 io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ);
659 connection_add(mesh, c);
661 c->allow_request = ID;
665 static void free_outgoing(outgoing_t *outgoing) {
666 meshlink_handle_t *mesh = outgoing->mesh;
668 timeout_del(&mesh->loop, &outgoing->ev);
671 freeaddrinfo(outgoing->ai);
675 free_known_addresses(outgoing->nai);
678 if(outgoing->config_tree) {
679 exit_configuration(&outgoing->config_tree);
683 free(outgoing->name);
689 void try_outgoing_connections(meshlink_handle_t *mesh) {
690 /* If there is no outgoing list yet, create one. Otherwise, mark all outgoings as deleted. */
692 if(!mesh->outgoings) {
693 mesh->outgoings = list_alloc((list_action_t)free_outgoing);
695 for list_each(outgoing_t, outgoing, mesh->outgoings) {
696 outgoing->timeout = -1;
700 /* Make sure there is one outgoing_t in the list for each ConnectTo. */
702 // TODO: Drop support for ConnectTo since AutoConnect is now always on?
703 for(config_t *cfg = lookup_config(mesh->config, "ConnectTo"); cfg; cfg = lookup_config_next(mesh->config, cfg)) {
705 get_config_string(cfg, &name);
707 if(!check_id(name)) {
708 logger(mesh, MESHLINK_ERROR,
709 "Invalid name for outgoing connection in %s line %d",
710 cfg->file, cfg->line);
717 for list_each(outgoing_t, outgoing, mesh->outgoings) {
718 if(!strcmp(outgoing->name, name)) {
720 outgoing->timeout = 0;
726 outgoing_t *outgoing = xzalloc(sizeof(*outgoing));
727 outgoing->mesh = mesh;
728 outgoing->name = name;
729 list_insert_tail(mesh->outgoings, outgoing);
730 setup_outgoing_connection(mesh, outgoing);
734 /* Terminate any connections whose outgoing_t is to be deleted. */
736 for list_each(connection_t, c, mesh->connections) {
737 if(c->outgoing && c->outgoing->timeout == -1) {
739 logger(mesh, MESHLINK_INFO, "No more outgoing connection to %s", c->name);
740 terminate_connection(mesh, c, c->status.active);
744 /* Delete outgoing_ts for which there is no ConnectTo. */
746 for list_each(outgoing_t, outgoing, mesh->outgoings)
747 if(outgoing->timeout == -1) {
748 list_delete_node(mesh->outgoings, node);