Fix 'occurred' typo
[bpdyndnsd] / src / service_zoneedit.cpp
CommitLineData
089a7152
BS
1/** @file
2 * @brief ZONEEDIT Service class implementation. This class represents the ZONEEDIT service.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
9
4de6a9b8
BS
10#include "service_zoneedit.hpp"
11#include "util.hpp"
089a7152
BS
12
13#include <time.h>
14#include <boost/foreach.hpp>
15#include <boost/algorithm/string.hpp>
16
17namespace ba = boost::algorithm;
18
19using namespace std;
20
21
22/**
23 * Default Constructor, needed for object serialization.
24 */
629d8110 25ServiceZoneedit::ServiceZoneedit()
089a7152
BS
26{
27}
28
29
30/**
31 * Constructor.
32 * @param _hostname The hostname to update
33 * @param _login The login name.
34 * @param _password The corresponding password.
35 */
4553e833 36ServiceZoneedit::ServiceZoneedit(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)
089a7152
BS
37{
38 if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
a01ce6d7 39 set_update_interval(15); // use default protocol value
089a7152
BS
40 else
41 set_update_interval(_update_interval);
42
43 if ( _max_updates_within_interval == -1 )
a01ce6d7 44 set_max_updates_within_interval(3);
089a7152
BS
45 else
46 set_max_updates_within_interval(_max_updates_within_interval);
47
4553e833
BS
48 if ( _max_equal_updates_in_succession == -1 )
49 set_max_equal_updates_in_succession(2);
50 else
51 set_max_equal_updates_in_succession(_max_equal_updates_in_succession);
52
089a7152
BS
53 if ( _dns_cache_ttl == -1 )
54 set_dns_cache_ttl(60);
55 else
56 set_dns_cache_ttl(_dns_cache_ttl);
57
58 set_protocol(_protocol);
59 set_hostname(_hostname);
60 set_login(_login);
61 set_password(_password);
62 set_logger(_logger);
63
64 // create http helper class
ce70569b 65 HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port));
089a7152
BS
66
67 BaseUrl = assemble_base_url(get_hostname());
68}
69
70
71/**
72 * Default destructor
73 */
629d8110 74ServiceZoneedit::~ServiceZoneedit()
089a7152
BS
75{
76}
77
78
79/**
80 * Assemble the zoneedit update url from the given fqhn
81 * @param hostname The fqhn hostname to update IP for.
82 * @return The assembled update url without IP.
83 */
629d8110 84string ServiceZoneedit::assemble_base_url(const string& fqhn) const
089a7152
BS
85{
86 string base_url;
87
88 base_url = "http://dynamic.zoneedit.com";
89 base_url.append("/auth/dynamic.html?host=");
90 base_url.append(fqhn);
91 base_url.append("&dnsto=");
92
93 return base_url;
94}
95
96
97/**
98 * Performs the Service update.
99 * @param ip IP Address to set.
d008afbe 100 * @return UpdateOk if all is fine, GenericError otherwise.
089a7152 101 */
d008afbe 102Service::UpdateErrorCode ServiceZoneedit::perform_update(const std::string& ip)
089a7152 103{
089a7152
BS
104 string url = BaseUrl;
105 url.append(ip);
106
08a5a621 107 if ( HTTPHelp->is_initialized() )
089a7152 108 {
31af6a2e 109 long http_status_code = HTTPHelp->http_get(url);
089a7152 110
31af6a2e
BS
111 get_logger()->print_http_status_code(url,http_status_code);
112
113 // HTTP operation completed successful.
114 // Now we have to parse the data received by curl,
115 // cause http status code is not significant for dyns update errors
116 if ( http_status_code == 200 )
117 {
118 // Get the received http data and parse the status code.
119 string curl_data = HTTPHelp->get_curl_data();
120 string status_code = Util::parse_status_code(curl_data);
121
122 if ( status_code == "200" )
123 {
d008afbe 124 return UpdateOk;
31af6a2e
BS
125 }
126 else
127 {
128 get_logger()->print_update_failure(url,curl_data);
7335d7a7 129 return UpdateError;
31af6a2e
BS
130 }
131 }
132 else if ( http_status_code == 401 )
a78b44b5 133 {
31af6a2e 134 get_logger()->print_service_not_authorized(url,get_login(),get_password());
7335d7a7 135 return NotAuth;
a78b44b5 136 }
089a7152 137 else
a78b44b5 138 {
31af6a2e 139 get_logger()->print_update_failure(url,http_status_code);
a78b44b5 140 }
089a7152 141 }
089a7152 142 else
a78b44b5 143 {
e8787e2e 144 get_logger()->print_httphelper_not_initialized();
e417b034 145 HTTPHelp->re_initialize();
a78b44b5 146 }
d008afbe 147 return GenericError;
089a7152 148}