X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_socket.c;h=44b7ec7829d58f8b139f02d089607caa362468f0;hb=636200d1a2024982fe5b3062153daa72a8253015;hp=5394d3646c306791dcd5296401814919699f10a5;hpb=a67ab277c9fdbcfc8c0550e9046df2a00b5fed81;p=tinc diff --git a/src/net_socket.c b/src/net_socket.c index 5394d364..44b7ec78 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -22,7 +22,7 @@ #include "system.h" -#include "avl_tree.h" +#include "splay_tree.h" #include "conf.h" #include "connection.h" #include "logger.h" @@ -51,8 +51,7 @@ int listen_sockets; /* Setup sockets */ -static void configure_tcp(connection_t *c) -{ +static void configure_tcp(connection_t *c) { int option; #ifdef O_NONBLOCK @@ -71,17 +70,16 @@ static void configure_tcp(connection_t *c) #if defined(SOL_TCP) && defined(TCP_NODELAY) option = 1; - setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option)); + setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof option); #endif #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY) option = IPTOS_LOWDELAY; - setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option)); + setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof option); #endif } -int setup_listen_socket(const sockaddr_t *sa) -{ +int setup_listen_socket(const sockaddr_t *sa) { int nfd; char *addrstr; int option; @@ -99,17 +97,17 @@ int setup_listen_socket(const sockaddr_t *sa) /* Optimize TCP settings */ option = 1; - setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); + setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof option); if(get_config_string (lookup_config(config_tree, "BindToInterface"), &iface)) { #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) struct ifreq ifr; - memset(&ifr, 0, sizeof(ifr)); + memset(&ifr, 0, sizeof ifr); strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ); - if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) { + if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof ifr)) { closesocket(nfd); logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface, strerror(errno)); @@ -139,8 +137,7 @@ int setup_listen_socket(const sockaddr_t *sa) return nfd; } -int setup_vpn_in_socket(const sockaddr_t *sa) -{ +int setup_vpn_in_socket(const sockaddr_t *sa) { int nfd; char *addrstr; int option; @@ -178,7 +175,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) #endif option = 1; - setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); + setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof option); #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) { @@ -186,7 +183,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) { option = IP_PMTUDISC_DO; - setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option)); + setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof option); } } #endif @@ -197,7 +194,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice) { option = IPV6_PMTUDISC_DO; - setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option)); + setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof option); } } #endif @@ -208,10 +205,10 @@ int setup_vpn_in_socket(const sockaddr_t *sa) struct ifreq ifr; if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) { - memset(&ifr, 0, sizeof(ifr)); + memset(&ifr, 0, sizeof ifr); strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ); - if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) { + if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof ifr)) { closesocket(nfd); logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface, strerror(errno)); @@ -234,7 +231,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) } static void retry_outgoing_handler(int fd, short events, void *data) { - retry_outgoing(data); + setup_outgoing_connection(data); } void retry_outgoing(outgoing_t *outgoing) { @@ -253,21 +250,20 @@ void retry_outgoing(outgoing_t *outgoing) { outgoing->timeout); } -void finish_connecting(connection_t *c) -{ +void finish_connecting(connection_t *c) { cp(); ifdebug(CONNECTIONS) logger(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname); configure_tcp(c); - c->last_ping_time = now; + c->last_ping_time = time(NULL); + c->status.connecting = false; send_id(c); } -void do_outgoing_connection(connection_t *c) -{ +void do_outgoing_connection(connection_t *c) { char *address, *port; int result; @@ -278,8 +274,9 @@ begin: if(!c->outgoing->cfg) { ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"), c->name); - c->status.remove = true; retry_outgoing(c->outgoing); + c->outgoing = NULL; + connection_del(c); return; } @@ -297,7 +294,8 @@ begin: } if(!c->outgoing->aip) { - freeaddrinfo(c->outgoing->ai); + if(c->outgoing->ai) + freeaddrinfo(c->outgoing->ai); c->outgoing->ai = NULL; goto begin; } @@ -352,8 +350,22 @@ begin: return; } -void setup_outgoing_connection(outgoing_t *outgoing) -{ +void handle_meta_read(struct bufferevent *event, void *data) { + logger(LOG_EMERG, _("handle_meta_read() called")); + abort(); +} + +void handle_meta_write(struct bufferevent *event, void *data) { + ifdebug(META) logger(LOG_DEBUG, _("handle_meta_write() called")); +} + +void handle_meta_connection_error(struct bufferevent *event, short what, void *data) { + connection_t *c = data; + logger(LOG_EMERG, _("handle_meta_connection_error() called: %d: %s"), what, strerror(errno)); + terminate_connection(c, c->status.active); +} + +void setup_outgoing_connection(outgoing_t *outgoing) { connection_t *c; node_t *n; @@ -390,30 +402,31 @@ void setup_outgoing_connection(outgoing_t *outgoing) } c->outgoing = outgoing; - c->last_ping_time = now; + c->last_ping_time = time(NULL); connection_add(c); do_outgoing_connection(c); - event_set(&c->ev, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c); - event_set(&c->outev, c->socket, EV_WRITE | EV_PERSIST, flush_meta, c); - if(event_add(&c->ev, NULL) < 0) { - logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno)); + event_set(&c->inevent, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c); + event_add(&c->inevent, NULL); + c->buffer = bufferevent_new(c->socket, handle_meta_read, handle_meta_write, handle_meta_connection_error, c); + if(!c->buffer) { + logger(LOG_EMERG, _("bufferevent_new() failed: %s"), strerror(errno)); abort(); } + bufferevent_disable(c->buffer, EV_READ); } /* accept a new tcp connect and create a new connection */ -void handle_new_meta_connection(int sock, short events, void *data) -{ +void handle_new_meta_connection(int sock, short events, void *data) { connection_t *c; sockaddr_t sa; int fd; - socklen_t len = sizeof(sa); + socklen_t len = sizeof sa; cp(); @@ -436,17 +449,18 @@ void handle_new_meta_connection(int sock, short events, void *data) c->address = sa; c->hostname = sockaddr2hostname(&sa); c->socket = fd; - c->last_ping_time = now; + c->last_ping_time = time(NULL); ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname); - event_set(&c->ev, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c); - event_set(&c->outev, c->socket, EV_WRITE | EV_PERSIST, flush_meta, c); - if(event_add(&c->ev, NULL) < 0) { - logger(LOG_ERR, _("event_add failed: %s"), strerror(errno)); - connection_del(c); - return; + event_set(&c->inevent, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c); + event_add(&c->inevent, NULL); + c->buffer = bufferevent_new(c->socket, NULL, handle_meta_write, handle_meta_connection_error, c); + if(!c->buffer) { + logger(LOG_EMERG, _("bufferevent_new() failed: %s"), strerror(errno)); + abort(); } + bufferevent_disable(c->buffer, EV_READ); configure_tcp(c); @@ -456,8 +470,7 @@ void handle_new_meta_connection(int sock, short events, void *data) send_id(c); } -void try_outgoing_connections(void) -{ +void try_outgoing_connections(void) { static config_t *cfg = NULL; char *name; outgoing_t *outgoing; @@ -475,7 +488,7 @@ void try_outgoing_connections(void) continue; } - outgoing = xmalloc_and_zero(sizeof(*outgoing)); + outgoing = xmalloc_and_zero(sizeof *outgoing); outgoing->name = name; setup_outgoing_connection(outgoing); }