@@ -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 ;
0 commit comments