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