-
Notifications
You must be signed in to change notification settings - Fork 35.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document listening on port 0 assigns a random unused port #24116
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's not documented by the APIs, it's undefined behaviour, and we shouldn't document it either (unless we explicitly add code to support it).
I could not reproduce this on Win 10 based on below logs for
|
The log does not report the actual listening port when 0 is selected. Try |
I think we should just abort initialization if port 0 is requested. It's not actually something you can listen on, and the apparently default behavior of selecting a random port is undesirable for us. |
IMO that means it isn't working as described by this PR. Do we even recognise the assigned port for addr publishing purposes?
I agree. It seems code would be needed to support a random port, and I don't see justification for the added complexity that entails. Better to just make it an error. |
Agree |
Closing in favor of a new PR to do bounds checking on |
0452678 Validate `port` options (amadeuszpawlik) f8387c4 Validate port value in `SplitHostPort` (amadeuszpawlik) Pull request description: Validate `port`-options, so that invalid values are rejected early in the startup. Ports are `uint16_t`s, which effectively limits a port's value to <=65535. As discussed in bitcoin/bitcoin#24116 and bitcoin/bitcoin#24344, port "0" is considered invalid too. Proposed in bitcoin/bitcoin#21893 (comment) The `SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut)` now returns a bool that indicates whether the port value was set and within the allowed range. This is an improvement that can be used not only for port validation of options at startup, but also in rpc calls, etc, ACKs for top commit: luke-jr: utACK 0452678 ryanofsky: Code review ACK 0452678. Just suggested changes since last review: reverting some SplitHostPort changes, adding release notes, avoiding 'GetArgs[0]` problem. Tree-SHA512: f1ac80bf98520b287a6413ceadb41bc3a93c491955de9b9319ee1298ac0ab982751905762a287e748997ead6198a8bb7a3bc8817ac9e3d2468e11ab4a0f8496d
If port 0 is supplied, the Linux kernel will select an available port. Reference: https://github.com/torvalds/linux/blob/2c271fe77d52a0555161926c232cd5bc07178b39/net/ipv4/inet_connection_sock.c#L358
This is also true for Windows: https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind#remarks
Since this behavior does not appear in manpages for bind(), listen(), etc. it is not reasonable to expect node operators to know this behavior exists, so it's best to document it where they will find it: in the help text.