2 * @brief EASYDNS Service class implementation. This class represents the EASYDNS service.
6 * @copyright Intra2net AG
10 #include "service_easydns.h"
13 #include <boost/foreach.hpp>
14 #include <boost/algorithm/string.hpp>
16 namespace ba = boost::algorithm;
22 * Default Constructor, needed for object serialization.
24 ServiceEasydns::ServiceEasydns()
31 * @param _hostname The hostname to update
32 * @param _login The login name.
33 * @param _password The corresponding password.
35 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)
37 if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
38 set_update_interval(10); // use default protocol value
40 set_update_interval(_update_interval);
42 if ( _max_updates_within_interval == -1 )
43 set_max_updates_within_interval(1);
45 set_max_updates_within_interval(_max_updates_within_interval);
47 if ( _dns_cache_ttl == -1 )
48 set_dns_cache_ttl(1200);
50 set_dns_cache_ttl(_dns_cache_ttl);
52 set_protocol(_protocol);
53 set_hostname(_hostname);
55 set_password(_password);
58 // create http helper class
59 HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
60 HTTPHelp.swap(_http_help);
62 // extract domain part from hostname
63 list<string> host_domain_part = separate_domain_and_host_part(get_hostname());
65 string two_level_tld = get_two_level_tld(host_domain_part.back());
66 if ( !two_level_tld.empty() )
67 BaseUrl = assemble_base_url(get_hostname(),two_level_tld);
69 BaseUrl = assemble_base_url(get_hostname(),"");
76 ServiceEasydns::~ServiceEasydns()
82 * Tries to extract the two_level domain part if there is one
83 * @param domain_part The complete domain part.
84 * @return Two_level_domain part if there is one or "" if not so.
86 string ServiceEasydns::get_two_level_tld(const string& domain_part) const
88 // split the domain_part
89 list<string> domain_tokens;
90 ba::split(domain_tokens,domain_part,boost::is_any_of("."));
92 domain_tokens.pop_front();
94 if ( domain_tokens.size() > 1 )
96 string two_level_tld = domain_tokens.front();
97 domain_tokens.pop_front();
99 BOOST_FOREACH(string domain_part, domain_tokens)
101 two_level_tld.append(".");
102 two_level_tld.append(domain_part);
105 return two_level_tld;
115 * Assemble the easydns update url from the given hostname and domain part
116 * @param hostname The hostname to update IP for.
117 * @param domain_part The domain_part in which the hostname is located.
118 * @return The assembled update url without IP.
120 string ServiceEasydns::assemble_base_url(const string& hostname, const string& two_level_tld) const
123 if ( !two_level_tld.empty() )
125 base_url = "https://members.easydns.com";
126 base_url.append("/dyn/dyndns.php?hostname=");
127 base_url.append(hostname);
128 base_url.append("&tld=");
129 base_url.append(two_level_tld);
130 base_url.append("&myip=");
134 base_url = "https://members.easydns.com";
135 base_url.append("/dyn/dyndns.php?hostname=");
136 base_url.append(hostname);
137 base_url.append("&myip=");
144 * Separates the hostname from the domain part.
145 * @param fqdn Hostname with domain part.
146 * @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.
148 list<string> ServiceEasydns::separate_domain_and_host_part(const string& fqdn) const
150 list<string> splitted;
151 ba::split(splitted,fqdn,boost::is_any_of("."));
153 if ( splitted.size() > 1 )
155 string host = splitted.front();
156 splitted.pop_front();
158 string domain = splitted.front();
159 splitted.pop_front();
161 BOOST_FOREACH(string domain_part, splitted)
164 domain.append(domain_part);
168 splitted.push_back(host);
169 splitted.push_back(domain);
177 * Performs the Service update.
178 * @param ip IP Address to set.
179 * @return 0 if all is fine, -1 otherwise.
181 int ServiceEasydns::perform_update(const std::string& ip)
183 string url = BaseUrl;
186 long http_status_code = HTTPHelp->http_get(url);
188 get_logger()->print_http_status_code(url,http_status_code);
190 // HTTP operation completed successful.
191 // Now we have to parse the data received by curl,
192 // cause http status code is not significant for easydns update errors
193 if ( http_status_code == 200 )
195 // Get the received http data.
196 string curl_data = HTTPHelp->get_curl_data();
198 if ( curl_data == "NOERROR" )
202 else if ( curl_data == "NOACCESS" )
204 get_logger()->print_service_not_authorized(url,get_login(),get_password());
208 get_logger()->print_update_failure(url, curl_data);
213 get_logger()->print_update_failure(url,http_status_code);