]> git.meshlink.io Git - meshlink/blobdiff - src/devtools.c
Never automatically try to bind to ports >= 32768.
[meshlink] / src / devtools.c
index 629d90bd3c049f9858741c4e31a33fd2808a7c51..12c5432e2ca8dbf9ae7ece2accb612fa543c15e6 100644 (file)
@@ -30,7 +30,7 @@
 
 #include "devtools.h"
 
-static void trybind_nop_probe(void) {
+static void nop_probe(void) {
        return;
 }
 
@@ -39,8 +39,21 @@ static void keyrotate_nop_probe(int stage) {
        return;
 }
 
-void (*devtool_trybind_probe)(void) = trybind_nop_probe;
+static void inviter_commits_first_nop_probe(bool stage) {
+       (void)stage;
+       return;
+}
+
+static void sptps_renewal_nop_probe(meshlink_node_t *node) {
+       (void)node;
+       return;
+}
+
+void (*devtool_trybind_probe)(void) = nop_probe;
 void (*devtool_keyrotate_probe)(int stage) = keyrotate_nop_probe;
+void (*devtool_set_inviter_commits_first)(bool inviter_commited_first) = inviter_commits_first_nop_probe;
+void (*devtool_adns_resolve_probe)(void) = nop_probe;
+void (*devtool_sptps_renewal_probe)(meshlink_node_t *node) = sptps_renewal_nop_probe;
 
 /* Return an array of edges in the current network graph.
  * Data captures the current state and will not be updated.
@@ -52,7 +65,9 @@ devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *e
                return NULL;
        }
 
-       pthread_mutex_lock(&(mesh->mesh_mutex));
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
 
        devtool_edge_t *result = NULL;
        unsigned int result_size = 0;
@@ -61,7 +76,7 @@ devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *e
 
        // if result is smaller than edges, we have to dealloc all the excess devtool_edge_t
        if((size_t)result_size > *nmemb) {
-               result = realloc(edges, result_size * sizeof(*result));
+               result = xrealloc(edges, result_size * sizeof(*result));
        } else {
                result = edges;
        }
@@ -72,7 +87,7 @@ devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *e
 
                for splay_each(edge_t, e, mesh->edges) {
                        // skip edges that do not represent a two-directional connection
-                       if((!e->reverse) || (e->reverse->to != e->from)) {
+                       if(!e->reverse || e->reverse->to != e->from) {
                                continue;
                        }
 
@@ -93,19 +108,21 @@ devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *e
                }
 
                // shrink result to the actual amount of memory used
-               result = realloc(result, n * sizeof(*result));
+               result = xrealloc(result, n * sizeof(*result));
                *nmemb = n;
        } else {
                *nmemb = 0;
                meshlink_errno = MESHLINK_ENOMEM;
        }
 
-       pthread_mutex_unlock(&(mesh->mesh_mutex));
+       pthread_mutex_unlock(&mesh->mutex);
 
        return result;
 }
 
 static bool fstrwrite(const char *str, FILE *stream) {
+       assert(stream);
+
        size_t len = strlen(str);
 
        if(fwrite((void *)str, 1, len, stream) != len) {
@@ -116,9 +133,13 @@ static bool fstrwrite(const char *str, FILE *stream) {
 }
 
 bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) {
+       assert(stream);
+
        bool result = true;
 
-       pthread_mutex_lock(&(mesh->mesh_mutex));
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
 
        // export edges and nodes
        size_t node_count = 0;
@@ -233,50 +254,78 @@ done:
        free(nodes);
        free(edges);
 
-       pthread_mutex_unlock(&(mesh->mesh_mutex));
+       pthread_mutex_unlock(&mesh->mutex);
 
        return result;
 }
 
+static void devtool_get_reset_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status, bool reset) {
+       node_t *internal = (node_t *)node;
+
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
+
+       if(status) {
+               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_data = internal->in_data;
+               status->out_data = internal->out_data;
+               status->in_forward = internal->in_forward;
+               status->out_forward = internal->out_forward;
+               status->in_meta = internal->in_meta;
+               status->out_meta = internal->out_meta;
+
+               // 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;
+               }
+       }
+
+       if(reset) {
+               internal->in_data = 0;
+               internal->out_data = 0;
+               internal->in_forward = 0;
+               internal->out_forward = 0;
+               internal->in_meta = 0;
+               internal->out_meta = 0;
+       }
+
+       pthread_mutex_unlock(&mesh->mutex);
+}
+
 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;
+       devtool_get_reset_node_status(mesh, node, status, false);
+}
 
-       pthread_mutex_lock(&mesh->mesh_mutex);
-
-       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;
+void devtool_reset_node_counters(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
+       if(!mesh || !node) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
        }
 
-       pthread_mutex_unlock(&mesh->mesh_mutex);
+       devtool_get_reset_node_status(mesh, node, status, true);
 }
 
 meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink_submesh_t **submeshes, size_t *nmemb) {
@@ -288,7 +337,9 @@ meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink
        meshlink_submesh_t **result;
 
        //lock mesh->nodes
-       pthread_mutex_lock(&(mesh->mesh_mutex));
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
 
        *nmemb = mesh->submeshes->count;
        result = realloc(submeshes, *nmemb * sizeof(*submeshes));
@@ -305,10 +356,11 @@ meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, meshlink
                meshlink_errno = MESHLINK_ENOMEM;
        }
 
-       pthread_mutex_unlock(&(mesh->mesh_mutex));
+       pthread_mutex_unlock(&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);
@@ -325,3 +377,33 @@ meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name,
 
        return handle;
 }
+
+void devtool_force_sptps_renewal(meshlink_handle_t *mesh, meshlink_node_t *node) {
+       if(!mesh || !node) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
+       }
+
+       node_t *n = (node_t *)node;
+       connection_t *c = n->connection;
+
+       n->last_req_key = -3600;
+
+       if(c) {
+               c->last_key_renewal = -3600;
+       }
+}
+
+void devtool_set_meta_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
+       }
+
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
+
+       mesh->meta_status_cb = cb;
+       pthread_mutex_unlock(&mesh->mutex);
+}