summaryrefslogtreecommitdiffstats
path: root/src/receive.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-01-26 01:52:00 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-01-26 01:52:00 +0100
commit23d90c62f2807bda87c6021785e6e51d3c627e0c (patch)
tree6327ca4bde6a9b4a796cabac53dd8fd87070bb3c /src/receive.c
parent8430688acb6e922bba903f04f560744736853c07 (diff)
downloadfastd-23d90c62f2807bda87c6021785e6e51d3c627e0c.tar
fastd-23d90c62f2807bda87c6021785e6e51d3c627e0c.zip
Add minimum handshake interval for unknown addresses (handles up to 8 addresses for now)
Diffstat (limited to 'src/receive.c')
-rw-r--r--src/receive.c33
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: