From 76d955efa5fcd46fd325015d6cd3a389f8d9bb93 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 26 Jan 2014 05:17:34 +0100 Subject: Get rid of long_ago variable, use timeout helpers --- src/fastd.c | 11 ++++------- src/fastd.h | 2 -- src/methods/common.c | 7 ++----- src/methods/common.h | 7 +++---- src/peer.c | 10 +++++----- src/peer.h | 8 ++++---- src/protocols/ec25519_fhmqvc/handshake.c | 6 +++--- src/protocols/ec25519_fhmqvc/handshake.h | 4 ++-- src/protocols/ec25519_fhmqvc/state.c | 11 ++++------- src/resolve.c | 7 ++++--- 10 files changed, 31 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/fastd.c b/src/fastd.c index 7c34720..dadfe7b 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -430,14 +430,14 @@ static void send_handshake(fastd_context_t *ctx, fastd_peer_t *peer) { return; } - if (timespec_diff(&ctx->now, &peer->last_handshake) < (int)ctx->conf->min_handshake_interval*1000 + if (!fastd_timed_out(ctx, &peer->last_handshake_timeout) && 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); return; } pr_debug(ctx, "sending handshake to %P[%I]...", peer, &peer->address); - peer->last_handshake = ctx->now; + peer->last_handshake_timeout = fastd_in_seconds(ctx, ctx->conf->min_handshake_interval); peer->last_handshake_address = peer->address; ctx->conf->protocol->handshake_init(ctx, peer->sock, &peer->local_address, &peer->address, peer); } @@ -447,7 +447,7 @@ static void handle_handshake_queue(fastd_context_t *ctx) { return; fastd_peer_t *peer = container_of(ctx->handshake_queue.next, fastd_peer_t, handshake_entry); - if (timespec_after(&peer->next_handshake, &ctx->now)) + if (!fastd_timed_out(ctx, &peer->next_handshake)) return; fastd_peer_schedule_handshake_default(ctx, peer); @@ -672,7 +672,7 @@ static void maintenance(fastd_context_t *ctx) { fastd_socket_handle_binds(ctx); - if (timespec_after(&ctx->now, &ctx->next_keepalives)) { + if (fastd_timed_out(ctx, &ctx->next_keepalives)) { fastd_peer_t *peer; for (peer = ctx->peers; peer; peer = peer->next) { if (!fastd_peer_is_established(peer)) @@ -915,9 +915,6 @@ int main(int argc, char *argv[]) { update_time(&ctx); - conf.long_ago = ctx.now; - conf.long_ago.tv_sec -= 86400; /* 24h in the past */ - ctx.next_keepalives = ctx.now; ctx.next_keepalives.tv_sec += conf.keepalive_interval; diff --git a/src/fastd.h b/src/fastd.h index 42878e2..ddd05a3 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -144,8 +144,6 @@ struct fastd_handshake_timeout { }; struct fastd_config { - struct timespec long_ago; - fastd_loglevel_t log_stderr_level; fastd_loglevel_t log_syslog_level; char *log_syslog_ident; diff --git a/src/methods/common.c b/src/methods/common.c index 7118ef4..75a61b3 100644 --- a/src/methods/common.c +++ b/src/methods/common.c @@ -30,11 +30,8 @@ void fastd_method_common_init(fastd_context_t *ctx, fastd_method_common_t *session, bool initiator) { memset(session, 0, sizeof(*session)); - session->valid_till = ctx->now; - session->valid_till.tv_sec += ctx->conf->key_valid; - - session->refresh_after = ctx->now; - session->refresh_after.tv_sec += ctx->conf->key_refresh - fastd_rand(ctx, 0, ctx->conf->key_refresh_splay); + session->valid_till = fastd_in_seconds(ctx, ctx->conf->key_valid); + session->refresh_after = fastd_in_seconds(ctx, ctx->conf->key_refresh - fastd_rand(ctx, 0, ctx->conf->key_refresh_splay)); if (initiator) { session->send_nonce[COMMON_NONCEBYTES-1] = 3; diff --git a/src/methods/common.h b/src/methods/common.h index 0bd7aae..a171746 100644 --- a/src/methods/common.h +++ b/src/methods/common.h @@ -56,7 +56,7 @@ static inline bool fastd_method_session_common_is_valid(fastd_context_t *ctx, co if (session->send_nonce[0] == 0xff && session->send_nonce[1] == 0xff) return false; - return (timespec_after(&session->valid_till, &ctx->now)); + return (!fastd_timed_out(ctx, &session->valid_till)); } static inline bool fastd_method_session_common_is_initiator(const fastd_method_common_t *session) { @@ -67,15 +67,14 @@ static inline bool fastd_method_session_common_want_refresh(fastd_context_t *ctx if (session->send_nonce[0] == 0xff) return true; - if (fastd_method_session_common_is_initiator(session) && timespec_after(&ctx->now, &session->refresh_after)) + if (fastd_method_session_common_is_initiator(session) && fastd_timed_out(ctx, &session->refresh_after)) return true; return false; } static inline void fastd_method_session_common_superseded(fastd_context_t *ctx, fastd_method_common_t *session) { - struct timespec valid_max = ctx->now; - valid_max.tv_sec += ctx->conf->key_valid_old; + struct timespec valid_max = fastd_in_seconds(ctx, ctx->conf->key_valid_old); if (timespec_after(&session->valid_till, &valid_max)) session->valid_till = valid_max; diff --git a/src/peer.c b/src/peer.c index cb336f2..6929b2f 100644 --- a/src/peer.c +++ b/src/peer.c @@ -186,7 +186,7 @@ static void init_handshake(fastd_context_t *ctx, fastd_peer_t *peer) { } void fastd_peer_handle_resolve(fastd_context_t *ctx, fastd_peer_t *peer, fastd_remote_t *remote, size_t n_addresses, const fastd_peer_address_t *addresses) { - remote->last_resolve_return = ctx->now; + remote->resolving = false; free(remote->addresses); remote->addresses = malloc(n_addresses*sizeof(fastd_peer_address_t)); @@ -207,16 +207,16 @@ static void setup_peer(fastd_context_t *ctx, fastd_peer_t *peer) { fastd_remote_t *remote; for (remote = peer->remotes; remote; remote = remote->next) { - remote->last_resolve = ctx->conf->long_ago; - remote->last_resolve_return = ctx->conf->long_ago; + remote->last_resolve_timeout = ctx->now; + remote->resolving = false; } peer->next_remote = peer->remotes; - peer->last_handshake = ctx->conf->long_ago; + peer->last_handshake_timeout = ctx->now; peer->last_handshake_address.sa.sa_family = AF_UNSPEC; - peer->last_handshake_response = ctx->conf->long_ago; + peer->last_handshake_response_timeout = ctx->now; peer->last_handshake_response_address.sa.sa_family = AF_UNSPEC; if (!peer->protocol_state) diff --git a/src/peer.h b/src/peer.h index 1a34ef5..2720f42 100644 --- a/src/peer.h +++ b/src/peer.h @@ -50,10 +50,10 @@ struct fastd_peer { struct timespec next_handshake; fastd_dlist_head_t handshake_entry; - struct timespec last_handshake; + struct timespec last_handshake_timeout; fastd_peer_address_t last_handshake_address; - struct timespec last_handshake_response; + struct timespec last_handshake_response_timeout; fastd_peer_address_t last_handshake_response_address; fastd_protocol_peer_config_t *protocol_config; @@ -94,8 +94,8 @@ struct fastd_remote { size_t current_address; fastd_peer_address_t *addresses; - struct timespec last_resolve; - struct timespec last_resolve_return; + struct timespec last_resolve_timeout; + bool resolving; }; struct fastd_remote_config { diff --git a/src/protocols/ec25519_fhmqvc/handshake.c b/src/protocols/ec25519_fhmqvc/handshake.c index 11c4443..8d55fc9 100644 --- a/src/protocols/ec25519_fhmqvc/handshake.c +++ b/src/protocols/ec25519_fhmqvc/handshake.c @@ -601,15 +601,15 @@ void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_context_t *ctx, fastd_ memcpy(&peer_handshake_key, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, PUBLICKEYBYTES); if (handshake->type == 1) { - if (timespec_diff(&ctx->now, &peer->last_handshake_response) < (int)ctx->conf->min_handshake_interval*1000 + if (!fastd_timed_out(ctx, &peer->last_handshake_response_timeout) && fastd_peer_address_equal(remote_addr, &peer->last_handshake_response_address)) { - pr_debug(ctx, "not responding repeated handshake from %P[%I]", peer, remote_addr); + pr_debug(ctx, "not responding to repeated handshake from %P[%I]", peer, remote_addr); return; } pr_verbose(ctx, "received handshake from %P[%I]%s%s", peer, remote_addr, handshake->peer_version ? " using fastd " : "", handshake->peer_version ?: ""); - peer->last_handshake_response = ctx->now; + peer->last_handshake_response_timeout = fastd_in_seconds(ctx, ctx->conf->min_handshake_interval); peer->last_handshake_response_address = *remote_addr; respond_handshake(ctx, sock, local_addr, remote_addr, peer, &ctx->protocol_state->handshake_key, &peer_handshake_key, handshake, method); return; diff --git a/src/protocols/ec25519_fhmqvc/handshake.h b/src/protocols/ec25519_fhmqvc/handshake.h index 2929b2b..c6eadf9 100644 --- a/src/protocols/ec25519_fhmqvc/handshake.h +++ b/src/protocols/ec25519_fhmqvc/handshake.h @@ -45,11 +45,11 @@ struct fastd_protocol_state { static inline bool is_handshake_key_valid(fastd_context_t *ctx, const handshake_key_t *handshake_key) { - return timespec_after(&handshake_key->valid_till, &ctx->now); + return !fastd_timed_out(ctx, &handshake_key->valid_till); } static inline bool is_handshake_key_preferred(fastd_context_t *ctx, const handshake_key_t *handshake_key) { - return timespec_after(&handshake_key->preferred_till, &ctx->now); + return !fastd_timed_out(ctx, &handshake_key->preferred_till); } #endif /* _FASTD_PROTOCOL_EC25519_FHMQVC_HANDSHAKE_H_ */ diff --git a/src/protocols/ec25519_fhmqvc/state.c b/src/protocols/ec25519_fhmqvc/state.c index 990d1f0..d7fd151 100644 --- a/src/protocols/ec25519_fhmqvc/state.c +++ b/src/protocols/ec25519_fhmqvc/state.c @@ -32,8 +32,8 @@ static void init_protocol_state(fastd_context_t *ctx) { if (!ctx->protocol_state) { ctx->protocol_state = calloc(1, sizeof(fastd_protocol_state_t)); - ctx->protocol_state->prev_handshake_key.preferred_till = ctx->conf->long_ago; - ctx->protocol_state->handshake_key.preferred_till = ctx->conf->long_ago; + ctx->protocol_state->prev_handshake_key.preferred_till = ctx->now; + ctx->protocol_state->handshake_key.preferred_till = ctx->now; } } @@ -58,11 +58,8 @@ void fastd_protocol_ec25519_fhmqvc_maintenance(fastd_context_t *ctx) { new_handshake_key(ctx, &ctx->protocol_state->handshake_key.key); - ctx->protocol_state->handshake_key.preferred_till = ctx->now; - ctx->protocol_state->handshake_key.preferred_till.tv_sec += 15; - - ctx->protocol_state->handshake_key.valid_till = ctx->now; - ctx->protocol_state->handshake_key.valid_till.tv_sec += 30; + ctx->protocol_state->handshake_key.preferred_till = fastd_in_seconds(ctx, 15); + ctx->protocol_state->handshake_key.valid_till = fastd_in_seconds(ctx, 30); } } diff --git a/src/resolve.c b/src/resolve.c index 64b1714..ff09094 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -108,12 +108,12 @@ void fastd_resolve_peer(fastd_context_t *ctx, fastd_peer_t *peer, fastd_remote_t if (!peer->config) exit_bug(ctx, "trying to resolve temporary peer"); - if (timespec_after(&remote->last_resolve, &remote->last_resolve_return)) { + if (remote->resolving) { pr_debug(ctx, "not resolving %P as there is already a resolve running", peer); return; } - if (timespec_diff(&ctx->now, &remote->last_resolve) < (int)ctx->conf->min_resolve_interval*1000) { + if (!fastd_timed_out(ctx, &remote->last_resolve_timeout)) { /* last resolve was just a few seconds ago */ return; } @@ -121,7 +121,8 @@ void fastd_resolve_peer(fastd_context_t *ctx, fastd_peer_t *peer, fastd_remote_t pr_verbose(ctx, "resolving host `%s' for peer %P...", remote->config->hostname, peer); fastd_remote_ref(remote); - remote->last_resolve = ctx->now; + remote->last_resolve_timeout = fastd_in_seconds(ctx, ctx->conf->min_resolve_interval); + remote->resolving = true; resolv_arg_t *arg = malloc(sizeof(resolv_arg_t)); -- cgit v1.2.3