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