]> git.meshlink.io Git - meshlink/blob - src/devtools.c
6d22516d145c16b04e095610977f57bf4c7d55a3
[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 "splay_tree.h"
27 #include "netutl.h"
28 #include "xalloc.h"
29
30 #include "devtools.h"
31
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.
35  */
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;
39                 return NULL;
40         }
41
42         pthread_mutex_lock(&(mesh->mesh_mutex));
43
44         devtool_edge_t *result = NULL;
45         unsigned int result_size = 0;
46
47         result_size = mesh->edges->count / 2;
48
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));
52         } else {
53                 result = edges;
54         }
55
56         if(result) {
57                 devtool_edge_t *p = result;
58                 unsigned int n = 0;
59
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)) {
63                                 continue;
64                         }
65
66                         // don't count edges twice
67                         if(e->to < e->from) {
68                                 continue;
69                         }
70
71                         assert(n < result_size);
72
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;
78
79                         n++;
80                         p++;
81                 }
82
83                 // shrink result to the actual amount of memory used
84                 result = realloc(result, n * sizeof(*result));
85                 *nmemb = n;
86         } else {
87                 *nmemb = 0;
88                 meshlink_errno = MESHLINK_ENOMEM;
89         }
90
91         pthread_mutex_unlock(&(mesh->mesh_mutex));
92
93         return result;
94 }
95
96 static bool fstrwrite(const char *str, FILE *stream) {
97         size_t len = strlen(str);
98
99         if(fwrite((void *)str, 1, len, stream) != len) {
100                 return false;
101         }
102
103         return true;
104 }
105
106 static const char *__itoa(int value) {
107         static char buffer[sizeof(int) * 8 + 1];        // not thread safe
108
109         if(snprintf(buffer, sizeof(buffer), "%d", value) == -1) {
110                 return "";
111         }
112
113         return buffer;
114 }
115
116 bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) {
117         bool result = true;
118
119         pthread_mutex_lock(&(mesh->mesh_mutex));
120
121         // export edges and nodes
122         size_t node_count = 0;
123         size_t edge_count = 0;
124
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);
127
128         if((!nodes && node_count != 0) || (!edges && edge_count != 0)) {
129                 goto fail;
130         }
131
132         // export begin
133         if(!fstrwrite("{\n", stream)) {
134                 goto fail;
135         }
136
137         // export nodes
138         if(!fstrwrite("\t\"nodes\": {\n", stream)) {
139                 goto fail;
140         }
141
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)) {
144                         goto fail;
145                 }
146
147                 if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\",\n", stream)) {
148                         goto fail;
149                 }
150
151                 if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->options), stream) || !fstrwrite(",\n", stream)) {
152                         goto fail;
153                 }
154
155                 if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->devclass), stream) || !fstrwrite("\n", stream)) {
156                         goto fail;
157                 }
158
159                 if(!fstrwrite((i + 1) != node_count ? "\t\t},\n" : "\t\t}\n", stream)) {
160                         goto fail;
161                 }
162         }
163
164         if(!fstrwrite("\t},\n", stream)) {
165                 goto fail;
166         }
167
168         // export edges
169
170         if(!fstrwrite("\t\"edges\": {\n", stream)) {
171                 goto fail;
172         }
173
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)) {
176                         goto fail;
177                 }
178
179                 if(!fstrwrite("\t\t\t\"from\": \"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("\",\n", stream)) {
180                         goto fail;
181                 }
182
183                 if(!fstrwrite("\t\t\t\"to\": \"", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\",\n", stream)) {
184                         goto fail;
185                 }
186
187                 char *host = NULL, *port = NULL, *address = NULL;
188                 sockaddr2str((const sockaddr_t *)&edges[i].address, &host, &port);
189
190                 if(host && port) {
191                         xasprintf(&address, "{ \"host\": \"%s\", \"port\": %s }", host, port);
192                 }
193
194                 free(host);
195                 free(port);
196
197                 if(!fstrwrite("\t\t\t\"address\": ", stream) || !fstrwrite(address ? address : "null", stream) || !fstrwrite(",\n", stream)) {
198                         free(address);
199                         goto fail;
200                 }
201
202                 free(address);
203
204                 if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(edges[i].options), stream) || !fstrwrite(",\n", stream)) {
205                         goto fail;
206                 }
207
208                 if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(__itoa(edges[i].weight), stream) || !fstrwrite("\n", stream)) {
209                         goto fail;
210                 }
211
212                 if(!fstrwrite((i + 1) != edge_count ? "\t\t},\n" : "\t\t}\n", stream)) {
213                         goto fail;
214                 }
215         }
216
217         if(!fstrwrite("\t}\n", stream)) {
218                 goto fail;
219         }
220
221         // DONE!
222
223         if(!fstrwrite("}", stream)) {
224                 goto fail;
225         }
226
227         goto done;
228
229 fail:
230         result = false;
231
232 done:
233         free(nodes);
234         free(edges);
235
236         pthread_mutex_unlock(&(mesh->mesh_mutex));
237
238         return result;
239 }
240
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;
244                 return;
245         }
246
247         node_t *internal = (node_t *)node;
248
249         pthread_mutex_lock(&mesh->mesh_mutex);
250
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;
262
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;
276         } else {
277                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
278         }
279
280         pthread_mutex_unlock(&mesh->mesh_mutex);
281 }
282
283 meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns) {
284         meshlink_open_params_t *params = meshlink_open_params_init(confbase, name, appname, devclass);
285         params->netns = dup(netns);
286         meshlink_handle_t *handle;
287
288         if(params->netns == -1) {
289                 handle = NULL;
290                 meshlink_errno = MESHLINK_EINVAL;
291         } else {
292                 handle = meshlink_open_ex(params);
293         }
294
295         meshlink_open_params_free(params);
296
297         return handle;
298 }