2019-08-26 11:44:36 +00:00
|
|
|
/*
|
|
|
|
edge.c -- edge tree management
|
2019-08-26 11:44:37 +00:00
|
|
|
Copyright (C) 2000-2006 Guus Sliepen <guus@tinc-vpn.org>,
|
|
|
|
2000-2005 Ivo Timmermans
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2019-08-26 11:44:36 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "system.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "avl_tree.h"
|
|
|
|
#include "edge.h"
|
|
|
|
#include "logger.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "netutl.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "node.h"
|
|
|
|
#include "utils.h"
|
2019-08-26 11:44:36 +00:00
|
|
|
#include "xalloc.h"
|
|
|
|
|
2019-08-26 11:44:48 +00:00
|
|
|
avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
static int edge_compare(const edge_t *a, const edge_t *b) {
|
2019-08-26 11:44:36 +00:00
|
|
|
return strcmp(a->to->name, b->to->name);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
static int edge_weight_compare(const edge_t *a, const edge_t *b) {
|
2019-08-26 11:44:36 +00:00
|
|
|
int result;
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
result = a->weight - b->weight;
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:48 +00:00
|
|
|
if(result) {
|
2019-08-26 11:44:36 +00:00
|
|
|
return result;
|
2019-08-26 11:44:48 +00:00
|
|
|
}
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
result = strcmp(a->from->name, b->from->name);
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:48 +00:00
|
|
|
if(result) {
|
2019-08-26 11:44:36 +00:00
|
|
|
return result;
|
2019-08-26 11:44:48 +00:00
|
|
|
}
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
return strcmp(a->to->name, b->to->name);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
void init_edges(void) {
|
2019-08-26 11:44:36 +00:00
|
|
|
edge_weight_tree = avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
avl_tree_t *new_edge_tree(void) {
|
2019-08-26 11:44:36 +00:00
|
|
|
return avl_alloc_tree((avl_compare_t) edge_compare, (avl_action_t) free_edge);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
void free_edge_tree(avl_tree_t *edge_tree) {
|
2019-08-26 11:44:36 +00:00
|
|
|
avl_delete_tree(edge_tree);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
void exit_edges(void) {
|
2019-08-26 11:44:36 +00:00
|
|
|
avl_delete_tree(edge_weight_tree);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Creation and deletion of connection elements */
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
edge_t *new_edge(void) {
|
2019-08-26 11:44:36 +00:00
|
|
|
return xmalloc_and_zero(sizeof(edge_t));
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
void free_edge(edge_t *e) {
|
2019-08-26 11:44:36 +00:00
|
|
|
sockaddrfree(&e->address);
|
|
|
|
|
|
|
|
free(e);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
void edge_add(edge_t *e) {
|
2019-08-26 11:44:36 +00:00
|
|
|
avl_insert(edge_weight_tree, e);
|
|
|
|
avl_insert(e->from->edge_tree, e);
|
|
|
|
|
|
|
|
e->reverse = lookup_edge(e->to, e->from);
|
|
|
|
|
2019-08-26 11:44:48 +00:00
|
|
|
if(e->reverse) {
|
2019-08-26 11:44:36 +00:00
|
|
|
e->reverse->reverse = e;
|
2019-08-26 11:44:48 +00:00
|
|
|
}
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
void edge_del(edge_t *e) {
|
2019-08-26 11:44:48 +00:00
|
|
|
if(e->reverse) {
|
2019-08-26 11:44:36 +00:00
|
|
|
e->reverse->reverse = NULL;
|
2019-08-26 11:44:48 +00:00
|
|
|
}
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
avl_delete(edge_weight_tree, e);
|
|
|
|
avl_delete(e->from->edge_tree, e);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
edge_t *lookup_edge(node_t *from, node_t *to) {
|
2019-08-26 11:44:36 +00:00
|
|
|
edge_t v;
|
2019-08-26 11:44:48 +00:00
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
v.from = from;
|
|
|
|
v.to = to;
|
2019-08-26 11:44:36 +00:00
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
return avl_search(from->edge_tree, &v);
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
void dump_edges(void) {
|
2019-08-26 11:44:36 +00:00
|
|
|
avl_node_t *node, *node2;
|
|
|
|
node_t *n;
|
|
|
|
edge_t *e;
|
|
|
|
char *address;
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
logger(LOG_DEBUG, "Edges:");
|
2019-08-26 11:44:36 +00:00
|
|
|
|
|
|
|
for(node = node_tree->head; node; node = node->next) {
|
|
|
|
n = node->data;
|
2019-08-26 11:44:48 +00:00
|
|
|
|
2019-08-26 11:44:36 +00:00
|
|
|
for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
|
|
|
|
e = node2->data;
|
|
|
|
address = sockaddr2hostname(&e->address);
|
2019-08-26 11:44:38 +00:00
|
|
|
logger(LOG_DEBUG, " %s to %s at %s options %x weight %d",
|
2019-08-26 11:44:48 +00:00
|
|
|
e->from->name, e->to->name, address, e->options, e->weight);
|
2019-08-26 11:44:36 +00:00
|
|
|
free(address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:44:38 +00:00
|
|
|
logger(LOG_DEBUG, "End of edges.");
|
2019-08-26 11:44:36 +00:00
|
|
|
}
|