2 * @brief TZO Service class implementation. This class represents the TZO service.
6 * @copyright Intra2net AG
10 #include "service_tzo.h"
13 #include <boost/foreach.hpp>
14 #include <boost/algorithm/string.hpp>
16 namespace ba = boost::algorithm;
22 * Default Constructor, needed for object serialization.
24 SERVICE_TZO::SERVICE_TZO()
31 * @param _hostname The hostname to update
32 * @param _login The login name.
33 * @param _password The corresponding password.
35 SERVICE_TZO::SERVICE_TZO(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 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
40 set_update_interval(_update_interval);
42 if ( _max_updates_within_interval == -1 )
43 set_max_updates_within_interval(0);
45 set_max_updates_within_interval(_max_updates_within_interval);
47 if ( _dns_cache_ttl == -1 )
48 set_dns_cache_ttl(60);
50 set_dns_cache_ttl(_dns_cache_ttl);
52 set_protocol(_protocol);
53 set_hostname(_hostname);
55 set_password(_password);
58 // create http helper class
59 HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port));
60 HTTPHelp = _http_help;
63 BaseUrl = assemble_base_url(get_hostname(),get_login(),get_password());
70 SERVICE_TZO::~SERVICE_TZO()
76 * Assemble the dyns update url from the given fqhn
77 * @param hostname The fqhn hostname to update IP for.
78 * @param username The username to use.
79 * @param hostname The password to use.
80 * @return The assembled update url without IP.
82 string SERVICE_TZO::assemble_base_url(const string& fqhn, const string& username, const string& password) const
86 base_url = "http://rh.tzo.com";
87 base_url.append("/webclient/tzoperl.html?system=tzodns&info=1&tzoname=");
88 base_url.append(fqhn);
89 base_url.append("&email=");
90 base_url.append(username);
91 base_url.append("&tzokey=");
92 base_url.append(password);
93 base_url.append("&ipaddress=");
100 * Performs the Service update.
101 * @param ip IP Address to set.
102 * @return 0 if all is fine, -1 otherwise.
104 int SERVICE_TZO::perform_update(const std::string& ip)
108 string url = BaseUrl;
111 long http_status_code = HTTPHelp->http_get(url);
113 get_logger()->print_http_status_code(url,http_status_code);
115 // HTTP operation completed successful.
116 // Now we have to parse the data received by curl,
117 // cause http status code is not significant for dyns update errors
118 if ( http_status_code == 200 )
120 // Get the received http data and parse the status code.
121 string curl_data = HTTPHelp->get_curl_data();
122 string status_code = parse_status_code(curl_data);
124 if ( status_code == "200" )
126 else if ( status_code == "401" )
127 get_logger()->print_http_not_authorized(url,get_login(),get_password());
129 get_logger()->print_update_failure(url,curl_data);
137 * Get the status code from the returned http data
138 * @param data The returned http data.
139 * @return The status code.
141 string SERVICE_TZO::parse_status_code(const string& data) const
144 ba::split(tokens,data,boost::is_any_of(" "));
145 return tokens.front();
150 * Serialize function needed by boost/serialization to define which members should be stored as the object state.
152 * @param version Version
154 template<class Archive>
155 void SERVICE_TZO::serialize(Archive & ar, const unsigned int version)
157 ar & boost::serialization::base_object<Service>(*this);