LCOV - code coverage report
Current view: top level - src - netbase.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 212 443 47.9 %
Date: 2024-05-24 08:22:33 Functions: 23 36 63.9 %
Branches: 219 773 28.3 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2022 The Bitcoin Core developers
       3                 :            : // Distributed under the MIT software license, see the accompanying
       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :            : 
       6                 :            : #include <config/bitcoin-config.h> // IWYU pragma: keep
       7                 :            : 
       8                 :            : #include <netbase.h>
       9                 :            : 
      10                 :            : #include <compat/compat.h>
      11                 :            : #include <logging.h>
      12                 :            : #include <sync.h>
      13                 :            : #include <tinyformat.h>
      14                 :            : #include <util/sock.h>
      15                 :            : #include <util/strencodings.h>
      16                 :            : #include <util/string.h>
      17                 :            : #include <util/time.h>
      18                 :            : 
      19                 :            : #include <atomic>
      20                 :            : #include <chrono>
      21                 :            : #include <cstdint>
      22                 :            : #include <functional>
      23                 :            : #include <limits>
      24                 :            : #include <memory>
      25                 :            : 
      26                 :            : #if HAVE_SOCKADDR_UN
      27                 :            : #include <sys/un.h>
      28                 :            : #endif
      29                 :            : 
      30                 :            : // Settings
      31                 :            : static GlobalMutex g_proxyinfo_mutex;
      32   [ +  -  +  + ]:          7 : static Proxy proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex);
      33                 :          1 : static Proxy nameProxy GUARDED_BY(g_proxyinfo_mutex);
      34                 :            : int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
      35                 :            : bool fNameLookup = DEFAULT_NAME_LOOKUP;
      36                 :            : 
      37                 :            : // Need ample time for negotiation for very slow proxies such as Tor
      38                 :            : std::chrono::milliseconds g_socks5_recv_timeout = 20s;
      39                 :          1 : CThreadInterrupt g_socks5_interrupt;
      40                 :            : 
      41                 :          1 : ReachableNets g_reachable_nets;
      42                 :            : 
      43                 :     770199 : std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup)
      44                 :            : {
      45                 :     770199 :     addrinfo ai_hint{};
      46                 :            :     // We want a TCP port, which is a streaming socket type
      47                 :     770199 :     ai_hint.ai_socktype = SOCK_STREAM;
      48                 :     770199 :     ai_hint.ai_protocol = IPPROTO_TCP;
      49                 :            :     // We don't care which address family (IPv4 or IPv6) is returned
      50                 :     770199 :     ai_hint.ai_family = AF_UNSPEC;
      51                 :            :     // If we allow lookups of hostnames, use the AI_ADDRCONFIG flag to only
      52                 :            :     // return addresses whose family we have an address configured for.
      53                 :            :     //
      54                 :            :     // If we don't allow lookups, then use the AI_NUMERICHOST flag for
      55                 :            :     // getaddrinfo to only decode numerical network addresses and suppress
      56                 :            :     // hostname lookups.
      57                 :     770199 :     ai_hint.ai_flags = allow_lookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
      58                 :            : 
      59                 :     770199 :     addrinfo* ai_res{nullptr};
      60                 :     770199 :     const int n_err{getaddrinfo(name.c_str(), nullptr, &ai_hint, &ai_res)};
      61         [ +  + ]:     770199 :     if (n_err != 0) {
      62                 :     644432 :         return {};
      63                 :            :     }
      64                 :            : 
      65                 :            :     // Traverse the linked list starting with ai_trav.
      66                 :     125767 :     addrinfo* ai_trav{ai_res};
      67                 :     125767 :     std::vector<CNetAddr> resolved_addresses;
      68         [ +  + ]:     251534 :     while (ai_trav != nullptr) {
      69         [ +  + ]:     125767 :         if (ai_trav->ai_family == AF_INET) {
      70         [ +  - ]:      57728 :             assert(ai_trav->ai_addrlen >= sizeof(sockaddr_in));
      71         [ +  - ]:      57728 :             resolved_addresses.emplace_back(reinterpret_cast<sockaddr_in*>(ai_trav->ai_addr)->sin_addr);
      72                 :      57728 :         }
      73         [ +  + ]:     125767 :         if (ai_trav->ai_family == AF_INET6) {
      74         [ +  - ]:      68040 :             assert(ai_trav->ai_addrlen >= sizeof(sockaddr_in6));
      75                 :      68039 :             const sockaddr_in6* s6{reinterpret_cast<sockaddr_in6*>(ai_trav->ai_addr)};
      76         [ +  - ]:      68039 :             resolved_addresses.emplace_back(s6->sin6_addr, s6->sin6_scope_id);
      77                 :      68039 :         }
      78                 :     125767 :         ai_trav = ai_trav->ai_next;
      79                 :            :     }
      80                 :     125767 :     freeaddrinfo(ai_res);
      81                 :            : 
      82                 :     125767 :     return resolved_addresses;
      83         [ +  - ]:     770199 : }
      84                 :            : 
      85                 :          1 : DNSLookupFn g_dns_lookup{WrappedGetAddrInfo};
      86                 :            : 
      87                 :       1023 : enum Network ParseNetwork(const std::string& net_in) {
      88                 :       1023 :     std::string net = ToLower(net_in);
      89   [ +  -  +  + ]:       1023 :     if (net == "ipv4") return NET_IPV4;
      90   [ +  -  +  + ]:       1021 :     if (net == "ipv6") return NET_IPV6;
      91   [ +  -  +  + ]:       1019 :     if (net == "onion") return NET_ONION;
      92   [ +  -  +  + ]:       1017 :     if (net == "tor") {
      93   [ +  -  +  -  :          5 :         LogPrintf("Warning: net name 'tor' is deprecated and will be removed in the future. You should use 'onion' instead.\n");
                   -  + ]
      94                 :          5 :         return NET_ONION;
      95                 :            :     }
      96   [ +  -  +  + ]:       1012 :     if (net == "i2p") {
      97                 :          2 :         return NET_I2P;
      98                 :            :     }
      99   [ +  -  +  + ]:       1010 :     if (net == "cjdns") {
     100                 :          2 :         return NET_CJDNS;
     101                 :            :     }
     102                 :       1008 :     return NET_UNROUTABLE;
     103                 :       1023 : }
     104                 :            : 
     105                 :      11301 : std::string GetNetworkName(enum Network net)
     106                 :            : {
     107   [ +  +  +  +  :      11301 :     switch (net) {
             +  -  +  -  
                      - ]
     108         [ +  - ]:         21 :     case NET_UNROUTABLE: return "not_publicly_routable";
     109         [ +  - ]:       2256 :     case NET_IPV4: return "ipv4";
     110         [ +  - ]:       2256 :     case NET_IPV6: return "ipv6";
     111         [ +  - ]:       2256 :     case NET_ONION: return "onion";
     112         [ +  - ]:       2256 :     case NET_I2P: return "i2p";
     113         [ +  - ]:       2256 :     case NET_CJDNS: return "cjdns";
     114         [ #  # ]:          0 :     case NET_INTERNAL: return "internal";
     115                 :          0 :     case NET_MAX: assert(false);
     116                 :            :     } // no default case, so the compiler can warn about missing cases
     117                 :            : 
     118                 :          0 :     assert(false);
     119                 :      11301 : }
     120                 :            : 
     121                 :       2251 : std::vector<std::string> GetNetworkNames(bool append_unroutable)
     122                 :            : {
     123                 :       2251 :     std::vector<std::string> names;
     124         [ +  + ]:      18008 :     for (int n = 0; n < NET_MAX; ++n) {
     125                 :      15757 :         const enum Network network{static_cast<Network>(n)};
     126   [ +  +  +  + ]:      15757 :         if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
     127   [ +  -  +  - ]:      11255 :         names.emplace_back(GetNetworkName(network));
     128      [ -  +  + ]:      15757 :     }
     129         [ +  + ]:       2251 :     if (append_unroutable) {
     130   [ +  -  +  - ]:         21 :         names.emplace_back(GetNetworkName(NET_UNROUTABLE));
     131                 :         21 :     }
     132                 :       2251 :     return names;
     133         [ +  - ]:       2251 : }
     134                 :            : 
     135                 :     794574 : static std::vector<CNetAddr> LookupIntern(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
     136                 :            : {
     137         [ +  - ]:     794574 :     if (!ContainsNoNUL(name)) return {};
     138                 :            :     {
     139                 :     794574 :         CNetAddr addr;
     140                 :            :         // From our perspective, onion addresses are not hostnames but rather
     141                 :            :         // direct encodings of CNetAddr much like IPv4 dotted-decimal notation
     142                 :            :         // or IPv6 colon-separated hextet notation. Since we can't use
     143                 :            :         // getaddrinfo to decode them and it wouldn't make sense to resolve
     144                 :            :         // them, we return a network address representing it instead. See
     145                 :            :         // CNetAddr::SetSpecial(const std::string&) for more details.
     146   [ +  -  +  +  :     794574 :         if (addr.SetSpecial(name)) return {addr};
             +  -  +  - ]
     147      [ -  +  + ]:     794574 :     }
     148                 :            : 
     149                 :     772624 :     std::vector<CNetAddr> addresses;
     150                 :            : 
     151   [ +  -  +  + ]:    1069253 :     for (const CNetAddr& resolved : dns_lookup_function(name, fAllowLookup)) {
     152   [ +  +  +  + ]:     296629 :         if (nMaxSolutions > 0 && addresses.size() >= nMaxSolutions) {
     153                 :        283 :             break;
     154                 :            :         }
     155                 :            :         /* Never allow resolving to an internal address. Consider any such result invalid */
     156   [ +  -  +  + ]:     296346 :         if (!resolved.IsInternal()) {
     157         [ +  - ]:     292328 :             addresses.push_back(resolved);
     158                 :     292328 :         }
     159         [ +  + ]:     296629 :     }
     160                 :            : 
     161                 :     772624 :     return addresses;
     162         [ +  - ]:    1567198 : }
     163                 :            : 
     164                 :      44857 : std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
     165                 :            : {
     166         [ +  + ]:      44857 :     if (!ContainsNoNUL(name)) return {};
     167                 :      44849 :     std::string strHost = name;
     168         [ +  + ]:      44849 :     if (strHost.empty()) return {};
     169   [ +  +  +  + ]:      44000 :     if (strHost.front() == '[' && strHost.back() == ']') {
     170         [ -  + ]:        120 :         strHost = strHost.substr(1, strHost.size() - 2);
     171                 :        120 :     }
     172                 :            : 
     173   [ +  -  +  - ]:      44000 :     return LookupIntern(strHost, nMaxSolutions, fAllowLookup, dns_lookup_function);
     174                 :      44857 : }
     175                 :            : 
     176                 :      44356 : std::optional<CNetAddr> LookupHost(const std::string& name, bool fAllowLookup, DNSLookupFn dns_lookup_function)
     177                 :            : {
     178         [ +  - ]:      44356 :     const std::vector<CNetAddr> addresses{LookupHost(name, 1, fAllowLookup, dns_lookup_function)};
     179   [ +  +  -  + ]:      44356 :     return addresses.empty() ? std::nullopt : std::make_optional(addresses.front());
     180                 :      44356 : }
     181                 :            : 
     182                 :     791871 : std::vector<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
     183                 :            : {
     184   [ +  +  +  + ]:     791871 :     if (name.empty() || !ContainsNoNUL(name)) {
     185                 :      41297 :         return {};
     186                 :            :     }
     187                 :     750574 :     uint16_t port{portDefault};
     188                 :     750574 :     std::string hostname;
     189         [ +  - ]:     750574 :     SplitHostPort(name, port, hostname);
     190                 :            : 
     191   [ +  -  +  - ]:     750574 :     const std::vector<CNetAddr> addresses{LookupIntern(hostname, nMaxSolutions, fAllowLookup, dns_lookup_function)};
     192         [ +  + ]:     750574 :     if (addresses.empty()) return {};
     193                 :     106443 :     std::vector<CService> services;
     194         [ -  + ]:     106443 :     services.reserve(addresses.size());
     195         [ +  + ]:     288355 :     for (const auto& addr : addresses)
     196         [ +  - ]:     181912 :         services.emplace_back(addr, port);
     197                 :     106443 :     return services;
     198         [ +  - ]:     898314 : }
     199                 :            : 
     200                 :     791370 : std::optional<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function)
     201                 :            : {
     202         [ +  - ]:     791370 :     const std::vector<CService> services{Lookup(name, portDefault, fAllowLookup, 1, dns_lookup_function)};
     203                 :            : 
     204   [ +  +  -  + ]:     791370 :     return services.empty() ? std::nullopt : std::make_optional(services.front());
     205                 :     791370 : }
     206                 :            : 
     207                 :     937017 : CService LookupNumeric(const std::string& name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
     208                 :            : {
     209         [ +  + ]:     937017 :     if (!ContainsNoNUL(name)) {
     210                 :     146471 :         return {};
     211                 :            :     }
     212                 :            :     // "1.2:345" will fail to resolve the ip, but will still set the port.
     213                 :            :     // If the ip fails to resolve, re-init the result.
     214   [ +  -  +  -  :     790546 :     return Lookup(name, portDefault, /*fAllowLookup=*/false, dns_lookup_function).value_or(CService{});
                   -  + ]
     215                 :     937017 : }
     216                 :            : 
     217                 :          0 : bool IsUnixSocketPath(const std::string& name)
     218                 :            : {
     219                 :            : #if HAVE_SOCKADDR_UN
     220         [ #  # ]:          0 :     if (name.find(ADDR_PREFIX_UNIX) != 0) return false;
     221                 :            : 
     222                 :            :     // Split off "unix:" prefix
     223                 :          0 :     std::string str{name.substr(ADDR_PREFIX_UNIX.length())};
     224                 :            : 
     225                 :            :     // Path size limit is platform-dependent
     226                 :            :     // see https://manpages.ubuntu.com/manpages/xenial/en/man7/unix.7.html
     227         [ #  # ]:          0 :     if (str.size() + 1 > sizeof(((sockaddr_un*)nullptr)->sun_path)) return false;
     228                 :            : 
     229                 :          0 :     return true;
     230                 :            : #else
     231                 :            :     return false;
     232                 :            : #endif
     233                 :          0 : }
     234                 :            : 
     235                 :            : /** SOCKS version */
     236                 :            : enum SOCKSVersion: uint8_t {
     237                 :            :     SOCKS4 = 0x04,
     238                 :            :     SOCKS5 = 0x05
     239                 :            : };
     240                 :            : 
     241                 :            : /** Values defined for METHOD in RFC1928 */
     242                 :            : enum SOCKS5Method: uint8_t {
     243                 :            :     NOAUTH = 0x00,        //!< No authentication required
     244                 :            :     GSSAPI = 0x01,        //!< GSSAPI
     245                 :            :     USER_PASS = 0x02,     //!< Username/password
     246                 :            :     NO_ACCEPTABLE = 0xff, //!< No acceptable methods
     247                 :            : };
     248                 :            : 
     249                 :            : /** Values defined for CMD in RFC1928 */
     250                 :            : enum SOCKS5Command: uint8_t {
     251                 :            :     CONNECT = 0x01,
     252                 :            :     BIND = 0x02,
     253                 :            :     UDP_ASSOCIATE = 0x03
     254                 :            : };
     255                 :            : 
     256                 :            : /** Values defined for REP in RFC1928 */
     257                 :            : enum SOCKS5Reply: uint8_t {
     258                 :            :     SUCCEEDED = 0x00,        //!< Succeeded
     259                 :            :     GENFAILURE = 0x01,       //!< General failure
     260                 :            :     NOTALLOWED = 0x02,       //!< Connection not allowed by ruleset
     261                 :            :     NETUNREACHABLE = 0x03,   //!< Network unreachable
     262                 :            :     HOSTUNREACHABLE = 0x04,  //!< Network unreachable
     263                 :            :     CONNREFUSED = 0x05,      //!< Connection refused
     264                 :            :     TTLEXPIRED = 0x06,       //!< TTL expired
     265                 :            :     CMDUNSUPPORTED = 0x07,   //!< Command not supported
     266                 :            :     ATYPEUNSUPPORTED = 0x08, //!< Address type not supported
     267                 :            : };
     268                 :            : 
     269                 :            : /** Values defined for ATYPE in RFC1928 */
     270                 :            : enum SOCKS5Atyp: uint8_t {
     271                 :            :     IPV4 = 0x01,
     272                 :            :     DOMAINNAME = 0x03,
     273                 :            :     IPV6 = 0x04,
     274                 :            : };
     275                 :            : 
     276                 :            : /** Status codes that can be returned by InterruptibleRecv */
     277                 :            : enum class IntrRecvError {
     278                 :            :     OK,
     279                 :            :     Timeout,
     280                 :            :     Disconnected,
     281                 :            :     NetworkError,
     282                 :            :     Interrupted
     283                 :            : };
     284                 :            : 
     285                 :            : /**
     286                 :            :  * Try to read a specified number of bytes from a socket. Please read the "see
     287                 :            :  * also" section for more detail.
     288                 :            :  *
     289                 :            :  * @param data The buffer where the read bytes should be stored.
     290                 :            :  * @param len The number of bytes to read into the specified buffer.
     291                 :            :  * @param timeout The total timeout for this read.
     292                 :            :  * @param sock The socket (has to be in non-blocking mode) from which to read bytes.
     293                 :            :  *
     294                 :            :  * @returns An IntrRecvError indicating the resulting status of this read.
     295                 :            :  *          IntrRecvError::OK only if all of the specified number of bytes were
     296                 :            :  *          read.
     297                 :            :  *
     298                 :            :  * @see This function can be interrupted by calling g_socks5_interrupt().
     299                 :            :  *      Sockets can be made non-blocking with Sock::SetNonBlocking().
     300                 :            :  */
     301                 :         64 : static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, std::chrono::milliseconds timeout, const Sock& sock)
     302                 :            : {
     303                 :         64 :     auto curTime{Now<SteadyMilliseconds>()};
     304                 :         64 :     const auto endTime{curTime + timeout};
     305   [ -  +  +  - ]:         64 :     while (len > 0 && curTime < endTime) {
     306                 :         64 :         ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
     307         [ +  + ]:         64 :         if (ret > 0) {
     308                 :         34 :             len -= ret;
     309                 :         34 :             data += ret;
     310         [ +  + ]:         64 :         } else if (ret == 0) { // Unexpected disconnection
     311                 :          7 :             return IntrRecvError::Disconnected;
     312                 :            :         } else { // Other error or blocking
     313                 :         23 :             int nErr = WSAGetLastError();
     314   [ +  -  +  +  :         23 :             if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
                   +  + ]
     315                 :            :                 // Only wait at most MAX_WAIT_FOR_IO at a time, unless
     316                 :            :                 // we're approaching the end of the specified total timeout
     317                 :         13 :                 const auto remaining = std::chrono::milliseconds{endTime - curTime};
     318                 :         13 :                 const auto timeout = std::min(remaining, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
     319         [ +  + ]:         13 :                 if (!sock.Wait(timeout, Sock::RECV)) {
     320                 :          6 :                     return IntrRecvError::NetworkError;
     321                 :            :                 }
     322         [ +  + ]:         13 :             } else {
     323                 :         10 :                 return IntrRecvError::NetworkError;
     324                 :            :             }
     325         [ +  + ]:         23 :         }
     326         [ +  - ]:         41 :         if (g_socks5_interrupt) {
     327                 :         41 :             return IntrRecvError::Interrupted;
     328                 :            :         }
     329                 :          0 :         curTime = Now<SteadyMilliseconds>();
     330         [ +  - ]:         64 :     }
     331                 :          0 :     return len == 0 ? IntrRecvError::OK : IntrRecvError::Timeout;
     332                 :         64 : }
     333                 :            : 
     334                 :            : /** Convert SOCKS5 reply to an error message */
     335                 :          0 : static std::string Socks5ErrorString(uint8_t err)
     336                 :            : {
     337   [ #  #  #  #  :          0 :     switch(err) {
             #  #  #  #  
                      # ]
     338                 :            :         case SOCKS5Reply::GENFAILURE:
     339         [ #  # ]:          0 :             return "general failure";
     340                 :            :         case SOCKS5Reply::NOTALLOWED:
     341         [ #  # ]:          0 :             return "connection not allowed";
     342                 :            :         case SOCKS5Reply::NETUNREACHABLE:
     343         [ #  # ]:          0 :             return "network unreachable";
     344                 :            :         case SOCKS5Reply::HOSTUNREACHABLE:
     345         [ #  # ]:          0 :             return "host unreachable";
     346                 :            :         case SOCKS5Reply::CONNREFUSED:
     347         [ #  # ]:          0 :             return "connection refused";
     348                 :            :         case SOCKS5Reply::TTLEXPIRED:
     349         [ #  # ]:          0 :             return "TTL expired";
     350                 :            :         case SOCKS5Reply::CMDUNSUPPORTED:
     351         [ #  # ]:          0 :             return "protocol error";
     352                 :            :         case SOCKS5Reply::ATYPEUNSUPPORTED:
     353         [ #  # ]:          0 :             return "address type not supported";
     354                 :            :         default:
     355         [ #  # ]:          0 :             return "unknown";
     356                 :            :     }
     357                 :          0 : }
     358                 :            : 
     359                 :        107 : bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& sock)
     360                 :            : {
     361                 :            :     try {
     362                 :        107 :         IntrRecvError recvr;
     363   [ +  -  +  -  :        107 :         LogPrint(BCLog::NET, "SOCKS5 connecting %s\n", strDest);
          #  #  #  #  #  
                      # ]
     364         [ +  + ]:        107 :         if (strDest.size() > 255) {
     365   [ +  -  +  -  :          4 :             LogError("Hostname too long\n");
                   +  - ]
     366                 :          4 :             return false;
     367                 :            :         }
     368                 :            :         // Construct the version identifier/method selection message
     369                 :        103 :         std::vector<uint8_t> vSocks5Init;
     370         [ +  - ]:        103 :         vSocks5Init.push_back(SOCKSVersion::SOCKS5); // We want the SOCK5 protocol
     371         [ +  + ]:        103 :         if (auth) {
     372         [ +  - ]:         55 :             vSocks5Init.push_back(0x02); // 2 method identifiers follow...
     373         [ +  - ]:         55 :             vSocks5Init.push_back(SOCKS5Method::NOAUTH);
     374         [ +  - ]:         55 :             vSocks5Init.push_back(SOCKS5Method::USER_PASS);
     375                 :         55 :         } else {
     376         [ +  - ]:         48 :             vSocks5Init.push_back(0x01); // 1 method identifier follows...
     377         [ +  - ]:         48 :             vSocks5Init.push_back(SOCKS5Method::NOAUTH);
     378                 :            :         }
     379   [ +  -  +  + ]:        103 :         sock.SendComplete(vSocks5Init, g_socks5_recv_timeout, g_socks5_interrupt);
     380                 :         64 :         uint8_t pchRet1[2];
     381   [ +  -  +  - ]:         64 :         if (InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
     382   [ +  -  +  -  :         64 :             LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port);
                   +  - ]
     383                 :         64 :             return false;
     384                 :            :         }
     385         [ #  # ]:          0 :         if (pchRet1[0] != SOCKSVersion::SOCKS5) {
     386   [ #  #  #  #  :          0 :             LogError("Proxy failed to initialize\n");
                   #  # ]
     387                 :          0 :             return false;
     388                 :            :         }
     389   [ #  #  #  # ]:          0 :         if (pchRet1[1] == SOCKS5Method::USER_PASS && auth) {
     390                 :            :             // Perform username/password authentication (as described in RFC1929)
     391                 :          0 :             std::vector<uint8_t> vAuth;
     392         [ #  # ]:          0 :             vAuth.push_back(0x01); // Current (and only) version of user/pass subnegotiation
     393   [ #  #  #  # ]:          0 :             if (auth->username.size() > 255 || auth->password.size() > 255) {
     394   [ #  #  #  #  :          0 :                 LogError("Proxy username or password too long\n");
                   #  # ]
     395                 :          0 :                 return false;
     396                 :            :             }
     397         [ #  # ]:          0 :             vAuth.push_back(auth->username.size());
     398         [ #  # ]:          0 :             vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end());
     399         [ #  # ]:          0 :             vAuth.push_back(auth->password.size());
     400         [ #  # ]:          0 :             vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end());
     401   [ #  #  #  # ]:          0 :             sock.SendComplete(vAuth, g_socks5_recv_timeout, g_socks5_interrupt);
     402   [ #  #  #  #  :          0 :             LogPrint(BCLog::PROXY, "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
          #  #  #  #  #  
                      # ]
     403                 :          0 :             uint8_t pchRetA[2];
     404   [ #  #  #  # ]:          0 :             if (InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
     405   [ #  #  #  #  :          0 :                 LogError("Error reading proxy authentication response\n");
                   #  # ]
     406                 :          0 :                 return false;
     407                 :            :             }
     408   [ #  #  #  # ]:          0 :             if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
     409   [ #  #  #  #  :          0 :                 LogError("Proxy authentication unsuccessful\n");
                   #  # ]
     410                 :          0 :                 return false;
     411                 :            :             }
     412   [ #  #  #  # ]:          0 :         } else if (pchRet1[1] == SOCKS5Method::NOAUTH) {
     413                 :            :             // Perform no authentication
     414                 :          0 :         } else {
     415   [ #  #  #  #  :          0 :             LogError("Proxy requested wrong authentication method %02x\n", pchRet1[1]);
                   #  # ]
     416                 :          0 :             return false;
     417                 :            :         }
     418                 :          0 :         std::vector<uint8_t> vSocks5;
     419         [ #  # ]:          0 :         vSocks5.push_back(SOCKSVersion::SOCKS5);   // VER protocol version
     420         [ #  # ]:          0 :         vSocks5.push_back(SOCKS5Command::CONNECT); // CMD CONNECT
     421         [ #  # ]:          0 :         vSocks5.push_back(0x00);                   // RSV Reserved must be 0
     422         [ #  # ]:          0 :         vSocks5.push_back(SOCKS5Atyp::DOMAINNAME); // ATYP DOMAINNAME
     423         [ #  # ]:          0 :         vSocks5.push_back(strDest.size());         // Length<=255 is checked at beginning of function
     424         [ #  # ]:          0 :         vSocks5.insert(vSocks5.end(), strDest.begin(), strDest.end());
     425         [ #  # ]:          0 :         vSocks5.push_back((port >> 8) & 0xFF);
     426         [ #  # ]:          0 :         vSocks5.push_back((port >> 0) & 0xFF);
     427   [ #  #  #  # ]:          0 :         sock.SendComplete(vSocks5, g_socks5_recv_timeout, g_socks5_interrupt);
     428                 :          0 :         uint8_t pchRet2[4];
     429   [ #  #  #  # ]:          0 :         if ((recvr = InterruptibleRecv(pchRet2, 4, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
     430         [ #  # ]:          0 :             if (recvr == IntrRecvError::Timeout) {
     431                 :            :                 /* If a timeout happens here, this effectively means we timed out while connecting
     432                 :            :                  * to the remote node. This is very common for Tor, so do not print an
     433                 :            :                  * error message. */
     434                 :          0 :                 return false;
     435                 :            :             } else {
     436   [ #  #  #  #  :          0 :                 LogError("Error while reading proxy response\n");
                   #  # ]
     437                 :          0 :                 return false;
     438                 :            :             }
     439                 :            :         }
     440         [ #  # ]:          0 :         if (pchRet2[0] != SOCKSVersion::SOCKS5) {
     441   [ #  #  #  #  :          0 :             LogError("Proxy failed to accept request\n");
                   #  # ]
     442                 :          0 :             return false;
     443                 :            :         }
     444         [ #  # ]:          0 :         if (pchRet2[1] != SOCKS5Reply::SUCCEEDED) {
     445                 :            :             // Failures to connect to a peer that are not proxy errors
     446   [ #  #  #  #  :          0 :             LogPrintf("Socks5() connect to %s:%d failed: %s\n", strDest, port, Socks5ErrorString(pchRet2[1]));
             #  #  #  # ]
     447                 :          0 :             return false;
     448                 :            :         }
     449         [ #  # ]:          0 :         if (pchRet2[2] != 0x00) { // Reserved field must be 0
     450   [ #  #  #  #  :          0 :             LogError("Error: malformed proxy response\n");
                   #  # ]
     451                 :          0 :             return false;
     452                 :            :         }
     453                 :          0 :         uint8_t pchRet3[256];
     454   [ #  #  #  # ]:          0 :         switch (pchRet2[3]) {
     455         [ #  # ]:          0 :         case SOCKS5Atyp::IPV4: recvr = InterruptibleRecv(pchRet3, 4, g_socks5_recv_timeout, sock); break;
     456         [ #  # ]:          0 :         case SOCKS5Atyp::IPV6: recvr = InterruptibleRecv(pchRet3, 16, g_socks5_recv_timeout, sock); break;
     457                 :            :         case SOCKS5Atyp::DOMAINNAME: {
     458         [ #  # ]:          0 :             recvr = InterruptibleRecv(pchRet3, 1, g_socks5_recv_timeout, sock);
     459         [ #  # ]:          0 :             if (recvr != IntrRecvError::OK) {
     460   [ #  #  #  #  :          0 :                 LogError("Error reading from proxy\n");
                   #  # ]
     461                 :          0 :                 return false;
     462                 :            :             }
     463                 :          0 :             int nRecv = pchRet3[0];
     464         [ #  # ]:          0 :             recvr = InterruptibleRecv(pchRet3, nRecv, g_socks5_recv_timeout, sock);
     465                 :            :             break;
     466                 :          0 :         }
     467                 :            :         default: {
     468   [ #  #  #  #  :          0 :             LogError("Error: malformed proxy response\n");
                   #  # ]
     469                 :          0 :             return false;
     470                 :            :         }
     471                 :            :         }
     472         [ #  # ]:          0 :         if (recvr != IntrRecvError::OK) {
     473   [ #  #  #  #  :          0 :             LogError("Error reading from proxy\n");
                   #  # ]
     474                 :          0 :             return false;
     475                 :            :         }
     476   [ #  #  #  # ]:          0 :         if (InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
     477   [ #  #  #  #  :          0 :             LogError("Error reading from proxy\n");
                   #  # ]
     478                 :          0 :             return false;
     479                 :            :         }
     480   [ #  #  #  #  :          0 :         LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest);
          #  #  #  #  #  
                      # ]
     481                 :          0 :         return true;
     482         [ -  + ]:        107 :     } catch (const std::runtime_error& e) {
     483   [ +  -  +  -  :         39 :         LogError("Error during SOCKS5 proxy handshake: %s\n", e.what());
                   +  - ]
     484                 :         39 :         return false;
     485         [ #  # ]:         39 :     }
     486                 :        146 : }
     487                 :            : 
     488                 :          0 : std::unique_ptr<Sock> CreateSockOS(sa_family_t address_family)
     489                 :            : {
     490                 :            :     // Not IPv4, IPv6 or UNIX
     491         [ #  # ]:          0 :     if (address_family == AF_UNSPEC) return nullptr;
     492                 :            : 
     493                 :          0 :     int protocol{IPPROTO_TCP};
     494                 :            : #if HAVE_SOCKADDR_UN
     495         [ #  # ]:          0 :     if (address_family == AF_UNIX) protocol = 0;
     496                 :            : #endif
     497                 :            : 
     498                 :            :     // Create a socket in the specified address family.
     499                 :          0 :     SOCKET hSocket = socket(address_family, SOCK_STREAM, protocol);
     500         [ #  # ]:          0 :     if (hSocket == INVALID_SOCKET) {
     501                 :          0 :         return nullptr;
     502                 :            :     }
     503                 :            : 
     504                 :          0 :     auto sock = std::make_unique<Sock>(hSocket);
     505                 :            : 
     506                 :            :     // Ensure that waiting for I/O on this socket won't result in undefined
     507                 :            :     // behavior.
     508   [ #  #  #  # ]:          0 :     if (!sock->IsSelectable()) {
     509   [ #  #  #  #  :          0 :         LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
                   #  # ]
     510                 :          0 :         return nullptr;
     511                 :            :     }
     512                 :            : 
     513                 :            : #ifdef SO_NOSIGPIPE
     514                 :            :     int set = 1;
     515                 :            :     // Set the no-sigpipe option on the socket for BSD systems, other UNIXes
     516                 :            :     // should use the MSG_NOSIGNAL flag for every send.
     517                 :            :     if (sock->SetSockOpt(SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int)) == SOCKET_ERROR) {
     518                 :            :         LogPrintf("Error setting SO_NOSIGPIPE on socket: %s, continuing anyway\n",
     519                 :            :                   NetworkErrorString(WSAGetLastError()));
     520                 :            :     }
     521                 :            : #endif
     522                 :            : 
     523                 :            :     // Set the non-blocking option on the socket.
     524   [ #  #  #  # ]:          0 :     if (!sock->SetNonBlocking()) {
     525   [ #  #  #  #  :          0 :         LogPrintf("Error setting socket to non-blocking: %s\n", NetworkErrorString(WSAGetLastError()));
             #  #  #  # ]
     526                 :          0 :         return nullptr;
     527                 :            :     }
     528                 :            : 
     529                 :            : #if HAVE_SOCKADDR_UN
     530         [ #  # ]:          0 :     if (address_family == AF_UNIX) return sock;
     531                 :            : #endif
     532                 :            : 
     533                 :            :     // Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
     534                 :          0 :     const int on{1};
     535   [ #  #  #  # ]:          0 :     if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
     536   [ #  #  #  #  :          0 :         LogPrint(BCLog::NET, "Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n");
          #  #  #  #  #  
                      # ]
     537                 :          0 :     }
     538                 :          0 :     return sock;
     539                 :          0 : }
     540                 :            : 
     541                 :          1 : std::function<std::unique_ptr<Sock>(const sa_family_t&)> CreateSock = CreateSockOS;
     542                 :            : 
     543                 :            : template<typename... Args>
     544                 :          0 : static void LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args) {
     545                 :          0 :     std::string error_message = tfm::format(fmt, args...);
     546         [ #  # ]:          0 :     if (manual_connection) {
     547   [ #  #  #  #  :          0 :         LogPrintf("%s\n", error_message);
                   #  # ]
     548                 :          0 :     } else {
     549   [ #  #  #  #  :          0 :         LogPrint(BCLog::NET, "%s\n", error_message);
          #  #  #  #  #  
                      # ]
     550                 :            :     }
     551                 :          0 : }
     552                 :            : 
     553                 :          0 : static bool ConnectToSocket(const Sock& sock, struct sockaddr* sockaddr, socklen_t len, const std::string& dest_str, bool manual_connection)
     554                 :            : {
     555                 :            :     // Connect to `sockaddr` using `sock`.
     556         [ #  # ]:          0 :     if (sock.Connect(sockaddr, len) == SOCKET_ERROR) {
     557                 :          0 :         int nErr = WSAGetLastError();
     558                 :            :         // WSAEINVAL is here because some legacy version of winsock uses it
     559   [ #  #  #  #  :          0 :         if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
                   #  # ]
     560                 :            :         {
     561                 :            :             // Connection didn't actually fail, but is being established
     562                 :            :             // asynchronously. Thus, use async I/O api (select/poll)
     563                 :            :             // synchronously to check for successful connection with a timeout.
     564                 :          0 :             const Sock::Event requested = Sock::RECV | Sock::SEND;
     565                 :          0 :             Sock::Event occurred;
     566         [ #  # ]:          0 :             if (!sock.Wait(std::chrono::milliseconds{nConnectTimeout}, requested, &occurred)) {
     567   [ #  #  #  #  :          0 :                 LogPrintf("wait for connect to %s failed: %s\n",
             #  #  #  # ]
     568                 :            :                           dest_str,
     569                 :            :                           NetworkErrorString(WSAGetLastError()));
     570                 :          0 :                 return false;
     571         [ #  # ]:          0 :             } else if (occurred == 0) {
     572   [ #  #  #  #  :          0 :                 LogPrint(BCLog::NET, "connection attempt to %s timed out\n", dest_str);
             #  #  #  # ]
     573                 :          0 :                 return false;
     574                 :            :             }
     575                 :            : 
     576                 :            :             // Even if the wait was successful, the connect might not
     577                 :            :             // have been successful. The reason for this failure is hidden away
     578                 :            :             // in the SO_ERROR for the socket in modern systems. We read it into
     579                 :            :             // sockerr here.
     580                 :          0 :             int sockerr;
     581                 :          0 :             socklen_t sockerr_len = sizeof(sockerr);
     582         [ #  # ]:          0 :             if (sock.GetSockOpt(SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&sockerr, &sockerr_len) ==
     583                 :            :                 SOCKET_ERROR) {
     584   [ #  #  #  #  :          0 :                 LogPrintf("getsockopt() for %s failed: %s\n", dest_str, NetworkErrorString(WSAGetLastError()));
             #  #  #  # ]
     585                 :          0 :                 return false;
     586                 :            :             }
     587         [ #  # ]:          0 :             if (sockerr != 0) {
     588   [ #  #  #  # ]:          0 :                 LogConnectFailure(manual_connection,
     589                 :            :                                   "connect() to %s failed after wait: %s",
     590                 :          0 :                                   dest_str,
     591                 :          0 :                                   NetworkErrorString(sockerr));
     592                 :          0 :                 return false;
     593                 :            :             }
     594         [ #  # ]:          0 :         }
     595                 :            : #ifdef WIN32
     596                 :            :         else if (WSAGetLastError() != WSAEISCONN)
     597                 :            : #else
     598                 :            :         else
     599                 :            : #endif
     600                 :            :         {
     601         [ #  # ]:          0 :             LogConnectFailure(manual_connection, "connect() to %s failed: %s", dest_str, NetworkErrorString(WSAGetLastError()));
     602                 :          0 :             return false;
     603                 :            :         }
     604      [ #  #  # ]:          0 :     }
     605                 :          0 :     return true;
     606                 :          0 : }
     607                 :            : 
     608                 :          0 : std::unique_ptr<Sock> ConnectDirectly(const CService& dest, bool manual_connection)
     609                 :            : {
     610                 :          0 :     auto sock = CreateSock(dest.GetSAFamily());
     611         [ #  # ]:          0 :     if (!sock) {
     612   [ #  #  #  #  :          0 :         LogPrintLevel(BCLog::NET, BCLog::Level::Error, "Cannot create a socket for connecting to %s\n", dest.ToStringAddrPort());
          #  #  #  #  #  
                #  #  # ]
     613                 :          0 :         return {};
     614                 :            :     }
     615                 :            : 
     616                 :            :     // Create a sockaddr from the specified service.
     617                 :          0 :     struct sockaddr_storage sockaddr;
     618                 :          0 :     socklen_t len = sizeof(sockaddr);
     619   [ #  #  #  # ]:          0 :     if (!dest.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
     620   [ #  #  #  #  :          0 :         LogPrintf("Cannot get sockaddr for %s: unsupported network\n", dest.ToStringAddrPort());
             #  #  #  # ]
     621                 :          0 :         return {};
     622                 :            :     }
     623                 :            : 
     624   [ #  #  #  #  :          0 :     if (!ConnectToSocket(*sock, (struct sockaddr*)&sockaddr, len, dest.ToStringAddrPort(), manual_connection)) {
                   #  # ]
     625                 :          0 :         return {};
     626                 :            :     }
     627                 :            : 
     628                 :          0 :     return sock;
     629                 :          0 : }
     630                 :            : 
     631                 :          0 : std::unique_ptr<Sock> Proxy::Connect() const
     632                 :            : {
     633         [ #  # ]:          0 :     if (!IsValid()) return {};
     634                 :            : 
     635         [ #  # ]:          0 :     if (!m_is_unix_socket) return ConnectDirectly(proxy, /*manual_connection=*/true);
     636                 :            : 
     637                 :            : #if HAVE_SOCKADDR_UN
     638                 :          0 :     auto sock = CreateSock(AF_UNIX);
     639         [ #  # ]:          0 :     if (!sock) {
     640   [ #  #  #  #  :          0 :         LogPrintLevel(BCLog::NET, BCLog::Level::Error, "Cannot create a socket for connecting to %s\n", m_unix_socket_path);
          #  #  #  #  #  
                      # ]
     641                 :          0 :         return {};
     642                 :            :     }
     643                 :            : 
     644         [ #  # ]:          0 :     const std::string path{m_unix_socket_path.substr(ADDR_PREFIX_UNIX.length())};
     645                 :            : 
     646                 :          0 :     struct sockaddr_un addrun;
     647                 :          0 :     memset(&addrun, 0, sizeof(addrun));
     648                 :          0 :     addrun.sun_family = AF_UNIX;
     649                 :            :     // leave the last char in addrun.sun_path[] to be always '\0'
     650         [ #  # ]:          0 :     memcpy(addrun.sun_path, path.c_str(), std::min(sizeof(addrun.sun_path) - 1, path.length()));
     651                 :          0 :     socklen_t len = sizeof(addrun);
     652                 :            : 
     653   [ #  #  #  # ]:          0 :     if(!ConnectToSocket(*sock, (struct sockaddr*)&addrun, len, path, /*manual_connection=*/true)) {
     654                 :          0 :         return {};
     655                 :            :     }
     656                 :            : 
     657                 :          0 :     return sock;
     658                 :            : #else
     659                 :            :     return {};
     660                 :            : #endif
     661                 :          0 : }
     662                 :            : 
     663                 :          0 : bool SetProxy(enum Network net, const Proxy &addrProxy) {
     664   [ #  #  #  # ]:          0 :     assert(net >= 0 && net < NET_MAX);
     665         [ #  # ]:          0 :     if (!addrProxy.IsValid())
     666                 :          0 :         return false;
     667                 :          0 :     LOCK(g_proxyinfo_mutex);
     668         [ #  # ]:          0 :     proxyInfo[net] = addrProxy;
     669                 :          0 :     return true;
     670                 :          0 : }
     671                 :            : 
     672                 :         15 : bool GetProxy(enum Network net, Proxy &proxyInfoOut) {
     673   [ -  +  +  - ]:         15 :     assert(net >= 0 && net < NET_MAX);
     674                 :         15 :     LOCK(g_proxyinfo_mutex);
     675   [ +  -  -  + ]:         15 :     if (!proxyInfo[net].IsValid())
     676                 :         15 :         return false;
     677         [ #  # ]:          0 :     proxyInfoOut = proxyInfo[net];
     678                 :          0 :     return true;
     679                 :         15 : }
     680                 :            : 
     681                 :          0 : bool SetNameProxy(const Proxy &addrProxy) {
     682         [ #  # ]:          0 :     if (!addrProxy.IsValid())
     683                 :          0 :         return false;
     684                 :          0 :     LOCK(g_proxyinfo_mutex);
     685         [ #  # ]:          0 :     nameProxy = addrProxy;
     686                 :          0 :     return true;
     687                 :          0 : }
     688                 :            : 
     689                 :          0 : bool GetNameProxy(Proxy &nameProxyOut) {
     690                 :          0 :     LOCK(g_proxyinfo_mutex);
     691   [ #  #  #  # ]:          0 :     if(!nameProxy.IsValid())
     692                 :          0 :         return false;
     693         [ #  # ]:          0 :     nameProxyOut = nameProxy;
     694                 :          0 :     return true;
     695                 :          0 : }
     696                 :            : 
     697                 :          0 : bool HaveNameProxy() {
     698                 :          0 :     LOCK(g_proxyinfo_mutex);
     699         [ #  # ]:          0 :     return nameProxy.IsValid();
     700                 :          0 : }
     701                 :            : 
     702                 :      10115 : bool IsProxy(const CNetAddr &addr) {
     703                 :      10115 :     LOCK(g_proxyinfo_mutex);
     704   [ +  +  -  + ]:      80920 :     for (int i = 0; i < NET_MAX; i++) {
     705   [ +  -  +  -  :      70805 :         if (addr == static_cast<CNetAddr>(proxyInfo[i].proxy))
                   +  - ]
     706                 :          0 :             return true;
     707                 :      70805 :     }
     708                 :      10115 :     return false;
     709                 :      10115 : }
     710                 :            : 
     711                 :          0 : std::unique_ptr<Sock> ConnectThroughProxy(const Proxy& proxy,
     712                 :            :                                           const std::string& dest,
     713                 :            :                                           uint16_t port,
     714                 :            :                                           bool& proxy_connection_failed)
     715                 :            : {
     716                 :            :     // first connect to proxy server
     717                 :          0 :     auto sock = proxy.Connect();
     718         [ #  # ]:          0 :     if (!sock) {
     719                 :          0 :         proxy_connection_failed = true;
     720                 :          0 :         return {};
     721                 :            :     }
     722                 :            : 
     723                 :            :     // do socks negotiation
     724         [ #  # ]:          0 :     if (proxy.m_randomize_credentials) {
     725                 :          0 :         ProxyCredentials random_auth;
     726                 :            :         static std::atomic_int counter(0);
     727   [ #  #  #  # ]:          0 :         random_auth.username = random_auth.password = strprintf("%i", counter++);
     728   [ #  #  #  # ]:          0 :         if (!Socks5(dest, port, &random_auth, *sock)) {
     729                 :          0 :             return {};
     730                 :            :         }
     731         [ #  # ]:          0 :     } else {
     732   [ #  #  #  # ]:          0 :         if (!Socks5(dest, port, nullptr, *sock)) {
     733                 :          0 :             return {};
     734                 :            :         }
     735                 :            :     }
     736                 :          0 :     return sock;
     737                 :          0 : }
     738                 :            : 
     739                 :      35396 : CSubNet LookupSubNet(const std::string& subnet_str)
     740                 :            : {
     741                 :      35396 :     CSubNet subnet;
     742   [ +  -  +  - ]:      35396 :     assert(!subnet.IsValid());
     743         [ +  + ]:      35396 :     if (!ContainsNoNUL(subnet_str)) {
     744                 :         17 :         return subnet;
     745                 :            :     }
     746                 :            : 
     747                 :      35379 :     const size_t slash_pos{subnet_str.find_last_of('/')};
     748         [ +  - ]:      35379 :     const std::string str_addr{subnet_str.substr(0, slash_pos)};
     749   [ +  -  +  - ]:      35379 :     std::optional<CNetAddr> addr{LookupHost(str_addr, /*fAllowLookup=*/false)};
     750                 :            : 
     751         [ +  + ]:      35379 :     if (addr.has_value()) {
     752   [ +  -  +  -  :      34009 :         addr = static_cast<CNetAddr>(MaybeFlipIPv6toCJDNS(CService{addr.value(), /*port=*/0}));
                   +  - ]
     753         [ +  + ]:      34009 :         if (slash_pos != subnet_str.npos) {
     754         [ +  - ]:      31187 :             const std::string netmask_str{subnet_str.substr(slash_pos + 1)};
     755                 :      31187 :             uint8_t netmask;
     756   [ +  -  +  + ]:      31187 :             if (ParseUInt8(netmask_str, &netmask)) {
     757                 :            :                 // Valid number; assume CIDR variable-length subnet masking.
     758   [ +  -  +  - ]:      30624 :                 subnet = CSubNet{addr.value(), netmask};
     759                 :      30624 :             } else {
     760                 :            :                 // Invalid number; try full netmask syntax. Never allow lookup for netmask.
     761   [ +  -  -  + ]:        563 :                 const std::optional<CNetAddr> full_netmask{LookupHost(netmask_str, /*fAllowLookup=*/false)};
     762         [ +  + ]:        563 :                 if (full_netmask.has_value()) {
     763   [ +  -  +  -  :        166 :                     subnet = CSubNet{addr.value(), full_netmask.value()};
                   +  - ]
     764                 :        166 :                 }
     765                 :        563 :             }
     766                 :      31187 :         } else {
     767                 :            :             // Single IP subnet (<ipv4>/32 or <ipv6>/128).
     768   [ +  -  +  - ]:       2822 :             subnet = CSubNet{addr.value()};
     769                 :            :         }
     770                 :      34009 :     }
     771                 :            : 
     772                 :      35379 :     return subnet;
     773         [ +  - ]:      35396 : }
     774                 :            : 
     775                 :          0 : bool IsBadPort(uint16_t port)
     776                 :            : {
     777                 :            :     /* Don't forget to update doc/p2p-bad-ports.md if you change this list. */
     778                 :            : 
     779         [ #  # ]:          0 :     switch (port) {
     780                 :            :     case 1:     // tcpmux
     781                 :            :     case 7:     // echo
     782                 :            :     case 9:     // discard
     783                 :            :     case 11:    // systat
     784                 :            :     case 13:    // daytime
     785                 :            :     case 15:    // netstat
     786                 :            :     case 17:    // qotd
     787                 :            :     case 19:    // chargen
     788                 :            :     case 20:    // ftp data
     789                 :            :     case 21:    // ftp access
     790                 :            :     case 22:    // ssh
     791                 :            :     case 23:    // telnet
     792                 :            :     case 25:    // smtp
     793                 :            :     case 37:    // time
     794                 :            :     case 42:    // name
     795                 :            :     case 43:    // nicname
     796                 :            :     case 53:    // domain
     797                 :            :     case 69:    // tftp
     798                 :            :     case 77:    // priv-rjs
     799                 :            :     case 79:    // finger
     800                 :            :     case 87:    // ttylink
     801                 :            :     case 95:    // supdup
     802                 :            :     case 101:   // hostname
     803                 :            :     case 102:   // iso-tsap
     804                 :            :     case 103:   // gppitnp
     805                 :            :     case 104:   // acr-nema
     806                 :            :     case 109:   // pop2
     807                 :            :     case 110:   // pop3
     808                 :            :     case 111:   // sunrpc
     809                 :            :     case 113:   // auth
     810                 :            :     case 115:   // sftp
     811                 :            :     case 117:   // uucp-path
     812                 :            :     case 119:   // nntp
     813                 :            :     case 123:   // NTP
     814                 :            :     case 135:   // loc-srv /epmap
     815                 :            :     case 137:   // netbios
     816                 :            :     case 139:   // netbios
     817                 :            :     case 143:   // imap2
     818                 :            :     case 161:   // snmp
     819                 :            :     case 179:   // BGP
     820                 :            :     case 389:   // ldap
     821                 :            :     case 427:   // SLP (Also used by Apple Filing Protocol)
     822                 :            :     case 465:   // smtp+ssl
     823                 :            :     case 512:   // print / exec
     824                 :            :     case 513:   // login
     825                 :            :     case 514:   // shell
     826                 :            :     case 515:   // printer
     827                 :            :     case 526:   // tempo
     828                 :            :     case 530:   // courier
     829                 :            :     case 531:   // chat
     830                 :            :     case 532:   // netnews
     831                 :            :     case 540:   // uucp
     832                 :            :     case 548:   // AFP (Apple Filing Protocol)
     833                 :            :     case 554:   // rtsp
     834                 :            :     case 556:   // remotefs
     835                 :            :     case 563:   // nntp+ssl
     836                 :            :     case 587:   // smtp (rfc6409)
     837                 :            :     case 601:   // syslog-conn (rfc3195)
     838                 :            :     case 636:   // ldap+ssl
     839                 :            :     case 989:   // ftps-data
     840                 :            :     case 990:   // ftps
     841                 :            :     case 993:   // ldap+ssl
     842                 :            :     case 995:   // pop3+ssl
     843                 :            :     case 1719:  // h323gatestat
     844                 :            :     case 1720:  // h323hostcall
     845                 :            :     case 1723:  // pptp
     846                 :            :     case 2049:  // nfs
     847                 :            :     case 3659:  // apple-sasl / PasswordServer
     848                 :            :     case 4045:  // lockd
     849                 :            :     case 5060:  // sip
     850                 :            :     case 5061:  // sips
     851                 :            :     case 6000:  // X11
     852                 :            :     case 6566:  // sane-port
     853                 :            :     case 6665:  // Alternate IRC
     854                 :            :     case 6666:  // Alternate IRC
     855                 :            :     case 6667:  // Standard IRC
     856                 :            :     case 6668:  // Alternate IRC
     857                 :            :     case 6669:  // Alternate IRC
     858                 :            :     case 6697:  // IRC + TLS
     859                 :            :     case 10080: // Amanda
     860                 :          0 :         return true;
     861                 :            :     }
     862                 :          0 :     return false;
     863                 :          0 : }
     864                 :            : 
     865                 :     164049 : CService MaybeFlipIPv6toCJDNS(const CService& service)
     866                 :            : {
     867                 :     164049 :     CService ret{service};
     868   [ +  -  +  +  :     164049 :     if (ret.IsIPv6() && ret.HasCJDNSPrefix() && g_reachable_nets.Contains(NET_CJDNS)) {
          +  -  +  +  +  
                -  -  + ]
     869                 :        126 :         ret.m_net = NET_CJDNS;
     870                 :        126 :     }
     871                 :     164049 :     return ret;
     872         [ +  - ]:     164049 : }

Generated by: LCOV version 1.16