/*
edge.c -- edge tree management
- Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
- 2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>
+ Copyright (C) 2000-2006 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
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.
-
- $Id: edge.c,v 1.1.2.2 2001/10/28 10:16:18 guus Exp $
+ 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 "config.h"
-
-#include <stdio.h>
-#include <syslog.h>
-#include <string.h>
-
-#include <avl_tree.h>
-#include <list.h>
+#include "system.h"
-#include "net.h" /* Don't ask. */
+#include "avl_tree.h"
+#include "edge.h"
+#include "logger.h"
#include "netutl.h"
-#include "config.h"
-#include "conf.h"
-#include <utils.h>
-#include "subnet.h"
-
+#include "node.h"
+#include "utils.h"
#include "xalloc.h"
-#include "system.h"
-avl_tree_t *edge_tree; /* Tree with all known edges (replaces active_tree) */
-avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
+avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
-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);
+static int edge_compare(const edge_t *a, const edge_t *b) {
+ return strcmp(a->to->name, b->to->name);
}
-/* Evil edge_compare() from a parallel universe ;)
+static int edge_weight_compare(const edge_t *a, const edge_t *b) {
+ int result;
+
+ result = a->weight - b->weight;
-int edge_compare(edge_t *a, edge_t *b)
-{
- int result;
+ if(result)
+ return result;
- return (result = strcmp(a->from->name, b->from->name)) || (result = strcmp(a->to->name, b->to->name)), result;
+ result = strcmp(a->from->name, b->from->name);
+
+ if(result)
+ return result;
+
+ return strcmp(a->to->name, b->to->name);
}
-*/
+void init_edges(void) {
+ edge_weight_tree = avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
+}
-int edge_weight_compare(edge_t *a, edge_t *b)
-{
- int result;
- char *name_a1, *name_a2, *name_b1, *name_b2;
-
-
- result = a->weight - b->weight;
-
- if(result)
- return result;
-
- if(strcmp(a->from->name, a->to->name) < 0)
- name_a1 = a->from->name, name_a2 = a->to->name;
- else
- name_a1 = a->to->name, name_a2 = a->from->name;
-
- if(strcmp(b->from->name, b->to->name) < 0)
- name_b1 = b->from->name, name_b2 = b->to->name;
- else
- name_b1 = b->to->name, name_b2 = b->from->name;
-
- result = strcmp(name_a1, name_b1);
-
- if(result)
- return result;
- else
- return strcmp(name_a2, name_b2);
+avl_tree_t *new_edge_tree(void) {
+ return avl_alloc_tree((avl_compare_t) edge_compare, (avl_action_t) free_edge);
}
-void init_edges(void)
-{
-cp
- edge_tree = avl_alloc_tree((avl_compare_t)edge_compare, NULL);
- edge_weight_tree = avl_alloc_tree((avl_compare_t)edge_weight_compare, NULL);
-cp
+void free_edge_tree(avl_tree_t *edge_tree) {
+ avl_delete_tree(edge_tree);
}
-void exit_edges(void)
-{
-cp
- avl_delete_tree(edge_tree);
-cp
+void exit_edges(void) {
+ avl_delete_tree(edge_weight_tree);
}
/* Creation and deletion of connection elements */
-edge_t *new_edge(void)
-{
-cp
- edge_t *e = (edge_t *)xmalloc_and_zero(sizeof(*e));
-cp
- return e;
+edge_t *new_edge(void) {
+ return xmalloc_and_zero(sizeof(edge_t));
}
-void free_edge(edge_t *e)
-{
-cp
- free(e);
-cp
-}
+void free_edge(edge_t *e) {
+ sockaddrfree(&e->address);
-void edge_add(edge_t *e)
-{
-cp
- avl_insert(edge_tree, e);
- avl_insert(edge_weight_tree, e);
-cp
+ free(e);
}
-void edge_del(edge_t *e)
-{
-cp
- avl_delete(edge_tree, e);
- avl_delete(edge_weight_tree, e);
-cp
+void edge_add(edge_t *e) {
+ avl_insert(edge_weight_tree, e);
+ avl_insert(e->from->edge_tree, e);
+
+ e->reverse = lookup_edge(e->to, e->from);
+
+ if(e->reverse)
+ e->reverse->reverse = e;
}
-edge_t *lookup_edge(node_t *from, node_t *to)
-{
- edge_t v, *result;
-cp
- v.from = from;
- v.to = to;
+void edge_del(edge_t *e) {
+ if(e->reverse)
+ e->reverse->reverse = NULL;
- result = avl_search(edge_tree, &v);
+ avl_delete(edge_weight_tree, e);
+ avl_delete(e->from->edge_tree, e);
+}
- if(result)
- return result;
-cp
- v.from = to;
- v.to = from;
+edge_t *lookup_edge(node_t *from, node_t *to) {
+ edge_t v;
+
+ v.from = from;
+ v.to = to;
- return avl_search(edge_tree, &v);
+ return avl_search(from->edge_tree, &v);
}
-void dump_edges(void)
-{
- avl_node_t *node;
- edge_t *e;
-cp
- syslog(LOG_DEBUG, _("Edges:"));
-
- for(node = edge_tree->head; node; node = node->next)
- {
- e = (edge_t *)node->data;
- syslog(LOG_DEBUG, _(" %s - %s options %ld"),
- e->from->name, e->to->name, e->options);
- }
-
- syslog(LOG_DEBUG, _("End of edges."));
-cp
+void dump_edges(void) {
+ avl_node_t *node, *node2;
+ node_t *n;
+ edge_t *e;
+ char *address;
+
+ logger(LOG_DEBUG, "Edges:");
+
+ for(node = node_tree->head; node; node = node->next) {
+ n = node->data;
+ for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
+ e = node2->data;
+ address = sockaddr2hostname(&e->address);
+ logger(LOG_DEBUG, " %s to %s at %s options %x weight %d",
+ e->from->name, e->to->name, address, e->options, e->weight);
+ free(address);
+ }
+ }
+
+ logger(LOG_DEBUG, "End of edges.");
}