From 1ae3aae35193dce25e5534b12a46011ec7912bb4 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 24 Aug 2014 16:07:50 +0200 Subject: Use simple int64_t timestamps in ms instead of timespecs --- src/compat.h | 2 +- src/fastd.c | 6 ++--- src/fastd.h | 33 +++++++------------------- src/fastd_config.h.in | 26 ++++++++++---------- src/methods/common.c | 8 +++---- src/methods/common.h | 14 +++++------ src/peer.c | 34 ++++++++++----------------- src/peer.h | 26 ++++++++++---------- src/poll.c | 6 ++--- src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c | 2 +- src/protocols/ec25519_fhmqvc/handshake.c | 14 +++++------ src/protocols/ec25519_fhmqvc/handshake.h | 8 +++---- src/protocols/ec25519_fhmqvc/state.c | 4 ++-- src/receive.c | 4 ++-- src/resolve.c | 4 ++-- src/types.h | 4 ++++ 16 files changed, 88 insertions(+), 107 deletions(-) diff --git a/src/compat.h b/src/compat.h index c73a340..d5d60fc 100644 --- a/src/compat.h +++ b/src/compat.h @@ -116,7 +116,7 @@ static inline char *get_current_dir_name(void) { #define CLOCK_MONOTONIC 0 #define clockid_t int -static inline int clock_gettime(clockid_t clk_id __attribute__((unused)), struct timespec *tp) { +static inline int clock_gettime(clockid_t clk_id __attribute__((unused)), fastd_timeout_t *tp) { static mach_timebase_info_data_t timebase_info = {}; if (!timebase_info.denom) diff --git a/src/fastd.c b/src/fastd.c index 247b390..8c58718 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -485,7 +485,7 @@ static inline void init(int argc, char *argv[]) { init_config(&status_fd); fastd_update_time(); - ctx.next_maintenance = fastd_in_seconds(MAINTENANCE_INTERVAL); + ctx.next_maintenance = ctx.now + MAINTENANCE_INTERVAL; ctx.unknown_handshakes[0].timeout = ctx.now; #ifdef WITH_DYNAMIC_PEERS @@ -544,13 +544,13 @@ static inline void init(int argc, char *argv[]) { /** Performs periodic maintenance tasks */ static inline void maintenance(void) { - if (!fastd_timed_out(&ctx.next_maintenance)) + if (!fastd_timed_out(ctx.next_maintenance)) return; fastd_socket_handle_binds(); fastd_peer_maintenance(); - ctx.next_maintenance.tv_sec += MAINTENANCE_INTERVAL; + ctx.next_maintenance += MAINTENANCE_INTERVAL; } /** Reaps zombies of asynchronous shell commands. */ diff --git a/src/fastd.h b/src/fastd.h index ea30045..61d10f1 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -154,7 +154,7 @@ struct fastd_stats { /** A data structure keeping track of an unknown addresses that a handshakes was received from recently */ struct fastd_handshake_timeout { fastd_peer_address_t address; /**< An address a handshake was received from */ - struct timespec timeout; /**< Timeout until handshakes from this address are ignored */ + fastd_timeout_t timeout; /**< Timeout until handshakes from this address are ignored */ }; @@ -235,7 +235,7 @@ struct fastd_context { char *ifname; /**< The actual interface name */ - struct timespec now; /**< The current monotonous timestamp */ + int64_t now; /**< The current monotonous timestamp in microseconds after an arbitrary point in time */ uint64_t next_peer_id; /**< An monotonously increasing ID peers are identified with in some components */ VECTOR(fastd_peer_t*) peers; /**< The currectly active peers */ @@ -256,7 +256,7 @@ struct fastd_context { VECTOR(fastd_peer_t*) *peer_addr_ht; /**< An array of hash buckets for the peer hash table */ fastd_dlist_head_t handshake_queue; /**< A doubly linked list of the peers currently queued for handshakes (ordered by the time of the next handshake) */ - struct timespec next_maintenance; /**< The time of the next maintenance call */ + fastd_timeout_t next_maintenance; /**< The time of the next maintenance call */ VECTOR(pid_t) async_pids; /**< PIDs of asynchronously executed commands which still have to be reaped */ int async_rfd; /**< The read side of the pipe used to send data from other thread to the main thread */ @@ -435,17 +435,6 @@ static inline void fastd_string_stack_free(fastd_string_stack_t *str) { } } -/** Compares two timespecs and returns \em true if \p tp1 is after \p tp2 */ -static inline bool timespec_after(const struct timespec *tp1, const struct timespec *tp2) { - return (tp1->tv_sec > tp2->tv_sec || - (tp1->tv_sec == tp2->tv_sec && tp1->tv_nsec > tp2->tv_nsec)); -} - -/** Returns (\a tp1 - \a tp2) in milliseconds */ -static inline int timespec_diff(const struct timespec *tp1, const struct timespec *tp2) { - return ((tp1->tv_sec - tp2->tv_sec))*1000 + (tp1->tv_nsec - tp2->tv_nsec)/1000000; -} - /** Checks if a timeout has occured @@ -455,20 +444,16 @@ static inline int timespec_diff(const struct timespec *tp1, const struct timespe \note The current time is updated only once per main loop iteration, after waiting for input. */ -static inline bool fastd_timed_out(const struct timespec *timeout) { - return !timespec_after(timeout, &ctx.now); -} - -/** Returns a timespec that lies a given number of seconds in the future */ -static inline struct timespec fastd_in_seconds(const int seconds) { - struct timespec ret = ctx.now; - ret.tv_sec += seconds; - return ret; +static inline bool fastd_timed_out(fastd_timeout_t timeout) { + return timeout <= ctx.now; } /** Updates the current time */ static inline void fastd_update_time(void) { - clock_gettime(CLOCK_MONOTONIC, &ctx.now); + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + + ctx.now = (1000*(int64_t)ts.tv_sec) + ts.tv_nsec/1000000; } /** Checks if a on-verify command is set */ diff --git a/src/fastd_config.h.in b/src/fastd_config.h.in index 0860ab7..9949eca 100644 --- a/src/fastd_config.h.in +++ b/src/fastd_config.h.in @@ -101,46 +101,46 @@ /** The interval of periodic maintenance tasks */ -#define MAINTENANCE_INTERVAL 10 +#define MAINTENANCE_INTERVAL 10000 /* 10 seconds */ /** The time after which a keepalive should be sent */ -#define KEEPALIVE_TIMEOUT 15 +#define KEEPALIVE_TIMEOUT 15000 /* 15 seconds */ /** The time after with a peer is reset if no traffic is received from it */ -#define PEER_STALE_TIME 90 +#define PEER_STALE_TIME 90000 /* 90 seconds */ /** The time after which a peer's ethernet address is forgotten if it is not seen */ -#define ETH_ADDR_STALE_TIME 300 +#define ETH_ADDR_STALE_TIME 300000 /* 5 minutes */ /** The time after a packet is received and no packets with lower sequence numbers are accepted anymore */ -#define REORDER_TIME 10 +#define REORDER_TIME 10000 /** The minimum time that must pass between two on-verify calls on the same peer */ -#define MIN_VERIFY_INTERVAL 10 +#define MIN_VERIFY_INTERVAL 10000 /* 10 seconds */ /** How long a peer stays valid after a successful on-verify run */ -#define VERIFY_VALID_TIME 60 /* 1 minute */ +#define VERIFY_VALID_TIME 60000 /* 1 minute */ /** Maximum number of concurrent on-verify runs */ #define VERIFY_LIMIT 32 /** The minimum interval between two handshakes with a peer */ -#define MIN_HANDSHAKE_INTERVAL 15 +#define MIN_HANDSHAKE_INTERVAL 15000 /* 15 seconds */ /** The minimum interval between two resolves of the same remote */ -#define MIN_RESOLVE_INTERVAL 15 +#define MIN_RESOLVE_INTERVAL 15000 /* 15 seconds */ /** How long a session stays valid after a key is negotiated */ -#define KEY_VALID 3600 /* 60 minutes */ +#define KEY_VALID 3600000 /* 60 minutes */ /** How long an old session stays valid after a new session has been established */ -#define KEY_VALID_OLD 60 /* 1 minute */ +#define KEY_VALID_OLD 60000 /* 1 minute */ /** How many seconds after the establishment of a session we want to refresh the session */ -#define KEY_REFRESH 3300 /* 55 minutes */ +#define KEY_REFRESH 3300000 /* 55 minutes */ /** A random time up to KEY_REFRESH_SPLAY is subtracted from KEY_REFRESH */ -#define KEY_REFRESH_SPLAY 300 /* 5 minutes */ +#define KEY_REFRESH_SPLAY 300000 /* 5 minutes */ diff --git a/src/methods/common.c b/src/methods/common.c index c6d6519..51cd6e8 100644 --- a/src/methods/common.c +++ b/src/methods/common.c @@ -37,8 +37,8 @@ void fastd_method_common_init(fastd_method_common_t *session, bool initiator) { memset(session, 0, sizeof(*session)); - session->valid_till = fastd_in_seconds(KEY_VALID); - session->refresh_after = fastd_in_seconds(KEY_REFRESH - fastd_rand(0, KEY_REFRESH_SPLAY)); + session->valid_till = ctx.now + KEY_VALID; + session->refresh_after = ctx.now + KEY_REFRESH - fastd_rand(0, KEY_REFRESH_SPLAY); if (initiator) { session->send_nonce[COMMON_NONCEBYTES-1] = 3; @@ -65,7 +65,7 @@ bool fastd_method_is_nonce_valid(const fastd_method_common_t *session, const uin *age >>= 1; if (*age >= 0) { - if (fastd_timed_out(&session->reorder_timeout)) + if (fastd_timed_out(session->reorder_timeout)) return false; if (*age > 64) @@ -89,7 +89,7 @@ bool fastd_method_reorder_check(fastd_peer_t *peer, fastd_method_common_t *sessi session->receive_reorder_seen |= ((uint64_t)1 << (shift-1)); memcpy(session->receive_nonce, nonce, COMMON_NONCEBYTES); - session->reorder_timeout = fastd_in_seconds(REORDER_TIME); + session->reorder_timeout = ctx.now + 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 7a06f92..d200931 100644 --- a/src/methods/common.h +++ b/src/methods/common.h @@ -46,13 +46,13 @@ /** Common method session state */ typedef struct fastd_method_common { - struct timespec valid_till; /**< How long the session is valid */ - struct timespec refresh_after; /**< When to try refreshing the session */ + fastd_timeout_t valid_till; /**< How long the session is valid */ + fastd_timeout_t refresh_after; /**< When to try refreshing the session */ uint8_t send_nonce[COMMON_NONCEBYTES]; /**< The next nonce to use */ uint8_t receive_nonce[COMMON_NONCEBYTES]; /**< The hightest nonce received to far for this session */ - struct timespec reorder_timeout; /**< How long to packets with a lower sequence number (nonce) than the newest received */ + fastd_timeout_t reorder_timeout; /**< How long to packets with a lower sequence number (nonce) than the newest received */ uint64_t receive_reorder_seen; /**< Bitmap specifying which of the 64 sequence numbers (nonces) before \a receive_nonce have bit seen */ } fastd_method_common_t; @@ -71,7 +71,7 @@ static inline bool fastd_method_session_common_is_valid(const fastd_method_commo if (session->send_nonce[0] == 0xff && session->send_nonce[1] == 0xff) return false; - return (!fastd_timed_out(&session->valid_till)); + return (!fastd_timed_out(session->valid_till)); } /** @@ -92,7 +92,7 @@ static inline bool fastd_method_session_common_want_refresh(const fastd_method_c if (session->send_nonce[0] == 0xff) return true; - if (fastd_method_session_common_is_initiator(session) && fastd_timed_out(&session->refresh_after)) + if (fastd_method_session_common_is_initiator(session) && fastd_timed_out(session->refresh_after)) return true; return false; @@ -100,9 +100,9 @@ static inline bool fastd_method_session_common_want_refresh(const fastd_method_c /** The common \a session_superseded implementation */ static inline void fastd_method_session_common_superseded(fastd_method_common_t *session) { - struct timespec valid_max = fastd_in_seconds(KEY_VALID_OLD); + fastd_timeout_t valid_max = ctx.now + KEY_VALID_OLD; - if (timespec_after(&session->valid_till, &valid_max)) + if (valid_max < session->valid_till) session->valid_till = valid_max; } diff --git a/src/peer.c b/src/peer.c index aad752b..5dee686 100644 --- a/src/peer.c +++ b/src/peer.c @@ -258,21 +258,13 @@ void fastd_peer_reset_socket(fastd_peer_t *peer) { void fastd_peer_schedule_handshake(fastd_peer_t *peer, int delay) { fastd_peer_unschedule_handshake(peer); - peer->next_handshake = ctx.now; - - peer->next_handshake.tv_sec += delay/1000; - peer->next_handshake.tv_nsec += (delay%1000)*1000000; - - if (peer->next_handshake.tv_nsec > 1000000000) { - peer->next_handshake.tv_sec++; - peer->next_handshake.tv_nsec -= 1000000000; - } + peer->next_handshake = ctx.now + delay; fastd_dlist_head_t *list; for (list = &ctx.handshake_queue; list->next; list = list->next) { fastd_peer_t *entry = container_of(list->next, fastd_peer_t, handshake_entry); - if (timespec_after(&entry->next_handshake, &peer->next_handshake)) + if (entry->next_handshake > peer->next_handshake) break; } @@ -785,13 +777,13 @@ static void send_handshake(fastd_peer_t *peer, fastd_remote_t *next_remote) { return; } - if (!fastd_timed_out(&peer->last_handshake_timeout) + if (!fastd_timed_out(peer->last_handshake_timeout) && fastd_peer_address_equal(&peer->address, &peer->last_handshake_address)) { pr_debug("not sending a handshake to %P as we sent one a short time ago", peer); return; } - peer->last_handshake_timeout = fastd_in_seconds(MIN_HANDSHAKE_INTERVAL); + peer->last_handshake_timeout = ctx.now + MIN_HANDSHAKE_INTERVAL; peer->last_handshake_address = peer->address; conf.protocol->handshake_init(peer->sock, &peer->local_address, &peer->address, peer); } @@ -802,7 +794,7 @@ void fastd_peer_handle_handshake_queue(void) { return; fastd_peer_t *peer = container_of(ctx.handshake_queue.next, fastd_peer_t, handshake_entry); - if (!fastd_timed_out(&peer->next_handshake)) + if (!fastd_timed_out(peer->next_handshake)) return; fastd_peer_schedule_handshake_default(peer); @@ -875,7 +867,7 @@ void fastd_peer_eth_addr_add(fastd_peer_t *peer, fastd_eth_addr_t addr) { if (cmp == 0) { VECTOR_INDEX(ctx.eth_addrs, cur).peer = peer; - VECTOR_INDEX(ctx.eth_addrs, cur).timeout = fastd_in_seconds(ETH_ADDR_STALE_TIME); + VECTOR_INDEX(ctx.eth_addrs, cur).timeout = ctx.now + ETH_ADDR_STALE_TIME; return; /* We're done here. */ } else if (cmp < 0) { @@ -886,7 +878,7 @@ void fastd_peer_eth_addr_add(fastd_peer_t *peer, fastd_eth_addr_t addr) { } } - VECTOR_INSERT(ctx.eth_addrs, ((fastd_peer_eth_addr_t) {addr, peer, fastd_in_seconds(ETH_ADDR_STALE_TIME)}), min); + VECTOR_INSERT(ctx.eth_addrs, ((fastd_peer_eth_addr_t) {addr, peer, ctx.now + ETH_ADDR_STALE_TIME}), min); pr_debug("learned new MAC address %E on peer %P", &addr, peer); } @@ -911,11 +903,11 @@ fastd_peer_t* fastd_peer_find_by_eth_addr(const fastd_eth_addr_t addr) { static bool maintain_peer(fastd_peer_t *peer) { if (fastd_peer_is_dynamic(peer) || fastd_peer_is_established(peer)) { /* check for peer timeout */ - if (fastd_timed_out(&peer->timeout)) { + if (fastd_timed_out(peer->timeout)) { #ifdef WITH_DYNAMIC_PEERS if (fastd_peer_is_dynamic(peer) && - fastd_timed_out(&peer->verify_timeout) && - fastd_timed_out(&peer->verify_valid_timeout)) { + fastd_timed_out(peer->verify_timeout) && + fastd_timed_out(peer->verify_valid_timeout)) { fastd_peer_delete(peer); return false; } @@ -930,7 +922,7 @@ static bool maintain_peer(fastd_peer_t *peer) { if (!fastd_peer_is_established(peer)) return true; - if (!fastd_timed_out(&peer->keepalive_timeout)) + if (!fastd_timed_out(peer->keepalive_timeout)) return true; pr_debug2("sending keepalive to %P", peer); @@ -945,10 +937,10 @@ static void eth_addr_cleanup(void) { size_t i, deleted = 0; for (i = 0; i < VECTOR_LEN(ctx.eth_addrs); i++) { - if (fastd_timed_out(&VECTOR_INDEX(ctx.eth_addrs, i).timeout)) { + if (fastd_timed_out(VECTOR_INDEX(ctx.eth_addrs, i).timeout)) { deleted++; pr_debug("MAC address %E not seen for more than %u seconds, removing", - &VECTOR_INDEX(ctx.eth_addrs, i).addr, ETH_ADDR_STALE_TIME); + &VECTOR_INDEX(ctx.eth_addrs, i).addr, ETH_ADDR_STALE_TIME/1000); } else if (deleted) { VECTOR_INDEX(ctx.eth_addrs, i-deleted) = VECTOR_INDEX(ctx.eth_addrs, i); diff --git a/src/peer.h b/src/peer.h index 3ddead4..fde5bdf 100644 --- a/src/peer.h +++ b/src/peer.h @@ -69,8 +69,8 @@ struct fastd_peer { fastd_peer_config_state_t config_state; /**< Specifies the way this peer was configured and if it is enabled */ fastd_peer_state_t state; /**< The peer's state */ - struct timespec timeout; /**< The timeout after which the peer is reset */ - struct timespec keepalive_timeout; /**< The timeout after which a keepalive is sent to the peer */ + fastd_timeout_t timeout; /**< The timeout after which the peer is reset */ + fastd_timeout_t keepalive_timeout; /**< The timeout after which a keepalive is sent to the peer */ const fastd_peer_group_t *group; /**< The peer group the peer belongs to */ @@ -78,22 +78,22 @@ struct fastd_peer { ssize_t next_remote; /**< An index into the field remotes or -1 */ bool floating; /**< Specifies if the peer has any floating remotes */ - struct timespec next_handshake; /**< The time of the next handshake */ + fastd_timeout_t next_handshake; /**< The time of the next handshake */ fastd_dlist_head_t handshake_entry; /**< Entry in the handshake queue */ - struct timespec last_handshake_timeout; /**< No handshakes are sent to the peer until this timeout has occured to avoid flooding the peer */ + fastd_timeout_t last_handshake_timeout; /**< No handshakes are sent to the peer until this timeout has occured to avoid flooding the peer */ fastd_peer_address_t last_handshake_address; /**< The address the last handshake was sent to */ - struct timespec last_handshake_response_timeout; /**< All handshakes from last_handshake_address will be ignored until this timeout has occured */ + fastd_timeout_t last_handshake_response_timeout; /**< All handshakes from last_handshake_address will be ignored until this timeout has occured */ fastd_peer_address_t last_handshake_response_address; /**< The address the last handshake was received from */ - struct timespec establish_handshake_timeout; /**< A timeout during which all handshakes for this peer will be ignored after a new connection has been established */ + fastd_timeout_t establish_handshake_timeout; /**< A timeout during which all handshakes for this peer will be ignored after a new connection has been established */ const char *config_source_dir; /**< The directory this peer's configuration was loaded from */ #ifdef WITH_DYNAMIC_PEERS - struct timespec verify_timeout; /**< Specifies the minimum time after which on-verify may be run again */ - struct timespec verify_valid_timeout; /**< Specifies how long a peer stays valid after a successful on-verify run */ + fastd_timeout_t verify_timeout; /**< Specifies the minimum time after which on-verify may be run again */ + fastd_timeout_t verify_valid_timeout; /**< Specifies how long a peer stays valid after a successful on-verify run */ #endif fastd_protocol_key_t *key; /**< The peer's public key */ @@ -122,7 +122,7 @@ struct fastd_peer_group { struct fastd_peer_eth_addr { fastd_eth_addr_t addr; /**< The MAC address */ fastd_peer_t *peer; /**< The corresponding peer */ - struct timespec timeout; /**< Timeout after which the address entry will be purged */ + fastd_timeout_t timeout; /**< Timeout after which the address entry will be purged */ }; /** A remote entry */ @@ -134,7 +134,7 @@ struct fastd_remote { size_t current_address; /**< The index of the remote the next handshake will be sent to */ fastd_peer_address_t *addresses; /**< The IP addresses the remote was resolved to */ - struct timespec last_resolve_timeout; /**< Timeout before the remote must not be resolved again */ + fastd_timeout_t last_resolve_timeout; /**< Timeout before the remote must not be resolved again */ }; @@ -190,12 +190,12 @@ static inline void fastd_peer_unschedule_handshake(fastd_peer_t *peer) { #ifdef WITH_DYNAMIC_PEERS /** Call to signal that there is currently an asychronous on-verify command running for the peer */ static inline void fastd_peer_set_verifying(fastd_peer_t *peer) { - peer->verify_timeout = fastd_in_seconds(MIN_VERIFY_INTERVAL); + peer->verify_timeout = ctx.now + MIN_VERIFY_INTERVAL; } /** Marks the peer verification as successful or failed */ static inline void fastd_peer_set_verified(fastd_peer_t *peer, bool ok) { - peer->verify_valid_timeout = ok ? fastd_in_seconds(VERIFY_VALID_TIME) : ctx.now; + peer->verify_valid_timeout = ctx.now + (ok ? VERIFY_VALID_TIME : 0); } #endif @@ -252,7 +252,7 @@ static inline bool fastd_peer_is_established(const fastd_peer_t *peer) { /** Signals that a valid packet was received from the peer */ static inline void fastd_peer_seen(fastd_peer_t *peer) { - peer->timeout = fastd_in_seconds(PEER_STALE_TIME); + peer->timeout = ctx.now + PEER_STALE_TIME; } /** Checks if a peer uses dynamic sockets (which means that each connection attempt uses a new socket) */ diff --git a/src/poll.c b/src/poll.c index 3f94379..cee827b 100644 --- a/src/poll.c +++ b/src/poll.c @@ -57,7 +57,7 @@ static inline int handshake_timeout(void) { 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); + int diff_msec = peer->next_handshake - ctx.now; if (diff_msec < 0) return 0; else @@ -129,7 +129,7 @@ void fastd_poll_delete_peer(size_t i UNUSED) { void fastd_poll_handle(void) { - int maintenance_timeout = timespec_diff(&ctx.next_maintenance, &ctx.now); + int maintenance_timeout = ctx.next_maintenance - ctx.now; if (maintenance_timeout < 0) maintenance_timeout = 0; @@ -244,7 +244,7 @@ void fastd_poll_delete_peer(size_t i) { void fastd_poll_handle(void) { size_t i; - int maintenance_timeout = timespec_diff(&ctx.next_maintenance, &ctx.now); + int maintenance_timeout = ctx.next_maintenance - ctx.now; if (maintenance_timeout < 0) maintenance_timeout = 0; diff --git a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c index 080c555..8528074 100644 --- a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c +++ b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c @@ -172,7 +172,7 @@ static void session_send(fastd_peer_t *peer, fastd_buffer_t buffer, protocol_ses } fastd_send(peer->sock, &peer->local_address, &peer->address, peer, send_buffer, stat_size); - peer->keepalive_timeout = fastd_in_seconds(KEEPALIVE_TIMEOUT); + peer->keepalive_timeout = ctx.now + KEEPALIVE_TIMEOUT; } /** Encrypts and sends a packet to a peer */ diff --git a/src/protocols/ec25519_fhmqvc/handshake.c b/src/protocols/ec25519_fhmqvc/handshake.c index 8f468cd..94aa338 100644 --- a/src/protocols/ec25519_fhmqvc/handshake.c +++ b/src/protocols/ec25519_fhmqvc/handshake.c @@ -177,7 +177,7 @@ static bool establish(fastd_peer_t *peer, const fastd_method_info_t *method, fas return false; } - peer->establish_handshake_timeout = fastd_in_seconds(MIN_HANDSHAKE_INTERVAL); + peer->establish_handshake_timeout = ctx.now + MIN_HANDSHAKE_INTERVAL; fastd_peer_seen(peer); fastd_peer_set_established(peer); @@ -567,8 +567,8 @@ static fastd_peer_t * add_dynamic(fastd_socket_t *sock, const fastd_peer_address /** Is called when a handshake from a dynamic peer is received */ static bool handle_dynamic(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake, const fastd_method_info_t *method) { - if (handshake->type > 2 || !fastd_timed_out(&peer->verify_timeout)) - return !fastd_timed_out(&peer->verify_valid_timeout); + if (handshake->type > 2 || !fastd_timed_out(peer->verify_timeout)) + return !fastd_timed_out(peer->verify_valid_timeout); verify_data_t verify_data; memset(&verify_data, 0, sizeof(verify_data)); @@ -597,7 +597,7 @@ void fastd_protocol_ec25519_fhmqvc_handle_verify_return(fastd_peer_t *peer, fast const verify_data_t *data = protocol_data; - peer->last_handshake_response_timeout = fastd_in_seconds(MIN_HANDSHAKE_INTERVAL); + peer->last_handshake_response_timeout = ctx.now + MIN_HANDSHAKE_INTERVAL; peer->last_handshake_response_address = *remote_addr; respond_handshake(sock, local_addr, remote_addr, peer, &data->peer_handshake_key, method); } @@ -652,7 +652,7 @@ void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_socket_t *sock, const return; } - if (!fastd_timed_out(&peer->establish_handshake_timeout)) { + if (!fastd_timed_out(peer->establish_handshake_timeout)) { pr_debug("received repeated handshakes from %P[%I], ignoring", peer, remote_addr); return; } @@ -675,7 +675,7 @@ void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_socket_t *sock, const memcpy(&peer_handshake_key, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, PUBLICKEYBYTES); if (handshake->type == 1) { - if (!fastd_timed_out(&peer->last_handshake_response_timeout) + if (!fastd_timed_out(peer->last_handshake_response_timeout) && fastd_peer_address_equal(remote_addr, &peer->last_handshake_response_address)) { pr_debug("not responding to repeated handshake from %P[%I]", peer, remote_addr); return; @@ -683,7 +683,7 @@ void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_socket_t *sock, const pr_verbose("received handshake from %P[%I]%s%s", peer, remote_addr, handshake->peer_version ? " using fastd " : "", handshake->peer_version ?: ""); - peer->last_handshake_response_timeout = fastd_in_seconds(MIN_HANDSHAKE_INTERVAL); + peer->last_handshake_response_timeout = ctx.now + MIN_HANDSHAKE_INTERVAL; peer->last_handshake_response_address = *remote_addr; respond_handshake(sock, local_addr, remote_addr, peer, &peer_handshake_key, method); return; diff --git a/src/protocols/ec25519_fhmqvc/handshake.h b/src/protocols/ec25519_fhmqvc/handshake.h index a05e542..7779858 100644 --- a/src/protocols/ec25519_fhmqvc/handshake.h +++ b/src/protocols/ec25519_fhmqvc/handshake.h @@ -50,8 +50,8 @@ typedef struct handshake_key { */ uint64_t serial; - struct timespec preferred_till; /**< Specifies how long this keypair will be used for new handshakes */ - struct timespec valid_till; /**< Specifies how long handshakes using this keypair will be answered */ + fastd_timeout_t preferred_till; /**< Specifies how long this keypair will be used for new handshakes */ + fastd_timeout_t valid_till; /**< Specifies how long handshakes using this keypair will be answered */ keypair_t key; /**< The actual keypair */ } handshake_key_t; @@ -69,10 +69,10 @@ struct fastd_protocol_state { /** Checks if a handshake keypair is currently valid */ static inline bool is_handshake_key_valid(const handshake_key_t *handshake_key) { - return !fastd_timed_out(&handshake_key->valid_till); + return !fastd_timed_out(handshake_key->valid_till); } /** Checks if a handshake keypair is currently peferred */ static inline bool is_handshake_key_preferred(const handshake_key_t *handshake_key) { - return !fastd_timed_out(&handshake_key->preferred_till); + return !fastd_timed_out(handshake_key->preferred_till); } diff --git a/src/protocols/ec25519_fhmqvc/state.c b/src/protocols/ec25519_fhmqvc/state.c index d4a2a0e..79bb6e2 100644 --- a/src/protocols/ec25519_fhmqvc/state.c +++ b/src/protocols/ec25519_fhmqvc/state.c @@ -72,8 +72,8 @@ void fastd_protocol_ec25519_fhmqvc_maintenance(void) { new_handshake_key(&ctx.protocol_state->handshake_key.key); - ctx.protocol_state->handshake_key.preferred_till = fastd_in_seconds(15); - ctx.protocol_state->handshake_key.valid_till = fastd_in_seconds(30); + ctx.protocol_state->handshake_key.preferred_till = ctx.now + 15000; + ctx.protocol_state->handshake_key.valid_till = ctx.now + 30000; } } diff --git a/src/receive.c b/src/receive.c index 10f70e7..f4172f8 100644 --- a/src/receive.c +++ b/src/receive.c @@ -96,7 +96,7 @@ static bool backoff_unknown(const fastd_peer_address_t *addr) { 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 (fastd_timed_out(&t->timeout)) + if (fastd_timed_out(t->timeout)) break; if (fastd_peer_address_equal(addr, &t->address)) { @@ -113,7 +113,7 @@ static bool backoff_unknown(const fastd_peer_address_t *addr) { fastd_handshake_timeout_t *t = &ctx.unknown_handshakes[ctx.unknown_handshake_pos]; t->address = *addr; - t->timeout = fastd_in_seconds(MIN_HANDSHAKE_INTERVAL); + t->timeout = ctx.now + MIN_HANDSHAKE_INTERVAL; return false; } diff --git a/src/resolve.c b/src/resolve.c index 048c1b3..ba44ffe 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -116,14 +116,14 @@ void fastd_resolve_peer(fastd_peer_t *peer, fastd_remote_t *remote) { if (fastd_peer_is_dynamic(peer)) exit_bug("trying to resolve dynamic peer"); - if (!fastd_timed_out(&remote->last_resolve_timeout)) { + if (!fastd_timed_out(remote->last_resolve_timeout)) { /* last resolve was just a few seconds ago */ return; } pr_verbose("resolving host `%s' for peer %P...", remote->hostname, peer); - remote->last_resolve_timeout = fastd_in_seconds(MIN_RESOLVE_INTERVAL); + remote->last_resolve_timeout = ctx.now + MIN_RESOLVE_INTERVAL; resolv_arg_t *arg = fastd_new(resolv_arg_t); diff --git a/src/types.h b/src/types.h index 927d707..352e7be 100644 --- a/src/types.h +++ b/src/types.h @@ -76,6 +76,10 @@ typedef enum fastd_drop_caps { } fastd_drop_caps_t; +/** A timestamp used as a timeout */ +typedef int64_t fastd_timeout_t; + + typedef struct fastd_buffer fastd_buffer_t; typedef union fastd_peer_address fastd_peer_address_t; -- cgit v1.2.3