From 9fe7f35ce87dceb368c7ddffd7f6902dca24b97a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 26 Jan 2014 04:48:35 +0100 Subject: Add timeout helpers --- src/fastd.h | 10 ++++++++++ src/receive.c | 6 ++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/fastd.h b/src/fastd.h index f713841..42878e2 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -409,6 +409,16 @@ static inline int timespec_diff(const struct timespec *tp1, const struct timespe return ((tp1->tv_sec - tp2->tv_sec))*1000 + (tp1->tv_nsec - tp2->tv_nsec)/1e6; } +static inline bool fastd_timed_out(const fastd_context_t *ctx, const struct timespec *timeout) { + return !timespec_after(timeout, &ctx->now); +} + +static inline struct timespec fastd_in_seconds(const fastd_context_t *ctx, int seconds) { + struct timespec ret = ctx->now; + ret.tv_sec += seconds; + return ret; +} + static inline bool strequal(const char *str1, const char *str2) { if (str1 && str2) return (!strcmp(str1, str2)); diff --git a/src/receive.c b/src/receive.c index fa45dde..19b450a 100644 --- a/src/receive.c +++ b/src/receive.c @@ -110,7 +110,7 @@ static inline bool backoff_unknown(fastd_context_t *ctx, const fastd_peer_addres for (i = 0; i < array_size(ctx->unknown_handshakes); i++) { const fastd_handshake_timeout_t *t = &ctx->unknown_handshakes[(ctx->unknown_handshake_pos + i) % array_size(ctx->unknown_handshakes)]; - if (!timespec_after(&t->timeout, &ctx->now)) + if (fastd_timed_out(ctx, &t->timeout)) break; if (fastd_peer_address_equal(addr, &t->address)) { @@ -127,9 +127,7 @@ static inline bool backoff_unknown(fastd_context_t *ctx, const fastd_peer_addres fastd_handshake_timeout_t *t = &ctx->unknown_handshakes[ctx->unknown_handshake_pos]; t->address = *addr; - - t->timeout = ctx->now; - t->timeout.tv_sec += ctx->conf->min_handshake_interval; + t->timeout = fastd_in_seconds(ctx, ctx->conf->min_handshake_interval); return false; } -- cgit v1.2.3