From: Guus Sliepen Date: Wed, 28 Oct 2015 21:57:49 +0000 (+0100) Subject: Add meshlink_get_self(). X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=e80127addf7a0a0a67e54bac9b19d0922b50e67e;p=meshlink Add meshlink_get_self(). This returns a handle for the local node. This can also be used to determine our own name using meshlink_get_self(mesh)->name. --- diff --git a/src/meshlink++.h b/src/meshlink++.h index 8e0745e4..34654901 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -274,6 +274,15 @@ namespace meshlink { return (node *)meshlink_get_node(handle, name); } + /// Get a handle for our own node. + /** This function returns a handle for the local node. + * + * @return A pointer to a meshlink::node which represents the local node. + */ + node *get_self() { + return (node *)meshlink_get_self(handle); + } + /// Get a list of all nodes. /** This function returns a list with handles for all known nodes. * diff --git a/src/meshlink.c b/src/meshlink.c index e9250e25..1d682c40 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1233,6 +1233,15 @@ char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node) { return fingerprint; } +meshlink_node_t *meshlink_get_self(meshlink_handle_t *mesh) { + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + return (meshlink_node_t *)mesh->self; +} + meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) { if(!mesh || !name) { meshlink_errno = MESHLINK_EINVAL; diff --git a/src/meshlink.h b/src/meshlink.h index cca5db1a..651f05c8 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -329,6 +329,16 @@ extern bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, */ extern ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination); +/// Get a handle for our own node. +/** This function returns a handle for the local node. + * + * @param mesh A handle which represents an instance of MeshLink. + * + * @return A pointer to a meshlink_node_t which represents the local node. + * The pointer is guaranteed to be valid until meshlink_close() is called. + */ +extern meshlink_node_t *meshlink_get_self(meshlink_handle_t *mesh); + /// Get a handle for a specific node. /** This function returns a handle for the node with the given name. * diff --git a/test/basic.c b/test/basic.c index 93b7f5e9..d3ac1373 100644 --- a/test/basic.c +++ b/test/basic.c @@ -16,7 +16,7 @@ int main(int argc, char *argv[]) { // Check that our own node exists. - meshlink_node_t *self = meshlink_get_node(mesh, "foo"); + meshlink_node_t *self = meshlink_get_self(mesh); if(!self) { fprintf(stderr, "Foo does not know about itself\n"); return 1; @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) { return 1; } - self = meshlink_get_node(mesh, "foo"); + self = meshlink_get_self(mesh); if(!self) { fprintf(stderr, "Foo doesn't know about itself the second time\n"); return 1; diff --git a/test/basicpp.cpp b/test/basicpp.cpp index c52e2ddb..02d4cb61 100644 --- a/test/basicpp.cpp +++ b/test/basicpp.cpp @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) { // Check that our own node exists. - meshlink::node *self = mesh.get_node("foo"); + meshlink::node *self = mesh.get_self(); if(!self) { cerr << "Foo does not know about itself\n"; return 1; @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) { return 1; } - self = mesh.get_node("foo"); + self = mesh.get_self(); if(!self) { cerr << "Foo doesn't know about itself the second time\n"; return 1;