X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_socket.c;h=986a7668ef8acfc1712fbb0c6e7bc3452aaca2fe;hb=b99af2f813b897e1fd49c87a7cf44241cad3a017;hp=7f3db5c8bf59b499ebd4aced678498af1719bc81;hpb=19be9cf7150858311f7898fa3fb525d692d02f64;p=tinc diff --git a/src/net_socket.c b/src/net_socket.c index 7f3db5c8..986a7668 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -306,7 +306,7 @@ static void do_outgoing_pipe(connection_t *c, char *command) { int fd[2]; if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not create socketpair: %s\n", strerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Could not create socketpair: %s", strerror(errno)); return; } @@ -339,7 +339,7 @@ static void do_outgoing_pipe(connection_t *c, char *command) { int result = system(command); if(result < 0) - logger(DEBUG_ALWAYS, LOG_ERR, "Could not execute %s: %s\n", command, strerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Could not execute %s: %s", command, strerror(errno)); else if(result) logger(DEBUG_ALWAYS, LOG_ERR, "%s exited with non-zero status %d", command, result); exit(result); @@ -473,7 +473,15 @@ static void handle_meta_write(int sock, short events, void *data) { ssize_t outlen = send(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset, 0); if(outlen <= 0) { - logger(DEBUG_ALWAYS, LOG_ERR, "Onoes, outlen = %d (%s)", (int)outlen, strerror(errno)); + if(!errno || errno == EPIPE) { + logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)", c->name, c->hostname); + } else if(sockwouldblock(sockerrno)) { + logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Sending %d bytes to %s (%s) would block", c->outbuf.len - c->outbuf.offset, c->name, c->hostname); + return; + } else { + logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not send %d bytes of data to %s (%s): %s", c->outbuf.len - c->outbuf.offset, c->name, c->hostname, strerror(errno)); + } + terminate_connection(c, c->status.active); return; } @@ -576,6 +584,9 @@ void handle_new_meta_connection(int sock, short events, void *data) { } static void free_outgoing(outgoing_t *outgoing) { + if(event_initialized(&outgoing->ev)) + event_del(&outgoing->ev); + if(outgoing->ai) freeaddrinfo(outgoing->ai); @@ -590,8 +601,19 @@ void try_outgoing_connections(void) { char *name; outgoing_t *outgoing; - outgoing_list = list_alloc((list_action_t)free_outgoing); - + /* If there is no outgoing list yet, create one. Otherwise, mark all outgoings as deleted. */ + + if(!outgoing_list) { + outgoing_list = list_alloc((list_action_t)free_outgoing); + } else { + for(list_node_t *i = outgoing_list->head; i; i = i->next) { + outgoing = i->data; + outgoing->timeout = -1; + } + } + + /* Make sure there is one outgoing_t in the list for each ConnectTo. */ + for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) { get_config_string(cfg, &name); @@ -603,9 +625,42 @@ void try_outgoing_connections(void) { continue; } - outgoing = xmalloc_and_zero(sizeof *outgoing); - outgoing->name = name; - list_insert_tail(outgoing_list, outgoing); - setup_outgoing_connection(outgoing); + bool found = false; + + for(list_node_t *i = outgoing_list->head; i; i = i->next) { + outgoing = i->data; + if(!strcmp(outgoing->name, name)) { + found = true; + outgoing->timeout = 0; + break; + } + } + + if(!found) { + outgoing = xmalloc_and_zero(sizeof *outgoing); + outgoing->name = name; + list_insert_tail(outgoing_list, outgoing); + setup_outgoing_connection(outgoing); + } + } + + /* Terminate any connections whose outgoing_t is to be deleted. */ + + for(splay_node_t *n = connection_tree->head, *next; n; n = next) { + next = n->next; + connection_t *c = n->data; + if(c->outgoing && c->outgoing->timeout == -1) { + c->outgoing = NULL; + terminate_connection(c, c->status.active); + } + } + + /* Delete outgoing_ts for which there is no ConnectTo. */ + + for(list_node_t *i = outgoing_list->head, *next; i; i = next) { + next = i->next; + outgoing = i->data; + if(outgoing->timeout == -1) + list_delete_node(outgoing_list, i); } }