CRLF
[ghsmtp.git] / DNS.hpp
blob94b9e49a71bf4bc52e992cb8dc3e498783f25731
1 #ifndef DNS_DOT_HPP
2 #define DNS_DOT_HPP
4 #include <memory>
6 #include "DNS-message.hpp"
7 #include "DNS-rrs.hpp"
8 #include "Sock.hpp"
9 #include "iobuffer.hpp"
11 #include <glog/logging.h>
13 #include "pcg.hpp"
15 namespace DNS {
17 class Resolver {
18 public:
19 Resolver(Resolver const&) = delete;
20 Resolver& operator=(Resolver const&) = delete;
22 Resolver(fs::path config_path);
24 RR_collection get_records(RR_type typ, char const* name);
25 RR_collection get_records(RR_type typ, std::string const& name)
27 return get_records(typ, name.c_str());
30 std::vector<std::string> get_strings(RR_type typ, char const* name);
31 std::vector<std::string> get_strings(RR_type typ, std::string const& name)
33 return get_strings(typ, name.c_str());
36 message xchg(message const& q);
38 uint16_t rnd_id()
40 static_assert(std::numeric_limits<uint16_t>::min() == 0);
41 static_assert(std::numeric_limits<uint16_t>::max() == 65535);
42 std::uniform_int_distribution<int> uniform_dist(0, 65535);
43 return uniform_dist(rng_);
46 private:
47 std::unique_ptr<Sock> ns_sock_;
48 int ns_;
49 int ns_fd_;
51 inline static pcg_extras::seed_seq_from<std::random_device> seed_source_;
52 inline static pcg32 rng_{seed_source_};
55 class Query {
56 public:
57 Query(Query const&) = delete;
58 Query& operator=(Query const&) = delete;
60 Query(Resolver& res, RR_type type, char const* name);
61 Query(Resolver& res, RR_type type, std::string const& name)
62 : Query(res, type, name.c_str())
66 bool authentic_data() const { return authentic_data_; }
67 bool bogus_or_indeterminate() const { return bogus_or_indeterminate_; }
68 bool truncation() const { return truncation_; }
69 bool nx_domain() const { return nx_domain_; }
71 bool has_record() const { return has_record_; }
72 RR_collection get_records();
73 std::vector<std::string> get_strings();
75 uint16_t rcode() const { return rcode_; }
76 uint16_t extended_rcode() const { return extended_rcode_; }
78 private:
79 bool xchg_(Resolver& res, uint16_t id);
80 void check_answer_(Resolver& res, RR_type type, char const* name);
82 uint16_t rcode_{0};
83 uint16_t extended_rcode_{0};
85 RR_type type_;
87 message q_;
88 message a_;
90 bool authentic_data_{false};
91 bool bogus_or_indeterminate_{false};
92 bool truncation_{false};
93 bool nx_domain_{false};
94 bool has_record_{false};
97 inline std::vector<std::string>
98 get_strings(Resolver& res, RR_type type, char const* name)
100 return res.get_strings(type, name);
103 inline std::vector<std::string>
104 get_strings(Resolver& res, RR_type type, std::string const& name)
106 return res.get_strings(type, name.c_str());
109 inline bool has_record(Resolver& res, RR_type type, char const* name)
111 Query q(res, type, name);
112 return q.has_record();
115 inline bool has_record(Resolver& res, RR_type type, std::string const& name)
117 return has_record(res, type, name.c_str());
119 } // namespace DNS
121 #endif // DNS_DOT_HPP