]> git.meshlink.io Git - meshlink/blob - src/node.c
171b95fe78544753a43cf633a6a9106e8f9f8aef
[meshlink] / src / node.c
1 /*
2     node.c -- node tree management
3     Copyright (C) 2001-2013 Guus Sliepen <guus@meshlink.io>,
4                   2001-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 "hash.h"
24 #include "logger.h"
25 #include "net.h"
26 #include "netutl.h"
27 #include "node.h"
28 #include "splay_tree.h"
29 #include "utils.h"
30 #include "xalloc.h"
31
32 splay_tree_t *node_tree;
33 static hash_t *node_udp_cache;
34
35 node_t *myself;
36
37 static int node_compare(const node_t *a, const node_t *b) {
38         return strcmp(a->name, b->name);
39 }
40
41 void init_nodes(void) {
42         node_tree = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node);
43         node_udp_cache = hash_alloc(0x100, sizeof(sockaddr_t));
44 }
45
46 void exit_nodes(void) {
47         hash_free(node_udp_cache);
48         splay_delete_tree(node_tree);
49 }
50
51 node_t *new_node(void) {
52         node_t *n = xzalloc(sizeof *n);
53
54         if(replaywin) n->late = xzalloc(replaywin);
55         n->edge_tree = new_edge_tree();
56         n->mtu = MTU;
57         n->maxmtu = MTU;
58
59         return n;
60 }
61
62 void free_node(node_t *n) {
63         if(n->edge_tree)
64                 free_edge_tree(n->edge_tree);
65
66         sockaddrfree(&n->address);
67
68         cipher_close(n->incipher);
69         digest_close(n->indigest);
70         cipher_close(n->outcipher);
71         digest_close(n->outdigest);
72
73         ecdsa_free(n->ecdsa);
74         sptps_stop(&n->sptps);
75
76         timeout_del(&n->mtutimeout);
77
78         if(n->hostname)
79                 free(n->hostname);
80
81         if(n->name)
82                 free(n->name);
83
84         if(n->late)
85                 free(n->late);
86
87         free(n);
88 }
89
90 void node_add(node_t *n) {
91         splay_insert(node_tree, n);
92 }
93
94 void node_del(node_t *n) {
95         for splay_each(edge_t, e, n->edge_tree)
96                 edge_del(e);
97
98         splay_delete(node_tree, n);
99 }
100
101 node_t *lookup_node(char *name) {
102         node_t n = {NULL};
103
104         n.name = name;
105
106         return splay_search(node_tree, &n);
107 }
108
109 node_t *lookup_node_udp(const sockaddr_t *sa) {
110         return hash_search(node_udp_cache, sa);
111 }
112
113 void update_node_udp(node_t *n, const sockaddr_t *sa) {
114         if(n == myself) {
115                 logger(DEBUG_ALWAYS, LOG_WARNING, "Trying to update UDP address of myself!");
116                 return;
117         }
118
119         hash_insert(node_udp_cache, &n->address, NULL);
120
121         if(sa) {
122                 n->address = *sa;
123                 n->sock = 0;
124                 for(int i = 0; i < listen_sockets; i++) {
125                         if(listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
126                                 n->sock = i;
127                                 break;
128                         }
129                 }
130                 hash_insert(node_udp_cache, sa, n);
131                 free(n->hostname);
132                 n->hostname = sockaddr2hostname(&n->address);
133                 logger(DEBUG_PROTOCOL, LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname);
134         }
135 }