diff options
author | Jon Atack <jon@atack.com> | 2021-10-06 22:26:18 +0200 |
---|---|---|
committer | Jon Atack <jon@atack.com> | 2021-12-07 13:13:18 +0100 |
commit | f0c9e68080432c1ab11b14e571b8dfb7cfe727f8 (patch) | |
tree | d38e4f34d36866bb682fce2926eddf61f7975ca5 /src/netbase.cpp | |
parent | 084c81c8b6ef4c185cb0554b530fd940fbc631e5 (diff) |
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
Diffstat (limited to 'src/netbase.cpp')
-rw-r--r-- | src/netbase.cpp | 52 |
1 files changed, 24 insertions, 28 deletions
diff --git a/src/netbase.cpp b/src/netbase.cpp index 3cb12f1abc..5d7d45f2ef 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -676,40 +676,36 @@ bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uin return true; } -bool LookupSubNet(const std::string& strSubnet, CSubNet& ret, DNSLookupFn dns_lookup_function) +bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out, DNSLookupFn dns_lookup_function) { - if (!ValidAsCString(strSubnet)) { + if (!ValidAsCString(subnet_str)) { return false; } - size_t slash = strSubnet.find_last_of('/'); - CNetAddr network; - std::string strAddress = strSubnet.substr(0, slash); - if (LookupHost(strAddress, network, false, dns_lookup_function)) - { - if (slash != strSubnet.npos) - { - std::string strNetmask = strSubnet.substr(slash + 1); - uint8_t n; - if (ParseUInt8(strNetmask, &n)) { - // If valid number, assume CIDR variable-length subnet masking - ret = CSubNet(network, n); - return ret.IsValid(); - } - else // If not a valid number, try full netmask syntax - { - CNetAddr netmask; - // Never allow lookup for netmask - if (LookupHost(strNetmask, netmask, false, dns_lookup_function)) { - ret = CSubNet(network, netmask); - return ret.IsValid(); + const size_t slash_pos{subnet_str.find_last_of('/')}; + const std::string str_addr{subnet_str.substr(0, slash_pos)}; + CNetAddr addr; + + if (LookupHost(str_addr, addr, /*fAllowLookup=*/false, dns_lookup_function)) { + if (slash_pos != subnet_str.npos) { + const std::string netmask_str{subnet_str.substr(slash_pos + 1)}; + uint8_t netmask; + if (ParseUInt8(netmask_str, &netmask)) { + // Valid number; assume CIDR variable-length subnet masking. + subnet_out = CSubNet{addr, netmask}; + return subnet_out.IsValid(); + } else { + // Invalid number; try full netmask syntax. Never allow lookup for netmask. + CNetAddr full_netmask; + if (LookupHost(netmask_str, full_netmask, /*fAllowLookup=*/false, dns_lookup_function)) { + subnet_out = CSubNet{addr, full_netmask}; + return subnet_out.IsValid(); } } - } - else // Single IP subnet (<ipv4>/32 or <ipv6>/128) - { - ret = CSubNet(network); - return ret.IsValid(); + } else { + // Single IP subnet (<ipv4>/32 or <ipv6>/128). + subnet_out = CSubNet{addr}; + return subnet_out.IsValid(); } } return false; |