fuzz coverage

Coverage Report

Created: 2025-09-17 22:41

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