From: Lakshminarayana Gurram Date: Wed, 6 Feb 2019 02:33:35 +0000 (+0530) Subject: Added meshlink_submesh_open X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=ed1f31db1e8840ffd6849b48bcad93e9975a24bb;p=meshlink Added meshlink_submesh_open --- diff --git a/src/Makefile.am b/src/Makefile.am index ef0d8d9e..f793e912 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -64,6 +64,7 @@ libmeshlink_la_SOURCES = \ net_socket.c \ netutl.c netutl.h \ node.c node.h \ + submesh.c submesh.h \ prf.c prf.h \ protocol.c protocol.h \ protocol_auth.c \ diff --git a/src/meshlink++.h b/src/meshlink++.h index 16a6e366..f7b0e1ac 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -27,6 +27,7 @@ namespace meshlink { class mesh; class node; class channel; +class submesh; /// Severity of log messages generated by MeshLink. typedef meshlink_log_level_t log_level_t; @@ -94,6 +95,10 @@ typedef void (*channel_poll_cb_t)(mesh *mesh, channel *channel, size_t len); class node: public meshlink_node_t { }; +/// A class describing a MeshLink Sub-Mesh. +class submesh: public meshlink_submesh_t { +}; + /// A class describing a MeshLink channel. class channel: public meshlink_channel_t { public: diff --git a/src/meshlink.c b/src/meshlink.c index 815acdea..0ea38dbb 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -37,6 +37,7 @@ typedef struct { #include "meshlink_internal.h" #include "netutl.h" #include "node.h" +#include "submesh.h" #include "protocol.h" #include "route.h" #include "sockaddr.h" @@ -1415,6 +1416,37 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { return mesh; } +meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t *mesh, const char *submesh) { + meshlink_submesh_t *s = NULL; + if(!mesh) { + logger(NULL, MESHLINK_ERROR, "No mesh handle given!\n"); + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + if(!submesh || !*submesh) { + logger(NULL, MESHLINK_ERROR, "No submesh name given!\n"); + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + s = (meshlink_submesh_t *)lookup_submesh(mesh, submesh); + + if (s) { + logger(NULL, MESHLINK_ERROR, "SubMesh Already exists!\n"); + meshlink_errno = MESHLINK_EEXIST; + return NULL; + } + + s = (meshlink_submesh_t *)new_submesh(); + s->name = xstrdup(submesh); + + submesh_add(mesh, (submesh_t *)s); + + meshlink_errno = MESHLINK_OK; + return s; +} + static void *meshlink_main_loop(void *arg) { meshlink_handle_t *mesh = arg; diff --git a/src/meshlink.h b/src/meshlink.h index 35b9bd64..6e03343b 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -54,6 +54,9 @@ typedef struct meshlink_channel meshlink_channel_t; /// A struct containing all parameters used for opening a mesh. typedef struct meshlink_open_params meshlink_open_params_t; +/// A handle for a MeshLink sub-mesh. +typedef struct meshlink_submesh meshlink_submesh_t; + /// Code of most recent error encountered. typedef enum { MESHLINK_OK, ///< Everything is fine @@ -67,7 +70,7 @@ typedef enum { MESHLINK_ENETWORK, ///< MeshLink encountered a network error MESHLINK_EPEER, ///< A peer caused an error MESHLINK_ENOTSUP, ///< The operation is not supported in the current configuration of MeshLink - MESHLINK_EBUSY, ///< The MeshLink instance is already in use by another process + MESHLINK_EBUSY ///< The MeshLink instance is already in use by another process } meshlink_errno_t; /// Device class @@ -114,6 +117,11 @@ struct meshlink_node { void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink. }; +struct meshlink_submesh { + const char *const name; ///< Textual name of this Sub-Mesh. It is stored in a nul-terminated C string, which is allocated by MeshLink. + void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink. +}; + struct meshlink_channel { struct meshlink_node *const node; ///< Pointer to the peer of this channel. void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink. @@ -210,6 +218,21 @@ extern meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) */ extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass); +/// Create Sub-Mesh. +/** This function causes MeshLink to open a new Sub-Mesh network + * create a new thread, which will handle all network I/O. + * + * It is allowed to call this function even if MeshLink is already started, in which case it will return true. + * + * @param mesh A handle which represents an instance of MeshLink. + * + * @param submesh Name of the new Sub-Mesh to create. + * + * @return A pointer to a meshlink_submesh_t which represents this instance of SubMesh, or NULL in case of an error. + * The pointer is valid until meshlink_close() is called. + */ +meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t *mesh, const char *submesh); + /// Start MeshLink. /** This function causes MeshLink to open network sockets, make outgoing connections, and * create a new thread, which will handle all network I/O. @@ -328,7 +351,7 @@ typedef enum { MESHLINK_INFO, ///< Informational messages. MESHLINK_WARNING, ///< Warnings which might indicate problems, but which are not real errors. MESHLINK_ERROR, ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely. - MESHLINK_CRITICAL, ///< Critical errors which cause MeshLink to fail completely. + MESHLINK_CRITICAL ///< Critical errors which cause MeshLink to fail completely. } meshlink_log_level_t; /// A callback for receiving log messages generated by MeshLink. diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index f576eb12..45c6c951 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -101,6 +101,7 @@ struct meshlink_handle { struct list_t *connections; struct list_t *outgoings; + struct list_t *submeshes; meshlink_queue_t outpacketqueue; @@ -164,6 +165,12 @@ struct meshlink_node { void *priv; }; +/// A handle for a node Sub-Mesh. +struct meshlink_submesh { + const char *name; + void *priv; +}; + /// A channel. struct meshlink_channel { struct node_t *node; diff --git a/src/node.h b/src/node.h index eaba79d0..acbf2e8b 100644 --- a/src/node.h +++ b/src/node.h @@ -24,6 +24,7 @@ #include "sockaddr.h" #include "sptps.h" #include "utcp/utcp.h" +#include "submesh.h" typedef struct node_status_t { unsigned int unused_active: 1; /* 1 if active (not used for nodes) */ @@ -49,6 +50,7 @@ typedef struct node_t { dev_class_t devclass; struct meshlink_handle *mesh; /* The mesh this node belongs to */ + struct submesh_t *submesh; /* Nodes Sub-Mesh Handle*/ int sock; /* Socket to use for outgoing UDP packets */ sockaddr_t address; /* his real (internet) ip to send UDP packets to */ diff --git a/src/submesh.c b/src/submesh.c new file mode 100644 index 00000000..0a2bfb09 --- /dev/null +++ b/src/submesh.c @@ -0,0 +1,82 @@ +/* + node.c -- node tree management + Copyright (C) 2014 Guus Sliepen , + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "system.h" + +#include "hash.h" +#include "logger.h" +#include "meshlink_internal.h" +#include "net.h" +#include "netutl.h" +#include "submesh.h" +#include "splay_tree.h" +#include "utils.h" +#include "xalloc.h" + +void init_submeshes(meshlink_handle_t *mesh) { + mesh->submeshes = list_alloc((list_action_t)free_submesh); +} + +void exit_submeshes(meshlink_handle_t *mesh) { + list_delete_list(mesh->submeshes); + mesh->submeshes = NULL; +} + +submesh_t *new_submesh(void) { + submesh_t *s = xzalloc(sizeof(*s)); + + s->name = NULL; + s->priv = NULL; + + return s; +} + +void free_submesh(submesh_t *s) { + if(s->name) { + free(s->name); + } + + free(s); +} + +void submesh_add(meshlink_handle_t *mesh, submesh_t *s) { + s->mesh = mesh; + list_insert_tail(mesh->submeshes, (void *)s); +} + +void submesh_del(meshlink_handle_t *mesh, submesh_t *s) { + list_delete(mesh->submeshes, (void *)s); +} + +submesh_t *lookup_submesh(struct meshlink_handle *mesh, const char *submesh_name) { + submesh_t *submesh = NULL; + + if ( !mesh->submeshes ) { + return NULL; + } + + for list_each(submesh_t, s, mesh->submeshes) { + if ( !strcmp(submesh_name, s->name) ) { + submesh = s; + break; + } + } + + return submesh; +} \ No newline at end of file diff --git a/src/submesh.h b/src/submesh.h new file mode 100644 index 00000000..958ce9c7 --- /dev/null +++ b/src/submesh.h @@ -0,0 +1,40 @@ +#ifndef MESHLINK_SUBMESH_H +#define MESHLINK_SUBMESH_H + +/* + submesh.h -- header for node.c + Copyright (C) 2014, 2017 Guus Sliepen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "meshlink_internal.h" + +typedef struct submesh_t { + char *name; /* name of this Sub-Mesh */ + void *priv; + + struct meshlink_handle *mesh; /* the mesh this submesh belongs to */ +} submesh_t; + +extern void init_submeshes(struct meshlink_handle *mesh); +extern void exit_submeshes(struct meshlink_handle *mesh); +extern submesh_t *new_submesh(void) __attribute__((__malloc__)); +extern void free_submesh(submesh_t *); +extern void submesh_add(struct meshlink_handle *mesh, submesh_t *); +extern void submesh_del(struct meshlink_handle *mesh, submesh_t *); +extern submesh_t *lookup_submesh(struct meshlink_handle *mesh, const char *); + +#endif