Electroneum
dns_utils.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, 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 
36 namespace tools
37 {
38 
39 // RFC defines for record types and classes for DNS, gleaned from ldns source
40 const static int DNS_CLASS_IN = 1;
41 const static int DNS_TYPE_A = 1;
42 const static int DNS_TYPE_TXT = 16;
43 const static int DNS_TYPE_AAAA = 8;
44 
45 struct DNSResolverData;
46 
55 {
56 private:
57 
63  DNSResolver();
64 
65 public:
66 
70  ~DNSResolver();
71 
84  std::vector<std::string> get_ipv4(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
85 
96  std::vector<std::string> get_ipv6(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
97 
106  // TODO: modify this to accommodate DNSSEC
107  std::vector<std::string> get_txt_record(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
108 
119  std::string get_dns_format_from_oa_address(const std::string& oa_addr);
120 
126  static DNSResolver& instance();
127 
133  static DNSResolver create();
134 
135 private:
136 
147  // TODO: modify this to accommodate DNSSEC
148  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);
149 
157  bool check_address_syntax(const char *addr) const;
158 
160 }; // class DNSResolver
161 
162 namespace dns_utils
163 {
164 
165 std::string address_from_txt_record(const std::string& s);
166 std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid);
167 
168 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);
169 
170 bool load_txt_records_from_dns(std::vector<std::string> &records, const std::vector<std::string> &dns_urls, std::string type);
171 
172 std::vector<std::string> parse_dns_public(const char *s);
173 
174 } // namespace tools::dns_utils
175 
176 } // 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:305
static DNSResolver & instance()
Gets the singleton instance of DNSResolver.
Definition: dns_utils.cpp:328
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:300
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:269
static const int DNS_TYPE_A
Definition: dns_utils.h:41
bool check_address_syntax(const char *addr) const
Checks a string to see if it looks like a URL.
Definition: dns_utils.cpp:341
~DNSResolver()
takes care of freeing C pointers and such
Definition: dns_utils.cpp:257
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:424
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:310
std::string get_dns_format_from_oa_address(const std::string &oa_addr)
Gets a DNS address from OpenAlias format.
Definition: dns_utils.cpp:315
Definition: dns_utils.cpp:199
Various Tools.
Definition: base58.cpp:43
static const int DNS_TYPE_AAAA
Definition: dns_utils.h:43
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:397
Provides high-level access to DNS resolution.
Definition: dns_utils.h:54
type
Definition: json.h:74
static const int DNS_TYPE_TXT
Definition: dns_utils.h:42
DNSResolverData * m_data
Definition: dns_utils.h:159
std::vector< std::string > parse_dns_public(const char *s)
Definition: dns_utils.cpp:549
DNSResolver()
Constructs an instance of DNSResolver.
Definition: dns_utils.cpp:215
static DNSResolver create()
Gets a new instance of DNSResolver.
Definition: dns_utils.cpp:336
std::string address_from_txt_record(const std::string &s)
Definition: dns_utils.cpp:356
static const int DNS_CLASS_IN
Definition: dns_utils.h:40
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:460
#define s(x, c)
Definition: aesb.c:46