Commit | Line | Data |
---|---|---|
089a7152 BS |
1 | /** @file |
2 | * @brief DYNDNS Service class implementation. This class represents the DYNDNS service. | |
3 | * | |
4 | * | |
5 | * | |
6 | * @copyright Intra2net AG | |
7 | * @license GPLv2 | |
8 | */ | |
9 | ||
4de6a9b8 | 10 | #include "service_dyndns.hpp" |
089a7152 | 11 | |
089a7152 BS |
12 | #include <boost/foreach.hpp> |
13 | ||
14 | using namespace std; | |
15 | ||
16 | ||
17 | /** | |
18 | * Default Constructor, needed for object serialization. | |
19 | */ | |
629d8110 | 20 | ServiceDyndns::ServiceDyndns() |
089a7152 BS |
21 | { |
22 | } | |
23 | ||
24 | ||
25 | /** | |
26 | * Constructor. | |
27 | * @param _hostname The hostname to update | |
28 | * @param _login The login name. | |
29 | * @param _password The corresponding password. | |
30 | */ | |
4553e833 | 31 | ServiceDyndns::ServiceDyndns(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, const string& _alternative_server) |
f04a7cb4 | 32 | : AlternativeServer(_alternative_server) |
089a7152 BS |
33 | { |
34 | if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config) | |
a01ce6d7 | 35 | set_update_interval(15); // use default protocol value |
089a7152 BS |
36 | else |
37 | set_update_interval(_update_interval); | |
38 | ||
39 | if ( _max_updates_within_interval == -1 ) | |
a01ce6d7 | 40 | set_max_updates_within_interval(3); |
089a7152 BS |
41 | else |
42 | set_max_updates_within_interval(_max_updates_within_interval); | |
43 | ||
4553e833 BS |
44 | if ( _max_equal_updates_in_succession == -1 ) |
45 | set_max_equal_updates_in_succession(2); | |
46 | else | |
47 | set_max_equal_updates_in_succession(_max_equal_updates_in_succession); | |
48 | ||
089a7152 BS |
49 | if ( _dns_cache_ttl == -1 ) |
50 | set_dns_cache_ttl(60); | |
51 | else | |
52 | set_dns_cache_ttl(_dns_cache_ttl); | |
53 | ||
54 | set_protocol(_protocol); | |
55 | set_hostname(_hostname); | |
56 | set_login(_login); | |
57 | set_password(_password); | |
58 | set_logger(_logger); | |
59 | ||
60 | // create http helper class | |
ce70569b | 61 | HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password)); |
089a7152 BS |
62 | |
63 | BaseUrl = assemble_base_url(get_hostname()); | |
64 | } | |
65 | ||
66 | ||
67 | /** | |
68 | * Default destructor | |
69 | */ | |
629d8110 | 70 | ServiceDyndns::~ServiceDyndns() |
089a7152 BS |
71 | { |
72 | } | |
73 | ||
74 | ||
75 | /** | |
76 | * Assemble the dyndns update url from the given fqhn | |
77 | * @param hostname The fqhn hostname to update IP for. | |
78 | * @return The assembled update url without IP. | |
79 | */ | |
629d8110 | 80 | string ServiceDyndns::assemble_base_url(const string& fqhn) const |
089a7152 | 81 | { |
f04a7cb4 BS |
82 | string base_url = "https://"; |
83 | ||
84 | // Test if a AlternativeServer name is given, needed for e.g. NO-IP | |
85 | if ( AlternativeServer.empty() ) | |
86 | base_url.append("members.dyndns.org"); | |
87 | else | |
7b955644 | 88 | base_url.append(AlternativeServer); |
089a7152 | 89 | |
089a7152 BS |
90 | base_url.append("/nic/update?hostname="); |
91 | base_url.append(fqhn); | |
92 | base_url.append("&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG&myip="); | |
93 | ||
94 | return base_url; | |
95 | } | |
96 | ||
97 | ||
98 | /** | |
99 | * Performs the Service update. | |
100 | * @param ip IP Address to set. | |
101 | * @return 0 if all is fine, -1 otherwise. | |
102 | */ | |
d008afbe | 103 | Service::UpdateErrorCode ServiceDyndns::perform_update(const std::string& ip) |
089a7152 | 104 | { |
089a7152 BS |
105 | string url = BaseUrl; |
106 | url.append(ip); | |
107 | ||
25ce914f | 108 | if ( !HTTPHelp->is_initialized() ) |
089a7152 | 109 | { |
25ce914f TJ |
110 | get_logger()->print_httphelper_not_initialized(); |
111 | HTTPHelp->re_initialize(); | |
d008afbe | 112 | return GenericError; |
25ce914f | 113 | } |
089a7152 | 114 | |
25ce914f TJ |
115 | // Perform curl operation on given url |
116 | long http_status_code = HTTPHelp->http_get(url); | |
117 | ||
118 | get_logger()->print_http_status_code(url,http_status_code); | |
119 | ||
120 | // HTTP operation completed successful. | |
121 | // Now we have to parse the data received by curl, | |
122 | // cause http status code is not significant for dyndns update errors | |
123 | if ( http_status_code == 200 ) | |
124 | { | |
125 | // Get the received http data. | |
126 | string curl_data = HTTPHelp->get_curl_data(); | |
31af6a2e | 127 | |
ffac9540 | 128 | if ( curl_data.compare(0,4,"good") == 0 ) |
a78b44b5 | 129 | { |
d008afbe | 130 | return UpdateOk; |
25ce914f | 131 | } |
dbb6abcb TJ |
132 | else if (curl_data.compare(0,5,"nochg") == 0 ) |
133 | { | |
134 | // IP didn't change, this might indicate a problem at the | |
135 | // dyndns backend or another client is running elsewhere. | |
d008afbe | 136 | get_logger()->print_update_failure(url, curl_data); |
cd5a41a4 | 137 | return NoChange; |
dbb6abcb TJ |
138 | } |
139 | else if (curl_data.compare(0,5,"abuse") == 0 ) | |
140 | { | |
141 | // Account is blocked. Log it as a successful update | |
142 | // so the IP address will be "burnt" until it changes. | |
d008afbe | 143 | get_logger()->print_update_failure(url, curl_data); |
cd5a41a4 | 144 | return Blocked; |
dbb6abcb | 145 | } |
25ce914f TJ |
146 | else if ( curl_data == "badauth" ) |
147 | { | |
148 | get_logger()->print_service_not_authorized(url,get_login(),get_password()); | |
a78b44b5 | 149 | } |
089a7152 | 150 | else |
a78b44b5 | 151 | { |
25ce914f | 152 | get_logger()->print_update_failure(url, curl_data); |
a78b44b5 BS |
153 | } |
154 | } | |
155 | else | |
156 | { | |
25ce914f | 157 | get_logger()->print_update_failure(url,http_status_code); |
089a7152 | 158 | } |
25ce914f | 159 | |
d008afbe | 160 | return GenericError; |
089a7152 | 161 | } |