Electroneum
dns_utils.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 
23 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #pragma once
31 
32 #include <vector>
33 #include <string>
34 #include <functional>
35 #include <boost/optional/optional_fwd.hpp>
36 
37 namespace tools
38 {
39 
40 // RFC defines for record types and classes for DNS, gleaned from ldns source
41 const static int DNS_CLASS_IN = 1;
42 const static int DNS_TYPE_A = 1;
43 const static int DNS_TYPE_TXT = 16;
44 const static int DNS_TYPE_AAAA = 8;
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 
121 
127  static DNSResolver& instance();
128 
134  static DNSResolver create();
135 
136 private:
137 
148  // TODO: modify this to accommodate DNSSEC
149  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);
150 
158  bool check_address_syntax(const char *addr) const;
159 
160  DNSResolverData *m_data;
161 }; // class DNSResolver
162 
163 namespace dns_utils
164 {
165 
167 std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid);
168 
169 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);
170 
171 bool load_txt_records_from_dns(std::vector<std::string> &records, const std::vector<std::string> &dns_urls, std::string type);
172 
173 std::vector<std::string> parse_dns_public(const char *s);
174 
175 std::vector<std::string> parse_dns_public(const char *s);
176 
177 } // namespace tools::dns_utils
178 
179 } // namespace tools
::std::string string
Definition: gtest-port.h:1097
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:355
static DNSResolver create()
Gets a new instance of DNSResolver.
Definition: dns_utils.cpp:391
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:479
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:360
Various Tools.
Definition: tools.cpp:31
std::vector< std::string > addresses_from_url(const std::string &url, bool &dnssec_valid)
gets a electroneum address from the TXT record of a DNS entry
Definition: dns_utils.cpp:452
std::string get_dns_format_from_oa_address(const std::string &oa_addr)
Gets a DNS address from OpenAlias format.
Definition: dns_utils.cpp:370
Provides high-level access to DNS resolution.
Definition: dns_utils.h:55
static DNSResolver & instance()
Gets the singleton instance of DNSResolver.
Definition: dns_utils.cpp:383
std::vector< std::string > parse_dns_public(const char *s)
Definition: dns_utils.cpp:601
std::string address_from_txt_record(const std::string &s)
Definition: dns_utils.cpp:411
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:365
bool load_txt_records_from_dns(std::vector< std::string > &good_records, const std::vector< std::string > &dns_urls, std::string type)
Definition: dns_utils.cpp:515
~DNSResolver()
takes care of freeing C pointers and such
Definition: dns_utils.cpp:307