/* 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/dnsmaster.h" #include #include #include #include "dns/ippseudoresolver.h" #include "dns/dnsresolver.h" using boost::bind; using I2n::Logger::GlobalLogger; DnsMasterItem DnsMaster::TheOnlyInstance; // just delegates work to other create_master function void DnsMaster::create_master(const IoServiceItem &io_serv, const boost::asio::ip::address &default_name_server, const int resolved_ip_ttl_threshold, const int min_time_between_resolves, const int max_address_resolution_attempts, const int max_recursion_count, const std::string &cache_file) { GlobalLogger.info() << "DnsMaster: Creating cache"; DnsCacheItem cache( new DnsCache(io_serv, cache_file, static_cast(min_time_between_resolves) ) ); create_master(io_serv, default_name_server, resolved_ip_ttl_threshold, max_address_resolution_attempts, max_recursion_count, cache); } void DnsMaster::create_master(const IoServiceItem &io_serv, const boost::asio::ip::address &default_name_server, const int resolved_ip_ttl_threshold, const int max_address_resolution_attempts, const int max_recursion_count, const DnsCacheItem &cache) { if (TheOnlyInstance) { GlobalLogger.warning() << "DnsMaster: " << "Blocking attempt to create another instance!"; return; } GlobalLogger.info() << "DNS Master: creating instance"; TheOnlyInstance.reset( new DnsMaster(io_serv, default_name_server, resolved_ip_ttl_threshold, max_address_resolution_attempts, max_recursion_count, cache) ); } DnsMaster::DnsMaster(const IoServiceItem &io_serv, const boost::asio::ip::address &default_name_server, const int resolved_ip_ttl_threshold, const int max_address_resolution_attempts, const int max_recursion_count, const DnsCacheItem &cache) : IoService( io_serv ) , DefaultNameServer( default_name_server ) , ResolvedIpTtlThreshold( resolved_ip_ttl_threshold ) , MaxAddressResolutionAttempts( max_address_resolution_attempts ) , MaxRecursionCount( max_recursion_count ) , Cache(cache) , ResolverMap() { } DnsMasterItem& DnsMaster::get_instance() { if ( !TheOnlyInstance ) GlobalLogger.error() << "DnsMaster: " << "Request to return instance before creating it!"; return TheOnlyInstance; } DnsMaster::~DnsMaster() { GlobalLogger.info() << "DnsMaster: being destructed"; if (DnsMaster::TheOnlyInstance) { // apparently, this static variable still exists while itself is // destructed... //GlobalLogger.warning() << "DnsMaster: being destructed but not " // << "singleton instance TheOnlyInstance!"; DnsMaster::TheOnlyInstance.reset(); } // Items in ResolverMap and the DnsCache might still be referenced by // Resolvers and are smart pointers, anyway --> nothing to do here } ResolverItem& DnsMaster::get_resolver_for( const std::string &hostname, const PingProtocol &ping_protocol ) { // find suitable DnsIpProtocol for ping protocol DnsIpProtocol protocol = ping2dns_protocol(ping_protocol); return get_resolver_for(hostname, protocol); } ResolverItem& DnsMaster::get_resolver_for(const std::string &hostname, const DnsIpProtocol &protocol) { // create key to ResolverMap resolver_key_type key(hostname, protocol); if ( ResolverMap.count(key) == 0 ) { // need to create a resolver // check if it is an ip address, so can create a simple pseudo resolver if ( is_ip(hostname) ) { boost::asio::ip::address ip = boost::asio::ip::address::from_string(hostname); if ( (protocol == DNS_IPv4 && !ip.is_v4()) || (protocol == DNS_IPv6 && !ip.is_v6()) ) GlobalLogger.warning() << "DnsMaster:" << "Asked to create a DNS resolver " << "for wrong IP protocol: v4 != v6! " << "We will comply."; GlobalLogger.info() << "DnsMaster: Creating PseudoResolver for IP " << ip; ResolverItem new_resolver( new IpPseudoResolver(IoService, hostname, protocol, Cache) ); ResolverMap[key] = new_resolver; } else { GlobalLogger.info() << "DnsMaster: Creating Resolver for host " << hostname << " and protocol " << to_string(protocol); ResolverItem new_resolver( new DnsResolver(IoService, hostname, protocol, Cache, DefaultNameServer) ); ResolverMap[key] = new_resolver; } } return ResolverMap[key]; } /** * return true if given hostname string actually is an IP * * delegates decision to boost::asio::ip::address::from_string */ bool DnsMaster::is_ip(const std::string &hostname) const { try { boost::asio::ip::address ip = boost::asio::ip::address::from_string( hostname); return ip.is_v4() || ip.is_v6(); } catch ( const std::exception &ex ) { return false; } } /*boost::asio::ip::address &DnsMaster::get_name_server() const { return NameServer; }*/ int DnsMaster::get_resolved_ip_ttl_threshold() const { return ResolvedIpTtlThreshold; } int DnsMaster::get_max_address_resolution_attempts() const { return MaxAddressResolutionAttempts; } int DnsMaster::get_max_recursion_count() const { return MaxRecursionCount; } std::string DnsMaster::get_cname_chain_str(const std::string &hostname) { DnsMasterItem master = get_instance(); if (master) return master->Cache->get_cname_chain_str(hostname); else return hostname; }