Debian Codegraph @

src/common/dns_utils.h

raw symbols SCIP
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
48 /**
49 * @brief Provides high-level access to DNS resolution
50 *
51 * This class is designed to provide a high-level abstraction to DNS resolution
52 * functionality, including access to TXT records and such. It will also
53 * handle DNSSEC validation of the results.
54 */
55 class DNSResolver
56 {
57 private:
58
59 /**
60 * @brief Constructs an instance of DNSResolver
61 *
62 * Constructs a class instance and does setup stuff for the backend resolver.
63 */
64 DNSResolver();
65
66 public:
67
68 /**
69 * @brief takes care of freeing C pointers and such
70 */
71 ~DNSResolver();
72
73 /**
74 * @brief gets ipv4 addresses from DNS query of a URL
75 *
76 * returns a vector of all IPv4 "A" records for given URL.
77 * If no "A" records found, returns an empty vector.
78 *
79 * @param url A string containing a URL to query for
80 *
81 * @param dnssec_available
82 *
83 * @return vector of strings containing ipv4 addresses
84 */
85 std::vector<std::string> get_ipv4(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
86
87 /**
88 * @brief gets ipv6 addresses from DNS query
89 *
90 * returns a vector of all IPv6 "A" records for given URL.
91 * If no "A" records found, returns an empty vector.
92 *
93 * @param url A string containing a URL to query for
94 *
95 * @return vector of strings containing ipv6 addresses
96 */
97 std::vector<std::string> get_ipv6(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
98
99 /**
100 * @brief gets all TXT records from a DNS query for the supplied URL;
101 * if no TXT record present returns an empty vector.
102 *
103 * @param url A string containing a URL to query for
104 *
105 * @return A vector of strings containing a TXT record; or an empty vector
106 */
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
110 /**
111 * @brief gets all TLSA TCP records from a DNS query for the supplied URL;
112 * if no TLSA record present returns an empty vector.
113 *
114 * @param url A string containing a URL to query for
115 * @param port The service port number (as string) to query
116 *
117 * @return A vector of strings containing all TLSA records; or an empty vector
118 */
119 std::vector<std::string> get_tlsa_tcp_record(boost::string_ref url, boost::string_ref port, bool& dnssec_available, bool& dnssec_valid);
120
121 /**
122 * @brief Gets a DNS address from OpenAlias format
123 *
124 * If the address looks good, but contains one @ symbol, replace that with a .
125 * e.g. [email protected] becomes donate.getmonero.org
126 *
127 * @param oa_addr OpenAlias address
128 *
129 * @return dns_addr DNS address
130 */
131 std::string get_dns_format_from_oa_address(const std::string& oa_addr);
132
133 /**
134 * @brief Gets the singleton instance of DNSResolver
135 *
136 * @return returns a pointer to the singleton
137 */
138 static DNSResolver& instance();
139
140 /**
141 * @brief Gets a new instance of DNSResolver
142 *
143 * @return returns a pointer to the new object
144 */
145 static DNSResolver create();
146
147 private:
148
149 /**
150 * @brief gets all records of a given type from a DNS query for the supplied URL;
151 * if no such record is present returns an empty vector.
152 *
153 * @param url A string containing a URL to query for
154 * @param record_type the record type to retrieve (DNS_TYPE_A, etc)
155 * @param reader a function that converts a record data to a string
156 *
157 * @return A vector of strings containing the requested record; or an empty vector
158 */
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
162 DNSResolverData *m_data;
163 }; // class DNSResolver
164
165 namespace dns_utils
166 {
167
168 std::string address_from_txt_record(const std::string& s);
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