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
24 * Two in one: a DNS resolver factory and a DNS cache
26 * Put these two things into one class because it is easier this way to avoid
27 * sync problems if e.g. there are 2 resolvers for the same host name
29 * This class is a Singleton. In case there are problems with this approach,
30 * there is an alternative:
31 * give every ResolverBase object a DnsMasterItem as variable, that is set
32 * during construction and call update / get_cached_results on that instance
39 #include <utility> // pair
41 #include <boost/shared_ptr.hpp>
42 #include <boost/noncopyable.hpp>
43 #include <boost/net/dns.hpp>
45 #include "host/pinger.h" // for IoserviceItem
46 #include "host/pingprotocol.h"
47 #include "dns/dnscache.h"
48 #include "dns/resolverbase.h"
52 typedef boost::shared_ptr<DnsMaster> DnsMasterItem;
54 typedef boost::net::dns::type_t DnsIpProtocol;
55 const DnsIpProtocol DNS_IPv4 = boost::net::dns::type_a;
56 const DnsIpProtocol DNS_IPv6 = boost::net::dns::type_a6;
57 const DnsIpProtocol DNS_IPALL = boost::net::dns::type_all;
59 typedef std::pair<std::string, DnsIpProtocol> resolver_key_type;
60 typedef std::map<resolver_key_type, ResolverItem> resolver_map_type;
63 class DnsMaster : boost::noncopyable
67 ResolverItem& get_resolver_for(const std::string &hostname,
68 const PingProtocol &ping_protocol);
69 ResolverItem& get_resolver_for(const std::string &hostname,
70 const DnsIpProtocol &protocol)
71 ResolverItem get_recursor_for(const std::string &hostname,
72 const DnsIpProtocol &protocol,
73 const boost::asio::ip::address &name_server);
75 // implementation of singleton
77 static DnsMasterItem TheOnlyInstance;
79 DnsMaster(const IoServiceItem &io_serv,
80 const boost::asio::ip::address &default_name_server,
81 const int resolved_ip_ttl_threshold,
82 const int max_address_resolution_attempts,
83 const DnsCacheItem &cache);
85 static void create_master(const IoServiceItem &io_serv,
86 const boost::asio::ip::address &default_name_server,
87 const int resolved_ip_ttl_threshold,
88 const int max_address_resolution_attempts,
89 const std::string &cache_file);
90 static DnsMasterItem& get_instance();
93 // storage of global variables
95 //boost::asio::ip::address &get_name_server() const; // currently unused
96 int get_resolved_ip_ttl_threshold() const;
97 int get_max_address_resolution_attempts() const;
101 IoServiceItem IoService;
102 const boost::asio::ip::address DefaultNameServer;
103 const int ResolvedIpTtlThreshold;
104 const int MaxAddressResolutionAttempts;
106 resolver_map_type ResolverMap;
108 // internal helper functions
110 bool is_ip(const std::string &hostname) const;
111 static DnsIpProtocol ping2dns_protocol(const PingProtocol& pprot);
116 // (created using vim -- the world's best text editor)