2 edge.c -- edge tree management
3 Copyright (C) 2000-2021 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.
23 #include "splay_tree.h"
24 #include "control_common.h"
32 splay_tree_t *edge_weight_tree;
34 static int edge_compare(const edge_t *a, const edge_t *b) {
35 return strcmp(a->to->name, b->to->name);
38 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
41 result = a->weight - b->weight;
47 result = strcmp(a->from->name, b->from->name);
53 return strcmp(a->to->name, b->to->name);
56 void init_edges(void) {
57 edge_weight_tree = splay_alloc_tree((splay_compare_t) edge_weight_compare, NULL);
60 splay_tree_t *new_edge_tree(void) {
61 return splay_alloc_tree((splay_compare_t) edge_compare, (splay_action_t) free_edge);
64 void free_edge_tree(splay_tree_t *edge_tree) {
65 splay_delete_tree(edge_tree);
68 void exit_edges(void) {
69 splay_delete_tree(edge_weight_tree);
72 /* Creation and deletion of connection elements */
74 edge_t *new_edge(void) {
75 return xzalloc(sizeof(edge_t));
78 void free_edge(edge_t *e) {
79 sockaddrfree(&e->address);
80 sockaddrfree(&e->local_address);
85 void edge_add(edge_t *e) {
86 splay_node_t *node = splay_insert(e->from->edge_tree, e);
89 logger(DEBUG_ALWAYS, LOG_ERR, "Edge from %s to %s already exists in edge_tree\n", e->from->name, e->to->name);
94 e->reverse = lookup_edge(e->to, e->from);
97 e->reverse->reverse = e;
100 node = splay_insert(edge_weight_tree, e);
103 logger(DEBUG_ALWAYS, LOG_ERR, "Edge from %s to %s already exists in edge_weight_tree\n", e->from->name, e->to->name);
108 void edge_del(edge_t *e) {
110 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) {
123 return splay_search(from->edge_tree, &v);
126 bool dump_edges(connection_t *c) {
127 for splay_each(node_t, n, node_tree) {
128 for splay_each(edge_t, e, n->edge_tree) {
129 char *address = sockaddr2hostname(&e->address);
130 char *local_address = sockaddr2hostname(&e->local_address);
131 send_request(c, "%d %d %s %s %s %s %x %d",
132 CONTROL, REQ_DUMP_EDGES,
133 e->from->name, e->to->name, address,
134 local_address, e->options, e->weight);
140 return send_request(c, "%d %d", CONTROL, REQ_DUMP_EDGES);