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"
26 #include "splay_tree.h"
32 /* Return an array of edges in the current network graph.
33 * Data captures the current state and will not be updated.
34 * Caller must deallocate data when done.
36 devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *edges, size_t *nmemb) {
37 if(!mesh || !nmemb || (*nmemb && !edges)) {
38 meshlink_errno = MESHLINK_EINVAL;
42 pthread_mutex_lock(&(mesh->mesh_mutex));
44 devtool_edge_t *result = NULL;
45 unsigned int result_size = 0;
47 result_size = mesh->edges->count / 2;
49 // if result is smaller than edges, we have to dealloc all the excess devtool_edge_t
50 if((size_t)result_size > *nmemb) {
51 result = realloc(edges, result_size * sizeof(*result));
57 devtool_edge_t *p = result;
60 for splay_each(edge_t, e, mesh->edges) {
61 // skip edges that do not represent a two-directional connection
62 if((!e->reverse) || (e->reverse->to != e->from)) {
66 // don't count edges twice
71 assert(n < result_size);
73 p->from = (meshlink_node_t *)e->from;
74 p->to = (meshlink_node_t *)e->to;
75 p->address = e->address.storage;
76 p->options = e->options;
77 p->weight = e->weight;
83 // shrink result to the actual amount of memory used
84 result = realloc(result, n * sizeof(*result));
88 meshlink_errno = MESHLINK_ENOMEM;
91 pthread_mutex_unlock(&(mesh->mesh_mutex));
96 static bool fstrwrite(const char *str, FILE *stream) {
97 size_t len = strlen(str);
99 if(fwrite((void *)str, 1, len, stream) != len) {
106 static const char *__itoa(int value) {
107 static char buffer[sizeof(int) * 8 + 1]; // not thread safe
109 if(snprintf(buffer, sizeof(buffer), "%d", value) == -1) {
116 bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) {
119 pthread_mutex_lock(&(mesh->mesh_mutex));
121 // export edges and nodes
122 size_t node_count = 0;
123 size_t edge_count = 0;
125 meshlink_node_t **nodes = meshlink_get_all_nodes(mesh, NULL, &node_count);
126 devtool_edge_t *edges = devtool_get_all_edges(mesh, NULL, &edge_count);
128 if((!nodes && node_count != 0) || (!edges && edge_count != 0)) {
133 if(!fstrwrite("{\n", stream)) {
138 if(!fstrwrite("\t\"nodes\": {\n", stream)) {
142 for(size_t i = 0; i < node_count; ++i) {
143 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream)) {
147 if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\",\n", stream)) {
151 if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->options), stream) || !fstrwrite(",\n", stream)) {
155 if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->devclass), stream) || !fstrwrite("\n", stream)) {
159 if(!fstrwrite((i + 1) != node_count ? "\t\t},\n" : "\t\t}\n", stream)) {
164 if(!fstrwrite("\t},\n", stream)) {
170 if(!fstrwrite("\t\"edges\": {\n", stream)) {
174 for(size_t i = 0; i < edge_count; ++i) {
175 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("_to_", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\": {\n", stream)) {
179 if(!fstrwrite("\t\t\t\"from\": \"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("\",\n", stream)) {
183 if(!fstrwrite("\t\t\t\"to\": \"", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\",\n", stream)) {
187 char *host = NULL, *port = NULL, *address = NULL;
188 sockaddr2str((const sockaddr_t *)&edges[i].address, &host, &port);
191 xasprintf(&address, "{ \"host\": \"%s\", \"port\": %s }", host, port);
197 if(!fstrwrite("\t\t\t\"address\": ", stream) || !fstrwrite(address ? address : "null", stream) || !fstrwrite(",\n", stream)) {
204 if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(edges[i].options), stream) || !fstrwrite(",\n", stream)) {
208 if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(__itoa(edges[i].weight), 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 status->options = internal->options;
252 memcpy(&status->status, &internal->status, sizeof status->status);
253 memcpy(&status->address, &internal->address, sizeof status->address);
254 status->mtu = internal->mtu;
255 status->minmtu = internal->minmtu;
256 status->maxmtu = internal->maxmtu;
257 status->mtuprobes = internal->mtuprobes;
258 status->in_packets = internal->in_packets;
259 status->in_bytes = internal->in_bytes;
260 status->out_packets = internal->out_packets;
261 status->out_bytes = internal->out_bytes;
263 // Derive UDP connection status
264 if(internal == mesh->self) {
265 status->udp_status = DEVTOOL_UDP_WORKING;
266 } else if(!internal->status.reachable) {
267 status->udp_status = DEVTOOL_UDP_IMPOSSIBLE;
268 } else if(!internal->status.validkey) {
269 status->udp_status = DEVTOOL_UDP_UNKNOWN;
270 } else if(internal->status.udp_confirmed) {
271 status->udp_status = DEVTOOL_UDP_WORKING;
272 } else if(internal->mtuprobes > 30) {
273 status->udp_status = DEVTOOL_UDP_FAILED;
274 } else if(internal->mtuprobes > 0) {
275 status->udp_status = DEVTOOL_UDP_TRYING;
277 status->udp_status = DEVTOOL_UDP_UNKNOWN;
280 pthread_mutex_unlock(&mesh->mesh_mutex);