]> git.meshlink.io Git - meshlink/blob - src/devtools.c
Move the routing header out of the SPTPS payload.
[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
22 #include "logger.h"
23 #include "meshlink_internal.h"
24 #include "node.h"
25 #include "splay_tree.h"
26 #include "netutl.h"
27 #include "xalloc.h"
28
29 #include "devtools.h"
30
31 /* Return an array of edges in the current network graph.
32  * Data captures the current state and will not be updated.
33  * Caller must deallocate data when done.
34  */
35 devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *edges, size_t *nmemb) {
36         if(!mesh || !nmemb || (*nmemb && !edges)) {
37                 meshlink_errno = MESHLINK_EINVAL;
38                 return NULL;
39         }
40
41         pthread_mutex_lock(&(mesh->mesh_mutex));
42
43         devtool_edge_t *result = NULL;
44         unsigned int result_size = 0;
45
46         result_size = mesh->edges->count / 2;
47
48         // if result is smaller than edges, we have to dealloc all the excess devtool_edge_t
49         if((size_t)result_size > *nmemb) {
50                 result = realloc(edges, result_size * sizeof(*result));
51         } else {
52                 result = edges;
53         }
54
55         if(result) {
56                 devtool_edge_t *p = result;
57                 unsigned int n = 0;
58
59                 for splay_each(edge_t, e, mesh->edges) {
60                         // skip edges that do not represent a two-directional connection
61                         if((!e->reverse) || (e->reverse->to != e->from)) {
62                                 continue;
63                         }
64
65                         // don't count edges twice
66                         if(e->to < e->from) {
67                                 continue;
68                         }
69
70                         assert(n < result_size);
71
72                         p->from = (meshlink_node_t *)e->from;
73                         p->to = (meshlink_node_t *)e->to;
74                         p->address = e->address.storage;
75                         p->options = e->options;
76                         p->weight = e->weight;
77
78                         n++;
79                         p++;
80                 }
81
82                 // shrink result to the actual amount of memory used
83                 result = realloc(result, n * sizeof(*result));
84                 *nmemb = n;
85         } else {
86                 *nmemb = 0;
87                 meshlink_errno = MESHLINK_ENOMEM;
88         }
89
90         pthread_mutex_unlock(&(mesh->mesh_mutex));
91
92         return result;
93 }
94
95 static bool fstrwrite(const char *str, FILE *stream) {
96         size_t len = strlen(str);
97
98         if(fwrite((void *)str, 1, len, stream) != len) {
99                 return false;
100         }
101
102         return true;
103 }
104
105 static const char *__itoa(int value) {
106         static char buffer[sizeof(int) * 8 + 1];        // not thread safe
107
108         if(snprintf(buffer, sizeof(buffer), "%d", value) == -1) {
109                 return "";
110         }
111
112         return buffer;
113 }
114
115 bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) {
116         bool result = true;
117
118         pthread_mutex_lock(&(mesh->mesh_mutex));
119
120         // export edges and nodes
121         size_t node_count = 0;
122         size_t edge_count = 0;
123
124         meshlink_node_t **nodes = meshlink_get_all_nodes(mesh, NULL, &node_count);
125         devtool_edge_t *edges = devtool_get_all_edges(mesh, NULL, &edge_count);
126
127         if((!nodes && node_count != 0) || (!edges && edge_count != 0)) {
128                 goto fail;
129         }
130
131         // export begin
132         if(!fstrwrite("{\n", stream)) {
133                 goto fail;
134         }
135
136         // export nodes
137         if(!fstrwrite("\t\"nodes\": {\n", stream)) {
138                 goto fail;
139         }
140
141         for(size_t i = 0; i < node_count; ++i) {
142                 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream)) {
143                         goto fail;
144                 }
145
146                 if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\",\n", stream)) {
147                         goto fail;
148                 }
149
150                 if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->options), stream) || !fstrwrite(",\n", stream)) {
151                         goto fail;
152                 }
153
154                 if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->devclass), stream) || !fstrwrite("\n", stream)) {
155                         goto fail;
156                 }
157
158                 if(!fstrwrite((i + 1) != node_count ? "\t\t},\n" : "\t\t}\n", stream)) {
159                         goto fail;
160                 }
161         }
162
163         if(!fstrwrite("\t},\n", stream)) {
164                 goto fail;
165         }
166
167         // export edges
168
169         if(!fstrwrite("\t\"edges\": {\n", stream)) {
170                 goto fail;
171         }
172
173         for(size_t i = 0; i < edge_count; ++i) {
174                 if(!fstrwrite("\t\t\"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("_to_", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\": {\n", stream)) {
175                         goto fail;
176                 }
177
178                 if(!fstrwrite("\t\t\t\"from\": \"", stream) || !fstrwrite(edges[i].from->name, stream) || !fstrwrite("\",\n", stream)) {
179                         goto fail;
180                 }
181
182                 if(!fstrwrite("\t\t\t\"to\": \"", stream) || !fstrwrite(edges[i].to->name, stream) || !fstrwrite("\",\n", stream)) {
183                         goto fail;
184                 }
185
186                 char *host = NULL, *port = NULL, *address = NULL;
187                 sockaddr2str((const sockaddr_t *)&edges[i].address, &host, &port);
188
189                 if(host && port) {
190                         xasprintf(&address, "{ \"host\": \"%s\", \"port\": %s }", host, port);
191                 }
192
193                 free(host);
194                 free(port);
195
196                 if(!fstrwrite("\t\t\t\"address\": ", stream) || !fstrwrite(address ? address : "null", stream) || !fstrwrite(",\n", stream)) {
197                         free(address);
198                         goto fail;
199                 }
200
201                 free(address);
202
203                 if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(edges[i].options), stream) || !fstrwrite(",\n", stream)) {
204                         goto fail;
205                 }
206
207                 if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(__itoa(edges[i].weight), stream) || !fstrwrite("\n", stream)) {
208                         goto fail;
209                 }
210
211                 if(!fstrwrite((i + 1) != edge_count ? "\t\t},\n" : "\t\t}\n", stream)) {
212                         goto fail;
213                 }
214         }
215
216         if(!fstrwrite("\t}\n", stream)) {
217                 goto fail;
218         }
219
220         // DONE!
221
222         if(!fstrwrite("}", stream)) {
223                 goto fail;
224         }
225
226         goto done;
227
228 fail:
229         result = false;
230
231 done:
232         free(nodes);
233         free(edges);
234
235         pthread_mutex_unlock(&(mesh->mesh_mutex));
236
237         return result;
238 }
239
240 void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
241         if(!mesh || !node || !status) {
242                 meshlink_errno = MESHLINK_EINVAL;
243                 return;
244         }
245
246         node_t *internal = (node_t *)node;
247
248         pthread_mutex_lock(&mesh->mesh_mutex);
249
250         status->options = internal->options;
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;
261
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;
275         } else {
276                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
277         }
278
279         pthread_mutex_unlock(&mesh->mesh_mutex);
280 }