42 #ifndef _DWMDNSRESOLVER_HH_ 43 #define _DWMDNSRESOLVER_HH_ 46 #include <type_traits> 71 Resolver(
const std::string & filename =
"/etc/resolv.conf",
72 const std::string & order =
"files,dns");
80 std::vector<in6_addr> & in6Addrs,
81 std::vector<in_addr> & inAddrs);
89 std::vector<std::string> & names);
97 std::vector<std::string> & names);
110 bool Get(
const std::string & name, uint16_t rrtype,
111 std::vector<ResourceRecord> & results,
112 bool doEDNS =
false,
bool doDNSSEC =
false,
113 bool tcpFallback =
true);
126 template <
typename RRT>
127 bool Get(
const std::string & name,
128 std::vector<ResourceRecord> & results,
129 bool doEDNS =
false,
bool doDNSSEC =
false,
130 bool tcpFallback =
true)
133 if (std::is_same<RRT,RRDataPTR>::value
134 || std::is_same<RRT,RRDataA>::value
135 || std::is_same<RRT,RRDataAAAA>::value) {
136 if (_order.find(
"files") == 0) {
138 etcHosts.
Get(name, RRT::k_rrtype, results);
142 if (results.empty() && (_order.find(
"dns") != std::string::npos)) {
143 std::vector<std::string> namesToTry;
144 GetNamesToTry(name, RRT::k_rrtype, namesToTry);
145 for (
auto & n : namesToTry) {
155 for (
auto & ns : _nameservers) {
156 if (GetViaUDP<RRT>(ns, message, results)) {
159 else if (tcpFallback) {
160 if (GetViaTCP<RRT>(ns, message, results)) {
165 if (! results.empty()) {
171 if (std::is_same<RRT,RRDataPTR>::value
172 || std::is_same<RRT,RRDataA>::value
173 || std::is_same<RRT,RRDataAAAA>::value) {
174 std::string::size_type fidx = _order.find(
"files");
175 if (results.empty() && (fidx != std::string::npos) && (fidx > 0)) {
177 etcHosts.
Get(name, RRT::k_rrtype, results);
181 return (! results.empty());
194 template <
typename RRT>
195 bool Get(
const std::string & name, std::vector<RRT> & results,
196 bool doEDNS =
false,
bool doDNSSEC =
false,
197 bool tcpFallback =
true)
200 std::vector<ResourceRecord> rrs;
201 bool rc = Get<RRT>(name, rrs, doEDNS, doDNSSEC, tcpFallback);
202 for (
auto rr : rrs) {
203 RRT *data = rr.Data<RRT>();
204 if (data !=
nullptr) {
205 results.push_back(*data);
208 return (! results.empty());
228 std::vector<NameServer> _nameservers;
229 std::vector<std::string> _searchList;
232 std::atomic<uint16_t> _msgid;
237 template <
typename RRT>
239 std::vector<ResourceRecord> & results)
244 for (
auto & answer : rmsg.
Answers()) {
245 RRT *rrdata = answer.Data<RRT>();
246 if (rrdata !=
nullptr) {
247 results.push_back(answer);
252 return (! results.empty());
258 template <
typename RRT>
260 std::vector<ResourceRecord> & results)
265 for (
auto & answer : rmsg.
Answers()) {
266 RRT *rrdata = answer.Data<RRT>();
267 if (rrdata !=
nullptr) {
268 results.push_back(answer);
273 return (! results.empty());
279 void GetNamesToTry(
const std::string & name, uint16_t rrtype,
280 std::vector<std::string> & names)
const;
287 #endif // _DWMDNSRESOLVER_HH_ bool SendMessage(const Message &message)
Sends the given message to the name server via UDP.
bool ReadMessage(Message &message)
Reads a message from the name server via TCP.
const std::vector< NameServer > & NameServers() const
Returns a const reference to the contained nameservers.
Definition: DwmDnsResolver.hh:214
Encapsulates a resolver 'hosts' file, such as /etc/hosts.
Definition: DwmDnsEtcHosts.hh:68
const std::vector< ResourceRecord > & Answers() const
Returns a const reference to the records from the answers section of the message. ...
Definition: DwmDnsMessage.hh:122
Dwm::Dns::EtcHosts class definition.
Dwm::Dns::NameServer class definition.
const std::vector< MessageQuestion > & Questions() const
Returns a const reference to the questions in the message.
Definition: DwmDnsMessage.hh:105
Encapsulates a DNS message.
Definition: DwmDnsMessage.hh:68
bool Get(const std::string &name, std::vector< ResourceRecord > &results, bool doEDNS=false, bool doDNSSEC=false, bool tcpFallback=true)
Fetches resource records whose data type is RRT for the given domain name name.
Definition: DwmDnsResolver.hh:127
void EnableEDNS(uint16_t pduSize, bool doDnssec)
Enable EDNS by setting the maximum packet size to pduSize and enable or disable DNSSEC.
Encapsulates a DNS message question.
Definition: DwmDnsMessageQuestion.hh:59
Resolver(const std::string &filename="/etc/resolv.conf", const std::string &order="files,dns")
Construct with the given resolver configuration file and the preferred order of resolver sources (ala...
bool WriteMessage(const Message &message)
Sends the given message to the name server via TCP.
Definition: DwmDnsEtcHosts.hh:60
Simple DNS resolver class.
Definition: DwmDnsResolver.hh:64
bool Get(const std::string &name, uint16_t rrtype, std::vector< ResourceRecord > &results)
Gets DNS resource records of type rrtype for the given host name.
bool ReceiveMessage(Message &message)
Receives a message from the name server via UDP.
static constexpr uint16_t k_classIN
Query classes.
Definition: DwmDnsMessageQuestion.hh:102
bool Get(const std::string &name, std::vector< RRT > &results, bool doEDNS=false, bool doDNSSEC=false, bool tcpFallback=true)
Gets resource record data of type RRT for the given domain name name.
Definition: DwmDnsResolver.hh:195
bool GetHostByName(const std::string &name, std::vector< in6_addr > &in6Addrs, std::vector< in_addr > &inAddrs)
Fetches the IPv6 and IPv4 addresses for the given host name.
bool Get(const std::string &name, uint16_t rrtype, std::vector< ResourceRecord > &results, bool doEDNS=false, bool doDNSSEC=false, bool tcpFallback=true)
Fetches resource records of type rrtype for the given domain name name.
bool GetHostByAddr(const in6_addr &addr, std::vector< std::string > &names)
Fetches the host names for the given IPv6 address addr.
const MessageHeader & Header() const
Returns a const reference to the message header.
Definition: DwmDnsMessage.hh:89
Encapsulates a DNS name server, from the perspective of a DNS client.
Definition: DwmDnsNameServer.hh:65
std::vector< NameServer > & NameServers()
Returns a mutable reference to the contained nameservers.
Definition: DwmDnsResolver.hh:222