9f4abece64443335e59c111dca9fb68112082f5e
[tinc] / src / autoconnect.c
1 /*
2     autoconnect.c -- automatic connection establishment
3     Copyright (C) 2017 Guus Sliepen <guus@tinc-vpn.org>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include "connection.h"
23 #include "crypto.h"
24 #include "logger.h"
25 #include "node.h"
26 #include "xalloc.h"
27
28 static void make_new_connection() {
29         /* Select a random node we haven't connected to yet. */
30         uint32_t count = 0;
31
32         for splay_each(node_t, n, &node_tree) {
33                 if(n == myself || n->connection || !(n->status.has_address || n->status.reachable)) {
34                         continue;
35                 }
36
37                 count++;
38         }
39
40         if(!count) {
41                 return;
42         }
43
44         uint32_t r = prng(count);
45
46         for splay_each(node_t, n, &node_tree) {
47                 if(n == myself || n->connection || !(n->status.has_address || n->status.reachable)) {
48                         continue;
49                 }
50
51                 if(r) {
52                         --r;
53                         continue;
54                 }
55
56                 bool found = false;
57
58                 for list_each(outgoing_t, outgoing, &outgoing_list) {
59                         if(outgoing->node == n) {
60                                 found = true;
61                                 break;
62                         }
63                 }
64
65                 if(!found) {
66                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
67                         outgoing_t *outgoing = xzalloc(sizeof(*outgoing));
68                         outgoing->node = n;
69                         list_insert_tail(&outgoing_list, outgoing);
70                         setup_outgoing_connection(outgoing, false);
71                 }
72
73                 break;
74         }
75 }
76
77 static void connect_to_unreachable() {
78         /* Select a random known node. The rationale is that if there are many
79          * reachable nodes, and only a few unreachable nodes, we don't want all
80          * reachable nodes to try to connect to the unreachable ones at the
81          * same time. This way, we back off automatically. Conversely, if there
82          * are only a few reachable nodes, and many unreachable ones, we're
83          * going to try harder to connect to them. */
84
85         uint32_t r = prng(node_tree.count);
86
87         for splay_each(node_t, n, &node_tree) {
88                 if(r--) {
89                         continue;
90                 }
91
92                 /* Is it unreachable and do we know an address for it? If not, return. */
93                 if(n == myself || n->connection || n->status.reachable || !n->status.has_address) {
94                         return;
95                 }
96
97                 /* Are we already trying to make an outgoing connection to it? If so, return. */
98                 for list_each(outgoing_t, outgoing, &outgoing_list) {
99                         if(outgoing->node == n) {
100                                 return;
101                         }
102                 }
103
104                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
105                 outgoing_t *outgoing = xzalloc(sizeof(*outgoing));
106                 outgoing->node = n;
107                 list_insert_tail(&outgoing_list, outgoing);
108                 setup_outgoing_connection(outgoing, false);
109
110                 return;
111         }
112 }
113
114 static void drop_superfluous_outgoing_connection() {
115         /* Choose a random outgoing connection to a node that has at least one other connection. */
116         uint32_t count = 0;
117
118         for list_each(connection_t, c, &connection_list) {
119                 if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree.count < 2) {
120                         continue;
121                 }
122
123                 count++;
124         }
125
126         if(!count) {
127                 return;
128         }
129
130         uint32_t r = prng(count);
131
132         for list_each(connection_t, c, &connection_list) {
133                 if(!c->edge || !c->outgoing || !c->node || c->node->edge_tree.count < 2) {
134                         continue;
135                 }
136
137                 if(r--) {
138                         continue;
139                 }
140
141                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
142                 list_delete(&outgoing_list, c->outgoing);
143                 c->outgoing = NULL;
144                 terminate_connection(c, c->edge);
145                 break;
146         }
147 }
148
149 static void drop_superfluous_pending_connections() {
150         for list_each(outgoing_t, o, &outgoing_list) {
151                 /* Only look for connections that are waiting to be retried later. */
152                 bool found = false;
153
154                 for list_each(connection_t, c, &connection_list) {
155                         if(c->outgoing == o) {
156                                 found = true;
157                                 break;
158                         }
159                 }
160
161                 if(found) {
162                         continue;
163                 }
164
165                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->node->name);
166                 list_delete_node(&outgoing_list, node);
167         }
168 }
169
170 void do_autoconnect() {
171         /* Count number of active connections. */
172         uint32_t nc = 0;
173
174         for list_each(connection_t, c, &connection_list) {
175                 if(c->edge) {
176                         nc++;
177                 }
178         }
179
180         /* Less than 3 connections? Eagerly try to make a new one. */
181         if(nc < 3) {
182                 make_new_connection();
183                 return;
184         }
185
186         /* More than 3 connections? See if we can get rid of a superfluous one. */
187         if(nc > 3) {
188                 drop_superfluous_outgoing_connection();
189         }
190
191         /* Drop pending outgoing connections from the outgoing list. */
192         drop_superfluous_pending_connections();
193
194         /* Check if there are unreachable nodes that we should try to connect to. */
195         connect_to_unreachable();
196 }