remove the footer saying that vim is the best editor -- anyone knows anyway
[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>
96779587
CH
33
34#include "host/pinger.h" // for IoserviceItem
35#include "dns/hostaddress.h"
8d26221d 36#include "dns/cname.h"
96779587 37
4e7b6ff9 38typedef std::vector<HostAddress> HostAddressVec;
ad83004d 39typedef std::map<std::string, HostAddressVec> ip_map_type;
dbe986b9
CH
40typedef std::map<std::string, Cname> cname_map_type;
41
23f51766
CH
42// -----------------------------------------------------------------------------
43// DnsCache
44// -----------------------------------------------------------------------------
45
96779587
CH
46class DnsCache
47{
48public:
8d26221d
CH
49 /// constant to give as cache_file arg to DnsCache constructor
50 /// indicating that no cache file should be used
51 static const std::string DoNotUseCacheFile;
52
96779587 53 DnsCache( const IoServiceItem &io_serv,
f833126b
CH
54 const std::string &cache_file,
55 const uint32_t min_time_between_resolves );
96779587
CH
56 ~DnsCache();
57
58 // accessed from ResolverBase subclasses
26b0f687 59 void update(const std::string &hostname, const HostAddressVec &new_ips);
dbe986b9 60 void update(const std::string &hostname, const Cname &cname);
23f51766
CH
61
62 // retrieval
dbe986b9 63 HostAddressVec get_ips(const std::string &hostname,
23f51766
CH
64 const bool check_up_to_date=false);
65 Cname get_cname(const std::string &hostname,
66 const bool check_up_to_date=false);
dbe986b9 67 HostAddressVec get_ips_recursive(const std::string &hostname,
23f51766
CH
68 const bool check_up_to_date=false);
69 std::string get_first_outdated_cname(const std::string &hostname,
70 const uint32_t ttl_thresh);
96779587
CH
71
72// variables
73private:
ad83004d
CH
74 ip_map_type IpCache;
75 cname_map_type CnameCache;
96779587
CH
76 boost::asio::deadline_timer SaveTimer;
77 std::string CacheFile;
78 bool HasChanged;
f833126b 79 uint32_t MinTimeBetweenResolves;
96779587 80
23f51766 81// internal functions
96779587
CH
82private:
83 void schedule_save(const boost::system::error_code &error);
84 void save_to_cachefile();
85 void load_from_cachefile();
26b0f687 86 std::string key_for_hostname(const std::string &hostname) const;
96779587
CH
87
88};
89
90typedef boost::shared_ptr<DnsCache> DnsCacheItem;
91
92#endif
93