a75ea7ff70c8c9d452c1c28b607bb3c1e6516e4e
[pingcheck] / src / dns / dnsresolver.h
1 /*
2  The software in this package is distributed under the GNU General
3  Public License version 2 (with a special exception described below).
4
5  A copy of GNU General Public License (GPL) is included in this distribution,
6  in the file COPYING.GPL.
7
8  As a special exception, if other files instantiate templates or use macros
9  or inline functions from this file, or you compile this file and link it
10  with other works to produce a work based on this file, this file
11  does not by itself cause the resulting work to be covered
12  by the GNU General Public License.
13
14  However the source code for this file must still be made available
15  in accordance with section (3) of the GNU General Public License.
16
17  This exception does not invalidate any other reasons why a work based
18  on this file might be covered by the GNU General Public License.
19
20  Christian Herdtweck, Intra2net AG 2015
21  */
22
23 #ifndef DNS_RESOLVER_H
24 #define DNS_RESOLVER_H
25
26 #include "dns/dnsmaster.h"
27
28 #include <boost/asio/deadline_timer.hpp>
29 #include <boost/asio/ip/udp.hpp>
30 #include <boost/asio/ip/address.hpp>
31 #include <boost/system/error_code.hpp>
32 #include <boost/net/network_array.hpp>   // dns_buffer_t
33
34 #include "dns/resolverbase.h"
35 #include "dns/dnsmaster.h"
36 #include "dns/dnscache.h"
37
38
39 class DnsResolver : public ResolverBase
40 {
41 public:
42     ~DnsResolver();
43
44 // constructor accessible from friend DnsMaster
45 public:
46     friend ResolverItem& DnsMaster::get_resolver_for(
47                                                  const std::string &hostname,
48                                                  const DnsIpProtocol &protocol);
49 private:
50     DnsResolver(IoServiceItem &io_serv,
51                 const std::string &hostname,
52                 const DnsIpProtocol &protocol,
53                 const DnsCacheItem cache,
54                 const boost::asio::ip::address &name_server);
55
56 // only real public function (called from pingers)
57 public:
58     HostAddress get_next_ip();
59     bool have_up_to_date_ip();
60     int get_resolved_ip_count();
61
62 // implementation of ResolverBase::async_resolve
63 protected:
64     void do_resolve();
65
66 // internal helper functions
67 private:
68     void handle_resolve_timeout(const boost::system::error_code &error);
69     void handle_dns_result(const boost::system::error_code &error,
70                            const std::size_t bytes_transferred);
71     void handle_unavailable();
72     void handle_ips(const HostAddressVec &ips);
73     void handle_cname(const std::string &canonical_name);
74     void cname_resolve_callback(const std::string &canonical_name,
75                                 const bool was_success,
76                                 const int cname_count);
77     void finalize_resolve(const bool success, const int cname_count=0);
78     void stop_trying();
79     void wait_timer_timeout_handler(const boost::system::error_code &error);
80
81 // variables
82 private:
83     boost::asio::ip::udp::socket Socket;
84     boost::net::dns_buffer_t ReceiveBuffer;
85     DnsIpProtocol Protocol;
86     boost::asio::ip::udp::endpoint NameServer;
87     boost::asio::deadline_timer ResolveTimeoutTimer;
88     boost::asio::deadline_timer PauseBeforeRetryTimer;
89     boost::asio::deadline_timer StaleDataLongtermTimer;
90     std::size_t NextIpIndex;
91     int RetryCount;
92     bool IsResolving;
93     std::string LogPrefix;
94 };
95
96 #endif
97 // (created using vim -- the world's best text editor)
98