From: Guus Sliepen Date: Sat, 26 Apr 2014 08:50:27 +0000 (+0200) Subject: Stop using global variable mesh in net.c. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=3268e9a2b9fea6779224fe1f3da48b7fc4b9cb46;p=meshlink Stop using global variable mesh in net.c. --- diff --git a/src/meshlink.c b/src/meshlink.c index 124b6e8f..8e94fa1e 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -255,7 +255,7 @@ void *meshlink_main_loop(void *arg) { try_outgoing_connections(mesh); - main_loop(); + main_loop(mesh); return NULL; } @@ -285,7 +285,7 @@ void meshlink_stop(meshlink_handle_t *mesh) { void meshlink_close(meshlink_handle_t *mesh) { // Close and free all resources used. - close_network_connections(); + close_network_connections(mesh); logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating"); diff --git a/src/net.c b/src/net.c index 61cb1538..1fe33342 100644 --- a/src/net.c +++ b/src/net.c @@ -31,36 +31,6 @@ #include "protocol.h" #include "xalloc.h" -/* Purge edges of unreachable nodes. Use carefully. */ - -// TODO: remove -void purge(void) { - logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes"); - - /* Remove all edges owned by unreachable nodes. */ - - for splay_each(node_t, n, mesh->nodes) { - if(!n->status.reachable) { - logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname); - - for splay_each(edge_t, e, n->edge_tree) { - send_del_edge(mesh->everyone, e); - edge_del(e); - } - } - } - - /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */ - - for splay_each(node_t, n, mesh->nodes) { - if(!n->status.reachable) { - for splay_each(edge_t, e, mesh->edges) - if(e->to == n) - return; - } - } -} - /* Terminate a connection: - Mark it as inactive @@ -68,7 +38,7 @@ void purge(void) { - Kill it with fire - Check if we need to retry making an outgoing connection */ -void terminate_connection(connection_t *c, bool report) { +void terminate_connection(meshlink_handle_t *mesh, connection_t *c, bool report) { logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname); c->status.active = false; @@ -123,6 +93,8 @@ void terminate_connection(connection_t *c, bool report) { and close the connection. */ static void timeout_handler(event_loop_t *loop, void *data) { + meshlink_handle_t *mesh = loop->data; + for list_each(connection_t, c, mesh->connections) { if(c->last_ping_time + mesh->pingtimeout <= mesh->loop.now.tv_sec) { if(c->status.active) { @@ -140,7 +112,7 @@ static void timeout_handler(event_loop_t *loop, void *data) { else logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname); } - terminate_connection(c, c->status.active); + terminate_connection(mesh, c, c->status.active); } } @@ -148,6 +120,8 @@ static void timeout_handler(event_loop_t *loop, void *data) { } static void periodic_handler(event_loop_t *loop, void *data) { + meshlink_handle_t *mesh = loop->data; + /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages. This usually only happens when another node has the same Name as this node. If so, sleep for a short while to prevent a storm of contradicting messages. @@ -233,7 +207,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name); list_delete(mesh->outgoings, c->outgoing); c->outgoing = NULL; - terminate_connection(c, c->status.active); + terminate_connection(mesh, c, c->status.active); break; } } @@ -261,14 +235,14 @@ static void periodic_handler(event_loop_t *loop, void *data) { timeout_set(&mesh->loop, data, &(struct timeval){5, rand() % 100000}); } -void handle_meta_connection_data(connection_t *c) { +void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) { if (!receive_meta(c)) { - terminate_connection(c, c->status.active); + terminate_connection(mesh, c, c->status.active); return; } } -void retry(void) { +void retry(meshlink_handle_t *mesh) { /* Reset the reconnection timers for all outgoing connections */ for list_each(outgoing_t, outgoing, mesh->outgoings) { outgoing->timeout = 0; @@ -289,7 +263,7 @@ void retry(void) { /* this is where it all happens... */ -int main_loop(void) { +int main_loop(meshlink_handle_t *mesh) { timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval){mesh->pingtimeout, rand() % 100000}); timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){mesh->pingtimeout, rand() % 100000}); diff --git a/src/net.h b/src/net.h index 843d2180..94349d94 100644 --- a/src/net.h +++ b/src/net.h @@ -97,17 +97,15 @@ extern char *get_name(struct meshlink_handle *mesh); extern bool setup_myself_reloadable(struct meshlink_handle *mesh); extern bool setup_network(struct meshlink_handle *mesh); extern void setup_outgoing_connection(struct meshlink_handle *mesh, struct outgoing_t *); -extern void try_outgoing_connections(struct meshlink_handle * mesh); -extern void close_network_connections(void); -extern int main_loop(void); -extern void terminate_connection(struct connection_t *, bool); +extern void try_outgoing_connections(struct meshlink_handle *mesh); +extern void close_network_connections(struct meshlink_handle *mesh); +extern int main_loop(struct meshlink_handle *mesh); +extern void terminate_connection(struct meshlink_handle *mesh, struct connection_t *, bool); extern bool node_read_ecdsa_public_key(struct meshlink_handle *mesh, struct node_t *); extern bool read_ecdsa_public_key(struct meshlink_handle *mesh, struct connection_t *); extern void send_mtu_probe(struct node_t *); -extern void handle_meta_connection_data(struct connection_t *); -extern void regenerate_key(void); -extern void purge(void); -extern void retry(void); +extern void handle_meta_connection_data(struct meshlink_handle *mesh, struct connection_t *); +extern void retry(struct meshlink_handle *mesh); #ifndef HAVE_MINGW #define closesocket(s) close(s) diff --git a/src/net_setup.c b/src/net_setup.c index 02e90aef..eae283d6 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -380,19 +380,19 @@ bool setup_network(meshlink_handle_t *mesh) { /* close all open network connections */ -void close_network_connections(void) { +void close_network_connections(meshlink_handle_t *mesh) { for(list_node_t *node = mesh->connections->head, *next; node; node = next) { next = node->next; connection_t *c = node->data; c->outgoing = NULL; - terminate_connection(c, false); + terminate_connection(mesh, c, false); } if(mesh->outgoings) list_delete_list(mesh->outgoings); if(mesh->self && mesh->self->connection) { - terminate_connection(mesh->self->connection, false); + terminate_connection(mesh, mesh->self->connection, false); free_connection(mesh->self->connection); } diff --git a/src/net_socket.c b/src/net_socket.c index 4ef7eb0c..a6adb2a6 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -319,7 +319,7 @@ static void handle_meta_write(meshlink_handle_t *mesh, connection_t *c) { 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); + terminate_connection(mesh, c, c->status.active); return; } @@ -343,7 +343,7 @@ static void handle_meta_io(event_loop_t *loop, void *data, int flags) { finish_connecting(mesh, c); else { logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): %s", c->name, c->hostname, sockstrerror(result)); - terminate_connection(c, false); + terminate_connection(mesh, c, false); return; } } @@ -351,7 +351,7 @@ static void handle_meta_io(event_loop_t *loop, void *data, int flags) { if(flags & IO_WRITE) handle_meta_write(mesh, c); else - handle_meta_connection_data(c); + handle_meta_connection_data(mesh, c); } bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) { @@ -690,7 +690,7 @@ void try_outgoing_connections(meshlink_handle_t *mesh) { if(c->outgoing && c->outgoing->timeout == -1) { c->outgoing = NULL; logger(DEBUG_CONNECTIONS, LOG_INFO, "No more outgoing connection to %s", c->name); - terminate_connection(c, c->status.active); + terminate_connection(mesh, c, c->status.active); } } diff --git a/src/protocol_auth.c b/src/protocol_auth.c index 107280c1..8fad8cc0 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -412,7 +412,7 @@ bool ack_h(connection_t *c, const char *request) { n->connection->outgoing = NULL; } - terminate_connection(n->connection, false); + terminate_connection(mesh, n->connection, false); /* Run graph algorithm to keep things in sync */ graph(); }