mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-09 01:40:27 +02:00
net: fix GetListenPort() to derive the proper port
`GetListenPort()` uses a simple logic: "if `-port=P` is given, then we must be listening on `P`, otherwise we must be listening on `8333`". This is however not true if `-bind=` has been provided with `:port` part or if `-whitebind=` has been provided. Thus, extend `GetListenPort()` to return the port from `-bind=` or `-whitebind=`, if any. Fixes https://github.com/bitcoin/bitcoin/issues/20184 (cases 1. 2. 3. 5.)
This commit is contained in:
37
src/net.cpp
37
src/net.cpp
@ -126,6 +126,31 @@ void CConnman::AddAddrFetch(const std::string& strDest)
|
||||
|
||||
uint16_t GetListenPort()
|
||||
{
|
||||
// If -bind= is provided with ":port" part, use that (first one if multiple are provided).
|
||||
for (const std::string& bind_arg : gArgs.GetArgs("-bind")) {
|
||||
CService bind_addr;
|
||||
constexpr uint16_t dummy_port = 0;
|
||||
|
||||
if (Lookup(bind_arg, bind_addr, dummy_port, /*fAllowLookup=*/false)) {
|
||||
if (bind_addr.GetPort() != dummy_port) {
|
||||
return bind_addr.GetPort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, if -whitebind= without NetPermissionFlags::NoBan is provided, use that
|
||||
// (-whitebind= is required to have ":port").
|
||||
for (const std::string& whitebind_arg : gArgs.GetArgs("-whitebind")) {
|
||||
NetWhitebindPermissions whitebind;
|
||||
bilingual_str error;
|
||||
if (NetWhitebindPermissions::TryParse(whitebind_arg, whitebind, error)) {
|
||||
if (!NetPermissions::HasFlag(whitebind.m_flags, NetPermissionFlags::NoBan)) {
|
||||
return whitebind.m_service.GetPort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, if -port= is provided, use that. Otherwise use the default port.
|
||||
return static_cast<uint16_t>(gArgs.GetIntArg("-port", Params().GetDefaultPort()));
|
||||
}
|
||||
|
||||
@ -221,7 +246,17 @@ std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
|
||||
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
|
||||
rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
|
||||
{
|
||||
addrLocal.SetIP(pnode->GetAddrLocal());
|
||||
if (pnode->IsInboundConn()) {
|
||||
// For inbound connections, assume both the address and the port
|
||||
// as seen from the peer.
|
||||
addrLocal = CAddress{pnode->GetAddrLocal(), addrLocal.nServices};
|
||||
} else {
|
||||
// For outbound connections, assume just the address as seen from
|
||||
// the peer and leave the port in `addrLocal` as returned by
|
||||
// `GetLocalAddress()` above. The peer has no way to observe our
|
||||
// listening port when we have initiated the connection.
|
||||
addrLocal.SetIP(pnode->GetAddrLocal());
|
||||
}
|
||||
}
|
||||
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
|
||||
{
|
||||
|
Reference in New Issue
Block a user