2 autoconnect.c -- automatic connection establishment
3 Copyright (C) 2017-2022 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "autoconnect.h"
23 #include "connection.h"
29 static void make_new_connection(void) {
30 /* Select a random node we haven't connected to yet. */
33 for splay_each(node_t, n, &node_tree) {
34 if(n == myself || n->connection || !(n->status.has_address || n->status.reachable)) {
45 uint32_t r = prng(count);
47 for splay_each(node_t, n, &node_tree) {
48 if(n == myself || n->connection || !(n->status.has_address || n->status.reachable)) {
59 for list_each(outgoing_t, outgoing, &outgoing_list) {
60 if(outgoing->node == n) {
67 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
68 outgoing_t *outgoing = xzalloc(sizeof(*outgoing));
70 list_insert_tail(&outgoing_list, outgoing);
71 setup_outgoing_connection(outgoing, false);
78 static void connect_to_unreachable(void) {
79 /* Select a random known node. The rationale is that if there are many
80 * reachable nodes, and only a few unreachable nodes, we don't want all
81 * reachable nodes to try to connect to the unreachable ones at the
82 * same time. This way, we back off automatically. Conversely, if there
83 * are only a few reachable nodes, and many unreachable ones, we're
84 * going to try harder to connect to them. */
86 uint32_t r = prng(node_tree.count);
88 for splay_each(node_t, n, &node_tree) {
93 /* Is it unreachable and do we know an address for it? If not, return. */
94 if(n == myself || n->connection || n->status.reachable || !n->status.has_address) {
98 /* Are we already trying to make an outgoing connection to it? If so, return. */
99 for list_each(outgoing_t, outgoing, &outgoing_list) {
100 if(outgoing->node == n) {
105 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
106 outgoing_t *outgoing = xzalloc(sizeof(*outgoing));
108 list_insert_tail(&outgoing_list, outgoing);
109 setup_outgoing_connection(outgoing, false);
115 static void drop_superfluous_outgoing_connection(void) {
116 /* Choose a random outgoing connection to a node that has at least one other connection. */
119 for list_each(connection_t, c, &connection_list) {
120 if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree.count < 2) {
131 uint32_t r = prng(count);
133 for list_each(connection_t, c, &connection_list) {
134 if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree.count < 2) {
142 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
143 list_delete(&outgoing_list, c->outgoing);
145 terminate_connection(c, c->edge);
150 static void drop_superfluous_pending_connections(void) {
151 for list_each(outgoing_t, o, &outgoing_list) {
152 /* Only look for connections that are waiting to be retried later. */
155 for list_each(connection_t, c, &connection_list) {
156 if(c->outgoing == o) {
166 logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->node->name);
167 list_delete_node(&outgoing_list, node);
171 void do_autoconnect(void) {
172 /* Count number of active connections. */
175 for list_each(connection_t, c, &connection_list) {
181 /* Less than 3 connections? Eagerly try to make a new one. */
183 make_new_connection();
187 /* More than 3 connections? See if we can get rid of a superfluous one. */
189 drop_superfluous_outgoing_connection();
192 /* Drop pending outgoing connections from the outgoing list. */
193 drop_superfluous_pending_connections();
195 /* Check if there are unreachable nodes that we should try to connect to. */
196 connect_to_unreachable();