some work
[ghsmtp.git] / DNS-ldns.hpp
blob745ba7713a0c4736ccfe6f000a29170fb7091e8a
1 #ifndef DNS_LDNS_DOT_HPP
2 #define DNS_LDNS_DOT_HPP
4 #include <cstddef>
5 #include <string>
6 #include <variant>
7 #include <vector>
9 #include <netinet/in.h>
11 #include "DNS-rrs.hpp"
13 // forward decl
14 typedef struct ldns_struct_pkt ldns_pkt;
15 typedef struct ldns_struct_rdf ldns_rdf;
16 typedef struct ldns_struct_resolver ldns_resolver;
17 typedef struct ldns_struct_rr_list ldns_rr_list;
19 namespace DNS_ldns {
21 class Domain {
22 public:
23 Domain(Domain const&) = delete;
24 Domain& operator=(Domain const&) = delete;
26 explicit Domain(char const* domain);
27 explicit Domain(std::string const& domain);
28 ~Domain();
30 std::string const& str() const { return str_; }
31 ldns_rdf* get() const { return rdfp_; }
33 private:
34 std::string str_;
35 ldns_rdf* rdfp_;
37 friend std::ostream& operator<<(std::ostream& os, Domain const& dom)
39 return os << dom.str();
43 class Resolver {
44 public:
45 Resolver(Resolver const&) = delete;
46 Resolver& operator=(Resolver const&) = delete;
48 Resolver();
49 ~Resolver();
51 DNS::RR_collection get_records(DNS::RR_type typ,
52 std::string const& domain) const
54 return get_records(typ, domain.c_str());
56 DNS::RR_collection get_records(DNS::RR_type typ, char const* domain) const;
58 std::vector<std::string> get_strings(DNS::RR_type typ,
59 std::string const& domain) const
61 return get_strings(typ, domain.c_str());
63 std::vector<std::string> get_strings(DNS::RR_type typ,
64 char const* domain) const;
66 ldns_resolver* get() const { return res_; }
68 private:
69 ldns_resolver* res_;
72 class Query {
73 public:
74 Query(Query const&) = delete;
75 Query& operator=(Query const&) = delete;
77 Query(Resolver const& res, DNS::RR_type type, std::string const& dom);
78 Query(Resolver const& res, DNS::RR_type type, char const* dom);
79 ~Query();
81 ldns_pkt* get() const { return p_; }
83 bool authentic_data() const { return authentic_data_; }
84 bool bogus_or_indeterminate() const { return bogus_or_indeterminate_; }
85 bool nx_domain() const { return nx_domain_; }
87 DNS::RR_collection get_records() const;
88 std::vector<std::string> get_strings() const;
90 private:
91 ldns_pkt* p_{nullptr};
93 bool authentic_data_{false};
94 bool bogus_or_indeterminate_{false};
95 bool nx_domain_{false};
98 class RR_list {
99 public:
100 RR_list(RR_list const&) = delete;
101 RR_list& operator=(RR_list const&) = delete;
103 explicit RR_list(Query const& q);
104 ~RR_list();
106 DNS::RR_collection get_records() const;
107 std::vector<std::string> get_strings() const;
109 private:
110 ldns_rr_list* rrlst_answer_{nullptr};
111 ldns_rr_list* rrlst_additional_{nullptr};
114 inline std::vector<std::string>
115 get_strings(Resolver const& res, DNS::RR_type type, char const* domain)
117 return res.get_strings(type, domain);
120 inline std::vector<std::string>
121 get_strings(Resolver const& res, DNS::RR_type type, std::string const& domain)
123 return res.get_strings(type, domain.c_str());
126 inline bool
127 has_record(Resolver const& res, DNS::RR_type type, char const* domain)
129 auto rr_set = res.get_records(type, domain);
130 return !rr_set.empty();
133 inline bool
134 has_record(Resolver const& res, DNS::RR_type type, std::string const& domain)
136 return has_record(res, type, domain.c_str());
139 } // namespace DNS_ldns
141 #endif // DNS_LDNS_DOT_HPP