aee1be0fd7a5fa78c919446f9ce4630baf08b84f
[oweals/tinc.git] / src / edge.c
1 /*
2     edge.c -- edge tree management
3     Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000,2001 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.2.2 2001/10/28 10:16:18 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
39 #include "xalloc.h"
40 #include "system.h"
41
42 avl_tree_t *edge_tree;        /* Tree with all known edges (replaces active_tree) */
43 avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
44
45 int edge_compare(edge_t *a, edge_t *b)
46 {
47   int result;
48
49   result = strcmp(a->from->name, b->from->name);
50   
51   if(result)
52     return result;
53   else
54     return strcmp(a->to->name, b->to->name);
55 }
56
57 /* Evil edge_compare() from a parallel universe ;)
58
59 int edge_compare(edge_t *a, edge_t *b)
60 {
61   int result;
62
63   return (result = strcmp(a->from->name, b->from->name)) || (result = strcmp(a->to->name, b->to->name)), result;
64 }
65
66 */
67
68 int edge_weight_compare(edge_t *a, edge_t *b)
69 {
70   int result;
71   char *name_a1, *name_a2, *name_b1, *name_b2;
72   
73   
74   result = a->weight - b->weight;
75   
76   if(result)
77     return result;
78
79   if(strcmp(a->from->name, a->to->name) < 0)
80     name_a1 = a->from->name, name_a2 = a->to->name;
81   else
82     name_a1 = a->to->name, name_a2 = a->from->name;
83
84   if(strcmp(b->from->name, b->to->name) < 0)
85     name_b1 = b->from->name, name_b2 = b->to->name;
86   else
87     name_b1 = b->to->name, name_b2 = b->from->name;
88
89   result = strcmp(name_a1, name_b1);
90   
91   if(result)
92     return result;
93   else
94     return strcmp(name_a2, name_b2);
95 }
96
97 void init_edges(void)
98 {
99 cp
100   edge_tree = avl_alloc_tree((avl_compare_t)edge_compare, NULL);
101   edge_weight_tree = avl_alloc_tree((avl_compare_t)edge_weight_compare, NULL);
102 cp
103 }
104
105 void exit_edges(void)
106 {
107 cp
108   avl_delete_tree(edge_tree);
109 cp
110 }
111
112 /* Creation and deletion of connection elements */
113
114 edge_t *new_edge(void)
115 {
116 cp
117   edge_t *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 cp
135 }
136
137 void edge_del(edge_t *e)
138 {
139 cp
140   avl_delete(edge_tree, e);
141   avl_delete(edge_weight_tree, e);
142 cp
143 }
144
145 edge_t *lookup_edge(node_t *from, node_t *to)
146 {
147   edge_t v, *result;
148 cp
149   v.from = from;
150   v.to = to;
151
152   result = avl_search(edge_tree, &v);
153
154   if(result)
155     return result;
156 cp
157   v.from = to;
158   v.to = from;
159
160   return avl_search(edge_tree, &v);
161 }
162
163 void dump_edges(void)
164 {
165   avl_node_t *node;
166   edge_t *e;
167 cp
168   syslog(LOG_DEBUG, _("Edges:"));
169
170   for(node = edge_tree->head; node; node = node->next)
171     {
172       e = (edge_t *)node->data;
173       syslog(LOG_DEBUG, _(" %s - %s options %ld"),
174              e->from->name, e->to->name, e->options);
175     }
176     
177   syslog(LOG_DEBUG, _("End of edges."));
178 cp
179 }