Hm.
[tinc] / lib / edge.c
1 /*
2     edge.c -- edge tree management
3     Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000-2002 Ivo Timmermans <itimmermans@bigfoot.com>
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 2002/04/28 12:46:25 zarq Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <syslog.h>
28 #include <string.h>
29
30 #include <avl_tree.h>
31 #include <list.h>
32
33 #include "net.h"        /* Don't ask. */
34 #include "netutl.h"
35 #include "config.h"
36 #include "conf.h"
37 #include <utils.h>
38 #include "subnet.h"
39
40 #include "xalloc.h"
41 #include "system.h"
42
43 avl_tree_t *edge_tree;        /* Tree with all known edges (replaces active_tree) */
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   int result;
49
50   result = strcmp(a->from.node->name, b->from.node->name);
51
52   if(result)
53     return result;
54   else
55     return strcmp(a->to.node->name, b->to.node->name);
56 }
57
58 /* Evil edge_compare() from a parallel universe ;)
59
60 int edge_compare(edge_t *a, edge_t *b)
61 {
62   int result;
63
64   return (result = strcmp(a->from.node->name, b->from.node->name)) || (result = strcmp(a->to.node->name, b->to.node->name)), result;
65 }
66
67 */
68
69 int edge_name_compare(edge_t *a, edge_t *b)
70 {
71   int result;
72   char *name_a1, *name_a2, *name_b1, *name_b2;
73
74   if(strcmp(a->from.node->name, a->to.node->name) < 0)
75     name_a1 = a->from.node->name, name_a2 = a->to.node->name;
76   else
77     name_a1 = a->to.node->name, name_a2 = a->from.node->name;
78
79   if(strcmp(b->from.node->name, b->to.node->name) < 0)
80     name_b1 = b->from.node->name, name_b2 = b->to.node->name;
81   else
82     name_b1 = b->to.node->name, name_b2 = b->from.node->name;
83
84   result = strcmp(name_a1, name_b1);
85
86   if(result)
87     return result;
88   else
89     return strcmp(name_a2, name_b2);
90 }
91
92 int edge_weight_compare(edge_t *a, edge_t *b)
93 {
94   int result;
95
96   result = a->weight - b->weight;
97
98   if(result)
99     return result;
100   else
101     return edge_name_compare(a, b);
102 }
103
104 void init_edges(void)
105 {
106 cp
107   edge_tree = avl_alloc_tree((avl_compare_t)edge_compare, NULL);
108   edge_weight_tree = avl_alloc_tree((avl_compare_t)edge_weight_compare, NULL);
109 cp
110 }
111
112 avl_tree_t *new_edge_tree(void)
113 {
114 cp
115   return avl_alloc_tree((avl_compare_t)edge_name_compare, NULL);
116 cp
117 }
118
119 void free_edge_tree(avl_tree_t *edge_tree)
120 {
121 cp
122   avl_delete_tree(edge_tree);
123 cp
124 }
125
126 void exit_edges(void)
127 {
128 cp
129   avl_delete_tree(edge_tree);
130 cp
131 }
132
133 /* Creation and deletion of connection elements */
134
135 edge_t *new_edge(void)
136 {
137   edge_t *e;
138 cp
139   e = (edge_t *)xmalloc_and_zero(sizeof(*e));
140 cp
141   return e;
142 }
143
144 void free_edge(edge_t *e)
145 {
146 cp
147   free(e);
148 cp
149 }
150
151 void edge_add(edge_t *e)
152 {
153 cp
154   avl_insert(edge_tree, e);
155   avl_insert(edge_weight_tree, e);
156   avl_insert(e->from.node->edge_tree, e);
157   avl_insert(e->to.node->edge_tree, e);
158 cp
159 }
160
161 void edge_del(edge_t *e)
162 {
163 cp
164   avl_delete(edge_tree, e);
165   avl_delete(edge_weight_tree, e);
166   avl_delete(e->from.node->edge_tree, e);
167   avl_delete(e->to.node->edge_tree, e);
168 cp
169 }
170
171 edge_t *lookup_edge(node_t *from, node_t *to)
172 {
173   edge_t v, *result;
174 cp
175   v.from.node = from;
176   v.to.node = to;
177
178   result = avl_search(edge_tree, &v);
179
180   if(result)
181     return result;
182 cp
183   v.from.node = to;
184   v.to.node = from;
185
186   return avl_search(edge_tree, &v);
187 }
188
189 void dump_edges(void)
190 {
191   avl_node_t *node;
192   edge_t *e;
193   char *from_udp, *to_udp;
194 cp
195   syslog(LOG_DEBUG, _("Edges:"));
196
197   for(node = edge_tree->head; node; node = node->next)
198     {
199       e = (edge_t *)node->data;
200       from_udp = sockaddr2hostname(&e->from.udpaddress);
201       to_udp = sockaddr2hostname(&e->to.udpaddress);
202       syslog(LOG_DEBUG, _(" %s at %s - %s at %s options %lx weight %d"),
203              e->from.node->name, from_udp,
204              e->to.node->name, to_udp,
205              e->options, e->weight);
206       free(from_udp);
207       free(to_udp);     
208     }
209
210   syslog(LOG_DEBUG, _("End of edges."));
211 cp
212 }