improved information content of logs: in LinkAnalyzer messages add cname chain
[pingcheck] / src / dns / dnsmaster.h
CommitLineData
36ad976b
CH
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
96779587
CH
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
36ad976b
CH
35#ifndef DNS_MASTER_H
36#define DNS_MASTER_H
37
38#include <map>
923626c0 39#include <utility> // pair
36ad976b 40
96779587 41#include <boost/shared_ptr.hpp>
36ad976b 42#include <boost/noncopyable.hpp>
923626c0 43#include <boost/net/dns.hpp>
36ad976b 44
96779587 45#include "host/pinger.h" // for IoserviceItem
923626c0 46#include "host/pingprotocol.h"
8f00b3df 47#include "dns/dnsipprotocol.h"
c5b4902d
CH
48#include "dns/dnscache.h"
49#include "dns/resolverbase.h"
36ad976b
CH
50
51class DnsMaster;
36ad976b 52
96779587 53typedef boost::shared_ptr<DnsMaster> DnsMasterItem;
36ad976b 54
923626c0
CH
55typedef std::pair<std::string, DnsIpProtocol> resolver_key_type;
56typedef std::map<resolver_key_type, ResolverItem> resolver_map_type;
36ad976b 57
dbe986b9
CH
58/**
59 * Factory and Cache of DNS resolvers
60 *
61 * to avoid having several resolvers resolving the same hostname which might
62 * result in conflicts with caching, this class is a singleton factory and the
63 * only place where Resolvers are constructed. They are remembered in an
64 * internal cache by hostname and IP version requested (v4, v6 or both).
65 *
66 * During resolving, several different name servers will have to be queried for
67 * the same hostname. These recursive resolvers are created using
68 * get_recursor_for and are NOT cached, so they should only be used from another
69 * "regular" resolver (created using get_resolver_for)
70 *
71 * The DnsMaster also remembers a few global variables that can be queried
72 * using public getter functions and it creates the DnsCache used by all its
73 * resolvers
74 */
36ad976b
CH
75class DnsMaster : boost::noncopyable
76{
923626c0 77// Resolver factory
96779587 78public:
923626c0
CH
79 ResolverItem& get_resolver_for(const std::string &hostname,
80 const PingProtocol &ping_protocol);
81 ResolverItem& get_resolver_for(const std::string &hostname,
e18c1337 82 const DnsIpProtocol &protocol);
36ad976b
CH
83
84// implementation of singleton
85private:
86 static DnsMasterItem TheOnlyInstance;
87
88 DnsMaster(const IoServiceItem &io_serv,
ad83004d 89 const boost::asio::ip::address &default_name_server,
923626c0
CH
90 const int resolved_ip_ttl_threshold,
91 const int max_address_resolution_attempts,
cd71d095 92 const int max_recursion_count,
4e7b6ff9 93 const DnsCacheItem &cache);
36ad976b 94public:
923626c0 95 static void create_master(const IoServiceItem &io_serv,
ad83004d
CH
96 const boost::asio::ip::address &default_name_server,
97 const int resolved_ip_ttl_threshold,
f833126b 98 const int min_time_between_resolves,
ad83004d 99 const int max_address_resolution_attempts,
cd71d095 100 const int max_recursion_count,
ad83004d 101 const std::string &cache_file);
8d26221d
CH
102 static void create_master(const IoServiceItem &io_serv,
103 const boost::asio::ip::address &default_name_server,
104 const int resolved_ip_ttl_threshold,
105 const int max_address_resolution_attempts,
cd71d095 106 const int max_recursion_count,
8d26221d 107 const DnsCacheItem &cache); // needed for unit test
96779587 108 static DnsMasterItem& get_instance();
36ad976b
CH
109 ~DnsMaster();
110
923626c0
CH
111// storage of global variables
112public:
dbe986b9 113 //boost::asio::ip::address &get_default_name_server() const; // unused
923626c0
CH
114 int get_resolved_ip_ttl_threshold() const;
115 int get_max_address_resolution_attempts() const;
cd71d095 116 int get_max_recursion_count() const;
923626c0 117
e638894d
CH
118 // access to Cache
119 static std::string get_cname_chain_str(const std::string &hostname);
120
36ad976b
CH
121// variables
122private:
123 IoServiceItem IoService;
ad83004d 124 const boost::asio::ip::address DefaultNameServer;
923626c0
CH
125 const int ResolvedIpTtlThreshold;
126 const int MaxAddressResolutionAttempts;
cd71d095 127 const int MaxRecursionCount;
96779587 128 DnsCacheItem Cache;
36ad976b 129 resolver_map_type ResolverMap;
36ad976b 130
923626c0 131// internal helper functions
36ad976b 132private:
96779587 133 bool is_ip(const std::string &hostname) const;
36ad976b
CH
134};
135
136#endif
137