Fix reading broken BER in gcrypt/rsa.c
[tinc] / src / edge.c
1 /*
2     edge.c -- edge tree management
3     Copyright (C) 2000-2021 Guus Sliepen <guus@tinc-vpn.org>,
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 "control_common.h"
25 #include "edge.h"
26 #include "logger.h"
27 #include "netutl.h"
28 #include "node.h"
29 #include "xalloc.h"
30
31 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
32         int result;
33
34         result = a->weight - b->weight;
35
36         if(result) {
37                 return result;
38         }
39
40         result = strcmp(a->from->name, b->from->name);
41
42         if(result) {
43                 return result;
44         }
45
46         return strcmp(a->to->name, b->to->name);
47 }
48
49 splay_tree_t edge_weight_tree = {
50         .compare = (splay_compare_t) edge_weight_compare,
51 };
52
53 static int edge_compare(const edge_t *a, const edge_t *b) {
54         return strcmp(a->to->name, b->to->name);
55 }
56
57 void init_edge_tree(splay_tree_t *tree) {
58         memset(tree, 0, sizeof(*tree));
59         tree->compare = (splay_compare_t) edge_compare;
60         tree->delete = (splay_action_t) free_edge;
61 }
62
63 void exit_edges(void) {
64         splay_empty_tree(&edge_weight_tree);
65 }
66
67 /* Creation and deletion of connection elements */
68
69 edge_t *new_edge(void) {
70         return xzalloc(sizeof(edge_t));
71 }
72
73 void free_edge(edge_t *e) {
74         if(e) {
75                 sockaddrfree(&e->address);
76                 sockaddrfree(&e->local_address);
77
78                 free(e);
79         }
80 }
81
82 void edge_add(edge_t *e) {
83         splay_node_t *node = splay_insert(&e->from->edge_tree, e);
84
85         if(!node) {
86                 logger(DEBUG_ALWAYS, LOG_ERR, "Edge from %s to %s already exists in edge_tree\n", e->from->name, e->to->name);
87                 return;
88         }
89
90
91         e->reverse = lookup_edge(e->to, e->from);
92
93         if(e->reverse) {
94                 e->reverse->reverse = e;
95         }
96
97         node = splay_insert(&edge_weight_tree, e);
98
99         if(!node) {
100                 logger(DEBUG_ALWAYS, LOG_ERR, "Edge from %s to %s already exists in edge_weight_tree\n", e->from->name, e->to->name);
101                 return;
102         }
103 }
104
105 void edge_del(edge_t *e) {
106         if(e->reverse) {
107                 e->reverse->reverse = NULL;
108         }
109
110         splay_delete(&edge_weight_tree, e);
111         splay_delete(&e->from->edge_tree, e);
112 }
113
114 edge_t *lookup_edge(node_t *from, node_t *to) {
115         edge_t v;
116
117         v.from = from;
118         v.to = to;
119
120         return splay_search(&from->edge_tree, &v);
121 }
122
123 bool dump_edges(connection_t *c) {
124         for splay_each(node_t, n, &node_tree) {
125                 for splay_each(edge_t, e, &n->edge_tree) {
126                         char *address = sockaddr2hostname(&e->address);
127                         char *local_address = sockaddr2hostname(&e->local_address);
128                         send_request(c, "%d %d %s %s %s %s %x %d",
129                                      CONTROL, REQ_DUMP_EDGES,
130                                      e->from->name, e->to->name, address,
131                                      local_address, e->options, e->weight);
132                         free(address);
133                         free(local_address);
134                 }
135         }
136
137         return send_request(c, "%d %d", CONTROL, REQ_DUMP_EDGES);
138 }