2 node.c -- node tree management
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>,
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.
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.
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.
24 #include "meshlink_internal.h"
31 void init_nodes(meshlink_handle_t *mesh) {
35 void exit_nodes(meshlink_handle_t *mesh) {
37 free_node(mesh->peer);
43 node_t *new_node(void) {
44 node_t *n = xzalloc(sizeof(*n));
48 n->devclass = DEV_CLASS_UNKNOWN;
53 void free_node(node_t *n) {
54 n->status.destroyed = true;
59 sptps_stop(&n->sptps);
61 if(n->mtutimeout.cb) {
66 free(n->canonical_address);
71 void node_add(meshlink_handle_t *mesh, node_t *n) {
81 void node_del(meshlink_handle_t *mesh, node_t *n) {
86 assert(mesh->peer && mesh->peer == n);
87 timeout_del(&mesh->loop, &n->mtutimeout);
92 node_t *lookup_node(meshlink_handle_t *mesh, const char *name) {
93 if(mesh->peer && !strcmp(name, mesh->peer->name)) {
95 } else if(!strcmp(name, mesh->self->name)) {
102 bool node_add_recent_address(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
107 /* Check if we already know this address */
108 for(i = 0; i < MAX_RECENT && n->recent[i].sa.sa_family; i++) {
109 if(!sockaddrcmp(&n->recent[i], sa)) {
115 if(found && i == 0) {
116 /* It's already the most recent address, nothing to do. */
120 if(i >= MAX_RECENT) {
124 memmove(n->recent + 1, n->recent, i * sizeof(*n->recent));
125 memcpy(n->recent, sa, SALEN(sa->sa));
127 n->status.dirty = true;