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 std::string &cache_file)
46 GlobalLogger.info() << "DnsMaster: Creating cache";
50 static_cast<uint32_t>(min_time_between_resolves)
53 create_master(io_serv,
55 resolved_ip_ttl_threshold,
56 max_address_resolution_attempts,
60 void DnsMaster::create_master(const IoServiceItem &io_serv,
61 const boost::asio::ip::address &default_name_server,
62 const int resolved_ip_ttl_threshold,
63 const int max_address_resolution_attempts,
64 const DnsCacheItem &cache)
68 GlobalLogger.warning() << "DnsMaster: "
69 << "Blocking attempt to create another instance!";
73 GlobalLogger.info() << "DNS Master: creating instance";
74 TheOnlyInstance.reset( new DnsMaster(io_serv,
76 resolved_ip_ttl_threshold,
77 max_address_resolution_attempts,
82 DnsMaster::DnsMaster(const IoServiceItem &io_serv,
83 const boost::asio::ip::address &default_name_server,
84 const int resolved_ip_ttl_threshold,
85 const int max_address_resolution_attempts,
86 const DnsCacheItem &cache)
87 : IoService( io_serv )
88 , DefaultNameServer( default_name_server )
89 , ResolvedIpTtlThreshold( resolved_ip_ttl_threshold )
90 , MaxAddressResolutionAttempts( max_address_resolution_attempts )
97 DnsMasterItem& DnsMaster::get_instance()
99 if ( !TheOnlyInstance )
100 GlobalLogger.error() << "DnsMaster: "
101 << "Request to return instance before creating it!";
102 return TheOnlyInstance;
105 DnsMaster::~DnsMaster()
107 GlobalLogger.info() << "DnsMaster: being destructed";
109 if (DnsMaster::TheOnlyInstance)
110 { // apparently, this static variable still exists while itself is
112 //GlobalLogger.warning() << "DnsMaster: being destructed but not "
113 // << "singleton instance TheOnlyInstance!";
114 DnsMaster::TheOnlyInstance.reset();
117 // Items in ResolverMap and the DnsCache might still be referenced by
118 // Resolvers and are smart pointers, anyway --> nothing to do here
123 ResolverItem& DnsMaster::get_resolver_for( const std::string &hostname,
124 const PingProtocol &ping_protocol )
126 // find suitable DnsIpProtocol for ping protocol
127 DnsIpProtocol protocol = ping2dns_protocol(ping_protocol);
128 return get_resolver_for(hostname, protocol);
132 ResolverItem& DnsMaster::get_resolver_for(const std::string &hostname,
133 const DnsIpProtocol &protocol)
135 // create key to ResolverMap
136 resolver_key_type key(hostname, protocol);
137 if ( ResolverMap.count(key) == 0 )
138 { // need to create a resolver
140 // check if it is an ip address, so can create a simple pseudo resolver
141 if ( is_ip(hostname) )
143 boost::asio::ip::address ip
144 = boost::asio::ip::address::from_string(hostname);
145 if ( (protocol == DNS_IPv4 && !ip.is_v4()) ||
146 (protocol == DNS_IPv6 && !ip.is_v6()) )
147 GlobalLogger.warning() << "DnsMaster:"
148 << "Asked to create a DNS resolver "
149 << "for wrong IP protocol: v4 != v6! "
150 << "We will comply.";
151 GlobalLogger.info() << "DnsMaster: Creating PseudoResolver for IP "
153 ResolverItem new_resolver( new IpPseudoResolver(IoService,
156 ResolverMap[key] = new_resolver;
160 GlobalLogger.info() << "DnsMaster: Creating Resolver for host "
161 << hostname << " and protocol " << to_string(protocol);
162 ResolverItem new_resolver( new DnsResolver(IoService,
166 DefaultNameServer) );
167 ResolverMap[key] = new_resolver;
170 return ResolverMap[key];
174 * return true if given hostname string actually is an IP
176 * delegates decision to boost::asio::ip::address::from_string
178 bool DnsMaster::is_ip(const std::string &hostname) const
182 boost::asio::ip::address ip = boost::asio::ip::address::from_string(
184 return ip.is_v4() || ip.is_v6();
186 catch ( const std::exception &ex )
193 DnsIpProtocol DnsMaster::ping2dns_protocol(const PingProtocol& pprot)
197 case PingProtocol_ICMP: return DNS_IPv4; break;
198 case PingProtocol_ICMPv6: return DNS_IPv6; break;
199 case PingProtocol_TCP: return DNS_IPv4; break;
200 case PingProtocol_TCP_IPv6: return DNS_IPv6; break;
202 GlobalLogger.warning() << "DnsMaster: Unexpected ping protocol: "
203 << static_cast<int>(pprot);
209 /*boost::asio::ip::address &DnsMaster::get_name_server() const
214 int DnsMaster::get_resolved_ip_ttl_threshold() const
216 return ResolvedIpTtlThreshold;
219 int DnsMaster::get_max_address_resolution_attempts() const
221 return MaxAddressResolutionAttempts;
224 std::string to_string(const DnsIpProtocol &protocol)
228 case DNS_IPv4: return "IPv4"; break;
229 case DNS_IPv6: return "IPv6"; break;
230 case DNS_IPALL: return "IPv4/6"; break;
231 default: GlobalLogger.warning() << "Unexpected protocol in to_string!";
232 return "Unexpected Protocol"; break;