]> git.meshlink.io Git - meshlink-tiny/blob - src/node.c
01173f8380617807aa5d8f279ff31cf8b4fb2aca
[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         utcp_exit(n->utcp);
55
56         ecdsa_free(n->ecdsa);
57         sptps_stop(&n->sptps);
58
59         free(n->name);
60         free(n->canonical_address);
61
62         free(n);
63 }
64
65 void node_add(meshlink_handle_t *mesh, node_t *n) {
66         if(n == mesh->self) {
67                 return;
68         }
69
70         assert(!mesh->peer);
71         n->mesh = mesh;
72         mesh->peer = n;
73 }
74
75 void node_del(meshlink_handle_t *mesh, node_t *n) {
76         if(n == mesh->self) {
77                 return;
78         }
79
80         assert(mesh->peer && mesh->peer == n);
81         free_node(n);
82         mesh->peer = NULL;
83 }
84
85 node_t *lookup_node(meshlink_handle_t *mesh, const char *name) {
86         if(mesh->peer && !strcmp(name, mesh->peer->name)) {
87                 return mesh->peer;
88         } else if(!strcmp(name, mesh->self->name)) {
89                 return mesh->self;
90         } else {
91                 return NULL;
92         }
93 }
94
95 bool node_add_recent_address(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
96         (void)mesh;
97         bool found = false;
98         int i;
99
100         /* Check if we already know this address */
101         for(i = 0; i < MAX_RECENT && n->recent[i].sa.sa_family; i++) {
102                 if(!sockaddrcmp(&n->recent[i], sa)) {
103                         found = true;
104                         break;
105                 }
106         }
107
108         if(found && i == 0) {
109                 /* It's already the most recent address, nothing to do. */
110                 return false;
111         }
112
113         if(i >= MAX_RECENT) {
114                 i = MAX_RECENT - 1;
115         }
116
117         memmove(n->recent + 1, n->recent, i * sizeof(*n->recent));
118         memcpy(n->recent, sa, SALEN(sa->sa));
119
120         n->status.dirty = true;
121         return !found;
122 }