LCOV - code coverage report
Current view: top level - src - netbase.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 2 7 28.6 %
Date: 2023-11-06 23:13:05 Functions: 3 9 33.3 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2022 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #ifndef BITCOIN_NETBASE_H
       6                 :            : #define BITCOIN_NETBASE_H
       7                 :            : 
       8                 :            : #if defined(HAVE_CONFIG_H)
       9                 :            : #include <config/bitcoin-config.h>
      10                 :            : #endif
      11                 :            : 
      12                 :            : #include <compat/compat.h>
      13                 :            : #include <netaddress.h>
      14                 :            : #include <serialize.h>
      15                 :            : #include <util/sock.h>
      16                 :            : 
      17                 :            : #include <functional>
      18                 :            : #include <memory>
      19                 :            : #include <stdint.h>
      20                 :            : #include <string>
      21                 :            : #include <type_traits>
      22                 :            : #include <vector>
      23                 :            : 
      24                 :            : extern int nConnectTimeout;
      25                 :            : extern bool fNameLookup;
      26                 :            : 
      27                 :            : //! -timeout default
      28                 :            : static const int DEFAULT_CONNECT_TIMEOUT = 5000;
      29                 :            : //! -dns default
      30                 :            : static const int DEFAULT_NAME_LOOKUP = true;
      31                 :            : 
      32                 :            : enum class ConnectionDirection {
      33                 :            :     None = 0,
      34                 :            :     In = (1U << 0),
      35                 :            :     Out = (1U << 1),
      36                 :            :     Both = (In | Out),
      37                 :            : };
      38                 :            : static inline ConnectionDirection& operator|=(ConnectionDirection& a, ConnectionDirection b) {
      39                 :            :     using underlying = typename std::underlying_type<ConnectionDirection>::type;
      40                 :            :     a = ConnectionDirection(underlying(a) | underlying(b));
      41                 :            :     return a;
      42                 :            : }
      43                 :          0 : static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
      44                 :            :     using underlying = typename std::underlying_type<ConnectionDirection>::type;
      45                 :          0 :     return (underlying(a) & underlying(b));
      46                 :            : }
      47                 :            : 
      48                 :          0 : class Proxy
      49                 :            : {
      50                 :            : public:
      51                 :        161 :     Proxy(): randomize_credentials(false) {}
      52                 :          0 :     explicit Proxy(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
      53                 :            : 
      54                 :        145 :     bool IsValid() const { return proxy.IsValid(); }
      55                 :            : 
      56                 :            :     CService proxy;
      57                 :            :     bool randomize_credentials;
      58                 :            : };
      59                 :            : 
      60                 :            : /** Credentials for proxy authentication */
      61                 :          0 : struct ProxyCredentials
      62                 :            : {
      63                 :            :     std::string username;
      64                 :            :     std::string password;
      65                 :            : };
      66                 :            : 
      67                 :            : /**
      68                 :            :  * Wrapper for getaddrinfo(3). Do not use directly: call Lookup/LookupHost/LookupNumeric/LookupSubNet.
      69                 :            :  */
      70                 :            : std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
      71                 :            : 
      72                 :            : enum Network ParseNetwork(const std::string& net);
      73                 :            : std::string GetNetworkName(enum Network net);
      74                 :            : /** Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. */
      75                 :            : std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
      76                 :            : bool SetProxy(enum Network net, const Proxy &addrProxy);
      77                 :            : bool GetProxy(enum Network net, Proxy &proxyInfoOut);
      78                 :            : bool IsProxy(const CNetAddr &addr);
      79                 :            : /**
      80                 :            :  * Set the name proxy to use for all connections to nodes specified by a
      81                 :            :  * hostname. After setting this proxy, connecting to a node specified by a
      82                 :            :  * hostname won't result in a local lookup of said hostname, rather, connect to
      83                 :            :  * the node by asking the name proxy for a proxy connection to the hostname,
      84                 :            :  * effectively delegating the hostname lookup to the specified proxy.
      85                 :            :  *
      86                 :            :  * This delegation increases privacy for those who set the name proxy as they no
      87                 :            :  * longer leak their external hostname queries to their DNS servers.
      88                 :            :  *
      89                 :            :  * @returns Whether or not the operation succeeded.
      90                 :            :  *
      91                 :            :  * @note SOCKS5's support for UDP-over-SOCKS5 has been considered, but no SOCK5
      92                 :            :  *       server in common use (most notably Tor) actually implements UDP
      93                 :            :  *       support, and a DNS resolver is beyond the scope of this project.
      94                 :            :  */
      95                 :            : bool SetNameProxy(const Proxy &addrProxy);
      96                 :            : bool HaveNameProxy();
      97                 :            : bool GetNameProxy(Proxy &nameProxyOut);
      98                 :            : 
      99                 :            : using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
     100                 :            : extern DNSLookupFn g_dns_lookup;
     101                 :            : 
     102                 :            : /**
     103                 :            :  * Resolve a host string to its corresponding network addresses.
     104                 :            :  *
     105                 :            :  * @param name    The string representing a host. Could be a name or a numerical
     106                 :            :  *                IP address (IPv6 addresses in their bracketed form are
     107                 :            :  *                allowed).
     108                 :            :  *
     109                 :            :  * @returns The resulting network addresses to which the specified host
     110                 :            :  *          string resolved.
     111                 :            :  *
     112                 :            :  * @see Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn)
     113                 :            :  *      for additional parameter descriptions.
     114                 :            :  */
     115                 :            : std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
     116                 :            : 
     117                 :            : /**
     118                 :            :  * Resolve a host string to its first corresponding network address.
     119                 :            :  *
     120                 :            :  * @returns The resulting network address to which the specified host
     121                 :            :  *          string resolved or std::nullopt if host does not resolve to an address.
     122                 :            :  *
     123                 :            :  * @see LookupHost(const std::string&, unsigned int, bool, DNSLookupFn)
     124                 :            :  *      for additional parameter descriptions.
     125                 :            :  */
     126                 :            : std::optional<CNetAddr> LookupHost(const std::string& name, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
     127                 :            : 
     128                 :            : /**
     129                 :            :  * Resolve a service string to its corresponding service.
     130                 :            :  *
     131                 :            :  * @param name    The string representing a service. Could be a name or a
     132                 :            :  *                numerical IP address (IPv6 addresses should be in their
     133                 :            :  *                disambiguated bracketed form), optionally followed by a uint16_t port
     134                 :            :  *                number. (e.g. example.com:8333 or
     135                 :            :  *                [2001:db8:85a3:8d3:1319:8a2e:370:7348]:420)
     136                 :            :  * @param portDefault The default port for resulting services if not specified
     137                 :            :  *                    by the service string.
     138                 :            :  * @param fAllowLookup Whether or not hostname lookups are permitted. If yes,
     139                 :            :  *                     external queries may be performed.
     140                 :            :  * @param nMaxSolutions The maximum number of results we want, specifying 0
     141                 :            :  *                      means "as many solutions as we get."
     142                 :            :  *
     143                 :            :  * @returns The resulting services to which the specified service string
     144                 :            :  *          resolved.
     145                 :            :  */
     146                 :            : std::vector<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function = g_dns_lookup);
     147                 :            : 
     148                 :            : /**
     149                 :            :  * Resolve a service string to its first corresponding service.
     150                 :            :  *
     151                 :            :  * @see Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn)
     152                 :            :  *      for additional parameter descriptions.
     153                 :            :  */
     154                 :            : std::optional<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
     155                 :            : 
     156                 :            : /**
     157                 :            :  * Resolve a service string with a numeric IP to its first corresponding
     158                 :            :  * service.
     159                 :            :  *
     160                 :            :  * @returns The resulting CService if the resolution was successful, [::]:0 otherwise.
     161                 :            :  *
     162                 :            :  * @see Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn)
     163                 :            :  *      for additional parameter descriptions.
     164                 :            :  */
     165                 :            : CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
     166                 :            : 
     167                 :            : /**
     168                 :            :  * Parse and resolve a specified subnet string into the appropriate internal
     169                 :            :  * representation.
     170                 :            :  *
     171                 :            :  * @param[in]  subnet_str  A string representation of a subnet of the form
     172                 :            :  *                         `network address [ "/", ( CIDR-style suffix | netmask ) ]`
     173                 :            :  *                         e.g. "2001:db8::/32", "192.0.2.0/255.255.255.0" or "8.8.8.8".
     174                 :            :  * @param[out] subnet_out  Internal subnet representation, if parsable/resolvable
     175                 :            :  *                         from `subnet_str`.
     176                 :            :  * @returns whether the operation succeeded or not.
     177                 :            :  */
     178                 :            : bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out);
     179                 :            : 
     180                 :            : /**
     181                 :            :  * Create a TCP socket in the given address family.
     182                 :            :  * @param[in] address_family The socket is created in the same address family as this address.
     183                 :            :  * @return pointer to the created Sock object or unique_ptr that owns nothing in case of failure
     184                 :            :  */
     185                 :            : std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
     186                 :            : 
     187                 :            : /**
     188                 :            :  * Socket factory. Defaults to `CreateSockTCP()`, but can be overridden by unit tests.
     189                 :            :  */
     190                 :            : extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
     191                 :            : 
     192                 :            : /**
     193                 :            :  * Try to connect to the specified service on the specified socket.
     194                 :            :  *
     195                 :            :  * @param addrConnect The service to which to connect.
     196                 :            :  * @param sock The socket on which to connect.
     197                 :            :  * @param nTimeout Wait this many milliseconds for the connection to be
     198                 :            :  *                 established.
     199                 :            :  * @param manual_connection Whether or not the connection was manually requested
     200                 :            :  *                          (e.g. through the addnode RPC)
     201                 :            :  *
     202                 :            :  * @returns Whether or not a connection was successfully made.
     203                 :            :  */
     204                 :            : bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection);
     205                 :            : 
     206                 :            : /**
     207                 :            :  * Connect to a specified destination service through a SOCKS5 proxy by first
     208                 :            :  * connecting to the SOCKS5 proxy.
     209                 :            :  *
     210                 :            :  * @param proxy The SOCKS5 proxy.
     211                 :            :  * @param strDest The destination service to which to connect.
     212                 :            :  * @param port The destination port.
     213                 :            :  * @param sock The socket on which to connect to the SOCKS5 proxy.
     214                 :            :  * @param nTimeout Wait this many milliseconds for the connection to the SOCKS5
     215                 :            :  *                 proxy to be established.
     216                 :            :  * @param[out] outProxyConnectionFailed Whether or not the connection to the
     217                 :            :  *                                      SOCKS5 proxy failed.
     218                 :            :  *
     219                 :            :  * @returns Whether or not the operation succeeded.
     220                 :            :  */
     221                 :            : bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
     222                 :            : 
     223                 :            : void InterruptSocks5(bool interrupt);
     224                 :            : 
     225                 :            : /**
     226                 :            :  * Connect to a specified destination service through an already connected
     227                 :            :  * SOCKS5 proxy.
     228                 :            :  *
     229                 :            :  * @param strDest The destination fully-qualified domain name.
     230                 :            :  * @param port The destination port.
     231                 :            :  * @param auth The credentials with which to authenticate with the specified
     232                 :            :  *             SOCKS5 proxy.
     233                 :            :  * @param socket The SOCKS5 proxy socket.
     234                 :            :  *
     235                 :            :  * @returns Whether or not the operation succeeded.
     236                 :            :  *
     237                 :            :  * @note The specified SOCKS5 proxy socket must already be connected to the
     238                 :            :  *       SOCKS5 proxy.
     239                 :            :  *
     240                 :            :  * @see <a href="https://www.ietf.org/rfc/rfc1928.txt">RFC1928: SOCKS Protocol
     241                 :            :  *      Version 5</a>
     242                 :            :  */
     243                 :            : bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
     244                 :            : 
     245                 :            : /**
     246                 :            :  * Determine if a port is "bad" from the perspective of attempting to connect
     247                 :            :  * to a node on that port.
     248                 :            :  * @see doc/p2p-bad-ports.md
     249                 :            :  * @param[in] port Port to check.
     250                 :            :  * @returns whether the port is bad
     251                 :            :  */
     252                 :            : bool IsBadPort(uint16_t port);
     253                 :            : 
     254                 :            : #endif // BITCOIN_NETBASE_H

Generated by: LCOV version 1.14