X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fnet_socket.c;h=36fa57044d0fe0fe167be36e9bd5e0207addc509;hb=HEAD;hp=63bf49ea3c6f0511de3322e9d84e21d8ddcc5092;hpb=4c57e6902219ecca1872e18e34365d8e54a0f407;p=meshlink diff --git a/src/net_socket.c b/src/net_socket.c index 63bf49ea..8b1263b0 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -19,6 +19,7 @@ #include "system.h" +#include "adns.h" #include "conf.h" #include "connection.h" #include "list.h" @@ -70,6 +71,11 @@ static void configure_tcp(connection_t *c) { int lowdelay = IPTOS_LOWDELAY; setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay)); #endif + +#if defined(SO_NOSIGPIPE) + int nosigpipe = 1; + setsockopt(c->socket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&nosigpipe, sizeof(nosigpipe)); +#endif } static void retry_outgoing_handler(event_loop_t *loop, void *data) { @@ -81,14 +87,16 @@ static void retry_outgoing_handler(event_loop_t *loop, void *data) { } void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) { - if(!mesh->reachable && mesh->loop.now.tv_sec < mesh->last_unreachable + mesh->dev_class_traits[mesh->devclass].fast_retry_period) { + if(!mesh->reachable && mesh->loop.now.tv_sec < mesh->last_unreachable + mesh->dev_class_traits[outgoing->node->devclass].fast_retry_period) { outgoing->timeout = 1; } else { outgoing->timeout += 5; } - if(outgoing->timeout > mesh->maxtimeout) { - outgoing->timeout = mesh->maxtimeout; + int maxtimeout = mesh->dev_class_traits[outgoing->node->devclass].maxtimeout; + + if(outgoing->timeout > maxtimeout) { + outgoing->timeout = maxtimeout; } timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timespec) { @@ -128,6 +136,10 @@ static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) { return; } + if(c->node) { + c->node->out_meta += outlen; + } + buffer_read(&c->outbuf, outlen); if(!c->outbuf.len) { @@ -135,6 +147,10 @@ static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) { } } +void flush_meta(meshlink_handle_t *mesh, connection_t *c) { + handle_meta_write(mesh, c); +} + static void handle_meta_io(event_loop_t *loop, void *data, int flags) { meshlink_handle_t *mesh = loop->data; connection_t *c = data; @@ -149,7 +165,7 @@ static void handle_meta_io(event_loop_t *loop, void *data, int flags) { if(!result) { finish_connecting(mesh, c); } else { - logger(mesh, MESHLINK_DEBUG, "Error while connecting to %s: %s", c->name, sockstrerror(result)); + logger(mesh, MESHLINK_ERROR, "Error while connecting to %s: %s", c->name, sockstrerror(result)); terminate_connection(mesh, c, false); return; } @@ -240,25 +256,26 @@ static void free_known_addresses(struct addrinfo *ai) { } } -static struct addrinfo *get_canonical_address(node_t *n) { - if(!n->canonical_address) { - return false; - } - - char *address = xstrdup(n->canonical_address); - char *port = strchr(address, ' '); +static void canonical_resolve_cb(meshlink_handle_t *mesh, char *host, char *serv, void *data, struct addrinfo *ai, int err) { + (void)serv; + (void)err; + node_t *n = data; + + free(host); + free(serv); + + for list_each(outgoing_t, outgoing, mesh->outgoings) { + if(outgoing->node == n) { + if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) { + outgoing->ai = ai; + outgoing->aip = NULL; + outgoing->state = OUTGOING_CANONICAL; + do_outgoing_connection(mesh, outgoing); + } - if(!port) { - free(address); - return false; + return; + } } - - *port++ = 0; - - struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM); - free(address); - - return ai; } static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) { @@ -268,12 +285,34 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo if(outgoing->state == OUTGOING_START) { start = true; - outgoing->state = OUTGOING_CANONICAL; + outgoing->state = OUTGOING_CANONICAL_RESOLVE; + } + + if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) { + node_t *n = outgoing->node; + + if(n->canonical_address) { + char *address = xstrdup(n->canonical_address); + char *port = strchr(address, ' '); + + if(port) { + *port++ = 0; + port = xstrdup(port); + adns_queue(mesh, address, port, canonical_resolve_cb, outgoing->node, 2); + return false; + } else { + logger(mesh, MESHLINK_ERROR, "Canonical address for %s is missing port number", n->name); + free(address); + outgoing->state = OUTGOING_RECENT; + } + + } else { + outgoing->state = OUTGOING_RECENT; + } } if(outgoing->state == OUTGOING_CANONICAL) { if(!outgoing->aip) { - outgoing->ai = get_canonical_address(outgoing->node); outgoing->aip = outgoing->ai; } else { outgoing->aip = outgoing->aip->ai_next; @@ -283,10 +322,13 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo return true; } - freeaddrinfo(outgoing->ai); + if(outgoing->ai) { + freeaddrinfo(outgoing->ai); + } + outgoing->ai = NULL; outgoing->aip = NULL; - outgoing->state = OUTGOING_RECENT; + outgoing->state = OUTGOING_END; } if(outgoing->state == OUTGOING_RECENT) { @@ -333,14 +375,14 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo } void do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) { - struct addrinfo *proxyai = NULL; - int result; - begin: if(!get_next_outgoing_address(mesh, outgoing)) { - if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) { + if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) { + /* We are waiting for a callback from the ADNS thread */ + } else if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) { logger(mesh, MESHLINK_ERROR, "No known addresses for %s", outgoing->node->name); + list_delete(mesh->outgoings, outgoing); } else { logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->node->name); retry_outgoing(mesh, outgoing); @@ -354,35 +396,26 @@ begin: memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen); - char *hostname = sockaddr2hostname(&c->address); - - logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname); + if(mesh->log_level <= MESHLINK_INFO) { + char *hostname = sockaddr2hostname(&c->address); + logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname); + free(hostname); + } - if(!mesh->proxytype) { - c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP); - configure_tcp(c); - } else { - proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM); + c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP); - if(!proxyai) { - free_connection(c); + if(c->socket == -1) { + if(mesh->log_level <= MESHLINK_ERROR) { + char *hostname = sockaddr2hostname(&c->address); + logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno)); free(hostname); - goto begin; } - logger(mesh, MESHLINK_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport); - c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP); - configure_tcp(c); - } - - if(c->socket == -1) { - logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno)); free_connection(c); - free(hostname); goto begin; } - free(hostname); + configure_tcp(c); #ifdef FD_CLOEXEC fcntl(c->socket, F_SETFD, FD_CLOEXEC); @@ -399,17 +432,16 @@ begin: /* Connect */ - if(!mesh->proxytype) { - result = connect(c->socket, &c->address.sa, SALEN(c->address.sa)); - } else { - result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen); - freeaddrinfo(proxyai); - } + int result = connect(c->socket, &c->address.sa, SALEN(c->address.sa)); if(result == -1 && !sockinprogress(sockerrno)) { - logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno)); - free_connection(c); + if(mesh->log_level <= MESHLINK_ERROR) { + char *hostname = sockaddr2hostname(&c->address); + logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno)); + free(hostname); + } + free_connection(c); goto begin; } @@ -497,6 +529,10 @@ void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) { fd = accept(l->tcp.fd, &sa.sa, &len); if(fd < 0) { + if(sockwouldblock(errno)) { + return; + } + if(errno == EINVAL) { // TODO: check if Windows agrees event_loop_stop(loop); return;