]> git.meshlink.io Git - meshlink-tiny/blob - src/node.c
Add a metering test.
[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 "utils.h"
29 #include "xalloc.h"
30
31 void init_nodes(meshlink_handle_t *mesh) {
32         mesh->peer = NULL;
33 }
34
35 void exit_nodes(meshlink_handle_t *mesh) {
36         if(mesh->peer) {
37                 free_node(mesh->peer);
38         }
39
40         mesh->peer = NULL;
41 }
42
43 node_t *new_node(void) {
44         node_t *n = xzalloc(sizeof(*n));
45
46         n->devclass = DEV_CLASS_UNKNOWN;
47
48         return n;
49 }
50
51 void free_node(node_t *n) {
52         n->status.destroyed = true;
53
54         ecdsa_free(n->ecdsa);
55
56         free(n->name);
57         free(n->canonical_address);
58
59         free(n);
60 }
61
62 void node_add(meshlink_handle_t *mesh, node_t *n) {
63         if(n == mesh->self) {
64                 return;
65         }
66
67         assert(!mesh->peer);
68         n->mesh = mesh;
69         mesh->peer = n;
70 }
71
72 void node_del(meshlink_handle_t *mesh, node_t *n) {
73         if(n == mesh->self) {
74                 return;
75         }
76
77         assert(mesh->peer && mesh->peer == n);
78         free_node(n);
79         mesh->peer = NULL;
80 }
81
82 node_t *lookup_node(meshlink_handle_t *mesh, const char *name) {
83         if(mesh->peer && !strcmp(name, mesh->peer->name)) {
84                 return mesh->peer;
85         } else if(!strcmp(name, mesh->self->name)) {
86                 return mesh->self;
87         } else {
88                 return NULL;
89         }
90 }
91
92 bool node_add_recent_address(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
93         (void)mesh;
94         bool found = false;
95         int i;
96
97         /* Check if we already know this address */
98         for(i = 0; i < MAX_RECENT && n->recent[i].sa.sa_family; i++) {
99                 if(!sockaddrcmp(&n->recent[i], sa)) {
100                         found = true;
101                         break;
102                 }
103         }
104
105         if(found && i == 0) {
106                 /* It's already the most recent address, nothing to do. */
107                 return false;
108         }
109
110         if(i >= MAX_RECENT) {
111                 i = MAX_RECENT - 1;
112         }
113
114         memmove(n->recent + 1, n->recent, i * sizeof(*n->recent));
115         memcpy(n->recent, sa, SALEN(sa->sa));
116
117         n->status.dirty = true;
118         return !found;
119 }