completed partial IPv6 compatibility in DNS; does retrieve and Cache IPv6 IPs
[pingcheck] / src / dns / resolverbase.cpp
CommitLineData
4e7b6ff9
CH
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
c5b4902d
CH
23#include "dns/resolverbase.h"
24#include "dns/dnsmaster.h"
4e7b6ff9
CH
25
26#include <boost/bind.hpp>
27
cd71d095
CH
28#include <logfunc.hpp>
29
30using I2n::Logger::GlobalLogger;
31
c5b4902d 32
4e7b6ff9 33ResolverBase::~ResolverBase()
c5b4902d 34{ }
4e7b6ff9
CH
35
36/**
37 * callbacks should be of type
38 * void resolve_callback(const bool was_success,
cd71d095 39 * const int recursion_count)
4e7b6ff9 40 */
cd71d095
CH
41void ResolverBase::async_resolve(const callback_type &callback,
42 const int recursion_count)
4e7b6ff9 43{
cd71d095
CH
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) );
49 }
50 else
51 {
52 // remember callback
53 CallbackList.push(callback);
54
55 // check for trouble
56 if (CallbackList.size() > 10)
57 GlobalLogger.warning() << "ResolverBase: have "
58 << CallbackList.size() << " callback[s] listed for " << Hostname
59 << "!";
60
61 // let subclass do the resolving
62 do_resolve(recursion_count);
63 }
4e7b6ff9 64
4e7b6ff9
CH
65}
66
67ResolverBase::ResolverBase(const IoServiceItem &io_serv,
68 const std::string &hostname,
8f00b3df 69 const DnsIpProtocol &protocol,
4e7b6ff9
CH
70 const DnsCacheItem &cache )
71 : IoService( io_serv )
72 , Hostname( hostname )
8f00b3df 73 , Protocol( protocol )
4e7b6ff9
CH
74 , Cache( cache )
75 , CallbackList()
76{}
77
dbe986b9
CH
78std::string ResolverBase::get_hostname() const
79{ return Hostname; }
80
26b0f687 81std::string ResolverBase::get_skip_cname() const
23f51766
CH
82{
83 DnsMasterItem master = DnsMaster::get_instance();
84 std::string first_out_of_date = Cache->get_first_outdated_cname(
85 Hostname,
86 master->get_resolved_ip_ttl_threshold() );
87 if ( first_out_of_date == Hostname )
88 return "";
89 else
8d26221d 90 return first_out_of_date;
23f51766
CH
91}
92
e18c1337 93void ResolverBase::update_cache( const std::string &hostname,
26b0f687 94 const HostAddressVec &new_ips ) const
8f00b3df 95{ Cache->update( hostname, Protocol, new_ips ); }
e18c1337
CH
96
97void ResolverBase::update_cache( const std::string &hostname,
dbe986b9 98 const Cname &cname ) const
e18c1337
CH
99{ Cache->update( hostname, cname ); }
100
26b0f687 101void ResolverBase::update_cache( const HostAddressVec &new_ips ) const
8f00b3df 102{ Cache->update( Hostname, Protocol, new_ips ); }
4e7b6ff9 103
dbe986b9 104void ResolverBase::update_cache( const Cname &cname ) const
ad83004d
CH
105{ Cache->update( Hostname, cname ); }
106
ad83004d 107
8f00b3df
CH
108/**
109 * @brief get IPs like a "real" resolver, i.e. recursively going through CNAMEs
110 *
111 * if host is empty, will use this Resolver's Hostname and Protocol
112 *
113 * just calls DnsCache::get_ips_recursive
114 */
dbe986b9
CH
115HostAddressVec ResolverBase::get_cached_ips_recursively(const std::string host,
116 bool check_up_to_date) const
4e7b6ff9
CH
117{
118 if (host.empty())
8f00b3df 119 return Cache->get_ips_recursive(Hostname, Protocol, check_up_to_date);
4e7b6ff9 120 else
8f00b3df 121 return Cache->get_ips_recursive(host, Protocol, check_up_to_date);
4e7b6ff9
CH
122}
123
124void ResolverBase::schedule_callbacks(const bool was_success,
cd71d095 125 const int recursion_count)
4e7b6ff9
CH
126{
127 while ( !CallbackList.empty() )
128 {
129 callback_type callback = CallbackList.front();
130 CallbackList.pop();
cd71d095 131 IoService->post( boost::bind(callback, was_success, recursion_count) );
4e7b6ff9
CH
132 }
133}
134