2 net.c -- most of the network code
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2010 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "splay_tree.h"
27 #include "connection.h"
39 int contradicting_add_edge = 0;
40 int contradicting_del_edge = 0;
43 /* Purge edges and subnets of unreachable nodes. Use carefully. */
46 splay_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
51 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Purging unreachable nodes");
53 /* Remove all edges and subnets owned by unreachable nodes. */
55 for(nnode = node_tree->head; nnode; nnode = nnext) {
59 if(!n->status.reachable) {
60 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Purging node %s (%s)", n->name,
63 for(snode = n->subnet_tree->head; snode; snode = snext) {
66 send_del_subnet(broadcast, s);
71 for(enode = n->edge_tree->head; enode; enode = enext) {
75 send_del_edge(broadcast, e);
81 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
83 for(nnode = node_tree->head; nnode; nnode = nnext) {
87 if(!n->status.reachable) {
88 for(enode = edge_weight_tree->head; enode; enode = enext) {
96 if(!enode && (!strictsubnets || !n->subnet_tree->head))
97 /* in strictsubnets mode do not delete nodes with subnets */
104 Terminate a connection:
106 - Remove associated edge and tell other connections about it if report = true
107 - Check if we need to retry making an outgoing connection
108 - Deactivate the host
110 void terminate_connection(connection_t *c, bool report) {
111 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)",
112 c->name, c->hostname);
114 c->status.active = false;
117 c->node->connection = NULL;
120 closesocket(c->socket);
123 if(report && !tunnelserver)
124 send_del_edge(broadcast, c->edge);
128 /* Run MST and SSSP algorithms */
132 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
134 if(report && !c->node->status.reachable) {
136 e = lookup_edge(c->node, myself);
139 send_del_edge(broadcast, e);
145 /* Check if this was our outgoing connection */
148 retry_outgoing(c->outgoing);
154 Check if the other end is active.
155 If we have sent packets, but didn't receive any,
156 then possibly the other end is dead. We send a
157 PING request over the meta connection. If the other
158 end does not reply in time, we consider them dead
159 and close the connection.
161 static void timeout_handler(void *arg) {
162 event_t *event = arg;
163 splay_node_t *node, *next;
165 time_t now = time(NULL);
167 for(node = connection_tree->head; node; node = next) {
171 if(c->last_ping_time + pingtimeout < now) {
172 if(c->status.active) {
173 if(c->status.pinged) {
174 ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds",
175 c->name, c->hostname, now - c->last_ping_time);
176 terminate_connection(c, true);
178 } else if(c->last_ping_time + pinginterval < now) {
182 if(c->status.connecting) {
184 logger(LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
185 c->status.connecting = false;
186 closesocket(c->socket);
187 do_outgoing_connection(c);
189 ifdebug(CONNECTIONS) logger(LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
190 terminate_connection(c, false);
197 if(contradicting_del_edge && contradicting_add_edge) {
198 logger(LOG_WARNING, "Possible node with same Name as us!");
200 if(rand() % 3 == 0) {
201 logger(LOG_ERR, "Shutting down, check configuration of all nodes for duplicate Names!");
206 contradicting_add_edge = 0;
207 contradicting_del_edge = 0;
210 event->time = now + pingtimeout;
214 void handle_meta_connection_data(void *data) {
215 connection_t *c = data;
217 socklen_t len = sizeof result;
219 while(c->status.connecting) {
220 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
224 c->status.connecting = false;
225 finish_connecting(c);
226 mutex_unlock(&mutex);
228 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
229 "Error while connecting to %s (%s): %s",
230 c->name, c->hostname, sockstrerror(result));
231 closesocket(c->socket);
232 c->status.connecting = false;
234 do_outgoing_connection(c);
235 mutex_unlock(&mutex);
240 if (!receive_meta(c)) {
241 terminate_connection(c, c->status.active);
247 int reload_configuration(void) {
249 splay_node_t *node, *next;
252 static time_t last_config_check = 0;
254 /* Reread our own configuration file */
256 exit_configuration(&config_tree);
257 init_configuration(&config_tree);
259 if(!read_server_config()) {
260 logger(LOG_ERR, "Unable to reread configuration file, exitting.");
265 /* Close connections to hosts that have a changed or deleted host config file */
267 for(node = connection_tree->head; node; node = next) {
272 free(c->outgoing->name);
274 freeaddrinfo(c->outgoing->ai);
279 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
280 if(stat(fname, &s) || s.st_mtime > last_config_check)
281 terminate_connection(c, c->status.active);
285 last_config_check = time(NULL);
287 /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
293 for(node = subnet_tree->head; node; node = node->next) {
300 for(node = subnet_tree->head; node; node = next) {
303 if(subnet->expires == 1) {
304 send_del_subnet(broadcast, subnet);
305 if(subnet->owner->status.reachable)
306 subnet_update(subnet->owner, subnet, false);
307 subnet_del(subnet->owner, subnet);
308 } else if(subnet->expires == -1) {
311 send_add_subnet(broadcast, subnet);
312 if(subnet->owner->status.reachable)
313 subnet_update(subnet->owner, subnet, true);
318 /* Try to make outgoing connections */
320 try_outgoing_connections();
329 for(node = connection_tree->head; node; node = node->next) {
332 if(c->outgoing && !c->node) {
333 event_del(&c->outgoing->ev);
334 if(c->status.connecting)
336 c->outgoing->timeout = 0;
337 do_outgoing_connection(c);
343 this is where it all happens...
345 int main_loop(void) {
346 struct event timeout_event;
348 timeout_event.time = time(NULL) + pingtimeout;
349 timeout_event.handler = timeout_handler;
350 timeout_event.data = &timeout_event;
352 event_add(&timeout_event);
355 mutex_unlock(&mutex);
360 while((event = get_expired_event())) {
361 event->handler(event->data);
365 event_del(&timeout_event);