Remove redundant spaces.
[tinc] / src / edge.c
1 /*
2     edge.c -- edge tree management
3     Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.eu.org>,
4                   2000-2002 Ivo Timmermans <ivo@o2w.nl>
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     $Id: edge.c,v 1.1.2.17 2002/09/09 22:32:30 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <syslog.h>
27 #include <string.h>
28
29 #include <avl_tree.h>
30 #include <list.h>
31
32 #include "net.h"                                /* Don't ask. */
33 #include "netutl.h"
34 #include "config.h"
35 #include "conf.h"
36 #include <utils.h>
37 #include "subnet.h"
38 #include "edge.h"
39 #include "node.h"
40
41 #include "xalloc.h"
42 #include "system.h"
43
44 avl_tree_t *edge_weight_tree;   /* Tree with all edges, sorted on weight */
45
46 int edge_compare(edge_t *a, edge_t *b)
47 {
48         return strcmp(a->to->name, b->to->name);
49 }
50
51 int edge_weight_compare(edge_t *a, edge_t *b)
52 {
53         int result;
54
55         result = a->weight - b->weight;
56
57         if(result)
58                 return result;
59
60         result = strcmp(a->from->name, b->from->name);
61
62         if(result)
63                 return result;
64
65         return strcmp(a->to->name, b->to->name);
66 }
67
68 void init_edges(void)
69 {
70         cp();
71
72         edge_weight_tree =
73                 avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
74 }
75
76 avl_tree_t *new_edge_tree(void)
77 {
78         cp();
79
80         return avl_alloc_tree((avl_compare_t) edge_compare, NULL);
81 }
82
83 void free_edge_tree(avl_tree_t *edge_tree)
84 {
85         cp();
86
87         avl_delete_tree(edge_tree);
88 }
89
90 void exit_edges(void)
91 {
92         cp();
93
94         avl_delete_tree(edge_weight_tree);
95 }
96
97 /* Creation and deletion of connection elements */
98
99 edge_t *new_edge(void)
100 {
101         cp();
102
103         return (edge_t *) xmalloc_and_zero(sizeof(edge_t));
104 }
105
106 void free_edge(edge_t *e)
107 {
108         cp();
109
110         free(e);
111 }
112
113 void edge_add(edge_t *e)
114 {
115         cp();
116
117         avl_insert(edge_weight_tree, e);
118         avl_insert(e->from->edge_tree, e);
119
120         e->reverse = lookup_edge(e->to, e->from);
121
122         if(e->reverse)
123                 e->reverse->reverse = e;
124 }
125
126 void edge_del(edge_t *e)
127 {
128         cp();
129
130         if(e->reverse)
131                 e->reverse->reverse = NULL;
132
133         avl_delete(e->from->edge_tree, e);
134         avl_delete(edge_weight_tree, e);
135 }
136
137 edge_t *lookup_edge(node_t *from, node_t *to)
138 {
139         edge_t v;
140
141         cp();
142
143         v.from = from;
144         v.to = to;
145
146         return avl_search(from->edge_tree, &v);
147 }
148
149 void dump_edges(void)
150 {
151         avl_node_t *node, *node2;
152         node_t *n;
153         edge_t *e;
154         char *address;
155
156         cp();
157
158         syslog(LOG_DEBUG, _("Edges:"));
159
160         for(node = node_tree->head; node; node = node->next) {
161                 n = (node_t *) node->data;
162                 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
163                         e = (edge_t *) node2->data;
164                         address = sockaddr2hostname(&e->address);
165                         syslog(LOG_DEBUG, _(" %s to %s at %s options %lx weight %d"),
166                                    e->from->name, e->to->name, address, e->options, e->weight);
167                         free(address);
168                 }
169         }
170
171         syslog(LOG_DEBUG, _("End of edges."));
172 }