/** @file * @brief EASYDNS Service class implementation. This class represents the EASYDNS service. * * * * @copyright Intra2net AG * @license GPLv2 */ #include "service_easydns.h" #include "util.h" #include #include #include namespace ba = boost::algorithm; using namespace std; /** * Default Constructor, needed for object serialization. */ ServiceEasydns::ServiceEasydns() { } /** * Constructor. * @param _hostname The hostname to update * @param _login The login name. * @param _password The corresponding password. */ ServiceEasydns::ServiceEasydns(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) { if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config) set_update_interval(10); // use default protocol value else set_update_interval(_update_interval); if ( _max_updates_within_interval == -1 ) set_max_updates_within_interval(1); else set_max_updates_within_interval(_max_updates_within_interval); if ( _dns_cache_ttl == -1 ) set_dns_cache_ttl(1200); 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()); string two_level_tld = get_two_level_tld(host_domain_part.back()); if ( !two_level_tld.empty() ) BaseUrl = assemble_base_url(get_hostname(),two_level_tld); else BaseUrl = assemble_base_url(get_hostname(),""); } /** * Default destructor */ ServiceEasydns::~ServiceEasydns() { } /** * Tries to extract the two_level domain part if there is one * @param domain_part The complete domain part. * @return Two_level_domain part if there is one or "" if not so. */ string ServiceEasydns::get_two_level_tld(const string& domain_part) const { // split the domain_part list domain_tokens; ba::split(domain_tokens,domain_part,boost::is_any_of(".")); domain_tokens.pop_front(); if ( domain_tokens.size() > 1 ) { string two_level_tld = domain_tokens.front(); domain_tokens.pop_front(); BOOST_FOREACH(string domain_part, domain_tokens) { two_level_tld.append("."); two_level_tld.append(domain_part); } return two_level_tld; } else { return ""; } } /** * Assemble the easydns 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 ServiceEasydns::assemble_base_url(const string& hostname, const string& two_level_tld) const { string base_url; if ( !two_level_tld.empty() ) { base_url = "https://members.easydns.com"; base_url.append("/dyn/dyndns.php?hostname="); base_url.append(hostname); base_url.append("&tld="); base_url.append(two_level_tld); base_url.append("&myip="); } else { base_url = "https://members.easydns.com"; base_url.append("/dyn/dyndns.php?hostname="); base_url.append(hostname); base_url.append("&myip="); } 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 ServiceEasydns::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. */ int ServiceEasydns::perform_update(const std::string& ip) { string url = BaseUrl; url.append(ip); if ( HTTPHelp->is_initialized() == true ) { 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 easydns update errors if ( http_status_code == 200 ) { // Get the received http data. string curl_data = HTTPHelp->get_curl_data(); string status_code = Util::parse_status_code(curl_data,"\n"); if ( status_code == "NOERROR" ) { return 0; } else if ( status_code == "NOACCESS" ) { get_logger()->print_service_not_authorized(url,get_login(),get_password()); } else { get_logger()->print_update_failure(url, curl_data); } } else { get_logger()->print_update_failure(url,http_status_code); } } else { get_logger()->print_service_not_initialized(url); } return -1; }