From b0a169a1465a75592f0083a3e4e17c307878fc73 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 7 Jun 2012 00:56:47 +0200 Subject: Limit handshake frequency where possible --- src/config.c | 1 + src/fastd.c | 12 ++++++++++-- src/fastd.h | 1 + src/peer.c | 8 ++++++++ src/peer.h | 6 ++++++ src/protocol_ec25519_fhmqvc.c | 10 +++++++++- 6 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/config.c b/src/config.c index 64903cb..4613295 100644 --- a/src/config.c +++ b/src/config.c @@ -63,6 +63,7 @@ static void default_config(fastd_config *conf) { conf->reorder_count = 64; conf->reorder_time = 10; + conf->min_handshake_interval = 15; conf->min_resolve_interval = 15; conf->ifname = NULL; diff --git a/src/fastd.c b/src/fastd.c index 406cb53..457a488 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -403,8 +403,16 @@ static inline void update_time(fastd_context *ctx) { static inline void send_handshake(fastd_context *ctx, fastd_peer *peer) { if (peer->address.sa.sa_family != AF_UNSPEC) { - pr_debug(ctx, "sending handshake to %P...", peer); - ctx->conf->protocol->handshake_init(ctx, &peer->address, peer->config); + if (timespec_diff(&ctx->now, &peer->last_handshake) < ctx->conf->min_handshake_interval*1000 + && fastd_peer_address_equal(&peer->address, &peer->last_handshake_address)) { + pr_debug(ctx, "not sending a handshake to %P as we sent one a short time ago", peer); + } + else { + 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->address, peer->config); + } } fastd_task_schedule_handshake(ctx, peer, fastd_rand(ctx, 17500, 22500)); diff --git a/src/fastd.h b/src/fastd.h index add22f2..8a0fcce 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -136,6 +136,7 @@ struct _fastd_config { unsigned reorder_count; unsigned reorder_time; + unsigned min_handshake_interval; unsigned min_resolve_interval; char *ifname; diff --git a/src/peer.c b/src/peer.c index 078bfdc..f412836 100644 --- a/src/peer.c +++ b/src/peer.c @@ -176,9 +176,17 @@ static inline void setup_peer(fastd_context *ctx, fastd_peer *peer) { peer->address = peer->config->address; peer->established = false; + peer->last_resolve = (struct timespec){0, 0}; peer->last_resolve_return = (struct timespec){0, 0}; peer->seen = (struct timespec){0, 0}; + + peer->last_handshake = (struct timespec){0, 0}; + peer->last_handshake_address.sa.sa_family = AF_UNSPEC; + + peer->last_handshake_response = (struct timespec){0, 0}; + peer->last_handshake_response_address.sa.sa_family = AF_UNSPEC; + peer->protocol_state = NULL; if (!fastd_peer_is_floating(peer)) diff --git a/src/peer.h b/src/peer.h index 15a5769..007eefd 100644 --- a/src/peer.h +++ b/src/peer.h @@ -43,6 +43,12 @@ struct _fastd_peer { struct timespec last_resolve_return; struct timespec seen; + struct timespec last_handshake; + fastd_peer_address last_handshake_address; + + struct timespec last_handshake_response; + fastd_peer_address last_handshake_response_address; + fastd_protocol_peer_state *protocol_state; }; diff --git a/src/protocol_ec25519_fhmqvc.c b/src/protocol_ec25519_fhmqvc.c index 75360b7..96f0847 100644 --- a/src/protocol_ec25519_fhmqvc.c +++ b/src/protocol_ec25519_fhmqvc.c @@ -577,12 +577,20 @@ static void protocol_handshake_handle(fastd_context *ctx, const fastd_peer_addre switch(handshake->type) { case 1: + if (timespec_diff(&ctx->now, &peer->last_handshake_response) < ctx->conf->min_handshake_interval*1000 + && fastd_peer_address_equal(address, &peer->last_handshake_response_address)) { + pr_debug(ctx, "not responding repeated handshake from %P[%I]", peer, address); + return; + } + if (handshake->records[RECORD_VERSION_NAME].data) peer_version_name = strndup(handshake->records[RECORD_VERSION_NAME].data, handshake->records[RECORD_VERSION_NAME].length); - + pr_verbose(ctx, "received handshake from %P[%I] using fastd %s", peer, address, peer_version_name); free(peer_version_name); + peer->last_handshake_response = ctx->now; + peer->last_handshake_response_address = *address; respond_handshake(ctx, address, peer, &ctx->protocol_state->handshake_key, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, handshake); break; -- cgit v1.2.3