tinc/src/edge.c

147 lines
2.8 KiB
C
Raw Normal View History

/*
2001-10-28 08:41:19 +00:00
edge.c -- edge tree management
Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
2001-10-28 08:41:19 +00:00
$Id: edge.c,v 1.1.2.1 2001/10/28 08:41:19 guus Exp $
*/
#include "config.h"
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <avl_tree.h>
#include <list.h>
#include "net.h" /* Don't ask. */
#include "netutl.h"
#include "config.h"
#include "conf.h"
#include <utils.h>
#include "subnet.h"
#include "xalloc.h"
#include "system.h"
2001-10-28 08:41:19 +00:00
avl_tree_t *edge_tree; /* Tree with all known vertices (replaces active_tree) */
2001-10-28 08:41:19 +00:00
int edge_compare(edge_t *a, edge_t *b)
{
int result;
result = strcmp(a->from->name, b->from->name);
if(result)
return result;
else
return strcmp(a->to->name, b->to->name);
}
2001-10-28 08:41:19 +00:00
/* Evil edge_compare() from a parallel universe ;)
2001-10-28 08:41:19 +00:00
int edge_compare(edge_t *a, edge_t *b)
{
int result;
return (result = strcmp(a->from->name, b->from->name)) || (result = strcmp(a->to->name, b->to->name)), result;
}
*/
void init_vertices(void)
{
cp
2001-10-28 08:41:19 +00:00
edge_tree = avl_alloc_tree((avl_compare_t)edge_compare, NULL);
cp
}
void exit_vertices(void)
{
cp
2001-10-28 08:41:19 +00:00
avl_delete_tree(edge_tree);
cp
}
/* Creation and deletion of connection elements */
2001-10-28 08:41:19 +00:00
edge_t *new_edge(void)
{
cp
2001-10-28 08:41:19 +00:00
edge_t *v = (edge_t *)xmalloc_and_zero(sizeof(*v));
cp
return v;
}
2001-10-28 08:41:19 +00:00
void free_edge(edge_t *v)
{
cp
free(v);
cp
}
2001-10-28 08:41:19 +00:00
void edge_add(edge_t *v)
{
cp
2001-10-28 08:41:19 +00:00
avl_insert(edge_tree, v);
cp
}
2001-10-28 08:41:19 +00:00
void edge_del(edge_t *v)
{
cp
2001-10-28 08:41:19 +00:00
avl_delete(edge_tree, v);
cp
}
2001-10-28 08:41:19 +00:00
edge_t *lookup_edge(node_t *from, node_t *to)
{
2001-10-28 08:41:19 +00:00
edge_t v, *result;
cp
v.from = from;
v.to = to;
2001-10-28 08:41:19 +00:00
result = avl_search(edge_tree, &v);
if(result)
return result;
cp
v.from = to;
v.to = from;
2001-10-28 08:41:19 +00:00
return avl_search(edge_tree, &v);
}
void dump_vertices(void)
{
avl_node_t *node;
2001-10-28 08:41:19 +00:00
edge_t *v;
cp
syslog(LOG_DEBUG, _("Vertices:"));
2001-10-28 08:41:19 +00:00
for(node = edge_tree->head; node; node = node->next)
{
2001-10-28 08:41:19 +00:00
v = (edge_t *)node->data;
syslog(LOG_DEBUG, _(" %s - %s options %ld"),
v->from->name, v->to->name, v->options);
}
syslog(LOG_DEBUG, _("End of vertices."));
cp
}