Monero
dns_utils.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #pragma once
29 
30 #include <vector>
31 #include <string>
32 #include <functional>
33 #include <boost/optional/optional_fwd.hpp>
34 #include <boost/utility/string_ref_fwd.hpp>
35 
36 namespace tools
37 {
38 
39 // RFC defines for record types and classes for DNS, gleaned from ldns source
40 constexpr const int DNS_CLASS_IN = 1;
41 constexpr const int DNS_TYPE_A = 1;
42 constexpr const int DNS_TYPE_TXT = 16;
43 constexpr const int DNS_TYPE_AAAA = 8;
44 constexpr const int DNS_TYPE_TLSA = 52;
45 
46 struct DNSResolverData;
47 
56 {
57 private:
58 
64  DNSResolver();
65 
66 public:
67 
71  ~DNSResolver();
72 
85  std::vector<std::string> get_ipv4(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
86 
97  std::vector<std::string> get_ipv6(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
98 
107  // TODO: modify this to accommodate DNSSEC
108  std::vector<std::string> get_txt_record(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
109 
119  std::vector<std::string> get_tlsa_tcp_record(boost::string_ref url, boost::string_ref port, bool& dnssec_available, bool& dnssec_valid);
120 
132 
138  static DNSResolver& instance();
139 
145  static DNSResolver create();
146 
147 private:
148 
159  // TODO: modify this to accommodate DNSSEC
160  std::vector<std::string> get_record(const std::string& url, int record_type, boost::optional<std::string> (*reader)(const char *,size_t), bool& dnssec_available, bool& dnssec_valid);
161 
163 }; // class DNSResolver
164 
165 namespace dns_utils
166 {
167 
169 std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid);
170 
171 std::string get_account_address_as_str_from_url(const std::string& url, bool& dnssec_valid, std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> confirm_dns);
172 
173 bool load_txt_records_from_dns(std::vector<std::string> &records, const std::vector<std::string> &dns_urls);
174 
175 std::vector<std::string> parse_dns_public(const char *s);
176 
177 } // namespace tools::dns_utils
178 
179 } // namespace tools
epee::net_utils::http::url_content url
Definition: parse_url.cpp:38
constexpr const int DNS_TYPE_AAAA
Definition: dns_utils.h:43
std::vector< std::string > get_ipv6(const std::string &url, bool &dnssec_available, bool &dnssec_valid)
gets ipv6 addresses from DNS query
Definition: dns_utils.cpp:367
constexpr const int DNS_TYPE_TLSA
Definition: dns_utils.h:44
::std::string string
Definition: gtest-port.h:1097
static DNSResolver & instance()
Gets the singleton instance of DNSResolver.
Definition: dns_utils.cpp:401
std::vector< std::string > get_ipv4(const std::string &url, bool &dnssec_available, bool &dnssec_valid)
gets ipv4 addresses from DNS query of a URL
Definition: dns_utils.cpp:362
const char * s
Definition: minissdp.c:596
~DNSResolver()
takes care of freeing C pointers and such
Definition: dns_utils.cpp:314
std::string get_account_address_as_str_from_url(const std::string &url, bool &dnssec_valid, std::function< std::string(const std::string &, const std::vector< std::string > &, bool)> dns_confirm)
Definition: dns_utils.cpp:487
std::vector< std::string > get_txt_record(const std::string &url, bool &dnssec_available, bool &dnssec_valid)
gets all TXT records from a DNS query for the supplied URL; if no TXT record present returns an empty...
Definition: dns_utils.cpp:372
std::string get_dns_format_from_oa_address(const std::string &oa_addr)
Gets a DNS address from OpenAlias format.
Definition: dns_utils.cpp:388
Definition: dns_utils.cpp:229
Various Tools.
Definition: apply_permutation.h:39
std::vector< std::string > addresses_from_url(const std::string &url, bool &dnssec_valid)
gets a monero address from the TXT record of a DNS entry
Definition: dns_utils.cpp:460
Provides high-level access to DNS resolution.
Definition: dns_utils.h:55
DNSResolverData * m_data
Definition: dns_utils.h:162
boost::endian::big_uint16_t port
Definition: socks.cpp:61
constexpr const int DNS_TYPE_A
Definition: dns_utils.h:41
std::vector< std::string > get_tlsa_tcp_record(boost::string_ref url, boost::string_ref port, bool &dnssec_available, bool &dnssec_valid)
gets all TLSA TCP records from a DNS query for the supplied URL; if no TLSA record present returns an...
Definition: dns_utils.cpp:377
std::vector< std::string > parse_dns_public(const char *s)
Definition: dns_utils.cpp:589
DNSResolver()
Constructs an instance of DNSResolver.
Definition: dns_utils.cpp:255
static DNSResolver create()
Gets a new instance of DNSResolver.
Definition: dns_utils.cpp:409
std::string address_from_txt_record(const std::string &s)
Definition: dns_utils.cpp:419
bool load_txt_records_from_dns(std::vector< std::string > &good_records, const std::vector< std::string > &dns_urls)
Definition: dns_utils.cpp:499
constexpr const int DNS_CLASS_IN
Definition: dns_utils.h:40
constexpr const int DNS_TYPE_TXT
Definition: dns_utils.h:42
std::vector< std::string > get_record(const std::string &url, int record_type, boost::optional< std::string >(*reader)(const char *, size_t), bool &dnssec_available, bool &dnssec_valid)
gets all records of a given type from a DNS query for the supplied URL; if no such record is present ...
Definition: dns_utils.cpp:326