]> git.meshlink.io Git - meshlink/blob - src/node.c
Send a PING packet on a meta-connection if a UDP ping failed.
[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         }
45
46         if(mesh->nodes) {
47                 splay_delete_tree(mesh->nodes);
48         }
49
50         mesh->node_udp_cache = NULL;
51         mesh->nodes = NULL;
52 }
53
54 node_t *new_node(void) {
55         node_t *n = xzalloc(sizeof(*n));
56
57         n->edge_tree = new_edge_tree();
58         n->mtu = MTU;
59         n->maxmtu = MTU;
60         n->devclass = DEV_CLASS_UNKNOWN;
61
62         return n;
63 }
64
65 void free_node(node_t *n) {
66         n->status.destroyed = true;
67
68         utcp_exit(n->utcp);
69
70         if(n->edge_tree) {
71                 free_edge_tree(n->edge_tree);
72         }
73
74         sockaddrfree(&n->address);
75
76         ecdsa_free(n->ecdsa);
77         sptps_stop(&n->sptps);
78
79         free(n->name);
80         free(n->canonical_address);
81
82         free(n);
83 }
84
85 void node_add(meshlink_handle_t *mesh, node_t *n) {
86         n->mesh = mesh;
87         splay_insert(mesh->nodes, n);
88 }
89
90 void node_del(meshlink_handle_t *mesh, node_t *n) {
91         timeout_del(&mesh->loop, &n->udp_ping_timeout);
92
93         for splay_each(edge_t, e, n->edge_tree) {
94                 edge_del(mesh, e);
95         }
96
97         splay_delete(mesh->nodes, n);
98 }
99
100 node_t *lookup_node(meshlink_handle_t *mesh, const char *name) {
101         const node_t n = {.name = (char *)name};
102         node_t *result;
103
104         result = splay_search(mesh->nodes, &n);
105
106         return result;
107 }
108
109 node_t *lookup_node_udp(meshlink_handle_t *mesh, const sockaddr_t *sa) {
110         return hash_search(mesh->node_udp_cache, sa);
111 }
112
113 void update_node_udp(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
114         if(n == mesh->self) {
115                 logger(mesh, MESHLINK_WARNING, "Trying to update UDP address of mesh->self!");
116                 return;
117         }
118
119         hash_insert(mesh->node_udp_cache, &n->address, NULL);
120
121         if(sa) {
122                 n->address = *sa;
123                 n->sock = 0;
124
125                 for(int i = 0; i < mesh->listen_sockets; i++) {
126                         if(mesh->listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
127                                 n->sock = i;
128                                 break;
129                         }
130                 }
131
132                 hash_insert(mesh->node_udp_cache, sa, n);
133
134                 node_add_recent_address(mesh, n, sa);
135
136                 if(mesh->log_level <= MESHLINK_DEBUG) {
137                         char *hostname = sockaddr2hostname(&n->address);
138                         logger(mesh, MESHLINK_DEBUG, "UDP address of %s set to %s", n->name, hostname);
139                         free(hostname);
140                 }
141         }
142 }
143
144 bool node_add_recent_address(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
145         (void)mesh;
146         bool found = false;
147         int i;
148
149         /* Check if we already know this address */
150         for(i = 0; i < MAX_RECENT && n->recent[i].sa.sa_family; i++) {
151                 if(!sockaddrcmp(&n->recent[i], sa)) {
152                         found = true;
153                         break;
154                 }
155         }
156
157         if(found && i == 0) {
158                 /* It's already the most recent address, nothing to do. */
159                 return false;
160         }
161
162         if(i >= MAX_RECENT) {
163                 i = MAX_RECENT - 1;
164         }
165
166         memmove(n->recent + 1, n->recent, i * sizeof(*n->recent));
167         memcpy(n->recent, sa, SALEN(sa->sa));
168
169         n->status.dirty = true;
170         return !found;
171 }