summaryrefslogtreecommitdiffstats
path: root/src/receive.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/receive.c')
-rw-r--r--src/receive.c58
1 files changed, 30 insertions, 28 deletions
diff --git a/src/receive.c b/src/receive.c
index 19b450a..42dbe5a 100644
--- a/src/receive.c
+++ b/src/receive.c
@@ -76,6 +76,33 @@ static inline void handle_socket_control(struct msghdr *message, const fastd_soc
}
}
+static 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 (fastd_timed_out(ctx, &t->timeout))
+ 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 = fastd_in_seconds(ctx, ctx->conf->min_handshake_interval);
+
+ return false;
+}
+
static inline void handle_socket_receive_known(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer) {
if (!fastd_peer_may_connect(ctx, peer)) {
fastd_buffer_free(buffer);
@@ -89,7 +116,9 @@ static inline void handle_socket_receive_known(fastd_context_t *ctx, fastd_socke
case PACKET_DATA:
if (!fastd_peer_is_established(peer) || !fastd_peer_address_equal(&peer->local_address, local_addr)) {
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);
return;
}
@@ -105,33 +134,6 @@ 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 (fastd_timed_out(ctx, &t->timeout))
- 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 = fastd_in_seconds(ctx, 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);