Skip to content

Commit f0c9e68

Browse files
committed
p2p, refactor: tidy up LookupSubNet()
- consistent param naming between function declaration and definition - brackets, param naming and localvar naming per current standards in doc/developer-notes.md - update/improve doxygen documentation in the declaration - improve comments and other localvar names - constness - named args
1 parent 084c81c commit f0c9e68

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
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, DNSLookupFn dns_lookup_function)
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, dns_lookup_function)) {
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, dns_lookup_function)) {
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, DNSLookupFn dns_lookup_function = g_dns_lookup);
179180

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

0 commit comments

Comments
 (0)