]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Merge branch 'discovery' into everbase
[meshlink] / src / meshlink.c
index c25e08c5c72a25da131ed0c11cc32dcba239744a..79db920b6a9d39eef024f6ee56c692679024925b 100644 (file)
@@ -47,6 +47,8 @@ typedef struct {
 #define MSG_NOSIGNAL 0
 #endif
 
+static pthread_mutex_t global_mutex;
+
 __thread meshlink_errno_t meshlink_errno;
 
 //TODO: this can go away completely
@@ -350,12 +352,7 @@ static bool try_bind(int port) {
 }
 
 static int check_port(meshlink_handle_t *mesh) {
-       if(try_bind(655))
-               return 655;
-
-       fprintf(stderr, "Warning: could not bind to port 655.\n");
-
-       for(int i = 0; i < 100; i++) {
+       for(int i = 0; i < 1000; i++) {
                int port = 0x1000 + (rand() & 0x7fff);
                if(try_bind(port)) {
                        char filename[PATH_MAX];
@@ -368,7 +365,6 @@ static int check_port(meshlink_handle_t *mesh) {
 
                        fprintf(f, "Port = %d\n", port);
                        fclose(f);
-                       fprintf(stderr, "MeshLink will instead listen on port %d.\n", port);
                        return port;
                }
        }
@@ -743,7 +739,12 @@ static bool meshlink_setup(meshlink_handle_t *mesh) {
        return true;
 }
 
-meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
+meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname) {
+       return meshlink_open_with_size(confbase, name, appname, sizeof(meshlink_handle_t));
+}
+
+meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, size_t size) {
+
        // Validate arguments provided by the application
        bool usingname = false;
 
@@ -753,6 +754,12 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
                return NULL;
        }
 
+       if(!appname || !*appname) {
+               fprintf(stderr, "No appname given!\n");
+               meshlink_errno = MESHLINK_EINVAL;
+               return NULL;
+       }
+
        if(!name || !*name) {
                fprintf(stderr, "No name given!\n");
                //return NULL;
@@ -766,10 +773,10 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
                } else { usingname = true;}
        }
 
-       meshlink_handle_t *mesh = xzalloc(sizeof *mesh);
+       meshlink_handle_t *mesh = xzalloc(size);
        mesh->confbase = xstrdup(confbase);
+       mesh->appname = xstrdup(appname);
        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);
@@ -830,12 +837,17 @@ static void *meshlink_main_loop(void *arg) {
 
        try_outgoing_connections(mesh);
 
+       fprintf(stderr, "Starting main_loop...\n");
        main_loop(mesh);
+       fprintf(stderr, "main_loop returned.\n");
 
        return NULL;
 }
 
 bool meshlink_start(meshlink_handle_t *mesh) {
+
+       fprintf(stderr, "meshlink_start called\n");
+
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
                return false;
@@ -861,14 +873,15 @@ bool meshlink_start(meshlink_handle_t *mesh) {
 
        mesh->threadstarted=true;
 
-       // Start discovery
-       if(!discovery_start(mesh))
-               return false;
+       discovery_start(mesh);
 
        return true;
 }
 
 void meshlink_stop(meshlink_handle_t *mesh) {
+
+       fprintf(stderr, "meshlink_stop called\n");
+       
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
                return;
@@ -877,16 +890,25 @@ void meshlink_stop(meshlink_handle_t *mesh) {
        // Stop discovery
        discovery_stop(mesh);
 
-       // Shut down the listening sockets to signal the main thread to shut down
+       // Shut down a listening socket to signal the main thread to shut down
 
-       for(int i = 0; i < mesh->listen_sockets; i++) {
-               shutdown(mesh->listen_socket[i].tcp.fd, SHUT_RDWR);
-               shutdown(mesh->listen_socket[i].udp.fd, SHUT_RDWR);
-       }
+       listen_socket_t *s = &mesh->listen_socket[0];
+       shutdown(s->tcp.fd, SHUT_RDWR);
 
        // Wait for the main thread to finish
 
        pthread_join(mesh->thread, NULL);
+       mesh->threadstarted = false;
+
+       // Fix the socket
+       
+       closesocket(s->tcp.fd);
+       io_del(&mesh->loop, &s->tcp);
+       s->tcp.fd = setup_listen_socket(&s->sa);
+       if(s->tcp.fd < 0)
+               logger(DEBUG_ALWAYS, LOG_ERR, "Could not repair listenen socket!");
+       else
+               io_add(&mesh->loop, &s->tcp, handle_new_meta_connection, s, s->tcp.fd, IO_READ);
 }
 
 void meshlink_close(meshlink_handle_t *mesh) {
@@ -961,19 +983,15 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const
                return false;
        }
 
-       /* If there is no outgoing list yet, create one. */
-
-       if(!mesh->outpacketqueue)
-               mesh->outpacketqueue = list_alloc(NULL);
-
        //add packet to the queue
        outpacketqueue_t *packet_in_queue = xzalloc(sizeof *packet_in_queue);
        packet_in_queue->destination=destination;
        packet_in_queue->data=data;
        packet_in_queue->len=len;
-       pthread_mutex_lock(&(mesh->outpacketqueue_mutex));
-       list_insert_head(mesh->outpacketqueue,packet_in_queue);
-       pthread_mutex_unlock(&(mesh->outpacketqueue_mutex));
+       if(!meshlink_queue_push(&mesh->outpacketqueue, packet_in_queue)) {
+               free(packet_in_queue);
+               return false;
+       }
 
        //notify event loop
        signal_trigger(&(mesh->loop),&(mesh->datafromapp));
@@ -984,10 +1002,9 @@ void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh) {
        vpn_packet_t packet;
        meshlink_packethdr_t *hdr = (meshlink_packethdr_t *)packet.data;
 
-       outpacketqueue_t* p = list_get_tail(mesh->outpacketqueue);
-       if (p)
-       list_delete_tail(mesh->outpacketqueue);
-       else return ;
+       outpacketqueue_t* p = meshlink_queue_pop(&mesh->outpacketqueue);
+       if(!p)
+               return;
 
        if (sizeof(meshlink_packethdr_t) + p->len > MAXSIZE) {
                //log something
@@ -1396,7 +1413,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
        }
 
        if(!port)
-               port = "655";
+               goto invalid;
 
        if(!b64decode(slash, mesh->hash, 18) || !b64decode(slash + 24, mesh->cookie, 18))
                goto invalid;
@@ -1660,11 +1677,10 @@ void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
 /* Hint that a hostname may be found at an address
  * See header file for detailed comment.
  */
-extern void meshlink_hint_address(meshlink_handle_t *mesh, const char *hostname, struct sockaddr *addr) {
-       if(!mesh || !hostname || !addr)
+extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, struct sockaddr *addr) {
+       if(!mesh || !node || !addr)
                return;
        
-       node_t *n = NULL;
        char *addr_str = malloc(MAX_ADDRESS_LENGTH*sizeof(char));
        memset(addr_str, 0, MAX_ADDRESS_LENGTH*sizeof(char));
 
@@ -1677,29 +1693,25 @@ extern void meshlink_hint_address(meshlink_handle_t *mesh, const char *hostname,
        char *full_addr_str = malloc(full_addr_len*sizeof(char));
        memset(full_addr_str, 0, full_addr_len*sizeof(char));
        
-       // check that hostname matches an existing node
-       n = lookup_node(mesh, hostname);
-       if(!n)
-               return;
-
        // get address and port number
        if(!get_ip_str(addr, addr_str, MAX_ADDRESS_LENGTH))
-               return;
+               goto fail;
        if(!get_port_str(addr, port_str, MAX_ADDRESS_LENGTH))
-               return;
+               goto fail;
 
        // append_config_file expects an address, a space, and then a port number
        strcat(full_addr_str, addr_str);
        strcat(full_addr_str, " ");
        strcat(full_addr_str, port_str);
        
-       append_config_file(mesh, n->name, "Address", full_addr_str);
+       append_config_file(mesh, node->name, "Address", full_addr_str);
 
+fail:
        free(addr_str);
        free(port_str);
        free(full_addr_str);
 
-       // TODO do we want to fire off a connection attempt right away?
+       // @TODO do we want to fire off a connection attempt right away?
 }
 
 static void __attribute__((constructor)) meshlink_init(void) {