created arg recursion_count to async_resolve and many other to avoid infinite loops
[pingcheck] / src / dns / resolverbase.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 RESOLVER_BASE_H
24#define RESOLVER_BASE_H
25
26#include <boost/shared_ptr.hpp>
4e7b6ff9 27#include <boost/function.hpp>
36ad976b 28
4e7b6ff9
CH
29#include <queue>
30#include "host/pinger.h"
96779587 31#include "dns/hostaddress.h"
c5b4902d 32#include "dns/dnscache.h"
96779587 33
4e7b6ff9
CH
34typedef boost::function<void(const bool, const int)> callback_type;
35typedef std::queue<callback_type> callback_list_type;
96779587 36
23f51766
CH
37class ResolverBase;
38typedef boost::shared_ptr<ResolverBase> ResolverItem;
39
40
36ad976b
CH
41/**
42 * @brief: abstract base class for DnsResolver and IpPseudoResolver
43 */
44class ResolverBase
45{
46public:
4e7b6ff9
CH
47 virtual ~ResolverBase();
48
49 /**
50 * callbacks should be of type
51 * void resolve_callback(const bool was_success,
cd71d095 52 * const int recursion_count)
4e7b6ff9 53 */
cd71d095
CH
54 void async_resolve(const callback_type &callback,
55 const int recursion_count=0);
e18c1337 56 virtual void cancel_resolve() = 0;
72be9e7d
CH
57 virtual bool is_resolving() const = 0;
58 virtual bool is_waiting_to_resolve() const = 0;
36ad976b 59
26b0f687 60 virtual HostAddress get_next_ip(const bool check_up_to_date=true) = 0;
923626c0 61 virtual bool have_up_to_date_ip() = 0;
fd62d09f 62 virtual int get_resolved_ip_count(const bool check_up_to_date=true) = 0;
923626c0 63
dbe986b9
CH
64 std::string get_hostname() const;
65
26b0f687 66 std::string get_skip_cname() const;
23f51766 67
96779587 68protected:
4e7b6ff9
CH
69 ResolverBase(const IoServiceItem &io_serv,
70 const std::string &hostname,
71 const DnsCacheItem &cache );
96779587
CH
72
73// variables
4e7b6ff9
CH
74protected:
75 IoServiceItem IoService;
dbe986b9 76 const std::string Hostname;
96779587 77 DnsCacheItem Cache;
4e7b6ff9 78 callback_list_type CallbackList;
96779587
CH
79
80// functions for subclasses
81protected:
cd71d095
CH
82 // recursion_count is guaranteed to be below
83 // DnsMaster::get_max_recursion_count
84 virtual void do_resolve(const int recursion_count) = 0;
4e7b6ff9 85
dbe986b9 86 // convenient forwards to DnsCache that just insert Hostname
26b0f687 87 void update_cache( const HostAddressVec &new_ips ) const;
dbe986b9 88 void update_cache( const Cname &cname ) const;
e18c1337 89 void update_cache( const std::string &hostname,
26b0f687 90 const HostAddressVec &new_ips ) const;
e18c1337 91 void update_cache( const std::string &hostname,
dbe986b9 92 const Cname &cname ) const;
4e7b6ff9 93
dbe986b9
CH
94 HostAddressVec get_cached_ips_recursively(const std::string host="",
95 const bool check_up_to_date=false) const;
4e7b6ff9
CH
96
97 void schedule_callbacks(const bool was_success,
cd71d095 98 const int recursion_count);
4e7b6ff9 99
96779587 100};
36ad976b
CH
101
102#endif
103
104// (created using vim -- the world's best text editor)
105