Various small fixes to make tinc runnable again.
[tinc] / src / vertex.c
1 /*
2     vertex.c -- vertex 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: vertex.c,v 1.1.2.3 2001/10/27 13:13:35 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 *vertex_tree;        /* Tree with all known vertices (replaces active_tree) */
43
44 int vertex_compare(vertex_t *a, vertex_t *b)
45 {
46   int result;
47
48   result = strcmp(a->from->name, b->from->name);
49   
50   if(result)
51     return result;
52   else
53     return strcmp(a->to->name, b->to->name);
54 }
55
56 /* Evil vertex_compare() from a parallel universe ;)
57
58 int vertex_compare(vertex_t *a, vertex_t *b)
59 {
60   int result;
61
62   return (result = strcmp(a->from->name, b->from->name)) || (result = strcmp(a->to->name, b->to->name)), result;
63 }
64
65 */
66
67 void init_vertices(void)
68 {
69 cp
70   vertex_tree = avl_alloc_tree((avl_compare_t)vertex_compare, NULL);
71 cp
72 }
73
74 void exit_vertices(void)
75 {
76 cp
77   avl_delete_tree(vertex_tree);
78 cp
79 }
80
81 /* Creation and deletion of connection elements */
82
83 vertex_t *new_vertex(void)
84 {
85 cp
86   vertex_t *v = (vertex_t *)xmalloc_and_zero(sizeof(*v));
87 cp
88   return v;
89 }
90
91 void free_vertex(vertex_t *v)
92 {
93 cp
94   free(v);
95 cp
96 }
97
98 void vertex_add(vertex_t *v)
99 {
100 cp
101   avl_insert(vertex_tree, v);
102 cp
103 }
104
105 void vertex_del(vertex_t *v)
106 {
107 cp
108   avl_delete(vertex_tree, v);
109 cp
110 }
111
112 vertex_t *lookup_vertex(node_t *from, node_t *to)
113 {
114   vertex_t v, *result;
115 cp
116   v.from = from;
117   v.to = to;
118
119   result = avl_search(vertex_tree, &v);
120
121   if(result)
122     return result;
123 cp
124   v.from = to;
125   v.to = from;
126
127   return avl_search(vertex_tree, &v);
128 }
129
130 void dump_vertices(void)
131 {
132   avl_node_t *node;
133   vertex_t *v;
134 cp
135   syslog(LOG_DEBUG, _("Vertices:"));
136
137   for(node = vertex_tree->head; node; node = node->next)
138     {
139       v = (vertex_t *)node->data;
140       syslog(LOG_DEBUG, _(" %s - %s options %ld"),
141              v->from->name, v->to->name, v->options);
142     }
143     
144   syslog(LOG_DEBUG, _("End of vertices."));
145 cp
146 }