Added implementation for tzo.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 7 Sep 2009 12:53:35 +0000 (14:53 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 7 Sep 2009 12:53:35 +0000 (14:53 +0200)
src/config.cpp
src/dhs.cpp
src/logger.cpp
src/logger.h
src/main.cpp
src/tzo.cpp [new file with mode: 0644]
src/tzo.h [new file with mode: 0644]
src/updater.cpp

index 335e7ff..9629d2e 100644 (file)
@@ -14,6 +14,7 @@
 #include "dyndns.h"
 #include "dyns.h"
 #include "easydns.h"
+#include "tzo.h"
 
 #include <time.h>
 #include <iostream>
@@ -284,6 +285,11 @@ Service::Ptr Config::create_service(const string &protocol,const string &hostnam
         Service::Ptr service_easydns(new EASYDNS(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
         return service_easydns;
     }
+    else if(protocol == "tzo")
+    {
+        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
     {
         Log->print_unknown_protocol(protocol);
index 485047a..b148867 100644 (file)
@@ -148,7 +148,10 @@ int DHS::perform_update(const std::string& ip)
 
     // Check the status code for protocol errors.
     if ( http_status_code == 200 )
+    {
+        // TODO: Account related errors should be handled here!
         ret_val = 0;
+    }
     else if ( http_status_code == 401 )
         get_logger()->print_http_not_authorized(url,get_login(),get_password());
     else
index 45fc9cc..a5689cc 100644 (file)
@@ -1307,6 +1307,22 @@ void Logger::print_update_failure(const string& url, const string& curl_data) co
 
 
 /**
+ * Generic failure while trying to update service
+ * @param url The requested URL
+ * @param http_status_code The received http status code
+ */
+void Logger::print_update_failure(const string& url, const long http_status_code) const
+{
+    int level = 0;
+    if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
+    {
+        ostringstream msg;
+        msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << http_status_code << endl;
+        log_warning(msg.str(),level);
+    }
+}
+
+/**
  * Hostname is invalid, contains no or only one domain part.
  * @param hostname The full qualified host name. 
  */
index 3abf4eb..e8f4052 100644 (file)
@@ -192,6 +192,8 @@ public:
 
     void print_update_failure(const std::string& url, const std::string& curl_data) const;
 
+    void print_update_failure(const std::string& url, const long http_status_code) const;
+
     void print_invalid_hostname(const std::string& hostname) const;
 };
 
index 4deb83e..32de8c1 100644 (file)
@@ -42,6 +42,7 @@
 #include "ods.cpp"
 #include "dhs.cpp"
 #include "easydns.cpp"
+#include "tzo.cpp"
 
 using namespace std;
 
diff --git a/src/tzo.cpp b/src/tzo.cpp
new file mode 100644 (file)
index 0000000..d47a7bc
--- /dev/null
@@ -0,0 +1,158 @@
+/** @file
+ * @brief DYNS Service class implementation. This class represents the DYNS service.
+ *
+ *
+ *
+ * @copyright Intra2net AG
+ * @license GPLv2
+*/
+
+#include "tzo.h"
+
+#include <time.h>
+#include <boost/foreach.hpp>
+#include <boost/algorithm/string.hpp>
+
+namespace ba = boost::algorithm;
+
+using namespace std;
+
+
+/**
+ * Default Constructor, needed for object serialization.
+ */
+TZO::TZO()
+{
+}
+
+
+/**
+ * Constructor.
+ * @param _hostname The hostname to update
+ * @param _login The login name.
+ * @param _password The corresponding password.
+ */
+TZO::TZO(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(),get_login(),get_password());
+}
+
+
+/**
+ * Default destructor
+ */
+TZO::~TZO()
+{
+}
+
+
+/**
+ * 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 TZO::assemble_base_url(const string& fqhn, const string& username, const string& password) const
+{
+    string base_url;
+
+    base_url = "http://rh.tzo.com";
+    base_url.append("/webclient/tzoperl.html?system=tzodns&info=1&tzoname=");
+    base_url.append(fqhn);
+    base_url.append("&email=");
+    base_url.append(username);
+    base_url.append("&tzokey=");
+    base_url.append(password);
+    base_url.append("&ipaddress=");
+
+    return base_url;
+}
+
+
+/**
+ * Performs the Service update.
+ * @param ip IP Address to set.
+ * @return 0 if all is fine, -1 otherwise.
+ */
+int TZO::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 if ( status_code == "401" )
+            get_logger()->print_http_not_authorized(url,get_login(),get_password());
+        else
+            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 TZO::parse_status_code(const string& data) const
+{
+    list<string> 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<class Archive>
+void TZO::serialize(Archive & ar, const unsigned int version)
+{
+    ar & boost::serialization::base_object<Service>(*this);
+}
diff --git a/src/tzo.h b/src/tzo.h
new file mode 100644 (file)
index 0000000..34cccd1
--- /dev/null
+++ b/src/tzo.h
@@ -0,0 +1,52 @@
+/** @file
+ * @brief DYNS Service class header. This class represents the DYNS service.
+ *
+ *
+ *
+ * @copyright Intra2net AG
+ * @license GPLv2
+*/
+
+#ifndef TZO_H
+#define TZO_H
+
+#include <string>
+
+#include "service.h"
+#include "logger.h"
+
+#include <boost/serialization/array.hpp>
+#include <boost/shared_ptr.hpp>
+#include <list>
+
+class TZO : 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;
+
+public:
+
+    typedef boost::shared_ptr<TZO> Ptr;
+
+    TZO();
+
+    TZO(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);
+
+    ~TZO();
+
+    int perform_update(const std::string& ip);
+};
+
+#endif
index d471c6e..36c3958 100644 (file)
@@ -15,6 +15,7 @@
 #include "dyndns.h"
 #include "dyns.h"
 #include "easydns.h"
+#include "tzo.h"
 
 #include <boost/foreach.hpp>
 
@@ -25,6 +26,7 @@ BOOST_CLASS_EXPORT_GUID(DHS, "DHS")
 BOOST_CLASS_EXPORT_GUID(DYNS, "DYNS")
 BOOST_CLASS_EXPORT_GUID(DYNDNS, "DYNDNS")
 BOOST_CLASS_EXPORT_GUID(EASYDNS, "EASYDNS")
+BOOST_CLASS_EXPORT_GUID(TZO, "TZO")
 
 
 namespace fs = boost::filesystem;