K&R style braces.
[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 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 "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         return strcmp(a->to->name, b->to->name);
35 }
36
37 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
38         int result;
39
40         result = a->weight - b->weight;
41
42         if(result)
43                 return result;
44
45         result = strcmp(a->from->name, b->from->name);
46
47         if(result)
48                 return result;
49
50         return strcmp(a->to->name, b->to->name);
51 }
52
53 void init_edges(void) {
54         cp();
55
56         edge_weight_tree = avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
57 }
58
59 avl_tree_t *new_edge_tree(void) {
60         cp();
61
62         return avl_alloc_tree((avl_compare_t) edge_compare, (avl_action_t) free_edge);
63 }
64
65 void free_edge_tree(avl_tree_t *edge_tree) {
66         cp();
67
68         avl_delete_tree(edge_tree);
69 }
70
71 void exit_edges(void) {
72         cp();
73
74         avl_delete_tree(edge_weight_tree);
75 }
76
77 /* Creation and deletion of connection elements */
78
79 edge_t *new_edge(void) {
80         cp();
81
82         return xmalloc_and_zero(sizeof(edge_t));
83 }
84
85 void free_edge(edge_t *e) {
86         cp();
87         
88         sockaddrfree(&e->address);
89
90         free(e);
91 }
92
93 void edge_add(edge_t *e) {
94         cp();
95
96         avl_insert(edge_weight_tree, e);
97         avl_insert(e->from->edge_tree, e);
98
99         e->reverse = lookup_edge(e->to, e->from);
100
101         if(e->reverse)
102                 e->reverse->reverse = e;
103 }
104
105 void edge_del(edge_t *e) {
106         cp();
107
108         if(e->reverse)
109                 e->reverse->reverse = NULL;
110
111         avl_delete(edge_weight_tree, e);
112         avl_delete(e->from->edge_tree, e);
113 }
114
115 edge_t *lookup_edge(node_t *from, node_t *to) {
116         edge_t v;
117         
118         cp();
119
120         v.from = from;
121         v.to = to;
122
123         return avl_search(from->edge_tree, &v);
124 }
125
126 void dump_edges(void) {
127         avl_node_t *node, *node2;
128         node_t *n;
129         edge_t *e;
130         char *address;
131
132         cp();
133
134         logger(LOG_DEBUG, _("Edges:"));
135
136         for(node = node_tree->head; node; node = node->next) {
137                 n = node->data;
138                 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
139                         e = node2->data;
140                         address = sockaddr2hostname(&e->address);
141                         logger(LOG_DEBUG, _(" %s to %s at %s options %lx weight %d"),
142                                    e->from->name, e->to->name, address, e->options, e->weight);
143                         free(address);
144                 }
145         }
146
147         logger(LOG_DEBUG, _("End of edges."));
148 }