tinc/src/edge.c

123 lines
2.8 KiB
C
Raw Normal View History

/*
2001-10-28 08:41:19 +00:00
edge.c -- edge tree management
2012-10-14 15:42:49 +00:00
Copyright (C) 2000-2012 Guus Sliepen <guus@tinc-vpn.org>,
2000-2005 Ivo Timmermans
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.
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.
*/
#include "system.h"
2007-05-18 10:05:26 +00:00
#include "splay_tree.h"
#include "control_common.h"
#include "edge.h"
#include "logger.h"
#include "netutl.h"
#include "node.h"
#include "utils.h"
#include "xalloc.h"
2012-10-10 15:17:49 +00:00
splay_tree_t *edge_weight_tree;
2007-05-18 10:00:00 +00:00
static int edge_compare(const edge_t *a, const edge_t *b) {
2002-09-09 21:25:28 +00:00
return strcmp(a->to->name, b->to->name);
}
2007-05-18 10:00:00 +00:00
static int edge_weight_compare(const edge_t *a, const edge_t *b) {
2002-09-09 21:25:28 +00:00
int result;
2002-09-09 21:25:28 +00:00
result = a->weight - b->weight;
2002-09-09 21:25:28 +00:00
if(result)
return result;
2002-09-09 21:25:28 +00:00
result = strcmp(a->from->name, b->from->name);
2002-09-09 21:25:28 +00:00
if(result)
return result;
2002-09-09 21:25:28 +00:00
return strcmp(a->to->name, b->to->name);
}
2007-05-18 10:00:00 +00:00
void init_edges(void) {
2007-05-18 10:05:26 +00:00
edge_weight_tree = splay_alloc_tree((splay_compare_t) edge_weight_compare, NULL);
}
2007-05-18 10:05:26 +00:00
splay_tree_t *new_edge_tree(void) {
return splay_alloc_tree((splay_compare_t) edge_compare, (splay_action_t) free_edge);
}
2007-05-18 10:05:26 +00:00
void free_edge_tree(splay_tree_t *edge_tree) {
splay_delete_tree(edge_tree);
}
2007-05-18 10:00:00 +00:00
void exit_edges(void) {
2007-05-18 10:05:26 +00:00
splay_delete_tree(edge_weight_tree);
}
/* Creation and deletion of connection elements */
2007-05-18 10:00:00 +00:00
edge_t *new_edge(void) {
return xzalloc(sizeof(edge_t));
}
2007-05-18 10:00:00 +00:00
void free_edge(edge_t *e) {
sockaddrfree(&e->address);
2002-09-09 21:25:28 +00:00
free(e);
}
2007-05-18 10:00:00 +00:00
void edge_add(edge_t *e) {
2007-05-18 10:05:26 +00:00
splay_insert(edge_weight_tree, e);
splay_insert(e->from->edge_tree, e);
2002-09-09 21:25:28 +00:00
e->reverse = lookup_edge(e->to, e->from);
if(e->reverse)
e->reverse->reverse = e;
}
2007-05-18 10:00:00 +00:00
void edge_del(edge_t *e) {
2002-09-09 21:25:28 +00:00
if(e->reverse)
e->reverse->reverse = NULL;
2007-05-18 10:05:26 +00:00
splay_delete(edge_weight_tree, e);
splay_delete(e->from->edge_tree, e);
}
2007-05-18 10:00:00 +00:00
edge_t *lookup_edge(node_t *from, node_t *to) {
edge_t v;
2012-10-10 15:17:49 +00:00
v.from = from;
v.to = to;
2007-05-18 10:05:26 +00:00
return splay_search(from->edge_tree, &v);
}
bool dump_edges(connection_t *c) {
2012-10-07 22:35:38 +00:00
for splay_each(node_t, n, node_tree) {
for splay_each(edge_t, e, n->edge_tree) {
char *address = sockaddr2hostname(&e->address);
send_request(c, "%d %d %s %s %s %x %d",
CONTROL, REQ_DUMP_EDGES,
e->from->name, e->to->name, address,
e->options, e->weight);
2002-09-09 21:25:28 +00:00
free(address);
}
2002-09-06 10:23:52 +00:00
}
return send_request(c, "%d %d", CONTROL, REQ_DUMP_EDGES);
}