merged PingRotate into PingScheduler; fixed save/load of cache to/from file; started...
[pingcheck] / src / dns / hostaddress.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 #ifndef HOST_ADDRESS_H
21 #define HOST_ADDRESS_H
22
23 #include <stdint.h>
24
25 #include <list>
26
27 #include <boost/asio.hpp>
28 #include <boost/serialization/access.hpp>
29 #include <boost/serialization/nvp.hpp>
30 #include <boost/serialization/split_member.hpp>
31
32 #include "dns/timetolive.h"
33
34 //-----------------------------------------------------------------------------
35 // HostAddress
36 //-----------------------------------------------------------------------------
37
38 class HostAddress
39 {
40 public:
41     HostAddress();
42     HostAddress(
43             const boost::asio::ip::address &ip,
44             uint32_t ttl
45     );
46     ~HostAddress();
47
48     boost::asio::ip::address get_ip() const;
49     void set_ip( const boost::asio::ip::address &ip );
50
51     TimeToLive get_ttl() const;
52     void set_ttl( const TimeToLive &ttl );
53
54     // return false if this HostAddress was default-constructed, so does not
55     // actually point to an ip (ip is probably 0.0.0.0)
56     bool is_valid() const;
57
58 private:
59     /// IP address of the host
60     boost::asio::ip::address Ip;
61     /// time-to-live of the host IP
62     TimeToLive Ttl;
63
64     /// serialization: needs to be implemented here or will not compile
65     // since there is no serialize in ip::address nor ptime,
66     // need to save in other formats here
67     friend class boost::serialization::access;
68     template<class Archive>
69     void save(Archive & ar, const unsigned int version) const
70     {
71         std::string ip_str = Ip.to_string();
72         ar & BOOST_SERIALIZATION_NVP(ip_str);
73         ar & BOOST_SERIALIZATION_NVP(Ttl);
74     }
75
76     template<class Archive>
77     void load(Archive & ar, const unsigned int version)
78     {
79         std::string ip_str;
80         ar & BOOST_SERIALIZATION_NVP(ip_str);
81         ar & BOOST_SERIALIZATION_NVP(Ttl);
82         Ip = boost::asio::ip::address::from_string(ip_str);
83
84     }
85
86     BOOST_SERIALIZATION_SPLIT_MEMBER()
87 };
88
89 //-----------------------------------------------------------------------------
90 // HostAddressList
91 //-----------------------------------------------------------------------------
92
93 typedef std::list<HostAddress> HostAddressList;
94
95 #endif // HOST_ADDRESS_H