/** @file * @brief DHS Service class implementation. This class represents the DHS service. * * * * @copyright Intra2net AG * @license GPLv2 */ #include "service_dhs.hpp" #include #include #include #include namespace ba = boost::algorithm; using namespace std; /** * Default Constructor, needed for object serialization. */ ServiceDhs::ServiceDhs() { } /** * Constructor. * @param _hostname The hostname to update * @param _login The login name. * @param _password The corresponding password. */ ServiceDhs::ServiceDhs(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(7200); 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,_login,_password)); // extract domain part from hostname list host_domain_part = separate_domain_and_host_part(get_hostname()); BaseUrl = assemble_base_url(host_domain_part.front(),host_domain_part.back()); /*lint !e864 */ } /** * Default destructor */ ServiceDhs::~ServiceDhs() { } /** * Assemble the dhs update url from the given hostname and domain part * @param hostname The hostname to update IP for. * @param domain_part The domain_part in which the hostname is located. * @return The assembled update url without IP. */ string ServiceDhs::assemble_base_url(const string& hostname, const string& domain_part) const { string base_url; base_url = "http://members.dhs.org"; base_url.append("/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&domain="); base_url.append(domain_part); base_url.append("&hostname="); base_url.append(hostname); base_url.append("&updatetype=Online&ip="); return base_url; } /** * Separates the hostname from the domain part. * @param fqdn Hostname with domain part. * @return A list with 2 elements (first element is the hostname, second element the domain part), or a list with 1 element if the domain part couldn't be determined. */ list ServiceDhs::separate_domain_and_host_part(const string& fqdn) const { list splitted; ba::split(splitted,fqdn,boost::is_any_of(".")); if ( splitted.size() > 1 ) { string host = splitted.front(); splitted.pop_front(); string domain = splitted.front(); splitted.pop_front(); BOOST_FOREACH(string domain_part, splitted) { domain.append("."); domain.append(domain_part); } splitted.clear(); splitted.push_back(host); splitted.push_back(domain); } return splitted; } /** * Performs the Service update. * @param ip IP Address to set. * @return 0 if all is fine, -1 otherwise. */ Service::UpdateErrorCode ServiceDhs::perform_update(const std::string& ip) { string url = BaseUrl; url.append(ip); if ( HTTPHelp->is_initialized() ) { // Perform curl operation on given url. long http_status_code = HTTPHelp->http_get(url); get_logger()->print_http_status_code(url,http_status_code); // Check the status code for protocol errors. if ( http_status_code == 200 ) { // Get the received http data. string curl_data = HTTPHelp->get_curl_data(); // Search for the following string in the received http data boost::regex expr_done("Updating ip on .*: done"); if ( boost::regex_search(curl_data,expr_done) ) { // Update successful 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; }