Skip to content

Listen(port=0) - htons the port copied to the next addrinfo#183

Open
Vizit0r wants to merge 1 commit into
winddriver:masterfrom
Vizit0r:fix/listen-port-htons
Open

Listen(port=0) - htons the port copied to the next addrinfo#183
Vizit0r wants to merge 1 commit into
winddriver:masterfrom
Vizit0r:fix/listen-port-htons

Conversation

@Vizit0r
Copy link
Copy Markdown

@Vizit0r Vizit0r commented May 29, 2026

When Listen(port=0) resolves to multiple addrinfos (typically IPv6 + IPv4) the first listener binds an ephemeral port, then that port is copied into the next addrinfo's sin_port so all listeners share it. But sin_port is in network byte order, while LListen.LocalPort is host order (the integer parsed out of getnameinfo's NI_NUMERICSERV string in TSocketAPI.ExtractAddrInfo), so without htons() the second listener binds a byte-swapped port -- the dual-stack port-0 listen no longer shares the port.

Fixed in all three backends (Kqueue/Epoll/Iocp). The Connect paths in the same units already use htons(ALocalPort), confirming intent.

When Listen(port=0) resolves to multiple addrinfos (typically IPv6 + IPv4) the first listener binds an ephemeral port, then that port is
copied into the next addrinfo's sin_port so all listeners share it. But sin_port is in network byte order, while LListen.LocalPort is host
order (the integer parsed out of getnameinfo's NI_NUMERICSERV string in TSocketAPI.ExtractAddrInfo), so without htons() the second listener
binds a byte-swapped port -- the dual-stack port-0 listen no longer shares the port.

Fixed in all three backends (Kqueue/Epoll/Iocp). The Connect paths in the same units already use htons(ALocalPort), confirming intent.
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.

1 participant