X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fnet.c;h=69462d5c56f1ee73e9796ab972d201f55957fd70;hb=f79cc0e0bba16a3aa42a5fa13098cda714623205;hp=03bac61763962183edb0886cbbfa1fe4bb7495e2;hpb=196806f90f40fcc0cf727abd4bed6bc5aefff5ff;p=meshlink diff --git a/src/net.c b/src/net.c index 03bac617..69462d5c 100644 --- a/src/net.c +++ b/src/net.c @@ -1,6 +1,6 @@ /* net.c -- most of the network code - Copyright (C) 2014 Guus Sliepen + Copyright (C) 2014-2017 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -34,7 +34,7 @@ #include #if !defined(min) -static const int min(int a, int b) { +static inline int min(int a, int b) { return a < b ? a : b; } #endif @@ -47,7 +47,7 @@ static const int min(int a, int b) { - Check if we need to retry making an outgoing connection */ void terminate_connection(meshlink_handle_t *mesh, connection_t *c, bool report) { - logger(mesh, MESHLINK_INFO, "Closing connection with %s (%s)", c->name, c->hostname); + logger(mesh, MESHLINK_INFO, "Closing connection with %s", c->name); c->status.active = false; @@ -67,7 +67,7 @@ void terminate_connection(meshlink_handle_t *mesh, connection_t *c, bool report) /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */ - if(report && !c->node->status.reachable) { + if(report && c->node && !c->node->status.reachable) { edge_t *e; e = lookup_edge(c->node, mesh->self); if(e) { @@ -113,7 +113,7 @@ static void timeout_handler(event_loop_t *loop, void *data) { if(c->last_ping_time + mesh->pingtimeout <= mesh->loop.now.tv_sec) { if(c->status.active) { if(c->status.pinged) - logger(mesh, MESHLINK_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, (long)mesh->loop.now.tv_sec - c->last_ping_time); + logger(mesh, MESHLINK_INFO, "%s didn't respond to PING in %ld seconds", c->name, (long)mesh->loop.now.tv_sec - c->last_ping_time); else if(c->last_ping_time + mesh->pinginterval <= mesh->loop.now.tv_sec) { send_ping(mesh, c); continue; @@ -121,9 +121,9 @@ static void timeout_handler(event_loop_t *loop, void *data) { continue; } else { if(c->status.connecting) - logger(mesh, MESHLINK_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname); + logger(mesh, MESHLINK_WARNING, "Timeout while connecting to %s", c->name); else - logger(mesh, MESHLINK_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname); + logger(mesh, MESHLINK_WARNING, "Timeout from %s during authentication", c->name); } terminate_connection(mesh, c, c->status.active); } @@ -338,13 +338,13 @@ static void periodic_handler(event_loop_t *loop, void *data) { // connect disconnect nodes - node_t* connect_to = NULL; - node_t* disconnect_from = NULL; + node_t *connect_to = NULL; + node_t *disconnect_from = NULL; // get cur_connects - int cur_connects = 0; + unsigned int cur_connects = 0; for list_each(connection_t, c, mesh->connections) { if(c->status.active) @@ -358,8 +358,8 @@ static void periodic_handler(event_loop_t *loop, void *data) { assert(mesh->devclass >= 0 && mesh->devclass <= _DEV_CLASS_MAX); - int min_connects = dev_class_traits[mesh->devclass].min_connects; - int max_connects = dev_class_traits[mesh->devclass].max_connects; + unsigned int min_connects = dev_class_traits[mesh->devclass].min_connects; + unsigned int max_connects = dev_class_traits[mesh->devclass].max_connects; logger(mesh, MESHLINK_DEBUG, "* min_connects = %d", min_connects); logger(mesh, MESHLINK_DEBUG, "* max_connects = %d", max_connects); @@ -380,7 +380,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { logger(mesh, MESHLINK_DEBUG, "* found best one for initial connect"); //timeout = 0; - connect_to = (node_t*)nodes->head->data; + connect_to = (node_t *)nodes->head->data; } else logger(mesh, MESHLINK_DEBUG, "* could not find node for initial connect"); @@ -393,7 +393,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { if(!connect_to && min_connects <= cur_connects && cur_connects < max_connects) { unsigned int connects = 0; - for(int devclass = 0; devclass <= mesh->devclass; ++devclass) { + for(unsigned int devclass = 0; devclass <= mesh->devclass; ++devclass) { for list_each(connection_t, c, mesh->connections) { if(c->status.active && c->node && c->node->devclass == devclass) connects += 1; @@ -409,7 +409,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { if(nodes->head) { logger(mesh, MESHLINK_DEBUG, "* found better node"); - connect_to = (node_t*)nodes->head->data; + connect_to = (node_t *)nodes->head->data; splay_free_tree(nodes); break; @@ -437,7 +437,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { if(nodes->head) { logger(mesh, MESHLINK_DEBUG, "* try to heal partition"); - connect_to = (node_t*)nodes->head->data; + connect_to = (node_t *)nodes->head->data; } else logger(mesh, MESHLINK_DEBUG, "* could not find nodes for partition healing"); @@ -476,7 +476,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { if(min_connects < cur_connects /*&& cur_connects <= max_connects*/) { unsigned int connects = 0; - for(int devclass = 0; devclass <= mesh->devclass; ++devclass) { + for(unsigned int devclass = 0; devclass <= mesh->devclass; ++devclass) { for list_each(connection_t, c, mesh->connections) { if(c->status.active && c->node && c->node->devclass == devclass) connects += 1; @@ -492,7 +492,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { if(nodes->head) { logger(mesh, MESHLINK_DEBUG, "* disconnect suboptimal outgoing connection"); - disconnect_from = (node_t*)nodes->head->data; + disconnect_from = (node_t *)nodes->head->data; } splay_free_tree(nodes); @@ -519,7 +519,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { logger(mesh, MESHLINK_DEBUG, "* disconnect connection (too many connections)"); //timeout = 0; - disconnect_from = (node_t*)nodes->head->data; + disconnect_from = (node_t *)nodes->head->data; } else logger(mesh, MESHLINK_DEBUG, "* no node we want to disconnect, even though we have too many connections"); @@ -589,7 +589,7 @@ int main_loop(meshlink_handle_t *mesh) { //Add signal handler mesh->datafromapp.signum = 0; - signal_add(&(mesh->loop),&(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue,mesh, mesh->datafromapp.signum); + signal_add(&(mesh->loop), &(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue, mesh, mesh->datafromapp.signum); if(!event_loop_run(&(mesh->loop), &(mesh->mesh_mutex))) { logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));