Skip to content

Commit c006ab2

Browse files
committed
Merge bitcoin/bitcoin#23219: p2p, refactor: tidy up LookupSubNet()
c44c201 p2p, refactor: drop unused DNSLookupFn param in LookupSubnet() (Vasil Dimov) f0c9e68 p2p, 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: ACK c44c201 vasild: ACK c44c201 shaavan: crACK c44c201 Tree-SHA512: 55f64c7f403819dec84f4da06e63db50f7c0601a2d9a1ec196fda667c220ec6f5ad2a3c95e0e02275da9f6da6b984275d1dc10e19ed82005c5e13da5c5ecab02
2 parents cb27d60 + c44c201 commit c006ab2

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

src/netbase.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -676,40 +676,36 @@ bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uin
676676
return true;
677677
}
678678

679-
bool LookupSubNet(const std::string& strSubnet, CSubNet& ret, DNSLookupFn dns_lookup_function)
679+
bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out)
680680
{
681-
if (!ValidAsCString(strSubnet)) {
681+
if (!ValidAsCString(subnet_str)) {
682682
return false;
683683
}
684-
size_t slash = strSubnet.find_last_of('/');
685-
CNetAddr network;
686684

687-
std::string strAddress = strSubnet.substr(0, slash);
688-
if (LookupHost(strAddress, network, false, dns_lookup_function))
689-
{
690-
if (slash != strSubnet.npos)
691-
{
692-
std::string strNetmask = strSubnet.substr(slash + 1);
693-
uint8_t n;
694-
if (ParseUInt8(strNetmask, &n)) {
695-
// If valid number, assume CIDR variable-length subnet masking
696-
ret = CSubNet(network, n);
697-
return ret.IsValid();
698-
}
699-
else // If not a valid number, try full netmask syntax
700-
{
701-
CNetAddr netmask;
702-
// Never allow lookup for netmask
703-
if (LookupHost(strNetmask, netmask, false, dns_lookup_function)) {
704-
ret = CSubNet(network, netmask);
705-
return ret.IsValid();
685+
const size_t slash_pos{subnet_str.find_last_of('/')};
686+
const std::string str_addr{subnet_str.substr(0, slash_pos)};
687+
CNetAddr addr;
688+
689+
if (LookupHost(str_addr, addr, /*fAllowLookup=*/false)) {
690+
if (slash_pos != subnet_str.npos) {
691+
const std::string netmask_str{subnet_str.substr(slash_pos + 1)};
692+
uint8_t netmask;
693+
if (ParseUInt8(netmask_str, &netmask)) {
694+
// Valid number; assume CIDR variable-length subnet masking.
695+
subnet_out = CSubNet{addr, netmask};
696+
return subnet_out.IsValid();
697+
} else {
698+
// Invalid number; try full netmask syntax. Never allow lookup for netmask.
699+
CNetAddr full_netmask;
700+
if (LookupHost(netmask_str, full_netmask, /*fAllowLookup=*/false)) {
701+
subnet_out = CSubNet{addr, full_netmask};
702+
return subnet_out.IsValid();
706703
}
707704
}
708-
}
709-
else // Single IP subnet (<ipv4>/32 or <ipv6>/128)
710-
{
711-
ret = CSubNet(network);
712-
return ret.IsValid();
705+
} else {
706+
// Single IP subnet (<ipv4>/32 or <ipv6>/128).
707+
subnet_out = CSubNet{addr};
708+
return subnet_out.IsValid();
713709
}
714710
}
715711
return false;

src/netbase.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,14 @@ CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLoo
169169
* Parse and resolve a specified subnet string into the appropriate internal
170170
* representation.
171171
*
172-
* @param strSubnet A string representation of a subnet of the form `network
173-
* address [ "/", ( CIDR-style suffix | netmask ) ]`(e.g.
174-
* `2001:db8::/32`, `192.0.2.0/255.255.255.0`, or `8.8.8.8`).
175-
*
176-
* @returns Whether the operation succeeded or not.
172+
* @param[in] subnet_str A string representation of a subnet of the form
173+
* `network address [ "/", ( CIDR-style suffix | netmask ) ]`
174+
* e.g. "2001:db8::/32", "192.0.2.0/255.255.255.0" or "8.8.8.8".
175+
* @param[out] subnet_out Internal subnet representation, if parsable/resolvable
176+
* from `subnet_str`.
177+
* @returns whether the operation succeeded or not.
177178
*/
178-
bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet, DNSLookupFn dns_lookup_function = g_dns_lookup);
179+
bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out);
179180

180181
/**
181182
* Create a TCP socket in the given address family.

src/test/fuzz/netbase_dns_lookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ FUZZ_TARGET(netbase_dns_lookup)
6464
}
6565
{
6666
CSubNet resolved_subnet;
67-
if (LookupSubNet(name, resolved_subnet, fuzzed_dns_lookup_function)) {
67+
if (LookupSubNet(name, resolved_subnet)) {
6868
assert(resolved_subnet.IsValid());
6969
}
7070
}

0 commit comments

Comments
 (0)