2 edge.c -- edge tree management
3 Copyright (C) 2000-2006 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
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.
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.
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.
25 #include "splay_tree.h"
33 splay_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
35 static int edge_compare(const edge_t *a, const edge_t *b) {
36 return strcmp(a->to->name, b->to->name);
39 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
42 result = a->weight - b->weight;
47 result = strcmp(a->from->name, b->from->name);
52 return strcmp(a->to->name, b->to->name);
55 void init_edges(void) {
58 edge_weight_tree = splay_alloc_tree((splay_compare_t) edge_weight_compare, NULL);
61 splay_tree_t *new_edge_tree(void) {
64 return splay_alloc_tree((splay_compare_t) edge_compare, (splay_action_t) free_edge);
67 void free_edge_tree(splay_tree_t *edge_tree) {
70 splay_delete_tree(edge_tree);
73 void exit_edges(void) {
76 splay_delete_tree(edge_weight_tree);
79 /* Creation and deletion of connection elements */
81 edge_t *new_edge(void) {
84 return xmalloc_and_zero(sizeof(edge_t));
87 void free_edge(edge_t *e) {
90 sockaddrfree(&e->address);
95 void edge_add(edge_t *e) {
98 splay_insert(edge_weight_tree, e);
99 splay_insert(e->from->edge_tree, e);
101 e->reverse = lookup_edge(e->to, e->from);
104 e->reverse->reverse = e;
107 void edge_del(edge_t *e) {
111 e->reverse->reverse = NULL;
113 splay_delete(edge_weight_tree, e);
114 splay_delete(e->from->edge_tree, e);
117 edge_t *lookup_edge(node_t *from, node_t *to) {
125 return splay_search(from->edge_tree, &v);
128 int dump_edges(struct evbuffer *out) {
129 splay_node_t *node, *node2;
136 for(node = node_tree->head; node; node = node->next) {
138 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
140 address = sockaddr2hostname(&e->address);
141 if(evbuffer_add_printf(out,
142 _(" %s to %s at %s options %lx weight %d\n"),
143 e->from->name, e->to->name, address,
144 e->options, e->weight) == -1) {