5 #ifndef BITCOIN_TEST_UTIL_NET_H 6 #define BITCOIN_TEST_UTIL_NET_H 24 #include <condition_variable> 30 #include <unordered_map> 64 m_nodes.push_back(&
node);
66 if (
node.IsManualOrFullOutboundConn()) ++m_network_conn_counts[
node.addr.GetNetwork()];
97 bool successfully_connected,
174 ssize_t
Send(
const void*,
size_t len,
int)
const override;
176 ssize_t
Recv(
void* buf,
size_t len,
int flags)
const override;
178 int Connect(
const sockaddr*, socklen_t)
const override;
180 int Bind(
const sockaddr*, socklen_t)
const override;
182 int Listen(
int)
const override;
184 std::unique_ptr<Sock>
Accept(sockaddr* addr, socklen_t* addr_len)
const override;
186 int GetSockOpt(
int level,
int opt_name,
void* opt_val, socklen_t* opt_len)
const override;
188 int SetSockOpt(
int,
int,
const void*, socklen_t)
const override;
196 bool Wait(std::chrono::milliseconds timeout,
198 Event* occurred =
nullptr)
const override;
220 ssize_t
Recv(
void* buf,
size_t len,
int flags)
const override;
272 template <
typename... Args>
304 using S = std::unique_ptr<DynSock>;
309 m_queue.push(std::move(
s));
315 if (m_queue.empty()) {
318 S front{std::move(m_queue.front())};
326 return m_queue.empty();
339 explicit DynSock(std::shared_ptr<Pipes> pipes, std::shared_ptr<Queue> accept_sockets);
343 ssize_t
Recv(
void* buf,
size_t len,
int flags)
const override;
345 ssize_t
Send(
const void* buf,
size_t len,
int)
const override;
347 std::unique_ptr<Sock>
Accept(sockaddr* addr, socklen_t* addr_len)
const override;
349 bool Wait(std::chrono::milliseconds timeout,
351 Event* occurred =
nullptr)
const override;
362 template <
typename... Args>
374 const auto& [bytes, _more, _msg_type] = transport.GetBytesToSend(
true);
378 m_data.insert(m_data.end(), bytes.begin(), bytes.end());
379 transport.MarkBytesSent(bytes.size());
387 #endif // BITCOIN_TEST_UTIL_NET_H static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
void SocketHandlerPublic()
std::atomic< bool > flagInterruptMsgProc
void SetPeerConnectTimeout(std::chrono::seconds timeout)
void Push(S s) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
AddrFetch connections are short lived connections used to solicit addresses from peers.
std::reference_wrapper< AddrMan > addrman
bool SetNonBlocking() const override
Set the non-blocking option on the socket.
A mocked Sock alternative that returns a statically contained data upon read and succeeds and ignores...
ServiceFlags
nServices flags
Inbound connections are those initiated by a peer.
A set of addresses that represent the hash of a string or FQDN.
void SocketHandler() EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex
Check connected and listening sockets for IO readiness and process them accordingly.
std::condition_variable m_cond
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.
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
bool SetMessageToSend(CSerializedNetMsg &msg) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Set the next message to send.
ssize_t Send(const void *buf, size_t len, int) const override
send(2) wrapper.
A mocked Sock alternative that allows providing the data to be returned by Recv() and inspecting the ...
bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
Wait for readiness for input (recv) or output (send).
std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const override
accept(2) wrapper.
void FlushSendBuffer(CNode &node) const
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Unidirectional bytes or CNetMessage queue (FIFO).
Interface for message handling.
StaticContentsSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
std::vector< CNode * > TestNodes()
CConnman(uint64_t seed0, uint64_t seed1, AddrMan &addrman, const NetGroupManager &netgroupman, const CChainParams ¶ms, bool network_active=true, std::shared_ptr< CThreadInterrupt > interrupt_net=std::make_shared< CThreadInterrupt >())
These are the default connections that we use to connect with the network.
std::optional< S > Pop() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
std::shared_ptr< Pipes > m_pipes
void WaitForDataOrEof(UniqueLock< Mutex > &lock) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Return when there is some data to read or EOF has been signaled.
CSerializedNetMsg Make(std::string msg_type, Args &&... args)
Stochastic address manager.
std::vector< uint8_t > m_data GUARDED_BY(m_mutex)
Mutex m_unused_i2p_sessions_mutex
Mutex protecting m_i2p_sam_sessions.
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const override
Same as Wait(), but wait on many sockets within the same timeout.
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
constexpr ConnectionType ALL_CONNECTION_TYPES[]
void ResetMaxOutboundCycle()
std::optional< CNetMessage > GetNetMsg() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Deserialize a CNetMessage and remove it from the pipe.
void Eof() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Signal end-of-file on the receiving end (GetBytes() or GetNetMsg()).
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
int Listen(int) const override
listen(2) wrapper.
We open manual connections to addresses that users explicitly requested via the addnode RPC or the -a...
bool IsSelectable() const override
Check if the underlying socket can be used for select(2) (or the Wait() method).
A CService with information about it as peer.
std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const override
accept(2) wrapper.
int SetSockOpt(int, int, const void *, socklen_t) const override
setsockopt(2) wrapper.
bool AlreadyConnectedToAddressPublic(const CNetAddr &addr)
std::queue< S > m_queue GUARDED_BY(m_mutex)
A mocked Sock alternative that succeeds on all operations.
ZeroSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
bool InitBinds(const Options &options)
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...
int Bind(const sockaddr *, socklen_t) const override
bind(2) wrapper.
bool IsConnected(std::string &) const override
Check if still connected.
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &&ser_msg) const
StaticContentsSock(const std::string &contents)
bool m_eof GUARDED_BY(m_mutex)
std::unique_ptr< DynSock > S
RecursiveMutex m_nodes_mutex
const std::string m_contents
bool Empty() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
ssize_t GetBytes(void *buf, size_t len, int flags=0) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get bytes and remove them from the pipe.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
getsockopt(2) wrapper.
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
DynSock(std::shared_ptr< Pipes > pipes, std::shared_ptr< Queue > accept_sockets)
Create a new mocked sock.
void PushBytes(const void *buf, size_t len) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Push bytes to the pipe.
bool AlreadyConnectedToAddress(const CNetAddr &addr) const
Determine whether we're already connected to a given address.
void NodeReceiveMsgBytes(CNode &node, std::span< const uint8_t > msg_bytes, bool &complete) const
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 ...
void SetAddrman(AddrMan &in)
constexpr auto ALL_NETWORKS
ConnectionType
Different types of connections to a peer.
Wrapper around std::unique_lock style lock for MutexType.
void SetMsgProc(NetEventsInterface *msgproc)
RAII helper class that manages a socket and closes it automatically when it goes out of scope...
bool InitBindsPublic(const CConnman::Options &options)
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.
Information about a peer.
int GetSockName(sockaddr *name, socklen_t *name_len) const override
getsockname(2) wrapper.
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
int Connect(const sockaddr *, socklen_t) const override
connect(2) wrapper.
void CreateNodeFromAcceptedSocketPublic(std::unique_ptr< Sock > sock, NetPermissionFlags permissions, const CAddress &addr_bind, const CAddress &addr_peer)
void PushNetMsg(const std::string &type, Args &&... payload) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Construct and push CNetMessage to the pipe.
CNode * ConnectNodePublic(PeerManager &peerman, const char *pszDest, ConnectionType conn_type) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex)
void AddTestNode(CNode &node)
void Reset()
Reset the internal state.
std::shared_ptr< Queue > m_accept_sockets
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().
DynSock & operator=(Sock &&) override
Move assignment operator, grab the socket from another object and close ours (if set).
bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
Wait for readiness for input (recv) or output (send).
Addresses from these networks are not publicly routable on the global Internet.