/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. Christian Herdtweck, Intra2net AG 2015 */ #ifndef DNS_RESOLVER_H #define DNS_RESOLVER_H #include "dns/dnsmaster.h" #include #include #include #include #include // dns_buffer_t #include #include "dns/resolverbase.h" #include "dns/dnsmaster.h" #include "dns/dnscache.h" typedef std::pair string_pair; typedef std::pair src_cname_pair; typedef std::pair host_addr_pair; class DnsResolver : public ResolverBase { public: ~DnsResolver(); // constructor accessible from friend DnsMaster public: friend ResolverItem& DnsMaster::get_resolver_for( const std::string &hostname, const DnsIpProtocol &protocol); private: DnsResolver(IoServiceItem &io_serv, const std::string &hostname, const DnsIpProtocol &protocol, const DnsCacheItem cache, const boost::asio::ip::address &name_server); // only real public function (called from pingers) public: HostAddress get_next_ip(const bool check_up_to_date=true); bool have_up_to_date_ip(); int get_resolved_ip_count(const bool check_up_to_date=true); void cancel_resolve(); bool is_resolving() const; bool is_waiting_to_resolve() const; // implementation of ResolverBase::async_resolve protected: void do_resolve(const int recursion_count); // internal helper functions private: void handle_resolve_timeout(const int recursion_count, const boost::system::error_code &error); void handle_dns_result(const int recursion_count, const boost::system::error_code &error, const std::size_t bytes_transferred); void handle_unavailable(const int recursion_count); void handle_ips(const int recursion_count, const std::vector &result_ips); void handle_cname(const int recursion_count, const std::vector &result_cnames); void cname_resolve_callback(const bool was_success, const int recursion_count); void schedule_retry(const int recursion_count); void finalize_resolve(const bool success, const int recursion_count); void stop_trying(const bool was_success); void wait_timer_timeout_handler(const int recursion_count, const boost::system::error_code &error); void gather_results( const boost::net::dns::rr_list_t *rr_list, std::vector *result_ips, std::vector *result_cnames, std::vector *result_nameservers ) const; // variables private: boost::asio::ip::udp::socket Socket; boost::net::dns_buffer_t ReceiveBuffer; boost::net::dns_buffer_t RequestBuffer; boost::asio::ip::udp::endpoint NameServer; boost::asio::deadline_timer ResolveTimeoutTimer; boost::asio::deadline_timer PauseBeforeRetryTimer; boost::asio::deadline_timer StaleDataLongtermTimer; std::size_t NextIpIndex; int RetryCount; bool IsResolving; // true from async_resolve until finalize_resolve; // false e.g. while waiting for StaleDataLongtermTimer std::string LogPrefix; boost::uuids::random_generator RandomIdGenerator; uint16_t RequestId; bool OperationCancelled; bool LongtermTimerIsActive; }; #endif