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