From 26cf96bea0e07df934c807b78c2d77383556c1ce Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 26 Jan 2014 06:00:04 +0100 Subject: Switch to the new timeout helpers where possible --- src/fastd.c | 19 +++++++++---------- src/methods/common.c | 4 ++-- src/methods/common.h | 2 +- src/peer.c | 10 ++++++---- src/peer.h | 10 ++++++---- src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c | 2 +- src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h | 2 -- src/protocols/ec25519_fhmqvc/handshake.c | 9 ++------- 8 files changed, 27 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/fastd.c b/src/fastd.c index dadfe7b..532c8be 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -558,13 +558,13 @@ static inline int handshake_timeout(fastd_context_t *ctx) { if (!ctx->handshake_queue.next) return -1; - fastd_peer_t *peer = container_of(ctx->handshake_queue.next, fastd_peer_t, handshake_entry); + fastd_peer_t *peer = container_of(ctx->handshake_queue.next, fastd_peer_t, handshake_entry); - int diff_msec = timespec_diff(&peer->next_handshake, &ctx->now); - if (diff_msec < 0) - return 0; - else - return diff_msec; + int diff_msec = timespec_diff(&peer->next_handshake, &ctx->now); + if (diff_msec < 0) + return 0; + else + return diff_msec; } static void handle_input(fastd_context_t *ctx) { @@ -647,7 +647,7 @@ static void cleanup_peers(fastd_context_t *ctx) { next = peer->next; if (fastd_peer_is_temporary(peer) || fastd_peer_is_established(peer)) { - if (timespec_diff(&ctx->now, &peer->seen) > (int)ctx->conf->peer_stale_time*1000) { + if (fastd_timed_out(ctx, &peer->timeout)) { if (fastd_peer_is_temporary(peer)) { fastd_peer_delete(ctx, peer); } @@ -678,7 +678,7 @@ static void maintenance(fastd_context_t *ctx) { if (!fastd_peer_is_established(peer)) continue; - if (timespec_diff(&ctx->now, &peer->last_send) < (int)ctx->conf->keepalive_timeout*1000) + if (!fastd_timed_out(ctx, &peer->keepalive_timeout)) continue; pr_debug2(ctx, "sending keepalive to %P", peer); @@ -915,8 +915,7 @@ int main(int argc, char *argv[]) { update_time(&ctx); - ctx.next_keepalives = ctx.now; - ctx.next_keepalives.tv_sec += conf.keepalive_interval; + ctx.next_keepalives = fastd_in_seconds(&ctx, conf.keepalive_interval); ctx.unknown_handshakes[0].timeout = ctx.now; diff --git a/src/methods/common.c b/src/methods/common.c index 75a61b3..32e827c 100644 --- a/src/methods/common.c +++ b/src/methods/common.c @@ -57,7 +57,7 @@ bool fastd_method_is_nonce_valid(fastd_context_t *ctx, const fastd_method_common *age >>= 1; if (*age >= 0) { - if (timespec_diff(&ctx->now, &session->receive_last) > (int)ctx->conf->reorder_time*1000) + if (fastd_timed_out(ctx, &session->reorder_timeout)) return false; if (*age > 64) @@ -79,7 +79,7 @@ bool fastd_method_reorder_check(fastd_context_t *ctx, fastd_peer_t *peer, fastd_ session->receive_reorder_seen |= (1 << (shift-1)); memcpy(session->receive_nonce, nonce, COMMON_NONCEBYTES); - session->receive_last = ctx->now; + session->reorder_timeout = fastd_in_seconds(ctx, ctx->conf->reorder_time); return true; } else if (age == 0 || session->receive_reorder_seen & (1 << (age-1))) { diff --git a/src/methods/common.h b/src/methods/common.h index a171746..b01401b 100644 --- a/src/methods/common.h +++ b/src/methods/common.h @@ -42,7 +42,7 @@ typedef struct fastd_method_common { uint8_t send_nonce[COMMON_NONCEBYTES]; uint8_t receive_nonce[COMMON_NONCEBYTES]; - struct timespec receive_last; + struct timespec reorder_timeout; uint64_t receive_reorder_seen; } fastd_method_common_t; diff --git a/src/peer.c b/src/peer.c index 6929b2f..a1a9fc2 100644 --- a/src/peer.c +++ b/src/peer.c @@ -219,6 +219,8 @@ static void setup_peer(fastd_context_t *ctx, fastd_peer_t *peer) { peer->last_handshake_response_timeout = ctx->now; peer->last_handshake_response_address.sa.sa_family = AF_UNSPEC; + peer->establish_handshake_timeout = ctx->now; + if (!peer->protocol_state) ctx->conf->protocol->init_peer_state(ctx, peer); @@ -565,7 +567,7 @@ fastd_peer_t* fastd_peer_add_temporary(fastd_context_t *ctx) { ctx->peers_temp = peer; peer->group = ctx->peer_group; - peer->seen = ctx->now; + fastd_peer_seen(ctx, peer); pr_debug(ctx, "adding temporary peer"); @@ -678,7 +680,7 @@ void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, fastd_eth if (cmp == 0) { ctx->eth_addr[cur].peer = peer; - ctx->eth_addr[cur].seen = ctx->now; + ctx->eth_addr[cur].timeout = fastd_in_seconds(ctx, ctx->conf->eth_addr_stale_time); return; /* We're done here. */ } else if (cmp < 0) { @@ -703,7 +705,7 @@ void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, fastd_eth for (i = ctx->n_eth_addr-1; i > min; i--) ctx->eth_addr[i] = ctx->eth_addr[i-1]; - ctx->eth_addr[min] = (fastd_peer_eth_addr_t){ addr, peer, ctx->now }; + ctx->eth_addr[min] = (fastd_peer_eth_addr_t){ addr, peer, fastd_in_seconds(ctx, ctx->conf->eth_addr_stale_time) }; pr_debug(ctx, "learned new MAC address %E on peer %P", &addr, peer); } @@ -712,7 +714,7 @@ void fastd_peer_eth_addr_cleanup(fastd_context_t *ctx) { size_t i, deleted = 0; for (i = 0; i < ctx->n_eth_addr; i++) { - if (timespec_diff(&ctx->now, &ctx->eth_addr[i].seen) > (int)ctx->conf->eth_addr_stale_time*1000) { + if (fastd_timed_out(ctx, &ctx->eth_addr[i].timeout)) { deleted++; pr_debug(ctx, "MAC address %E not seen for more than %u seconds, removing", &ctx->eth_addr[i].addr, ctx->conf->eth_addr_stale_time); diff --git a/src/peer.h b/src/peer.h index 2720f42..f72fe0b 100644 --- a/src/peer.h +++ b/src/peer.h @@ -41,8 +41,8 @@ struct fastd_peer { fastd_peer_address_t address; fastd_peer_state_t state; - struct timespec seen; - struct timespec last_send; + struct timespec timeout; + struct timespec keepalive_timeout; fastd_remote_t *remotes; fastd_remote_t *next_remote; @@ -56,6 +56,8 @@ struct fastd_peer { struct timespec last_handshake_response_timeout; fastd_peer_address_t last_handshake_response_address; + struct timespec establish_handshake_timeout; + fastd_protocol_peer_config_t *protocol_config; fastd_protocol_peer_state_t *protocol_state; }; @@ -80,7 +82,7 @@ struct fastd_peer_config { struct fastd_peer_eth_addr { fastd_eth_addr_t addr; fastd_peer_t *peer; - struct timespec seen; + struct timespec timeout; }; struct fastd_remote { @@ -203,7 +205,7 @@ static inline bool fastd_remote_is_dynamic(const fastd_remote_t *remote) { } static inline void fastd_peer_seen(fastd_context_t *ctx, fastd_peer_t *peer) { - peer->seen = ctx->now; + peer->timeout = fastd_in_seconds(ctx, ctx->conf->peer_stale_time); } static inline bool fastd_peer_is_socket_dynamic(const fastd_peer_t *peer) { diff --git a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c index 05479d0..1e3c36a 100644 --- a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c +++ b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c @@ -160,7 +160,7 @@ static void session_send(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_ } fastd_send(ctx, peer->sock, &peer->local_address, &peer->address, peer, send_buffer, stat_size); - peer->last_send = ctx->now; + peer->keepalive_timeout = fastd_in_seconds(ctx, ctx->conf->keepalive_timeout); } static void protocol_send(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer) { diff --git a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h index eae7f75..51d303a 100644 --- a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h +++ b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h @@ -55,8 +55,6 @@ struct fastd_protocol_peer_config { }; typedef struct protocol_session { - struct timespec established; - bool handshakes_cleaned; bool refreshing; diff --git a/src/protocols/ec25519_fhmqvc/handshake.c b/src/protocols/ec25519_fhmqvc/handshake.c index 8d55fc9..3f08ecb 100644 --- a/src/protocols/ec25519_fhmqvc/handshake.c +++ b/src/protocols/ec25519_fhmqvc/handshake.c @@ -113,7 +113,6 @@ static inline bool new_session(fastd_context_t *ctx, fastd_peer_t *peer, const f if (!peer->protocol_state->session.method_state) return false; - peer->protocol_state->session.established = ctx->now; peer->protocol_state->session.handshakes_cleaned = false; peer->protocol_state->session.refreshing = false; peer->protocol_state->session.method = method; @@ -150,6 +149,7 @@ static bool establish(fastd_context_t *ctx, fastd_peer_t *peer, const fastd_meth return false; } + peer->establish_handshake_timeout = fastd_in_seconds(ctx, ctx->conf->min_handshake_interval); fastd_peer_seen(ctx, peer); fastd_peer_set_established(ctx, peer); @@ -515,11 +515,6 @@ static inline fastd_peer_t* add_temporary(fastd_context_t *ctx, const fastd_peer } -static inline 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) < (int)ctx->conf->min_handshake_interval*1000); -} - void fastd_protocol_ec25519_fhmqvc_handshake_init(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer) { fastd_protocol_ec25519_fhmqvc_maintenance(ctx); @@ -580,7 +575,7 @@ void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_context_t *ctx, fastd_ return; } - if (backoff(ctx, peer)) { + if (!fastd_timed_out(ctx, &peer->establish_handshake_timeout)) { pr_debug(ctx, "received repeated handshakes from %P[%I], ignoring", peer, remote_addr); return; } -- cgit v1.2.3