Monero
dns_utils.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, 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 
34 namespace tools
35 {
36 
37 // RFC defines for record types and classes for DNS, gleaned from ldns source
38 const static int DNS_CLASS_IN = 1;
39 const static int DNS_TYPE_A = 1;
40 const static int DNS_TYPE_TXT = 16;
41 const static int DNS_TYPE_AAAA = 8;
42 
43 struct DNSResolverData;
44 
53 {
54 private:
55 
61  DNSResolver();
62 
63 public:
64 
68  ~DNSResolver();
69 
82  std::vector<std::string> get_ipv4(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
83 
94  std::vector<std::string> get_ipv6(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
95 
104  // TODO: modify this to accommodate DNSSEC
105  std::vector<std::string> get_txt_record(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
106 
117  std::string get_dns_format_from_oa_address(const std::string& oa_addr);
118 
124  static DNSResolver& instance();
125 
131  static DNSResolver create();
132 
133 private:
134 
145  // TODO: modify this to accommodate DNSSEC
146  std::vector<std::string> get_record(const std::string& url, int record_type, std::string (*reader)(const char *,size_t), bool& dnssec_available, bool& dnssec_valid);
147 
155  bool check_address_syntax(const char *addr) const;
156 
158 }; // class DNSResolver
159 
160 namespace dns_utils
161 {
162 
163 std::string address_from_txt_record(const std::string& s);
164 std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid);
165 
166 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);
167 
168 bool load_txt_records_from_dns(std::vector<std::string> &records, const std::vector<std::string> &dns_urls);
169 
170 std::vector<std::string> parse_dns_public(const char *s);
171 
172 } // namespace tools::dns_utils
173 
174 } // namespace tools
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:304
static DNSResolver & instance()
Gets the singleton instance of DNSResolver.
Definition: dns_utils.cpp:327
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:299
std::vector< std::string > get_record(const std::string &url, int record_type, 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:268
static const int DNS_TYPE_A
Definition: dns_utils.h:39
bool check_address_syntax(const char *addr) const
Checks a string to see if it looks like a URL.
Definition: dns_utils.cpp:340
~DNSResolver()
takes care of freeing C pointers and such
Definition: dns_utils.cpp:256
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:423
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:309
std::string get_dns_format_from_oa_address(const std::string &oa_addr)
Gets a DNS address from OpenAlias format.
Definition: dns_utils.cpp:314
Definition: dns_utils.cpp:198
Various Tools.
Definition: apply_permutation.h:39
static const int DNS_TYPE_AAAA
Definition: dns_utils.h:41
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:396
Provides high-level access to DNS resolution.
Definition: dns_utils.h:52
static const int DNS_TYPE_TXT
Definition: dns_utils.h:40
DNSResolverData * m_data
Definition: dns_utils.h:157
std::vector< std::string > parse_dns_public(const char *s)
Definition: dns_utils.cpp:548
DNSResolver()
Constructs an instance of DNSResolver.
Definition: dns_utils.cpp:214
static DNSResolver create()
Gets a new instance of DNSResolver.
Definition: dns_utils.cpp:335
std::string address_from_txt_record(const std::string &s)
Definition: dns_utils.cpp:355
static const int DNS_CLASS_IN
Definition: dns_utils.h:38
bool load_txt_records_from_dns(std::vector< std::string > &good_records, const std::vector< std::string > &dns_urls)
Definition: dns_utils.cpp:459
#define s(x, c)
Definition: aesb.c:46