merged PingRotate into PingScheduler; fixed save/load of cache to/from file; started...
[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>
33#include <boost/serialization/access.hpp>
34#include <boost/serialization/nvp.hpp>
35#include <boost/serialization/split_member.hpp>
36#include <boost/serialization/string.hpp>
96779587
CH
37
38#include "host/pinger.h" // for IoserviceItem
39#include "dns/hostaddress.h"
dbe986b9 40#include "dns/timetolive.h"
96779587 41
4e7b6ff9 42typedef std::vector<HostAddress> HostAddressVec;
ad83004d 43typedef std::map<std::string, HostAddressVec> ip_map_type;
23f51766
CH
44
45// -----------------------------------------------------------------------------
46// CNAME
47// -----------------------------------------------------------------------------
48
49struct Cname
50{ // all public:
51 std::string Host;
52 TimeToLive Ttl;
53
54 Cname();
55 Cname(const std::string &host, const uint32_t ttl);
26b0f687 56 Cname(const std::string &host, const TimeToLive &ttl);
23f51766
CH
57
58 // serialization
59 friend class boost::serialization::access;
60 template<class Archive>
61 void save(Archive & ar, const unsigned int version) const
62 {
63 ar & BOOST_SERIALIZATION_NVP(Host);
64 ar & BOOST_SERIALIZATION_NVP(Ttl);
65 }
66 template<class Archive>
67 void load(Archive & ar, const unsigned int version)
68 {
69 ar & BOOST_SERIALIZATION_NVP(Host);
70 ar & BOOST_SERIALIZATION_NVP(Ttl);
71 }
72 BOOST_SERIALIZATION_SPLIT_MEMBER()
73};
dbe986b9
CH
74typedef std::map<std::string, Cname> cname_map_type;
75
26b0f687
CH
76/// constant to give as cache_file arg to DnsCache constructor
77/// indicating that no cache file should be used
78const std::string DO_NOT_USE_CACHE_FILE = "do not use cache file!";
79
96779587 80
23f51766
CH
81// -----------------------------------------------------------------------------
82// DnsCache
83// -----------------------------------------------------------------------------
84
96779587
CH
85class DnsCache
86{
87public:
88 DnsCache( const IoServiceItem &io_serv,
89 const std::string &cache_file );
90 ~DnsCache();
91
92 // accessed from ResolverBase subclasses
26b0f687 93 void update(const std::string &hostname, const HostAddressVec &new_ips);
dbe986b9 94 void update(const std::string &hostname, const Cname &cname);
23f51766
CH
95
96 // retrieval
dbe986b9 97 HostAddressVec get_ips(const std::string &hostname,
23f51766
CH
98 const bool check_up_to_date=false);
99 Cname get_cname(const std::string &hostname,
100 const bool check_up_to_date=false);
dbe986b9 101 HostAddressVec get_ips_recursive(const std::string &hostname,
23f51766
CH
102 const bool check_up_to_date=false);
103 std::string get_first_outdated_cname(const std::string &hostname,
104 const uint32_t ttl_thresh);
96779587
CH
105
106// variables
107private:
ad83004d
CH
108 ip_map_type IpCache;
109 cname_map_type CnameCache;
96779587
CH
110 boost::asio::deadline_timer SaveTimer;
111 std::string CacheFile;
112 bool HasChanged;
113
23f51766 114// internal functions
96779587
CH
115private:
116 void schedule_save(const boost::system::error_code &error);
117 void save_to_cachefile();
118 void load_from_cachefile();
26b0f687 119 std::string key_for_hostname(const std::string &hostname) const;
96779587
CH
120
121};
122
123typedef boost::shared_ptr<DnsCache> DnsCacheItem;
124
125#endif
126
127// (created using vim -- the world's best text editor)
128