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