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 | ||
10 | #include "service_dyndns.h" | |
11 | ||
12 | #include <time.h> | |
13 | #include <boost/foreach.hpp> | |
14 | ||
15 | using namespace std; | |
16 | ||
17 | ||
18 | /** | |
19 | * Default Constructor, needed for object serialization. | |
20 | */ | |
629d8110 | 21 | ServiceDyndns::ServiceDyndns() |
089a7152 BS |
22 | { |
23 | } | |
24 | ||
25 | ||
26 | /** | |
27 | * Constructor. | |
28 | * @param _hostname The hostname to update | |
29 | * @param _login The login name. | |
30 | * @param _password The corresponding password. | |
31 | */ | |
629d8110 | 32 | 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) |
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 | |
56 | HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password)); | |
57 | HTTPHelp = _http_help; | |
58 | _http_help.reset(); | |
59 | ||
60 | BaseUrl = assemble_base_url(get_hostname()); | |
61 | } | |
62 | ||
63 | ||
64 | /** | |
65 | * Default destructor | |
66 | */ | |
629d8110 | 67 | ServiceDyndns::~ServiceDyndns() |
089a7152 BS |
68 | { |
69 | } | |
70 | ||
71 | ||
72 | /** | |
73 | * Assemble the dyndns update url from the given fqhn | |
74 | * @param hostname The fqhn hostname to update IP for. | |
75 | * @return The assembled update url without IP. | |
76 | */ | |
629d8110 | 77 | string ServiceDyndns::assemble_base_url(const string& fqhn) const |
089a7152 BS |
78 | { |
79 | string base_url; | |
80 | ||
81 | base_url = "https://members.dyndns.org"; | |
82 | base_url.append("/nic/update?hostname="); | |
83 | base_url.append(fqhn); | |
84 | base_url.append("&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG&myip="); | |
85 | ||
86 | return base_url; | |
87 | } | |
88 | ||
89 | ||
90 | /** | |
91 | * Performs the Service update. | |
92 | * @param ip IP Address to set. | |
93 | * @return 0 if all is fine, -1 otherwise. | |
94 | */ | |
629d8110 | 95 | int ServiceDyndns::perform_update(const std::string& ip) |
089a7152 BS |
96 | { |
97 | int ret_val = -1; | |
98 | ||
99 | // the result string if update was successful | |
100 | string good = "good "; | |
101 | good.append(ip); | |
102 | ||
103 | string url = BaseUrl; | |
104 | url.append(ip); | |
105 | ||
106 | // Perform curl operation on given url | |
107 | long http_status_code = HTTPHelp->http_get(url); | |
108 | ||
109 | get_logger()->print_http_status_code(url,http_status_code); | |
110 | ||
111 | // HTTP operation completed successful. | |
112 | // Now we have to parse the data received by curl, | |
113 | // cause http status code is not significant for dyndns update errors | |
114 | if ( http_status_code == 200 ) | |
115 | { | |
116 | // Get the received http data. | |
117 | string curl_data = HTTPHelp->get_curl_data(); | |
118 | ||
119 | if ( curl_data == good ) | |
120 | ret_val = 0; | |
121 | else if ( curl_data == "badauth" ) | |
122 | get_logger()->print_http_not_authorized(url,get_login(),get_password()); | |
123 | else | |
124 | get_logger()->print_update_failure(url, curl_data); | |
125 | } | |
126 | ||
127 | return ret_val; | |
128 | } | |
129 | ||
130 | ||
131 | /** | |
132 | * Serialize function needed by boost/serialization to define which members should be stored as the object state. | |
133 | * @param ar Archive | |
134 | * @param version Version | |
135 | */ | |
136 | template<class Archive> | |
629d8110 | 137 | void ServiceDyndns::serialize(Archive & ar, const unsigned int version) |
089a7152 BS |
138 | { |
139 | ar & boost::serialization::base_object<Service>(*this); | |
140 | } |