added clean-up to DnsCache: will remove very old entries after 60 days
[pingcheck] / src / dns / dnscache.h
CommitLineData
96779587
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
23#ifndef DNS_CACHE_H
24#define DNS_CACHE_H
25
26#include <map>
27
28#include <boost/shared_ptr.hpp>
29#include <boost/asio/deadline_timer.hpp>
30#include <boost/system/error_code.hpp>
23f51766
CH
31#include <boost/archive/xml_oarchive.hpp>
32#include <boost/archive/xml_iarchive.hpp>
77319ba8 33#include <boost/date_time/posix_time/posix_time.hpp>
96779587
CH
34
35#include "host/pinger.h" // for IoserviceItem
8f00b3df 36#include "dns/dnsipprotocol.h"
96779587 37#include "dns/hostaddress.h"
8d26221d 38#include "dns/cname.h"
96779587 39
4e7b6ff9 40typedef std::vector<HostAddress> HostAddressVec;
8f00b3df
CH
41typedef std::pair<std::string, DnsIpProtocol> ip_map_key_type;
42typedef std::map<ip_map_key_type, HostAddressVec> ip_map_type;
43typedef std::string cname_map_key_type;
44typedef std::map<cname_map_key_type, Cname> cname_map_type;
dbe986b9 45
23f51766
CH
46// -----------------------------------------------------------------------------
47// DnsCache
48// -----------------------------------------------------------------------------
49
96779587
CH
50class DnsCache
51{
52public:
8d26221d
CH
53 /// constant to give as cache_file arg to DnsCache constructor
54 /// indicating that no cache file should be used
55 static const std::string DoNotUseCacheFile;
56
96779587 57 DnsCache( const IoServiceItem &io_serv,
f833126b
CH
58 const std::string &cache_file,
59 const uint32_t min_time_between_resolves );
96779587
CH
60 ~DnsCache();
61
62 // accessed from ResolverBase subclasses
8f00b3df
CH
63 void update(const std::string &hostname,
64 const DnsIpProtocol &protocol,
65 const HostAddressVec &new_ips);
66 void update(const std::string &hostname,
67 const Cname &cname);
23f51766
CH
68
69 // retrieval
dbe986b9 70 HostAddressVec get_ips(const std::string &hostname,
8f00b3df 71 const DnsIpProtocol &protocol,
23f51766
CH
72 const bool check_up_to_date=false);
73 Cname get_cname(const std::string &hostname,
8f00b3df 74 const bool check_up_to_date=false);
dbe986b9 75 HostAddressVec get_ips_recursive(const std::string &hostname,
8f00b3df 76 const DnsIpProtocol &protocol,
23f51766
CH
77 const bool check_up_to_date=false);
78 std::string get_first_outdated_cname(const std::string &hostname,
79 const uint32_t ttl_thresh);
e638894d 80 std::string get_cname_chain_str(const std::string &hostname);
96779587
CH
81
82// variables
83private:
ad83004d
CH
84 ip_map_type IpCache;
85 cname_map_type CnameCache;
96779587
CH
86 boost::asio::deadline_timer SaveTimer;
87 std::string CacheFile;
88 bool HasChanged;
f833126b 89 uint32_t MinTimeBetweenResolves;
96779587 90
23f51766 91// internal functions
96779587
CH
92private:
93 void schedule_save(const boost::system::error_code &error);
94 void save_to_cachefile();
95 void load_from_cachefile();
8f00b3df
CH
96 ip_map_key_type key_for_ips(const std::string &hostname,
97 const DnsIpProtocol &protocol) const;
98 cname_map_key_type key_for_cname(const std::string &hostname) const;
77319ba8 99 bool check_timestamps(const boost::posix_time::ptime &cache_save_time);
c98b5fcb 100 void remove_old_entries();
96779587
CH
101
102};
103
104typedef boost::shared_ptr<DnsCache> DnsCacheItem;
105
106#endif
107