]> git.meshlink.io Git - meshlink/blob - src/node.c
Remove everything GPL that is not copyright Guus Sliepen, update copyright statements.
[meshlink] / src / node.c
1 /*
2     node.c -- node tree management
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>,
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "hash.h"
23 #include "logger.h"
24 #include "net.h"
25 #include "netutl.h"
26 #include "node.h"
27 #include "splay_tree.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 splay_tree_t *node_tree;
32 static hash_t *node_udp_cache;
33
34 node_t *myself;
35
36 static int node_compare(const node_t *a, const node_t *b) {
37         return strcmp(a->name, b->name);
38 }
39
40 void init_nodes(void) {
41         node_tree = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node);
42         node_udp_cache = hash_alloc(0x100, sizeof(sockaddr_t));
43 }
44
45 void exit_nodes(void) {
46         hash_free(node_udp_cache);
47         splay_delete_tree(node_tree);
48 }
49
50 node_t *new_node(void) {
51         node_t *n = xzalloc(sizeof *n);
52
53         if(replaywin) n->late = xzalloc(replaywin);
54         n->edge_tree = new_edge_tree();
55         n->mtu = MTU;
56         n->maxmtu = MTU;
57
58         return n;
59 }
60
61 void free_node(node_t *n) {
62         if(n->edge_tree)
63                 free_edge_tree(n->edge_tree);
64
65         sockaddrfree(&n->address);
66
67         cipher_close(n->incipher);
68         digest_close(n->indigest);
69         cipher_close(n->outcipher);
70         digest_close(n->outdigest);
71
72         ecdsa_free(n->ecdsa);
73         sptps_stop(&n->sptps);
74
75         timeout_del(&n->mtutimeout);
76
77         if(n->hostname)
78                 free(n->hostname);
79
80         if(n->name)
81                 free(n->name);
82
83         if(n->late)
84                 free(n->late);
85
86         free(n);
87 }
88
89 void node_add(node_t *n) {
90         splay_insert(node_tree, n);
91 }
92
93 void node_del(node_t *n) {
94         for splay_each(edge_t, e, n->edge_tree)
95                 edge_del(e);
96
97         splay_delete(node_tree, n);
98 }
99
100 node_t *lookup_node(char *name) {
101         node_t n = {NULL};
102
103         n.name = name;
104
105         return splay_search(node_tree, &n);
106 }
107
108 node_t *lookup_node_udp(const sockaddr_t *sa) {
109         return hash_search(node_udp_cache, sa);
110 }
111
112 void update_node_udp(node_t *n, const sockaddr_t *sa) {
113         if(n == myself) {
114                 logger(DEBUG_ALWAYS, LOG_WARNING, "Trying to update UDP address of myself!");
115                 return;
116         }
117
118         hash_insert(node_udp_cache, &n->address, NULL);
119
120         if(sa) {
121                 n->address = *sa;
122                 n->sock = 0;
123                 for(int i = 0; i < listen_sockets; i++) {
124                         if(listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
125                                 n->sock = i;
126                                 break;
127                         }
128                 }
129                 hash_insert(node_udp_cache, sa, n);
130                 free(n->hostname);
131                 n->hostname = sockaddr2hostname(&n->address);
132                 logger(DEBUG_PROTOCOL, LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname);
133         }
134 }