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 | */ | |
f04a7cb4 BS |
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 _dns_cache_ttl, const string& _proxy, const int _proxy_port, const string& _alternative_server) |
32 | : AlternativeServer(_alternative_server) | |
089a7152 BS |
33 | { |
34 | if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config) | |
35 | set_update_interval(0); // use default protocol value | |
36 | else | |
37 | set_update_interval(_update_interval); | |
38 | ||
39 | if ( _max_updates_within_interval == -1 ) | |
40 | set_max_updates_within_interval(0); | |
41 | else | |
42 | set_max_updates_within_interval(_max_updates_within_interval); | |
43 | ||
44 | if ( _dns_cache_ttl == -1 ) | |
45 | set_dns_cache_ttl(60); | |
46 | else | |
47 | set_dns_cache_ttl(_dns_cache_ttl); | |
48 | ||
49 | set_protocol(_protocol); | |
50 | set_hostname(_hostname); | |
51 | set_login(_login); | |
52 | set_password(_password); | |
53 | set_logger(_logger); | |
54 | ||
55 | // create http helper class | |
ce70569b | 56 | HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password)); |
089a7152 BS |
57 | |
58 | BaseUrl = assemble_base_url(get_hostname()); | |
59 | } | |
60 | ||
61 | ||
62 | /** | |
63 | * Default destructor | |
64 | */ | |
629d8110 | 65 | ServiceDyndns::~ServiceDyndns() |
089a7152 BS |
66 | { |
67 | } | |
68 | ||
69 | ||
70 | /** | |
71 | * Assemble the dyndns update url from the given fqhn | |
72 | * @param hostname The fqhn hostname to update IP for. | |
73 | * @return The assembled update url without IP. | |
74 | */ | |
629d8110 | 75 | string ServiceDyndns::assemble_base_url(const string& fqhn) const |
089a7152 | 76 | { |
f04a7cb4 BS |
77 | string base_url = "https://"; |
78 | ||
79 | // Test if a AlternativeServer name is given, needed for e.g. NO-IP | |
80 | if ( AlternativeServer.empty() ) | |
81 | base_url.append("members.dyndns.org"); | |
82 | else | |
7b955644 | 83 | base_url.append(AlternativeServer); |
089a7152 | 84 | |
089a7152 BS |
85 | base_url.append("/nic/update?hostname="); |
86 | base_url.append(fqhn); | |
87 | base_url.append("&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG&myip="); | |
88 | ||
89 | return base_url; | |
90 | } | |
91 | ||
92 | ||
93 | /** | |
94 | * Performs the Service update. | |
95 | * @param ip IP Address to set. | |
96 | * @return 0 if all is fine, -1 otherwise. | |
97 | */ | |
629d8110 | 98 | int ServiceDyndns::perform_update(const std::string& ip) |
089a7152 | 99 | { |
089a7152 BS |
100 | // the result string if update was successful |
101 | string good = "good "; | |
102 | good.append(ip); | |
103 | ||
104 | string url = BaseUrl; | |
105 | url.append(ip); | |
106 | ||
08a5a621 | 107 | if ( HTTPHelp->is_initialized() ) |
089a7152 | 108 | { |
31af6a2e BS |
109 | // Perform curl operation on given url |
110 | long http_status_code = HTTPHelp->http_get(url); | |
089a7152 | 111 | |
31af6a2e BS |
112 | get_logger()->print_http_status_code(url,http_status_code); |
113 | ||
114 | // HTTP operation completed successful. | |
115 | // Now we have to parse the data received by curl, | |
116 | // cause http status code is not significant for dyndns update errors | |
117 | if ( http_status_code == 200 ) | |
a78b44b5 | 118 | { |
31af6a2e BS |
119 | // Get the received http data. |
120 | string curl_data = HTTPHelp->get_curl_data(); | |
121 | ||
122 | if ( curl_data == good ) | |
123 | { | |
124 | return 0; | |
125 | } | |
126 | else if ( curl_data == "badauth" ) | |
127 | { | |
128 | get_logger()->print_service_not_authorized(url,get_login(),get_password()); | |
129 | } | |
130 | else | |
131 | { | |
132 | get_logger()->print_update_failure(url, curl_data); | |
133 | } | |
a78b44b5 | 134 | } |
089a7152 | 135 | else |
a78b44b5 | 136 | { |
31af6a2e | 137 | get_logger()->print_update_failure(url,http_status_code); |
a78b44b5 BS |
138 | } |
139 | } | |
140 | else | |
141 | { | |
e8787e2e | 142 | get_logger()->print_httphelper_not_initialized(); |
e417b034 | 143 | HTTPHelp->re_initialize(); |
089a7152 | 144 | } |
a78b44b5 | 145 | return -1; |
089a7152 | 146 | } |