return;
}
- invitation = meshlink_invite(mesh, arg);
+ invitation = meshlink_invite(mesh, NULL, arg);
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
return;
}
- invitation = meshlink_invite(mesh, arg);
+ invitation = meshlink_invite(mesh, NULL, arg);
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
return;
}
- invitation = mesh->invite(arg);
+ invitation = mesh->invite(NULL, arg);
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
return;
}
- invitation = meshlink_invite(mesh[nodeindex], arg);
+ invitation = meshlink_invite(mesh[nodeindex], NULL, arg);
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
* @return This function returns a string that contains the invitation URL.
* The application should call free() after it has finished using the URL.
*/
- char *invite(const char *name, uint32_t flags = 0) {
- return meshlink_invite_ex(handle, name, flags);
+ char *invite(submesh *submesh, const char *name, uint32_t flags = 0) {
+ return meshlink_invite_ex(handle, submesh, name, flags);
}
/// Use an invitation to join a mesh.
{"ConnectTo", VAR_SERVER | VAR_MULTIPLE | VAR_SAFE},
{"Name", VAR_SERVER},
/* Host configuration */
+ {"SubMesh", VAR_HOST | VAR_SAFE},
{"CanonicalAddress", VAR_HOST},
{"Address", VAR_HOST | VAR_MULTIPLE},
{"ECDSAPublicKey", VAR_HOST},
mesh->discovery = true;
mesh->invitation_timeout = 604800; // 1 week
mesh->netns = params->netns;
+ mesh->submeshes = NULL;
if(usingname) {
mesh->name = xstrdup(params->name);
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;
s = (meshlink_submesh_t *)lookup_submesh(mesh, submesh);
- if (s) {
+ if(s) {
logger(NULL, MESHLINK_ERROR, "SubMesh Already exists!\n");
meshlink_errno = MESHLINK_EEXIST;
return NULL;
mesh->invitation_timeout = timeout;
}
-char *meshlink_invite_ex(meshlink_handle_t *mesh, const char *name, uint32_t flags) {
+char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name, uint32_t flags) {
+ meshlink_submesh_t *s = NULL;
+
if(!mesh) {
meshlink_errno = MESHLINK_EINVAL;
return NULL;
}
+ if(submesh) {
+ s = (meshlink_submesh_t *)lookup_submesh(mesh, submesh->name);
+
+ if(s != submesh) {
+ logger(mesh, MESHLINK_DEBUG, "Invalid SubMesh Handle.\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+ }
+
pthread_mutex_lock(&(mesh->mesh_mutex));
// Check validity of the new node's name
// Fill in the details.
fprintf(f, "Name = %s\n", name);
+
+ if(s) {
+ fprintf(f, "SubMesh = %s\n", s->name);
+ }
+
fprintf(f, "ConnectTo = %s\n", mesh->self->name);
// Copy Broadcast and Mode
return url;
}
-char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
- return meshlink_invite_ex(mesh, name, 0);
+char *meshlink_invite(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name) {
+ return meshlink_invite_ex(mesh, submesh, name, 0);
}
bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
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
+/** 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.
* The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
*
* @param mesh A handle which represents an instance of MeshLink.
+ * @param submesh A handle which represents an instance of SubMesh.
* @param name A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
* After this function returns, the application is free to overwrite or free @a name @a.
* @param flags A bitwise-or'd combination of flags that controls how the URL is generated.
* @return This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
* The application should call free() after it has finished using the URL.
*/
-extern char *meshlink_invite_ex(meshlink_handle_t *mesh, const char *name, uint32_t flags);
+extern char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name, uint32_t flags);
/// Invite another node into the mesh.
/** This function generates an invitation that can be used by another node to join the same mesh as the local node.
* Calling this function is equal to callen meshlink_invite_ex() with flags set to 0.
*
* @param mesh A handle which represents an instance of MeshLink.
+ * @param submesh A handle which represents an instance of SubMesh.
* @param name A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
* After this function returns, the application is free to overwrite or free @a name @a.
*
* @return This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
* The application should call free() after it has finished using the URL.
*/
-extern char *meshlink_invite(meshlink_handle_t *mesh, const char *name);
+extern char *meshlink_invite(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name);
/// Use an invitation to join a mesh.
/** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
#include "route.h"
#include "utils.h"
#include "xalloc.h"
+#include "submesh.h"
bool node_read_ecdsa_public_key(meshlink_handle_t *mesh, node_t *n) {
if(ecdsa_active(n->ecdsa)) {
return n->devclass != 0;
}
+bool node_read_submesh(meshlink_handle_t *mesh, node_t *n) {
+
+ splay_tree_t *config_tree;
+ char *p;
+
+ init_configuration(&config_tree);
+
+ if(!read_host_config(mesh, config_tree, n->name)) {
+ goto exit;
+ }
+
+ if(get_config_string(lookup_config(config_tree, "SubMesh"), &p)) {
+ n->submesh = NULL;
+
+ for list_each(submesh_t, s, mesh->submeshes) {
+ if(!strcmp(p, s->name)) {
+ n->submesh = s;
+ break;
+ }
+ }
+
+ if(!n->submesh) {
+ n->submesh = (submesh_t *)meshlink_submesh_open(mesh, p);
+ }
+
+ free(p);
+ }
+
+exit:
+ exit_configuration(&config_tree);
+ return n->submesh != NULL;
+}
+
bool node_write_devclass(meshlink_handle_t *mesh, node_t *n) {
if((int)n->devclass < 0 || n->devclass > _DEV_CLASS_MAX) {
return result;
}
+bool node_write_submesh(meshlink_handle_t *mesh, node_t *n) {
+
+ if(!n->submesh) {
+ return false;
+ }
+
+ bool result = false;
+
+ splay_tree_t *config_tree;
+ init_configuration(&config_tree);
+
+ // ignore read errors; in case the file does not exist we will create it
+ read_host_config(mesh, config_tree, n->name);
+
+ config_t *cnf = lookup_config(config_tree, "SubMesh");
+
+ if(!cnf) {
+ cnf = new_config();
+ cnf->variable = xstrdup("SubMesh");
+ config_add(config_tree, cnf);
+ }
+
+ set_config_string(cnf, n->submesh->name);
+
+ if(!write_host_config(mesh, config_tree, n->name)) {
+ goto fail;
+ }
+
+ result = true;
+
+fail:
+ exit_configuration(&config_tree);
+ return result;
+}
+
void load_all_nodes(meshlink_handle_t *mesh) {
DIR *dir;
struct dirent *ent;
node_t *n = lookup_node(mesh, ent->d_name);
if(n) {
+ if(n == mesh->self && !n->submesh) {
+ node_read_submesh(mesh, n);
+ }
+
continue;
}
n = new_node();
n->name = xstrdup(ent->d_name);
node_read_devclass(mesh, n);
+ node_read_submesh(mesh, n);
node_add(mesh, n);
}
*/
bool setup_network(meshlink_handle_t *mesh) {
init_connections(mesh);
+ init_submeshes(mesh);
init_nodes(mesh);
init_edges(mesh);
init_requests(mesh);
dev_class_t devclass;
struct meshlink_handle *mesh; /* The mesh this node belongs to */
- struct submesh_t *submesh; /* Nodes Sub-Mesh Handle*/
+ 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 */
submesh_t *lookup_submesh(struct meshlink_handle *mesh, const char *submesh_name) {
submesh_t *submesh = NULL;
- if ( !mesh->submeshes ) {
+ if(!mesh->submeshes) {
return NULL;
}
for list_each(submesh_t, s, mesh->submeshes) {
- if ( !strcmp(submesh_name, s->name) ) {
+ if(!strcmp(submesh_name, s->name)) {
submesh = s;
break;
}
char *name; /* name of this Sub-Mesh */
void *priv;
- struct meshlink_handle *mesh; /* the mesh this submesh belongs to */
+ struct meshlink_handle *mesh; /* the mesh this submesh belongs to */
} submesh_t;
extern void init_submeshes(struct meshlink_handle *mesh);