GitHub CI: update list of container images
[tinc] / src / edge.c
index 0a47f8c..baa6d26 100644 (file)
@@ -1,7 +1,7 @@
 /*
     edge.c -- edge tree management
-    Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.eu.org>,
-                  2000-2002 Ivo Timmermans <ivo@o2w.nl>
+    Copyright (C) 2000-2021 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.18 2002/09/10 22:12:33 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 "netutl.h"
-#include "conf.h"
-#include <utils.h>
-#include "subnet.h"
+#include "splay_tree.h"
+#include "control_common.h"
 #include "edge.h"
+#include "logger.h"
+#include "netutl.h"
 #include "node.h"
-
 #include "xalloc.h"
-#include "system.h"
-
-avl_tree_t *edge_weight_tree;  /* Tree with all edges, sorted on weight */
 
-int edge_compare(edge_t *a, edge_t *b)
-{
-       return strcmp(a->to->name, b->to->name);
-}
-
-int edge_weight_compare(edge_t *a, edge_t *b)
-{
+static int edge_weight_compare(const edge_t *a, const edge_t *b) {
        int result;
 
        result = a->weight - b->weight;
 
-       if(result)
+       if(result) {
                return result;
+       }
 
        result = strcmp(a->from->name, b->from->name);
 
-       if(result)
+       if(result) {
                return result;
+       }
 
        return strcmp(a->to->name, b->to->name);
 }
 
-void init_edges(void)
-{
-       cp();
-
-       edge_weight_tree =
-               avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
-}
-
-avl_tree_t *new_edge_tree(void)
-{
-       cp();
+splay_tree_t edge_weight_tree = {
+       .compare = (splay_compare_t) edge_weight_compare,
+};
 
-       return avl_alloc_tree((avl_compare_t) edge_compare, NULL);
+static int edge_compare(const edge_t *a, const edge_t *b) {
+       return strcmp(a->to->name, b->to->name);
 }
 
-void free_edge_tree(avl_tree_t *edge_tree)
-{
-       cp();
-
-       avl_delete_tree(edge_tree);
+void init_edge_tree(splay_tree_t *tree) {
+       memset(tree, 0, sizeof(*tree));
+       tree->compare = (splay_compare_t) edge_compare;
+       tree->delete = (splay_action_t) free_edge;
 }
 
-void exit_edges(void)
-{
-       cp();
-
-       avl_delete_tree(edge_weight_tree);
+void exit_edges(void) {
+       splay_empty_tree(&edge_weight_tree);
 }
 
 /* Creation and deletion of connection elements */
 
-edge_t *new_edge(void)
-{
-       cp();
-
-       return (edge_t *) xmalloc_and_zero(sizeof(edge_t));
+edge_t *new_edge(void) {
+       return xzalloc(sizeof(edge_t));
 }
 
-void free_edge(edge_t *e)
-{
-       cp();
+void free_edge(edge_t *e) {
+       if(e) {
+               sockaddrfree(&e->address);
+               sockaddrfree(&e->local_address);
 
-       free(e);
+               free(e);
+       }
 }
 
-void edge_add(edge_t *e)
-{
-       cp();
+void edge_add(edge_t *e) {
+       splay_node_t *node = splay_insert(&e->from->edge_tree, e);
+
+       if(!node) {
+               logger(DEBUG_ALWAYS, LOG_ERR, "Edge from %s to %s already exists in edge_tree\n", e->from->name, e->to->name);
+               return;
+       }
 
-       avl_insert(edge_weight_tree, e);
-       avl_insert(e->from->edge_tree, e);
 
        e->reverse = lookup_edge(e->to, e->from);
 
-       if(e->reverse)
+       if(e->reverse) {
                e->reverse->reverse = e;
-}
+       }
 
-void edge_del(edge_t *e)
-{
-       cp();
+       node = splay_insert(&edge_weight_tree, e);
 
-       if(e->reverse)
+       if(!node) {
+               logger(DEBUG_ALWAYS, LOG_ERR, "Edge from %s to %s already exists in edge_weight_tree\n", e->from->name, e->to->name);
+               return;
+       }
+}
+
+void edge_del(edge_t *e) {
+       if(e->reverse) {
                e->reverse->reverse = NULL;
+       }
 
-       avl_delete(e->from->edge_tree, e);
-       avl_delete(edge_weight_tree, e);
+       splay_delete(&edge_weight_tree, e);
+       splay_delete(&e->from->edge_tree, e);
 }
 
-edge_t *lookup_edge(node_t *from, node_t *to)
-{
+edge_t *lookup_edge(node_t *from, node_t *to) {
        edge_t v;
 
-       cp();
-
        v.from = from;
        v.to = to;
 
-       return avl_search(from->edge_tree, &v);
+       return splay_search(&from->edge_tree, &v);
 }
 
-void dump_edges(void)
-{
-       avl_node_t *node, *node2;
-       node_t *n;
-       edge_t *e;
-       char *address;
-
-       cp();
-
-       syslog(LOG_DEBUG, _("Edges:"));
-
-       for(node = node_tree->head; node; node = node->next) {
-               n = (node_t *) node->data;
-               for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
-                       e = (edge_t *) node2->data;
-                       address = sockaddr2hostname(&e->address);
-                       syslog(LOG_DEBUG, _(" %s to %s at %s options %lx weight %d"),
-                                  e->from->name, e->to->name, address, e->options, e->weight);
+bool dump_edges(connection_t *c) {
+       for splay_each(node_t, n, &node_tree) {
+               for splay_each(edge_t, e, &n->edge_tree) {
+                       char *address = sockaddr2hostname(&e->address);
+                       char *local_address = sockaddr2hostname(&e->local_address);
+                       send_request(c, "%d %d %s %s %s %s %x %d",
+                                    CONTROL, REQ_DUMP_EDGES,
+                                    e->from->name, e->to->name, address,
+                                    local_address, e->options, e->weight);
                        free(address);
+                       free(local_address);
                }
        }
 
-       syslog(LOG_DEBUG, _("End of edges."));
+       return send_request(c, "%d %d", CONTROL, REQ_DUMP_EDGES);
 }