Bitcoin Core 28.99.0
P2P Digital Currency
netbase_dns_lookup.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-2022 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#include <netaddress.h>
6#include <netbase.h>
8#include <test/fuzz/fuzz.h>
10
11#include <cstdint>
12#include <string>
13#include <vector>
14
15FUZZ_TARGET(netbase_dns_lookup)
16{
17 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
18 const std::string name = fuzzed_data_provider.ConsumeRandomLengthString(512);
19 const unsigned int max_results = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
20 const bool allow_lookup = fuzzed_data_provider.ConsumeBool();
21 const uint16_t default_port = fuzzed_data_provider.ConsumeIntegral<uint16_t>();
22
23 auto fuzzed_dns_lookup_function = [&](const std::string&, bool) {
24 std::vector<CNetAddr> resolved_addresses;
25 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
26 resolved_addresses.push_back(ConsumeNetAddr(fuzzed_data_provider));
27 }
28 return resolved_addresses;
29 };
30
31 {
32 const std::vector<CNetAddr> resolved_addresses{LookupHost(name, max_results, allow_lookup, fuzzed_dns_lookup_function)};
33 for (const CNetAddr& resolved_address : resolved_addresses) {
34 assert(!resolved_address.IsInternal());
35 }
36 assert(resolved_addresses.size() <= max_results || max_results == 0);
37 }
38 {
39 const std::optional<CNetAddr> resolved_address{LookupHost(name, allow_lookup, fuzzed_dns_lookup_function)};
40 if (resolved_address.has_value()) {
41 assert(!resolved_address.value().IsInternal());
42 }
43 }
44 {
45 const std::vector<CService> resolved_services{Lookup(name, default_port, allow_lookup, max_results, fuzzed_dns_lookup_function)};
46 for (const CNetAddr& resolved_service : resolved_services) {
47 assert(!resolved_service.IsInternal());
48 }
49 assert(resolved_services.size() <= max_results || max_results == 0);
50 }
51 {
52 const std::optional<CService> resolved_service{Lookup(name, default_port, allow_lookup, fuzzed_dns_lookup_function)};
53 if (resolved_service.has_value()) {
54 assert(!resolved_service.value().IsInternal());
55 }
56 }
57 {
58 CService resolved_service = LookupNumeric(name, default_port, fuzzed_dns_lookup_function);
59 assert(!resolved_service.IsInternal());
60 }
61 {
62 (void)LookupSubNet(name);
63 }
64}
Network address.
Definition: netaddress.h:112
bool IsInternal() const
Definition: netaddress.cpp:476
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
CSubNet LookupSubNet(const std::string &subnet_str)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:756
std::vector< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:177
std::vector< CService > Lookup(const std::string &name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
Resolve a service string to its corresponding service.
Definition: netbase.cpp:195
CService LookupNumeric(const std::string &name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
Resolve a service string with a numeric IP to its first corresponding service.
Definition: netbase.cpp:220
FUZZ_TARGET(netbase_dns_lookup)
const char * name
Definition: rest.cpp:49
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
Definition: net.cpp:28
assert(!tx.IsCoinBase())