finished self-implementation of DNS resolver recursion; will now remove all that!
[pingcheck] / src / dns / dnsmaster.h
1 /*
2  The software in this package is distributed under the GNU General
3  Public License version 2 (with a special exception described below).
4
5  A copy of GNU General Public License (GPL) is included in this distribution,
6  in the file COPYING.GPL.
7
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.
13
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.
16
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.
19
20  Christian Herdtweck, Intra2net AG 2015
21  */
22
23 /**
24  * Two in one: a DNS resolver factory and a DNS cache
25  *
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
28  *
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
33  */
34
35 #ifndef DNS_MASTER_H
36 #define DNS_MASTER_H
37
38 #include <map>
39 #include <utility>   // pair
40
41 #include <boost/shared_ptr.hpp>
42 #include <boost/noncopyable.hpp>
43 #include <boost/net/dns.hpp>
44
45 #include "host/pinger.h"    // for IoserviceItem
46 #include "host/pingprotocol.h"
47 #include "dns/dnscache.h"
48 #include "dns/resolverbase.h"
49
50 class DnsMaster;
51
52 typedef boost::shared_ptr<DnsMaster> DnsMasterItem;
53
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;
58
59 typedef std::pair<std::string, DnsIpProtocol> resolver_key_type;
60 typedef std::map<resolver_key_type, ResolverItem> resolver_map_type;
61
62 std::string to_string(const DnsIpProtocol &protocol);
63
64 /**
65  * Factory and Cache of DNS resolvers
66  * 
67  * to avoid having several resolvers resolving the same hostname which might
68  * result in conflicts with caching, this class is a singleton factory and the
69  * only place where Resolvers are constructed. They are remembered in an
70  * internal cache by hostname and IP version requested (v4, v6 or both).
71  * 
72  * During resolving, several different name servers will have to be queried for
73  * the same hostname. These recursive resolvers are created using
74  * get_recursor_for and are NOT cached, so they should only be used from another
75  * "regular" resolver (created using get_resolver_for)
76  *
77  * The DnsMaster also remembers a few global variables that can be queried
78  * using public getter functions and it creates the DnsCache used by all its
79  * resolvers
80  */
81 class DnsMaster : boost::noncopyable
82 {
83 // Resolver factory
84 public:
85     ResolverItem& get_resolver_for(const std::string &hostname,
86                                    const PingProtocol &ping_protocol);
87     ResolverItem& get_resolver_for(const std::string &hostname,
88                                    const DnsIpProtocol &protocol);
89     ResolverItem get_recursor_for(const std::string &hostname,
90                                   const DnsIpProtocol &protocol,
91                                   const boost::asio::ip::address &name_server);
92
93 // implementation of singleton
94 private:
95     static DnsMasterItem TheOnlyInstance;
96
97     DnsMaster(const IoServiceItem &io_serv,
98               const boost::asio::ip::address &default_name_server,
99               const int resolved_ip_ttl_threshold,
100               const int max_address_resolution_attempts,
101               const DnsCacheItem &cache);
102 public:
103     static void create_master(const IoServiceItem &io_serv,
104                             const boost::asio::ip::address &default_name_server,
105                             const int resolved_ip_ttl_threshold,
106                             const int max_address_resolution_attempts,
107                             const std::string &cache_file);
108     static DnsMasterItem& get_instance();
109     ~DnsMaster();
110
111 // storage of global variables
112 public:
113     //boost::asio::ip::address &get_default_name_server() const;  // unused
114     int get_resolved_ip_ttl_threshold() const;
115     int get_max_address_resolution_attempts() const;
116
117 // variables
118 private:
119     IoServiceItem IoService;
120     const boost::asio::ip::address DefaultNameServer;
121     const int ResolvedIpTtlThreshold;
122     const int MaxAddressResolutionAttempts;
123     DnsCacheItem Cache;
124     resolver_map_type ResolverMap;
125
126 // internal helper functions
127 private:
128     bool is_ip(const std::string &hostname) const;
129     static DnsIpProtocol ping2dns_protocol(const PingProtocol& pprot);
130 };
131
132 #endif
133
134 // (created using vim -- the world's best text editor)
135