X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmeshlink.c;h=d1d8edcc52752261c208f0f84fc9901a366c3bc6;hb=refs%2Fheads%2Frchannel;hp=511a6d15546850ffe8b45d82b3c460bb75694cc2;hpb=62031be5f0a4b7e37a99cce43d0a77a67690e791;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 511a6d15..d1d8edcc 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -715,6 +715,7 @@ static bool meshlink_setup(meshlink_handle_t *mesh) { meshlink_handle_t *meshlink_open(const char *confbase, const char *name) { // Validate arguments provided by the application + bool usingname = false; if(!confbase || !*confbase) { fprintf(stderr, "No confbase given!\n"); @@ -723,19 +724,22 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) { if(!name || !*name) { fprintf(stderr, "No name given!\n"); - return NULL; + //return NULL; } + else { //check name only if there is a name != NULL - if(!check_id(name)) { - fprintf(stderr, "Invalid name given!\n"); - return NULL; + if(!check_id(name)) { + fprintf(stderr, "Invalid name given!\n"); + return NULL; + } else { usingname = true;} } meshlink_handle_t *mesh = xzalloc(sizeof *mesh); mesh->confbase = xstrdup(confbase); - mesh->name = xstrdup(name); + if (usingname) mesh->name = xstrdup(name); pthread_mutex_init ( &(mesh->outpacketqueue_mutex), NULL); pthread_mutex_init ( &(mesh->nodes_mutex), NULL); + mesh->threadstarted = false; event_loop_init(&mesh->loop); mesh->loop.data = mesh; @@ -791,6 +795,12 @@ void *meshlink_main_loop(void *arg) { bool meshlink_start(meshlink_handle_t *mesh) { // TODO: open listening sockets first + //Check that a valid name is set + if(!mesh->name ) { + fprintf(stderr, "No name given!\n"); + return false; + } + // Start the main thread if(pthread_create(&mesh->thread, NULL, meshlink_main_loop, mesh) != 0) { @@ -799,6 +809,8 @@ bool meshlink_start(meshlink_handle_t *mesh) { return false; } + mesh->threadstarted=true; + return true; } @@ -825,6 +837,8 @@ void meshlink_close(meshlink_handle_t *mesh) { exit_configuration(&mesh->config); event_loop_exit(&mesh->loop); + free(mesh); + #ifdef HAVE_MINGW WSACleanup(); #endif @@ -1171,6 +1185,11 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { char *b64key = ecdsa_get_base64_public_key(key); + //Before doing meshlink_join make sure we are not connected to another mesh + if ( mesh->threadstarted ){ + goto invalid; + } + // Connect to the meshlink daemon mentioned in the URL. struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM); if(!ai) @@ -1261,7 +1280,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { return true; invalid: - fprintf(stderr, "Invalid invitation URL.\n"); + fprintf(stderr, "Invalid invitation URL or you are already connected to a Mesh ?\n"); return false; } @@ -1351,6 +1370,28 @@ void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) { } +meshlink_connection_t* meshlink_open_rchannel(meshlink_handle_t *mesh, meshlink_node_t *destination, uint16_t dport) { + +//TODO: check in mesh->socketsbitmap for a free descriptor and allocate a source port for this connection + + int descriptor=-1; + + for (int i=0; i < 1024 ;i++) { + if (mesh->socketsbitmap.bitValues[i].bit == 0) { + mesh->socketsbitmap.bitValues[i].bit=1; + descriptor=i; + break; + } + } + + if (descriptor == -1 ) return NULL; + +//TODO: register a callback for the data that will be received from this connection + +//TODO: return a description value that the user will use to write on this channel +return NULL; +} + static void __attribute__((constructor)) meshlink_init(void) { crypto_init(); }