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