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 = _http_help;
63 // extract domain part from hostname
64 list<string> host_domain_part = separate_domain_and_host_part(get_hostname());
66 string two_level_tld = get_two_level_tld(host_domain_part.back());
67 if ( !two_level_tld.empty() )
68 BaseUrl = assemble_base_url(get_hostname(),two_level_tld);
70 BaseUrl = assemble_base_url(get_hostname(),"");
77 ServiceEasydns::~ServiceEasydns()
83 * Tries to extract the two_level domain part if there is one
84 * @param domain_part The complete domain part.
85 * @return Two_level_domain part if there is one or "" if not so.
87 string ServiceEasydns::get_two_level_tld(const string& domain_part) const
89 // TODO: There is a list with all two level TLD's, use it
91 // split the domain_part
92 list<string> domain_tokens;
93 ba::split(domain_tokens,domain_part,boost::is_any_of("."));
95 domain_tokens.pop_front();
97 if ( domain_tokens.size() > 1 )
99 string two_level_tld = domain_tokens.front();
100 domain_tokens.pop_front();
102 BOOST_FOREACH(string domain_part, domain_tokens)
104 two_level_tld.append(".");
105 two_level_tld.append(domain_part);
108 return two_level_tld;
118 * Assemble the easydns update url from the given hostname and domain part
119 * @param hostname The hostname to update IP for.
120 * @param domain_part The domain_part in which the hostname is located.
121 * @return The assembled update url without IP.
123 string ServiceEasydns::assemble_base_url(const string& hostname, const string& two_level_tld) const
126 if ( !two_level_tld.empty() )
128 base_url = "https://members.easydns.com";
129 base_url.append("/dyn/dyndns.php?hostname=");
130 base_url.append(hostname);
131 base_url.append("&tld=");
132 base_url.append(two_level_tld);
133 base_url.append("&myip=");
137 base_url = "https://members.easydns.com";
138 base_url.append("/dyn/dyndns.php?hostname=");
139 base_url.append(hostname);
140 base_url.append("&myip=");
147 * Separates the hostname from the domain part.
148 * @param fqdn Hostname with domain part.
149 * @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.
151 list<string> ServiceEasydns::separate_domain_and_host_part(const string& fqdn) const
153 list<string> splitted;
154 ba::split(splitted,fqdn,boost::is_any_of("."));
156 if ( splitted.size() > 1 )
158 string host = splitted.front();
159 splitted.pop_front();
161 string domain = splitted.front();
162 splitted.pop_front();
164 BOOST_FOREACH(string domain_part, splitted)
167 domain.append(domain_part);
171 splitted.push_back(host);
172 splitted.push_back(domain);
180 * Performs the Service update.
181 * @param ip IP Address to set.
182 * @return 0 if all is fine, -1 otherwise.
184 int ServiceEasydns::perform_update(const std::string& ip)
188 string url = BaseUrl;
191 long http_status_code = HTTPHelp->http_get(url);
193 get_logger()->print_http_status_code(url,http_status_code);
195 // HTTP operation completed successful.
196 // Now we have to parse the data received by curl,
197 // cause http status code is not significant for easydns update errors
198 if ( http_status_code == 200 )
200 // Get the received http data.
201 string curl_data = HTTPHelp->get_curl_data();
203 if ( curl_data == "NOERROR" )
205 else if ( curl_data == "NOACCESS" )
206 get_logger()->print_http_not_authorized(url,get_login(),get_password());
208 get_logger()->print_update_failure(url, curl_data);
216 * Serialize function needed by boost/serialization to define which members should be stored as the object state.
218 * @param version Version
220 template<class Archive>
221 void ServiceEasydns::serialize(Archive & ar, const unsigned int version)
223 ar & boost::serialization::base_object<Service>(*this);