]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Fix buffer overflow in meshlink_hint_address().
[meshlink] / src / meshlink.c
index 0da4cbbed0e4fe38d1d5cd6d8ce4666b5284fb8c..6e2ca709b28679163d550d6890459904ed41d4a4 100644 (file)
@@ -21,6 +21,8 @@
 #define VAR_MULTIPLE 4  /* Multiple statements allowed */
 #define VAR_OBSOLETE 8  /* Should not be used anymore */
 #define VAR_SAFE 16     /* Variable is safe when accepting invitations */
+#define MAX_ADDRESS_LENGTH 45 /* Max length of an (IPv6) address */
+#define MAX_PORT_LENGTH 5 /* 0-65535 */
 typedef struct {
        const char *name;
        int type;
@@ -39,11 +41,14 @@ typedef struct {
 #include "utils.h"
 #include "xalloc.h"
 #include "ed25519/sha512.h"
+#include "discovery.h"
 
 #ifndef MSG_NOSIGNAL
 #define MSG_NOSIGNAL 0
 #endif
 
+static pthread_mutex_t global_mutex;
+
 __thread meshlink_errno_t meshlink_errno;
 
 //TODO: this can go away completely
@@ -347,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];
@@ -365,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;
                }
        }
@@ -740,13 +739,12 @@ static bool meshlink_setup(meshlink_handle_t *mesh) {
        return true;
 }
 
-
-meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
-       return meshlink_open_with_size(confbase, name, sizeof(meshlink_handle_t) );
+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) {
 
-meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, size_t size) {
        // Validate arguments provided by the application
        bool usingname = false;
 
@@ -756,6 +754,12 @@ meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *nam
                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;
@@ -771,6 +775,7 @@ meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *nam
 
        meshlink_handle_t *mesh = xzalloc(size);
        mesh->confbase = xstrdup(confbase);
+       mesh->appname = xstrdup(appname);
        if (usingname) mesh->name = xstrdup(name);
        pthread_mutex_init ( &(mesh->nodes_mutex), NULL);
        mesh->threadstarted = false;
@@ -840,6 +845,9 @@ static void *meshlink_main_loop(void *arg) {
 }
 
 bool meshlink_start(meshlink_handle_t *mesh) {
+
+       fprintf(stderr, "meshlink_start called\n");
+
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
                return false;
@@ -865,19 +873,26 @@ bool meshlink_start(meshlink_handle_t *mesh) {
 
        mesh->threadstarted=true;
 
+       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;
        }
 
-       listen_socket_t *s = &mesh->listen_socket[0];
+       // Stop discovery
+       discovery_stop(mesh);
 
        // Shut down a listening socket to signal the main thread to shut down
 
+       listen_socket_t *s = &mesh->listen_socket[0];
        shutdown(s->tcp.fd, SHUT_RDWR);
 
        // Wait for the main thread to finish
@@ -1398,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;
@@ -1659,6 +1674,28 @@ void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
        return;
 }
 
+/* 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, meshlink_node_t *node, const struct sockaddr *addr) {
+       if(!mesh || !node || !addr)
+               return;
+       
+       char *host = NULL, *port = NULL, *str = NULL;
+       sockaddr2str((const sockaddr_t *)addr, &host, &port);
+
+       if(host && port) {
+               xasprintf(&str, "%s %s", host, port);
+               append_config_file(mesh, node->name, "Address", str);
+       }
+
+       free(str);
+       free(host);
+       free(port);
+
+       // @TODO do we want to fire off a connection attempt right away?
+}
+
 static void __attribute__((constructor)) meshlink_init(void) {
        crypto_init();
 }