Added regex tests to test if given IP is within a private range.
[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>
15
16namespace ba = boost::algorithm;
17
18using namespace std;
19
20
21/**
22 * Default Constructor, needed for object serialization.
23 */
629d8110 24ServiceDhs::ServiceDhs()
089a7152
BS
25{
26}
27
28
29/**
30 * Constructor.
31 * @param _hostname The hostname to update
32 * @param _login The login name.
33 * @param _password The corresponding password.
34 */
629d8110 35ServiceDhs::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
36{
37 if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
38 set_update_interval(15); // use default protocol value
39 else
40 set_update_interval(_update_interval);
41
42 if ( _max_updates_within_interval == -1 )
43 set_max_updates_within_interval(3);
44 else
45 set_max_updates_within_interval(_max_updates_within_interval);
46
47 if ( _dns_cache_ttl == -1 )
48 set_dns_cache_ttl(7200);
49 else
50 set_dns_cache_ttl(_dns_cache_ttl);
51
52 set_protocol(_protocol);
53 set_hostname(_hostname);
54 set_login(_login);
55 set_password(_password);
56 set_logger(_logger);
57
58 // create http helper class
59 HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
60 HTTPHelp = _http_help;
61 _http_help.reset();
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
BS
138{
139 int ret_val = -1;
140
141 string url = BaseUrl;
142 url.append(ip);
143
144 // Perform curl operation on given url.
145 long http_status_code = HTTPHelp->http_get(url);
146
147 get_logger()->print_http_status_code(url,http_status_code);
148
149 // Check the status code for protocol errors.
150 if ( http_status_code == 200 )
151 {
152 // TODO: Account related errors should be handled here!
153 ret_val = 0;
154 }
155 else if ( http_status_code == 401 )
156 get_logger()->print_http_not_authorized(url,get_login(),get_password());
157 else
158 get_logger()->print_update_failure(url,http_status_code);
159
160 return ret_val;
161}
162
163
164/**
165 * Serialize function needed by boost/serialization to define which members should be stored as the object state.
166 * @param ar Archive
167 * @param version Version
168 */
169template<class Archive>
629d8110 170void ServiceDhs::serialize(Archive & ar, const unsigned int version)
089a7152
BS
171{
172 ar & boost::serialization::base_object<Service>(*this);
173}