]> git.meshlink.io Git - meshlink-tiny/blob - src/node.c
Remove graph, edges and communication via UDP.
[meshlink-tiny] / 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 }
39
40 void exit_nodes(meshlink_handle_t *mesh) {
41         if(mesh->nodes) {
42                 splay_delete_tree(mesh->nodes);
43         }
44
45         mesh->nodes = NULL;
46 }
47
48 node_t *new_node(void) {
49         node_t *n = xzalloc(sizeof(*n));
50
51         n->mtu = MTU;
52         n->maxmtu = MTU;
53         n->devclass = DEV_CLASS_UNKNOWN;
54
55         return n;
56 }
57
58 void free_node(node_t *n) {
59         n->status.destroyed = true;
60
61         utcp_exit(n->utcp);
62
63         sockaddrfree(&n->address);
64
65         ecdsa_free(n->ecdsa);
66         sptps_stop(&n->sptps);
67
68         if(n->mtutimeout.cb) {
69                 abort();
70         }
71
72         free(n->name);
73         free(n->canonical_address);
74
75         free(n);
76 }
77
78 void node_add(meshlink_handle_t *mesh, node_t *n) {
79         n->mesh = mesh;
80         splay_insert(mesh->nodes, n);
81 }
82
83 void node_del(meshlink_handle_t *mesh, node_t *n) {
84         timeout_del(&mesh->loop, &n->mtutimeout);
85         splay_delete(mesh->nodes, n);
86 }
87
88 node_t *lookup_node(meshlink_handle_t *mesh, const char *name) {
89         const node_t n = {.name = (char *)name};
90         node_t *result;
91
92         result = splay_search(mesh->nodes, &n);
93
94         return result;
95 }
96
97 bool node_add_recent_address(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
98         (void)mesh;
99         bool found = false;
100         int i;
101
102         /* Check if we already know this address */
103         for(i = 0; i < MAX_RECENT && n->recent[i].sa.sa_family; i++) {
104                 if(!sockaddrcmp(&n->recent[i], sa)) {
105                         found = true;
106                         break;
107                 }
108         }
109
110         if(found && i == 0) {
111                 /* It's already the most recent address, nothing to do. */
112                 return false;
113         }
114
115         if(i >= MAX_RECENT) {
116                 i = MAX_RECENT - 1;
117         }
118
119         memmove(n->recent + 1, n->recent, i * sizeof(*n->recent));
120         memcpy(n->recent, sa, SALEN(sa->sa));
121
122         n->status.dirty = true;
123         return !found;
124 }