From 88b24fcfccc5802db199d3fac2cec659ac48ca78 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 3 Aug 2021 01:09:45 +0200 Subject: [PATCH] Add meshlink_get_node_tiny(). This allows querying if a node is connected using the tiny MeshLink version. --- src/meshlink++.h | 12 ++++++++++++ src/meshlink.c | 19 +++++++++++++++++++ src/meshlink.h | 12 ++++++++++++ src/meshlink.sym | 1 + src/node.h | 1 + src/protocol_auth.c | 1 + 6 files changed, 46 insertions(+) diff --git a/src/meshlink++.h b/src/meshlink++.h index 25fd34f3..fd663992 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -449,6 +449,18 @@ public: return meshlink_get_node_blacklisted(handle, node); } + /// Get the node's tiny status. + /** This function returns true if the given node is a tiny node. + * Note that the tiny status of a node will only be known if the node has been reachable at least once. + * + * @param node A pointer to a meshlink::node describing the node. + * + * @return This function returns true if the node is a tiny node. + */ + bool get_node_tiny(node *node) { + return meshlink_get_node_tiny(handle, node); + } + /// Get a handle for a specific submesh. /** This function returns a handle for the submesh with the given name. * diff --git a/src/meshlink.c b/src/meshlink.c index f153f757..71263b91 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -2562,6 +2562,25 @@ dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t return devclass; } +bool meshlink_get_node_tiny(meshlink_handle_t *mesh, meshlink_node_t *node) { + if(!mesh || !node) { + meshlink_errno = MESHLINK_EINVAL; + return -1; + } + + bool tiny; + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + tiny = ((node_t *)node)->status.tiny; + + pthread_mutex_unlock(&mesh->mutex); + + return tiny; +} + bool meshlink_get_node_blacklisted(meshlink_handle_t *mesh, meshlink_node_t *node) { if(!mesh) { meshlink_errno = MESHLINK_EINVAL; diff --git a/src/meshlink.h b/src/meshlink.h index a6c5b4b9..9a887168 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -835,6 +835,18 @@ struct meshlink_node **meshlink_get_all_nodes_by_blacklisted(struct meshlink_han */ dev_class_t meshlink_get_node_dev_class(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__)); +/// Get the node's tiny status. +/** This function returns true if the given node is a tiny node. + * Note that the tiny status of a node will only be known if the node has been reachable at least once. + * + * \memberof meshlink_node + * @param mesh A handle which represents an instance of MeshLink. + * @param node A pointer to a struct meshlink_node describing the node. + * + * @return This function returns true if the node is a tiny node. + */ +bool meshlink_get_node_tiny(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__)); + /// Get the node's blacklist status. /** This function returns the given node is blacklisted. * diff --git a/src/meshlink.sym b/src/meshlink.sym index 7c4de629..f598bcb6 100644 --- a/src/meshlink.sym +++ b/src/meshlink.sym @@ -51,6 +51,7 @@ meshlink_get_node_blacklisted meshlink_get_node_dev_class meshlink_get_node_reachability meshlink_get_node_submesh +meshlink_get_node_tiny meshlink_get_pmtu meshlink_get_port meshlink_get_self diff --git a/src/node.h b/src/node.h index 63f3c2c8..f88cde68 100644 --- a/src/node.h +++ b/src/node.h @@ -38,6 +38,7 @@ typedef struct node_status_t { uint16_t duplicate: 1; /* 1 if the node is duplicate, ie. multiple nodes using the same Name are online */ uint16_t dirty: 1; /* 1 if the configuration of the node is dirty and needs to be written out */ uint16_t want_udp: 1; /* 1 if we want working UDP because we have data to send */ + uint16_t tiny: 1; /* 1 if this is a tiny node */ } node_status_t; #define MAX_RECENT 5 diff --git a/src/protocol_auth.c b/src/protocol_auth.c index 80332428..0a107e4c 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -380,6 +380,7 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { n->devclass = devclass; n->status.dirty = true; + n->status.tiny = c->flags & PROTOCOL_TINY; n->last_successfull_connection = mesh->loop.now.tv_sec; -- 2.39.2