Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/netbase.h
Line
Count
Source
1
// Copyright (c) 2009-present 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
#include <compat/compat.h>
9
#include <netaddress.h>
10
#include <serialize.h>
11
#include <util/sock.h>
12
#include <util/threadinterrupt.h>
13
14
#include <cstdint>
15
#include <functional>
16
#include <memory>
17
#include <optional>
18
#include <string>
19
#include <type_traits>
20
#include <unordered_set>
21
#include <vector>
22
23
extern int nConnectTimeout;
24
extern bool fNameLookup;
25
26
//! -timeout default
27
static const int DEFAULT_CONNECT_TIMEOUT = 5000;
28
//! -dns default
29
static const int DEFAULT_NAME_LOOKUP = true;
30
31
/** Prefix for unix domain socket addresses (which are local filesystem paths) */
32
const std::string ADDR_PREFIX_UNIX = "unix:";
33
34
enum class ConnectionDirection {
35
    None = 0,
36
    In = (1U << 0),
37
    Out = (1U << 1),
38
    Both = (In | Out),
39
};
40
225
static inline ConnectionDirection& operator|=(ConnectionDirection& a, ConnectionDirection b) {
41
225
    using underlying = std::underlying_type_t<ConnectionDirection>;
42
225
    a = ConnectionDirection(underlying(a) | underlying(b));
43
225
    return a;
44
225
}
Unexecuted instantiation: addrman_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: banman_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: denialofservice_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: headers_sync_chainwork_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: i2p_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: net_peer_connection_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: net_peer_eviction_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: net_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: netbase_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: node_init_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: orphanage_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: pcp_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: peerman_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: private_broadcast_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: txdownload_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: txreconciliation_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: txrequest_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: validation_tests.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: net.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: setup_common.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: addrdb.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: headerssync.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: httpserver.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: i2p.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: init.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: mapport.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: net_processing.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: context.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: eviction.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: interfaces.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: peerman_args.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: transaction.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: txdownloadman_impl.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: txorphanage.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: txreconciliation.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: private_broadcast.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: blockchain.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: mempool.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: mining.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: node.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: server_util.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: torcontrol.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: txrequest.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: ipc_test.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: protocol.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: process.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: init.capnp.proxy-client.c++:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: init.capnp.proxy-types.c++:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: init.capnp.proxy-server.c++:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: netif.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: pcp.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
net_permissions.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Line
Count
Source
40
225
static inline ConnectionDirection& operator|=(ConnectionDirection& a, ConnectionDirection b) {
41
225
    using underlying = std::underlying_type_t<ConnectionDirection>;
42
225
    a = ConnectionDirection(underlying(a) | underlying(b));
43
225
    return a;
44
225
}
Unexecuted instantiation: net_types.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: netbase.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
Unexecuted instantiation: bitcoind.cpp:operator|=(ConnectionDirection&, ConnectionDirection)
45
1.29k
static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
46
1.29k
    using underlying = std::underlying_type_t<ConnectionDirection>;
47
1.29k
    return (underlying(a) & underlying(b));
48
1.29k
}
Unexecuted instantiation: addrman_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: banman_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: denialofservice_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: headers_sync_chainwork_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: i2p_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: net_peer_connection_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: net_peer_eviction_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: net_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: netbase_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: node_init_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: orphanage_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: pcp_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: peerman_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: private_broadcast_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: txdownload_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: txreconciliation_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: txrequest_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: validation_tests.cpp:operator&(ConnectionDirection, ConnectionDirection)
net.cpp:operator&(ConnectionDirection, ConnectionDirection)
Line
Count
Source
45
1.01k
static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
46
1.01k
    using underlying = std::underlying_type_t<ConnectionDirection>;
47
1.01k
    return (underlying(a) & underlying(b));
48
1.01k
}
Unexecuted instantiation: setup_common.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: addrdb.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: headerssync.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: httpserver.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: i2p.cpp:operator&(ConnectionDirection, ConnectionDirection)
init.cpp:operator&(ConnectionDirection, ConnectionDirection)
Line
Count
Source
45
286
static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
46
286
    using underlying = std::underlying_type_t<ConnectionDirection>;
47
286
    return (underlying(a) & underlying(b));
48
286
}
Unexecuted instantiation: mapport.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: net_processing.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: context.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: eviction.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: interfaces.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: peerman_args.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: transaction.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: txdownloadman_impl.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: txorphanage.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: txreconciliation.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: private_broadcast.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: blockchain.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: mempool.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: mining.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: node.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: server_util.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: torcontrol.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: txrequest.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: ipc_test.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: protocol.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: process.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: init.capnp.proxy-client.c++:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: init.capnp.proxy-types.c++:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: init.capnp.proxy-server.c++:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: netif.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: pcp.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: net_permissions.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: net_types.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: netbase.cpp:operator&(ConnectionDirection, ConnectionDirection)
Unexecuted instantiation: bitcoind.cpp:operator&(ConnectionDirection, ConnectionDirection)
49
50
/**
51
 * Check if a string is a valid UNIX domain socket path
52
 *
53
 * @param      name     The string provided by the user representing a local path
54
 *
55
 * @returns Whether the string has proper format, length, and points to an existing file path
56
 */
57
bool IsUnixSocketPath(const std::string& name);
58
59
class Proxy
60
{
61
public:
62
15.9k
    Proxy() : m_is_unix_socket(false), m_tor_stream_isolation(false) {}
63
62
    explicit Proxy(const CService& _proxy, bool tor_stream_isolation = false) : proxy(_proxy), m_is_unix_socket(false), m_tor_stream_isolation(tor_stream_isolation) {}
64
    explicit Proxy(std::string path, bool tor_stream_isolation = false)
65
2
        : m_unix_socket_path(std::move(path)), m_is_unix_socket(true), m_tor_stream_isolation(tor_stream_isolation) {}
66
67
    CService proxy;
68
    std::string m_unix_socket_path;
69
    bool m_is_unix_socket;
70
    bool m_tor_stream_isolation;
71
72
    bool IsValid() const
73
12.2k
    {
74
12.2k
        if (m_is_unix_socket) return IsUnixSocketPath(m_unix_socket_path);
75
12.1k
        return proxy.IsValid();
76
12.2k
    }
77
78
    sa_family_t GetFamily() const
79
0
    {
80
0
        if (m_is_unix_socket) return AF_UNIX;
81
0
        return proxy.GetSAFamily();
82
0
    }
83
84
    std::string ToString() const
85
312
    {
86
312
        if (m_is_unix_socket) return m_unix_socket_path;
87
295
        return proxy.ToStringAddrPort();
88
312
    }
89
90
    std::unique_ptr<Sock> Connect() const;
91
};
92
93
/** Credentials for proxy authentication */
94
struct ProxyCredentials
95
{
96
    std::string username;
97
    std::string password;
98
};
99
100
/**
101
 * List of reachable networks. Everything is reachable by default.
102
 */
103
class ReachableNets {
104
public:
105
    void Add(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
106
25
    {
107
25
        AssertLockNotHeld(m_mutex);
108
25
        LOCK(m_mutex);
109
25
        m_reachable.insert(net);
110
25
    }
111
112
    void Remove(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
113
3.06k
    {
114
3.06k
        AssertLockNotHeld(m_mutex);
115
3.06k
        LOCK(m_mutex);
116
3.06k
        m_reachable.erase(net);
117
3.06k
    }
118
119
    void RemoveAll() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
120
9
    {
121
9
        AssertLockNotHeld(m_mutex);
122
9
        LOCK(m_mutex);
123
9
        m_reachable.clear();
124
9
    }
125
126
    void Reset() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
127
668
    {
128
668
        AssertLockNotHeld(m_mutex);
129
668
        LOCK(m_mutex);
130
668
        m_reachable = DefaultNets();
131
668
    }
132
133
    [[nodiscard]] bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
134
10.6k
    {
135
10.6k
        AssertLockNotHeld(m_mutex);
136
10.6k
        LOCK(m_mutex);
137
10.6k
        return m_reachable.contains(net);
138
10.6k
    }
139
140
    [[nodiscard]] bool Contains(const CNetAddr& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
141
1.31k
    {
142
1.31k
        AssertLockNotHeld(m_mutex);
143
1.31k
        return Contains(addr.GetNetwork());
144
1.31k
    }
145
146
    [[nodiscard]] std::unordered_set<Network> All() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
147
17
    {
148
17
        AssertLockNotHeld(m_mutex);
149
17
        LOCK(m_mutex);
150
17
        return m_reachable;
151
17
    }
152
153
private:
154
    static std::unordered_set<Network> DefaultNets()
155
2.00k
    {
156
2.00k
        return {
157
2.00k
            NET_UNROUTABLE,
158
2.00k
            NET_IPV4,
159
2.00k
            NET_IPV6,
160
2.00k
            NET_ONION,
161
2.00k
            NET_I2P,
162
2.00k
            NET_CJDNS,
163
2.00k
            NET_INTERNAL
164
2.00k
        };
165
2.00k
    };
166
167
    mutable Mutex m_mutex;
168
    std::unordered_set<Network> m_reachable GUARDED_BY(m_mutex){DefaultNets()};
169
};
170
171
extern ReachableNets g_reachable_nets;
172
173
/**
174
 * Wrapper for getaddrinfo(3). Do not use directly: call Lookup/LookupHost/LookupNumeric/LookupSubNet.
175
 */
176
std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
177
178
enum Network ParseNetwork(const std::string& net);
179
std::string GetNetworkName(enum Network net);
180
/** Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. */
181
std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
182
bool SetProxy(enum Network net, const Proxy &addrProxy);
183
std::optional<Proxy> GetProxy(enum Network net);
184
bool IsProxy(const CNetAddr &addr);
185
/**
186
 * Set the name proxy to use for all connections to nodes specified by a
187
 * hostname. After setting this proxy, connecting to a node specified by a
188
 * hostname won't result in a local lookup of said hostname, rather, connect to
189
 * the node by asking the name proxy for a proxy connection to the hostname,
190
 * effectively delegating the hostname lookup to the specified proxy.
191
 *
192
 * This delegation increases privacy for those who set the name proxy as they no
193
 * longer leak their external hostname queries to their DNS servers.
194
 *
195
 * @returns Whether or not the operation succeeded.
196
 *
197
 * @note SOCKS5's support for UDP-over-SOCKS5 has been considered, but no SOCK5
198
 *       server in common use (most notably Tor) actually implements UDP
199
 *       support, and a DNS resolver is beyond the scope of this project.
200
 */
201
bool SetNameProxy(const Proxy &addrProxy);
202
bool HaveNameProxy();
203
std::optional<Proxy> GetNameProxy();
204
205
using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
206
extern DNSLookupFn g_dns_lookup;
207
208
/**
209
 * Resolve a host string to its corresponding network addresses.
210
 *
211
 * @param name    The string representing a host. Could be a name or a numerical
212
 *                IP address (IPv6 addresses in their bracketed form are
213
 *                allowed).
214
 *
215
 * @returns The resulting network addresses to which the specified host
216
 *          string resolved.
217
 *
218
 * @see Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn)
219
 *      for additional parameter descriptions.
220
 */
221
std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
222
223
/**
224
 * Resolve a host string to its first corresponding network address.
225
 *
226
 * @returns The resulting network address to which the specified host
227
 *          string resolved or std::nullopt if host does not resolve to an address.
228
 *
229
 * @see LookupHost(const std::string&, unsigned int, bool, DNSLookupFn)
230
 *      for additional parameter descriptions.
231
 */
232
std::optional<CNetAddr> LookupHost(const std::string& name, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
233
234
/**
235
 * Resolve a service string to its corresponding service.
236
 *
237
 * @param name    The string representing a service. Could be a name or a
238
 *                numerical IP address (IPv6 addresses should be in their
239
 *                disambiguated bracketed form), optionally followed by a uint16_t port
240
 *                number. (e.g. example.com:8333 or
241
 *                [2001:db8:85a3:8d3:1319:8a2e:370:7348]:420)
242
 * @param portDefault The default port for resulting services if not specified
243
 *                    by the service string.
244
 * @param fAllowLookup Whether or not hostname lookups are permitted. If yes,
245
 *                     external queries may be performed.
246
 * @param nMaxSolutions The maximum number of results we want, specifying 0
247
 *                      means "as many solutions as we get."
248
 *
249
 * @returns The resulting services to which the specified service string
250
 *          resolved.
251
 */
252
std::vector<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function = g_dns_lookup);
253
254
/**
255
 * Resolve a service string to its first corresponding service.
256
 *
257
 * @see Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn)
258
 *      for additional parameter descriptions.
259
 */
260
std::optional<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
261
262
/**
263
 * Resolve a service string with a numeric IP to its first corresponding
264
 * service.
265
 *
266
 * @returns The resulting CService if the resolution was successful, [::]:0 otherwise.
267
 *
268
 * @see Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn)
269
 *      for additional parameter descriptions.
270
 */
271
CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
272
273
/**
274
 * Parse and resolve a specified subnet string into the appropriate internal
275
 * representation.
276
 *
277
 * @param[in]  subnet_str  A string representation of a subnet of the form
278
 *                         `network address [ "/", ( CIDR-style suffix | netmask ) ]`
279
 *                         e.g. "2001:db8::/32", "192.0.2.0/255.255.255.0" or "8.8.8.8".
280
 * @returns a CSubNet object (that may or may not be valid).
281
 */
282
CSubNet LookupSubNet(const std::string& subnet_str);
283
284
/**
285
 * Create a real socket from the operating system.
286
 * @param[in] domain Communications domain, first argument to the socket(2) syscall.
287
 * @param[in] type Type of the socket, second argument to the socket(2) syscall.
288
 * @param[in] protocol The particular protocol to be used with the socket, third argument to the socket(2) syscall.
289
 * @return pointer to the created Sock object or unique_ptr that owns nothing in case of failure
290
 */
291
std::unique_ptr<Sock> CreateSockOS(int domain, int type, int protocol);
292
293
/**
294
 * Socket factory. Defaults to `CreateSockOS()`, but can be overridden by unit tests.
295
 */
296
extern std::function<std::unique_ptr<Sock>(int, int, int)> CreateSock;
297
298
/**
299
 * Create a socket and try to connect to the specified service.
300
 *
301
 * @param[in] dest The service to which to connect.
302
 * @param[in] manual_connection Whether or not the connection was manually requested (e.g. through the addnode RPC)
303
 *
304
 * @returns the connected socket if the operation succeeded, empty unique_ptr otherwise
305
 */
306
std::unique_ptr<Sock> ConnectDirectly(const CService& dest, bool manual_connection);
307
308
/**
309
 * Connect to a specified destination service through a SOCKS5 proxy by first
310
 * connecting to the SOCKS5 proxy.
311
 *
312
 * @param[in] proxy The SOCKS5 proxy.
313
 * @param[in] dest The destination service to which to connect.
314
 * @param[in] port The destination port.
315
 * @param[out] proxy_connection_failed Whether or not the connection to the SOCKS5 proxy failed.
316
 *
317
 * @returns the connected socket if the operation succeeded. Otherwise an empty unique_ptr.
318
 */
319
std::unique_ptr<Sock> ConnectThroughProxy(const Proxy& proxy,
320
                                          const std::string& dest,
321
                                          uint16_t port,
322
                                          bool& proxy_connection_failed);
323
324
/**
325
 * Interrupt SOCKS5 reads or writes.
326
 */
327
extern CThreadInterrupt g_socks5_interrupt;
328
329
/**
330
 * Connect to a specified destination service through an already connected
331
 * SOCKS5 proxy.
332
 *
333
 * @param strDest The destination fully-qualified domain name.
334
 * @param port The destination port.
335
 * @param auth The credentials with which to authenticate with the specified
336
 *             SOCKS5 proxy.
337
 * @param socket The SOCKS5 proxy socket.
338
 *
339
 * @returns Whether or not the operation succeeded.
340
 *
341
 * @note The specified SOCKS5 proxy socket must already be connected to the
342
 *       SOCKS5 proxy.
343
 *
344
 * @see <a href="https://www.ietf.org/rfc/rfc1928.txt">RFC1928: SOCKS Protocol
345
 *      Version 5</a>
346
 */
347
bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
348
349
/**
350
 * Determine if a port is "bad" from the perspective of attempting to connect
351
 * to a node on that port.
352
 * @see doc/p2p-bad-ports.md
353
 * @param[in] port Port to check.
354
 * @returns whether the port is bad
355
 */
356
bool IsBadPort(uint16_t port);
357
358
/**
359
 * If an IPv6 address belongs to the address range used by the CJDNS network and
360
 * the CJDNS network is reachable (-cjdnsreachable config is set), then change
361
 * the type from NET_IPV6 to NET_CJDNS.
362
 * @param[in] service Address to potentially convert.
363
 * @return a copy of `service` either unmodified or changed to CJDNS.
364
 */
365
CService MaybeFlipIPv6toCJDNS(const CService& service);
366
367
/** Get the bind address for a socket as CService. */
368
CService GetBindAddress(const Sock& sock);
369
370
#endif // BITCOIN_NETBASE_H