From 3f8b2557979e0443fc749dff002232b5e7e957a0 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Mon, 7 Sep 2009 15:41:57 +0200 Subject: [PATCH] Added implementation of service zoneedit. --- src/config.cpp | 6 ++ src/main.cpp | 1 + src/updater.cpp | 2 + src/zoneedit.cpp | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/zoneedit.h | 52 ++++++++++++++++++ 5 files changed, 215 insertions(+), 0 deletions(-) create mode 100644 src/zoneedit.cpp create mode 100644 src/zoneedit.h diff --git a/src/config.cpp b/src/config.cpp index 9629d2e..34674ab 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -15,6 +15,7 @@ #include "dyns.h" #include "easydns.h" #include "tzo.h" +#include "zoneedit.h" #include #include @@ -290,6 +291,11 @@ Service::Ptr Config::create_service(const string &protocol,const string &hostnam Service::Ptr service_tzo(new TZO(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort)); return service_tzo; } + else if(protocol == "zoneedit") + { + Service::Ptr service_zoneedit(new ZONEEDIT(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort)); + return service_zoneedit; + } else { Log->print_unknown_protocol(protocol); diff --git a/src/main.cpp b/src/main.cpp index 32de8c1..a60787d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,6 +43,7 @@ #include "dhs.cpp" #include "easydns.cpp" #include "tzo.cpp" +#include "zoneedit.cpp" using namespace std; diff --git a/src/updater.cpp b/src/updater.cpp index 36c3958..c7e6fe0 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -16,6 +16,7 @@ #include "dyns.h" #include "easydns.h" #include "tzo.h" +#include "zoneedit.h" #include @@ -27,6 +28,7 @@ BOOST_CLASS_EXPORT_GUID(DYNS, "DYNS") BOOST_CLASS_EXPORT_GUID(DYNDNS, "DYNDNS") BOOST_CLASS_EXPORT_GUID(EASYDNS, "EASYDNS") BOOST_CLASS_EXPORT_GUID(TZO, "TZO") +BOOST_CLASS_EXPORT_GUID(ZONEEDIT, "ZONEEDIT") namespace fs = boost::filesystem; diff --git a/src/zoneedit.cpp b/src/zoneedit.cpp new file mode 100644 index 0000000..c4b3b2b --- /dev/null +++ b/src/zoneedit.cpp @@ -0,0 +1,154 @@ +/** @file + * @brief DYNS Service class implementation. This class represents the DYNS service. + * + * + * + * @copyright Intra2net AG + * @license GPLv2 +*/ + +#include "zoneedit.h" + +#include +#include +#include + +namespace ba = boost::algorithm; + +using namespace std; + + +/** + * Default Constructor, needed for object serialization. + */ +ZONEEDIT::ZONEEDIT() +{ +} + + +/** + * Constructor. + * @param _hostname The hostname to update + * @param _login The login name. + * @param _password The corresponding password. + */ +ZONEEDIT::ZONEEDIT(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(0); // use default protocol value + else + set_update_interval(_update_interval); + + if ( _max_updates_within_interval == -1 ) + set_max_updates_within_interval(0); + else + set_max_updates_within_interval(_max_updates_within_interval); + + if ( _dns_cache_ttl == -1 ) + set_dns_cache_ttl(60); + 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()); +} + + +/** + * Default destructor + */ +ZONEEDIT::~ZONEEDIT() +{ +} + + +/** + * Assemble the zoneedit update url from the given fqhn + * @param hostname The fqhn hostname to update IP for. + * @return The assembled update url without IP. + */ +string ZONEEDIT::assemble_base_url(const string& fqhn) const +{ + string base_url; + + base_url = "http://dynamic.zoneedit.com"; + base_url.append("/auth/dynamic.html?host="); + base_url.append(fqhn); + base_url.append("&dnsto="); + + return base_url; +} + + +/** + * Performs the Service update. + * @param ip IP Address to set. + * @return 0 if all is fine, -1 otherwise. + */ +int ZONEEDIT::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 ) + { + // Get the received http data and parse the status code. + string curl_data = HTTPHelp->get_curl_data(); + string status_code = parse_status_code(curl_data); + + if ( status_code == "200" ) + ret_val = 0; + else + get_logger()->print_update_failure(url,curl_data); + } + else if ( http_status_code == 401 ) + get_logger()->print_http_not_authorized(url,get_login(),get_password()); + else + get_logger()->print_update_failure(url,http_status_code); + + return ret_val; +} + + +/** + * Get the status code from the returned http data + * @param data The returned http data. + * @return The status code. + */ +string ZONEEDIT::parse_status_code(const string& data) const +{ + list tokens; + ba::split(tokens,data,boost::is_any_of(" ")); + return tokens.front(); +} + + +/** + * Serialize function needed by boost/serialization to define which members should be stored as the object state. + * @param ar Archive + * @param version Version + */ +template +void ZONEEDIT::serialize(Archive & ar, const unsigned int version) +{ + ar & boost::serialization::base_object(*this); +} diff --git a/src/zoneedit.h b/src/zoneedit.h new file mode 100644 index 0000000..c7c947b --- /dev/null +++ b/src/zoneedit.h @@ -0,0 +1,52 @@ +/** @file + * @brief DYNS Service class header. This class represents the DYNS service. + * + * + * + * @copyright Intra2net AG + * @license GPLv2 +*/ + +#ifndef ZONEEDIT_H +#define ZONEEDIT_H + +#include + +#include "service.h" +#include "logger.h" + +#include +#include +#include + +class ZONEEDIT : public Service +{ + +private: + + friend class boost::serialization::access; + template + 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 parse_status_code(const std::string& data) const; + +public: + + typedef boost::shared_ptr Ptr; + + ZONEEDIT(); + + ZONEEDIT(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); + + ~ZONEEDIT(); + + int perform_update(const std::string& ip); +}; + +#endif -- 1.7.1