/* 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 */ /** * Two in one: a DNS resolver factory and a DNS cache * * Put these two things into one class because it is easier this way to avoid * sync problems if e.g. there are 2 resolvers for the same host name * * This class is a Singleton. In case there are problems with this approach, * there is an alternative: * give every ResolverBase object a DnsMasterItem as variable, that is set * during construction and call update / get_cached_results on that instance */ #ifndef DNS_MASTER_H #define DNS_MASTER_H #include #include // pair #include #include #include #include "host/pinger.h" // for IoserviceItem #include "host/pingprotocol.h" #include "dns/dnsipprotocol.h" #include "dns/dnscache.h" #include "dns/resolverbase.h" class DnsMaster; typedef boost::shared_ptr DnsMasterItem; typedef std::pair resolver_key_type; typedef std::map resolver_map_type; /** * Factory and Cache of DNS resolvers * * to avoid having several resolvers resolving the same hostname which might * result in conflicts with caching, this class is a singleton factory and the * only place where Resolvers are constructed. They are remembered in an * internal cache by hostname and IP version requested (v4, v6 or both). * * During resolving, several different name servers will have to be queried for * the same hostname. These recursive resolvers are created using * get_recursor_for and are NOT cached, so they should only be used from another * "regular" resolver (created using get_resolver_for) * * The DnsMaster also remembers a few global variables that can be queried * using public getter functions and it creates the DnsCache used by all its * resolvers */ class DnsMaster : boost::noncopyable { // Resolver factory public: ResolverItem& get_resolver_for(const std::string &hostname, const PingProtocol &ping_protocol); ResolverItem& get_resolver_for(const std::string &hostname, const DnsIpProtocol &protocol); // implementation of singleton private: static DnsMasterItem TheOnlyInstance; 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); public: static void 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); static void 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); // needed for unit test static DnsMasterItem& get_instance(); ~DnsMaster(); // storage of global variables public: //boost::asio::ip::address &get_default_name_server() const; // unused int get_resolved_ip_ttl_threshold() const; int get_max_address_resolution_attempts() const; int get_max_recursion_count() const; // access to Cache static std::string get_cname_chain_str(const std::string &hostname); // variables private: IoServiceItem IoService; const boost::asio::ip::address DefaultNameServer; const int ResolvedIpTtlThreshold; const int MaxAddressResolutionAttempts; const int MaxRecursionCount; DnsCacheItem Cache; resolver_map_type ResolverMap; // internal helper functions private: bool is_ip(const std::string &hostname) const; }; #endif