diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-02-23 14:28:33 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-02-23 14:28:33 +0100 |
commit | 2c0f4a5abbc5663417d1390f658b387378e28978 (patch) | |
tree | 9d5daba9a1713ff8d718c45df48ae844ca611dca | |
parent | a9ca525870f29e818935cfbb8977f13a225081be (diff) | |
download | fastd-2c0f4a5abbc5663417d1390f658b387378e28978.tar fastd-2c0f4a5abbc5663417d1390f658b387378e28978.zip |
Use fastd_peer_t instead of fastd_peer_config_t in handshake code
Directly using the peers allows us to get rid of the inefficient get_peer()
function and is necessary for adding support for unknown peers.
-rw-r--r-- | src/fastd.c | 4 | ||||
-rw-r--r-- | src/fastd.h | 4 | ||||
-rw-r--r-- | src/handshake.c | 6 | ||||
-rw-r--r-- | src/handshake.h | 2 | ||||
-rw-r--r-- | src/protocol_ec25519_fhmqvc.c | 55 |
5 files changed, 28 insertions, 43 deletions
diff --git a/src/fastd.c b/src/fastd.c index e48bb8a..704f383 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -683,7 +683,7 @@ static void send_handshake(fastd_context_t *ctx, fastd_peer_t *peer) { pr_debug(ctx, "sending handshake to %P...", peer); peer->last_handshake = ctx->now; peer->last_handshake_address = peer->address; - ctx->conf->protocol->handshake_init(ctx, peer->sock, &peer->address, peer->config); + ctx->conf->protocol->handshake_init(ctx, peer->sock, &peer->address, peer); } } @@ -818,7 +818,7 @@ static void handle_socket(fastd_context_t *ctx, fastd_socket_t *sock) { break; case PACKET_HANDSHAKE: - fastd_handshake_handle(ctx, sock, &recvaddr, peer->config, buffer); + fastd_handshake_handle(ctx, sock, &recvaddr, peer, buffer); break; default: diff --git a/src/fastd.h b/src/fastd.h index b2b7de7..3ebb44f 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -69,8 +69,8 @@ struct fastd_protocol { fastd_protocol_config_t* (*init)(fastd_context_t *ctx); void (*peer_configure)(fastd_context_t *ctx, fastd_peer_config_t *peer_conf); - void (*handshake_init)(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf); - void (*handshake_handle)(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, const fastd_handshake_t *handshake, const fastd_method_t *method); + void (*handshake_init)(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer); + void (*handshake_handle)(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer, const fastd_handshake_t *handshake, const fastd_method_t *method); void (*handle_recv)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer); void (*send)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer); diff --git a/src/handshake.c b/src/handshake.c index 860ec1b..d28ebad 100644 --- a/src/handshake.c +++ b/src/handshake.c @@ -180,7 +180,7 @@ static fastd_string_stack_t* parse_string_list(uint8_t *data, size_t len) { return ret; } -void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, fastd_buffer_t buffer) { +void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer, fastd_buffer_t buffer) { if (buffer.len < sizeof(fastd_packet_t)) { pr_warn(ctx, "received a short handshake from %I", address); goto end_free; @@ -307,7 +307,7 @@ void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fa fastd_send_handshake(ctx, sock, address, reply_buffer); } else { - ctx->conf->protocol->handshake_handle(ctx, sock, address, peer_conf, &handshake, method); + ctx->conf->protocol->handshake_handle(ctx, sock, address, peer, &handshake, method); } } else { @@ -334,7 +334,7 @@ void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fa goto end_free; } - ctx->conf->protocol->handshake_handle(ctx, sock, address, peer_conf, &handshake, method); + ctx->conf->protocol->handshake_handle(ctx, sock, address, peer, &handshake, method); } else { const char *error_field_str; diff --git a/src/handshake.h b/src/handshake.h index 6799568..67a81fc 100644 --- a/src/handshake.h +++ b/src/handshake.h @@ -71,7 +71,7 @@ struct fastd_handshake { fastd_buffer_t fastd_handshake_new_init(fastd_context_t *ctx, size_t tail_space); fastd_buffer_t fastd_handshake_new_reply(fastd_context_t *ctx, const fastd_handshake_t *handshake, const fastd_method_t *method, size_t tail_space); -void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, fastd_buffer_t buffer); +void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer, fastd_buffer_t buffer); static inline void fastd_handshake_add(fastd_context_t *ctx, fastd_buffer_t *buffer, fastd_handshake_record_type_t type, size_t len, const void *data) { diff --git a/src/protocol_ec25519_fhmqvc.c b/src/protocol_ec25519_fhmqvc.c index 07fc887..e614cfc 100644 --- a/src/protocol_ec25519_fhmqvc.c +++ b/src/protocol_ec25519_fhmqvc.c @@ -127,18 +127,6 @@ static inline bool is_session_valid(fastd_context_t *ctx, const protocol_session return (session->method && session->method->session_is_valid(ctx, session->method_state)); } -static fastd_peer_t* get_peer(fastd_context_t *ctx, const fastd_peer_config_t *peer_conf) { - fastd_peer_t *peer; - for (peer = ctx->peers; peer; peer = peer->next) { - if (peer->config == peer_conf) - break; - } - if (!peer) - exit_bug(ctx, "no peer for config found"); - - return peer; -} - static bool backoff(fastd_context_t *ctx, const fastd_peer_t *peer) { return (peer->protocol_state && is_session_valid(ctx, &peer->protocol_state->session) && timespec_diff(&ctx->now, &peer->protocol_state->session.established) < 15000); @@ -226,15 +214,15 @@ static void maintenance(fastd_context_t *ctx) { } } -static void protocol_handshake_init(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf) { +static void protocol_handshake_init(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer) { maintenance(ctx); fastd_buffer_t buffer = fastd_handshake_new_init(ctx, 3*(4+PUBLICKEYBYTES) /* sender key, receipient key, handshake key */); fastd_handshake_add(ctx, &buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, ctx->conf->protocol_config->public_key.p); - if (peer_conf) - fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, peer_conf->protocol_config->public_key.p); + if (peer) + fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, peer->config->protocol_config->public_key.p); else pr_debug(ctx, "sending handshake to unknown peer %I", address); @@ -517,39 +505,38 @@ static bool check_peer_config_match(const fastd_peer_config_t *config, const fas return (memcmp(config->protocol_config->public_key.p, key, PUBLICKEYBYTES) == 0); } -static const fastd_peer_config_t* match_sender_key(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, const unsigned char key[32]) { +static fastd_peer_t* match_sender_key(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer, const unsigned char key[32]) { if (sock->peer) { - if (peer_conf != sock->peer->config) { - if (peer_conf && !fastd_peer_config_is_floating(peer_conf) && !fastd_peer_config_is_dynamic(peer_conf)) + if (peer != sock->peer) { + if (peer && !fastd_peer_is_floating(peer) && !fastd_peer_is_dynamic(peer)) return NULL; - peer_conf = sock->peer->config; + peer = sock->peer; } } - if (peer_conf) { - if (memcmp(peer_conf->protocol_config->public_key.p, key, PUBLICKEYBYTES) == 0) { - if (sock->peer && sock->peer->config != peer_conf) + if (peer) { + if (memcmp(peer->config->protocol_config->public_key.p, key, PUBLICKEYBYTES) == 0) { + if (sock->peer && sock->peer != peer) return NULL; - return peer_conf; + return peer; } } - if (peer_conf && !fastd_peer_config_is_floating(peer_conf) && !fastd_peer_config_is_dynamic(peer_conf)) + if (peer && !fastd_peer_is_floating(peer) && !fastd_peer_is_dynamic(peer)) return NULL; - const fastd_peer_config_t *config; - for (config = ctx->conf->peers; config; config = config->next) { - if (!check_peer_config_match(config, address, key)) + for (peer = ctx->peers; peer; peer = peer->next) { + if (!check_peer_config_match(peer->config, address, key)) continue; - if (!fastd_peer_config_is_floating(config)) { /* matches dynamic */ - fastd_resolve_peer(ctx, get_peer(ctx, config)); + if (!fastd_peer_is_floating(peer)) { /* matches dynamic */ + fastd_resolve_peer(ctx, peer); return NULL; } - return config; + return peer; } return NULL; @@ -559,7 +546,7 @@ static inline bool has_field(const fastd_handshake_t *handshake, uint8_t type, s return (handshake->records[type].length == length); } -static void protocol_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, const fastd_handshake_t *handshake, const fastd_method_t *method) { +static void protocol_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer, const fastd_handshake_t *handshake, const fastd_method_t *method) { handshake_key_t *handshake_key; char *peer_version_name = NULL; @@ -570,14 +557,12 @@ static void protocol_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock return; } - peer_conf = match_sender_key(ctx, sock, address, peer_conf, handshake->records[RECORD_SENDER_KEY].data); - if (!peer_conf) { + peer = match_sender_key(ctx, sock, address, peer, handshake->records[RECORD_SENDER_KEY].data); + if (!peer) { pr_debug(ctx, "ignoring handshake from %I (unknown key or unresolved host)", address); return; } - fastd_peer_t *peer = get_peer(ctx, peer_conf); - if (!fastd_peer_may_connect(ctx, peer)) { pr_debug(ctx, "ignoring handshake from %P[%I] because of local constraints", peer, address); return; |