still must match obs first
[ghsmtp.git] / DNS-ldns.hpp
blob89fe3243fbb3af8d6a6ccd5f7197d5a91fd1d79c
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 truncation() const { return truncation_; }
86 bool nx_domain() const { return nx_domain_; }
88 DNS::RR_collection get_records() const;
89 std::vector<std::string> get_strings() const;
91 private:
92 ldns_pkt* p_{nullptr};
94 bool authentic_data_{false};
95 bool bogus_or_indeterminate_{false};
96 bool truncation_{false};
97 bool nx_domain_{false};
100 class RR_list {
101 public:
102 RR_list(RR_list const&) = delete;
103 RR_list& operator=(RR_list const&) = delete;
105 explicit RR_list(Query const& q);
106 ~RR_list();
108 DNS::RR_collection get_records() const;
109 std::vector<std::string> get_strings() const;
111 private:
112 ldns_rr_list* rrlst_answer_{nullptr};
113 ldns_rr_list* rrlst_additional_{nullptr};
116 inline std::vector<std::string>
117 get_strings(Resolver const& res, DNS::RR_type type, char const* domain)
119 return res.get_strings(type, domain);
122 inline std::vector<std::string>
123 get_strings(Resolver const& res, DNS::RR_type type, std::string const& domain)
125 return res.get_strings(type, domain.c_str());
128 inline bool
129 has_record(Resolver const& res, DNS::RR_type type, char const* domain)
131 auto rr_set = res.get_records(type, domain);
132 return !rr_set.empty();
135 inline bool
136 has_record(Resolver const& res, DNS::RR_type type, std::string const& domain)
138 return has_record(res, type, domain.c_str());
141 } // namespace DNS_ldns
143 #endif // DNS_LDNS_DOT_HPP