From: Felix Fietkau Date: Wed, 29 Jan 2025 20:43:02 +0000 (+0100) Subject: pex: improve keepalive handling X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=082b5482b97f20dc4745bc3d645e33b584cc28e4;p=project%2Funetd.git pex: improve keepalive handling Whenever idle time exceeds keepalive time, ping up to once every 1+keepalive/2 interval. Avoids spuriously treating a peer as disconnected. Signed-off-by: Felix Fietkau --- diff --git a/host.h b/host.h index a63a60b..c678046 100644 --- a/host.h +++ b/host.h @@ -30,7 +30,6 @@ struct network_peer { bool connected; bool handshake; bool has_local_ep_addr; - bool pinged; union network_addr local_ep_addr; union network_endpoint endpoint; @@ -43,6 +42,7 @@ struct network_peer { uint64_t last_request; uint64_t last_query_sent; + int ping_wait; int last_handshake_diff; int idle; int num_net_queries; diff --git a/pex.c b/pex.c index db143fc..cc374af 100644 --- a/pex.c +++ b/pex.c @@ -389,12 +389,12 @@ network_pex_query_hosts(struct network *net) static void network_pex_send_ping(struct network *net, struct network_peer *peer) { - if (peer->state.pinged || !peer->state.endpoint.sa.sa_family) + if (peer->state.ping_wait > 0 || !peer->state.endpoint.sa.sa_family) return; pex_msg_init(net, PEX_MSG_PING); pex_msg_send(net, peer); - peer->state.pinged = true; + peer->state.ping_wait = 1 + net->net_config.keepalive / 2; } static void diff --git a/wg.c b/wg.c index 74e8aae..61554bc 100644 --- a/wg.c +++ b/wg.c @@ -52,6 +52,8 @@ struct network_peer *wg_peer_update_start(struct network *net, const uint8_t *ke peer->state.handshake = false; peer->state.idle++; + if (peer->state.ping_wait > 0) + peer->state.ping_wait--; if (peer->state.idle >= 2 * net->net_config.keepalive) wg_peer_set_connected(net, peer, false); if (peer->state.idle > net->net_config.keepalive) @@ -80,7 +82,6 @@ void wg_peer_set_last_handshake(struct network *net, struct network_peer *peer, if (peer->state.idle > sec) peer->state.idle = sec; wg_peer_set_connected(net, peer, true); - peer->state.pinged = false; } }