From 5ea41672d71086aec005310600965d9cc3e6ce9c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 Dec 2013 03:49:55 +0100 Subject: Change common packet header: flags comes first, nonce is big endian --- src/methods/common.c | 16 ++++++++-------- src/methods/common.h | 19 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) (limited to 'src/methods') diff --git a/src/methods/common.c b/src/methods/common.c index 9308332..7118ef4 100644 --- a/src/methods/common.c +++ b/src/methods/common.c @@ -37,11 +37,11 @@ void fastd_method_common_init(fastd_context_t *ctx, fastd_method_common_t *sessi session->refresh_after.tv_sec += ctx->conf->key_refresh - fastd_rand(ctx, 0, ctx->conf->key_refresh_splay); if (initiator) { - session->send_nonce[0] = 3; + session->send_nonce[COMMON_NONCEBYTES-1] = 3; } else { - session->send_nonce[0] = 2; - session->receive_nonce[0] = 1; + session->send_nonce[COMMON_NONCEBYTES-1] = 2; + session->receive_nonce[COMMON_NONCEBYTES-1] = 1; } } @@ -49,15 +49,15 @@ bool fastd_method_is_nonce_valid(fastd_context_t *ctx, const fastd_method_common if ((nonce[0] & 1) != (session->receive_nonce[0] & 1)) return false; - int i; + size_t i; *age = 0; - for (i = COMMON_NONCEBYTES-1; i >= 0; i--) { - *age *= 256; - *age += session->receive_nonce[i]-nonce[i]; + for (i = 0; i < COMMON_NONCEBYTES; i++) { + *age <<= 8; + *age += session->receive_nonce[i] - nonce[i]; } - *age /= 2; + *age >>= 1; if (*age >= 0) { if (timespec_diff(&ctx->now, &session->receive_last) > (int)ctx->conf->reorder_time*1000) diff --git a/src/methods/common.h b/src/methods/common.h index b5f7272..0bd7aae 100644 --- a/src/methods/common.h +++ b/src/methods/common.h @@ -53,18 +53,18 @@ bool fastd_method_reorder_check(fastd_context_t *ctx, fastd_peer_t *peer, fastd_ static inline bool fastd_method_session_common_is_valid(fastd_context_t *ctx, const fastd_method_common_t *session) { - if (session->send_nonce[COMMON_NONCEBYTES-1] == 0xff && session->send_nonce[COMMON_NONCEBYTES-2] == 0xff) + if (session->send_nonce[0] == 0xff && session->send_nonce[1] == 0xff) return false; return (timespec_after(&session->valid_till, &ctx->now)); } static inline bool fastd_method_session_common_is_initiator(const fastd_method_common_t *session) { - return (session->send_nonce[0] & 1); + return (session->send_nonce[COMMON_NONCEBYTES-1] & 1); } static inline bool fastd_method_session_common_want_refresh(fastd_context_t *ctx, const fastd_method_common_t *session) { - if (session->send_nonce[COMMON_NONCEBYTES-1] == 0xff) + if (session->send_nonce[0] == 0xff) return true; if (fastd_method_session_common_is_initiator(session) && timespec_after(&ctx->now, &session->refresh_after)) @@ -82,26 +82,25 @@ static inline void fastd_method_session_common_superseded(fastd_context_t *ctx, } static inline void fastd_method_increment_nonce(fastd_method_common_t *session) { - session->send_nonce[0] += 2; + session->send_nonce[COMMON_NONCEBYTES-1] += 2; - if (session->send_nonce[0] == 0 || session->send_nonce[0] == 1) { + if (!(session->send_nonce[COMMON_NONCEBYTES-1] & (~1))) { int i; - for (i = 1; i < COMMON_NONCEBYTES; i++) { - session->send_nonce[i]++; - if (session->send_nonce[i] != 0) + for (i = COMMON_NONCEBYTES-2; i >= 0; i--) { + if (++session->send_nonce[i]) break; } } } static inline void fastd_method_put_common_header(fastd_context_t *ctx, fastd_buffer_t *buffer, const uint8_t nonce[COMMON_NONCEBYTES], uint8_t flags) { - fastd_buffer_pull_head_from(ctx, buffer, &flags, 1); fastd_buffer_pull_head_from(ctx, buffer, nonce, COMMON_NONCEBYTES); + fastd_buffer_pull_head_from(ctx, buffer, &flags, 1); } static inline void fastd_method_take_common_header(fastd_context_t *ctx, fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags) { - fastd_buffer_push_head_to(ctx, buffer, nonce, COMMON_NONCEBYTES); fastd_buffer_push_head_to(ctx, buffer, flags, 1); + fastd_buffer_push_head_to(ctx, buffer, nonce, COMMON_NONCEBYTES); } static inline bool fastd_method_handle_common_header(fastd_context_t *ctx, const fastd_method_common_t *session, fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags, int64_t *age) { -- cgit v1.2.3