Added comments and unfold deep "if"-construct in timeout_handler
[tinc] / src / edge.c
1 /*
2     edge.c -- edge tree management
3     Copyright (C) 2000-2013 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "splay_tree.h"
24 #include "control_common.h"
25 #include "edge.h"
26 #include "logger.h"
27 #include "netutl.h"
28 #include "node.h"
29 #include "utils.h"
30 #include "xalloc.h"
31
32 splay_tree_t *edge_weight_tree;
33
34 static int edge_compare(const edge_t *a, const edge_t *b) {
35         return strcmp(a->to->name, b->to->name);
36 }
37
38 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
39         int result;
40
41         result = a->weight - b->weight;
42
43         if(result)
44                 return result;
45
46         result = strcmp(a->from->name, b->from->name);
47
48         if(result)
49                 return result;
50
51         return strcmp(a->to->name, b->to->name);
52 }
53
54 void init_edges(void) {
55         edge_weight_tree = splay_alloc_tree((splay_compare_t) edge_weight_compare, NULL);
56 }
57
58 splay_tree_t *new_edge_tree(void) {
59         return splay_alloc_tree((splay_compare_t) edge_compare, (splay_action_t) free_edge);
60 }
61
62 void free_edge_tree(splay_tree_t *edge_tree) {
63         splay_delete_tree(edge_tree);
64 }
65
66 void exit_edges(void) {
67         splay_delete_tree(edge_weight_tree);
68 }
69
70 /* Creation and deletion of connection elements */
71
72 edge_t *new_edge(void) {
73         return xzalloc(sizeof(edge_t));
74 }
75
76 void free_edge(edge_t *e) {
77         sockaddrfree(&e->address);
78         sockaddrfree(&e->local_address);
79
80         free(e);
81 }
82
83 void edge_add(edge_t *e) {
84         splay_insert(edge_weight_tree, e);
85         splay_insert(e->from->edge_tree, e);
86
87         e->reverse = lookup_edge(e->to, e->from);
88
89         if(e->reverse)
90                 e->reverse->reverse = e;
91 }
92
93 void edge_del(edge_t *e) {
94         if(e->reverse)
95                 e->reverse->reverse = NULL;
96
97         splay_delete(edge_weight_tree, e);
98         splay_delete(e->from->edge_tree, e);
99 }
100
101 edge_t *lookup_edge(node_t *from, node_t *to) {
102         edge_t v;
103
104         v.from = from;
105         v.to = to;
106
107         return splay_search(from->edge_tree, &v);
108 }
109
110 bool dump_edges(connection_t *c) {
111         for splay_each(node_t, n, node_tree) {
112                 for splay_each(edge_t, e, n->edge_tree) {
113                         char *address = sockaddr2hostname(&e->address);
114                         char* local_address = sockaddr2hostname(&e->local_address);
115                         send_request(c, "%d %d %s %s %s %s %x %d",
116                                         CONTROL, REQ_DUMP_EDGES,
117                                         e->from->name, e->to->name, address,
118                                         local_address, e->options, e->weight);
119                         free(address);
120                         free(local_address);
121                 }
122         }
123
124         return send_request(c, "%d %d", CONTROL, REQ_DUMP_EDGES);
125 }