X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fautoconnect.c;h=9f4abece64443335e59c111dca9fb68112082f5e;hb=e32d4f3db3f1ed3eebe2e11d57512b60a6c02fa6;hp=d25d65e30f528e0c237df8e4b70493bdf512e3c3;hpb=de7d5a03c2b956ab5753faf63b8148a279a71f29;p=tinc diff --git a/src/autoconnect.c b/src/autoconnect.c index d25d65e3..9f4abece 100644 --- a/src/autoconnect.c +++ b/src/autoconnect.c @@ -20,15 +20,16 @@ #include "system.h" #include "connection.h" +#include "crypto.h" #include "logger.h" #include "node.h" #include "xalloc.h" static void make_new_connection() { /* Select a random node we haven't connected to yet. */ - int count = 0; + uint32_t count = 0; - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(n == myself || n->connection || !(n->status.has_address || n->status.reachable)) { continue; } @@ -40,20 +41,21 @@ static void make_new_connection() { return; } - int r = rand() % count; + uint32_t r = prng(count); - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(n == myself || n->connection || !(n->status.has_address || n->status.reachable)) { continue; } - if(r--) { + if(r) { + --r; continue; } bool found = false; - for list_each(outgoing_t, outgoing, outgoing_list) { + for list_each(outgoing_t, outgoing, &outgoing_list) { if(outgoing->node == n) { found = true; break; @@ -64,7 +66,7 @@ static void make_new_connection() { logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name); outgoing_t *outgoing = xzalloc(sizeof(*outgoing)); outgoing->node = n; - list_insert_tail(outgoing_list, outgoing); + list_insert_tail(&outgoing_list, outgoing); setup_outgoing_connection(outgoing, false); } @@ -80,9 +82,9 @@ static void connect_to_unreachable() { * are only a few reachable nodes, and many unreachable ones, we're * going to try harder to connect to them. */ - int r = rand() % node_tree->count; + uint32_t r = prng(node_tree.count); - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(r--) { continue; } @@ -93,7 +95,7 @@ static void connect_to_unreachable() { } /* Are we already trying to make an outgoing connection to it? If so, return. */ - for list_each(outgoing_t, outgoing, outgoing_list) { + for list_each(outgoing_t, outgoing, &outgoing_list) { if(outgoing->node == n) { return; } @@ -102,7 +104,7 @@ static void connect_to_unreachable() { logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name); outgoing_t *outgoing = xzalloc(sizeof(*outgoing)); outgoing->node = n; - list_insert_tail(outgoing_list, outgoing); + list_insert_tail(&outgoing_list, outgoing); setup_outgoing_connection(outgoing, false); return; @@ -111,10 +113,10 @@ static void connect_to_unreachable() { static void drop_superfluous_outgoing_connection() { /* Choose a random outgoing connection to a node that has at least one other connection. */ - int count = 0; + uint32_t count = 0; - for list_each(connection_t, c, connection_list) { - if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree->count < 2) { + for list_each(connection_t, c, &connection_list) { + if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree.count < 2) { continue; } @@ -125,10 +127,10 @@ static void drop_superfluous_outgoing_connection() { return; } - int r = rand() % count; + uint32_t r = prng(count); - for list_each(connection_t, c, connection_list) { - if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree->count < 2) { + for list_each(connection_t, c, &connection_list) { + if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree.count < 2) { continue; } @@ -137,7 +139,7 @@ static void drop_superfluous_outgoing_connection() { } logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name); - list_delete(outgoing_list, c->outgoing); + list_delete(&outgoing_list, c->outgoing); c->outgoing = NULL; terminate_connection(c, c->edge); break; @@ -145,11 +147,11 @@ static void drop_superfluous_outgoing_connection() { } static void drop_superfluous_pending_connections() { - for list_each(outgoing_t, o, outgoing_list) { + for list_each(outgoing_t, o, &outgoing_list) { /* Only look for connections that are waiting to be retried later. */ bool found = false; - for list_each(connection_t, c, connection_list) { + for list_each(connection_t, c, &connection_list) { if(c->outgoing == o) { found = true; break; @@ -161,15 +163,15 @@ static void drop_superfluous_pending_connections() { } logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->node->name); - list_delete_node(outgoing_list, node); + list_delete_node(&outgoing_list, node); } } void do_autoconnect() { /* Count number of active connections. */ - int nc = 0; + uint32_t nc = 0; - for list_each(connection_t, c, connection_list) { + for list_each(connection_t, c, &connection_list) { if(c->edge) { nc++; }