From fd7e81983ca4cae7cdf2a67a35500284f17761c4 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 4 Aug 2019 12:58:27 +0200 Subject: [PATCH] Remove global variables. There still were some global variables and buffers that were either unused or should be local to a MeshLink instance. --- src/devtools.c | 20 ++++++++------------ src/meshlink.c | 5 +++-- src/meshlink_internal.h | 9 ++++++++- src/net.h | 7 ------- src/net_packet.c | 12 ++++-------- src/net_setup.c | 4 +--- src/net_socket.c | 36 ++++++++++++++++-------------------- src/netutl.c | 7 ++----- src/netutl.h | 2 -- src/route.c | 2 -- src/route.h | 2 -- src/sptps.c | 4 +--- src/sptps.h | 1 - src/utils.c | 9 +++------ 14 files changed, 46 insertions(+), 74 deletions(-) diff --git a/src/devtools.c b/src/devtools.c index cd45e39f..629d90bd 100644 --- a/src/devtools.c +++ b/src/devtools.c @@ -115,16 +115,6 @@ static bool fstrwrite(const char *str, FILE *stream) { return true; } -static const char *__itoa(int value) { - static char buffer[sizeof(int) * 8 + 1]; // not thread safe - - if(snprintf(buffer, sizeof(buffer), "%d", value) == -1) { - return ""; - } - - return buffer; -} - bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) { bool result = true; @@ -151,6 +141,8 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) goto fail; } + char buf[16]; + for(size_t i = 0; i < node_count; ++i) { if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream)) { goto fail; @@ -160,7 +152,9 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) goto fail; } - if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->devclass), stream) || !fstrwrite("\n", stream)) { + snprintf(buf, sizeof(buf), "%d", ((node_t *)nodes[i])->devclass); + + if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) { goto fail; } @@ -209,7 +203,9 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) free(address); - if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(__itoa(edges[i].weight), stream) || !fstrwrite("\n", stream)) { + snprintf(buf, sizeof(buf), "%d", edges[i].weight); + + if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) { goto fail; } diff --git a/src/meshlink.c b/src/meshlink.c index c42eb769..7e2e20e5 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -744,8 +744,9 @@ static bool recvline(meshlink_handle_t *mesh, size_t len) { return true; } + static bool sendline(int fd, char *format, ...) { - static char buffer[4096]; + char buffer[4096]; char *p = buffer; int blen = 0; va_list ap; @@ -3112,7 +3113,7 @@ static void __attribute__((destructor)) meshlink_exit(void) { } /// Device class traits -dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX + 1] = { +const dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX + 1] = { { .min_connects = 3, .max_connects = 10000, .edge_weight = 1 }, // DEV_CLASS_BACKBONE { .min_connects = 3, .max_connects = 100, .edge_weight = 3 }, // DEV_CLASS_STATIONARY { .min_connects = 3, .max_connects = 3, .edge_weight = 6 }, // DEV_CLASS_PORTABLE diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 94e8b798..4056df31 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -110,15 +110,21 @@ struct meshlink_handle { struct splay_tree_t *past_request_tree; timeout_t past_request_timeout; + int connection_burst; int contradicting_add_edge; int contradicting_del_edge; int sleeptime; + time_t connection_burst_time; time_t last_config_check; + time_t last_hard_try; timeout_t pingtimer; timeout_t periodictimer; struct connection_t *everyone; + int next_pit; + int pits[10]; + // Infrequently used callbacks meshlink_node_status_cb_t node_status_cb; meshlink_channel_accept_cb_t channel_accept_cb; @@ -138,6 +144,7 @@ struct meshlink_handle { int pinginterval; /* seconds between pings */ int pingtimeout; /* seconds to wait for response */ int maxtimeout; + int udp_choice; int netns; @@ -226,6 +233,6 @@ typedef struct { int edge_weight; } dev_class_traits_t; -extern dev_class_traits_t dev_class_traits[]; +extern const dev_class_traits_t dev_class_traits[]; #endif diff --git a/src/net.h b/src/net.h index 6d27baf4..8ff07088 100644 --- a/src/net.h +++ b/src/net.h @@ -72,13 +72,6 @@ typedef struct outgoing_t { struct addrinfo *aip; } outgoing_t; -extern int maxoutbufsize; -extern int addressfamily; - -extern int keylifetime; -extern int max_connection_burst; -extern bool do_prune; - /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */ #include "connection.h" #include "node.h" diff --git a/src/net_packet.c b/src/net_packet.c index 36ad02bc..d1cbf47d 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -279,10 +279,8 @@ static void choose_udp_address(meshlink_handle_t *mesh, const node_t *n, const s to the node's reflexive UDP address discovered during key exchange. */ - static int x = 0; - - if(++x >= 3) { - x = 0; + if(++mesh->udp_choice >= 3) { + mesh->udp_choice = 0; return; } @@ -498,7 +496,6 @@ void broadcast_packet(meshlink_handle_t *mesh, const node_t *from, vpn_packet_t static node_t *try_harder(meshlink_handle_t *mesh, const sockaddr_t *from, const vpn_packet_t *pkt) { node_t *n = NULL; bool hard = false; - static time_t last_hard_try = 0; for splay_each(edge_t, e, mesh->edges) { if(!e->to->status.reachable || e->to == mesh->self) { @@ -506,7 +503,7 @@ static node_t *try_harder(meshlink_handle_t *mesh, const sockaddr_t *from, const } if(sockaddrcmp_noport(from, &e->address)) { - if(last_hard_try == mesh->loop.now.tv_sec) { + if(mesh->last_hard_try == mesh->loop.now.tv_sec) { continue; } @@ -522,10 +519,9 @@ static node_t *try_harder(meshlink_handle_t *mesh, const sockaddr_t *from, const } if(hard) { - last_hard_try = mesh->loop.now.tv_sec; + mesh->last_hard_try = mesh->loop.now.tv_sec; } - last_hard_try = mesh->loop.now.tv_sec; return n; } diff --git a/src/net_setup.c b/src/net_setup.c index f50f39cd..ccfaacce 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -321,7 +321,7 @@ static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bind struct addrinfo *ai; struct addrinfo hint = { - .ai_family = addressfamily, + .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM, .ai_protocol = IPPROTO_TCP, .ai_flags = AI_PASSIVE, @@ -395,7 +395,6 @@ static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bind bool setup_myself(meshlink_handle_t *mesh) { /* Set some defaults */ - keylifetime = 3600; // TODO: check if this can be removed as well mesh->maxtimeout = 900; /* Done */ @@ -456,7 +455,6 @@ bool setup_network(meshlink_handle_t *mesh) { mesh->pinginterval = 60; mesh->pingtimeout = 5; - maxoutbufsize = 10 * MTU; if(!setup_myself(mesh)) { return false; diff --git a/src/net_socket.c b/src/net_socket.c index b5505500..374ba205 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 */ @@ -622,18 +620,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 +667,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 diff --git a/src/netutl.c b/src/netutl.c index 38f4d487..f4ee5aa0 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -25,8 +25,6 @@ #include "utils.h" #include "xalloc.h" -bool hostnames = false; - /* Turn a string into a struct addrinfo. Return NULL on failure. @@ -36,7 +34,7 @@ struct addrinfo *str2addrinfo(const char *address, const char *service, int sock int err; struct addrinfo hint = { - .ai_family = addressfamily, + .ai_family = AF_UNSPEC, .ai_socktype = socktype, }; @@ -130,8 +128,7 @@ char *sockaddr2hostname(const sockaddr_t *sa) { return str; } - err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), - hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV)); + err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV); if(err) { logger(NULL, MESHLINK_ERROR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err)); diff --git a/src/netutl.h b/src/netutl.h index ce4c6a84..ebba1ac3 100644 --- a/src/netutl.h +++ b/src/netutl.h @@ -23,8 +23,6 @@ #include "net.h" #include "packmsg.h" -extern bool hostnames; - extern struct addrinfo *str2addrinfo(const char *, const char *, int) __attribute__((__malloc__)); extern sockaddr_t str2sockaddr(const char *, const char *); extern void sockaddr2str(const sockaddr_t *, char **, char **); diff --git a/src/route.c b/src/route.c index 9255712e..4de61026 100644 --- a/src/route.c +++ b/src/route.c @@ -25,8 +25,6 @@ #include "route.h" #include "utils.h" -bool decrement_ttl = false; - static bool checklength(node_t *source, vpn_packet_t *packet, uint16_t length) { if(packet->len < length) { logger(source->mesh, MESHLINK_WARNING, "Got too short packet from %s", source->name); diff --git a/src/route.h b/src/route.h index 16e3a97d..284fc69a 100644 --- a/src/route.h +++ b/src/route.h @@ -23,8 +23,6 @@ #include "net.h" #include "node.h" -extern bool decrement_ttl; - extern void route(struct meshlink_handle *mesh, struct node_t *, struct vpn_packet_t *); #endif diff --git a/src/sptps.c b/src/sptps.c index a2eb3883..9017656a 100644 --- a/src/sptps.c +++ b/src/sptps.c @@ -27,8 +27,6 @@ #include "prf.h" #include "sptps.h" -unsigned int sptps_replaywin = 32; - /* Nonce MUST be exchanged first (done) Signatures MUST be done over both nonces, to guarantee the signature is fresh @@ -670,7 +668,7 @@ bool sptps_start(sptps_t *s, void *handle, bool initiator, bool datagram, ecdsa_ s->datagram = datagram; s->mykey = mykey; s->hiskey = hiskey; - s->replaywin = sptps_replaywin; + s->replaywin = 32; if(s->replaywin) { s->late = malloc(s->replaywin); diff --git a/src/sptps.h b/src/sptps.h index d1e3607e..23066846 100644 --- a/src/sptps.h +++ b/src/sptps.h @@ -84,7 +84,6 @@ typedef struct sptps { } sptps_t; -extern unsigned int sptps_replaywin; extern void sptps_log_quiet(sptps_t *s, int s_errno, const char *format, va_list ap); extern void sptps_log_stderr(sptps_t *s, int s_errno, const char *format, va_list ap); extern void (*sptps_log)(sptps_t *s, int s_errno, const char *format, va_list ap); diff --git a/src/utils.c b/src/utils.c index 92505b4a..94052d3d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -178,19 +178,16 @@ int b64encode_urlsafe(const void *src, char *dst, int length) { #endif const char *winerror(int err) { - static char buf[1024], *ptr; + char buf[1024], *ptr; ptr = buf + sprintf(buf, "(%d) ", err); - if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) { + if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) { strncpy(buf, "(unable to format errormessage)", sizeof(buf)); } - ; - if((ptr = strchr(buf, '\r'))) { - * ptr = '\0'; + *ptr = '\0'; } return buf; -- 2.39.5