Commit | Line | Data |
---|---|---|
089a7152 BS |
1 | /** @file |
2 | * @brief EASYDNS Service class implementation. This class represents the EASYDNS service. | |
3 | * | |
4 | * | |
5 | * | |
6 | * @copyright Intra2net AG | |
7 | * @license GPLv2 | |
8 | */ | |
9 | ||
10 | #include "service_easydns.h" | |
11 | ||
12 | #include <time.h> | |
13 | #include <boost/foreach.hpp> | |
14 | #include <boost/algorithm/string.hpp> | |
15 | ||
16 | namespace ba = boost::algorithm; | |
17 | ||
18 | using namespace std; | |
19 | ||
20 | ||
21 | /** | |
22 | * Default Constructor, needed for object serialization. | |
23 | */ | |
629d8110 | 24 | ServiceEasydns::ServiceEasydns() |
089a7152 BS |
25 | { |
26 | } | |
27 | ||
28 | ||
29 | /** | |
30 | * Constructor. | |
31 | * @param _hostname The hostname to update | |
32 | * @param _login The login name. | |
33 | * @param _password The corresponding password. | |
34 | */ | |
629d8110 | 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) |
089a7152 BS |
36 | { |
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 | |
39 | else | |
40 | set_update_interval(_update_interval); | |
41 | ||
42 | if ( _max_updates_within_interval == -1 ) | |
43 | set_max_updates_within_interval(1); | |
44 | else | |
45 | set_max_updates_within_interval(_max_updates_within_interval); | |
46 | ||
47 | if ( _dns_cache_ttl == -1 ) | |
48 | set_dns_cache_ttl(1200); | |
49 | else | |
50 | set_dns_cache_ttl(_dns_cache_ttl); | |
51 | ||
52 | set_protocol(_protocol); | |
53 | set_hostname(_hostname); | |
54 | set_login(_login); | |
55 | set_password(_password); | |
56 | set_logger(_logger); | |
57 | ||
58 | // create http helper class | |
59 | HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password)); | |
60 | HTTPHelp = _http_help; | |
61 | _http_help.reset(); | |
62 | ||
63 | // extract domain part from hostname | |
64 | list<string> host_domain_part = separate_domain_and_host_part(get_hostname()); | |
65 | ||
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); | |
69 | else | |
70 | BaseUrl = assemble_base_url(get_hostname(),""); | |
71 | } | |
72 | ||
73 | ||
74 | /** | |
75 | * Default destructor | |
76 | */ | |
629d8110 | 77 | ServiceEasydns::~ServiceEasydns() |
089a7152 BS |
78 | { |
79 | } | |
80 | ||
81 | ||
82 | /** | |
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. | |
86 | */ | |
629d8110 | 87 | string ServiceEasydns::get_two_level_tld(const string& domain_part) const |
089a7152 BS |
88 | { |
89 | // TODO: There is a list with all two level TLD's, use it | |
90 | ||
91 | // split the domain_part | |
92 | list<string> domain_tokens; | |
93 | ba::split(domain_tokens,domain_part,boost::is_any_of(".")); | |
94 | ||
95 | domain_tokens.pop_front(); | |
96 | ||
97 | if ( domain_tokens.size() > 1 ) | |
98 | { | |
99 | string two_level_tld = domain_tokens.front(); | |
100 | domain_tokens.pop_front(); | |
101 | ||
102 | BOOST_FOREACH(string domain_part, domain_tokens) | |
103 | { | |
104 | two_level_tld.append("."); | |
105 | two_level_tld.append(domain_part); | |
106 | } | |
107 | ||
108 | return two_level_tld; | |
109 | } | |
110 | else | |
111 | { | |
112 | return ""; | |
113 | } | |
114 | } | |
115 | ||
116 | ||
117 | /** | |
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. | |
122 | */ | |
629d8110 | 123 | string ServiceEasydns::assemble_base_url(const string& hostname, const string& two_level_tld) const |
089a7152 BS |
124 | { |
125 | string base_url; | |
126 | if ( !two_level_tld.empty() ) | |
127 | { | |
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="); | |
134 | } | |
135 | else | |
136 | { | |
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="); | |
141 | } | |
142 | return base_url; | |
143 | } | |
144 | ||
145 | ||
146 | /** | |
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. | |
150 | */ | |
629d8110 | 151 | list<string> ServiceEasydns::separate_domain_and_host_part(const string& fqdn) const |
089a7152 BS |
152 | { |
153 | list<string> splitted; | |
154 | ba::split(splitted,fqdn,boost::is_any_of(".")); | |
155 | ||
156 | if ( splitted.size() > 1 ) | |
157 | { | |
158 | string host = splitted.front(); | |
159 | splitted.pop_front(); | |
160 | ||
161 | string domain = splitted.front(); | |
162 | splitted.pop_front(); | |
163 | ||
164 | BOOST_FOREACH(string domain_part, splitted) | |
165 | { | |
166 | domain.append("."); | |
167 | domain.append(domain_part); | |
168 | } | |
169 | ||
170 | splitted.clear(); | |
171 | splitted.push_back(host); | |
172 | splitted.push_back(domain); | |
173 | } | |
174 | ||
175 | return splitted; | |
176 | } | |
177 | ||
178 | ||
179 | /** | |
180 | * Performs the Service update. | |
181 | * @param ip IP Address to set. | |
182 | * @return 0 if all is fine, -1 otherwise. | |
183 | */ | |
629d8110 | 184 | int ServiceEasydns::perform_update(const std::string& ip) |
089a7152 BS |
185 | { |
186 | int ret_val = -1; | |
187 | ||
188 | string url = BaseUrl; | |
189 | url.append(ip); | |
190 | ||
191 | long http_status_code = HTTPHelp->http_get(url); | |
192 | ||
193 | get_logger()->print_http_status_code(url,http_status_code); | |
194 | ||
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 ) | |
199 | { | |
200 | // Get the received http data. | |
201 | string curl_data = HTTPHelp->get_curl_data(); | |
202 | ||
203 | if ( curl_data == "NOERROR" ) | |
204 | ret_val = 0; | |
205 | else if ( curl_data == "NOACCESS" ) | |
206 | get_logger()->print_http_not_authorized(url,get_login(),get_password()); | |
207 | else | |
208 | get_logger()->print_update_failure(url, curl_data); | |
209 | } | |
210 | ||
211 | return ret_val; | |
212 | } | |
213 | ||
214 | ||
215 | /** | |
216 | * Serialize function needed by boost/serialization to define which members should be stored as the object state. | |
217 | * @param ar Archive | |
218 | * @param version Version | |
219 | */ | |
220 | template<class Archive> | |
629d8110 | 221 | void ServiceEasydns::serialize(Archive & ar, const unsigned int version) |
089a7152 BS |
222 | { |
223 | ar & boost::serialization::base_object<Service>(*this); | |
224 | } |