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
45 static void configure_tcp(connection_t *c) {
47 int flags = fcntl(c->socket, F_GETFL);
49 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
50 logger(c->mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "fcntl", strerror(errno));
54 unsigned long arg = 1;
56 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
57 logger(c->mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
62 #if defined(SOL_TCP) && defined(TCP_NODELAY)
64 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay));
67 #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
68 int lowdelay = IPTOS_LOWDELAY;
69 setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay));
72 #if defined(SO_NOSIGPIPE)
74 setsockopt(c->socket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&nosigpipe, sizeof(nosigpipe));
78 static void retry_outgoing_handler(event_loop_t *loop, void *data) {
81 meshlink_handle_t *mesh = loop->data;
82 outgoing_t *outgoing = data;
83 setup_outgoing_connection(mesh, outgoing);
86 void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
87 if(!mesh->reachable && mesh->loop.now.tv_sec < mesh->last_unreachable + mesh->dev_class_traits[outgoing->node->devclass].fast_retry_period) {
88 outgoing->timeout = 1;
90 outgoing->timeout += 5;
93 int maxtimeout = mesh->dev_class_traits[outgoing->node->devclass].maxtimeout;
95 if(outgoing->timeout > maxtimeout) {
96 outgoing->timeout = maxtimeout;
99 timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timespec) {
100 outgoing->timeout, prng(mesh, TIMER_FUDGE)
103 logger(mesh, MESHLINK_INFO, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
106 void finish_connecting(meshlink_handle_t *mesh, connection_t *c) {
107 logger(mesh, MESHLINK_INFO, "Connected to %s", c->name);
109 c->last_ping_time = mesh->loop.now.tv_sec;
110 c->status.connecting = false;
115 static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) {
116 if(c->outbuf.len <= c->outbuf.offset) {
120 ssize_t outlen = send(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset, MSG_NOSIGNAL);
123 if(!errno || errno == EPIPE) {
124 logger(mesh, MESHLINK_INFO, "Connection closed by %s", c->name);
125 } else if(sockwouldblock(sockerrno)) {
126 logger(mesh, MESHLINK_DEBUG, "Sending %lu bytes to %s would block", (unsigned long)(c->outbuf.len - c->outbuf.offset), c->name);
129 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));
132 terminate_connection(mesh, c, c->status.active);
136 buffer_read(&c->outbuf, outlen);
139 io_set(&mesh->loop, &c->io, IO_READ);
143 void flush_meta(meshlink_handle_t *mesh, connection_t *c) {
144 handle_meta_write(mesh, c);
147 static void handle_meta_io(event_loop_t *loop, void *data, int flags) {
148 meshlink_handle_t *mesh = loop->data;
149 connection_t *c = data;
151 if(c->status.connecting) {
152 c->status.connecting = false;
155 socklen_t len = sizeof(result);
156 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
159 finish_connecting(mesh, c);
161 logger(mesh, MESHLINK_ERROR, "Error while connecting to %s: %s", c->name, sockstrerror(result));
162 terminate_connection(mesh, c, false);
167 if(flags & IO_WRITE) {
168 handle_meta_write(mesh, c);
170 handle_meta_connection_data(mesh, c);
174 // Build a list of recently seen addresses.
175 static struct addrinfo *get_recent_addresses(node_t *n) {
176 struct addrinfo *ai = NULL;
177 struct addrinfo *aip;
179 for(int i = 0; i < 5; i++) {
180 if(!n->recent[i].sa.sa_family) {
184 // Create a new struct addrinfo, and put it at the end of the list.
185 struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(n->recent[i].sa));
195 nai->ai_family = n->recent[i].sa.sa_family;
196 nai->ai_socktype = SOCK_STREAM;
197 nai->ai_protocol = IPPROTO_TCP;
198 nai->ai_addrlen = SALEN(n->recent[i].sa);
199 nai->ai_addr = (struct sockaddr *)(nai + 1);
200 memcpy(nai->ai_addr, &n->recent[i], nai->ai_addrlen);
206 // Free struct addrinfo list from get_known_addresses().
207 static void free_known_addresses(struct addrinfo *ai) {
208 for(struct addrinfo *aip = ai, *next; aip; aip = next) {
214 static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
219 if(outgoing->state == OUTGOING_START) {
221 outgoing->state = OUTGOING_CANONICAL_RESOLVE;
224 if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
225 node_t *n = outgoing->node;
227 if(n->canonical_address) {
228 char *address = xstrdup(n->canonical_address);
229 char *port = strchr(address, ' ');
233 outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
234 outgoing->aip = NULL;
235 outgoing->state = OUTGOING_CANONICAL;
237 logger(mesh, MESHLINK_ERROR, "Canonical address for %s is missing port number", n->name);
238 outgoing->state = OUTGOING_RECENT;
243 outgoing->state = OUTGOING_RECENT;
247 if(outgoing->state == OUTGOING_CANONICAL) {
249 outgoing->aip = outgoing->ai;
251 outgoing->aip = outgoing->aip->ai_next;
259 freeaddrinfo(outgoing->ai);
263 outgoing->aip = NULL;
264 outgoing->state = OUTGOING_END;
267 if(outgoing->state == OUTGOING_RECENT) {
269 outgoing->ai = get_recent_addresses(outgoing->node);
270 outgoing->aip = outgoing->ai;
272 outgoing->aip = outgoing->aip->ai_next;
279 free_known_addresses(outgoing->ai);
281 outgoing->aip = NULL;
282 outgoing->state = OUTGOING_END;
286 outgoing->state = OUTGOING_NO_KNOWN_ADDRESSES;
292 void do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
295 if(!get_next_outgoing_address(mesh, outgoing)) {
296 if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
297 /* We are waiting for a callback from the ADNS thread */
298 } else if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) {
299 logger(mesh, MESHLINK_ERROR, "No known addresses for %s", outgoing->node->name);
300 list_delete(mesh->outgoings, outgoing);
302 logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->node->name);
303 retry_outgoing(mesh, outgoing);
309 connection_t *c = new_connection();
310 c->outgoing = outgoing;
312 memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
314 if(mesh->log_level <= MESHLINK_INFO) {
315 char *hostname = sockaddr2hostname(&c->address);
316 logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname);
320 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
322 if(c->socket == -1) {
323 if(mesh->log_level <= MESHLINK_ERROR) {
324 char *hostname = sockaddr2hostname(&c->address);
325 logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
336 fcntl(c->socket, F_SETFD, FD_CLOEXEC);
339 #if defined(IPV6_V6ONLY)
341 if(c->address.sa.sa_family == AF_INET6) {
342 static const int option = 1;
343 setsockopt(c->socket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
350 int result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
352 if(result == -1 && !sockinprogress(sockerrno)) {
353 if(mesh->log_level <= MESHLINK_ERROR) {
354 char *hostname = sockaddr2hostname(&c->address);
355 logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno));
363 /* Now that there is a working socket, fill in the rest and register this connection. */
365 c->status.connecting = true;
366 c->status.initiator = true;
367 c->name = xstrdup(outgoing->node->name);
368 c->last_ping_time = mesh->loop.now.tv_sec;
370 connection_add(mesh, c);
372 io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ | IO_WRITE);
375 void reset_outgoing(outgoing_t *outgoing) {
377 if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
378 free_known_addresses(outgoing->ai);
380 freeaddrinfo(outgoing->ai);
385 outgoing->aip = NULL;
386 outgoing->state = OUTGOING_START;
389 void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
390 timeout_del(&mesh->loop, &outgoing->ev);
392 if(outgoing->node->connection) {
393 logger(mesh, MESHLINK_INFO, "Already connected to %s", outgoing->node->name);
395 outgoing->node->connection->outgoing = outgoing;
399 reset_outgoing(outgoing);
401 if(outgoing->node->status.blacklisted) {
405 if(mesh->connection_try_cb) {
406 mesh->connection_try_cb(mesh, (meshlink_node_t *)outgoing->node);
409 do_outgoing_connection(mesh, outgoing);
412 static void free_outgoing(outgoing_t *outgoing) {
413 meshlink_handle_t *mesh = outgoing->node->mesh;
415 timeout_del(&mesh->loop, &outgoing->ev);
418 if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
419 free_known_addresses(outgoing->ai);
421 freeaddrinfo(outgoing->ai);
428 void init_outgoings(meshlink_handle_t *mesh) {
429 mesh->outgoings = list_alloc((list_action_t)free_outgoing);
432 void exit_outgoings(meshlink_handle_t *mesh) {
433 if(mesh->outgoings) {
434 list_delete_list(mesh->outgoings);
435 mesh->outgoings = NULL;