X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fdevtools.c;h=2a98e595054850a78fecd489058acba6914db846;hb=HEAD;hp=2aba935b07e14a8563bbd8b220457f34c74007c9;hpb=3be622ad230c70e9753f9f9737333a2f803b125e;p=meshlink diff --git a/src/devtools.c b/src/devtools.c index 2aba935b..12c5432e 100644 --- a/src/devtools.c +++ b/src/devtools.c @@ -34,8 +34,26 @@ static void nop_probe(void) { return; } +static void keyrotate_nop_probe(int stage) { + (void)stage; + return; +} + +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) = 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. @@ -47,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; @@ -56,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; } @@ -67,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; } @@ -81,7 +101,6 @@ devtool_edge_t *devtool_get_all_edges(meshlink_handle_t *mesh, devtool_edge_t *e p->from = (meshlink_node_t *)e->from; p->to = (meshlink_node_t *)e->to; p->address = e->address.storage; - p->options = e->options; p->weight = e->weight; n++; @@ -89,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) { @@ -111,20 +132,14 @@ static bool fstrwrite(const char *str, FILE *stream) { return true; } -static const char *__itoa(int value) { - static char buffer[sizeof(int) * 8 + 1]; // not thread safe - - if(snprintf(buffer, sizeof(buffer), "%d", value) == -1) { - return ""; - } - - return buffer; -} - 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; @@ -147,6 +162,8 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) goto fail; } + char buf[16]; + for(size_t i = 0; i < node_count; ++i) { if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream)) { goto fail; @@ -156,11 +173,9 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) goto fail; } - if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->options), stream) || !fstrwrite(",\n", stream)) { - goto fail; - } + snprintf(buf, sizeof(buf), "%d", ((node_t *)nodes[i])->devclass); - if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->devclass), stream) || !fstrwrite("\n", stream)) { + if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) { goto fail; } @@ -209,11 +224,9 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) free(address); - if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(edges[i].options), stream) || !fstrwrite(",\n", stream)) { - goto fail; - } + snprintf(buf, sizeof(buf), "%d", edges[i].weight); - if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(__itoa(edges[i].weight), stream) || !fstrwrite("\n", stream)) { + if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(buf, stream) || !fstrwrite("\n", stream)) { goto fail; } @@ -241,51 +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); - - 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; +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) { @@ -297,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)); @@ -314,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); @@ -334,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); +}