/* 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 */ #include "dns/resolverbase.h" #include "dns/dnsmaster.h" #include #include using I2n::Logger::GlobalLogger; ResolverBase::~ResolverBase() { } /** * callbacks should be of type * void resolve_callback(const bool was_success, * const int recursion_count) */ void ResolverBase::async_resolve(const callback_type &callback, const int recursion_count) { // limit depth of recursion if (recursion_count >= DnsMaster::get_instance()->get_max_recursion_count()) { // if recursed too much, schedule callback with was_success = false bool was_success = false; IoService->post( boost::bind(callback, was_success, recursion_count) ); } else { // remember callback CallbackList.push(callback); // check for trouble if (CallbackList.size() > 10) GlobalLogger.warning() << "ResolverBase: have " << CallbackList.size() << " callback[s] listed for " << Hostname << "!"; // let subclass do the resolving do_resolve(recursion_count); } } ResolverBase::ResolverBase(const IoServiceItem &io_serv, const std::string &hostname, const DnsIpProtocol &protocol, const DnsCacheItem &cache ) : IoService( io_serv ) , Hostname( hostname ) , Protocol( protocol ) , Cache( cache ) , CallbackList() {} std::string ResolverBase::get_hostname() const { return Hostname; } std::string ResolverBase::get_skip_cname() const { DnsMasterItem master = DnsMaster::get_instance(); std::string first_out_of_date = Cache->get_first_outdated_cname( Hostname, master->get_resolved_ip_ttl_threshold() ); if ( first_out_of_date == Hostname ) return ""; else return first_out_of_date; } void ResolverBase::update_cache( const std::string &hostname, const HostAddressVec &new_ips ) const { Cache->update( hostname, Protocol, new_ips ); } void ResolverBase::update_cache( const std::string &hostname, const Cname &cname ) const { Cache->update( hostname, cname ); } void ResolverBase::update_cache( const HostAddressVec &new_ips ) const { Cache->update( Hostname, Protocol, new_ips ); } void ResolverBase::update_cache( const Cname &cname ) const { Cache->update( Hostname, cname ); } /** * @brief get IPs like a "real" resolver, i.e. recursively going through CNAMEs * * if host is empty, will use this Resolver's Hostname and Protocol * * just calls DnsCache::get_ips_recursive */ HostAddressVec ResolverBase::get_cached_ips_recursively(const std::string host, bool check_up_to_date) const { if (host.empty()) return Cache->get_ips_recursive(Hostname, Protocol, check_up_to_date); else return Cache->get_ips_recursive(host, Protocol, check_up_to_date); } void ResolverBase::schedule_callbacks(const bool was_success, const int recursion_count) { while ( !CallbackList.empty() ) { callback_type callback = CallbackList.front(); CallbackList.pop(); IoService->post( boost::bind(callback, was_success, recursion_count) ); } }