Refactoring: Class renaming. All service implementaion classes should start with...
[bpdyndnsd] / src / service_zoneedit.cpp
1 /** @file
2  * @brief ZONEEDIT Service class implementation. This class represents the ZONEEDIT service.
3  *
4  *
5  *
6  * @copyright Intra2net AG
7  * @license GPLv2
8 */
9
10 #include "service_zoneedit.h"
11
12 #include <time.h>
13 #include <boost/foreach.hpp>
14 #include <boost/algorithm/string.hpp>
15
16 namespace ba = boost::algorithm;
17
18 using namespace std;
19
20
21 /**
22  * Default Constructor, needed for object serialization.
23  */
24 SERVICE_ZONEEDIT::SERVICE_ZONEEDIT()
25 {
26 }
27
28
29 /**
30  * Constructor.
31  * @param _hostname The hostname to update
32  * @param _login The login name.
33  * @param _password The corresponding password.
34  */
35 SERVICE_ZONEEDIT::SERVICE_ZONEEDIT(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const Logger::Ptr& _logger, const int _update_interval, const int _max_updates_within_interval, const int _dns_cache_ttl, const string& _proxy, const int _proxy_port)
36 {
37     if ( _update_interval == -1 )        // If _update_interval is default po::option_desc (not specified via config)
38         set_update_interval(0);              // use default protocol value
39     else
40         set_update_interval(_update_interval);
41
42     if ( _max_updates_within_interval == -1 )
43         set_max_updates_within_interval(0);
44     else
45         set_max_updates_within_interval(_max_updates_within_interval);
46
47     if ( _dns_cache_ttl == -1 )
48         set_dns_cache_ttl(60);
49     else
50         set_dns_cache_ttl(_dns_cache_ttl);
51
52     set_protocol(_protocol);
53     set_hostname(_hostname);
54     set_login(_login);
55     set_password(_password);
56     set_logger(_logger);
57
58     // create http helper class
59     HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port));
60     HTTPHelp = _http_help;
61     _http_help.reset();
62
63     BaseUrl = assemble_base_url(get_hostname());
64 }
65
66
67 /**
68  * Default destructor
69  */
70 SERVICE_ZONEEDIT::~SERVICE_ZONEEDIT()
71 {
72 }
73
74
75 /**
76  * Assemble the zoneedit update url from the given fqhn
77  * @param hostname The fqhn hostname to update IP for.
78  * @return The assembled update url without IP.
79  */
80 string SERVICE_ZONEEDIT::assemble_base_url(const string& fqhn) const
81 {
82     string base_url;
83
84     base_url = "http://dynamic.zoneedit.com";
85     base_url.append("/auth/dynamic.html?host=");
86     base_url.append(fqhn);
87     base_url.append("&dnsto=");
88
89     return base_url;
90 }
91
92
93 /**
94  * Performs the Service update.
95  * @param ip IP Address to set.
96  * @return 0 if all is fine, -1 otherwise.
97  */
98 int SERVICE_ZONEEDIT::perform_update(const std::string& ip)
99 {
100     int ret_val = -1;
101
102     string url = BaseUrl;
103     url.append(ip);
104
105     long http_status_code = HTTPHelp->http_get(url);
106
107     get_logger()->print_http_status_code(url,http_status_code);
108
109     // HTTP operation completed successful.
110     // Now we have to parse the data received by curl,
111     // cause http status code is not significant for dyns update errors
112     if ( http_status_code == 200 )
113     {
114         // Get the received http data and parse the status code.
115         string curl_data = HTTPHelp->get_curl_data();
116         string status_code = parse_status_code(curl_data);
117
118         if ( status_code == "200" )
119             ret_val = 0;
120         else
121             get_logger()->print_update_failure(url,curl_data);
122     }
123     else if ( http_status_code == 401 )
124         get_logger()->print_http_not_authorized(url,get_login(),get_password());
125     else
126         get_logger()->print_update_failure(url,http_status_code);
127
128     return ret_val;
129 }
130
131
132 /**
133  * Get the status code from the returned http data
134  * @param data The returned http data.
135  * @return The status code.
136  */
137 string SERVICE_ZONEEDIT::parse_status_code(const string& data) const
138 {
139     list<string> tokens;
140     ba::split(tokens,data,boost::is_any_of(" "));
141     return tokens.front();
142 }
143
144
145 /**
146  * Serialize function needed by boost/serialization to define which members should be stored as the object state.
147  * @param ar Archive
148  * @param version Version
149  */
150 template<class Archive>
151 void SERVICE_ZONEEDIT::serialize(Archive & ar, const unsigned int version)
152 {
153     ar & boost::serialization::base_object<Service>(*this);
154 }