#ifndef BITCOIN_TEST_UTIL_NET_H
#define BITCOIN_TEST_UTIL_NET_H
#include <compat/compat.h>
#include <netmessagemaker.h>
#include <net.h>
#include <net_permissions.h>
#include <net_processing.h>
#include <netaddress.h>
#include <node/connection_types.h>
#include <node/eviction.h>
#include <span.h>
#include <sync.h>
#include <util/sock.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <cstring>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
class FastRandomContext;
struct ConnmanTestMsg : public CConnman {
using CConnman::CConnman;
void SetMsgProc(NetEventsInterface* msgproc)
{
m_msgproc = msgproc;
}
void SetPeerConnectTimeout(std::chrono::seconds timeout)
{
m_peer_connect_timeout = timeout;
}
std::vector<CNode*> TestNodes()
{
LOCK(m_nodes_mutex);
return m_nodes;
}
void AddTestNode(CNode& node)
{
LOCK(m_nodes_mutex);
m_nodes.push_back(&node);
if (node.IsManualOrFullOutboundConn()) ++m_network_conn_counts[node.addr.GetNetwork()];
}
void ClearTestNodes()
{
LOCK(m_nodes_mutex);
for (CNode* node : m_nodes) {
delete node;
}
m_nodes.clear();
}
void Handshake(CNode& node,
bool successfully_connected,
ServiceFlags remote_services,
ServiceFlags local_services,
int32_t version,
bool relay_txs)
EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex);
bool ProcessMessagesOnce(CNode& node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex)
{
return m_msgproc->ProcessMessages(&node, flagInterruptMsgProc);
}
void NodeReceiveMsgBytes(CNode& node, std::span<const uint8_t> msg_bytes, bool& complete) const;
bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg&& ser_msg) const;
void FlushSendBuffer(CNode& node) const;
bool AlreadyConnectedPublic(const CAddress& addr) { return AlreadyConnectedToAddress(addr); };
CNode* ConnectNodePublic(PeerManager& peerman, const char* pszDest, ConnectionType conn_type)
EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
};
constexpr ServiceFlags ALL_SERVICE_FLAGS[]{
NODE_NONE,
NODE_NETWORK,
NODE_BLOOM,
NODE_WITNESS,
NODE_COMPACT_FILTERS,
NODE_NETWORK_LIMITED,
NODE_P2P_V2,
};
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]{
NetPermissionFlags::None,
NetPermissionFlags::BloomFilter,
NetPermissionFlags::Relay,
NetPermissionFlags::ForceRelay,
NetPermissionFlags::NoBan,
NetPermissionFlags::Mempool,
NetPermissionFlags::Addr,
NetPermissionFlags::Download,
NetPermissionFlags::Implicit,
NetPermissionFlags::All,
};
constexpr ConnectionType ALL_CONNECTION_TYPES[]{
ConnectionType::INBOUND,
ConnectionType::OUTBOUND_FULL_RELAY,
ConnectionType::MANUAL,
ConnectionType::FEELER,
ConnectionType::BLOCK_RELAY,
ConnectionType::ADDR_FETCH,
};
constexpr auto ALL_NETWORKS = std::array{
Network::NET_UNROUTABLE,
Network::NET_IPV4,
Network::NET_IPV6,
Network::NET_ONION,
Network::NET_I2P,
Network::NET_CJDNS,
Network::NET_INTERNAL,
};
class ZeroSock : public Sock
{
public:
ZeroSock();
~ZeroSock() override;
ssize_t Send(const void*, size_t len, int) const override;
ssize_t Recv(void* buf, size_t len, int flags) const override;
int Connect(const sockaddr*, socklen_t) const override;
int Bind(const sockaddr*, socklen_t) const override;
int Listen(int) const override;
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;
int SetSockOpt(int, int, const void*, socklen_t) const override;
int GetSockName(sockaddr* name, socklen_t* name_len) const override;
bool SetNonBlocking() const override;
bool IsSelectable() const override;
bool Wait(std::chrono::milliseconds timeout,
Event requested,
Event* occurred = nullptr) const override;
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override;
private:
ZeroSock& operator=(Sock&& other) override;
};
class StaticContentsSock : public ZeroSock
{
public:
explicit StaticContentsSock(const std::string& contents);
ssize_t Recv(void* buf, size_t len, int flags) const override;
bool IsConnected(std::string&) const override
{
return true;
}
private:
StaticContentsSock& operator=(Sock&& other) override;
const std::string m_contents;
mutable size_t m_consumed{0};
};
class DynSock : public ZeroSock
{
public:
class Pipe
{
public:
ssize_t GetBytes(void* buf, size_t len, int flags = 0) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
std::optional<CNetMessage> GetNetMsg() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
void PushBytes(const void* buf, size_t len) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
template <typename... Args>
void PushNetMsg(const std::string& type, Args&&... payload) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
void Eof() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
private:
void WaitForDataOrEof(UniqueLock<Mutex>& lock) EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
Mutex m_mutex;
std::condition_variable m_cond;
std::vector<uint8_t> m_data GUARDED_BY(m_mutex);
bool m_eof GUARDED_BY(m_mutex){false};
};
struct Pipes {
Pipe recv;
Pipe send;
};
class Queue
{
public:
using S = std::unique_ptr<DynSock>;
void Push(S s) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
{
LOCK(m_mutex);
m_queue.push(std::move(s));
}
std::optional<S> Pop() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
{
LOCK(m_mutex);
if (m_queue.empty()) {
return std::nullopt;
}
S front{std::move(m_queue.front())};
m_queue.pop();
return front;
}
bool Empty() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
{
LOCK(m_mutex);
return m_queue.empty();
}
private:
mutable Mutex m_mutex;
std::queue<S> m_queue GUARDED_BY(m_mutex);
};
explicit DynSock(std::shared_ptr<Pipes> pipes, std::shared_ptr<Queue> accept_sockets);
~DynSock();
ssize_t Recv(void* buf, size_t len, int flags) const override;
ssize_t Send(const void* buf, size_t len, int) const override;
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
bool Wait(std::chrono::milliseconds timeout,
Event requested,
Event* occurred = nullptr) const override;
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override;
private:
DynSock& operator=(Sock&&) override;
std::shared_ptr<Pipes> m_pipes;
std::shared_ptr<Queue> m_accept_sockets;
};
template <typename... Args>
void DynSock::Pipe::PushNetMsg(const std::string& type, Args&&... payload)
{
auto msg = NetMsg::Make(type, std::forward<Args>(payload)...);
V1Transport transport{NodeId{0}};
const bool queued{transport.SetMessageToSend(msg)};
assert(queued);
LOCK(m_mutex);
for (;;) {
const auto& [bytes, _more, _msg_type] = transport.GetBytesToSend(true);
if (bytes.empty()) {
break;
}
m_data.insert(m_data.end(), bytes.begin(), bytes.end());
transport.MarkBytesSent(bytes.size());
}
m_cond.notify_all();
}
std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
#endif