--- /dev/null
+/** @file
+ * @brief DYNS Service class implementation. This class represents the DYNS service.
+ *
+ *
+ *
+ * @copyright Intra2net AG
+ * @license GPLv2
+*/
+
+#include "dyns.h"
+
+#include <time.h>
+#include <boost/foreach.hpp>
+
+using namespace std;
+
+
+/**
+ * Default Constructor, needed for object serialization.
+ */
+DYNS::DYNS()
+{
+}
+
+
+/**
+ * Constructor.
+ * @param _hostname The hostname to update
+ * @param _login The login name.
+ * @param _password The corresponding password.
+ */
+DYNS::DYNS(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)
+{
+ if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
+ set_update_interval(5); // use default protocol value
+ else
+ set_update_interval(_update_interval);
+
+ if ( _max_updates_within_interval == -1 )
+ set_max_updates_within_interval(1);
+ else
+ set_max_updates_within_interval(_max_updates_within_interval);
+
+ if ( _dns_cache_ttl == -1 )
+ set_dns_cache_ttl(300);
+ else
+ set_dns_cache_ttl(_dns_cache_ttl);
+
+ set_protocol(_protocol);
+ set_hostname(_hostname);
+ set_login(_login);
+ set_password(_password);
+ set_logger(_logger);
+
+ // create http helper class
+ HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port));
+ HTTPHelp = _http_help;
+ _http_help.reset();
+
+ BaseUrl = assemble_base_url(get_hostname(),get_login(),get_password());
+}
+
+
+/**
+ * Default destructor
+ */
+DYNS::~DYNS()
+{
+}
+
+
+/**
+ * Assemble the dyns update url from the given fqhn
+ * @param hostname The fqhn hostname to update IP for.
+ * @param username The username to use.
+ * @param hostname The password to use.
+ * @return The assembled update url without IP.
+ */
+string DYNS::assemble_base_url(const string& fqhn, const string& username, const string& password) const
+{
+ string base_url;
+
+ base_url = "http://www.dyns.net";
+ base_url.append("/postscript011.php?username=");
+ base_url.append(username);
+ base_url.append("&password=");
+ base_url.append(password);
+ base_url.append("&host=");
+ base_url.append(fqhn);
+ base_url.append("&ip=");
+
+ return base_url;
+}
+
+
+/**
+ * Performs the Service update.
+ * @param ip IP Address to set.
+ * @return 0 if all is fine, -1 otherwise.
+ */
+int DYNS::perform_update(const std::string& ip)
+{
+ int ret_val = -1;
+
+ string url = BaseUrl;
+ url.append(ip);
+
+ long http_status_code = HTTPHelp->http_get(url);
+
+ get_logger()->print_http_status_code(url,http_status_code);
+
+ // HTTP operation completed successful, now we have to parse the data received by curl cause http status code is not significant for dyns update errors
+ if ( http_status_code == 200 )
+ {
+ string curl_data = HTTPHelp->get_curl_data();
+ string status_code = parse_status_code(curl_data);
+
+ if ( status_code == "200" )
+ {
+ ret_val = 0;
+ }
+ else if ( status_code == "401" )
+ {
+ // badauth
+ get_logger()->print_http_not_authorized(url,get_login(),get_password());
+ }
+ else
+ {
+ // generic
+ get_logger()->print_update_failure(url,curl_data);
+ }
+ }
+
+ return ret_val;
+}
+
+
+/**
+ * Get the status code from the returned http data
+ * @param data The returned http data.
+ * @return The status code.
+ */
+string DYNS::parse_status_code(const string& data) const
+{
+ list<string> tokens = split(data," ");
+ return tokens.front();
+}
+
+
+/**
+ * Splitts a string into single tokens useing given delimiters
+ * @param str String to split
+ * @param delimiters Deliminters to use
+ * @return A list with the single tokens
+ */
+list<string> DYNS::split(const string& str,const string& delimiters) const
+{
+ list<string> tokens;
+ // Skip delimiters at beginning.
+ string::size_type lastPos = str.find_first_not_of(delimiters, 0);
+ // Find first "non-delimiter".
+ string::size_type pos = str.find_first_of(delimiters, lastPos);
+
+ while (string::npos != pos || string::npos != lastPos)
+ {
+ // Found a token, add it to the list.
+ tokens.push_back(str.substr(lastPos, pos - lastPos));
+ // Skip delimiters. Note the "not_of"
+ lastPos = str.find_first_not_of(delimiters, pos);
+ // Find next "non-delimiter"
+ pos = str.find_first_of(delimiters, lastPos);
+ }
+ return tokens;
+}
+
+
+/**
+ * Serialize function needed by boost/serialization to define which members should be stored as the object state.
+ * @param ar Archive
+ * @param version Version
+ */
+template<class Archive>
+void DYNS::serialize(Archive & ar, const unsigned int version)
+{
+ ar & boost::serialization::base_object<Service>(*this);
+}
--- /dev/null
+/** @file
+ * @brief DYNS Service class header. This class represents the DYNS service.
+ *
+ *
+ *
+ * @copyright Intra2net AG
+ * @license GPLv2
+*/
+
+#ifndef DYNS_H
+#define DYNS_H
+
+#include <string>
+
+#include "service.h"
+#include "logger.h"
+
+#include <boost/serialization/array.hpp>
+#include <boost/shared_ptr.hpp>
+#include <list>
+
+class DYNS : public Service
+{
+
+private:
+
+ friend class boost::serialization::access;
+ template<class Archive>
+ void serialize(Archive & ar, const unsigned int version);
+
+ std::string BaseUrl;
+
+ HTTPHelper::Ptr HTTPHelp;
+
+ std::string assemble_base_url(const std::string& fqhn, const std::string& username, const std::string& password) const;
+
+ std::string parse_status_code(const std::string& data) const;
+
+ std::list<std::string> split(const std::string& str,const std::string& delimiters) const;
+
+public:
+
+ typedef boost::shared_ptr<DYNS> Ptr;
+
+ DYNS();
+
+ DYNS(const std::string& _protocol, const std::string& _hostname, const std::string& _login, const std::string& _password, const Logger::Ptr& _logger, const int _update_interval, const int _max_updates_within_interval, const int dns_cache_ttl, const std::string& proxy, const int proxy_port);
+
+ ~DYNS();
+
+ int perform_update(const std::string& ip);
+};
+
+#endif