Merge bitcoin/bitcoin#26078: p2p: return CSubNet in LookupSubNet

fb3e812277 p2p: return `CSubNet` in `LookupSubNet` (brunoerg)

Pull request description:

  Analyzing the usage of `LookupSubNet`, noticed that most cases uses check if the subnet is valid by calling `subnet.IsValid()`, and the boolean returned by `LookupSubNet` hasn't been used so much, see:
  29d540b7ad/src/httpserver.cpp (L172-L174)
  29d540b7ad/src/net_permissions.cpp (L114-L116)

  It makes sense to return `CSubNet` instead of `bool`.

ACKs for top commit:
  achow101:
    ACK fb3e812277
  vasild:
    ACK fb3e812277
  theStack:
    Code-review ACK fb3e812277
  stickies-v:
    Concept ACK, but Approach ~0 (for now). Reviewed the code (fb3e812277) and it all looks good to me.

Tree-SHA512: ba50d6bd5d58dfdbe1ce1faebd80dd8cf8c92ac53ef33519860b83399afffab482d5658cb6921b849d7a3df6d5cea911412850e08f3f4e27f7af510fbde4b254
This commit is contained in:
Andrew Chow
2023-10-26 14:21:40 -04:00
8 changed files with 101 additions and 115 deletions

View File

@@ -645,10 +645,12 @@ bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_
return true;
}
bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out)
CSubNet LookupSubNet(const std::string& subnet_str)
{
CSubNet subnet;
assert(!subnet.IsValid());
if (!ContainsNoNUL(subnet_str)) {
return false;
return subnet;
}
const size_t slash_pos{subnet_str.find_last_of('/')};
@@ -662,23 +664,21 @@ bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out)
uint8_t netmask;
if (ParseUInt8(netmask_str, &netmask)) {
// Valid number; assume CIDR variable-length subnet masking.
subnet_out = CSubNet{addr.value(), netmask};
return subnet_out.IsValid();
subnet = CSubNet{addr.value(), netmask};
} else {
// Invalid number; try full netmask syntax. Never allow lookup for netmask.
const std::optional<CNetAddr> full_netmask{LookupHost(netmask_str, /*fAllowLookup=*/false)};
if (full_netmask.has_value()) {
subnet_out = CSubNet{addr.value(), full_netmask.value()};
return subnet_out.IsValid();
subnet = CSubNet{addr.value(), full_netmask.value()};
}
}
} else {
// Single IP subnet (<ipv4>/32 or <ipv6>/128).
subnet_out = CSubNet{addr.value()};
return subnet_out.IsValid();
subnet = CSubNet{addr.value()};
}
}
return false;
return subnet;
}
void InterruptSocks5(bool interrupt)