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