9 #ifndef __libutilxx__sella__net__DNSCallback_H__
10 #define __libutilxx__sella__net__DNSCallback_H__
12 #include "../../common.h"
13 #include "DNSException.h"
15 #include "IPAddress.h"
16 #include "../util/String.h"
18 #include <sys/socket.h>
31 class StoreDNSCallback;
32 class SharedStoreDNSCallback;
40 typedef std::shared_ptr<DNSCallback> shared;
42 enum Error :
int { None = DNS_E_NOERROR, Temporary = DNS_E_TEMPFAIL, Protocol = DNS_E_PROTOCOL, NXDomain = DNS_E_NXDOMAIN, NoData = DNS_E_NODATA, NoMemory = DNS_E_NOMEM, BadQuery = DNS_E_BADQUERY };
44 static const std::vector<IPAddress> none;
45 static const std::vector<std::string> empty;
50 virtual void success(
const std::string &qname,
const std::string &cname,
int ttl,
const std::vector<std::string> &response = empty)
throw () = 0;
51 virtual void failure(
const std::string &qname, Error error)
throw () = 0;
53 const std::string& getQname(
void)
const throw () {
57 int getRecord(
void)
const throw () {
61 int getProtocol(
void)
const throw () {
65 int getFlags(
void)
const throw () {
69 virtual void clear(
void)
throw () {
76 static const char* getErrorDetail(Error error) {
77 return ::dns_strerror((
int) error);
80 static const char* getErrorName(Error error) {
112 virtual void success(
const std::string &qname,
const std::string &cname,
int ttl,
const std::vector<std::string> &responses = empty)
throw () {
113 if (this->cname.empty()) {
121 this->responses.insert(this->responses.end(), responses.begin(), responses.end());
122 std::sort(this->responses.begin(), this->responses.end());
123 this->responses.erase(std::unique(this->responses.begin(), this->responses.end()), this->responses.end());
128 virtual void failure(
const std::string &qname, Error error)
throw () {
134 const std::string& getCname(
void)
const throw () {
138 int getTTL(
void)
const throw () {
142 const std::vector<std::string>& getResponses(
void)
const throw () {
146 Error getError(
void)
const throw () {
150 const char* getErrorName(
void)
const throw () {
151 return DNSCallback::getErrorName(error);
154 virtual void clear(
void)
throw () {
155 DNSCallback::clear();
165 std::vector<std::string> responses;
174 std::vector<std::string> responses;
183 virtual void success(
const std::string &qname,
const std::string &cname,
int ttl,
const std::vector<std::string> &responses = empty) throw () {
184 std::string query = qname;
185 std::string alias = cname;
188 query =
IPAddress(DNS::convertArpa(qname)).str();
192 if (!this->results.count(query)) {
193 this->results[query] = { alias, ttl, responses, record, protocol, None };
195 result_t &result = this->results[query];
197 if (ttl > result.ttl) {
201 result.responses.insert(result.responses.end(), responses.begin(), responses.end());
202 std::sort(result.responses.begin(), result.responses.end());
203 result.responses.erase(std::unique(result.responses.begin(), result.responses.end()), result.responses.end());
208 virtual void failure(
const std::string &qname, Error error)
throw () {
209 std::string query = qname;
213 query = IPAddress(DNS::convertArpa(qname)).str();
217 this->results[query] = { alias, -1, empty, record, protocol, error };
220 const std::map<std::string, result_t>& getResults(
void)
const throw () {
224 virtual void clear(
void) throw () {
225 DNSCallback::clear();
226 results.erase(qname);
230 std::map<std::string, result_t> &results;