/** @file * @brief ZONEEDIT Service class implementation. This class represents the ZONEEDIT service. * * * * @copyright Intra2net AG * @license GPLv2 */ #include "service_zoneedit.hpp" #include "util.hpp" #include #include #include namespace ba = boost::algorithm; using namespace std; /** * Default Constructor, needed for object serialization. */ ServiceZoneedit::ServiceZoneedit() { } /** * Constructor. * @param _hostname The hostname to update * @param _login The login name. * @param _password The corresponding password. */ 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) { if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config) set_update_interval(15); // use default protocol value else set_update_interval(_update_interval); if ( _max_updates_within_interval == -1 ) set_max_updates_within_interval(3); else set_max_updates_within_interval(_max_updates_within_interval); if ( _max_equal_updates_in_succession == -1 ) set_max_equal_updates_in_succession(2); else set_max_equal_updates_in_succession(_max_equal_updates_in_succession); if ( _dns_cache_ttl == -1 ) set_dns_cache_ttl(60); else set_dns_cache_ttl(_dns_cache_ttl); set_protocol(_protocol); set_hostname(_hostname); set_login(_login); set_password(_password); set_logger(_logger); // create http helper class HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port)); BaseUrl = assemble_base_url(get_hostname()); } /** * Default destructor */ ServiceZoneedit::~ServiceZoneedit() { } /** * Assemble the zoneedit update url from the given fqhn * @param hostname The fqhn hostname to update IP for. * @return The assembled update url without IP. */ string ServiceZoneedit::assemble_base_url(const string& fqhn) const { string base_url; base_url = "http://dynamic.zoneedit.com"; base_url.append("/auth/dynamic.html?host="); base_url.append(fqhn); base_url.append("&dnsto="); return base_url; } /** * Performs the Service update. * @param ip IP Address to set. * @return UpdateOk if all is fine, GenericError otherwise. */ Service::UpdateErrorCode ServiceZoneedit::perform_update(const std::string& ip) { string url = BaseUrl; url.append(ip); if ( HTTPHelp->is_initialized() ) { long http_status_code = HTTPHelp->http_get(url); get_logger()->print_http_status_code(url,http_status_code); // HTTP operation completed successful. // Now we have to parse the data received by curl, // cause http status code is not significant for dyns update errors if ( http_status_code == 200 ) { // Get the received http data and parse the status code. string curl_data = HTTPHelp->get_curl_data(); string status_code = Util::parse_status_code(curl_data); if ( status_code == "200" ) { return UpdateOk; } else { get_logger()->print_update_failure(url,curl_data); return UpdateError; } } else if ( http_status_code == 401 ) { get_logger()->print_service_not_authorized(url,get_login(),get_password()); return NotAuth; } else { get_logger()->print_update_failure(url,http_status_code); } } else { get_logger()->print_httphelper_not_initialized(); HTTPHelp->re_initialize(); } return GenericError; }