272222849beaa57d316af4eb1958b3fd63e7bef3
[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 #include <boost/date_time/posix_time/posix_time.hpp>
32
33 #include "dns/timetolive.h"
34
35 //-----------------------------------------------------------------------------
36 // HostAddress
37 //-----------------------------------------------------------------------------
38
39 class HostAddress
40 {
41 public:
42     HostAddress();
43     HostAddress(
44             const boost::asio::ip::address &ip,
45             uint32_t ttl
46     );
47     ~HostAddress();
48
49     boost::asio::ip::address get_ip() const;
50     void set_ip( const boost::asio::ip::address &ip );
51
52     TimeToLive get_ttl() const;
53     void set_ttl( const TimeToLive &ttl );
54
55 private:
56     /// IP address of the host
57     boost::asio::ip::address Ip;
58     /// time-to-live of the host IP
59     TimeToLive Ttl;
60
61     /// serialization: needs to be implemented here or will not compile
62     // since there is no serialize in ip::address nor ptime,
63     // need to save in other formats here
64     friend class boost::serialization::access;
65     template<class Archive>
66     void save(Archive & ar, const unsigned int version) const
67     {
68         std::string ip = Ip.to_string();
69         std::string now_str = boost::posix_time::to_iso_string(
70                              boost::posix_time::second_clock::universal_time());
71         uint32_t ttl_seconds_now = Ttl.get_updated_value();
72
73         ar & BOOST_SERIALIZATION_NVP(ip);
74         ar & BOOST_SERIALIZATION_NVP(now_str);
75         ar & BOOST_SERIALIZATION_NVP(ttl_seconds_now);
76     }
77
78     template<class Archive>
79     void load(Archive & ar, const unsigned int version)
80     {
81         std::string ip;
82         std::string save_time_str;
83         uint32_t ttl_seconds_at_save;
84         ar & BOOST_SERIALIZATION_NVP(ip);
85         ar & BOOST_SERIALIZATION_NVP(save_time_str);
86         ar & BOOST_SERIALIZATION_NVP(ttl_seconds_at_save);
87
88         // now convert to Ip and Ttl
89         Ip = boost::asio::ip::address::from_string(ip);
90         boost::posix_time::ptime save_time =
91                               boost::posix_time::from_iso_string(save_time_str);
92         boost::posix_time::ptime now =
93                               boost::posix_time::second_clock::universal_time();
94         uint32_t elapsed_seconds_since_save = (save_time - now).total_seconds();
95         if (elapsed_seconds_since_save > ttl_seconds_at_save)
96             Ttl = TimeToLive(0);
97         else
98             Ttl = TimeToLive(ttl_seconds_at_save - elapsed_seconds_since_save);
99     }
100
101     BOOST_SERIALIZATION_SPLIT_MEMBER()
102 };
103
104 //-----------------------------------------------------------------------------
105 // HostAddressList
106 //-----------------------------------------------------------------------------
107
108 typedef std::list<HostAddress> HostAddressList;
109
110 #endif // HOST_ADDRESS_H