X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fprotocol_key.c;h=836737aa7e284e2c11886707d02088ad5a1846a9;hb=771c923ba3d6e2122d822cd1ebd38373020dc422;hp=43a227bbd65b459aaf7254a5069609ff2a1ea6f7;hpb=0d133a5ff230ad78da3208d32521d7549836187e;p=meshlink diff --git a/src/protocol_key.c b/src/protocol_key.c index 43a227bb..836737aa 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -31,6 +31,8 @@ #include "utils.h" #include "xalloc.h" +static const int req_key_timeout = 2; + void send_key_changed(meshlink_handle_t *mesh) { send_request(mesh, mesh->everyone, NULL, "%d %x %s", KEY_CHANGED, rand(), mesh->self->name); @@ -80,7 +82,7 @@ static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data } bool send_req_key(meshlink_handle_t *mesh, node_t *to) { - if(!node_read_ecdsa_public_key(mesh, to)) { + if(!node_read_public_key(mesh, to)) { logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", to->name); send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY); return true; @@ -96,8 +98,7 @@ bool send_req_key(meshlink_handle_t *mesh, node_t *to) { to->status.validkey = false; to->status.waitingforkey = true; to->last_req_key = mesh->loop.now.tv_sec; - to->incompression = mesh->self->incompression; - return sptps_start(&to->sptps, to, true, true, mesh->self->connection->ecdsa, to->ecdsa, label, sizeof(label) - 1, send_initial_sptps_data, receive_sptps_record); + return sptps_start(&to->sptps, to, true, true, mesh->private_key, to->ecdsa, label, sizeof(label) - 1, send_initial_sptps_data, receive_sptps_record); } /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */ @@ -107,7 +108,7 @@ static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char * switch(reqno) { case REQ_PUBKEY: { - char *pubkey = ecdsa_get_base64_public_key(mesh->self->connection->ecdsa); + char *pubkey = ecdsa_get_base64_public_key(mesh->private_key); if(!from->nexthop || !from->nexthop->connection) { return false; @@ -119,7 +120,7 @@ static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char * } case ANS_PUBKEY: { - if(node_read_ecdsa_public_key(mesh, from)) { + if(node_read_public_key(mesh, from)) { logger(mesh, MESHLINK_WARNING, "Got ANS_PUBKEY from %s even though we already have his pubkey", from->name); return true; } @@ -132,12 +133,26 @@ static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char * } logger(mesh, MESHLINK_INFO, "Learned ECDSA public key from %s", from->name); - append_config_file(mesh, from->name, "ECDSAPublicKey", pubkey); + from->status.dirty = true; + + /* If we are trying to form an outgoing connection to this node, retry immediately */ + for list_each(outgoing_t, outgoing, mesh->outgoings) { + if(outgoing->node == from && outgoing->ev.cb) { + outgoing->timeout = 0; + timeout_set(&mesh->loop, &outgoing->ev, &(struct timeval) { + 0, 0 + }); + } + } + + /* Also reset any UTCP timers */ + utcp_reset_timers(from->utcp); + return true; } case REQ_KEY: { - if(!node_read_ecdsa_public_key(mesh, from)) { + if(!node_read_public_key(mesh, from)) { logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", from->name); send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY); return true; @@ -146,7 +161,7 @@ static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char * if(from->sptps.label) { logger(mesh, MESHLINK_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name); - if(strcmp(mesh->self->name, from->name) < 0) { + if(mesh->loop.now.tv_sec < from->last_req_key + req_key_timeout && strcmp(mesh->self->name, from->name) < 0) { logger(mesh, MESHLINK_DEBUG, "Ignoring REQ_KEY from %s.", from->name); return true; } @@ -166,7 +181,7 @@ static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char * from->status.validkey = false; from->status.waitingforkey = true; from->last_req_key = mesh->loop.now.tv_sec; - sptps_start(&from->sptps, from, false, true, mesh->self->connection->ecdsa, from->ecdsa, label, sizeof(label) - 1, send_sptps_data, receive_sptps_record); + sptps_start(&from->sptps, from, false, true, mesh->private_key, from->ecdsa, label, sizeof(label) - 1, send_sptps_data, receive_sptps_record); sptps_receive_data(&from->sptps, buf, len); return true; } @@ -324,8 +339,6 @@ bool ans_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { return true; } - from->outcompression = compression; - /* SPTPS or old-style key exchange? */ char buf[strlen(key)]; @@ -342,9 +355,7 @@ bool ans_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { update_node_udp(mesh, from, &sa); } - if(from->options & OPTION_PMTU_DISCOVERY && !(from->options & OPTION_TCPONLY)) { - send_mtu_probe(mesh, from); - } + send_mtu_probe(mesh, from); } return true;