]> git.meshlink.io Git - meshlink-tiny/blob - src/devtools.c
Remove support for submeshes.
[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 "splay_tree.h"
27 #include "netutl.h"
28 #include "xalloc.h"
29
30 #include "devtools.h"
31
32 static void nop_probe(void) {
33         return;
34 }
35
36 static void keyrotate_nop_probe(int stage) {
37         (void)stage;
38         return;
39 }
40
41 static void inviter_commits_first_nop_probe(bool stage) {
42         (void)stage;
43         return;
44 }
45
46 static void sptps_renewal_nop_probe(meshlink_node_t *node) {
47         (void)node;
48         return;
49 }
50
51 void (*devtool_trybind_probe)(void) = nop_probe;
52 void (*devtool_keyrotate_probe)(int stage) = keyrotate_nop_probe;
53 void (*devtool_set_inviter_commits_first)(bool inviter_commited_first) = inviter_commits_first_nop_probe;
54 void (*devtool_sptps_renewal_probe)(meshlink_node_t *node) = sptps_renewal_nop_probe;
55
56 void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
57         if(!mesh || !node || !status) {
58                 meshlink_errno = MESHLINK_EINVAL;
59                 return;
60         }
61
62         node_t *internal = (node_t *)node;
63
64         if(pthread_mutex_lock(&mesh->mutex) != 0) {
65                 abort();
66         }
67
68         memcpy(&status->status, &internal->status, sizeof status->status);
69         memcpy(&status->address, &internal->address, sizeof status->address);
70         status->mtu = internal->mtu;
71         status->minmtu = internal->minmtu;
72         status->maxmtu = internal->maxmtu;
73         status->mtuprobes = internal->mtuprobes;
74         status->in_packets = internal->in_packets;
75         status->in_bytes = internal->in_bytes;
76         status->out_packets = internal->out_packets;
77         status->out_bytes = internal->out_bytes;
78
79         // Derive UDP connection status
80         if(internal == mesh->self) {
81                 status->udp_status = DEVTOOL_UDP_WORKING;
82         } else if(!internal->status.reachable) {
83                 status->udp_status = DEVTOOL_UDP_IMPOSSIBLE;
84         } else if(!internal->status.validkey) {
85                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
86         } else if(internal->status.udp_confirmed) {
87                 status->udp_status = DEVTOOL_UDP_WORKING;
88         } else if(internal->mtuprobes > 30) {
89                 status->udp_status = DEVTOOL_UDP_FAILED;
90         } else if(internal->mtuprobes > 0) {
91                 status->udp_status = DEVTOOL_UDP_TRYING;
92         } else {
93                 status->udp_status = DEVTOOL_UDP_UNKNOWN;
94         }
95
96         pthread_mutex_unlock(&mesh->mutex);
97 }
98
99 meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns) {
100         meshlink_open_params_t *params = meshlink_open_params_init(confbase, name, appname, devclass);
101         params->netns = dup(netns);
102         meshlink_handle_t *handle;
103
104         if(params->netns == -1) {
105                 handle = NULL;
106                 meshlink_errno = MESHLINK_EINVAL;
107         } else {
108                 handle = meshlink_open_ex(params);
109         }
110
111         meshlink_open_params_free(params);
112
113         return handle;
114 }
115
116 void devtool_force_sptps_renewal(meshlink_handle_t *mesh, meshlink_node_t *node) {
117         if(!mesh || !node) {
118                 meshlink_errno = MESHLINK_EINVAL;
119                 return;
120         }
121
122         node_t *n = (node_t *)node;
123         connection_t *c = n->connection;
124
125         n->last_req_key = -3600;
126
127         if(c) {
128                 c->last_key_renewal = -3600;
129         }
130 }
131
132 void devtool_set_meta_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
133         if(!mesh) {
134                 meshlink_errno = MESHLINK_EINVAL;
135                 return;
136         }
137
138         if(pthread_mutex_lock(&mesh->mutex) != 0) {
139                 abort();
140         }
141
142         mesh->meta_status_cb = cb;
143         pthread_mutex_unlock(&mesh->mutex);
144 }