Monero
dns_utils.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2020, 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 
35 namespace tools
36 {
37 
38 // RFC defines for record types and classes for DNS, gleaned from ldns source
39 const static int DNS_CLASS_IN = 1;
40 const static int DNS_TYPE_A = 1;
41 const static int DNS_TYPE_TXT = 16;
42 const static int DNS_TYPE_AAAA = 8;
43 
44 struct DNSResolverData;
45 
54 {
55 private:
56 
62  DNSResolver();
63 
64 public:
65 
69  ~DNSResolver();
70 
83  std::vector<std::string> get_ipv4(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
84 
95  std::vector<std::string> get_ipv6(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
96 
105  // TODO: modify this to accommodate DNSSEC
106  std::vector<std::string> get_txt_record(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
107 
118  std::string get_dns_format_from_oa_address(const std::string& oa_addr);
119 
125  static DNSResolver& instance();
126 
132  static DNSResolver create();
133 
134 private:
135 
146  // TODO: modify this to accommodate DNSSEC
147  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);
148 
156  bool check_address_syntax(const char *addr) const;
157 
159 }; // class DNSResolver
160 
161 namespace dns_utils
162 {
163 
164 std::string address_from_txt_record(const std::string& s);
165 std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid);
166 
167 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);
168 
169 bool load_txt_records_from_dns(std::vector<std::string> &records, const std::vector<std::string> &dns_urls);
170 
171 std::vector<std::string> parse_dns_public(const char *s);
172 
173 } // namespace tools::dns_utils
174 
175 } // namespace tools
#define s(x, c)
Definition: aesb.c:47
Provides high-level access to DNS resolution.
Definition: dns_utils.h:54
DNSResolver()
Constructs an instance of DNSResolver.
Definition: dns_utils.cpp:244
static DNSResolver & instance()
Gets the singleton instance of DNSResolver.
Definition: dns_utils.cpp:379
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:361
std::string get_dns_format_from_oa_address(const std::string &oa_addr)
Gets a DNS address from OpenAlias format.
Definition: dns_utils.cpp:366
DNSResolverData * m_data
Definition: dns_utils.h:158
~DNSResolver()
takes care of freeing C pointers and such
Definition: dns_utils.cpp:303
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:351
bool check_address_syntax(const char *addr) const
Checks a string to see if it looks like a URL.
Definition: dns_utils.cpp:392
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:315
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:356
static DNSResolver create()
Gets a new instance of DNSResolver.
Definition: dns_utils.cpp:387
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:448
bool load_txt_records_from_dns(std::vector< std::string > &good_records, const std::vector< std::string > &dns_urls)
Definition: dns_utils.cpp:511
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:475
std::string address_from_txt_record(const std::string &s)
Definition: dns_utils.cpp:407
std::vector< std::string > parse_dns_public(const char *s)
Definition: dns_utils.cpp:597
Various Tools.
Definition: apply_permutation.h:40
static const int DNS_TYPE_TXT
Definition: dns_utils.h:41
static const int DNS_TYPE_A
Definition: dns_utils.h:40
static const int DNS_CLASS_IN
Definition: dns_utils.h:39
static const int DNS_TYPE_AAAA
Definition: dns_utils.h:42
Definition: dns_utils.cpp:219