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