First steps in fine tuning and improving error handling.
[bpdyndnsd] / src / service_dyndns.cpp
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  */
21 ServiceDyndns::ServiceDyndns()
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  */
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)
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.swap(_http_help);
58
59     BaseUrl = assemble_base_url(get_hostname());
60 }
61
62
63 /**
64  * Default destructor
65  */
66 ServiceDyndns::~ServiceDyndns()
67 {
68 }
69
70
71 /**
72  * Assemble the dyndns update url from the given fqhn
73  * @param hostname The fqhn hostname to update IP for.
74  * @return The assembled update url without IP.
75  */
76 string ServiceDyndns::assemble_base_url(const string& fqhn) const
77 {
78     string base_url;
79
80     base_url = "https://members.dyndns.org";
81     base_url.append("/nic/update?hostname=");
82     base_url.append(fqhn);
83     base_url.append("&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG&myip=");
84
85     return base_url;
86 }
87
88
89 /**
90  * Performs the Service update.
91  * @param ip IP Address to set.
92  * @return 0 if all is fine, -1 otherwise.
93  */
94 int ServiceDyndns::perform_update(const std::string& ip)
95 {
96     // the result string if update was successful
97     string good = "good ";
98     good.append(ip);
99
100     string url = BaseUrl;
101     url.append(ip);
102
103     if ( HTTPHelp->is_initialized() == true )
104     {
105         // Perform curl operation on given url
106         long http_status_code = HTTPHelp->http_get(url);
107
108         get_logger()->print_http_status_code(url,http_status_code);
109
110         // HTTP operation completed successful.
111         // Now we have to parse the data received by curl,
112         // cause http status code is not significant for dyndns update errors
113         if ( http_status_code == 200 )
114         {
115             // Get the received http data.
116             string curl_data = HTTPHelp->get_curl_data();
117
118             if ( curl_data == good )
119             {
120                 return 0;
121             }
122             else if ( curl_data == "badauth" )
123             {
124                 get_logger()->print_service_not_authorized(url,get_login(),get_password());
125             }
126             else
127             {
128                 get_logger()->print_update_failure(url, curl_data);
129             }
130         }
131         else
132         {
133             get_logger()->print_update_failure(url,http_status_code);
134         }
135     }
136     else
137     {
138         get_logger()->print_service_not_initialized(url);
139     }
140     return -1;
141 }