X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fautoconnect.c;h=5651f54d1a4d3aa396d33be4f5b8c1c13a109dc6;hb=0871c3095151bce6a4031a2662aa51b7193b855c;hp=8adb74f3a1156efe395c48c218df7c5958ca524a;hpb=f6e87ab476a0faf8b124ecaaa27f967d825e6457;p=tinc diff --git a/src/autoconnect.c b/src/autoconnect.c index 8adb74f3..5651f54d 100644 --- a/src/autoconnect.c +++ b/src/autoconnect.c @@ -53,8 +53,8 @@ static void make_new_connection() { bool found = false; - for list_each(outgoing_t, outgoing, outgoing_list) { - if(!strcmp(outgoing->name, n->name)) { + for list_each(outgoing_t, outgoing, &outgoing_list) { + if(outgoing->node == n) { found = true; break; } @@ -63,9 +63,9 @@ static void make_new_connection() { if(!found) { logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name); outgoing_t *outgoing = xzalloc(sizeof(*outgoing)); - outgoing->name = xstrdup(n->name); - list_insert_tail(outgoing_list, outgoing); - setup_outgoing_connection(outgoing); + outgoing->node = n; + list_insert_tail(&outgoing_list, outgoing); + setup_outgoing_connection(outgoing, false); } break; @@ -80,7 +80,7 @@ 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; + unsigned int r = rand() % node_tree->count; for splay_each(node_t, n, node_tree) { if(r--) { @@ -92,17 +92,18 @@ static void connect_to_unreachable() { return; } - /* Are we already trying to make an outgoing connection to it? If not, return. */ - for list_each(outgoing_t, outgoing, outgoing_list) - if(!strcmp(outgoing->name, n->name)) { + /* Are we already trying to make an outgoing connection to it? If so, return. */ + for list_each(outgoing_t, outgoing, &outgoing_list) { + if(outgoing->node == n) { return; } + } logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name); outgoing_t *outgoing = xzalloc(sizeof(*outgoing)); - outgoing->name = xstrdup(n->name); - list_insert_tail(outgoing_list, outgoing); - setup_outgoing_connection(outgoing); + outgoing->node = n; + list_insert_tail(&outgoing_list, outgoing); + setup_outgoing_connection(outgoing, false); return; } @@ -112,7 +113,7 @@ static void drop_superfluous_outgoing_connection() { /* Choose a random outgoing connection to a node that has at least one other connection. */ int count = 0; - for list_each(connection_t, c, connection_list) { + for list_each(connection_t, c, &connection_list) { if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree->count < 2) { continue; } @@ -126,7 +127,7 @@ static void drop_superfluous_outgoing_connection() { int r = rand() % count; - for list_each(connection_t, c, connection_list) { + for list_each(connection_t, c, &connection_list) { if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree->count < 2) { continue; } @@ -136,7 +137,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; @@ -144,11 +145,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; @@ -159,8 +160,8 @@ static void drop_superfluous_pending_connections() { continue; } - logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name); - list_delete_node(outgoing_list, node); + logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->node->name); + list_delete_node(&outgoing_list, node); } } @@ -168,7 +169,7 @@ void do_autoconnect() { /* Count number of active connections. */ int nc = 0; - for list_each(connection_t, c, connection_list) { + for list_each(connection_t, c, &connection_list) { if(c->edge) { nc++; } @@ -185,10 +186,9 @@ void do_autoconnect() { drop_superfluous_outgoing_connection(); } + /* Drop pending outgoing connections from the outgoing list. */ + drop_superfluous_pending_connections(); /* Check if there are unreachable nodes that we should try to connect to. */ connect_to_unreachable(); - - /* Drop pending outgoing connections from the outgoing list. */ - drop_superfluous_pending_connections(); }