]> git.meshlink.io Git - meshlink-tiny/blob - src/devtools.c
d14d1d81370cdadf7d18232585b02bdcd2861003
[meshlink-tiny] / 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 static void keyrotate_nop_probe(int stage) {
38         (void)stage;
39         return;
40 }
41
42 static void inviter_commits_first_nop_probe(bool stage) {
43         (void)stage;
44         return;
45 }
46
47 static void sptps_renewal_nop_probe(meshlink_node_t *node) {
48         (void)node;
49         return;
50 }
51
52 void (*devtool_trybind_probe)(void) = nop_probe;
53 void (*devtool_keyrotate_probe)(int stage) = keyrotate_nop_probe;
54 void (*devtool_set_inviter_commits_first)(bool inviter_commited_first) = inviter_commits_first_nop_probe;
55 void (*devtool_adns_resolve_probe)(void) = nop_probe;
56 void (*devtool_sptps_renewal_probe)(meshlink_node_t *node) = sptps_renewal_nop_probe;
57
58 void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
59         if(!mesh || !node || !status) {
60                 meshlink_errno = MESHLINK_EINVAL;
61                 return;
62         }
63
64         node_t *internal = (node_t *)node;
65
66         if(pthread_mutex_lock(&mesh->mutex) != 0) {
67                 abort();
68         }
69
70         memcpy(&status->status, &internal->status, sizeof status->status);
71         memcpy(&status->address, &internal->address, sizeof status->address);
72         status->mtu = internal->mtu;
73         status->minmtu = internal->minmtu;
74         status->maxmtu = internal->maxmtu;
75         status->mtuprobes = internal->mtuprobes;
76         status->in_packets = internal->in_packets;
77         status->in_bytes = internal->in_bytes;
78         status->out_packets = internal->out_packets;
79         status->out_bytes = internal->out_bytes;
80
81         // Derive UDP connection status
82         if(internal == mesh->self) {
83                 status->udp_status = DEVTOOL_UDP_WORKING;
84         } else if(!internal->status.reachable) {
85                 status->udp_status = DEVTOOL_UDP_IMPOSSIBLE;
86         } else if(!internal->status.validkey) {
87                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
88         } else if(internal->status.udp_confirmed) {
89                 status->udp_status = DEVTOOL_UDP_WORKING;
90         } else if(internal->mtuprobes > 30) {
91                 status->udp_status = DEVTOOL_UDP_FAILED;
92         } else if(internal->mtuprobes > 0) {
93                 status->udp_status = DEVTOOL_UDP_TRYING;
94         } else {
95                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
96         }
97
98         pthread_mutex_unlock(&mesh->mutex);
99 }
100
101 meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink_submesh_t **submeshes, size_t *nmemb) {
102         if(!mesh || !nmemb || (*nmemb && !submeshes)) {
103                 meshlink_errno = MESHLINK_EINVAL;
104                 return NULL;
105         }
106
107         meshlink_submesh_t **result;
108
109         //lock mesh->nodes
110         if(pthread_mutex_lock(&mesh->mutex) != 0) {
111                 abort();
112         }
113
114         *nmemb = mesh->submeshes->count;
115         result = realloc(submeshes, *nmemb * sizeof(*submeshes));
116
117         if(result) {
118                 meshlink_submesh_t **p = result;
119
120                 for list_each(submesh_t, s, mesh->submeshes) {
121                         *p++ = (meshlink_submesh_t *)s;
122                 }
123         } else {
124                 *nmemb = 0;
125                 free(submeshes);
126                 meshlink_errno = MESHLINK_ENOMEM;
127         }
128
129         pthread_mutex_unlock(&mesh->mutex);
130
131         return result;
132 }
133
134 meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns) {
135         meshlink_open_params_t *params = meshlink_open_params_init(confbase, name, appname, devclass);
136         params->netns = dup(netns);
137         meshlink_handle_t *handle;
138
139         if(params->netns == -1) {
140                 handle = NULL;
141                 meshlink_errno = MESHLINK_EINVAL;
142         } else {
143                 handle = meshlink_open_ex(params);
144         }
145
146         meshlink_open_params_free(params);
147
148         return handle;
149 }
150
151 void devtool_force_sptps_renewal(meshlink_handle_t *mesh, meshlink_node_t *node) {
152         if(!mesh || !node) {
153                 meshlink_errno = MESHLINK_EINVAL;
154                 return;
155         }
156
157         node_t *n = (node_t *)node;
158         connection_t *c = n->connection;
159
160         n->last_req_key = -3600;
161
162         if(c) {
163                 c->last_key_renewal = -3600;
164         }
165 }
166
167 void devtool_set_meta_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
168         if(!mesh) {
169                 meshlink_errno = MESHLINK_EINVAL;
170                 return;
171         }
172
173         if(pthread_mutex_lock(&mesh->mutex) != 0) {
174                 abort();
175         }
176
177         mesh->meta_status_cb = cb;
178         pthread_mutex_unlock(&mesh->mutex);
179 }