LCOV - code coverage report
Current view: top level - src - netbase.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 3 32 9.4 %
Date: 2023-11-12 01:39:15 Functions: 4 16 25.0 %
Branches: 1 8 12.5 %

           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 <unordered_set>
      23                 :            : #include <vector>
      24                 :            : 
      25                 :            : extern int nConnectTimeout;
      26                 :            : extern bool fNameLookup;
      27                 :            : 
      28                 :            : //! -timeout default
      29                 :            : static const int DEFAULT_CONNECT_TIMEOUT = 5000;
      30                 :            : //! -dns default
      31                 :            : static const int DEFAULT_NAME_LOOKUP = true;
      32                 :            : 
      33                 :            : enum class ConnectionDirection {
      34                 :            :     None = 0,
      35                 :            :     In = (1U << 0),
      36                 :            :     Out = (1U << 1),
      37                 :            :     Both = (In | Out),
      38                 :            : };
      39                 :            : static inline ConnectionDirection& operator|=(ConnectionDirection& a, ConnectionDirection b) {
      40                 :            :     using underlying = typename std::underlying_type<ConnectionDirection>::type;
      41                 :            :     a = ConnectionDirection(underlying(a) | underlying(b));
      42                 :            :     return a;
      43                 :            : }
      44                 :          0 : static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
      45                 :            :     using underlying = typename std::underlying_type<ConnectionDirection>::type;
      46                 :          0 :     return (underlying(a) & underlying(b));
      47                 :            : }
      48                 :            : 
      49                 :          0 : class Proxy
      50                 :            : {
      51                 :            : public:
      52                 :         16 :     Proxy(): randomize_credentials(false) {}
      53                 :          0 :     explicit Proxy(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
      54                 :            : 
      55                 :          0 :     bool IsValid() const { return proxy.IsValid(); }
      56                 :            : 
      57                 :            :     CService proxy;
      58                 :            :     bool randomize_credentials;
      59                 :            : };
      60                 :            : 
      61                 :            : /** Credentials for proxy authentication */
      62                 :          0 : struct ProxyCredentials
      63                 :            : {
      64                 :            :     std::string username;
      65                 :            :     std::string password;
      66                 :            : };
      67                 :            : 
      68                 :            : /**
      69                 :            :  * List of reachable networks. Everything is reachable by default.
      70                 :            :  */
      71                 :          2 : class ReachableNets {
      72                 :            : public:
      73                 :          0 :     void Add(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      74                 :            :     {
      75                 :          0 :         AssertLockNotHeld(m_mutex);
      76                 :          0 :         LOCK(m_mutex);
      77         [ #  # ]:          0 :         m_reachable.insert(net);
      78                 :          0 :     }
      79                 :            : 
      80                 :          0 :     void Remove(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      81                 :            :     {
      82                 :          0 :         AssertLockNotHeld(m_mutex);
      83                 :          0 :         LOCK(m_mutex);
      84         [ #  # ]:          0 :         m_reachable.erase(net);
      85                 :          0 :     }
      86                 :            : 
      87                 :          0 :     void RemoveAll() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      88                 :            :     {
      89                 :          0 :         AssertLockNotHeld(m_mutex);
      90                 :          0 :         LOCK(m_mutex);
      91                 :          0 :         m_reachable.clear();
      92                 :          0 :     }
      93                 :            : 
      94                 :          0 :     [[nodiscard]] bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      95                 :            :     {
      96                 :          0 :         AssertLockNotHeld(m_mutex);
      97                 :          0 :         LOCK(m_mutex);
      98         [ #  # ]:          0 :         return m_reachable.count(net) > 0;
      99                 :          0 :     }
     100                 :            : 
     101                 :          0 :     [[nodiscard]] bool Contains(const CNetAddr& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
     102                 :            :     {
     103                 :          0 :         AssertLockNotHeld(m_mutex);
     104                 :          0 :         return Contains(addr.GetNetwork());
     105                 :            :     }
     106                 :            : 
     107                 :            : private:
     108                 :            :     mutable Mutex m_mutex;
     109                 :            : 
     110         [ +  - ]:          2 :     std::unordered_set<Network> m_reachable GUARDED_BY(m_mutex){
     111                 :            :         NET_UNROUTABLE,
     112                 :            :         NET_IPV4,
     113                 :            :         NET_IPV6,
     114                 :            :         NET_ONION,
     115                 :            :         NET_I2P,
     116                 :            :         NET_CJDNS,
     117                 :            :         NET_INTERNAL
     118                 :            :     };
     119                 :            : };
     120                 :            : 
     121                 :            : extern ReachableNets g_reachable_nets;
     122                 :            : 
     123                 :            : /**
     124                 :            :  * Wrapper for getaddrinfo(3). Do not use directly: call Lookup/LookupHost/LookupNumeric/LookupSubNet.
     125                 :            :  */
     126                 :            : std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
     127                 :            : 
     128                 :            : enum Network ParseNetwork(const std::string& net);
     129                 :            : std::string GetNetworkName(enum Network net);
     130                 :            : /** Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. */
     131                 :            : std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
     132                 :            : bool SetProxy(enum Network net, const Proxy &addrProxy);
     133                 :            : bool GetProxy(enum Network net, Proxy &proxyInfoOut);
     134                 :            : bool IsProxy(const CNetAddr &addr);
     135                 :            : /**
     136                 :            :  * Set the name proxy to use for all connections to nodes specified by a
     137                 :            :  * hostname. After setting this proxy, connecting to a node specified by a
     138                 :            :  * hostname won't result in a local lookup of said hostname, rather, connect to
     139                 :            :  * the node by asking the name proxy for a proxy connection to the hostname,
     140                 :            :  * effectively delegating the hostname lookup to the specified proxy.
     141                 :            :  *
     142                 :            :  * This delegation increases privacy for those who set the name proxy as they no
     143                 :            :  * longer leak their external hostname queries to their DNS servers.
     144                 :            :  *
     145                 :            :  * @returns Whether or not the operation succeeded.
     146                 :            :  *
     147                 :            :  * @note SOCKS5's support for UDP-over-SOCKS5 has been considered, but no SOCK5
     148                 :            :  *       server in common use (most notably Tor) actually implements UDP
     149                 :            :  *       support, and a DNS resolver is beyond the scope of this project.
     150                 :            :  */
     151                 :            : bool SetNameProxy(const Proxy &addrProxy);
     152                 :            : bool HaveNameProxy();
     153                 :            : bool GetNameProxy(Proxy &nameProxyOut);
     154                 :            : 
     155                 :            : using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
     156                 :            : extern DNSLookupFn g_dns_lookup;
     157                 :            : 
     158                 :            : /**
     159                 :            :  * Resolve a host string to its corresponding network addresses.
     160                 :            :  *
     161                 :            :  * @param name    The string representing a host. Could be a name or a numerical
     162                 :            :  *                IP address (IPv6 addresses in their bracketed form are
     163                 :            :  *                allowed).
     164                 :            :  *
     165                 :            :  * @returns The resulting network addresses to which the specified host
     166                 :            :  *          string resolved.
     167                 :            :  *
     168                 :            :  * @see Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn)
     169                 :            :  *      for additional parameter descriptions.
     170                 :            :  */
     171                 :            : std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
     172                 :            : 
     173                 :            : /**
     174                 :            :  * Resolve a host string to its first corresponding network address.
     175                 :            :  *
     176                 :            :  * @returns The resulting network address to which the specified host
     177                 :            :  *          string resolved or std::nullopt if host does not resolve to an address.
     178                 :            :  *
     179                 :            :  * @see LookupHost(const std::string&, unsigned int, bool, DNSLookupFn)
     180                 :            :  *      for additional parameter descriptions.
     181                 :            :  */
     182                 :            : std::optional<CNetAddr> LookupHost(const std::string& name, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
     183                 :            : 
     184                 :            : /**
     185                 :            :  * Resolve a service string to its corresponding service.
     186                 :            :  *
     187                 :            :  * @param name    The string representing a service. Could be a name or a
     188                 :            :  *                numerical IP address (IPv6 addresses should be in their
     189                 :            :  *                disambiguated bracketed form), optionally followed by a uint16_t port
     190                 :            :  *                number. (e.g. example.com:8333 or
     191                 :            :  *                [2001:db8:85a3:8d3:1319:8a2e:370:7348]:420)
     192                 :            :  * @param portDefault The default port for resulting services if not specified
     193                 :            :  *                    by the service string.
     194                 :            :  * @param fAllowLookup Whether or not hostname lookups are permitted. If yes,
     195                 :            :  *                     external queries may be performed.
     196                 :            :  * @param nMaxSolutions The maximum number of results we want, specifying 0
     197                 :            :  *                      means "as many solutions as we get."
     198                 :            :  *
     199                 :            :  * @returns The resulting services to which the specified service string
     200                 :            :  *          resolved.
     201                 :            :  */
     202                 :            : std::vector<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function = g_dns_lookup);
     203                 :            : 
     204                 :            : /**
     205                 :            :  * Resolve a service string to its first corresponding service.
     206                 :            :  *
     207                 :            :  * @see Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn)
     208                 :            :  *      for additional parameter descriptions.
     209                 :            :  */
     210                 :            : std::optional<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
     211                 :            : 
     212                 :            : /**
     213                 :            :  * Resolve a service string with a numeric IP to its first corresponding
     214                 :            :  * service.
     215                 :            :  *
     216                 :            :  * @returns The resulting CService if the resolution was successful, [::]:0 otherwise.
     217                 :            :  *
     218                 :            :  * @see Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn)
     219                 :            :  *      for additional parameter descriptions.
     220                 :            :  */
     221                 :            : CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
     222                 :            : 
     223                 :            : /**
     224                 :            :  * Parse and resolve a specified subnet string into the appropriate internal
     225                 :            :  * representation.
     226                 :            :  *
     227                 :            :  * @param[in]  subnet_str  A string representation of a subnet of the form
     228                 :            :  *                         `network address [ "/", ( CIDR-style suffix | netmask ) ]`
     229                 :            :  *                         e.g. "2001:db8::/32", "192.0.2.0/255.255.255.0" or "8.8.8.8".
     230                 :            :  * @returns a CSubNet object (that may or may not be valid).
     231                 :            :  */
     232                 :            : CSubNet LookupSubNet(const std::string& subnet_str);
     233                 :            : 
     234                 :            : /**
     235                 :            :  * Create a TCP socket in the given address family.
     236                 :            :  * @param[in] address_family The socket is created in the same address family as this address.
     237                 :            :  * @return pointer to the created Sock object or unique_ptr that owns nothing in case of failure
     238                 :            :  */
     239                 :            : std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
     240                 :            : 
     241                 :            : /**
     242                 :            :  * Socket factory. Defaults to `CreateSockTCP()`, but can be overridden by unit tests.
     243                 :            :  */
     244                 :            : extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
     245                 :            : 
     246                 :            : /**
     247                 :            :  * Try to connect to the specified service on the specified socket.
     248                 :            :  *
     249                 :            :  * @param addrConnect The service to which to connect.
     250                 :            :  * @param sock The socket on which to connect.
     251                 :            :  * @param nTimeout Wait this many milliseconds for the connection to be
     252                 :            :  *                 established.
     253                 :            :  * @param manual_connection Whether or not the connection was manually requested
     254                 :            :  *                          (e.g. through the addnode RPC)
     255                 :            :  *
     256                 :            :  * @returns Whether or not a connection was successfully made.
     257                 :            :  */
     258                 :            : bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection);
     259                 :            : 
     260                 :            : /**
     261                 :            :  * Connect to a specified destination service through a SOCKS5 proxy by first
     262                 :            :  * connecting to the SOCKS5 proxy.
     263                 :            :  *
     264                 :            :  * @param proxy The SOCKS5 proxy.
     265                 :            :  * @param strDest The destination service to which to connect.
     266                 :            :  * @param port The destination port.
     267                 :            :  * @param sock The socket on which to connect to the SOCKS5 proxy.
     268                 :            :  * @param nTimeout Wait this many milliseconds for the connection to the SOCKS5
     269                 :            :  *                 proxy to be established.
     270                 :            :  * @param[out] outProxyConnectionFailed Whether or not the connection to the
     271                 :            :  *                                      SOCKS5 proxy failed.
     272                 :            :  *
     273                 :            :  * @returns Whether or not the operation succeeded.
     274                 :            :  */
     275                 :            : bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
     276                 :            : 
     277                 :            : void InterruptSocks5(bool interrupt);
     278                 :            : 
     279                 :            : /**
     280                 :            :  * Connect to a specified destination service through an already connected
     281                 :            :  * SOCKS5 proxy.
     282                 :            :  *
     283                 :            :  * @param strDest The destination fully-qualified domain name.
     284                 :            :  * @param port The destination port.
     285                 :            :  * @param auth The credentials with which to authenticate with the specified
     286                 :            :  *             SOCKS5 proxy.
     287                 :            :  * @param socket The SOCKS5 proxy socket.
     288                 :            :  *
     289                 :            :  * @returns Whether or not the operation succeeded.
     290                 :            :  *
     291                 :            :  * @note The specified SOCKS5 proxy socket must already be connected to the
     292                 :            :  *       SOCKS5 proxy.
     293                 :            :  *
     294                 :            :  * @see <a href="https://www.ietf.org/rfc/rfc1928.txt">RFC1928: SOCKS Protocol
     295                 :            :  *      Version 5</a>
     296                 :            :  */
     297                 :            : bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
     298                 :            : 
     299                 :            : /**
     300                 :            :  * Determine if a port is "bad" from the perspective of attempting to connect
     301                 :            :  * to a node on that port.
     302                 :            :  * @see doc/p2p-bad-ports.md
     303                 :            :  * @param[in] port Port to check.
     304                 :            :  * @returns whether the port is bad
     305                 :            :  */
     306                 :            : bool IsBadPort(uint16_t port);
     307                 :            : 
     308                 :            : /**
     309                 :            :  * If an IPv6 address belongs to the address range used by the CJDNS network and
     310                 :            :  * the CJDNS network is reachable (-cjdnsreachable config is set), then change
     311                 :            :  * the type from NET_IPV6 to NET_CJDNS.
     312                 :            :  * @param[in] service Address to potentially convert.
     313                 :            :  * @return a copy of `service` either unmodified or changed to CJDNS.
     314                 :            :  */
     315                 :            : CService MaybeFlipIPv6toCJDNS(const CService& service);
     316                 :            : 
     317                 :            : #endif // BITCOIN_NETBASE_H

Generated by: LCOV version 1.14