From f34484ceecc2cbefec3a40a1feca8ea8706e7d93 Mon Sep 17 00:00:00 2001 From: Neria Ifrah Date: Sun, 17 May 2026 19:49:13 +0300 Subject: [PATCH] modbus-tcp: apply IPv4 socket options to accepted sockets modbus_tcp_accept() did not call _modbus_tcp_set_ipv4_options() on the accepted socket, leaving Nagle's algorithm enabled and missing the IPTOS_LOWDELAY TOS marking on the server side. This created an asymmetry with the client connect path, where the same helper is already called after socket() and before connect(). With Nagle active on the server, the interaction with the peer's Delayed ACK mechanism (up to 40 ms per RFC 1122) can introduce latency in response delivery, which is undesirable in Modbus TCP where real-time response is a protocol expectation. The lack of IPTOS_LOWDELAY on responses also means server-side traffic does not receive low-latency prioritisation in QoS-aware infrastructure. Call _modbus_tcp_set_ipv4_options() on the accepted socket right after the FD_SETSIZE check, mirroring the client connect path. On failure, the socket is closed and the function returns -1, matching the existing error-handling style in the same function. The helper already contains the required platform guards (#ifndef OS_WIN32 for IP_TOS, !SOCK_NONBLOCK for FIONBIO), so no additional preprocessor logic is needed at the call site. Closes #849 --- src/modbus-tcp.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/modbus-tcp.c b/src/modbus-tcp.c index f1ec959b..785431ed 100644 --- a/src/modbus-tcp.c +++ b/src/modbus-tcp.c @@ -809,6 +809,18 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) return -1; } + if (_modbus_tcp_set_ipv4_options(ctx->s) == -1) { + if (ctx->debug) { + fprintf( + stderr, + "ERROR Failed to set IPv4 options on accepted socket %d\n", + ctx->s); + } + close(ctx->s); + ctx->s = -1; + return -1; + } + if (ctx->debug) { char buf[INET_ADDRSTRLEN]; if (inet_ntop(AF_INET, &(addr.sin_addr), buf, INET_ADDRSTRLEN) == NULL) {