]> git.meshlink.io Git - meshlink/blobdiff - src/devtools.c
Modify meshlink configuration base file structre
[meshlink] / src / devtools.c
index d4c00f139013fa1e0e69aa4d6bf173afc372cafb..5991d70cbbe5a1fb7af0c0998b17e18a2ab82bc1 100644 (file)
 #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;
+}