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" | |
8fb7a083 | 11 | #include "util.h" |
089a7152 BS |
12 | |
13 | #include <time.h> | |
14 | #include <boost/foreach.hpp> | |
15 | #include <boost/algorithm/string.hpp> | |
16 | ||
17 | namespace ba = boost::algorithm; | |
18 | ||
19 | using namespace std; | |
20 | ||
21 | ||
22 | /** | |
23 | * Default Constructor, needed for object serialization. | |
24 | */ | |
629d8110 | 25 | ServiceEasydns::ServiceEasydns() |
089a7152 BS |
26 | { |
27 | } | |
28 | ||
29 | ||
30 | /** | |
31 | * Constructor. | |
32 | * @param _hostname The hostname to update | |
33 | * @param _login The login name. | |
34 | * @param _password The corresponding password. | |
35 | */ | |
629d8110 | 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 _dns_cache_ttl, const string& _proxy, const int _proxy_port) |
089a7152 BS |
37 | { |
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 | |
40 | else | |
41 | set_update_interval(_update_interval); | |
42 | ||
43 | if ( _max_updates_within_interval == -1 ) | |
44 | set_max_updates_within_interval(1); | |
45 | else | |
46 | set_max_updates_within_interval(_max_updates_within_interval); | |
47 | ||
48 | if ( _dns_cache_ttl == -1 ) | |
49 | set_dns_cache_ttl(1200); | |
50 | else | |
51 | set_dns_cache_ttl(_dns_cache_ttl); | |
52 | ||
53 | set_protocol(_protocol); | |
54 | set_hostname(_hostname); | |
55 | set_login(_login); | |
56 | set_password(_password); | |
57 | set_logger(_logger); | |
58 | ||
59 | // create http helper class | |
60 | HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password)); | |
4dfdba68 | 61 | HTTPHelp.swap(_http_help); |
d55fbd67 | 62 | //HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password)); |
31af6a2e | 63 | |
089a7152 BS |
64 | // extract domain part from hostname |
65 | list<string> host_domain_part = separate_domain_and_host_part(get_hostname()); | |
66 | ||
67 | string two_level_tld = get_two_level_tld(host_domain_part.back()); | |
68 | if ( !two_level_tld.empty() ) | |
69 | BaseUrl = assemble_base_url(get_hostname(),two_level_tld); | |
70 | else | |
71 | BaseUrl = assemble_base_url(get_hostname(),""); | |
72 | } | |
73 | ||
74 | ||
75 | /** | |
76 | * Default destructor | |
77 | */ | |
629d8110 | 78 | ServiceEasydns::~ServiceEasydns() |
089a7152 BS |
79 | { |
80 | } | |
81 | ||
82 | ||
83 | /** | |
84 | * Tries to extract the two_level domain part if there is one | |
85 | * @param domain_part The complete domain part. | |
86 | * @return Two_level_domain part if there is one or "" if not so. | |
87 | */ | |
629d8110 | 88 | string ServiceEasydns::get_two_level_tld(const string& domain_part) const |
089a7152 | 89 | { |
089a7152 BS |
90 | // split the domain_part |
91 | list<string> domain_tokens; | |
92 | ba::split(domain_tokens,domain_part,boost::is_any_of(".")); | |
93 | ||
94 | domain_tokens.pop_front(); | |
95 | ||
96 | if ( domain_tokens.size() > 1 ) | |
97 | { | |
98 | string two_level_tld = domain_tokens.front(); | |
99 | domain_tokens.pop_front(); | |
100 | ||
101 | BOOST_FOREACH(string domain_part, domain_tokens) | |
102 | { | |
103 | two_level_tld.append("."); | |
104 | two_level_tld.append(domain_part); | |
105 | } | |
106 | ||
107 | return two_level_tld; | |
108 | } | |
109 | else | |
110 | { | |
111 | return ""; | |
112 | } | |
113 | } | |
114 | ||
115 | ||
116 | /** | |
117 | * Assemble the easydns update url from the given hostname and domain part | |
118 | * @param hostname The hostname to update IP for. | |
119 | * @param domain_part The domain_part in which the hostname is located. | |
120 | * @return The assembled update url without IP. | |
121 | */ | |
629d8110 | 122 | string ServiceEasydns::assemble_base_url(const string& hostname, const string& two_level_tld) const |
089a7152 BS |
123 | { |
124 | string base_url; | |
125 | if ( !two_level_tld.empty() ) | |
126 | { | |
127 | base_url = "https://members.easydns.com"; | |
128 | base_url.append("/dyn/dyndns.php?hostname="); | |
129 | base_url.append(hostname); | |
130 | base_url.append("&tld="); | |
131 | base_url.append(two_level_tld); | |
132 | base_url.append("&myip="); | |
133 | } | |
134 | else | |
135 | { | |
136 | base_url = "https://members.easydns.com"; | |
137 | base_url.append("/dyn/dyndns.php?hostname="); | |
138 | base_url.append(hostname); | |
139 | base_url.append("&myip="); | |
140 | } | |
141 | return base_url; | |
142 | } | |
143 | ||
144 | ||
145 | /** | |
146 | * Separates the hostname from the domain part. | |
147 | * @param fqdn Hostname with domain part. | |
148 | * @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. | |
149 | */ | |
629d8110 | 150 | list<string> ServiceEasydns::separate_domain_and_host_part(const string& fqdn) const |
089a7152 BS |
151 | { |
152 | list<string> splitted; | |
153 | ba::split(splitted,fqdn,boost::is_any_of(".")); | |
154 | ||
155 | if ( splitted.size() > 1 ) | |
156 | { | |
157 | string host = splitted.front(); | |
158 | splitted.pop_front(); | |
159 | ||
160 | string domain = splitted.front(); | |
161 | splitted.pop_front(); | |
162 | ||
163 | BOOST_FOREACH(string domain_part, splitted) | |
164 | { | |
165 | domain.append("."); | |
166 | domain.append(domain_part); | |
167 | } | |
168 | ||
169 | splitted.clear(); | |
170 | splitted.push_back(host); | |
171 | splitted.push_back(domain); | |
172 | } | |
173 | ||
174 | return splitted; | |
175 | } | |
176 | ||
177 | ||
178 | /** | |
179 | * Performs the Service update. | |
180 | * @param ip IP Address to set. | |
181 | * @return 0 if all is fine, -1 otherwise. | |
182 | */ | |
629d8110 | 183 | int ServiceEasydns::perform_update(const std::string& ip) |
089a7152 | 184 | { |
089a7152 BS |
185 | string url = BaseUrl; |
186 | url.append(ip); | |
187 | ||
31af6a2e | 188 | if ( HTTPHelp->is_initialized() == true ) |
089a7152 | 189 | { |
31af6a2e | 190 | long http_status_code = HTTPHelp->http_get(url); |
089a7152 | 191 | |
31af6a2e BS |
192 | get_logger()->print_http_status_code(url,http_status_code); |
193 | ||
194 | // HTTP operation completed successful. | |
195 | // Now we have to parse the data received by curl, | |
196 | // cause http status code is not significant for easydns update errors | |
197 | if ( http_status_code == 200 ) | |
a78b44b5 | 198 | { |
31af6a2e BS |
199 | // Get the received http data. |
200 | string curl_data = HTTPHelp->get_curl_data(); | |
201 | string status_code = Util::parse_status_code(curl_data,"\n"); | |
202 | ||
203 | if ( status_code == "NOERROR" ) | |
204 | { | |
205 | return 0; | |
206 | } | |
207 | else if ( status_code == "NOACCESS" ) | |
208 | { | |
209 | get_logger()->print_service_not_authorized(url,get_login(),get_password()); | |
210 | } | |
211 | else | |
212 | { | |
213 | get_logger()->print_update_failure(url, curl_data); | |
214 | } | |
a78b44b5 | 215 | } |
089a7152 | 216 | else |
a78b44b5 | 217 | { |
31af6a2e | 218 | get_logger()->print_update_failure(url,http_status_code); |
a78b44b5 BS |
219 | } |
220 | } | |
221 | else | |
222 | { | |
31af6a2e | 223 | get_logger()->print_service_not_initialized(url); |
089a7152 | 224 | } |
a78b44b5 | 225 | return -1; |
089a7152 | 226 | } |