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 trybind_nop_probe(void) {
37 static void keyrotate_nop_probe(int stage) {
42 void (*devtool_trybind_probe)(void) = trybind_nop_probe;
43 void (*devtool_keyrotate_probe)(int stage) = keyrotate_nop_probe;
45 /* Return an array of edges in the current network graph.
46 * Data captures the current state and will not be updated.
47 * Caller must deallocate data when done.
49 devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *edges, size_t *nmemb) {
50 if(!mesh || !nmemb || (*nmemb && !edges)) {
51 meshlink_errno = MESHLINK_EINVAL;
55 pthread_mutex_lock(&(mesh->mesh_mutex));
57 devtool_edge_t *result = NULL;
58 unsigned int result_size = 0;
60 result_size = mesh->edges->count / 2;
62 // if result is smaller than edges, we have to dealloc all the excess devtool_edge_t
63 if((size_t)result_size > *nmemb) {
64 result = realloc(edges, result_size * sizeof(*result));
70 devtool_edge_t *p = result;
73 for splay_each(edge_t, e, mesh->edges) {
74 // skip edges that do not represent a two-directional connection
75 if((!e->reverse) || (e->reverse->to != e->from)) {
79 // don't count edges twice
84 assert(n < result_size);
86 p->from = (meshlink_node_t *)e->from;
87 p->to = (meshlink_node_t *)e->to;
88 p->address = e->address.storage;
89 p->weight = e->weight;
95 // shrink result to the actual amount of memory used
96 result = realloc(result, n * sizeof(*result));
100 meshlink_errno = MESHLINK_ENOMEM;
103 pthread_mutex_unlock(&(mesh->mesh_mutex));
108 static bool fstrwrite(const char *str, FILE *stream) {
109 size_t len = strlen(str);
111 if(fwrite((void *)str, 1, len, stream) != len) {
118 bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) {
121 pthread_mutex_lock(&(mesh->mesh_mutex));
123 // export edges and nodes
124 size_t node_count = 0;
125 size_t edge_count = 0;
127 meshlink_node_t **nodes = meshlink_get_all_nodes(mesh, NULL, &node_count);
128 devtool_edge_t *edges = devtool_get_all_edges(mesh, NULL, &edge_count);
130 if((!nodes && node_count != 0) || (!edges && edge_count != 0)) {
135 if(!fstrwrite("{\n", stream)) {
140 if(!fstrwrite("\t\"nodes\": {\n", stream)) {
146 for(size_t i = 0; i < node_count; ++i) {
147 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream)) {
151 if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\",\n", stream)) {
155 snprintf(buf, sizeof(buf), "%d", ((node_t *)nodes[i])->devclass);
157 if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) {
161 if(!fstrwrite((i + 1) != node_count ? "\t\t},\n" : "\t\t}\n", stream)) {
166 if(!fstrwrite("\t},\n", stream)) {
172 if(!fstrwrite("\t\"edges\": {\n", stream)) {
176 for(size_t i = 0; i < edge_count; ++i) {
177 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("_to_", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\": {\n", stream)) {
181 if(!fstrwrite("\t\t\t\"from\": \"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("\",\n", stream)) {
185 if(!fstrwrite("\t\t\t\"to\": \"", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\",\n", stream)) {
189 char *host = NULL, *port = NULL, *address = NULL;
190 sockaddr2str((const sockaddr_t *)&edges[i].address, &host, &port);
193 xasprintf(&address, "{ \"host\": \"%s\", \"port\": %s }", host, port);
199 if(!fstrwrite("\t\t\t\"address\": ", stream) || !fstrwrite(address ? address : "null", stream) || !fstrwrite(",\n", stream)) {
206 snprintf(buf, sizeof(buf), "%d", edges[i].weight);
208 if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) {
212 if(!fstrwrite((i + 1) != edge_count ? "\t\t},\n" : "\t\t}\n", stream)) {
217 if(!fstrwrite("\t}\n", stream)) {
223 if(!fstrwrite("}", stream)) {
236 pthread_mutex_unlock(&(mesh->mesh_mutex));
241 void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
242 if(!mesh || !node || !status) {
243 meshlink_errno = MESHLINK_EINVAL;
247 node_t *internal = (node_t *)node;
249 pthread_mutex_lock(&mesh->mesh_mutex);
251 memcpy(&status->status, &internal->status, sizeof status->status);
252 memcpy(&status->address, &internal->address, sizeof status->address);
253 status->mtu = internal->mtu;
254 status->minmtu = internal->minmtu;
255 status->maxmtu = internal->maxmtu;
256 status->mtuprobes = internal->mtuprobes;
257 status->in_packets = internal->in_packets;
258 status->in_bytes = internal->in_bytes;
259 status->out_packets = internal->out_packets;
260 status->out_bytes = internal->out_bytes;
262 // Derive UDP connection status
263 if(internal == mesh->self) {
264 status->udp_status = DEVTOOL_UDP_WORKING;
265 } else if(!internal->status.reachable) {
266 status->udp_status = DEVTOOL_UDP_IMPOSSIBLE;
267 } else if(!internal->status.validkey) {
268 status->udp_status = DEVTOOL_UDP_UNKNOWN;
269 } else if(internal->status.udp_confirmed) {
270 status->udp_status = DEVTOOL_UDP_WORKING;
271 } else if(internal->mtuprobes > 30) {
272 status->udp_status = DEVTOOL_UDP_FAILED;
273 } else if(internal->mtuprobes > 0) {
274 status->udp_status = DEVTOOL_UDP_TRYING;
276 status->udp_status = DEVTOOL_UDP_UNKNOWN;
279 pthread_mutex_unlock(&mesh->mesh_mutex);
282 meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink_submesh_t **submeshes, size_t *nmemb) {
283 if(!mesh || !nmemb || (*nmemb && !submeshes)) {
284 meshlink_errno = MESHLINK_EINVAL;
288 meshlink_submesh_t **result;
291 pthread_mutex_lock(&(mesh->mesh_mutex));
293 *nmemb = mesh->submeshes->count;
294 result = realloc(submeshes, *nmemb * sizeof(*submeshes));
297 meshlink_submesh_t **p = result;
299 for list_each(submesh_t, s, mesh->submeshes) {
300 *p++ = (meshlink_submesh_t *)s;
305 meshlink_errno = MESHLINK_ENOMEM;
308 pthread_mutex_unlock(&(mesh->mesh_mutex));
312 meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns) {
313 meshlink_open_params_t *params = meshlink_open_params_init(confbase, name, appname, devclass);
314 params->netns = dup(netns);
315 meshlink_handle_t *handle;
317 if(params->netns == -1) {
319 meshlink_errno = MESHLINK_EINVAL;
321 handle = meshlink_open_ex(params);
324 meshlink_open_params_free(params);