X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fnet_socket.c;h=51e0caccfd19c1696cf5bf69c8c0aa4f7ed87189;hb=9cde0d32cf209388cc59b06b7dcb0c3432f97da5;hp=b5505500ebd18d7a60bdfd02895e6f7826ad24d9;hpb=282b99f3c71705bd6e34d4e4f9e8369ea63c87ed;p=meshlink diff --git a/src/net_socket.c b/src/net_socket.c index b5505500..51e0cacc 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -40,9 +40,7 @@ #define MSG_NOSIGNAL 0 #endif -int addressfamily = AF_UNSPEC; -int seconds_till_retry = 5; -int max_connection_burst = 100; +static const int max_connection_burst = 100; /* Setup sockets */ @@ -240,6 +238,8 @@ int setup_vpn_in_socket(meshlink_handle_t *mesh, const sockaddr_t *sa) { } /* int setup_vpn_in_socket */ static void retry_outgoing_handler(event_loop_t *loop, void *data) { + assert(data); + meshlink_handle_t *mesh = loop->data; outgoing_t *outgoing = data; setup_outgoing_connection(mesh, outgoing); @@ -579,6 +579,7 @@ begin: /* Now that there is a working socket, fill in the rest and register this connection. */ c->status.connecting = true; + c->status.initiator = true; c->name = xstrdup(outgoing->node->name); c->last_ping_time = mesh->loop.now.tv_sec; @@ -622,18 +623,19 @@ void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) { } /// Delayed close of a filedescriptor. -static void tarpit(int fd) { - static int pits[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; - static int next_pit = 0; +static void tarpit(meshlink_handle_t *mesh, int fd) { + if(!fd) { + return; + } - if(pits[next_pit] != -1) { - closesocket(pits[next_pit]); + if(mesh->pits[mesh->next_pit]) { + closesocket(mesh->pits[mesh->next_pit]); } - pits[next_pit++] = fd; + mesh->pits[mesh->next_pit++] = fd; - if(next_pit >= (int)(sizeof pits / sizeof pits[0])) { - next_pit = 0; + if(mesh->next_pit >= (int)(sizeof mesh->pits / sizeof mesh->pits[0])) { + mesh->next_pit = 0; } } @@ -668,20 +670,17 @@ void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) { /* Rate limit incoming connections to max_connection_burst/second. */ - static int connection_burst; - static int connection_burst_time; - - if(mesh->loop.now.tv_sec != connection_burst_time) { - connection_burst_time = mesh->loop.now.tv_sec; - connection_burst = 0; + if(mesh->loop.now.tv_sec != mesh->connection_burst_time) { + mesh->connection_burst_time = mesh->loop.now.tv_sec; + mesh->connection_burst = 0; } - if(connection_burst >= max_connection_burst) { - tarpit(fd); + if(mesh->connection_burst >= max_connection_burst) { + tarpit(mesh, fd); return; } - connection_burst++; + mesh->connection_burst++; // Accept the new connection