0943dd00548b4ee42e60cdee8d1f82c2fde5acc6
[tinc] / src / edge.c
1 /*
2     edge.c -- edge tree management
3     Copyright (C) 2000-2006 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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "system.h"
22
23 #include "avl_tree.h"
24 #include "edge.h"
25 #include "logger.h"
26 #include "netutl.h"
27 #include "node.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 avl_tree_t *edge_weight_tree;   /* Tree with all edges, sorted on weight */
32
33 static int edge_compare(const edge_t *a, const edge_t *b)
34 {
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 {
40         int result;
41
42         result = a->weight - b->weight;
43
44         if(result)
45                 return result;
46
47         result = strcmp(a->from->name, b->from->name);
48
49         if(result)
50                 return result;
51
52         return strcmp(a->to->name, b->to->name);
53 }
54
55 void init_edges(void)
56 {
57         cp();
58
59         edge_weight_tree = avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
60 }
61
62 avl_tree_t *new_edge_tree(void)
63 {
64         cp();
65
66         return avl_alloc_tree((avl_compare_t) edge_compare, (avl_action_t) free_edge);
67 }
68
69 void free_edge_tree(avl_tree_t *edge_tree)
70 {
71         cp();
72
73         avl_delete_tree(edge_tree);
74 }
75
76 void exit_edges(void)
77 {
78         cp();
79
80         avl_delete_tree(edge_weight_tree);
81 }
82
83 /* Creation and deletion of connection elements */
84
85 edge_t *new_edge(void)
86 {
87         cp();
88
89         return xmalloc_and_zero(sizeof(edge_t));
90 }
91
92 void free_edge(edge_t *e)
93 {
94         cp();
95         
96         sockaddrfree(&e->address);
97
98         free(e);
99 }
100
101 void edge_add(edge_t *e)
102 {
103         cp();
104
105         avl_insert(edge_weight_tree, e);
106         avl_insert(e->from->edge_tree, e);
107
108         e->reverse = lookup_edge(e->to, e->from);
109
110         if(e->reverse)
111                 e->reverse->reverse = e;
112 }
113
114 void edge_del(edge_t *e)
115 {
116         cp();
117
118         if(e->reverse)
119                 e->reverse->reverse = NULL;
120
121         avl_delete(edge_weight_tree, e);
122         avl_delete(e->from->edge_tree, e);
123 }
124
125 edge_t *lookup_edge(node_t *from, node_t *to)
126 {
127         edge_t v;
128         
129         cp();
130
131         v.from = from;
132         v.to = to;
133
134         return avl_search(from->edge_tree, &v);
135 }
136
137 void dump_edges(void)
138 {
139         avl_node_t *node, *node2;
140         node_t *n;
141         edge_t *e;
142         char *address;
143
144         cp();
145
146         logger(LOG_DEBUG, _("Edges:"));
147
148         for(node = node_tree->head; node; node = node->next) {
149                 n = node->data;
150                 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
151                         e = node2->data;
152                         address = sockaddr2hostname(&e->address);
153                         logger(LOG_DEBUG, _(" %s to %s at %s options %lx weight %d"),
154                                    e->from->name, e->to->name, address, e->options, e->weight);
155                         free(address);
156                 }
157         }
158
159         logger(LOG_DEBUG, _("End of edges."));
160 }