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 _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(7200);
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,_login,_password));
67 // extract domain part from hostname
68 list<string> host_domain_part = separate_domain_and_host_part(get_hostname());
70 BaseUrl = assemble_base_url(host_domain_part.front(),host_domain_part.back()); /*lint !e864 */
77 ServiceDhs::~ServiceDhs()
83 * Assemble the dhs update url from the given hostname and domain part
84 * @param hostname The hostname to update IP for.
85 * @param domain_part The domain_part in which the hostname is located.
86 * @return The assembled update url without IP.
88 string ServiceDhs::assemble_base_url(const string& hostname, const string& domain_part) const
92 base_url = "http://members.dhs.org";
93 base_url.append("/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&domain=");
94 base_url.append(domain_part);
95 base_url.append("&hostname=");
96 base_url.append(hostname);
97 base_url.append("&updatetype=Online&ip=");
104 * Separates the hostname from the domain part.
105 * @param fqdn Hostname with domain part.
106 * @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.
108 list<string> ServiceDhs::separate_domain_and_host_part(const string& fqdn) const
110 list<string> splitted;
111 ba::split(splitted,fqdn,boost::is_any_of("."));
113 if ( splitted.size() > 1 )
115 string host = splitted.front();
116 splitted.pop_front();
118 string domain = splitted.front();
119 splitted.pop_front();
121 BOOST_FOREACH(string domain_part, splitted)
124 domain.append(domain_part);
128 splitted.push_back(host);
129 splitted.push_back(domain);
137 * Performs the Service update.
138 * @param ip IP Address to set.
139 * @return 0 if all is fine, -1 otherwise.
141 int ServiceDhs::perform_update(const std::string& ip)
143 string url = BaseUrl;
146 if ( HTTPHelp->is_initialized() )
148 // Perform curl operation on given url.
149 long http_status_code = HTTPHelp->http_get(url);
151 get_logger()->print_http_status_code(url,http_status_code);
153 // Check the status code for protocol errors.
154 if ( http_status_code == 200 )
156 // Get the received http data.
157 string curl_data = HTTPHelp->get_curl_data();
159 // Search for the following string in the received http data
160 boost::regex expr_done("Updating ip on .*: done");
161 if ( boost::regex_search(curl_data,expr_done) )
168 get_logger()->print_update_failure(url,curl_data);
171 else if ( http_status_code == 401 )
173 get_logger()->print_service_not_authorized(url,get_login(),get_password());
177 get_logger()->print_update_failure(url,http_status_code);
182 get_logger()->print_httphelper_not_initialized();
183 HTTPHelp->re_initialize();