Bitcoin Core  24.99.0
P2P Digital Currency
netbase_dns_lookup.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 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>
9 #include <test/fuzz/util.h>
10 
11 #include <cstdint>
12 #include <string>
13 #include <vector>
14 
15 FUZZ_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  std::vector<CNetAddr> resolved_addresses;
33  if (LookupHost(name, resolved_addresses, max_results, allow_lookup, fuzzed_dns_lookup_function)) {
34  for (const CNetAddr& resolved_address : resolved_addresses) {
35  assert(!resolved_address.IsInternal());
36  }
37  }
38  assert(resolved_addresses.size() <= max_results || max_results == 0);
39  }
40  {
41  CNetAddr resolved_address;
42  if (LookupHost(name, resolved_address, allow_lookup, fuzzed_dns_lookup_function)) {
43  assert(!resolved_address.IsInternal());
44  }
45  }
46  {
47  std::vector<CService> resolved_services;
48  if (Lookup(name, resolved_services, default_port, allow_lookup, max_results, fuzzed_dns_lookup_function)) {
49  for (const CNetAddr& resolved_service : resolved_services) {
50  assert(!resolved_service.IsInternal());
51  }
52  }
53  assert(resolved_services.size() <= max_results || max_results == 0);
54  }
55  {
56  CService resolved_service;
57  if (Lookup(name, resolved_service, default_port, allow_lookup, fuzzed_dns_lookup_function)) {
58  assert(!resolved_service.IsInternal());
59  }
60  }
61  {
62  CService resolved_service = LookupNumeric(name, default_port, fuzzed_dns_lookup_function);
63  assert(!resolved_service.IsInternal());
64  }
65  {
66  CSubNet resolved_subnet;
67  if (LookupSubNet(name, resolved_subnet)) {
68  assert(resolved_subnet.IsValid());
69  }
70  }
71 }
Network address.
Definition: netaddress.h:118
bool IsInternal() const
Definition: netaddress.cpp:494
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:521
bool IsValid() const
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
bool LookupSubNet(const std::string &subnet_str, CSubNet &subnet_out)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:685
bool Lookup(const std::string &name, std::vector< CService > &vAddr, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
Resolve a service string to its corresponding service.
Definition: netbase.cpp:198
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:230
bool LookupHost(const std::string &name, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:170
FUZZ_TARGET(netbase_dns_lookup)
const char * name
Definition: rest.cpp:46
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:507
assert(!tx.IsCoinBase())