2 * @brief DHS Service class implementation. This class represents the DHS service.
6 * @copyright Intra2net AG
10 #include "service_dhs.hpp"
13 #include <boost/foreach.hpp>
14 #include <boost/algorithm/string.hpp>
15 #include <boost/regex.hpp>
17 namespace ba = boost::algorithm;
23 * Default Constructor, needed for object serialization.
25 ServiceDhs::ServiceDhs()
32 * @param _hostname The hostname to update
33 * @param _login The login name.
34 * @param _password The corresponding password.
36 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 _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 ( _dns_cache_ttl == -1 )
49 set_dns_cache_ttl(7200);
51 set_dns_cache_ttl(_dns_cache_ttl);
53 set_protocol(_protocol);
54 set_hostname(_hostname);
56 set_password(_password);
59 // create http helper class
60 HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
62 // extract domain part from hostname
63 list<string> host_domain_part = separate_domain_and_host_part(get_hostname());
65 BaseUrl = assemble_base_url(host_domain_part.front(),host_domain_part.back()); /*lint !e864 */
72 ServiceDhs::~ServiceDhs()
78 * Assemble the dhs update url from the given hostname and domain part
79 * @param hostname The hostname to update IP for.
80 * @param domain_part The domain_part in which the hostname is located.
81 * @return The assembled update url without IP.
83 string ServiceDhs::assemble_base_url(const string& hostname, const string& domain_part) const
87 base_url = "http://members.dhs.org";
88 base_url.append("/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&domain=");
89 base_url.append(domain_part);
90 base_url.append("&hostname=");
91 base_url.append(hostname);
92 base_url.append("&updatetype=Online&ip=");
99 * Separates the hostname from the domain part.
100 * @param fqdn Hostname with domain part.
101 * @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.
103 list<string> ServiceDhs::separate_domain_and_host_part(const string& fqdn) const
105 list<string> splitted;
106 ba::split(splitted,fqdn,boost::is_any_of("."));
108 if ( splitted.size() > 1 )
110 string host = splitted.front();
111 splitted.pop_front();
113 string domain = splitted.front();
114 splitted.pop_front();
116 BOOST_FOREACH(string domain_part, splitted)
119 domain.append(domain_part);
123 splitted.push_back(host);
124 splitted.push_back(domain);
132 * Performs the Service update.
133 * @param ip IP Address to set.
134 * @return 0 if all is fine, -1 otherwise.
136 int ServiceDhs::perform_update(const std::string& ip)
138 string url = BaseUrl;
141 if ( HTTPHelp->is_initialized() )
143 // Perform curl operation on given url.
144 long http_status_code = HTTPHelp->http_get(url);
146 get_logger()->print_http_status_code(url,http_status_code);
148 // Check the status code for protocol errors.
149 if ( http_status_code == 200 )
151 // Get the received http data.
152 string curl_data = HTTPHelp->get_curl_data();
154 // Search for the following string in the received http data
155 boost::regex expr_done("Updating ip on .*: done");
156 if ( boost::regex_search(curl_data,expr_done) )
163 get_logger()->print_update_failure(url,curl_data);
166 else if ( http_status_code == 401 )
168 get_logger()->print_service_not_authorized(url,get_login(),get_password());
172 get_logger()->print_update_failure(url,http_status_code);
177 get_logger()->print_httphelper_not_initialized();
178 HTTPHelp->re_initialize();