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.
24 #include "connection.h"
27 #include "meshlink_internal.h"
35 /* Needed on Mac OS/X */
37 #define SOL_TCP IPPROTO_TCP
41 #define MSG_NOSIGNAL 0
44 static const int max_connection_burst = 100;
48 static void configure_tcp(connection_t *c) {
50 int flags = fcntl(c->socket, F_GETFL);
52 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
53 logger(c->mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "fcntl", strerror(errno));
57 unsigned long arg = 1;
59 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
60 logger(c->mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
65 #if defined(SOL_TCP) && defined(TCP_NODELAY)
67 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay));
70 #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
71 int lowdelay = IPTOS_LOWDELAY;
72 setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay));
75 #if defined(SO_NOSIGPIPE)
77 setsockopt(c->socket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&nosigpipe, sizeof(nosigpipe));
81 static void retry_outgoing_handler(event_loop_t *loop, void *data) {
84 meshlink_handle_t *mesh = loop->data;
85 outgoing_t *outgoing = data;
86 setup_outgoing_connection(mesh, outgoing);
89 void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
90 if(!mesh->reachable && mesh->loop.now.tv_sec < mesh->last_unreachable + mesh->dev_class_traits[outgoing->node->devclass].fast_retry_period) {
91 outgoing->timeout = 1;
93 outgoing->timeout += 5;
96 int maxtimeout = mesh->dev_class_traits[outgoing->node->devclass].maxtimeout;
98 if(outgoing->timeout > maxtimeout) {
99 outgoing->timeout = maxtimeout;
102 timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timespec) {
103 outgoing->timeout, prng(mesh, TIMER_FUDGE)
106 logger(mesh, MESHLINK_INFO, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
109 void finish_connecting(meshlink_handle_t *mesh, connection_t *c) {
110 logger(mesh, MESHLINK_INFO, "Connected to %s", c->name);
112 c->last_ping_time = mesh->loop.now.tv_sec;
113 c->status.connecting = false;
118 static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) {
119 if(c->outbuf.len <= c->outbuf.offset) {
123 ssize_t outlen = send(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset, MSG_NOSIGNAL);
126 if(!errno || errno == EPIPE) {
127 logger(mesh, MESHLINK_INFO, "Connection closed by %s", c->name);
128 } else if(sockwouldblock(sockerrno)) {
129 logger(mesh, MESHLINK_DEBUG, "Sending %lu bytes to %s would block", (unsigned long)(c->outbuf.len - c->outbuf.offset), c->name);
132 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));
135 terminate_connection(mesh, c, c->status.active);
139 buffer_read(&c->outbuf, outlen);
142 io_set(&mesh->loop, &c->io, IO_READ);
146 static void handle_meta_io(event_loop_t *loop, void *data, int flags) {
147 meshlink_handle_t *mesh = loop->data;
148 connection_t *c = data;
150 if(c->status.connecting) {
151 c->status.connecting = false;
154 socklen_t len = sizeof(result);
155 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
158 finish_connecting(mesh, c);
160 logger(mesh, MESHLINK_DEBUG, "Error while connecting to %s: %s", c->name, sockstrerror(result));
161 terminate_connection(mesh, c, false);
166 if(flags & IO_WRITE) {
167 handle_meta_write(mesh, c);
169 handle_meta_connection_data(mesh, c);
173 // Find edges pointing to this node, and use them to build a list of unique, known addresses.
174 static struct addrinfo *get_known_addresses(node_t *n) {
175 struct addrinfo *ai = NULL;
177 for splay_each(edge_t, e, n->edge_tree) {
184 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
185 if(!sockaddrcmp(&e->reverse->address, (sockaddr_t *)aip->ai_addr)) {
195 // Create a new struct addrinfo, and put it at the head of the list.
196 struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(e->reverse->address.sa));
200 ai->ai_family = e->reverse->address.sa.sa_family;
201 ai->ai_socktype = SOCK_STREAM;
202 ai->ai_protocol = IPPROTO_TCP;
203 ai->ai_addrlen = SALEN(e->reverse->address.sa);
204 ai->ai_addr = (struct sockaddr *)(nai + 1);
205 memcpy(ai->ai_addr, &e->reverse->address, ai->ai_addrlen);
211 // Build a list of recently seen addresses.
212 static struct addrinfo *get_recent_addresses(node_t *n) {
213 struct addrinfo *ai = NULL;
214 struct addrinfo *aip;
216 for(int i = 0; i < 5; i++) {
217 if(!n->recent[i].sa.sa_family) {
221 // Create a new struct addrinfo, and put it at the end of the list.
222 struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(n->recent[i].sa));
232 nai->ai_family = n->recent[i].sa.sa_family;
233 nai->ai_socktype = SOCK_STREAM;
234 nai->ai_protocol = IPPROTO_TCP;
235 nai->ai_addrlen = SALEN(n->recent[i].sa);
236 nai->ai_addr = (struct sockaddr *)(nai + 1);
237 memcpy(nai->ai_addr, &n->recent[i], nai->ai_addrlen);
243 // Free struct addrinfo list from get_known_addresses().
244 static void free_known_addresses(struct addrinfo *ai) {
245 for(struct addrinfo *aip = ai, *next; aip; aip = next) {
251 static void canonical_resolve_cb(meshlink_handle_t *mesh, char *host, char *serv, void *data, struct addrinfo *ai, int err) {
259 for list_each(outgoing_t, outgoing, mesh->outgoings) {
260 if(outgoing->node == n) {
261 if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
263 outgoing->aip = NULL;
264 outgoing->state = OUTGOING_CANONICAL;
265 do_outgoing_connection(mesh, outgoing);
273 static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
278 if(outgoing->state == OUTGOING_START) {
280 outgoing->state = OUTGOING_CANONICAL_RESOLVE;
283 if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
284 node_t *n = outgoing->node;
286 if(n->canonical_address) {
287 char *address = xstrdup(n->canonical_address);
288 char *port = strchr(address, ' ');
292 port = xstrdup(port);
293 adns_queue(mesh, address, port, canonical_resolve_cb, outgoing->node, 2);
296 logger(mesh, MESHLINK_ERROR, "Canonical address for %s is missing port number", n->name);
298 outgoing->state = OUTGOING_RECENT;
302 outgoing->state = OUTGOING_RECENT;
306 if(outgoing->state == OUTGOING_CANONICAL) {
308 outgoing->aip = outgoing->ai;
310 outgoing->aip = outgoing->aip->ai_next;
318 freeaddrinfo(outgoing->ai);
322 outgoing->aip = NULL;
323 outgoing->state = OUTGOING_END;
326 if(outgoing->state == OUTGOING_RECENT) {
328 outgoing->ai = get_recent_addresses(outgoing->node);
329 outgoing->aip = outgoing->ai;
331 outgoing->aip = outgoing->aip->ai_next;
338 free_known_addresses(outgoing->ai);
340 outgoing->aip = NULL;
341 outgoing->state = OUTGOING_KNOWN;
344 if(outgoing->state == OUTGOING_KNOWN) {
346 outgoing->ai = get_known_addresses(outgoing->node);
347 outgoing->aip = outgoing->ai;
349 outgoing->aip = outgoing->aip->ai_next;
356 free_known_addresses(outgoing->ai);
358 outgoing->aip = NULL;
359 outgoing->state = OUTGOING_END;
363 outgoing->state = OUTGOING_NO_KNOWN_ADDRESSES;
369 void do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
372 if(!get_next_outgoing_address(mesh, outgoing)) {
373 if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
374 /* We are waiting for a callback from the ADNS thread */
375 } else if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) {
376 logger(mesh, MESHLINK_ERROR, "No known addresses for %s", outgoing->node->name);
378 logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->node->name);
379 retry_outgoing(mesh, outgoing);
385 connection_t *c = new_connection();
386 c->outgoing = outgoing;
388 memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
390 if(mesh->log_level <= MESHLINK_INFO) {
391 char *hostname = sockaddr2hostname(&c->address);
392 logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname);
396 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
398 if(c->socket == -1) {
399 if(mesh->log_level <= MESHLINK_ERROR) {
400 char *hostname = sockaddr2hostname(&c->address);
401 logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
412 fcntl(c->socket, F_SETFD, FD_CLOEXEC);
415 #if defined(IPV6_V6ONLY)
417 if(c->address.sa.sa_family == AF_INET6) {
418 static const int option = 1;
419 setsockopt(c->socket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
426 int result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
428 if(result == -1 && !sockinprogress(sockerrno)) {
429 if(mesh->log_level <= MESHLINK_ERROR) {
430 char *hostname = sockaddr2hostname(&c->address);
431 logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno));
439 /* Now that there is a working socket, fill in the rest and register this connection. */
441 c->status.connecting = true;
442 c->status.initiator = true;
443 c->name = xstrdup(outgoing->node->name);
444 c->last_ping_time = mesh->loop.now.tv_sec;
446 connection_add(mesh, c);
448 io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ | IO_WRITE);
451 void reset_outgoing(outgoing_t *outgoing) {
453 if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
454 free_known_addresses(outgoing->ai);
456 freeaddrinfo(outgoing->ai);
461 outgoing->aip = NULL;
462 outgoing->state = OUTGOING_START;
465 void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
466 timeout_del(&mesh->loop, &outgoing->ev);
468 if(outgoing->node->connection) {
469 logger(mesh, MESHLINK_INFO, "Already connected to %s", outgoing->node->name);
471 outgoing->node->connection->outgoing = outgoing;
475 reset_outgoing(outgoing);
477 if(outgoing->node->status.blacklisted) {
481 if(mesh->connection_try_cb) {
482 mesh->connection_try_cb(mesh, (meshlink_node_t *)outgoing->node);
485 do_outgoing_connection(mesh, outgoing);
488 /// Delayed close of a filedescriptor.
489 static void tarpit(meshlink_handle_t *mesh, int fd) {
494 if(mesh->pits[mesh->next_pit]) {
495 closesocket(mesh->pits[mesh->next_pit]);
498 mesh->pits[mesh->next_pit++] = fd;
500 if(mesh->next_pit >= (int)(sizeof mesh->pits / sizeof mesh->pits[0])) {
506 accept a new tcp connect and create a
509 void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) {
511 meshlink_handle_t *mesh = loop->data;
512 listen_socket_t *l = data;
516 socklen_t len = sizeof(sa);
518 memset(&sa, 0, sizeof(sa));
520 fd = accept(l->tcp.fd, &sa.sa, &len);
523 if(errno == EINVAL) { // TODO: check if Windows agrees
524 event_loop_stop(loop);
528 logger(mesh, MESHLINK_ERROR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
534 /* Rate limit incoming connections to max_connection_burst/second. */
536 if(mesh->loop.now.tv_sec != mesh->connection_burst_time) {
537 mesh->connection_burst_time = mesh->loop.now.tv_sec;
538 mesh->connection_burst = 0;
541 if(mesh->connection_burst >= max_connection_burst) {
546 mesh->connection_burst++;
548 // Accept the new connection
550 c = new_connection();
551 c->name = xstrdup("<unknown>");
555 c->last_ping_time = mesh->loop.now.tv_sec;
557 char *hostname = sockaddr2hostname(&sa);
558 logger(mesh, MESHLINK_INFO, "Connection from %s", hostname);
561 io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ);
565 connection_add(mesh, c);
567 c->allow_request = ID;
571 static void free_outgoing(outgoing_t *outgoing) {
572 meshlink_handle_t *mesh = outgoing->node->mesh;
574 timeout_del(&mesh->loop, &outgoing->ev);
577 if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
578 free_known_addresses(outgoing->ai);
580 freeaddrinfo(outgoing->ai);
587 void init_outgoings(meshlink_handle_t *mesh) {
588 mesh->outgoings = list_alloc((list_action_t)free_outgoing);
591 void exit_outgoings(meshlink_handle_t *mesh) {
592 if(mesh->outgoings) {
593 list_delete_list(mesh->outgoings);
594 mesh->outgoings = NULL;