]> git.meshlink.io Git - meshlink/blob - src/devtools.c
Add a probe point for SPTPS renewal and devtool_force_sptps_renewal().
[meshlink] / src / devtools.c
1 /*
2     devtools.c -- Debugging and quality control functions.
3     Copyright (C) 2014, 2017 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 #include <assert.h>
22
23 #include "logger.h"
24 #include "meshlink_internal.h"
25 #include "node.h"
26 #include "submesh.h"
27 #include "splay_tree.h"
28 #include "netutl.h"
29 #include "xalloc.h"
30
31 #include "devtools.h"
32
33 static void nop_probe(void) {
34         return;
35 }
36
37 static void keyrotate_nop_probe(int stage) {
38         (void)stage;
39         return;
40 }
41
42 static void inviter_commits_first_nop_probe(bool stage) {
43         (void)stage;
44         return;
45 }
46
47 static void sptps_renewal_nop_probe(meshlink_node_t *node) {
48         (void)node;
49         return;
50 }
51
52 void (*devtool_trybind_probe)(void) = nop_probe;
53 void (*devtool_keyrotate_probe)(int stage) = keyrotate_nop_probe;
54 void (*devtool_set_inviter_commits_first)(bool inviter_commited_first) = inviter_commits_first_nop_probe;
55 void (*devtool_adns_resolve_probe)(void) = nop_probe;
56 void (*devtool_sptps_renewal_probe)(meshlink_node_t *node) = sptps_renewal_nop_probe;
57
58 /* Return an array of edges in the current network graph.
59  * Data captures the current state and will not be updated.
60  * Caller must deallocate data when done.
61  */
62 devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *edges, size_t *nmemb) {
63         if(!mesh || !nmemb || (*nmemb && !edges)) {
64                 meshlink_errno = MESHLINK_EINVAL;
65                 return NULL;
66         }
67
68         pthread_mutex_lock(&mesh->mutex);
69
70         devtool_edge_t *result = NULL;
71         unsigned int result_size = 0;
72
73         result_size = mesh->edges->count / 2;
74
75         // if result is smaller than edges, we have to dealloc all the excess devtool_edge_t
76         if((size_t)result_size > *nmemb) {
77                 result = xrealloc(edges, result_size * sizeof(*result));
78         } else {
79                 result = edges;
80         }
81
82         if(result) {
83                 devtool_edge_t *p = result;
84                 unsigned int n = 0;
85
86                 for splay_each(edge_t, e, mesh->edges) {
87                         // skip edges that do not represent a two-directional connection
88                         if(!e->reverse || e->reverse->to != e->from) {
89                                 continue;
90                         }
91
92                         // don't count edges twice
93                         if(e->to < e->from) {
94                                 continue;
95                         }
96
97                         assert(n < result_size);
98
99                         p->from = (meshlink_node_t *)e->from;
100                         p->to = (meshlink_node_t *)e->to;
101                         p->address = e->address.storage;
102                         p->weight = e->weight;
103
104                         n++;
105                         p++;
106                 }
107
108                 // shrink result to the actual amount of memory used
109                 result = xrealloc(result, n * sizeof(*result));
110                 *nmemb = n;
111         } else {
112                 *nmemb = 0;
113                 meshlink_errno = MESHLINK_ENOMEM;
114         }
115
116         pthread_mutex_unlock(&mesh->mutex);
117
118         return result;
119 }
120
121 static bool fstrwrite(const char *str, FILE *stream) {
122         assert(stream);
123
124         size_t len = strlen(str);
125
126         if(fwrite((void *)str, 1, len, stream) != len) {
127                 return false;
128         }
129
130         return true;
131 }
132
133 bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) {
134         assert(stream);
135
136         bool result = true;
137
138         pthread_mutex_lock(&mesh->mutex);
139
140         // export edges and nodes
141         size_t node_count = 0;
142         size_t edge_count = 0;
143
144         meshlink_node_t **nodes = meshlink_get_all_nodes(mesh, NULL, &node_count);
145         devtool_edge_t *edges = devtool_get_all_edges(mesh, NULL, &edge_count);
146
147         if((!nodes && node_count != 0) || (!edges && edge_count != 0)) {
148                 goto fail;
149         }
150
151         // export begin
152         if(!fstrwrite("{\n", stream)) {
153                 goto fail;
154         }
155
156         // export nodes
157         if(!fstrwrite("\t\"nodes\": {\n", stream)) {
158                 goto fail;
159         }
160
161         char buf[16];
162
163         for(size_t i = 0; i < node_count; ++i) {
164                 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream)) {
165                         goto fail;
166                 }
167
168                 if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\",\n", stream)) {
169                         goto fail;
170                 }
171
172                 snprintf(buf, sizeof(buf), "%d", ((node_t *)nodes[i])->devclass);
173
174                 if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) {
175                         goto fail;
176                 }
177
178                 if(!fstrwrite((i + 1) != node_count ? "\t\t},\n" : "\t\t}\n", stream)) {
179                         goto fail;
180                 }
181         }
182
183         if(!fstrwrite("\t},\n", stream)) {
184                 goto fail;
185         }
186
187         // export edges
188
189         if(!fstrwrite("\t\"edges\": {\n", stream)) {
190                 goto fail;
191         }
192
193         for(size_t i = 0; i < edge_count; ++i) {
194                 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("_to_", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\": {\n", stream)) {
195                         goto fail;
196                 }
197
198                 if(!fstrwrite("\t\t\t\"from\": \"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("\",\n", stream)) {
199                         goto fail;
200                 }
201
202                 if(!fstrwrite("\t\t\t\"to\": \"", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\",\n", stream)) {
203                         goto fail;
204                 }
205
206                 char *host = NULL, *port = NULL, *address = NULL;
207                 sockaddr2str((const sockaddr_t *)&edges[i].address, &host, &port);
208
209                 if(host && port) {
210                         xasprintf(&address, "{ \"host\": \"%s\", \"port\": %s }", host, port);
211                 }
212
213                 free(host);
214                 free(port);
215
216                 if(!fstrwrite("\t\t\t\"address\": ", stream) || !fstrwrite(address ? address : "null", stream) || !fstrwrite(",\n", stream)) {
217                         free(address);
218                         goto fail;
219                 }
220
221                 free(address);
222
223                 snprintf(buf, sizeof(buf), "%d", edges[i].weight);
224
225                 if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) {
226                         goto fail;
227                 }
228
229                 if(!fstrwrite((i + 1) != edge_count ? "\t\t},\n" : "\t\t}\n", stream)) {
230                         goto fail;
231                 }
232         }
233
234         if(!fstrwrite("\t}\n", stream)) {
235                 goto fail;
236         }
237
238         // DONE!
239
240         if(!fstrwrite("}", stream)) {
241                 goto fail;
242         }
243
244         goto done;
245
246 fail:
247         result = false;
248
249 done:
250         free(nodes);
251         free(edges);
252
253         pthread_mutex_unlock(&mesh->mutex);
254
255         return result;
256 }
257
258 void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
259         if(!mesh || !node || !status) {
260                 meshlink_errno = MESHLINK_EINVAL;
261                 return;
262         }
263
264         node_t *internal = (node_t *)node;
265
266         pthread_mutex_lock(&mesh->mutex);
267
268         memcpy(&status->status, &internal->status, sizeof status->status);
269         memcpy(&status->address, &internal->address, sizeof status->address);
270         status->mtu = internal->mtu;
271         status->minmtu = internal->minmtu;
272         status->maxmtu = internal->maxmtu;
273         status->mtuprobes = internal->mtuprobes;
274         status->in_packets = internal->in_packets;
275         status->in_bytes = internal->in_bytes;
276         status->out_packets = internal->out_packets;
277         status->out_bytes = internal->out_bytes;
278
279         // Derive UDP connection status
280         if(internal == mesh->self) {
281                 status->udp_status = DEVTOOL_UDP_WORKING;
282         } else if(!internal->status.reachable) {
283                 status->udp_status = DEVTOOL_UDP_IMPOSSIBLE;
284         } else if(!internal->status.validkey) {
285                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
286         } else if(internal->status.udp_confirmed) {
287                 status->udp_status = DEVTOOL_UDP_WORKING;
288         } else if(internal->mtuprobes > 30) {
289                 status->udp_status = DEVTOOL_UDP_FAILED;
290         } else if(internal->mtuprobes > 0) {
291                 status->udp_status = DEVTOOL_UDP_TRYING;
292         } else {
293                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
294         }
295
296         pthread_mutex_unlock(&mesh->mutex);
297 }
298
299 meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink_submesh_t **submeshes, size_t *nmemb) {
300         if(!mesh || !nmemb || (*nmemb && !submeshes)) {
301                 meshlink_errno = MESHLINK_EINVAL;
302                 return NULL;
303         }
304
305         meshlink_submesh_t **result;
306
307         //lock mesh->nodes
308         pthread_mutex_lock(&mesh->mutex);
309
310         *nmemb = mesh->submeshes->count;
311         result = realloc(submeshes, *nmemb * sizeof(*submeshes));
312
313         if(result) {
314                 meshlink_submesh_t **p = result;
315
316                 for list_each(submesh_t, s, mesh->submeshes) {
317                         *p++ = (meshlink_submesh_t *)s;
318                 }
319         } else {
320                 *nmemb = 0;
321                 free(submeshes);
322                 meshlink_errno = MESHLINK_ENOMEM;
323         }
324
325         pthread_mutex_unlock(&mesh->mutex);
326
327         return result;
328 }
329
330 meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns) {
331         meshlink_open_params_t *params = meshlink_open_params_init(confbase, name, appname, devclass);
332         params->netns = dup(netns);
333         meshlink_handle_t *handle;
334
335         if(params->netns == -1) {
336                 handle = NULL;
337                 meshlink_errno = MESHLINK_EINVAL;
338         } else {
339                 handle = meshlink_open_ex(params);
340         }
341
342         meshlink_open_params_free(params);
343
344         return handle;
345 }
346
347 void devtool_force_sptps_renewal(meshlink_handle_t *mesh, meshlink_node_t *node) {
348         if(!mesh || !node) {
349                 meshlink_errno = MESHLINK_EINVAL;
350                 return;
351         }
352
353         node_t *n = (node_t *)node;
354         connection_t *c = n->connection;
355
356         n->last_req_key = 0;
357
358         if(c) {
359                 c->last_key_renewal = 0;
360         }
361 }