Fix 'occurred' typo
[bpdyndnsd] / src / service_dyns.cpp
CommitLineData
089a7152
BS
1/** @file
2 * @brief DYNS Service class implementation. This class represents the DYNS service.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
9
4de6a9b8
BS
10#include "service_dyns.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 25ServiceDyns::ServiceDyns()
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 36ServiceDyns::ServiceDyns(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)
39 set_update_interval(5); // use default protocol value
40 else
41 set_update_interval(_update_interval);
42
43 if ( _max_updates_within_interval == -1 )
44 set_max_updates_within_interval(1);
45 else
46 set_max_updates_within_interval(_max_updates_within_interval);
47
48 if ( _dns_cache_ttl == -1 )
49 set_dns_cache_ttl(300);
50 else
51 set_dns_cache_ttl(_dns_cache_ttl);
52
53 set_protocol(_protocol);
54 set_hostname(_hostname);
55 set_login(_login);
56 set_password(_password);
57 set_logger(_logger);
58
59 // create http helper class
ce70569b 60 HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port));
089a7152
BS
61
62 BaseUrl = assemble_base_url(get_hostname(),get_login(),get_password());
63}
64
65
66/**
67 * Default destructor
68 */
629d8110 69ServiceDyns::~ServiceDyns()
089a7152
BS
70{
71}
72
73
74/**
75 * Assemble the dyns update url from the given fqhn
76 * @param hostname The fqhn hostname to update IP for.
77 * @param username The username to use.
78 * @param hostname The password to use.
79 * @return The assembled update url without IP.
80 */
629d8110 81string ServiceDyns::assemble_base_url(const string& fqhn, const string& username, const string& password) const
089a7152
BS
82{
83 string base_url;
84
85 base_url = "http://www.dyns.net";
86 base_url.append("/postscript011.php?username=");
87 base_url.append(username);
88 base_url.append("&password=");
89 base_url.append(password);
90 base_url.append("&host=");
91 base_url.append(fqhn);
92 base_url.append("&ip=");
93
94 return base_url;
95}
96
97
98/**
99 * Performs the Service update.
100 * @param ip IP Address to set.
101 * @return 0 if all is fine, -1 otherwise.
102 */
d008afbe 103Service::UpdateErrorCode ServiceDyns::perform_update(const std::string& ip)
089a7152 104{
089a7152
BS
105 string url = BaseUrl;
106 url.append(ip);
107
08a5a621 108 if ( HTTPHelp->is_initialized() )
089a7152 109 {
31af6a2e 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 dyns update errors
117 if ( http_status_code == 200 )
a78b44b5 118 {
31af6a2e
BS
119 // Get the received http data and parse the status code.
120 string curl_data = HTTPHelp->get_curl_data();
121 string status_code = Util::parse_status_code(curl_data);
122
123 if ( status_code == "200" )
124 {
d008afbe 125 return UpdateOk;
31af6a2e
BS
126 }
127 else if ( status_code == "401" )
128 {
129 get_logger()->print_service_not_authorized(url,get_login(),get_password());
7335d7a7 130 return NotAuth;
31af6a2e
BS
131 }
132 else
133 {
134 get_logger()->print_update_failure(url,curl_data);
7335d7a7 135 return UpdateError;
31af6a2e 136 }
a78b44b5 137 }
089a7152 138 else
a78b44b5 139 {
31af6a2e 140 get_logger()->print_update_failure(url,http_status_code);
a78b44b5
BS
141 }
142 }
143 else
144 {
e8787e2e 145 get_logger()->print_httphelper_not_initialized();
e417b034 146 HTTPHelp->re_initialize();
089a7152 147 }
d008afbe 148 return GenericError;
089a7152 149}