Thanks @kevkevinpal. Good example of why review is valued. Your findings nudged me to add some assert test statements (which otherwise wouldn’t be present) to help cover cases involving IPv6 hosts.
We’re experiencing the result of the scope of SplitHostPort()
. SplitHostPort()
extracts the host and port. Although the port is validated, host is not (and the function declaration comment header mentions validating the port, but doesn’t mention validating the host).
https://github.com/bitcoin/bitcoin/blob/7fee0ca014b1513e836d2550d1b7512739d5a79a/src/util/strencodings.h#L97-L106
SplitHostPort()
is treating “127.0.0.1::” as an IPv6 host since “::” is used for IPv6 address shortening. SplitHostPort()
detects no port as part of the input string, which would be why Could not connect to the server 127.0.0.1:::18443
is the error provided for src/bitcoin-cli -rpcconnect=127.0.0.1:: getblockcount
(the “:18443” is tacked on the end because it’s the default port for regtest).
An enhancement would be to perform a validation of the host received from SplitHostPort()
before bitcoin-cli attempts to contact the host. I’m thinking we would need to first define what is “valid” and what is “invalid” for use with bitcoin-cli. The gist of this kind of checking seems to be present in CNetAddr
. In general, I would think bitcoin-cli shouldn’t be overly restrictive in what it lets the user attempt to use as a host.
https://github.com/bitcoin/bitcoin/blob/7fee0ca014b1513e836d2550d1b7512739d5a79a/src/netaddress.h#L157-L181
For now, it might make the most sense to leave the enhancement of host validation to a new PR since this PR is focused on port error detection.