Examples to improve code.
[bpdyndnsd] / src / service_dhs.cpp
CommitLineData
089a7152
BS
1/** @file
2 * @brief DHS Service class implementation. This class represents the DHS service.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
9
10#include "service_dhs.h"
11
12#include <time.h>
13#include <boost/foreach.hpp>
14#include <boost/algorithm/string.hpp>
113a03a9 15#include <boost/regex.hpp>
089a7152
BS
16
17namespace ba = boost::algorithm;
18
19using namespace std;
20
21
22/**
23 * Default Constructor, needed for object serialization.
24 */
629d8110 25ServiceDhs::ServiceDhs()
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 */
629d8110 36ServiceDhs::ServiceDhs(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
37{
38 if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
39 set_update_interval(15); // 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(3);
45 else
46 set_max_updates_within_interval(_max_updates_within_interval);
47
48 if ( _dns_cache_ttl == -1 )
49 set_dns_cache_ttl(7200);
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
60 HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
4dfdba68 61 HTTPHelp.swap(_http_help);
089a7152
BS
62
63 // extract domain part from hostname
64 list<string> host_domain_part = separate_domain_and_host_part(get_hostname());
65
66 BaseUrl = assemble_base_url(host_domain_part.front(),host_domain_part.back());
67}
68
69
70/**
71 * Default destructor
72 */
629d8110 73ServiceDhs::~ServiceDhs()
089a7152
BS
74{
75}
76
77
78/**
79 * Assemble the dhs update url from the given hostname and domain part
80 * @param hostname The hostname to update IP for.
81 * @param domain_part The domain_part in which the hostname is located.
82 * @return The assembled update url without IP.
83 */
629d8110 84string ServiceDhs::assemble_base_url(const string& hostname, const string& domain_part) const
089a7152
BS
85{
86 string base_url;
87
88 base_url = "http://members.dhs.org";
89 base_url.append("/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&domain=");
90 base_url.append(domain_part);
91 base_url.append("&hostname=");
92 base_url.append(hostname);
93 base_url.append("&updatetype=Online&ip=");
94
95 return base_url;
96}
97
98
99/**
100 * Separates the hostname from the domain part.
101 * @param fqdn Hostname with domain part.
102 * @return A list with 2 elements (first element is the hostname, second element the domain part), or a list with 1 element if the domain part couldn't be determined.
103 */
629d8110 104list<string> ServiceDhs::separate_domain_and_host_part(const string& fqdn) const
089a7152
BS
105{
106 list<string> splitted;
107 ba::split(splitted,fqdn,boost::is_any_of("."));
108
109 if ( splitted.size() > 1 )
110 {
111 string host = splitted.front();
112 splitted.pop_front();
113
114 string domain = splitted.front();
115 splitted.pop_front();
116
117 BOOST_FOREACH(string domain_part, splitted)
118 {
119 domain.append(".");
120 domain.append(domain_part);
121 }
122
123 splitted.clear();
124 splitted.push_back(host);
125 splitted.push_back(domain);
126 }
127
128 return splitted;
129}
130
131
132/**
133 * Performs the Service update.
134 * @param ip IP Address to set.
135 * @return 0 if all is fine, -1 otherwise.
136 */
629d8110 137int ServiceDhs::perform_update(const std::string& ip)
089a7152 138{
089a7152
BS
139 string url = BaseUrl;
140 url.append(ip);
141
142 // Perform curl operation on given url.
143 long http_status_code = HTTPHelp->http_get(url);
144
145 get_logger()->print_http_status_code(url,http_status_code);
146
147 // Check the status code for protocol errors.
148 if ( http_status_code == 200 )
149 {
113a03a9
BS
150 // Get the received http data.
151 string curl_data = HTTPHelp->get_curl_data();
152
153 // Search for the following string in the received http data
154 boost::regex expr_done("Updating ip on .*: done");
155 if ( boost::regex_search(curl_data,expr_done) )
156 {
157 // Update successful
158 return 0;
159 }
160 else
161 {
162 get_logger()->print_update_failure(url,curl_data);
163 }
089a7152
BS
164 }
165 else if ( http_status_code == 401 )
a78b44b5 166 {
a03fb896 167 get_logger()->print_service_not_authorized(url,get_login(),get_password());
a78b44b5 168 }
089a7152 169 else
a78b44b5 170 {
089a7152 171 get_logger()->print_update_failure(url,http_status_code);
a78b44b5 172 }
089a7152 173
a78b44b5 174 return -1;
089a7152 175}