]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Move assert()s that dereference a pointer to after the pointer NULL check.
[meshlink] / src / meshlink.c
index eb9142cc743d842c7f8967e0b9a4408aa170113e..fbacfd4a4131a813d8f2119a11472303a00f4886 100644 (file)
@@ -743,6 +743,10 @@ static bool finalize_join(join_state_t *state, const void *buf, uint16_t len) {
                return false;
        }
 
+       if(!mesh->inviter_commits_first) {
+               devtool_set_inviter_commits_first(false);
+       }
+
        sptps_send_record(&state->sptps, 1, ecdsa_get_public_key(mesh->private_key), 32);
 
        logger(mesh, MESHLINK_DEBUG, "Configuration stored in: %s\n", mesh->confbase);
@@ -775,21 +779,44 @@ static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint
        join_state_t *state = handle;
        meshlink_handle_t *mesh = state->mesh;
 
-       switch(type) {
-       case SPTPS_HANDSHAKE:
-               return sptps_send_record(&state->sptps, 0, state->cookie, 18);
+       if(mesh->inviter_commits_first) {
+               switch(type) {
+               case SPTPS_HANDSHAKE:
+                       return sptps_send_record(&state->sptps, 2, state->cookie, 18 + 32);
 
-       case 0:
-               return finalize_join(state, msg, len);
+               case 1:
+                       break;
 
-       case 1:
-               logger(mesh, MESHLINK_DEBUG, "Invitation successfully accepted.\n");
-               shutdown(state->sock, SHUT_RDWR);
-               state->success = true;
-               break;
+               case 0:
+                       if(!finalize_join(state, msg, len)) {
+                               return false;
+                       }
 
-       default:
-               return false;
+                       logger(mesh, MESHLINK_DEBUG, "Invitation successfully accepted.\n");
+                       shutdown(state->sock, SHUT_RDWR);
+                       state->success = true;
+                       break;
+
+               default:
+                       return false;
+               }
+       } else {
+               switch(type) {
+               case SPTPS_HANDSHAKE:
+                       return sptps_send_record(&state->sptps, 0, state->cookie, 18);
+
+               case 0:
+                       return finalize_join(state, msg, len);
+
+               case 1:
+                       logger(mesh, MESHLINK_DEBUG, "Invitation successfully accepted.\n");
+                       shutdown(state->sock, SHUT_RDWR);
+                       state->success = true;
+                       break;
+
+               default:
+                       return false;
+               }
        }
 
        return true;
@@ -1531,9 +1558,6 @@ static void *meshlink_main_loop(void *arg) {
 }
 
 bool meshlink_start(meshlink_handle_t *mesh) {
-       assert(mesh->self);
-       assert(mesh->private_key);
-
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
                return false;
@@ -1543,6 +1567,8 @@ bool meshlink_start(meshlink_handle_t *mesh) {
 
        pthread_mutex_lock(&mesh->mutex);
 
+       assert(mesh->self);
+       assert(mesh->private_key);
        assert(mesh->self->ecdsa);
        assert(!memcmp((uint8_t *)mesh->self->ecdsa + 64, (uint8_t *)mesh->private_key + 64, 32));
 
@@ -2252,7 +2278,7 @@ bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *
 }
 
 bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len, const void *signature, size_t siglen) {
-       if(!mesh || !data || !len || !signature) {
+       if(!mesh || !source || !data || !len || !signature) {
                meshlink_errno = MESHLINK_EINVAL;
                return false;
        }
@@ -2647,6 +2673,10 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
                goto invalid;
        }
 
+       if(mesh->inviter_commits_first) {
+               memcpy(state.cookie + 18, ecdsa_get_public_key(mesh->private_key), 32);
+       }
+
        // Generate a throw-away key for the invitation.
        key = ecdsa_generate();