]> git.meshlink.io Git - meshlink/blob - src/node.c
Move node_tree to mesh->nodes.
[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 "meshlink_internal.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 static hash_t *node_udp_cache;
33
34 static int node_compare(const node_t *a, const node_t *b) {
35         return strcmp(a->name, b->name);
36 }
37
38 void init_nodes(void) {
39         mesh->nodes = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node);
40         node_udp_cache = hash_alloc(0x100, sizeof(sockaddr_t));
41 }
42
43 void exit_nodes(void) {
44         hash_free(node_udp_cache);
45         splay_delete_tree(mesh->nodes);
46 }
47
48 node_t *new_node(void) {
49         node_t *n = xzalloc(sizeof *n);
50
51         if(replaywin) n->late = xzalloc(replaywin);
52         n->edge_tree = new_edge_tree();
53         n->mtu = MTU;
54         n->maxmtu = MTU;
55
56         return n;
57 }
58
59 void free_node(node_t *n) {
60         if(n->edge_tree)
61                 free_edge_tree(n->edge_tree);
62
63         sockaddrfree(&n->address);
64
65         ecdsa_free(n->ecdsa);
66         sptps_stop(&n->sptps);
67
68         timeout_del(&n->mtutimeout);
69
70         if(n->hostname)
71                 free(n->hostname);
72
73         if(n->name)
74                 free(n->name);
75
76         if(n->late)
77                 free(n->late);
78
79         free(n);
80 }
81
82 void node_add(node_t *n) {
83         splay_insert(mesh->nodes, n);
84 }
85
86 void node_del(node_t *n) {
87         for splay_each(edge_t, e, n->edge_tree)
88                 edge_del(e);
89
90         splay_delete(mesh->nodes, n);
91 }
92
93 node_t *lookup_node(char *name) {
94         node_t n = {NULL};
95
96         n.name = name;
97
98         return splay_search(mesh->nodes, &n);
99 }
100
101 node_t *lookup_node_udp(const sockaddr_t *sa) {
102         return hash_search(node_udp_cache, sa);
103 }
104
105 void update_node_udp(node_t *n, const sockaddr_t *sa) {
106         if(n == mesh->self) {
107                 logger(DEBUG_ALWAYS, LOG_WARNING, "Trying to update UDP address of mesh->self!");
108                 return;
109         }
110
111         hash_insert(node_udp_cache, &n->address, NULL);
112
113         if(sa) {
114                 n->address = *sa;
115                 n->sock = 0;
116                 for(int i = 0; i < listen_sockets; i++) {
117                         if(listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
118                                 n->sock = i;
119                                 break;
120                         }
121                 }
122                 hash_insert(node_udp_cache, sa, n);
123                 free(n->hostname);
124                 n->hostname = sockaddr2hostname(&n->address);
125                 logger(DEBUG_PROTOCOL, LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname);
126         }
127 }