2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
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.
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.
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.
20 Christian Herdtweck, Intra2net AG 2015
23 #include "dns/resolverbase.h"
24 #include "dns/dnsmaster.h"
26 #include <boost/bind.hpp>
28 #include <logfunc.hpp>
30 using I2n::Logger::GlobalLogger;
33 ResolverBase::~ResolverBase()
37 * callbacks should be of type
38 * void resolve_callback(const bool was_success,
39 * const int recursion_count)
41 void ResolverBase::async_resolve(const callback_type &callback,
42 const int recursion_count)
44 // limit depth of recursion
45 if (recursion_count >= DnsMaster::get_instance()->get_max_recursion_count())
46 { // if recursed too much, schedule callback with was_success = false
47 bool was_success = false;
48 IoService->post( boost::bind(callback, was_success, recursion_count) );
53 CallbackList.push(callback);
56 if (CallbackList.size() > 10)
57 GlobalLogger.warning() << "ResolverBase: have "
58 << CallbackList.size() << " callback[s] listed for " << Hostname
61 // let subclass do the resolving
62 do_resolve(recursion_count);
67 ResolverBase::ResolverBase(const IoServiceItem &io_serv,
68 const std::string &hostname,
69 const DnsCacheItem &cache )
70 : IoService( io_serv )
71 , Hostname( hostname )
76 std::string ResolverBase::get_hostname() const
79 std::string ResolverBase::get_skip_cname() const
81 DnsMasterItem master = DnsMaster::get_instance();
82 std::string first_out_of_date = Cache->get_first_outdated_cname(
84 master->get_resolved_ip_ttl_threshold() );
85 if ( first_out_of_date == Hostname )
88 return first_out_of_date;
91 void ResolverBase::update_cache( const std::string &hostname,
92 const HostAddressVec &new_ips ) const
93 { Cache->update( hostname, new_ips ); }
95 void ResolverBase::update_cache( const std::string &hostname,
96 const Cname &cname ) const
97 { Cache->update( hostname, cname ); }
99 void ResolverBase::update_cache( const HostAddressVec &new_ips ) const
100 { Cache->update( Hostname, new_ips ); }
102 void ResolverBase::update_cache( const Cname &cname ) const
103 { Cache->update( Hostname, cname ); }
106 HostAddressVec ResolverBase::get_cached_ips_recursively(const std::string host,
107 bool check_up_to_date) const
110 return Cache->get_ips_recursive(Hostname, check_up_to_date);
112 return Cache->get_ips_recursive(host, check_up_to_date);
115 void ResolverBase::schedule_callbacks(const bool was_success,
116 const int recursion_count)
118 while ( !CallbackList.empty() )
120 callback_type callback = CallbackList.front();
122 IoService->post( boost::bind(callback, was_success, recursion_count) );