2 * @brief ZONEEDIT Service class implementation. This class represents the ZONEEDIT service.
6 * @copyright Intra2net AG
10 #include "service_zoneedit.hpp"
14 #include <boost/foreach.hpp>
15 #include <boost/algorithm/string.hpp>
17 namespace ba = boost::algorithm;
23 * Default Constructor, needed for object serialization.
25 ServiceZoneedit::ServiceZoneedit()
32 * @param _hostname The hostname to update
33 * @param _login The login name.
34 * @param _password The corresponding password.
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 _max_equal_updates_in_succession, const int _dns_cache_ttl, const string& _proxy, const int _proxy_port)
38 if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
39 set_update_interval(15); // use default protocol value
41 set_update_interval(_update_interval);
43 if ( _max_updates_within_interval == -1 )
44 set_max_updates_within_interval(3);
46 set_max_updates_within_interval(_max_updates_within_interval);
48 if ( _max_equal_updates_in_succession == -1 )
49 set_max_equal_updates_in_succession(2);
51 set_max_equal_updates_in_succession(_max_equal_updates_in_succession);
53 if ( _dns_cache_ttl == -1 )
54 set_dns_cache_ttl(60);
56 set_dns_cache_ttl(_dns_cache_ttl);
58 set_protocol(_protocol);
59 set_hostname(_hostname);
61 set_password(_password);
64 // create http helper class
65 HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port));
67 BaseUrl = assemble_base_url(get_hostname());
74 ServiceZoneedit::~ServiceZoneedit()
80 * Assemble the zoneedit update url from the given fqhn
81 * @param hostname The fqhn hostname to update IP for.
82 * @return The assembled update url without IP.
84 string ServiceZoneedit::assemble_base_url(const string& fqhn) const
88 base_url = "http://dynamic.zoneedit.com";
89 base_url.append("/auth/dynamic.html?host=");
90 base_url.append(fqhn);
91 base_url.append("&dnsto=");
98 * Performs the Service update.
99 * @param ip IP Address to set.
100 * @return 0 if all is fine, -1 otherwise.
102 int ServiceZoneedit::perform_update(const std::string& ip)
104 string url = BaseUrl;
107 if ( HTTPHelp->is_initialized() )
109 long http_status_code = HTTPHelp->http_get(url);
111 get_logger()->print_http_status_code(url,http_status_code);
113 // HTTP operation completed successful.
114 // Now we have to parse the data received by curl,
115 // cause http status code is not significant for dyns update errors
116 if ( http_status_code == 200 )
118 // Get the received http data and parse the status code.
119 string curl_data = HTTPHelp->get_curl_data();
120 string status_code = Util::parse_status_code(curl_data);
122 if ( status_code == "200" )
128 get_logger()->print_update_failure(url,curl_data);
131 else if ( http_status_code == 401 )
133 get_logger()->print_service_not_authorized(url,get_login(),get_password());
137 get_logger()->print_update_failure(url,http_status_code);
142 get_logger()->print_httphelper_not_initialized();
143 HTTPHelp->re_initialize();