Bitcoin Core  22.99.0
P2P Digital Currency
net.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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.h>
9 #include <netaddress.h>
10 #include <net.h>
11 #include <util/sock.h>
12 
13 #include <array>
14 #include <cassert>
15 #include <cstring>
16 #include <memory>
17 #include <string>
18 
19 struct ConnmanTestMsg : public CConnman {
20  using CConnman::CConnman;
21 
22  void SetPeerConnectTimeout(std::chrono::seconds timeout)
23  {
24  m_peer_connect_timeout = timeout;
25  }
26 
28  {
30  m_nodes.push_back(&node);
31  }
33  {
35  for (CNode* node : m_nodes) {
36  delete node;
37  }
38  m_nodes.clear();
39  }
40 
42 
43  void NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const;
44 
45  bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const;
46 };
47 
49  NODE_NONE,
51  NODE_BLOOM,
55 };
56 
68 };
69 
77 };
78 
79 constexpr auto ALL_NETWORKS = std::array{
87 };
88 
94 class StaticContentsSock : public Sock
95 {
96 public:
97  explicit StaticContentsSock(const std::string& contents) : m_contents{contents}, m_consumed{0}
98  {
99  // Just a dummy number that is not INVALID_SOCKET.
100  m_socket = INVALID_SOCKET - 1;
101  }
102 
103  ~StaticContentsSock() override { Reset(); }
104 
105  StaticContentsSock& operator=(Sock&& other) override
106  {
107  assert(false && "Move of Sock into MockSock not allowed.");
108  return *this;
109  }
110 
111  void Reset() override
112  {
114  }
115 
116  ssize_t Send(const void*, size_t len, int) const override { return len; }
117 
118  ssize_t Recv(void* buf, size_t len, int flags) const override
119  {
120  const size_t consume_bytes{std::min(len, m_contents.size() - m_consumed)};
121  std::memcpy(buf, m_contents.data() + m_consumed, consume_bytes);
122  if ((flags & MSG_PEEK) == 0) {
123  m_consumed += consume_bytes;
124  }
125  return consume_bytes;
126  }
127 
128  int Connect(const sockaddr*, socklen_t) const override { return 0; }
129 
130  std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override
131  {
132  if (addr != nullptr) {
133  // Pretend all connections come from 5.5.5.5:6789
134  memset(addr, 0x00, *addr_len);
135  const socklen_t write_len = static_cast<socklen_t>(sizeof(sockaddr_in));
136  if (*addr_len >= write_len) {
137  *addr_len = write_len;
138  sockaddr_in* addr_in = reinterpret_cast<sockaddr_in*>(addr);
139  addr_in->sin_family = AF_INET;
140  memset(&addr_in->sin_addr, 0x05, sizeof(addr_in->sin_addr));
141  addr_in->sin_port = htons(6789);
142  }
143  }
144  return std::make_unique<StaticContentsSock>("");
145  };
146 
147  int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override
148  {
149  std::memset(opt_val, 0x0, *opt_len);
150  return 0;
151  }
152 
153  bool Wait(std::chrono::milliseconds timeout,
154  Event requested,
155  Event* occurred = nullptr) const override
156  {
157  if (occurred != nullptr) {
158  *occurred = requested;
159  }
160  return true;
161  }
162 
163 private:
164  const std::string m_contents;
165  mutable size_t m_consumed;
166 };
167 
168 std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
169 
170 #endif // BITCOIN_TEST_UTIL_NET_H
ConnmanTestMsg::ClearTestNodes
void ClearTestNodes()
Definition: net.h:32
NET_UNROUTABLE
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
Sock::m_socket
SOCKET m_socket
Contained socket.
Definition: sock.h:185
assert
assert(!tx.IsCoinBase())
NetPermissionFlags::None
@ None
NET_IPV4
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
StaticContentsSock
A mocked Sock alternative that returns a statically contained data upon read and succeeds and ignores...
Definition: net.h:94
flags
int flags
Definition: bitcoin-tx.cpp:529
NET_INTERNAL
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
StaticContentsSock::Connect
int Connect(const sockaddr *, socklen_t) const override
connect(2) wrapper.
Definition: net.h:128
NetPermissionFlags::NoBan
@ NoBan
StaticContentsSock::m_consumed
size_t m_consumed
Definition: net.h:165
ConnectionType::OUTBOUND_FULL_RELAY
@ OUTBOUND_FULL_RELAY
These are the default connections that we use to connect with the network.
CNode
Information about a peer.
Definition: net.h:394
ALL_NET_PERMISSION_FLAGS
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:57
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:271
NODE_NETWORK_LIMITED
@ NODE_NETWORK_LIMITED
Definition: protocol.h:291
StaticContentsSock::Send
ssize_t Send(const void *, size_t len, int) const override
send(2) wrapper.
Definition: net.h:116
NODE_NETWORK
@ NODE_NETWORK
Definition: protocol.h:277
ConnmanTestMsg::ReceiveMsgFrom
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &ser_msg) const
Definition: net.cpp:33
NODE_COMPACT_FILTERS
@ NODE_COMPACT_FILTERS
Definition: protocol.h:287
NetPermissionFlags
NetPermissionFlags
Definition: net_permissions.h:18
ConnectionType::MANUAL
@ MANUAL
We open manual connections to addresses that users explicitly requested via the addnode RPC or the -a...
Sock
RAII helper class that manages a socket.
Definition: sock.h:26
ConnmanTestMsg
Definition: net.h:19
ConnmanTestMsg::SetPeerConnectTimeout
void SetPeerConnectTimeout(std::chrono::seconds timeout)
Definition: net.h:22
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:96
NetPermissionFlags::BloomFilter
@ BloomFilter
ConnmanTestMsg::NodeReceiveMsgBytes
void NodeReceiveMsgBytes(CNode &node, Span< const uint8_t > msg_bytes, bool &complete) const
Definition: net.cpp:13
ConnmanTestMsg::AddTestNode
void AddTestNode(CNode &node)
Definition: net.h:27
ConnectionType::INBOUND
@ INBOUND
Inbound connections are those initiated by a peer.
NET_I2P
@ NET_I2P
I2P.
Definition: netaddress.h:59
CConnman::CConnman
CConnman(uint64_t seed0, uint64_t seed1, AddrMan &addrman, bool network_active=true)
Definition: net.cpp:2489
INVALID_SOCKET
#define INVALID_SOCKET
Definition: compat.h:53
NetPermissionFlags::Download
@ Download
net.h
compat.h
netaddress.h
CSerializedNetMsg
Definition: net.h:101
ALL_SERVICE_FLAGS
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
Definition: net.h:48
StaticContentsSock::Wait
bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
Wait for readiness for input (recv) or output (send).
Definition: net.h:153
NODE_BLOOM
@ NODE_BLOOM
Definition: protocol.h:281
NetPermissionFlags::ForceRelay
@ ForceRelay
ALL_CONNECTION_TYPES
constexpr ConnectionType ALL_CONNECTION_TYPES[]
Definition: net.h:70
ConnectionType::FEELER
@ FEELER
Feeler connections are short-lived connections made to check that a node is alive.
NetPermissionFlags::Relay
@ Relay
CConnman::m_peer_connect_timeout
std::chrono::seconds m_peer_connect_timeout
Definition: net.h:1095
NetPermissionFlags::All
@ All
NetEventsInterface::ProcessMessages
virtual bool ProcessMessages(CNode *pnode, std::atomic< bool > &interrupt)=0
Process protocol messages received from a given node.
NODE_NONE
@ NODE_NONE
Definition: protocol.h:274
CConnman::m_nodes_mutex
RecursiveMutex m_nodes_mutex
Definition: net.h:1114
NODE_WITNESS
@ NODE_WITNESS
Definition: protocol.h:284
CConnman
Definition: net.h:750
StaticContentsSock::Recv
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
Definition: net.h:118
NetPermissionFlags::Addr
@ Addr
NetPermissionFlags::Implicit
@ Implicit
GetRandomNodeEvictionCandidates
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:44
LOCK
#define LOCK(cs)
Definition: sync.h:226
ConnectionType::ADDR_FETCH
@ ADDR_FETCH
AddrFetch connections are short lived connections used to solicit addresses from peers.
NET_ONION
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
Sock::Event
uint8_t Event
Definition: sock.h:118
ConnmanTestMsg::ProcessMessagesOnce
void ProcessMessagesOnce(CNode &node)
Definition: net.h:41
StaticContentsSock::Reset
void Reset() override
Close if non-empty.
Definition: net.h:111
node
Definition: init.h:22
NET_IPV6
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
StaticContentsSock::~StaticContentsSock
~StaticContentsSock() override
Definition: net.h:103
ConnectionType::BLOCK_RELAY
@ BLOCK_RELAY
We use block-relay-only connections to help prevent against partition attacks.
NET_CJDNS
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:62
StaticContentsSock::StaticContentsSock
StaticContentsSock(const std::string &contents)
Definition: net.h:97
StaticContentsSock::m_contents
const std::string m_contents
Definition: net.h:164
CConnman::flagInterruptMsgProc
std::atomic< bool > flagInterruptMsgProc
Definition: net.h:1193
CConnman::m_msgproc
NetEventsInterface * m_msgproc
Definition: net.h:1175
StaticContentsSock::Accept
std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const override
accept(2) wrapper.
Definition: net.h:130
StaticContentsSock::operator=
StaticContentsSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
Definition: net.h:105
ConnectionType
ConnectionType
Different types of connections to a peer.
Definition: net.h:121
sock.h
NetPermissionFlags::Mempool
@ Mempool
StaticContentsSock::GetSockOpt
int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
getsockopt(2) wrapper.
Definition: net.h:147
FastRandomContext
Fast randomness source.
Definition: random.h:131
ALL_NETWORKS
constexpr auto ALL_NETWORKS
Definition: net.h:79