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