2 * @brief EASYDNS Service class implementation. This class represents the EASYDNS service.
6 * @copyright Intra2net AG
10 #include "service_easydns.hpp"
14 #include <boost/foreach.hpp>
15 #include <boost/algorithm/string.hpp>
17 namespace ba = boost::algorithm;
23 * Default Constructor, needed for object serialization.
25 ServiceEasydns::ServiceEasydns()
32 * @param _hostname The hostname to update
33 * @param _login The login name.
34 * @param _password The corresponding password.
36 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 _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(10); // use default protocol value
41 set_update_interval(_update_interval);
43 if ( _max_updates_within_interval == -1 )
44 set_max_updates_within_interval(1);
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(1200);
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 string two_level_tld = get_two_level_tld(host_domain_part.back());
71 if ( !two_level_tld.empty() )
72 BaseUrl = assemble_base_url(get_hostname(),two_level_tld);
74 BaseUrl = assemble_base_url(get_hostname(),"");
81 ServiceEasydns::~ServiceEasydns()
87 * Tries to extract the two_level domain part if there is one
88 * @param domain_part The complete domain part.
89 * @return Two_level_domain part if there is one or "" if not so.
91 string ServiceEasydns::get_two_level_tld(const string& domain_part) const
93 // split the domain_part
94 list<string> domain_tokens;
95 ba::split(domain_tokens,domain_part,boost::is_any_of("."));
97 domain_tokens.pop_front();
99 if ( domain_tokens.size() > 1 )
101 string two_level_tld = domain_tokens.front();
102 domain_tokens.pop_front();
104 BOOST_FOREACH(string domain_part, domain_tokens)
106 two_level_tld.append(".");
107 two_level_tld.append(domain_part);
110 return two_level_tld;
120 * Assemble the easydns update url from the given hostname and domain part
121 * @param hostname The hostname to update IP for.
122 * @param domain_part The domain_part in which the hostname is located.
123 * @return The assembled update url without IP.
125 string ServiceEasydns::assemble_base_url(const string& hostname, const string& two_level_tld) const
128 if ( !two_level_tld.empty() )
130 base_url = "https://members.easydns.com";
131 base_url.append("/dyn/dyndns.php?hostname=");
132 base_url.append(hostname);
133 base_url.append("&tld=");
134 base_url.append(two_level_tld);
135 base_url.append("&myip=");
139 base_url = "https://members.easydns.com";
140 base_url.append("/dyn/dyndns.php?hostname=");
141 base_url.append(hostname);
142 base_url.append("&myip=");
149 * Separates the hostname from the domain part.
150 * @param fqdn Hostname with domain part.
151 * @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.
153 list<string> ServiceEasydns::separate_domain_and_host_part(const string& fqdn) const
155 list<string> splitted;
156 ba::split(splitted,fqdn,boost::is_any_of("."));
158 if ( splitted.size() > 1 )
160 string host = splitted.front();
161 splitted.pop_front();
163 string domain = splitted.front();
164 splitted.pop_front();
166 BOOST_FOREACH(string domain_part, splitted)
169 domain.append(domain_part);
173 splitted.push_back(host);
174 splitted.push_back(domain);
182 * Performs the Service update.
183 * @param ip IP Address to set.
184 * @return 0 if all is fine, -1 otherwise.
186 int ServiceEasydns::perform_update(const std::string& ip)
188 string url = BaseUrl;
191 if ( HTTPHelp->is_initialized() )
193 long http_status_code = HTTPHelp->http_get(url);
195 get_logger()->print_http_status_code(url,http_status_code);
197 // HTTP operation completed successful.
198 // Now we have to parse the data received by curl,
199 // cause http status code is not significant for easydns update errors
200 if ( http_status_code == 200 )
202 // Get the received http data.
203 string curl_data = HTTPHelp->get_curl_data();
204 string status_code = Util::parse_status_code(curl_data,"\n");
206 if ( status_code == "NOERROR" )
210 else if ( status_code == "NOACCESS" )
212 get_logger()->print_service_not_authorized(url,get_login(),get_password());
216 get_logger()->print_update_failure(url, curl_data);
221 get_logger()->print_update_failure(url,http_status_code);
226 get_logger()->print_httphelper_not_initialized();
227 HTTPHelp->re_initialize();