5#ifndef BITCOIN_TEST_UTIL_NET_H
6#define BITCOIN_TEST_UTIL_NET_H
26#include <unordered_map>
56 m_nodes.push_back(&
node);
58 if (
node.IsManualOrFullOutboundConn()) ++m_network_conn_counts[
node.addr.GetNetwork()];
71 bool successfully_connected,
154 assert(
false &&
"Move of Sock into MockSock not allowed.");
158 ssize_t
Send(
const void*,
size_t len,
int)
const override {
return len; }
160 ssize_t
Recv(
void* buf,
size_t len,
int flags)
const override
164 if ((
flags & MSG_PEEK) == 0) {
167 return consume_bytes;
170 int Connect(
const sockaddr*, socklen_t)
const override {
return 0; }
172 int Bind(
const sockaddr*, socklen_t)
const override {
return 0; }
174 int Listen(
int)
const override {
return 0; }
176 std::unique_ptr<Sock>
Accept(sockaddr* addr, socklen_t* addr_len)
const override
178 if (addr !=
nullptr) {
180 memset(addr, 0x00, *addr_len);
181 const socklen_t write_len =
static_cast<socklen_t
>(
sizeof(sockaddr_in));
182 if (*addr_len >= write_len) {
183 *addr_len = write_len;
184 sockaddr_in* addr_in =
reinterpret_cast<sockaddr_in*
>(addr);
185 addr_in->sin_family = AF_INET;
186 memset(&addr_in->sin_addr, 0x05,
sizeof(addr_in->sin_addr));
187 addr_in->sin_port = htons(6789);
190 return std::make_unique<StaticContentsSock>(
"");
193 int GetSockOpt(
int level,
int opt_name,
void* opt_val, socklen_t* opt_len)
const override
195 std::memset(opt_val, 0x0, *opt_len);
199 int SetSockOpt(
int,
int,
const void*, socklen_t)
const override {
return 0; }
203 std::memset(
name, 0x0, *name_len);
211 bool Wait(std::chrono::milliseconds timeout,
213 Event* occurred =
nullptr)
const override
215 if (occurred !=
nullptr) {
216 *occurred = requested;
223 for (
auto& [sock, events] : events_per_sock) {
225 events.occurred = events.requested;
A CService with information about it as peer.
bool AlreadyConnectedToAddress(const CAddress &addr)
Determine whether we're already connected to a given address, in order to avoid initiating duplicate ...
CConnman(uint64_t seed0, uint64_t seed1, AddrMan &addrman, const NetGroupManager &netgroupman, const CChainParams ¶ms, bool network_active=true)
std::atomic< bool > flagInterruptMsgProc
RecursiveMutex m_nodes_mutex
Mutex m_unused_i2p_sessions_mutex
Mutex protecting m_i2p_sam_sessions.
Information about a peer.
Interface for message handling.
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
SOCKET m_socket
Contained socket.
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
A Span is an object that can refer to a contiguous sequence of objects.
A mocked Sock alternative that returns a statically contained data upon read and succeeds and ignores...
int SetSockOpt(int, int, const void *, socklen_t) const override
setsockopt(2) wrapper.
bool IsSelectable() const override
Check if the underlying socket can be used for select(2) (or the Wait() method).
bool IsConnected(std::string &) const override
Check if still connected.
bool SetNonBlocking() const override
Set the non-blocking option on the socket.
bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
Wait for readiness for input (recv) or output (send).
int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
getsockopt(2) wrapper.
int Connect(const sockaddr *, socklen_t) const override
connect(2) wrapper.
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const override
Same as Wait(), but wait on many sockets within the same timeout.
const std::string m_contents
ssize_t Send(const void *, size_t len, int) const override
send(2) wrapper.
std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const override
accept(2) wrapper.
int Bind(const sockaddr *, socklen_t) const override
bind(2) wrapper.
StaticContentsSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
int GetSockName(sockaddr *name, socklen_t *name_len) const override
getsockname(2) wrapper.
int Listen(int) const override
listen(2) wrapper.
~StaticContentsSock() override
StaticContentsSock(const std::string &contents)
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
ConnectionType
Different types of connections to a peer.
@ 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.
@ NET_ONION
TOR (v2 or v3)
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
ServiceFlags
nServices flags
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &&ser_msg) const
CNode * ConnectNodePublic(PeerManager &peerman, const char *pszDest, ConnectionType conn_type) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex)
bool AlreadyConnectedPublic(const CAddress &addr)
void NodeReceiveMsgBytes(CNode &node, Span< const uint8_t > msg_bytes, bool &complete) const
void SetMsgProc(NetEventsInterface *msgproc)
void AddTestNode(CNode &node)
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
std::vector< CNode * > TestNodes()
void FlushSendBuffer(CNode &node) const
void SetPeerConnectTimeout(std::chrono::seconds timeout)
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
constexpr ConnectionType ALL_CONNECTION_TYPES[]
constexpr auto ALL_NETWORKS
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
#define EXCLUSIVE_LOCKS_REQUIRED(...)