X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fdevtools.c;h=5991d70cbbe5a1fb7af0c0998b17e18a2ab82bc1;hb=1442d234fb6681e32b10348a6c7b226c11629203;hp=d4c00f139013fa1e0e69aa4d6bf173afc372cafb;hpb=b67296418c51784d39a24c3041e2cb199bee06f2;p=meshlink diff --git a/src/devtools.c b/src/devtools.c index d4c00f13..5991d70c 100644 --- a/src/devtools.c +++ b/src/devtools.c @@ -23,12 +23,25 @@ #include "logger.h" #include "meshlink_internal.h" #include "node.h" +#include "submesh.h" #include "splay_tree.h" #include "netutl.h" #include "xalloc.h" #include "devtools.h" +static void trybind_nop_probe(void) { + return; +} + +static void keyrotate_nop_probe(int stage) { + (void)stage; + return; +} + +void (*devtool_trybind_probe)(void) = trybind_nop_probe; +void (*devtool_keyrotate_probe)(int stage) = keyrotate_nop_probe; + /* Return an array of edges in the current network graph. * Data captures the current state and will not be updated. * Caller must deallocate data when done. @@ -185,7 +198,7 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) } char *host = NULL, *port = NULL, *address = NULL; - sockaddr2str((const sockaddr_t *) & (edges[i].address), &host, &port); + sockaddr2str((const sockaddr_t *)&edges[i].address, &host, &port); if(host && port) { xasprintf(&address, "{ \"host\": \"%s\", \"port\": %s }", host, port); @@ -237,3 +250,92 @@ done: return result; } + +void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) { + if(!mesh || !node || !status) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + node_t *internal = (node_t *)node; + + pthread_mutex_lock(&mesh->mesh_mutex); + + status->options = internal->options; + memcpy(&status->status, &internal->status, sizeof status->status); + memcpy(&status->address, &internal->address, sizeof status->address); + status->mtu = internal->mtu; + status->minmtu = internal->minmtu; + status->maxmtu = internal->maxmtu; + status->mtuprobes = internal->mtuprobes; + status->in_packets = internal->in_packets; + status->in_bytes = internal->in_bytes; + status->out_packets = internal->out_packets; + status->out_bytes = internal->out_bytes; + + // Derive UDP connection status + if(internal == mesh->self) { + status->udp_status = DEVTOOL_UDP_WORKING; + } else if(!internal->status.reachable) { + status->udp_status = DEVTOOL_UDP_IMPOSSIBLE; + } else if(!internal->status.validkey) { + status->udp_status = DEVTOOL_UDP_UNKNOWN; + } else if(internal->status.udp_confirmed) { + status->udp_status = DEVTOOL_UDP_WORKING; + } else if(internal->mtuprobes > 30) { + status->udp_status = DEVTOOL_UDP_FAILED; + } else if(internal->mtuprobes > 0) { + status->udp_status = DEVTOOL_UDP_TRYING; + } else { + status->udp_status = DEVTOOL_UDP_UNKNOWN; + } + + pthread_mutex_unlock(&mesh->mesh_mutex); +} + +meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink_submesh_t **submeshes, size_t *nmemb) { + if(!mesh || !nmemb || (*nmemb && !submeshes)) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + meshlink_submesh_t **result; + + //lock mesh->nodes + pthread_mutex_lock(&(mesh->mesh_mutex)); + + *nmemb = mesh->submeshes->count; + result = realloc(submeshes, *nmemb * sizeof(*submeshes)); + + if(result) { + meshlink_submesh_t **p = result; + + for list_each(submesh_t, s, mesh->submeshes) { + *p++ = (meshlink_submesh_t *)s; + } + } else { + *nmemb = 0; + free(submeshes); + meshlink_errno = MESHLINK_ENOMEM; + } + + pthread_mutex_unlock(&(mesh->mesh_mutex)); + + return result; +} +meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns) { + meshlink_open_params_t *params = meshlink_open_params_init(confbase, name, appname, devclass); + params->netns = dup(netns); + meshlink_handle_t *handle; + + if(params->netns == -1) { + handle = NULL; + meshlink_errno = MESHLINK_EINVAL; + } else { + handle = meshlink_open_ex(params); + } + + meshlink_open_params_free(params); + + return handle; +}