From: Guus Sliepen Date: Mon, 21 Apr 2014 21:16:56 +0000 (+0200) Subject: Move listen_sockets to mesh. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=068746e6cb8df6c197f415e09d3ade8426c2db5f;p=meshlink Move listen_sockets to mesh. --- diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 6426f83d..66b76aec 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -26,6 +26,15 @@ #include "meshlink.h" #include "sockaddr.h" +#define MAXSOCKETS 8 /* Probably overkill... */ + +typedef struct listen_socket_t { + struct io_t tcp; + struct io_t udp; + sockaddr_t sa; + bool bindto; +} listen_socket_t; + typedef enum proxytype_t { PROXY_NONE = 0, PROXY_SOCKS4, @@ -46,7 +55,8 @@ struct meshlink_handle { meshlink_log_level_t log_level; pthread_t thread; - struct list_t *sockets; + listen_socket_t listen_socket[MAXSOCKETS]; + int listen_sockets; struct node_t *self; diff --git a/src/net.h b/src/net.h index 432825fe..06797446 100644 --- a/src/net.h +++ b/src/net.h @@ -37,8 +37,6 @@ /* MAXBUFSIZE is the maximum size of a request: enough for a MAXSIZEd packet or a 8192 bits RSA key */ #define MAXBUFSIZE ((MAXSIZE > 2048 ? MAXSIZE : 2048) + 128) -#define MAXSOCKETS 8 /* Probably overkill... */ - typedef struct vpn_packet_t { struct { unsigned int probe:1; @@ -59,13 +57,6 @@ typedef enum packet_type_t { PACKET_PROBE } packet_type_t; -typedef struct listen_socket_t { - struct io_t tcp; - struct io_t udp; - sockaddr_t sa; - bool bindto; -} listen_socket_t; - #include "conf.h" #include "list.h" @@ -83,8 +74,6 @@ extern int maxoutbufsize; extern int addressfamily; extern unsigned replaywin; -extern listen_socket_t listen_socket[MAXSOCKETS]; -extern int listen_sockets; extern int keylifetime; extern int max_connection_burst; extern bool do_prune; diff --git a/src/net_packet.c b/src/net_packet.c index c7d6a9d8..a8c8b036 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -386,13 +386,13 @@ static void choose_udp_address(const node_t *n, const sockaddr_t **sa, int *sock if(candidate) { *sa = &candidate->address; - *sock = rand() % listen_sockets; + *sock = rand() % mesh->listen_sockets; } /* Make sure we have a suitable socket for the chosen address */ - if(listen_socket[*sock].sa.sa.sa_family != (*sa)->sa.sa_family) { - for(int i = 0; i < listen_sockets; i++) { - if(listen_socket[i].sa.sa.sa_family == (*sa)->sa.sa_family) { + if(mesh->listen_socket[*sock].sa.sa.sa_family != (*sa)->sa.sa_family) { + for(int i = 0; i < mesh->listen_sockets; i++) { + if(mesh->listen_socket[i].sa.sa.sa_family == (*sa)->sa.sa_family) { *sock = i; break; } @@ -417,15 +417,15 @@ static void choose_broadcast_address(const node_t *n, const sockaddr_t **sa, int } }; - *sock = rand() % listen_sockets; + *sock = rand() % mesh->listen_sockets; - if(listen_socket[*sock].sa.sa.sa_family == AF_INET6) { + if(mesh->listen_socket[*sock].sa.sa.sa_family == AF_INET6) { if(mesh->localdiscovery_address.sa.sa_family == AF_INET6) { mesh->localdiscovery_address.in6.sin6_port = n->prevedge->address.in.sin_port; *sa = &mesh->localdiscovery_address; } else { broadcast_ipv6.in6.sin6_port = n->prevedge->address.in.sin_port; - broadcast_ipv6.in6.sin6_scope_id = listen_socket[*sock].sa.in6.sin6_scope_id; + broadcast_ipv6.in6.sin6_scope_id = mesh->listen_socket[*sock].sa.in6.sin6_scope_id; *sa = &broadcast_ipv6; } } else { @@ -484,7 +484,7 @@ bool send_sptps_data(void *handle, uint8_t type, const char *data, size_t len) { else choose_udp_address(to, &sa, &sock); - if(sendto(listen_socket[sock].udp.fd, data, len, 0, &sa->sa, SALEN(sa->sa)) < 0 && !sockwouldblock(sockerrno)) { + if(sendto(mesh->listen_socket[sock].udp.fd, data, len, 0, &sa->sa, SALEN(sa->sa)) < 0 && !sockwouldblock(sockerrno)) { if(sockmsgsize(sockerrno)) { if(to->maxmtu >= len) to->maxmtu = len - 1; @@ -661,7 +661,7 @@ void handle_incoming_vpn_data(void *data, int flags) { return; } - n->sock = ls - listen_socket; + n->sock = ls - mesh->listen_socket; receive_udppacket(n, &pkt); } diff --git a/src/net_setup.c b/src/net_setup.c index bf6bab41..c5632339 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -222,8 +222,8 @@ static bool add_listen_address(char *address, bool bindto) { // Ignore duplicate addresses bool found = false; - for(int i = 0; i < listen_sockets; i++) - if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) { + for(int i = 0; i < mesh->listen_sockets; i++) + if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) { found = true; break; } @@ -231,7 +231,7 @@ static bool add_listen_address(char *address, bool bindto) { if(found) continue; - if(listen_sockets >= MAXSOCKETS) { + if(mesh->listen_sockets >= MAXSOCKETS) { logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets"); return false; } @@ -248,8 +248,8 @@ static bool add_listen_address(char *address, bool bindto) { continue; } - io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ); - io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ); + io_add(&mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ); + io_add(&mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ); if(debug_level >= DEBUG_CONNECTIONS) { char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr); @@ -257,9 +257,9 @@ static bool add_listen_address(char *address, bool bindto) { free(hostname); } - listen_socket[listen_sockets].bindto = bindto; - memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen); - listen_sockets++; + mesh->listen_socket[mesh->listen_sockets].bindto = bindto; + memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen); + mesh->listen_sockets++; } freeaddrinfo(ai); @@ -337,13 +337,13 @@ bool setup_myself(void) { /* Open sockets */ - listen_sockets = 0; + mesh->listen_sockets = 0; int cfgs = 0; if(!add_listen_address(address, NULL)) return false; - if(!listen_sockets) { + if(!mesh->listen_sockets) { logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!"); return false; } @@ -401,11 +401,11 @@ void close_network_connections(void) { free_connection(mesh->self->connection); } - for(int i = 0; i < listen_sockets; i++) { - io_del(&listen_socket[i].tcp); - io_del(&listen_socket[i].udp); - close(listen_socket[i].tcp.fd); - close(listen_socket[i].udp.fd); + for(int i = 0; i < mesh->listen_sockets; i++) { + io_del(&mesh->listen_socket[i].tcp); + io_del(&mesh->listen_socket[i].udp); + close(mesh->listen_socket[i].tcp.fd); + close(mesh->listen_socket[i].udp.fd); } exit_requests(); diff --git a/src/net_socket.c b/src/net_socket.c index bc599e8a..fbafc22b 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -41,9 +41,6 @@ int maxtimeout = 900; int seconds_till_retry = 5; int max_connection_burst = 100; -listen_socket_t listen_socket[MAXSOCKETS]; -int listen_sockets; - /* Setup sockets */ static void configure_tcp(connection_t *c) { @@ -77,8 +74,8 @@ static void configure_tcp(connection_t *c) { static bool bind_to_address(connection_t *c) { int s = -1; - for(int i = 0; i < listen_sockets && listen_socket[i].bindto; i++) { - if(listen_socket[i].sa.sa.sa_family != c->address.sa.sa_family) + for(int i = 0; i < mesh->listen_sockets && mesh->listen_socket[i].bindto; i++) { + if(mesh->listen_socket[i].sa.sa.sa_family != c->address.sa.sa_family) continue; if(s >= 0) return false; @@ -88,7 +85,7 @@ static bool bind_to_address(connection_t *c) { if(s < 0) return false; - sockaddr_t sa = listen_socket[s].sa; + sockaddr_t sa = mesh->listen_socket[s].sa; if(sa.sa.sa_family == AF_INET) sa.in.sin_port = 0; else if(sa.sa.sa_family == AF_INET6) diff --git a/src/node.c b/src/node.c index 52aa6d42..3068c9fb 100644 --- a/src/node.c +++ b/src/node.c @@ -113,8 +113,8 @@ void update_node_udp(node_t *n, const sockaddr_t *sa) { if(sa) { n->address = *sa; n->sock = 0; - for(int i = 0; i < listen_sockets; i++) { - if(listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) { + for(int i = 0; i < mesh->listen_sockets; i++) { + if(mesh->listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) { n->sock = i; break; }