2 devtools.c -- Debugging and quality control functions.
3 Copyright (C) 2014, 2017 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"
27 #include "splay_tree.h"
33 static void nop_probe(void) {
37 static void keyrotate_nop_probe(int stage) {
42 static void inviter_commits_first_nop_probe(bool stage) {
47 static void sptps_renewal_nop_probe(meshlink_node_t *node) {
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;
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.
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;
68 if(pthread_mutex_lock(&mesh->mutex) != 0) {
72 devtool_edge_t *result = NULL;
73 unsigned int result_size = 0;
75 result_size = mesh->edges->count / 2;
77 // if result is smaller than edges, we have to dealloc all the excess devtool_edge_t
78 if((size_t)result_size > *nmemb) {
79 result = xrealloc(edges, result_size * sizeof(*result));
85 devtool_edge_t *p = result;
88 for splay_each(edge_t, e, mesh->edges) {
89 // skip edges that do not represent a two-directional connection
90 if(!e->reverse || e->reverse->to != e->from) {
94 // don't count edges twice
99 assert(n < result_size);
101 p->from = (meshlink_node_t *)e->from;
102 p->to = (meshlink_node_t *)e->to;
103 p->address = e->address.storage;
104 p->weight = e->weight;
110 // shrink result to the actual amount of memory used
111 result = xrealloc(result, n * sizeof(*result));
115 meshlink_errno = MESHLINK_ENOMEM;
118 pthread_mutex_unlock(&mesh->mutex);
123 static bool fstrwrite(const char *str, FILE *stream) {
126 size_t len = strlen(str);
128 if(fwrite((void *)str, 1, len, stream) != len) {
135 bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) {
140 if(pthread_mutex_lock(&mesh->mutex) != 0) {
144 // export edges and nodes
145 size_t node_count = 0;
146 size_t edge_count = 0;
148 meshlink_node_t **nodes = meshlink_get_all_nodes(mesh, NULL, &node_count);
149 devtool_edge_t *edges = devtool_get_all_edges(mesh, NULL, &edge_count);
151 if((!nodes && node_count != 0) || (!edges && edge_count != 0)) {
156 if(!fstrwrite("{\n", stream)) {
161 if(!fstrwrite("\t\"nodes\": {\n", stream)) {
167 for(size_t i = 0; i < node_count; ++i) {
168 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream)) {
172 if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\",\n", stream)) {
176 snprintf(buf, sizeof(buf), "%d", ((node_t *)nodes[i])->devclass);
178 if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) {
182 if(!fstrwrite((i + 1) != node_count ? "\t\t},\n" : "\t\t}\n", stream)) {
187 if(!fstrwrite("\t},\n", stream)) {
193 if(!fstrwrite("\t\"edges\": {\n", stream)) {
197 for(size_t i = 0; i < edge_count; ++i) {
198 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("_to_", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\": {\n", stream)) {
202 if(!fstrwrite("\t\t\t\"from\": \"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("\",\n", stream)) {
206 if(!fstrwrite("\t\t\t\"to\": \"", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\",\n", stream)) {
210 char *host = NULL, *port = NULL, *address = NULL;
211 sockaddr2str((const sockaddr_t *)&edges[i].address, &host, &port);
214 xasprintf(&address, "{ \"host\": \"%s\", \"port\": %s }", host, port);
220 if(!fstrwrite("\t\t\t\"address\": ", stream) || !fstrwrite(address ? address : "null", stream) || !fstrwrite(",\n", stream)) {
227 snprintf(buf, sizeof(buf), "%d", edges[i].weight);
229 if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) {
233 if(!fstrwrite((i + 1) != edge_count ? "\t\t},\n" : "\t\t}\n", stream)) {
238 if(!fstrwrite("\t}\n", stream)) {
244 if(!fstrwrite("}", stream)) {
257 pthread_mutex_unlock(&mesh->mutex);
262 static void devtool_get_reset_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status, bool reset) {
263 node_t *internal = (node_t *)node;
265 if(pthread_mutex_lock(&mesh->mutex) != 0) {
270 memcpy(&status->status, &internal->status, sizeof status->status);
271 memcpy(&status->address, &internal->address, sizeof status->address);
272 status->mtu = internal->mtu;
273 status->minmtu = internal->minmtu;
274 status->maxmtu = internal->maxmtu;
275 status->mtuprobes = internal->mtuprobes;
276 status->in_data = internal->in_data;
277 status->out_data = internal->out_data;
278 status->in_forward = internal->in_forward;
279 status->out_forward = internal->out_forward;
280 status->in_meta = internal->in_meta;
281 status->out_meta = internal->out_meta;
283 // Derive UDP connection status
284 if(internal == mesh->self) {
285 status->udp_status = DEVTOOL_UDP_WORKING;
286 } else if(!internal->status.reachable) {
287 status->udp_status = DEVTOOL_UDP_IMPOSSIBLE;
288 } else if(!internal->status.validkey) {
289 status->udp_status = DEVTOOL_UDP_UNKNOWN;
290 } else if(internal->status.udp_confirmed) {
291 status->udp_status = DEVTOOL_UDP_WORKING;
292 } else if(internal->mtuprobes > 30) {
293 status->udp_status = DEVTOOL_UDP_FAILED;
294 } else if(internal->mtuprobes > 0) {
295 status->udp_status = DEVTOOL_UDP_TRYING;
297 status->udp_status = DEVTOOL_UDP_UNKNOWN;
302 internal->in_data = 0;
303 internal->out_data = 0;
304 internal->in_forward = 0;
305 internal->out_forward = 0;
306 internal->in_meta = 0;
307 internal->out_meta = 0;
310 pthread_mutex_unlock(&mesh->mutex);
313 void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
314 if(!mesh || !node || !status) {
315 meshlink_errno = MESHLINK_EINVAL;
319 devtool_get_reset_node_status(mesh, node, status, false);
322 void devtool_reset_node_counters(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
324 meshlink_errno = MESHLINK_EINVAL;
328 devtool_get_reset_node_status(mesh, node, status, true);
331 meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink_submesh_t **submeshes, size_t *nmemb) {
332 if(!mesh || !nmemb || (*nmemb && !submeshes)) {
333 meshlink_errno = MESHLINK_EINVAL;
337 meshlink_submesh_t **result;
340 if(pthread_mutex_lock(&mesh->mutex) != 0) {
344 *nmemb = mesh->submeshes->count;
345 result = realloc(submeshes, *nmemb * sizeof(*submeshes));
348 meshlink_submesh_t **p = result;
350 for list_each(submesh_t, s, mesh->submeshes) {
351 *p++ = (meshlink_submesh_t *)s;
356 meshlink_errno = MESHLINK_ENOMEM;
359 pthread_mutex_unlock(&mesh->mutex);
364 meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns) {
365 meshlink_open_params_t *params = meshlink_open_params_init(confbase, name, appname, devclass);
366 params->netns = dup(netns);
367 meshlink_handle_t *handle;
369 if(params->netns == -1) {
371 meshlink_errno = MESHLINK_EINVAL;
373 handle = meshlink_open_ex(params);
376 meshlink_open_params_free(params);
381 void devtool_force_sptps_renewal(meshlink_handle_t *mesh, meshlink_node_t *node) {
383 meshlink_errno = MESHLINK_EINVAL;
387 node_t *n = (node_t *)node;
388 connection_t *c = n->connection;
390 n->last_req_key = -3600;
393 c->last_key_renewal = -3600;
397 void devtool_set_meta_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
399 meshlink_errno = MESHLINK_EINVAL;
403 if(pthread_mutex_lock(&mesh->mutex) != 0) {
407 mesh->meta_status_cb = cb;
408 pthread_mutex_unlock(&mesh->mutex);