mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-15 12:51:00 +02:00
Merge bitcoin/bitcoin#23219: p2p, refactor: tidy up LookupSubNet()
c44c20108fp2p, refactor: drop unused DNSLookupFn param in LookupSubnet() (Vasil Dimov)f0c9e68080p2p, refactor: tidy up LookupSubNet() (Jon Atack) Pull request description: This pull originally resolved a code `TO-DO`, as well as fixing different param names between the function declaration and definition, updating the function to current style standards, clearer variable naming, and improving the Doxygen documentation. Following the merge of #17160, it now does the non-`TODO` changes and also now drops an unused param to simplify the function. ACKs for top commit: dunxen: ACKc44c201vasild: ACKc44c20108fshaavan: crACKc44c20108fTree-SHA512: 55f64c7f403819dec84f4da06e63db50f7c0601a2d9a1ec196fda667c220ec6f5ad2a3c95e0e02275da9f6da6b984275d1dc10e19ed82005c5e13da5c5ecab02
This commit is contained in:
commit
c006ab29ce
3 changed files with 32 additions and 35 deletions
|
|
@ -676,40 +676,36 @@ bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uin
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LookupSubNet(const std::string& strSubnet, CSubNet& ret, DNSLookupFn dns_lookup_function)
|
bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out)
|
||||||
{
|
{
|
||||||
if (!ValidAsCString(strSubnet)) {
|
if (!ValidAsCString(subnet_str)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
size_t slash = strSubnet.find_last_of('/');
|
|
||||||
CNetAddr network;
|
|
||||||
|
|
||||||
std::string strAddress = strSubnet.substr(0, slash);
|
const size_t slash_pos{subnet_str.find_last_of('/')};
|
||||||
if (LookupHost(strAddress, network, false, dns_lookup_function))
|
const std::string str_addr{subnet_str.substr(0, slash_pos)};
|
||||||
{
|
CNetAddr addr;
|
||||||
if (slash != strSubnet.npos)
|
|
||||||
{
|
if (LookupHost(str_addr, addr, /*fAllowLookup=*/false)) {
|
||||||
std::string strNetmask = strSubnet.substr(slash + 1);
|
if (slash_pos != subnet_str.npos) {
|
||||||
uint8_t n;
|
const std::string netmask_str{subnet_str.substr(slash_pos + 1)};
|
||||||
if (ParseUInt8(strNetmask, &n)) {
|
uint8_t netmask;
|
||||||
// If valid number, assume CIDR variable-length subnet masking
|
if (ParseUInt8(netmask_str, &netmask)) {
|
||||||
ret = CSubNet(network, n);
|
// Valid number; assume CIDR variable-length subnet masking.
|
||||||
return ret.IsValid();
|
subnet_out = CSubNet{addr, netmask};
|
||||||
}
|
return subnet_out.IsValid();
|
||||||
else // If not a valid number, try full netmask syntax
|
} else {
|
||||||
{
|
// Invalid number; try full netmask syntax. Never allow lookup for netmask.
|
||||||
CNetAddr netmask;
|
CNetAddr full_netmask;
|
||||||
// Never allow lookup for netmask
|
if (LookupHost(netmask_str, full_netmask, /*fAllowLookup=*/false)) {
|
||||||
if (LookupHost(strNetmask, netmask, false, dns_lookup_function)) {
|
subnet_out = CSubNet{addr, full_netmask};
|
||||||
ret = CSubNet(network, netmask);
|
return subnet_out.IsValid();
|
||||||
return ret.IsValid();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else // Single IP subnet (<ipv4>/32 or <ipv6>/128)
|
// Single IP subnet (<ipv4>/32 or <ipv6>/128).
|
||||||
{
|
subnet_out = CSubNet{addr};
|
||||||
ret = CSubNet(network);
|
return subnet_out.IsValid();
|
||||||
return ret.IsValid();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -169,13 +169,14 @@ CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLoo
|
||||||
* Parse and resolve a specified subnet string into the appropriate internal
|
* Parse and resolve a specified subnet string into the appropriate internal
|
||||||
* representation.
|
* representation.
|
||||||
*
|
*
|
||||||
* @param strSubnet A string representation of a subnet of the form `network
|
* @param[in] subnet_str A string representation of a subnet of the form
|
||||||
* address [ "/", ( CIDR-style suffix | netmask ) ]`(e.g.
|
* `network address [ "/", ( CIDR-style suffix | netmask ) ]`
|
||||||
* `2001:db8::/32`, `192.0.2.0/255.255.255.0`, or `8.8.8.8`).
|
* e.g. "2001:db8::/32", "192.0.2.0/255.255.255.0" or "8.8.8.8".
|
||||||
*
|
* @param[out] subnet_out Internal subnet representation, if parsable/resolvable
|
||||||
* @returns Whether the operation succeeded or not.
|
* from `subnet_str`.
|
||||||
|
* @returns whether the operation succeeded or not.
|
||||||
*/
|
*/
|
||||||
bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet, DNSLookupFn dns_lookup_function = g_dns_lookup);
|
bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a TCP socket in the given address family.
|
* Create a TCP socket in the given address family.
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ FUZZ_TARGET(netbase_dns_lookup)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
CSubNet resolved_subnet;
|
CSubNet resolved_subnet;
|
||||||
if (LookupSubNet(name, resolved_subnet, fuzzed_dns_lookup_function)) {
|
if (LookupSubNet(name, resolved_subnet)) {
|
||||||
assert(resolved_subnet.IsValid());
|
assert(resolved_subnet.IsValid());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue