7d71d97a64f7e4c5945dfed88fc055b476e32796
[bpdyndnsd] / src / service.hpp
1 /** @file
2  * @brief The abstract service class. This class represents all services.
3  *
4  *
5  *
6  * @copyright Intra2net AG
7  * @license GPLv2
8 */
9
10 #ifndef SERVICE_H
11 #define SERVICE_H
12
13 #include "logger.hpp"
14
15 #include <boost/archive/text_oarchive.hpp>
16 #include <boost/archive/text_iarchive.hpp>
17
18 #include <boost/shared_ptr.hpp>
19 #include <string>
20 #include <list>
21
22
23 class Service
24 {
25
26 private:
27
28     std::string Protocol;
29     std::string Hostname;
30
31     std::string Login;
32     std::string Password;
33
34     std::string ActualIP;
35     bool ActualIPIsBurnt;
36     bool Activated;
37
38     int UpdateInterval;
39     int MaxUpdatesWithinInterval;
40     int MaxEqualUpdatesInSuccession;
41     int DNSCacheTTL;
42
43     std::map<time_t,std::string> LastUpdates;
44
45     int ErrorCount;
46     time_t ErrorServiceBlockedUntil;
47
48     friend class boost::serialization::access;
49     template<class Archive>
50     void serialize(Archive & ar, const unsigned int version)
51     {
52         // protocol and hostname are the unique identifier for each service.
53         ar & Protocol;
54         ar & Hostname;
55         // lastupdated and actual_ip must also be serialized, cause these are essential infos of each service.
56         ar & LastUpdates;
57         ar & ActualIP;
58         ar & UpdateInterval;
59         ar & MaxUpdatesWithinInterval;
60         ar & MaxEqualUpdatesInSuccession;
61         ar & DNSCacheTTL;
62         ar & Activated;
63     }
64
65     Logger::Ptr Log;
66
67 protected:
68
69     enum UpdateErrorCode { UpdateOk=0, UpdateError=1, NoChange=2, Blocked=3, NotAuth=4, GenericError=4 };
70     virtual UpdateErrorCode perform_update(const std::string& ip) = 0;
71
72     // UpdateOk:        Update was successful
73     // UpdateError:     Unspecified error code returned from update server
74     // NoChange:        IP was not changed_to_online
75     // Blocked:         Client blocked due to abusive updates
76     // NotAuth:         Authentication was not successful
77     // GenericError:    Generic error. For example update server not reached, wrong hostname, not authenticated or network error.
78     //                  Generic error indicates that the update server couldn't associate the update request to any hostname.
79
80 public:
81
82     typedef boost::shared_ptr<Service> Ptr;
83
84     Service();
85
86     virtual ~Service();
87
88     void update(const std::string& ip, const time_t current_time, bool changed_to_online);
89
90     bool update_allowed(const time_t current_time, bool changed_to_online, const std::string& ip_host);
91
92     void set_last_update(const time_t current_time, const std::string& ip);
93
94     time_t get_last_update_time();
95
96     int get_update_interval()const;
97     void set_update_interval(const int _update_interval);
98
99     int get_max_updates_within_interval() const;
100     void set_max_updates_within_interval(const int _max_updates_within_interval);
101
102     int get_max_equal_updates_in_succession() const;
103     void set_max_equal_updates_in_succession(const int _max_equal_updates_in_succession);
104
105     int get_dns_cache_ttl() const;
106     void set_dns_cache_ttl(const int _dns_cache_ttl);
107
108     void set_protocol(const std::string& _protocol);
109     std::string get_protocol() const;
110
111     void set_hostname(const std::string& _hostname);
112     std::string get_hostname() const;
113
114     void set_login(const std::string& _login);
115     std::string get_login() const;
116
117     void set_password(const std::string& _password);
118     std::string get_password() const;
119
120     void set_last_updates(std::map<time_t,std::string> _last_updates);
121     const std::map<time_t,std::string> get_last_updates() const;
122
123     void set_actual_ip(const std::string& _actual_ip);
124     std::string get_actual_ip() const;
125
126     void set_logger(const Logger::Ptr& _log);
127     Logger::Ptr get_logger() const;
128
129     std::string get_service_name() const;
130
131     void set_activated();
132     bool get_activated() const;
133
134     bool operator== (const Service& other) const;
135     bool operator!= (const Service& other) const;
136 };
137
138 #endif