2 devtools.c -- Debugging and quality control functions.
3 Copyright (C) 2014, 2017 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
24 #include "meshlink_internal.h"
27 #include "splay_tree.h"
33 static void nop_probe(void) {
37 static void keyrotate_nop_probe(int stage) {
42 static void inviter_commits_first_nop_probe(bool stage) {
47 static void sptps_renewal_nop_probe(meshlink_node_t *node) {
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_sptps_renewal_probe)(meshlink_node_t *node) = sptps_renewal_nop_probe;
57 void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
58 if(!mesh || !node || !status) {
59 meshlink_errno = MESHLINK_EINVAL;
63 node_t *internal = (node_t *)node;
65 if(pthread_mutex_lock(&mesh->mutex) != 0) {
69 memcpy(&status->status, &internal->status, sizeof status->status);
70 memcpy(&status->address, &internal->address, sizeof status->address);
71 status->mtu = internal->mtu;
72 status->minmtu = internal->minmtu;
73 status->maxmtu = internal->maxmtu;
74 status->mtuprobes = internal->mtuprobes;
75 status->in_packets = internal->in_packets;
76 status->in_bytes = internal->in_bytes;
77 status->out_packets = internal->out_packets;
78 status->out_bytes = internal->out_bytes;
80 // Derive UDP connection status
81 if(internal == mesh->self) {
82 status->udp_status = DEVTOOL_UDP_WORKING;
83 } else if(!internal->status.reachable) {
84 status->udp_status = DEVTOOL_UDP_IMPOSSIBLE;
85 } else if(!internal->status.validkey) {
86 status->udp_status = DEVTOOL_UDP_UNKNOWN;
87 } else if(internal->status.udp_confirmed) {
88 status->udp_status = DEVTOOL_UDP_WORKING;
89 } else if(internal->mtuprobes > 30) {
90 status->udp_status = DEVTOOL_UDP_FAILED;
91 } else if(internal->mtuprobes > 0) {
92 status->udp_status = DEVTOOL_UDP_TRYING;
94 status->udp_status = DEVTOOL_UDP_UNKNOWN;
97 pthread_mutex_unlock(&mesh->mutex);
100 meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink_submesh_t **submeshes, size_t *nmemb) {
101 if(!mesh || !nmemb || (*nmemb && !submeshes)) {
102 meshlink_errno = MESHLINK_EINVAL;
106 meshlink_submesh_t **result;
109 if(pthread_mutex_lock(&mesh->mutex) != 0) {
113 *nmemb = mesh->submeshes->count;
114 result = realloc(submeshes, *nmemb * sizeof(*submeshes));
117 meshlink_submesh_t **p = result;
119 for list_each(submesh_t, s, mesh->submeshes) {
120 *p++ = (meshlink_submesh_t *)s;
125 meshlink_errno = MESHLINK_ENOMEM;
128 pthread_mutex_unlock(&mesh->mutex);
133 meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns) {
134 meshlink_open_params_t *params = meshlink_open_params_init(confbase, name, appname, devclass);
135 params->netns = dup(netns);
136 meshlink_handle_t *handle;
138 if(params->netns == -1) {
140 meshlink_errno = MESHLINK_EINVAL;
142 handle = meshlink_open_ex(params);
145 meshlink_open_params_free(params);
150 void devtool_force_sptps_renewal(meshlink_handle_t *mesh, meshlink_node_t *node) {
152 meshlink_errno = MESHLINK_EINVAL;
156 node_t *n = (node_t *)node;
157 connection_t *c = n->connection;
159 n->last_req_key = -3600;
162 c->last_key_renewal = -3600;
166 void devtool_set_meta_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
168 meshlink_errno = MESHLINK_EINVAL;
172 if(pthread_mutex_lock(&mesh->mutex) != 0) {
176 mesh->meta_status_cb = cb;
177 pthread_mutex_unlock(&mesh->mutex);