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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
33 static int edge_compare(const edge_t *a, const edge_t *b) {
34 return strcmp(a->to->name, b->to->name);
37 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
40 result = a->weight - b->weight;
45 result = strcmp(a->from->name, b->from->name);
50 return strcmp(a->to->name, b->to->name);
53 void init_edges(void) {
54 edge_weight_tree = avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
57 avl_tree_t *new_edge_tree(void) {
58 return avl_alloc_tree((avl_compare_t) edge_compare, (avl_action_t) free_edge);
61 void free_edge_tree(avl_tree_t *edge_tree) {
62 avl_delete_tree(edge_tree);
65 void exit_edges(void) {
66 avl_delete_tree(edge_weight_tree);
69 /* Creation and deletion of connection elements */
71 edge_t *new_edge(void) {
72 return xmalloc_and_zero(sizeof(edge_t));
75 void free_edge(edge_t *e) {
76 sockaddrfree(&e->address);
81 void edge_add(edge_t *e) {
82 avl_insert(edge_weight_tree, e);
83 avl_insert(e->from->edge_tree, e);
85 e->reverse = lookup_edge(e->to, e->from);
88 e->reverse->reverse = e;
91 void edge_del(edge_t *e) {
93 e->reverse->reverse = NULL;
95 avl_delete(edge_weight_tree, e);
96 avl_delete(e->from->edge_tree, e);
99 edge_t *lookup_edge(node_t *from, node_t *to) {
105 return avl_search(from->edge_tree, &v);
108 void dump_edges(void) {
109 avl_node_t *node, *node2;
114 logger(LOG_DEBUG, "Edges:");
116 for(node = node_tree->head; node; node = node->next) {
118 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
120 address = sockaddr2hostname(&e->address);
121 logger(LOG_DEBUG, " %s to %s at %s options %x weight %d",
122 e->from->name, e->to->name, address, e->options, e->weight);
127 logger(LOG_DEBUG, "End of edges.");