X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;fp=src%2Fmeshlink.c;h=6f83c22fbd762a8bc68fd0c71b01af1aa1cdd844;hb=0d133a5ff230ad78da3208d32521d7549836187e;hp=8f0f82d1a8ae1c3afe5f94f0db22c134c2783ecd;hpb=de126fa104aaeabbc66b5dd59da9d07da2f0fd97;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 8f0f82d1..6f83c22f 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -54,6 +54,8 @@ __thread meshlink_errno_t meshlink_errno; meshlink_log_cb_t global_log_cb; meshlink_log_level_t global_log_level; +typedef bool (*search_node_by_condition_t)(const node_t *, const void *); + //TODO: this can go away completely const var_t variables[] = { /* Server configuration */ @@ -1433,20 +1435,13 @@ meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t *mesh, const char * 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; - } + //lock mesh->nodes + pthread_mutex_lock(&(mesh->mesh_mutex)); - s = (meshlink_submesh_t *)new_submesh(); - s->name = xstrdup(submesh); + s = (meshlink_submesh_t *)create_submesh(mesh, submesh); - submesh_add(mesh, (submesh_t *)s); + pthread_mutex_unlock(&(mesh->mesh_mutex)); - meshlink_errno = MESHLINK_OK; return s; } @@ -1889,12 +1884,7 @@ meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_ return result; } -meshlink_node_t **meshlink_get_all_nodes_by_dev_class(meshlink_handle_t *mesh, dev_class_t devclass, meshlink_node_t **nodes, size_t *nmemb) { - if(!mesh || ((int)devclass < 0) || (devclass > _DEV_CLASS_MAX) || !nmemb) { - meshlink_errno = MESHLINK_EINVAL; - return NULL; - } - +static meshlink_node_t **meshlink_get_all_nodes_by_condition(meshlink_handle_t *mesh, const void *condition, meshlink_node_t **nodes, size_t *nmemb, search_node_by_condition_t search_node) { meshlink_node_t **result; pthread_mutex_lock(&(mesh->mesh_mutex)); @@ -1902,7 +1892,7 @@ meshlink_node_t **meshlink_get_all_nodes_by_dev_class(meshlink_handle_t *mesh, d *nmemb = 0; for splay_each(node_t, n, mesh->nodes) { - if(n->devclass == devclass) { + if(true == search_node(n, condition)) { *nmemb = *nmemb + 1; } } @@ -1919,7 +1909,7 @@ meshlink_node_t **meshlink_get_all_nodes_by_dev_class(meshlink_handle_t *mesh, d meshlink_node_t **p = result; for splay_each(node_t, n, mesh->nodes) { - if(n->devclass == devclass) { + if(true == search_node(n, condition)) { *p++ = (meshlink_node_t *)n; } } @@ -1934,6 +1924,42 @@ meshlink_node_t **meshlink_get_all_nodes_by_dev_class(meshlink_handle_t *mesh, d return result; } +static bool search_node_by_dev_class(const node_t *node, const void *condition) { + dev_class_t *devclass = (dev_class_t *)condition; + + if(*devclass == node->devclass) { + return true; + } + + return false; +} + +static bool search_node_by_submesh(const node_t *node, const void *condition) { + if(condition == node->submesh) { + return true; + } + + return false; +} + +meshlink_node_t **meshlink_get_all_nodes_by_dev_class(meshlink_handle_t *mesh, dev_class_t devclass, meshlink_node_t **nodes, size_t *nmemb) { + if(!mesh || ((int)devclass < 0) || (devclass > _DEV_CLASS_MAX) || !nmemb) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + return meshlink_get_all_nodes_by_condition(mesh, &devclass, nodes, nmemb, search_node_by_dev_class); +} + +meshlink_node_t **meshlink_get_all_nodes_by_submesh(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, meshlink_node_t **nodes, size_t *nmemb) { + if(!mesh || !submesh || !nmemb) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + return meshlink_get_all_nodes_by_condition(mesh, submesh, nodes, nmemb, search_node_by_submesh); +} + dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t *node) { if(!mesh || !node) { meshlink_errno = MESHLINK_EINVAL; @@ -1951,6 +1977,21 @@ dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t return devclass; } +meshlink_submesh_t *meshlink_get_node_submesh(meshlink_handle_t *mesh, meshlink_node_t *node) { + if(!mesh || !node) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + node_t *n = (node_t *)node; + + meshlink_submesh_t *s; + + s = (meshlink_submesh_t *)n->submesh; + + return s; +} + bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen) { if(!mesh || !data || !len || !signature || !siglen) { meshlink_errno = MESHLINK_EINVAL; @@ -2274,6 +2315,8 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c meshlink_errno = MESHLINK_EINVAL; return NULL; } + } else { + s = (meshlink_submesh_t *)mesh->self->submesh; } pthread_mutex_lock(&(mesh->mesh_mutex));