]> git.meshlink.io Git - meshlink/blob - src/node.c
a first mdns discovery implementation
[meshlink] / 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         pthread_mutex_lock(&(mesh->nodes_mutex));
38         mesh->nodes = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node);
39         mesh->node_udp_cache = hash_alloc(0x100, sizeof(sockaddr_t));
40         pthread_mutex_unlock(&(mesh->nodes_mutex));
41 }
42
43 void exit_nodes(meshlink_handle_t *mesh) {
44         pthread_mutex_lock(&(mesh->nodes_mutex));
45         if(mesh->node_udp_cache)
46                 hash_free(mesh->node_udp_cache);
47         if(mesh->nodes)
48                 splay_delete_tree(mesh->nodes);
49         mesh->node_udp_cache = NULL;
50         mesh->nodes = NULL;
51         pthread_mutex_unlock(&(mesh->nodes_mutex));
52 }
53
54 node_t *new_node(void) {
55         node_t *n = xzalloc(sizeof *n);
56
57         if(replaywin) n->late = xzalloc(replaywin);
58         n->edge_tree = new_edge_tree();
59         n->mtu = MTU;
60         n->maxmtu = MTU;
61
62         return n;
63 }
64
65 void free_node(node_t *n) {
66         if(n->edge_tree)
67                 free_edge_tree(n->edge_tree);
68
69         sockaddrfree(&n->address);
70
71         ecdsa_free(n->ecdsa);
72         sptps_stop(&n->sptps);
73
74         if(n->mtutimeout.cb)
75                 abort();
76
77         if(n->hostname)
78                 free(n->hostname);
79
80         if(n->name)
81                 free(n->name);
82
83         if(n->late)
84                 free(n->late);
85
86         free(n);
87 }
88
89 void node_add(meshlink_handle_t *mesh, node_t *n) {
90         pthread_mutex_lock(&(mesh->nodes_mutex));
91         n->mesh = mesh;
92         splay_insert(mesh->nodes, n);
93         pthread_mutex_unlock(&(mesh->nodes_mutex));
94 }
95
96 void node_del(meshlink_handle_t *mesh, node_t *n) {
97         pthread_mutex_lock(&(mesh->nodes_mutex));
98         timeout_del(&mesh->loop, &n->mtutimeout);
99
100         for splay_each(edge_t, e, n->edge_tree)
101                 edge_del(mesh, e);
102
103         splay_delete(mesh->nodes, n);
104         pthread_mutex_unlock(&(mesh->nodes_mutex));
105 }
106
107 node_t *lookup_node(meshlink_handle_t *mesh, const char *name) {
108         node_t n = {NULL};
109         node_t* result;
110
111         n.name = name;
112
113         pthread_mutex_lock(&(mesh->nodes_mutex));
114         result = splay_search(mesh->nodes, &n);
115         pthread_mutex_unlock(&(mesh->nodes_mutex));
116
117         return result;
118 }
119
120 node_t *lookup_node_udp(meshlink_handle_t *mesh, const sockaddr_t *sa) {
121         return hash_search(mesh->node_udp_cache, sa);
122 }
123
124 void update_node_udp(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
125         if(n == mesh->self) {
126                 logger(DEBUG_ALWAYS, LOG_WARNING, "Trying to update UDP address of mesh->self!");
127                 return;
128         }
129
130         hash_insert(mesh->node_udp_cache, &n->address, NULL);
131
132         if(sa) {
133                 n->address = *sa;
134                 n->sock = 0;
135                 for(int i = 0; i < mesh->listen_sockets; i++) {
136                         if(mesh->listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
137                                 n->sock = i;
138                                 break;
139                         }
140                 }
141                 hash_insert(mesh->node_udp_cache, sa, n);
142                 free(n->hostname);
143                 n->hostname = sockaddr2hostname(&n->address);
144                 logger(DEBUG_PROTOCOL, LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname);
145         }
146 }