diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/fastd.c | 48 | ||||
-rw-r--r-- | src/task.c | 2 |
2 files changed, 30 insertions, 20 deletions
diff --git a/src/fastd.c b/src/fastd.c index d2bc392..f479238 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -618,26 +618,33 @@ static inline void update_time(fastd_context *ctx) { clock_gettime(CLOCK_MONOTONIC, &ctx->now); } +static inline void schedule_new_handshake(fastd_context *ctx, fastd_peer *peer) { + fastd_task_schedule_handshake(ctx, peer, fastd_rand(ctx, 17500, 22500)); +} + static void send_handshake(fastd_context *ctx, fastd_peer *peer) { - if (fastd_peer_may_connect(ctx, peer)) { - if (!fastd_peer_is_established(peer)) - fastd_peer_reset_socket(ctx, peer); + if (!fastd_peer_may_connect(ctx, peer)) { + schedule_new_handshake(ctx, peer); + return; + } - if (peer->sock) { - if (timespec_diff(&ctx->now, &peer->last_handshake) < ctx->conf->min_handshake_interval*1000 - && fastd_peer_address_equal(&peer->address, &peer->last_handshake_address)) { - pr_debug(ctx, "not sending a handshake to %P as we sent one a short time ago", peer); - } - else { - pr_debug(ctx, "sending handshake to %P...", peer); - peer->last_handshake = ctx->now; - peer->last_handshake_address = peer->address; - ctx->conf->protocol->handshake_init(ctx, peer->sock, &peer->address, peer->config); - } + if (!fastd_peer_is_established(peer)) + fastd_peer_reset_socket(ctx, peer); + + if (peer->sock) { + if (timespec_diff(&ctx->now, &peer->last_handshake) < ctx->conf->min_handshake_interval*1000 + && fastd_peer_address_equal(&peer->address, &peer->last_handshake_address)) { + pr_debug(ctx, "not sending a handshake to %P as we sent one a short time ago", peer); + } + else { + pr_debug(ctx, "sending handshake to %P...", peer); + peer->last_handshake = ctx->now; + peer->last_handshake_address = peer->address; + ctx->conf->protocol->handshake_init(ctx, peer->sock, &peer->address, peer->config); } } - fastd_task_schedule_handshake(ctx, peer, fastd_rand(ctx, 17500, 22500)); + schedule_new_handshake(ctx, peer); } static void handle_tasks(fastd_context *ctx) { @@ -645,10 +652,15 @@ static void handle_tasks(fastd_context *ctx) { while ((task = fastd_task_get(ctx)) != NULL) { switch (task->type) { case TASK_HANDSHAKE: - if (fastd_peer_is_dynamic(task->peer) && !(fastd_peer_is_floating(task->peer) && fastd_peer_is_established(task->peer))) - fastd_resolve_peer(ctx, task->peer); - else + if (fastd_peer_is_dynamic(task->peer) && !(fastd_peer_is_floating(task->peer) && fastd_peer_is_established(task->peer))) { + if (fastd_peer_may_connect(ctx, task->peer)) + fastd_resolve_peer(ctx, task->peer); + else + schedule_new_handshake(ctx, task->peer); + } + else { send_handshake(ctx, task->peer); + } break; case TASK_KEEPALIVE: @@ -47,8 +47,6 @@ void fastd_task_schedule_handshake(fastd_context *ctx, fastd_peer *peer, int tim return; } - pr_debug(ctx, "scheduling handshake with %P", peer); - fastd_task *task = malloc(sizeof(fastd_task)); task->type = TASK_HANDSHAKE; |