| 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 | #include <boost/uuid/uuid_generators.hpp> |
| 34 | |
| 35 | #include "dns/resolverbase.h" |
| 36 | #include "dns/dnsmaster.h" |
| 37 | #include "dns/dnscache.h" |
| 38 | |
| 39 | |
| 40 | typedef std::pair<std::string, std::string> string_pair; |
| 41 | typedef std::pair<std::string, Cname> src_cname_pair; |
| 42 | typedef std::pair<std::string, HostAddress> host_addr_pair; |
| 43 | |
| 44 | |
| 45 | class DnsResolver : public ResolverBase |
| 46 | { |
| 47 | public: |
| 48 | ~DnsResolver(); |
| 49 | |
| 50 | // constructor accessible from friend DnsMaster |
| 51 | public: |
| 52 | friend ResolverItem& DnsMaster::get_resolver_for( |
| 53 | const std::string &hostname, |
| 54 | const DnsIpProtocol &protocol); |
| 55 | private: |
| 56 | DnsResolver(IoServiceItem &io_serv, |
| 57 | const std::string &hostname, |
| 58 | const DnsIpProtocol &protocol, |
| 59 | const DnsCacheItem cache, |
| 60 | const boost::asio::ip::address &name_server); |
| 61 | |
| 62 | // only real public function (called from pingers) |
| 63 | public: |
| 64 | HostAddress get_next_ip(const bool check_up_to_date=true); |
| 65 | bool have_up_to_date_ip(); |
| 66 | int get_resolved_ip_count(const bool check_up_to_date=true); |
| 67 | void cancel_resolve(); |
| 68 | bool is_resolving() const; |
| 69 | bool is_waiting_to_resolve() const; |
| 70 | |
| 71 | // implementation of ResolverBase::async_resolve |
| 72 | protected: |
| 73 | void do_resolve(const int recursion_count); |
| 74 | |
| 75 | // internal helper functions |
| 76 | private: |
| 77 | void handle_resolve_timeout(const int recursion_count, |
| 78 | const boost::system::error_code &error); |
| 79 | void handle_dns_result(const int recursion_count, |
| 80 | const boost::system::error_code &error, |
| 81 | const std::size_t bytes_transferred); |
| 82 | void handle_unavailable(const int recursion_count); |
| 83 | void handle_ips(const int recursion_count, |
| 84 | const std::vector<host_addr_pair> &result_ips); |
| 85 | void handle_cname(const int recursion_count, |
| 86 | const std::vector<src_cname_pair> &result_cnames); |
| 87 | void cname_resolve_callback(const bool was_success, |
| 88 | const int recursion_count); |
| 89 | void schedule_retry(const int recursion_count); |
| 90 | void finalize_resolve(const bool success, const int recursion_count); |
| 91 | void stop_trying(const bool was_success); |
| 92 | void wait_timer_timeout_handler(const int recursion_count, |
| 93 | const boost::system::error_code &error); |
| 94 | void gather_results( const boost::net::dns::rr_list_t *rr_list, |
| 95 | std::vector<host_addr_pair> *result_ips, |
| 96 | std::vector<src_cname_pair> *result_cnames, |
| 97 | std::vector<string_pair> *result_nameservers ) const; |
| 98 | |
| 99 | // variables |
| 100 | private: |
| 101 | boost::asio::ip::udp::socket Socket; |
| 102 | boost::net::dns_buffer_t ReceiveBuffer; |
| 103 | boost::net::dns_buffer_t RequestBuffer; |
| 104 | boost::asio::ip::udp::endpoint NameServer; |
| 105 | boost::asio::deadline_timer ResolveTimeoutTimer; |
| 106 | boost::asio::deadline_timer PauseBeforeRetryTimer; |
| 107 | boost::asio::deadline_timer StaleDataLongtermTimer; |
| 108 | std::size_t NextIpIndex; |
| 109 | int RetryCount; |
| 110 | bool IsResolving; // true from async_resolve until finalize_resolve; |
| 111 | // false e.g. while waiting for StaleDataLongtermTimer |
| 112 | std::string LogPrefix; |
| 113 | boost::uuids::random_generator RandomIdGenerator; |
| 114 | uint16_t RequestId; |
| 115 | bool OperationCancelled; |
| 116 | bool LongtermTimerIsActive; |
| 117 | }; |
| 118 | |
| 119 | #endif |
| 120 | |