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/dnsmaster.h"
25 #include <logfunc.hpp>
26 #include <boost/bind.hpp>
27 #include <boost/asio/placeholders.hpp>
29 #include "dns/ippseudoresolver.h"
30 #include "dns/dnsresolver.h"
33 using I2n::Logger::GlobalLogger;
36 DnsMasterItem DnsMaster::TheOnlyInstance;
38 // just delegates work to other create_master function
39 void DnsMaster::create_master(const IoServiceItem &io_serv,
40 const boost::asio::ip::address &default_name_server,
41 const int resolved_ip_ttl_threshold,
42 const int min_time_between_resolves,
43 const int max_address_resolution_attempts,
44 const int max_recursion_count,
45 const std::string &cache_file)
47 GlobalLogger.info() << "DnsMaster: Creating cache";
51 static_cast<uint32_t>(min_time_between_resolves)
54 create_master(io_serv,
56 resolved_ip_ttl_threshold,
57 max_address_resolution_attempts,
62 void DnsMaster::create_master(const IoServiceItem &io_serv,
63 const boost::asio::ip::address &default_name_server,
64 const int resolved_ip_ttl_threshold,
65 const int max_address_resolution_attempts,
66 const int max_recursion_count,
67 const DnsCacheItem &cache)
71 GlobalLogger.warning() << "DnsMaster: "
72 << "Blocking attempt to create another instance!";
76 GlobalLogger.info() << "DNS Master: creating instance";
77 TheOnlyInstance.reset( new DnsMaster(io_serv,
79 resolved_ip_ttl_threshold,
80 max_address_resolution_attempts,
86 DnsMaster::DnsMaster(const IoServiceItem &io_serv,
87 const boost::asio::ip::address &default_name_server,
88 const int resolved_ip_ttl_threshold,
89 const int max_address_resolution_attempts,
90 const int max_recursion_count,
91 const DnsCacheItem &cache)
92 : IoService( io_serv )
93 , DefaultNameServer( default_name_server )
94 , ResolvedIpTtlThreshold( resolved_ip_ttl_threshold )
95 , MaxAddressResolutionAttempts( max_address_resolution_attempts )
96 , MaxRecursionCount( max_recursion_count )
103 DnsMasterItem& DnsMaster::get_instance()
105 if ( !TheOnlyInstance )
106 GlobalLogger.error() << "DnsMaster: "
107 << "Request to return instance before creating it!";
108 return TheOnlyInstance;
111 DnsMaster::~DnsMaster()
113 GlobalLogger.info() << "DnsMaster: being destructed";
115 if (DnsMaster::TheOnlyInstance)
116 { // apparently, this static variable still exists while itself is
118 //GlobalLogger.warning() << "DnsMaster: being destructed but not "
119 // << "singleton instance TheOnlyInstance!";
120 DnsMaster::TheOnlyInstance.reset();
123 // Items in ResolverMap and the DnsCache might still be referenced by
124 // Resolvers and are smart pointers, anyway --> nothing to do here
129 ResolverItem& DnsMaster::get_resolver_for( const std::string &hostname,
130 const PingProtocol &ping_protocol )
132 // find suitable DnsIpProtocol for ping protocol
133 DnsIpProtocol protocol = ping2dns_protocol(ping_protocol);
134 return get_resolver_for(hostname, protocol);
138 ResolverItem& DnsMaster::get_resolver_for(const std::string &hostname,
139 const DnsIpProtocol &protocol)
141 // create key to ResolverMap
142 resolver_key_type key(hostname, protocol);
143 if ( ResolverMap.count(key) == 0 )
144 { // need to create a resolver
146 // check if it is an ip address, so can create a simple pseudo resolver
147 if ( is_ip(hostname) )
149 boost::asio::ip::address ip
150 = boost::asio::ip::address::from_string(hostname);
151 if ( (protocol == DNS_IPv4 && !ip.is_v4()) ||
152 (protocol == DNS_IPv6 && !ip.is_v6()) )
153 GlobalLogger.warning() << "DnsMaster:"
154 << "Asked to create a DNS resolver "
155 << "for wrong IP protocol: v4 != v6! "
156 << "We will comply.";
157 GlobalLogger.info() << "DnsMaster: Creating PseudoResolver for IP "
159 ResolverItem new_resolver( new IpPseudoResolver(IoService,
163 ResolverMap[key] = new_resolver;
167 GlobalLogger.info() << "DnsMaster: Creating Resolver for host "
168 << hostname << " and protocol " << to_string(protocol);
169 ResolverItem new_resolver( new DnsResolver(IoService,
173 DefaultNameServer) );
174 ResolverMap[key] = new_resolver;
177 return ResolverMap[key];
181 * return true if given hostname string actually is an IP
183 * delegates decision to boost::asio::ip::address::from_string
185 bool DnsMaster::is_ip(const std::string &hostname) const
189 boost::asio::ip::address ip = boost::asio::ip::address::from_string(
191 return ip.is_v4() || ip.is_v6();
193 catch ( const std::exception &ex )
200 /*boost::asio::ip::address &DnsMaster::get_name_server() const
205 int DnsMaster::get_resolved_ip_ttl_threshold() const
207 return ResolvedIpTtlThreshold;
210 int DnsMaster::get_max_address_resolution_attempts() const
212 return MaxAddressResolutionAttempts;
215 int DnsMaster::get_max_recursion_count() const
217 return MaxRecursionCount;