Add TODO entry about nochg response
[bpdyndnsd] / src / service_dyndns.cpp
index dd6fee0..8e49c53 100644 (file)
@@ -7,9 +7,8 @@
  * @license GPLv2
 */
 
-#include "service_dyndns.h"
+#include "service_dyndns.hpp"
 
-#include <time.h>
 #include <boost/foreach.hpp>
 
 using namespace std;
@@ -29,7 +28,8 @@ ServiceDyndns::ServiceDyndns()
  * @param _login The login name.
  * @param _password The corresponding password.
  */
-ServiceDyndns::ServiceDyndns(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)
+ServiceDyndns::ServiceDyndns(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, const string& _alternative_server)
+    : AlternativeServer(_alternative_server)
 {
     if ( _update_interval == -1 )        // If _update_interval is default po::option_desc (not specified via config)
         set_update_interval(0);              // use default protocol value
@@ -53,8 +53,7 @@ ServiceDyndns::ServiceDyndns(const string& _protocol, const string& _hostname, c
     set_logger(_logger);
 
     // create http helper class
-    HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
-    HTTPHelp.swap(_http_help);
+    HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
 
     BaseUrl = assemble_base_url(get_hostname());
 }
@@ -75,9 +74,14 @@ ServiceDyndns::~ServiceDyndns()
  */
 string ServiceDyndns::assemble_base_url(const string& fqhn) const
 {
-    string base_url;
+    string base_url = "https://";
+
+    // Test if a AlternativeServer name is given, needed for e.g. NO-IP
+    if ( AlternativeServer.empty() )
+        base_url.append("members.dyndns.org");
+    else
+        base_url.append(AlternativeServer);
 
-    base_url = "https://members.dyndns.org";
     base_url.append("/nic/update?hostname=");
     base_url.append(fqhn);
     base_url.append("&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG&myip=");
@@ -100,6 +104,13 @@ int ServiceDyndns::perform_update(const std::string& ip)
     string url = BaseUrl;
     url.append(ip);
 
+    if ( !HTTPHelp->is_initialized() )
+    {
+        get_logger()->print_httphelper_not_initialized();
+        HTTPHelp->re_initialize();
+        return -1;
+    }
+
     // Perform curl operation on given url
     long http_status_code = HTTPHelp->http_get(url);
 
@@ -113,6 +124,9 @@ int ServiceDyndns::perform_update(const std::string& ip)
         // Get the received http data.
         string curl_data = HTTPHelp->get_curl_data();
 
+        // Note: We don't handle "nochg" as this shouldn't happen
+        //       if only one client is active.
+
         if ( curl_data == good )
         {
             return 0;
@@ -133,15 +147,3 @@ int ServiceDyndns::perform_update(const std::string& ip)
 
     return -1;
 }
-
-
-/**
- * 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 ServiceDyndns::serialize(Archive & ar, const unsigned int version)
-{
-    ar & boost::serialization::base_object<Service>(*this);
-}