finished self-implementation of DNS resolver recursion; will now remove all that!
[pingcheck] / src / dns / dnsresolver.h
CommitLineData
36ad976b
CH
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
c5b4902d 26#include "dns/dnsmaster.h"
36ad976b 27
36ad976b 28#include <boost/asio/deadline_timer.hpp>
96779587
CH
29#include <boost/asio/ip/udp.hpp>
30#include <boost/asio/ip/address.hpp>
36ad976b 31#include <boost/system/error_code.hpp>
946356e1 32#include <boost/net/network_array.hpp> // dns_buffer_t
ad83004d 33#include <boost/uuid/uuid_generators.hpp>
36ad976b 34
c5b4902d
CH
35#include "dns/resolverbase.h"
36#include "dns/dnsmaster.h"
37#include "dns/dnscache.h"
36ad976b 38
36ad976b 39
ad83004d 40typedef std::pair<std::string, std::string> string_pair;
dbe986b9 41typedef std::pair<std::string, Cname> src_cname_pair;
e18c1337 42typedef std::pair<std::string, HostAddress> host_addr_pair;
ad83004d
CH
43
44
36ad976b
CH
45class DnsResolver : public ResolverBase
46{
36ad976b 47public:
96779587 48 ~DnsResolver();
36ad976b 49
96779587
CH
50// constructor accessible from friend DnsMaster
51public:
4e7b6ff9 52 friend ResolverItem& DnsMaster::get_resolver_for(
923626c0
CH
53 const std::string &hostname,
54 const DnsIpProtocol &protocol);
e18c1337
CH
55 friend ResolverItem DnsMaster::get_recursor_for(
56 const std::string &hostname,
57 const DnsIpProtocol &protocol,
58 const boost::asio::ip::address &name_server);
36ad976b 59private:
4e7b6ff9
CH
60 DnsResolver(IoServiceItem &io_serv,
61 const std::string &hostname,
923626c0 62 const DnsIpProtocol &protocol,
96779587 63 const DnsCacheItem cache,
96779587 64 const boost::asio::ip::address &name_server);
36ad976b 65
4e7b6ff9 66// only real public function (called from pingers)
36ad976b 67public:
4e7b6ff9 68 HostAddress get_next_ip();
923626c0
CH
69 bool have_up_to_date_ip();
70 int get_resolved_ip_count();
e18c1337
CH
71 void cancel_resolve();
72 bool is_resolving();
4e7b6ff9
CH
73
74// implementation of ResolverBase::async_resolve
75protected:
76 void do_resolve();
36ad976b 77
c5b4902d 78// internal helper functions
36ad976b 79private:
36ad976b
CH
80 void handle_resolve_timeout(const boost::system::error_code &error);
81 void handle_dns_result(const boost::system::error_code &error,
82 const std::size_t bytes_transferred);
4e7b6ff9 83 void handle_unavailable();
e18c1337 84 void handle_ips(const std::vector<host_addr_pair> &result_ips);
dbe986b9 85 void handle_cname(const std::vector<src_cname_pair> &result_cnames);
ad83004d 86 void handle_recurse(const HostAddress &name_server);
dbe986b9 87 void handle_recurse_without_ip(const std::string &name_server);
e18c1337 88 void cname_resolve_callback(const bool was_success,
ad83004d
CH
89 const int recursion_count);
90 void recursive_resolve_callback(const uint32_t min_ttl,
91 const bool was_success,
92 const int recursion_count);
dbe986b9
CH
93 void name_server_resolve_callback(const bool was_success,
94 const int recursion_count);
95 void schedule_retry();
ad83004d 96 void finalize_resolve(const bool success, const int recursion_count=0);
96779587
CH
97 void stop_trying();
98 void wait_timer_timeout_handler(const boost::system::error_code &error);
ad83004d 99 void gather_results( const boost::net::dns::rr_list_t *answers,
e18c1337 100 std::vector<host_addr_pair> *result_ips,
dbe986b9 101 std::vector<src_cname_pair> *result_cnames,
ad83004d 102 std::vector<string_pair> *result_nameservers ) const;
36ad976b 103
c5b4902d 104// variables
36ad976b 105private:
96779587 106 boost::asio::ip::udp::socket Socket;
4e7b6ff9 107 boost::net::dns_buffer_t ReceiveBuffer;
ad83004d 108 boost::net::dns_buffer_t RequestBuffer;
923626c0 109 DnsIpProtocol Protocol;
96779587 110 boost::asio::ip::udp::endpoint NameServer;
36ad976b
CH
111 boost::asio::deadline_timer ResolveTimeoutTimer;
112 boost::asio::deadline_timer PauseBeforeRetryTimer;
113 boost::asio::deadline_timer StaleDataLongtermTimer;
4e7b6ff9 114 std::size_t NextIpIndex;
36ad976b
CH
115 int RetryCount;
116 bool IsResolving;
e91538f0 117 std::string LogPrefix;
ad83004d
CH
118 boost::uuids::random_generator RandomIdGenerator;
119 uint16_t RequestId;
120 ResolverItem Recursor;
e18c1337 121 bool OperationCancelled;
96779587 122};
36ad976b 123
36ad976b
CH
124#endif
125// (created using vim -- the world's best text editor)
126