36 static constexpr std::array<NetAux, 6> nets{
45 const size_t nets_index{rand ==
nullptr
46 ? fuzzed_data_provider.ConsumeIntegralInRange<
size_t>(0, nets.size() - 1)
47 :
static_cast<size_t>(rand->randrange(nets.size()))};
49 const auto& aux = nets[nets_index];
54 if (rand ==
nullptr) {
55 addr.
SetInternal(fuzzed_data_provider.ConsumeBytesAsString(32));
57 const auto v = rand->randbytes(32);
65 s << static_cast<uint8_t>(aux.bip155);
67 std::vector<uint8_t> addr_bytes;
68 if (rand ==
nullptr) {
69 addr_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(aux.len);
70 addr_bytes.resize(aux.len);
72 addr_bytes = rand->randbytes(aux.len);
95 constexpr std::array ADDR_ENCODINGS{
99 constexpr std::array ADDR_FORMATS{
103 if constexpr (std::is_same_v<P, CNetAddr::SerParams>) {
104 return P{
PickValue(fuzzed_data_provider, ADDR_ENCODINGS)};
106 if constexpr (std::is_same_v<P, CAddress::SerParams>) {
107 return P{{
PickValue(fuzzed_data_provider, ADDR_ENCODINGS)},
PickValue(fuzzed_data_provider, ADDR_FORMATS)};
115 m_fuzzed_data_provider{fuzzed_data_provider},
116 m_selectable{fuzzed_data_provider.ConsumeBool()}
131 assert(
false &&
"Move of Sock into FuzzedSock not allowed.");
137 constexpr std::array send_errnos{
172 constexpr std::array recv_errnos{
184 assert(buf !=
nullptr || len == 0);
188 std::this_thread::sleep_for(std::chrono::milliseconds{2});
199 size_t copied_so_far{0};
203 const size_t copy_len{std::min(len,
m_peek_data.size())};
205 copied_so_far += copy_len;
206 if ((
flags & MSG_PEEK) == 0) {
211 if (copied_so_far == len) {
212 return copied_so_far;
216 if (new_data.empty())
return copied_so_far;
218 std::memcpy(
reinterpret_cast<uint8_t*
>(buf) + copied_so_far, new_data.data(), new_data.size());
219 copied_so_far += new_data.size();
221 if ((
flags & MSG_PEEK) != 0) {
226 return copied_so_far;
230 std::memset(
reinterpret_cast<uint8_t*
>(buf) + copied_so_far, 0x0, len - copied_so_far);
240 constexpr std::array connect_errnos{
264 constexpr std::array bind_errnos{
284 constexpr std::array listen_errnos{
298 constexpr std::array accept_errnos{
305 return std::unique_ptr<FuzzedSock>();
312 constexpr std::array getsockopt_errnos{
320 if (opt_val ==
nullptr) {
331 constexpr std::array setsockopt_errnos{
344 constexpr std::array getsockname_errnos{
358 constexpr std::array setnonblocking_errnos{
376 constexpr std::array wait_errnos{
385 if (occurred !=
nullptr) {
396 for (
auto& [sock, events] : events_per_sock) {
411 errmsg =
"disconnected at random by the fuzzer";
417 auto successfully_connected = fuzzed_data_provider.ConsumeBool();
420 auto version = fuzzed_data_provider.ConsumeIntegralInRange<int32_t>(
MIN_PEER_PROTO_VERSION, std::numeric_limits<int32_t>::max());
421 auto relay_txs = fuzzed_data_provider.ConsumeBool();
422 connman.Handshake(
node, successfully_connected, remote_services, local_services, version, relay_txs);
A CService with information about it as peer.
static constexpr SerParams V2_NETWORK
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
BIP155Network
BIP155 network ids recognized by this software.
Information about a peer.
Double ended buffer combining vector and stream-like interfaces.
T ConsumeIntegralInRange(T min, T max)
size_t ConsumeData(void *destination, size_t num_bytes)
std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const override
accept(2) wrapper.
int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
getsockopt(2) wrapper.
bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
Wait for readiness for input (recv) or output (send).
int Listen(int backlog) const override
listen(2) wrapper.
const bool m_selectable
Whether to pretend that the socket is select(2)-able.
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const override
Same as Wait(), but wait on many sockets within the same timeout.
bool IsConnected(std::string &errmsg) const override
Check if still connected.
int Connect(const sockaddr *, socklen_t) const override
connect(2) wrapper.
ssize_t Send(const void *data, size_t len, int flags) const override
send(2) wrapper.
std::vector< uint8_t > m_peek_data
Data to return when MSG_PEEK is used as a Recv() flag.
FuzzedSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
FuzzedDataProvider & m_fuzzed_data_provider
bool IsSelectable() const override
Check if the underlying socket can be used for select(2) (or the Wait() method).
bool SetNonBlocking() const override
Set the non-blocking option on the socket.
int SetSockOpt(int level, int opt_name, const void *opt_val, socklen_t opt_len) const override
setsockopt(2) wrapper.
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
int GetSockName(sockaddr *name, socklen_t *name_len) const override
getsockname(2) wrapper.
FuzzedSock(FuzzedDataProvider &fuzzed_data_provider)
int Bind(const sockaddr *, socklen_t) const override
bind(2) wrapper.
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().
static constexpr uint8_t CJDNS_PREFIX
All CJDNS addresses start with 0xFC.
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
@ NET_ONION
TOR (v2 or v3)
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
static const int MIN_PEER_PROTO_VERSION
disconnect from peers older than this proto version
CAddress ConsumeAddress(FuzzedDataProvider &fuzzed_data_provider) noexcept
P ConsumeDeserializationParams(FuzzedDataProvider &fuzzed_data_provider) noexcept
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
std::vector< B > ConsumeFixedLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const size_t length) noexcept
Returns a byte vector of specified size regardless of the number of remaining bytes available from th...
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
void SetFuzzedErrNo(FuzzedDataProvider &fuzzed_data_provider, const std::array< T, size > &errnos)
Sets errno to a value selected from the given std::array errnos.
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds