Skip to content

[TCP] Apply IPv4 socket options to accepted sockets in modbus_tcp_accept#850

Open
NeriaIfrah wants to merge 1 commit into
stephane:masterfrom
NeriaIfrah:fix/issue-849-accept-socket-options
Open

[TCP] Apply IPv4 socket options to accepted sockets in modbus_tcp_accept#850
NeriaIfrah wants to merge 1 commit into
stephane:masterfrom
NeriaIfrah:fix/issue-849-accept-socket-options

Conversation

@NeriaIfrah
Copy link
Copy Markdown

Fixes #849.

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.

Change

A single call to _modbus_tcp_set_ipv4_options(ctx->s) is added in
modbus_tcp_accept immediately after the FD_SETSIZE validation. 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 necessary platform guards, so no additional preprocessor
logic is needed at the call site.

Scope

This PR intentionally addresses only the IPv4 path (modbus_tcp_accept).
The IPv6 / protocol-independent path (modbus_tcp_pi_accept) is left
for a follow-up, since the helper is IPv4-only (it sets IPPROTO_IP/
IP_TOS) and the IPv6 case needs a slightly different approach.

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 stephane#849
@cla-bot
Copy link
Copy Markdown

cla-bot Bot commented May 17, 2026

We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TCP] Missing TCP_NODELAY and socket options on server-accepted connections in modbus_tcp_accept

1 participant