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