LCOV - code coverage report
Current view: top level - src - netbase.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 40.1 % 392 157
Test Date: 2024-08-28 04:44:32 Functions: 65.5 % 29 19
Branches: 25.9 % 556 144

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

Generated by: LCOV version 2.0-1