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