diff options
Diffstat (limited to 'src/receive.c')
-rw-r--r-- | src/receive.c | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/receive.c b/src/receive.c index 472b664..fa45dde 100644 --- a/src/receive.c +++ b/src/receive.c @@ -105,6 +105,35 @@ static inline bool allow_unknown_peers(fastd_context_t *ctx) { return ctx->conf->has_floating || ctx->conf->on_verify; } +static inline bool backoff_unknown(fastd_context_t *ctx, const fastd_peer_address_t *addr) { + size_t i; + 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)) + break; + + if (fastd_peer_address_equal(addr, &t->address)) { + pr_debug2(ctx, "sent a handshake to unknown address %I a short time ago, not sending again", addr); + return true; + } + } + + if (ctx->unknown_handshake_pos == 0) + ctx->unknown_handshake_pos = array_size(ctx->unknown_handshakes)-1; + else + ctx->unknown_handshake_pos--; + + 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; + + return false; +} + static inline void handle_socket_receive_unknown(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer) { const uint8_t *packet_type = buffer.data; fastd_buffer_push_head(ctx, &buffer, 1); @@ -112,7 +141,9 @@ static inline void handle_socket_receive_unknown(fastd_context_t *ctx, fastd_soc switch (*packet_type) { case PACKET_DATA: fastd_buffer_free(buffer); - ctx->conf->protocol->handshake_init(ctx, sock, local_addr, remote_addr, NULL); + + if (!backoff_unknown(ctx, remote_addr)) + ctx->conf->protocol->handshake_init(ctx, sock, local_addr, remote_addr, NULL); break; case PACKET_HANDSHAKE: |