]> git.meshlink.io Git - meshlink/blobdiff - src/protocol_auth.c
Fix incorrect SPTPS session labels.
[meshlink] / src / protocol_auth.c
index 85a0fbe16237bfad1c01e9fb9553f39815b44a68..7a405fe4d8ea3436326ad9ba872de02a0b59b971 100644 (file)
 #include "xalloc.h"
 #include "ed25519/sha512.h"
 
+#include <assert.h>
+
+extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
+
 static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) {
        switch(mesh->proxytype) {
                case PROXY_HTTP: {
@@ -124,8 +128,7 @@ static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) {
 }
 
 bool send_id(meshlink_handle_t *mesh, connection_t *c) {
-       gettimeofday(&c->start, NULL);
-
+       
        int minor = mesh->self->connection->protocol_minor;
 
        if(mesh->proxytype && c->outgoing)
@@ -286,7 +289,9 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                c->protocol_minor = 2;
                c->allow_request = 1;
 
-               return sptps_start(&c->sptps, c, false, false, mesh->invitation_key, c->ecdsa, "meshlink invitation", 15, send_meta_sptps, receive_invitation_sptps);
+               static const char label[] = "MeshLink invitation";
+
+               return sptps_start(&c->sptps, c, false, false, mesh->invitation_key, c->ecdsa, label, sizeof label - 1, send_meta_sptps, receive_invitation_sptps);
        }
 
        /* Check if identity is a valid name */
@@ -326,11 +331,20 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                        logger(mesh, MESHLINK_ERROR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
                        return false;
                }
+       }
+
+       read_ecdsa_public_key(mesh, c);
+
+       if(!ecdsa_active(c->ecdsa)) {
+               logger(mesh, MESHLINK_ERROR, "No key known for peer %s (%s)", c->name, c->hostname);
+
+               node_t *n = lookup_node(mesh, c->name);
+               if(n && !n->status.waitingforkey) {
+                       logger(mesh, MESHLINK_INFO, "Requesting key from peer %s (%s)", c->name, c->hostname);
+                       send_req_key(mesh, n);
+               }
 
-               read_ecdsa_public_key(mesh, c);
-       } else {
-               if(c->protocol_minor && !ecdsa_active(c->ecdsa))
-                       c->protocol_minor = 1;
+               return false;
        }
 
        /* Forbid version rollback for nodes whose ECDSA key we know */
@@ -342,33 +356,24 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
        }
 
        c->allow_request = ACK;
-       char label[25 + strlen(mesh->self->name) + strlen(c->name)];
+       char label[14 + strlen(mesh->self->name) + strlen(c->name) + 1];
 
        if(c->outgoing)
-               snprintf(label, sizeof label, "meshlink TCP key expansion %s %s", mesh->self->name, c->name);
+               snprintf(label, sizeof label, "MeshLink TCP %s %s", mesh->self->name, c->name);
        else
-               snprintf(label, sizeof label, "meshlink TCP key expansion %s %s", c->name, mesh->self->name);
+               snprintf(label, sizeof label, "MeshLink TCP %s %s", c->name, mesh->self->name);
 
-       return sptps_start(&c->sptps, c, c->outgoing, false, mesh->self->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
+       return sptps_start(&c->sptps, c, c->outgoing, false, mesh->self->connection->ecdsa, c->ecdsa, label, sizeof label - 1, send_meta_sptps, receive_meta_sptps);
 }
 
 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
-       /* ACK message contains rest of the information the other end needs
-          to create node_t and edge_t structures. */
-
-       struct timeval now;
-
-       /* Estimate weight */
-
-       gettimeofday(&now, NULL);
-       c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
 
        /* Check some options */
 
        if(mesh->self->options & OPTION_PMTU_DISCOVERY)
                c->options |= OPTION_PMTU_DISCOVERY;
 
-       return send_request(mesh, c, "%d %s %d %x", ACK, mesh->myport, c->estimated_weight, (c->options & 0xffffff) | (PROT_MINOR << 24));
+       return send_request(mesh, c, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, (c->options & 0xffffff) | (PROT_MINOR << 24));
 }
 
 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
@@ -383,16 +388,22 @@ static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
        char hisport[MAX_STRING_SIZE];
        char *hisaddress;
-       int weight;
+       int devclass;
        uint32_t options;
        node_t *n;
 
-       if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
+       if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &devclass, &options) != 3) {
                logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s)", "ACK", c->name,
                           c->hostname);
                return false;
        }
 
+       if(devclass < 0 || devclass > _DEV_CLASS_MAX) {
+               logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "ACK", c->name,
+                          c->hostname, "devclass invalid");
+               return false;
+       }
+
        /* Check if we already have a node_t for him */
 
        n = lookup_node(mesh, c->name);
@@ -421,6 +432,11 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                }
        }
 
+       n->devclass = devclass;
+       node_write_devclass(mesh, n);
+
+       n->last_successfull_connection = time(NULL);
+
        n->connection = c;
        c->node = n;
        if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
@@ -443,13 +459,15 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
 
        /* Create an edge_t for this connection */
 
+       assert(devclass >= 0 && devclass <= _DEV_CLASS_MAX);
+
        c->edge = new_edge();
        c->edge->from = mesh->self;
        c->edge->to = n;
        sockaddr2str(&c->address, &hisaddress, NULL);
        c->edge->address = str2sockaddr(hisaddress, hisport);
        free(hisaddress);
-       c->edge->weight = (weight + c->estimated_weight) / 2;
+       c->edge->weight = dev_class_traits[devclass].edge_weight;
        c->edge->connection = c;
        c->edge->options = c->options;