Bitcoin Core  22.99.0
P2P Digital Currency
net.h
Go to the documentation of this file.
1 // Copyright (c) 2020 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 <string>
17 
18 struct ConnmanTestMsg : public CConnman {
19  using CConnman::CConnman;
21  {
22  LOCK(cs_vNodes);
23  vNodes.push_back(&node);
24  }
26  {
27  LOCK(cs_vNodes);
28  for (CNode* node : vNodes) {
29  delete node;
30  }
31  vNodes.clear();
32  }
33 
35 
36  void NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const;
37 
38  bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const;
39 };
40 
42  NODE_NONE,
44  NODE_BLOOM,
48 };
49 
61 };
62 
70 };
71 
72 constexpr auto ALL_NETWORKS = std::array{
80 };
81 
87 class StaticContentsSock : public Sock
88 {
89 public:
90  explicit StaticContentsSock(const std::string& contents) : m_contents{contents}, m_consumed{0}
91  {
92  // Just a dummy number that is not INVALID_SOCKET.
94  }
95 
96  ~StaticContentsSock() override { Reset(); }
97 
98  StaticContentsSock& operator=(Sock&& other) override
99  {
100  assert(false && "Move of Sock into MockSock not allowed.");
101  return *this;
102  }
103 
104  void Reset() override
105  {
107  }
108 
109  ssize_t Send(const void*, size_t len, int) const override { return len; }
110 
111  ssize_t Recv(void* buf, size_t len, int flags) const override
112  {
113  const size_t consume_bytes{std::min(len, m_contents.size() - m_consumed)};
114  std::memcpy(buf, m_contents.data() + m_consumed, consume_bytes);
115  if ((flags & MSG_PEEK) == 0) {
116  m_consumed += consume_bytes;
117  }
118  return consume_bytes;
119  }
120 
121  int Connect(const sockaddr*, socklen_t) const override { return 0; }
122 
123  int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override
124  {
125  std::memset(opt_val, 0x0, *opt_len);
126  return 0;
127  }
128 
129  bool Wait(std::chrono::milliseconds timeout,
130  Event requested,
131  Event* occurred = nullptr) const override
132  {
133  if (occurred != nullptr) {
134  *occurred = requested;
135  }
136  return true;
137  }
138 
139 private:
140  const std::string m_contents;
141  mutable size_t m_consumed;
142 };
143 
144 std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
145 
146 #endif // BITCOIN_TEST_UTIL_NET_H
ConnmanTestMsg::ClearTestNodes
void ClearTestNodes()
Definition: net.h:25
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:176
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:87
flags
int flags
Definition: bitcoin-tx.cpp:513
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:121
NetPermissionFlags::NoBan
@ NoBan
StaticContentsSock::m_consumed
size_t m_consumed
Definition: net.h:141
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:393
ALL_NET_PERMISSION_FLAGS
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:50
CConnman::CConnman
CConnman(uint64_t seed0, uint64_t seed1, CAddrMan &addrman, bool network_active=true)
Definition: net.cpp:2440
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:109
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:25
ConnmanTestMsg
Definition: net.h:18
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
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:20
ConnectionType::INBOUND
@ INBOUND
Inbound connections are those initiated by a peer.
NET_I2P
@ NET_I2P
I2P.
Definition: netaddress.h:59
INVALID_SOCKET
#define INVALID_SOCKET
Definition: compat.h:53
NetPermissionFlags::Download
@ Download
net.h
compat.h
netaddress.h
CSerializedNetMsg
Definition: net.h:100
ALL_SERVICE_FLAGS
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
Definition: net.h:41
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:129
NODE_BLOOM
@ NODE_BLOOM
Definition: protocol.h:281
NetPermissionFlags::ForceRelay
@ ForceRelay
ALL_CONNECTION_TYPES
constexpr ConnectionType ALL_CONNECTION_TYPES[]
Definition: net.h:63
ConnectionType::FEELER
@ FEELER
Feeler connections are short-lived connections made to check that a node is alive.
NetPermissionFlags::Relay
@ Relay
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
NODE_WITNESS
@ NODE_WITNESS
Definition: protocol.h:284
CConnman
Definition: net.h:740
StaticContentsSock::Recv
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
Definition: net.h:111
NetPermissionFlags::Addr
@ Addr
NetPermissionFlags::Implicit
@ Implicit
GetRandomNodeEvictionCandidates
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:44
CConnman::cs_vNodes
RecursiveMutex cs_vNodes
Definition: net.h:1059
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:109
ConnmanTestMsg::ProcessMessagesOnce
void ProcessMessagesOnce(CNode &node)
Definition: net.h:34
StaticContentsSock::Reset
void Reset() override
Close if non-empty.
Definition: net.h:104
node
Definition: interfaces.cpp:68
NET_IPV6
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
StaticContentsSock::~StaticContentsSock
~StaticContentsSock() override
Definition: net.h:96
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:90
StaticContentsSock::m_contents
const std::string m_contents
Definition: net.h:140
CConnman::flagInterruptMsgProc
std::atomic< bool > flagInterruptMsgProc
Definition: net.h:1138
CConnman::m_msgproc
NetEventsInterface * m_msgproc
Definition: net.h:1120
StaticContentsSock::operator=
StaticContentsSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
Definition: net.h:98
ConnectionType
ConnectionType
Different types of connections to a peer.
Definition: net.h:120
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:123
FastRandomContext
Fast randomness source.
Definition: random.h:119
ALL_NETWORKS
constexpr auto ALL_NETWORKS
Definition: net.h:72