]> git.meshlink.io Git - meshlink/blob - src/edge.c
03f4cd11e97b3dcd018932b589a197527ea48be4
[meshlink] / src / edge.c
1 /*
2     edge.c -- edge tree management
3     Copyright (C) 2000-2013 Guus Sliepen <guus@meshlink.io>,
4                   2000-2005 Ivo Timmermans
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "splay_tree.h"
24 #include "edge.h"
25 #include "logger.h"
26 #include "netutl.h"
27 #include "node.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 splay_tree_t *edge_weight_tree;
32
33 static int edge_compare(const edge_t *a, const edge_t *b) {
34         return strcmp(a->to->name, b->to->name);
35 }
36
37 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
38         int result;
39
40         result = a->weight - b->weight;
41
42         if(result)
43                 return result;
44
45         result = strcmp(a->from->name, b->from->name);
46
47         if(result)
48                 return result;
49
50         return strcmp(a->to->name, b->to->name);
51 }
52
53 void init_edges(void) {
54         edge_weight_tree = splay_alloc_tree((splay_compare_t) edge_weight_compare, NULL);
55 }
56
57 splay_tree_t *new_edge_tree(void) {
58         return splay_alloc_tree((splay_compare_t) edge_compare, (splay_action_t) free_edge);
59 }
60
61 void free_edge_tree(splay_tree_t *edge_tree) {
62         splay_delete_tree(edge_tree);
63 }
64
65 void exit_edges(void) {
66         splay_delete_tree(edge_weight_tree);
67 }
68
69 /* Creation and deletion of connection elements */
70
71 edge_t *new_edge(void) {
72         return xzalloc(sizeof(edge_t));
73 }
74
75 void free_edge(edge_t *e) {
76         sockaddrfree(&e->address);
77
78         free(e);
79 }
80
81 void edge_add(edge_t *e) {
82         splay_insert(edge_weight_tree, e);
83         splay_insert(e->from->edge_tree, e);
84
85         e->reverse = lookup_edge(e->to, e->from);
86
87         if(e->reverse)
88                 e->reverse->reverse = e;
89 }
90
91 void edge_del(edge_t *e) {
92         if(e->reverse)
93                 e->reverse->reverse = NULL;
94
95         splay_delete(edge_weight_tree, e);
96         splay_delete(e->from->edge_tree, e);
97 }
98
99 edge_t *lookup_edge(node_t *from, node_t *to) {
100         edge_t v;
101
102         v.from = from;
103         v.to = to;
104
105         return splay_search(from->edge_tree, &v);
106 }