Bitcoin Core 31.99.0
P2P Digital Currency
net.h
Go to the documentation of this file.
1// Copyright (c) 2020-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_TEST_UTIL_NET_H
6#define BITCOIN_TEST_UTIL_NET_H
7
8#include <attributes.h>
9#include <compat/compat.h>
10#include <netmessagemaker.h>
11#include <net.h>
12#include <net_permissions.h>
13#include <net_processing.h>
14#include <netaddress.h>
16#include <node/eviction.h>
17#include <span.h>
18#include <sync.h>
19#include <util/sock.h>
20
21#include <algorithm>
22#include <array>
23#include <cassert>
24#include <chrono>
25#include <condition_variable>
26#include <cstdint>
27#include <cstring>
28#include <memory>
29#include <optional>
30#include <string>
31#include <unordered_map>
32#include <vector>
33
35
36struct ConnmanTestMsg : public CConnman {
38
40 {
41 m_msgproc = msgproc;
42 }
43
44 void SetAddrman(AddrMan& in) { addrman = in; }
45
46 void SetPeerConnectTimeout(std::chrono::seconds timeout)
47 {
48 m_peer_connect_timeout = timeout;
49 }
50
51 void ResetAddrCache();
54 void Reset();
55
56 std::vector<CNode*> TestNodes()
57 {
59 return m_nodes;
60 }
61
63 {
65 m_nodes.push_back(&node);
66
67 if (node.IsManualOrFullOutboundConn()) ++m_network_conn_counts[node.addr.GetNetwork()];
68 }
69
71 {
73 for (CNode* node : m_nodes) {
74 delete node;
75 }
76 m_nodes.clear();
77 }
78
79 void CreateNodeFromAcceptedSocketPublic(std::unique_ptr<Sock> sock,
80 NetPermissionFlags permissions,
81 const CAddress& addr_bind,
82 const CAddress& addr_peer)
83 {
84 CreateNodeFromAcceptedSocket(std::move(sock), permissions, addr_bind, addr_peer);
85 }
86
88 {
89 return InitBinds(options);
90 }
91
93 {
95 }
96
97 void Handshake(CNode& node,
98 bool successfully_connected,
99 ServiceFlags remote_services,
100 ServiceFlags local_services,
101 int32_t version,
102 bool relay_txs)
104
106 {
107 return m_msgproc->ProcessMessages(node, flagInterruptMsgProc);
108 }
109
110 void NodeReceiveMsgBytes(CNode& node, std::span<const uint8_t> msg_bytes, bool& complete) const;
111
112 bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg&& ser_msg) const;
113 void FlushSendBuffer(CNode& node) const;
114
116
117 CNode* ConnectNodePublic(PeerManager& peerman, const char* pszDest, ConnectionType conn_type)
119};
120
122 NODE_NONE,
129};
130
142};
143
152};
153
154constexpr auto ALL_NETWORKS = std::array{
162};
163
168class ZeroSock : public Sock
169{
170public:
171 ZeroSock();
172
173 ~ZeroSock() override;
174
175 ssize_t Send(const void*, size_t len, int) const override;
176
177 ssize_t Recv(void* buf, size_t len, int flags) const override;
178
179 int Connect(const sockaddr*, socklen_t) const override;
180
181 int Bind(const sockaddr*, socklen_t) const override;
182
183 int Listen(int) const override;
184
185 std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
186
187 int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;
188
189 int SetSockOpt(int, int, const void*, socklen_t) const override;
190
191 int GetSockName(sockaddr* name, socklen_t* name_len) const override;
192
193 bool SetNonBlocking() const override;
194
195 bool IsSelectable() const override;
196
197 bool Wait(std::chrono::milliseconds timeout,
198 Event requested,
199 Event* occurred = nullptr) const override;
200
201 bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override;
202
203private:
204 ZeroSock& operator=(Sock&& other) override;
205};
206
213{
214public:
215 explicit StaticContentsSock(const std::string& contents);
216
221 ssize_t Recv(void* buf, size_t len, int flags) const override;
222
223 bool IsConnected(std::string&) const override
224 {
225 return true;
226 }
227
228private:
229 StaticContentsSock& operator=(Sock&& other) override;
230
231 const std::string m_contents;
232 mutable size_t m_consumed{0};
233};
234
239class DynSock : public ZeroSock
240{
241public:
245 class Pipe
246 {
247 public:
256 ssize_t GetBytes(void* buf, size_t len, int flags = 0) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
257
263 std::optional<CNetMessage> GetNetMsg() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
264
268 void PushBytes(const void* buf, size_t len) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
269
273 template <typename... Args>
274 void PushNetMsg(const std::string& type, Args&&... payload) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
275
280
281 private:
287
289 std::condition_variable m_cond;
290 std::vector<uint8_t> m_data GUARDED_BY(m_mutex);
291 bool m_eof GUARDED_BY(m_mutex){false};
292 };
293
294 struct Pipes {
297 };
298
302 class Queue
303 {
304 public:
305 using S = std::unique_ptr<DynSock>;
306
308 {
309 LOCK(m_mutex);
310 m_queue.push(std::move(s));
311 }
312
314 {
315 LOCK(m_mutex);
316 if (m_queue.empty()) {
317 return std::nullopt;
318 }
319 S front{std::move(m_queue.front())};
320 m_queue.pop();
321 return front;
322 }
323
325 {
326 LOCK(m_mutex);
327 return m_queue.empty();
328 }
329
330 private:
331 mutable Mutex m_mutex;
332 std::queue<S> m_queue GUARDED_BY(m_mutex);
333 };
334
340 explicit DynSock(std::shared_ptr<Pipes> pipes, Queue* accept_sockets LIFETIMEBOUND);
341
348 explicit DynSock(std::shared_ptr<Pipes> pipes);
349
350 ~DynSock();
351
352 ssize_t Recv(void* buf, size_t len, int flags) const override;
353
354 ssize_t Send(const void* buf, size_t len, int) const override;
355
356 std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
357
358 bool Wait(std::chrono::milliseconds timeout,
359 Event requested,
360 Event* occurred = nullptr) const override;
361
362 bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override;
363
364private:
365 DynSock& operator=(Sock&&) override;
366
367 std::shared_ptr<Pipes> m_pipes;
369};
370
371template <typename... Args>
372void DynSock::Pipe::PushNetMsg(const std::string& type, Args&&... payload)
373{
374 auto msg = NetMsg::Make(type, std::forward<Args>(payload)...);
375 V1Transport transport{NodeId{0}};
376
377 const bool queued{transport.SetMessageToSend(msg)};
378 assert(queued);
379
380 LOCK(m_mutex);
381
382 for (;;) {
383 const auto& [bytes, _more, _msg_type] = transport.GetBytesToSend(/*have_next_message=*/true);
384 if (bytes.empty()) {
385 break;
386 }
387 m_data.insert(m_data.end(), bytes.begin(), bytes.end());
388 transport.MarkBytesSent(bytes.size());
389 }
390
391 m_cond.notify_all();
392}
393
394std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
395
396#endif // BITCOIN_TEST_UTIL_NET_H
#define LIFETIMEBOUND
Definition: attributes.h:16
int flags
Definition: bitcoin-tx.cpp:530
Stochastic address manager.
Definition: addrman.h:110
A CService with information about it as peer.
Definition: protocol.h:367
Definition: net.h:1071
std::reference_wrapper< AddrMan > addrman
Definition: net.h:1596
std::atomic< bool > flagInterruptMsgProc
Definition: net.h:1701
void CreateNodeFromAcceptedSocket(std::unique_ptr< Sock > &&sock, NetPermissionFlags permission_flags, const CService &addr_bind, const CService &addr)
Create a CNode object from a socket that has just been accepted and add the node to the m_nodes membe...
Definition: net.cpp:1766
bool InitBinds(const Options &options)
Definition: net.cpp:3432
bool AlreadyConnectedToAddress(const CNetAddr &addr) const
Determine whether we're already connected to a given address.
Definition: net.cpp:352
m_peer_connect_timeout
Definition: net.h:1119
void SocketHandler() EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex
Check connected and listening sockets for IO readiness and process them accordingly.
Definition: net.cpp:2097
RecursiveMutex m_nodes_mutex
Definition: net.h:1607
m_msgproc
Definition: net.h:1116
Mutex m_unused_i2p_sessions_mutex
Mutex protecting m_i2p_sam_sessions.
Definition: net.h:1761
CConnman(uint64_t seed0, uint64_t seed1, AddrMan &addrman, const NetGroupManager &netgroupman, const CChainParams &params, bool network_active=true, std::shared_ptr< CThreadInterrupt > interrupt_net=std::make_shared< CThreadInterrupt >())
Definition: net.cpp:3376
Network address.
Definition: netaddress.h:113
Information about a peer.
Definition: net.h:680
Unidirectional bytes or CNetMessage queue (FIFO).
Definition: net.h:246
ssize_t GetBytes(void *buf, size_t len, int flags=0) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get bytes and remove them from the pipe.
Definition: net.cpp:264
void WaitForDataOrEof(UniqueLock< Mutex > &lock) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Return when there is some data to read or EOF has been signaled.
Definition: net.cpp:339
void Eof() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Signal end-of-file on the receiving end (GetBytes() or GetNetMsg()).
Definition: net.cpp:332
std::condition_variable m_cond
Definition: net.h:289
std::optional< CNetMessage > GetNetMsg() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Deserialize a CNetMessage and remove it from the pipe.
Definition: net.cpp:286
Mutex m_mutex
Definition: net.h:288
void PushNetMsg(const std::string &type, Args &&... payload) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Construct and push CNetMessage to the pipe.
Definition: net.h:372
std::vector< uint8_t > m_data GUARDED_BY(m_mutex)
void PushBytes(const void *buf, size_t len) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Push bytes to the pipe.
Definition: net.cpp:324
bool m_eof GUARDED_BY(m_mutex)
Definition: net.h:291
A basic thread-safe queue, used for queuing sockets to be returned by Accept().
Definition: net.h:303
std::unique_ptr< DynSock > S
Definition: net.h:305
std::optional< S > Pop() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: net.h:313
std::queue< S > m_queue GUARDED_BY(m_mutex)
void Push(S s) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: net.h:307
Mutex m_mutex
Definition: net.h:331
bool Empty() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: net.h:324
A mocked Sock alternative that allows providing the data to be returned by Recv() and inspecting the ...
Definition: net.h:240
DynSock & operator=(Sock &&) override
Move assignment operator, grab the socket from another object and close ours (if set).
Definition: net.cpp:429
DynSock(std::shared_ptr< Pipes > pipes, Queue *accept_sockets LIFETIMEBOUND)
Create a new mocked sock.
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
Definition: net.cpp:364
std::shared_ptr< Pipes > m_pipes
Definition: net.h:367
~DynSock()
Definition: net.cpp:359
bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
Wait for readiness for input (recv) or output (send).
Definition: net.cpp:382
std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const override
accept(2) wrapper.
Definition: net.cpp:375
ssize_t Send(const void *buf, size_t len, int) const override
send(2) wrapper.
Definition: net.cpp:369
Queue *const m_accept_sockets
Definition: net.h:368
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const override
Same as Wait(), but wait on many sockets within the same timeout.
Definition: net.cpp:395
Fast randomness source.
Definition: random.h:386
Interface for message handling.
Definition: net.h:1027
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Definition: net.h:1030
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
Definition: sock.h:30
uint8_t Event
Definition: sock.h:141
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
Definition: sock.h:211
A mocked Sock alternative that returns a statically contained data upon read and succeeds and ignores...
Definition: net.h:213
bool IsConnected(std::string &) const override
Check if still connected.
Definition: net.h:223
size_t m_consumed
Definition: net.h:232
const std::string m_contents
Definition: net.h:231
StaticContentsSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
Definition: net.cpp:258
StaticContentsSock(const std::string &contents)
Definition: net.cpp:243
ssize_t Recv(void *buf, size_t len, int flags) const override
Return parts of the contents that was provided at construction until it is exhausted and then return ...
Definition: net.cpp:248
Wrapper around std::unique_lock style lock for MutexType.
Definition: sync.h:154
bool SetMessageToSend(CSerializedNetMsg &msg) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Set the next message to send.
Definition: net.cpp:842
A mocked Sock alternative that succeeds on all operations.
Definition: net.h:169
int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
getsockopt(2) wrapper.
Definition: net.cpp:202
bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
Wait for readiness for input (recv) or output (send).
Definition: net.cpp:220
ZeroSock()
Definition: net.cpp:166
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const override
Same as Wait(), but wait on many sockets within the same timeout.
Definition: net.cpp:228
int GetSockName(sockaddr *name, socklen_t *name_len) const override
getsockname(2) wrapper.
Definition: net.cpp:210
int Listen(int) const override
listen(2) wrapper.
Definition: net.cpp:183
~ZeroSock() override
Definition: net.cpp:169
int Bind(const sockaddr *, socklen_t) const override
bind(2) wrapper.
Definition: net.cpp:181
int Connect(const sockaddr *, socklen_t) const override
connect(2) wrapper.
Definition: net.cpp:179
ssize_t Send(const void *, size_t len, int) const override
send(2) wrapper.
Definition: net.cpp:171
std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const override
accept(2) wrapper.
Definition: net.cpp:185
bool SetNonBlocking() const override
Set the non-blocking option on the socket.
Definition: net.cpp:216
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
Definition: net.cpp:173
ZeroSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
Definition: net.cpp:237
int SetSockOpt(int, int, const void *, socklen_t) const override
setsockopt(2) wrapper.
Definition: net.cpp:208
bool IsSelectable() const override
Check if the underlying socket can be used for select(2) (or the Wait() method).
Definition: net.cpp:218
ConnectionType
Different types of connections to a peer.
@ PRIVATE_BROADCAST
Private broadcast connections are short-lived and only opened to privacy networks (Tor,...
@ BLOCK_RELAY
We use block-relay-only connections to help prevent against partition attacks.
@ MANUAL
We open manual connections to addresses that users explicitly requested via the addnode RPC or the -a...
@ OUTBOUND_FULL_RELAY
These are the default connections that we use to connect with the network.
@ FEELER
Feeler connections are short-lived connections made to check that a node is alive.
@ INBOUND
Inbound connections are those initiated by a peer.
@ ADDR_FETCH
AddrFetch connections are short lived connections used to solicit addresses from peers.
CSerializedNetMsg Make(std::string msg_type, Args &&... args)
Definition: messages.h:21
int64_t NodeId
Definition: net.h:103
NetPermissionFlags
@ NET_I2P
I2P.
Definition: netaddress.h:47
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:50
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:44
@ NET_IPV6
IPv6.
Definition: netaddress.h:41
@ NET_IPV4
IPv4.
Definition: netaddress.h:38
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:35
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:54
ServiceFlags
nServices flags
Definition: protocol.h:309
@ NODE_NONE
Definition: protocol.h:312
@ NODE_P2P_V2
Definition: protocol.h:330
@ NODE_WITNESS
Definition: protocol.h:320
@ NODE_NETWORK_LIMITED
Definition: protocol.h:327
@ NODE_BLOOM
Definition: protocol.h:317
@ NODE_NETWORK
Definition: protocol.h:315
@ NODE_COMPACT_FILTERS
Definition: protocol.h:323
const char * name
Definition: rest.cpp:49
void NodeReceiveMsgBytes(CNode &node, std::span< const uint8_t > msg_bytes, bool &complete) const
Definition: net.cpp:91
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &&ser_msg) const
Definition: net.cpp:111
CNode * ConnectNodePublic(PeerManager &peerman, const char *pszDest, ConnectionType conn_type) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex)
Definition: net.cpp:125
void SocketHandlerPublic()
Definition: net.h:92
void CreateNodeFromAcceptedSocketPublic(std::unique_ptr< Sock > sock, NetPermissionFlags permissions, const CAddress &addr_bind, const CAddress &addr_peer)
Definition: net.h:79
void ClearTestNodes()
Definition: net.h:70
void ResetAddrCache()
Definition: net.cpp:74
bool InitBindsPublic(const CConnman::Options &options)
Definition: net.h:87
bool AlreadyConnectedToAddressPublic(const CNetAddr &addr)
Definition: net.h:115
void SetMsgProc(NetEventsInterface *msgproc)
Definition: net.h:39
void AddTestNode(CNode &node)
Definition: net.h:62
void Reset()
Reset the internal state.
Definition: net.cpp:83
void SetAddrman(AddrMan &in)
Definition: net.h:44
void Handshake(CNode &node, bool successfully_connected, ServiceFlags remote_services, ServiceFlags local_services, int32_t version, bool relay_txs) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface bool ProcessMessagesOnce(CNode &node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface
Definition: net.h:105
std::vector< CNode * > TestNodes()
Definition: net.h:56
void FlushSendBuffer(CNode &node) const
Definition: net.cpp:99
void ResetMaxOutboundCycle()
Definition: net.cpp:76
void SetPeerConnectTimeout(std::chrono::seconds timeout)
Definition: net.h:46
Pipe send
Definition: net.h:296
Pipe recv
Definition: net.h:295
#define LOCK(cs)
Definition: sync.h:268
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:136
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
Definition: net.h:121
constexpr ConnectionType ALL_CONNECTION_TYPES[]
Definition: net.h:144
constexpr auto ALL_NETWORKS
Definition: net.h:154
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:131
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
assert(!tx.IsCoinBase())