]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Ensure everything compiles with -Wall without giving warnings.
[meshlink] / src / meshlink.c
index ce69e86d486d63eb144b1ab2851d985e2809b854..2aa79def7d47c6762e01f30715cb14e277933991 100644 (file)
@@ -341,18 +341,6 @@ static char *get_value(const char *data, const char *var) {
                return NULL;
        return val;
 }
-static FILE *fopenmask(const char *filename, const char *mode, mode_t perms) {
-       mode_t mask = umask(0);
-       perms &= ~mask;
-       umask(~perms);
-       FILE *f = fopen(filename, mode);
-#ifdef HAVE_FCHMOD
-       if((perms & 0444) && f)
-               fchmod(fileno(f), perms);
-#endif
-       umask(mask);
-       return f;
-}
 
 static bool try_bind(int port) {
        struct addrinfo *ai = NULL;
@@ -438,6 +426,7 @@ static bool finalize_join(meshlink_handle_t *mesh) {
        FILE *fh = fopen(filename, "w");
        if(!fh) {
                fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
+               fclose(f);
                return false;
        }
 
@@ -536,42 +525,31 @@ static bool finalize_join(meshlink_handle_t *mesh) {
                fclose(f);
        }
 
-       // Generate our key and send a copy to the server
-       ecdsa_t *key = ecdsa_generate();
-       if(!key)
-               return false;
-
-       char *b64key = ecdsa_get_base64_public_key(key);
+       char *b64key = ecdsa_get_base64_public_key(mesh->self->connection->ecdsa);
        if(!b64key)
                return false;
 
-       snprintf(filename, sizeof filename, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
-       f = fopenmask(filename, "w", 0600);
-
-       if(!ecdsa_write_pem_private_key(key, f)) {
-               fprintf(stderr, "Error writing private key!\n");
-               ecdsa_free(key);
-               fclose(f);
-               return false;
-       }
-
-       fclose(f);
-
        fprintf(fh, "ECDSAPublicKey = %s\n", b64key);
+       fprintf(fh, "Port = %s\n", mesh->myport);
+
+       fclose(fh);
 
        sptps_send_record(&(mesh->sptps), 1, b64key, strlen(b64key));
        free(b64key);
 
-       ecdsa_free(key);
-
-       check_port(mesh);
+       free(mesh->self->name);
+       free(mesh->self->connection->name);
+       mesh->self->name = xstrdup(name);
+       mesh->self->connection->name = xstrdup(name);
 
        fprintf(stderr, "Configuration stored in: %s\n", mesh->confbase);
 
+       load_all_nodes(mesh);
+
        return true;
 }
 
-static bool invitation_send(void *handle, uint8_t type, const char *data, size_t len) {
+static bool invitation_send(void *handle, uint8_t type, const void *data, size_t len) {
        meshlink_handle_t* mesh = handle;
        while(len) {
                int result = send(mesh->sock, data, len, 0);
@@ -585,7 +563,7 @@ static bool invitation_send(void *handle, uint8_t type, const char *data, size_t
        return true;
 }
 
-static bool invitation_receive(void *handle, uint8_t type, const char *msg, uint16_t len) {
+static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint16_t len) {
        meshlink_handle_t* mesh = handle;
        switch(type) {
                case SPTPS_HANDSHAKE:
@@ -908,7 +886,15 @@ meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) {
 }
 
 size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t nmemb) {
-       return 0;
+       size_t i = 0;
+
+       for splay_each(node_t, n, mesh->nodes) {
+               if(i < nmemb)
+                       nodes[i] = (meshlink_node_t *)n;
+               i++;
+       }
+
+       return i;
 }
 
 char *meshlink_sign(meshlink_handle_t *mesh, const char *data, size_t len) {
@@ -1141,7 +1127,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
                        *port++ = 0;
        }
 
-       if(!mesh->myport || !*port)
+       if(!*port)
                port = "655";
 
        if(!b64decode(slash, mesh->hash, 18) || !b64decode(slash + 24, mesh->cookie, 18))