Revert to edge and graph stuff. This time, use a directed graph.
[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.12 2002/09/04 13:48:51 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_tree;        /* Tree with all known edges (replaces active_tree) */
45 avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
46
47 int edge_compare(edge_t *a, edge_t *b)
48 {
49   int result;
50
51   result = strcmp(a->from->name, b->from->name);
52
53   if(result)
54     return result;
55   else
56     return strcmp(a->to->name, b->to->name);
57 }
58
59 /* Evil edge_compare() from a parallel universe ;)
60
61 int edge_compare(edge_t *a, edge_t *b)
62 {
63   int result;
64
65   return (result = strcmp(a->from->name, b->from->name)) || (result = strcmp(a->to->name, b->to->name)), result;
66 }
67
68 */
69
70 int edge_weight_compare(edge_t *a, edge_t *b)
71 {
72   int result;
73
74   result = a->weight - b->weight;
75
76   if(result)
77     return result;
78   else
79     return edge_compare(a, b);
80 }
81
82 void init_edges(void)
83 {
84 cp
85   edge_tree = avl_alloc_tree((avl_compare_t)edge_compare, NULL);
86   edge_weight_tree = avl_alloc_tree((avl_compare_t)edge_weight_compare, NULL);
87 cp
88 }
89
90 avl_tree_t *new_edge_tree(void)
91 {
92 cp
93   return avl_alloc_tree((avl_compare_t)edge_compare, NULL);
94 cp
95 }
96
97 void free_edge_tree(avl_tree_t *edge_tree)
98 {
99 cp
100   avl_delete_tree(edge_tree);
101 cp
102 }
103
104 void exit_edges(void)
105 {
106 cp
107   avl_delete_tree(edge_tree);
108 cp
109 }
110
111 /* Creation and deletion of connection elements */
112
113 edge_t *new_edge(void)
114 {
115   edge_t *e;
116 cp
117   e = (edge_t *)xmalloc_and_zero(sizeof(*e));
118 cp
119   return e;
120 }
121
122 void free_edge(edge_t *e)
123 {
124 cp
125   free(e);
126 cp
127 }
128
129 void edge_add(edge_t *e)
130 {
131 cp
132   avl_insert(edge_tree, e);
133   avl_insert(edge_weight_tree, e);
134   avl_insert(e->from->edge_tree, e);
135 cp
136   e->reverse = lookup_edge(e->to, e->from);
137   if(e->reverse)
138     e->reverse->reverse = e;
139 cp
140 }
141
142 void edge_del(edge_t *e)
143 {
144 cp
145   if(e->reverse)
146     e->reverse->reverse = NULL;
147 cp
148   avl_delete(edge_tree, e);
149   avl_delete(edge_weight_tree, e);
150   avl_delete(e->from->edge_tree, e);
151 cp
152 }
153
154 edge_t *lookup_edge(node_t *from, node_t *to)
155 {
156   edge_t v;
157 cp
158   v.from = from;
159   v.to = to;
160
161   return avl_search(edge_tree, &v);
162 }
163
164 void dump_edges(void)
165 {
166   avl_node_t *node;
167   edge_t *e;
168   char *address;
169 cp
170   syslog(LOG_DEBUG, _("Edges:"));
171
172   for(node = edge_tree->head; node; node = node->next)
173     {
174       e = (edge_t *)node->data;
175       address = sockaddr2hostname(&e->address);
176       syslog(LOG_DEBUG, _(" %s to %s at %s options %lx weight %d"),
177              e->from->name, e->to->name, address,
178              e->options, e->weight);
179       free(address);
180     }
181
182   syslog(LOG_DEBUG, _("End of edges."));
183 cp
184 }