]> git.meshlink.io Git - meshlink/blob - src/devtools.c
Add assert() calls to the library.
[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 trybind_nop_probe(void) {
34         return;
35 }
36
37 static void keyrotate_nop_probe(int stage) {
38         (void)stage;
39         return;
40 }
41
42 void (*devtool_trybind_probe)(void) = trybind_nop_probe;
43 void (*devtool_keyrotate_probe)(int stage) = keyrotate_nop_probe;
44
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.
48  */
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;
52                 return NULL;
53         }
54
55         pthread_mutex_lock(&(mesh->mesh_mutex));
56
57         devtool_edge_t *result = NULL;
58         unsigned int result_size = 0;
59
60         result_size = mesh->edges->count / 2;
61
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 = xrealloc(edges, result_size * sizeof(*result));
65         } else {
66                 result = edges;
67         }
68
69         if(result) {
70                 devtool_edge_t *p = result;
71                 unsigned int n = 0;
72
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)) {
76                                 continue;
77                         }
78
79                         // don't count edges twice
80                         if(e->to < e->from) {
81                                 continue;
82                         }
83
84                         assert(n < result_size);
85
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;
90
91                         n++;
92                         p++;
93                 }
94
95                 // shrink result to the actual amount of memory used
96                 result = xrealloc(result, n * sizeof(*result));
97                 *nmemb = n;
98         } else {
99                 *nmemb = 0;
100                 meshlink_errno = MESHLINK_ENOMEM;
101         }
102
103         pthread_mutex_unlock(&(mesh->mesh_mutex));
104
105         return result;
106 }
107
108 static bool fstrwrite(const char *str, FILE *stream) {
109         assert(stream);
110
111         size_t len = strlen(str);
112
113         if(fwrite((void *)str, 1, len, stream) != len) {
114                 return false;
115         }
116
117         return true;
118 }
119
120 bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) {
121         assert(stream);
122
123         bool result = true;
124
125         pthread_mutex_lock(&(mesh->mesh_mutex));
126
127         // export edges and nodes
128         size_t node_count = 0;
129         size_t edge_count = 0;
130
131         meshlink_node_t **nodes = meshlink_get_all_nodes(mesh, NULL, &node_count);
132         devtool_edge_t *edges = devtool_get_all_edges(mesh, NULL, &edge_count);
133
134         if((!nodes && node_count != 0) || (!edges && edge_count != 0)) {
135                 goto fail;
136         }
137
138         // export begin
139         if(!fstrwrite("{\n", stream)) {
140                 goto fail;
141         }
142
143         // export nodes
144         if(!fstrwrite("\t\"nodes\": {\n", stream)) {
145                 goto fail;
146         }
147
148         char buf[16];
149
150         for(size_t i = 0; i < node_count; ++i) {
151                 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream)) {
152                         goto fail;
153                 }
154
155                 if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\",\n", stream)) {
156                         goto fail;
157                 }
158
159                 snprintf(buf, sizeof(buf), "%d", ((node_t *)nodes[i])->devclass);
160
161                 if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) {
162                         goto fail;
163                 }
164
165                 if(!fstrwrite((i + 1) != node_count ? "\t\t},\n" : "\t\t}\n", stream)) {
166                         goto fail;
167                 }
168         }
169
170         if(!fstrwrite("\t},\n", stream)) {
171                 goto fail;
172         }
173
174         // export edges
175
176         if(!fstrwrite("\t\"edges\": {\n", stream)) {
177                 goto fail;
178         }
179
180         for(size_t i = 0; i < edge_count; ++i) {
181                 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("_to_", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\": {\n", stream)) {
182                         goto fail;
183                 }
184
185                 if(!fstrwrite("\t\t\t\"from\": \"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("\",\n", stream)) {
186                         goto fail;
187                 }
188
189                 if(!fstrwrite("\t\t\t\"to\": \"", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\",\n", stream)) {
190                         goto fail;
191                 }
192
193                 char *host = NULL, *port = NULL, *address = NULL;
194                 sockaddr2str((const sockaddr_t *)&edges[i].address, &host, &port);
195
196                 if(host && port) {
197                         xasprintf(&address, "{ \"host\": \"%s\", \"port\": %s }", host, port);
198                 }
199
200                 free(host);
201                 free(port);
202
203                 if(!fstrwrite("\t\t\t\"address\": ", stream) || !fstrwrite(address ? address : "null", stream) || !fstrwrite(",\n", stream)) {
204                         free(address);
205                         goto fail;
206                 }
207
208                 free(address);
209
210                 snprintf(buf, sizeof(buf), "%d", edges[i].weight);
211
212                 if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) {
213                         goto fail;
214                 }
215
216                 if(!fstrwrite((i + 1) != edge_count ? "\t\t},\n" : "\t\t}\n", stream)) {
217                         goto fail;
218                 }
219         }
220
221         if(!fstrwrite("\t}\n", stream)) {
222                 goto fail;
223         }
224
225         // DONE!
226
227         if(!fstrwrite("}", stream)) {
228                 goto fail;
229         }
230
231         goto done;
232
233 fail:
234         result = false;
235
236 done:
237         free(nodes);
238         free(edges);
239
240         pthread_mutex_unlock(&(mesh->mesh_mutex));
241
242         return result;
243 }
244
245 void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
246         if(!mesh || !node || !status) {
247                 meshlink_errno = MESHLINK_EINVAL;
248                 return;
249         }
250
251         node_t *internal = (node_t *)node;
252
253         pthread_mutex_lock(&mesh->mesh_mutex);
254
255         memcpy(&status->status, &internal->status, sizeof status->status);
256         memcpy(&status->address, &internal->address, sizeof status->address);
257         status->mtu = internal->mtu;
258         status->minmtu = internal->minmtu;
259         status->maxmtu = internal->maxmtu;
260         status->mtuprobes = internal->mtuprobes;
261         status->in_packets = internal->in_packets;
262         status->in_bytes = internal->in_bytes;
263         status->out_packets = internal->out_packets;
264         status->out_bytes = internal->out_bytes;
265
266         // Derive UDP connection status
267         if(internal == mesh->self) {
268                 status->udp_status = DEVTOOL_UDP_WORKING;
269         } else if(!internal->status.reachable) {
270                 status->udp_status = DEVTOOL_UDP_IMPOSSIBLE;
271         } else if(!internal->status.validkey) {
272                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
273         } else if(internal->status.udp_confirmed) {
274                 status->udp_status = DEVTOOL_UDP_WORKING;
275         } else if(internal->mtuprobes > 30) {
276                 status->udp_status = DEVTOOL_UDP_FAILED;
277         } else if(internal->mtuprobes > 0) {
278                 status->udp_status = DEVTOOL_UDP_TRYING;
279         } else {
280                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
281         }
282
283         pthread_mutex_unlock(&mesh->mesh_mutex);
284 }
285
286 meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink_submesh_t **submeshes, size_t *nmemb) {
287         if(!mesh || !nmemb || (*nmemb && !submeshes)) {
288                 meshlink_errno = MESHLINK_EINVAL;
289                 return NULL;
290         }
291
292         meshlink_submesh_t **result;
293
294         //lock mesh->nodes
295         pthread_mutex_lock(&(mesh->mesh_mutex));
296
297         *nmemb = mesh->submeshes->count;
298         result = realloc(submeshes, *nmemb * sizeof(*submeshes));
299
300         if(result) {
301                 meshlink_submesh_t **p = result;
302
303                 for list_each(submesh_t, s, mesh->submeshes) {
304                         *p++ = (meshlink_submesh_t *)s;
305                 }
306         } else {
307                 *nmemb = 0;
308                 free(submeshes);
309                 meshlink_errno = MESHLINK_ENOMEM;
310         }
311
312         pthread_mutex_unlock(&(mesh->mesh_mutex));
313
314         return result;
315 }
316
317 meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns) {
318         meshlink_open_params_t *params = meshlink_open_params_init(confbase, name, appname, devclass);
319         params->netns = dup(netns);
320         meshlink_handle_t *handle;
321
322         if(params->netns == -1) {
323                 handle = NULL;
324                 meshlink_errno = MESHLINK_EINVAL;
325         } else {
326                 handle = meshlink_open_ex(params);
327         }
328
329         meshlink_open_params_free(params);
330
331         return handle;
332 }