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
18struct ConnmanTestMsg : public CConnman {
20
21 void SetPeerConnectTimeout(int64_t timeout)
22 {
23 m_peer_connect_timeout = timeout;
24 }
25
27 {
29 vNodes.push_back(&node);
30 }
32 {
34 for (CNode* node : vNodes) {
35 delete node;
36 }
37 vNodes.clear();
38 }
39
41
42 void NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const;
43
44 bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const;
45};
46
54};
55
67};
68
76};
77
78constexpr auto ALL_NETWORKS = std::array{
86};
87
94{
95public:
96 explicit StaticContentsSock(const std::string& contents) : m_contents{contents}, m_consumed{0}
97 {
98 // Just a dummy number that is not INVALID_SOCKET.
100 }
101
102 ~StaticContentsSock() override { Reset(); }
103
105 {
106 assert(false && "Move of Sock into MockSock not allowed.");
107 return *this;
108 }
109
110 void Reset() override
111 {
113 }
114
115 ssize_t Send(const void*, size_t len, int) const override { return len; }
116
117 ssize_t Recv(void* buf, size_t len, int flags) const override
118 {
119 const size_t consume_bytes{std::min(len, m_contents.size() - m_consumed)};
120 std::memcpy(buf, m_contents.data() + m_consumed, consume_bytes);
121 if ((flags & MSG_PEEK) == 0) {
122 m_consumed += consume_bytes;
123 }
124 return consume_bytes;
125 }
126
127 int Connect(const sockaddr*, socklen_t) const override { return 0; }
128
129 int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override
130 {
131 std::memset(opt_val, 0x0, *opt_len);
132 return 0;
133 }
134
135 bool Wait(std::chrono::milliseconds timeout,
136 Event requested,
137 Event* occurred = nullptr) const override
138 {
139 if (occurred != nullptr) {
140 *occurred = requested;
141 }
142 return true;
143 }
144
145private:
146 const std::string m_contents;
147 mutable size_t m_consumed;
148};
149
150std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
151
152#endif // BITCOIN_TEST_UTIL_NET_H
int flags
Definition: bitcoin-tx.cpp:525
Definition: net.h:741
RecursiveMutex cs_vNodes
Definition: net.h:1059
std::atomic< bool > flagInterruptMsgProc
Definition: net.h:1138
int64_t m_peer_connect_timeout
Definition: net.h:1040
NetEventsInterface * m_msgproc
Definition: net.h:1120
CConnman(uint64_t seed0, uint64_t seed1, AddrMan &addrman, bool network_active=true)
Definition: net.cpp:2468
Information about a peer.
Definition: net.h:394
Fast randomness source.
Definition: random.h:120
virtual bool ProcessMessages(CNode *pnode, std::atomic< bool > &interrupt)=0
Process protocol messages received from a given node.
RAII helper class that manages a socket.
Definition: sock.h:26
SOCKET m_socket
Contained socket.
Definition: sock.h:176
uint8_t Event
Definition: sock.h:109
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
A mocked Sock alternative that returns a statically contained data upon read and succeeds and ignores...
Definition: net.h:94
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:135
int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
getsockopt(2) wrapper.
Definition: net.h:129
void Reset() override
Close if non-empty.
Definition: net.h:110
int Connect(const sockaddr *, socklen_t) const override
connect(2) wrapper.
Definition: net.h:127
size_t m_consumed
Definition: net.h:147
const std::string m_contents
Definition: net.h:146
ssize_t Send(const void *, size_t len, int) const override
send(2) wrapper.
Definition: net.h:115
StaticContentsSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
Definition: net.h:104
~StaticContentsSock() override
Definition: net.h:102
StaticContentsSock(const std::string &contents)
Definition: net.h:96
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
Definition: net.h:117
#define INVALID_SOCKET
Definition: compat.h:53
ConnectionType
Different types of connections to a peer.
Definition: net.h:120
@ 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.
NetPermissionFlags
@ NET_I2P
I2P.
Definition: netaddress.h:59
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:62
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
ServiceFlags
nServices flags
Definition: protocol.h:271
@ NODE_NONE
Definition: protocol.h:274
@ NODE_WITNESS
Definition: protocol.h:284
@ NODE_NETWORK_LIMITED
Definition: protocol.h:291
@ NODE_BLOOM
Definition: protocol.h:281
@ NODE_NETWORK
Definition: protocol.h:277
@ NODE_COMPACT_FILTERS
Definition: protocol.h:287
void ProcessMessagesOnce(CNode &node)
Definition: net.h:40
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &ser_msg) const
Definition: net.cpp:33
void SetPeerConnectTimeout(int64_t timeout)
Definition: net.h:21
void ClearTestNodes()
Definition: net.h:31
void NodeReceiveMsgBytes(CNode &node, Span< const uint8_t > msg_bytes, bool &complete) const
Definition: net.cpp:13
void AddTestNode(CNode &node)
Definition: net.h:26
#define LOCK(cs)
Definition: sync.h:226
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:44
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
Definition: net.h:47
constexpr ConnectionType ALL_CONNECTION_TYPES[]
Definition: net.h:69
constexpr auto ALL_NETWORKS
Definition: net.h:78
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:56
assert(!tx.IsCoinBase())