Fix 'occurred' typo
[bpdyndnsd] / src / service_gnudip.cpp
index 7109b6a..3f35cfd 100644 (file)
@@ -7,8 +7,8 @@
  * @license GPLv2
 */
 
-#include "service_gnudip.h"
-#include "util.h"
+#include "service_gnudip.hpp"
+#include "util.hpp"
 
 #include <time.h>
 #include <boost/foreach.hpp>
@@ -31,19 +31,24 @@ ServiceGnudip::ServiceGnudip()
  * @param _login The login name.
  * @param _password The corresponding password.
  */
-ServiceGnudip::ServiceGnudip(const string& _protocol, const string& _gnudip_server, 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)
+ServiceGnudip::ServiceGnudip(const string& _protocol, const string& _gnudip_server, 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 _max_equal_updates_in_succession, const int _dns_cache_ttl, const string& _proxy, const int _proxy_port)
     : GnudipServer(_gnudip_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
+        set_update_interval(15);              // use default protocol value
     else
         set_update_interval(_update_interval);
 
     if ( _max_updates_within_interval == -1 )
-        set_max_updates_within_interval(0);
+        set_max_updates_within_interval(3);
     else
         set_max_updates_within_interval(_max_updates_within_interval);
 
+    if ( _max_equal_updates_in_succession == -1 )
+        set_max_equal_updates_in_succession(2);
+    else
+        set_max_equal_updates_in_succession(_max_equal_updates_in_succession);
+
     if ( _dns_cache_ttl == -1 )
         set_dns_cache_ttl(60);
     else
@@ -56,11 +61,9 @@ ServiceGnudip::ServiceGnudip(const string& _protocol, const string& _gnudip_serv
     set_logger(_logger);
 
     // create http helper class
-    HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
-    HTTPHelp = _http_help;
-    _http_help.reset();
+    HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
 
-    BaseUrl = assemble_base_url(get_hostname(),_gnudip_server);
+    BaseUrl = assemble_base_url(_gnudip_server);
 }
 
 
@@ -74,10 +77,10 @@ ServiceGnudip::~ServiceGnudip()
 
 /**
  * Assemble the dyndns update url from the given fqhn
- * @param hostname The fqhn hostname to update IP for.
+ * @param gnudip_server The gnudip update server.
  * @return The assembled update url without IP.
  */
-string ServiceGnudip::assemble_base_url(const string& fqhn, const string& gnudip_server) const
+string ServiceGnudip::assemble_base_url(const string& gnudip_server) const
 {
     string base_url;
 
@@ -99,19 +102,19 @@ map<string,string> ServiceGnudip::parse_initial_request(const string& curl_data)
     map<string,string> response;
 
     // regex for salt
-    boost::regex expr_salt("<meta name=\"salt\" content=\"(.*)\">");
+    boost::regex expr_salt("<meta name=\"salt\" content=\"([^\"]*)\"");
     // regex for time
-    boost::regex expr_time("<meta name=\"time\" content=\"(.*)\">");
+    boost::regex expr_time("<meta name=\"time\" content=\"([^\"]*)\"");
     // regex for sign
-    boost::regex expr_sign("<meta name=\"sign\" content=\"(.*)\">");
+    boost::regex expr_sign("<meta name=\"sign\" content=\"([^\"]*)\"");
 
     boost::smatch matches;
 
     // Get the salt out of received http data
     if ( boost::regex_search(curl_data,matches,expr_salt) )
     {
-        response.insert(pair<string,string>("salt",matches[1]));
-        get_logger()->print_regex_match(expr_salt.str(),matches[1]);
+        response.insert(pair<string,string>("salt",matches[1].str()));  /*lint !e534 */
+        get_logger()->print_regex_match(expr_salt.str(),matches[1].str());
     }
     else
     {
@@ -123,8 +126,8 @@ map<string,string> ServiceGnudip::parse_initial_request(const string& curl_data)
     // Get the time out of received http data
     if ( boost::regex_search(curl_data,matches,expr_time) )
     {
-        response.insert(pair<string,string>("time",matches[1]));
-        get_logger()->print_regex_match(expr_salt.str(),matches[1]);
+        response.insert(pair<string,string>("time",matches[1].str()));  /*lint !e534 */
+        get_logger()->print_regex_match(expr_salt.str(),matches[1].str());
     }
     else
     {
@@ -136,8 +139,8 @@ map<string,string> ServiceGnudip::parse_initial_request(const string& curl_data)
     // Get the sign out of received http data
     if ( boost::regex_search(curl_data,matches,expr_sign) )
     {
-        response.insert(pair<string,string>("sign",matches[1]));
-        get_logger()->print_regex_match(expr_salt.str(),matches[1]);
+        response.insert(pair<string,string>("sign",matches[1].str()));  /*lint !e534 */
+        get_logger()->print_regex_match(expr_salt.str(),matches[1].str());
     }
     else
     {
@@ -151,6 +154,35 @@ map<string,string> ServiceGnudip::parse_initial_request(const string& curl_data)
 
 
 /**
+ * Parses the data received from the update request, which should contain the return code.
+ * @param curl_data The complete received curl data.
+ * @return A string containing the return code of the update request.
+ */
+string ServiceGnudip::parse_return_code(const string& curl_data) const
+{
+    string return_code;
+
+    // regex for return code
+    boost::regex expr_retc("<meta name=\"retc\" content=\"([^\"]*)\"");
+    boost::smatch matches;
+
+    // Get the return code out of received http data
+    if ( boost::regex_search(curl_data,matches,expr_retc) )
+    {
+        return_code = matches[1].str();
+        get_logger()->print_regex_match(expr_retc.str(),matches[1].str());
+    }
+    else
+    {
+        get_logger()->print_no_regex_match(expr_retc.str(),curl_data);
+        return return_code;
+    }
+
+    return return_code;
+}
+
+
+/**
  * Get the assembled update url.
  * @param salt Salt from the initial request
  * @param time Time from the initial request
@@ -159,20 +191,22 @@ map<string,string> ServiceGnudip::parse_initial_request(const string& curl_data)
  * @param ip IP to update
  * @return The assembled update url.
  */
-string ServiceGnudip::assemble_update_url(const string& salt, const string& time, const string& sign, const string& secret, const string& ip) const
+string ServiceGnudip::assemble_update_url(const string& salt, const string& curr_time, const string& sign, const string& secret, const string& ip) const
 {
         string url = BaseUrl;
 
         url.append("?salt=");
         url.append(salt);
         url.append("&time=");
-        url.append(time);
+        url.append(curr_time);
         url.append("&sign=");
         url.append(sign);
         url.append("&user=");
         url.append(get_login());
         url.append("&domn=");
-        url.append(get_hostname());
+        string fqhn = get_hostname();
+        string domain = fqhn.substr(fqhn.find('.')+1);
+        url.append(domain);
         url.append("&pass=");
         url.append(secret);
         url.append("&reqc=0&addr=");
@@ -187,101 +221,128 @@ string ServiceGnudip::assemble_update_url(const string& salt, const string& time
  * @param ip IP Address to set.
  * @return 0 if all is fine, -1 otherwise.
  */
-int ServiceGnudip::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceGnudip::perform_update(const std::string& ip)
 {
-    // initial request
-    long http_status_code = HTTPHelp->http_get(BaseUrl);
-
-    get_logger()->print_http_status_code(BaseUrl,http_status_code);
-
-    if ( http_status_code == 200 )
+    if ( HTTPHelp->is_initialized() )
     {
-        // Get the received http data which should contain the salt, time and sign
-        string curl_data = HTTPHelp->get_curl_data();
+        // initial request
+        long http_status_code = HTTPHelp->http_get(BaseUrl);
 
-        // Parse salt, time and sign out of the received data
-        map<string,string> salt_time_sign = parse_initial_request(curl_data);
+        get_logger()->print_http_status_code(BaseUrl,http_status_code);
 
-        if ( salt_time_sign.empty() )
+        if ( http_status_code == 200 )
         {
-            get_logger()->print_could_not_parse_received_data(curl_data);
-            return -1;
-        }
-
-        // at this point we have salt, time and sign parsed successfully
-        string salt, time, sign;
+            // Get the received http data which should contain the salt, time and sign
+            string curl_data = HTTPHelp->get_curl_data();
 
-        map<string,string>::iterator iter = salt_time_sign.find("salt");
-        if ( iter != salt_time_sign.end() )
-            salt = iter->second;
+            // Parse salt, time and sign out of the received data
+            map<string,string> salt_time_sign = parse_initial_request(curl_data);
 
-        iter = salt_time_sign.find("time");
-        if ( iter != salt_time_sign.end() )
-            time = iter->second;
+            if ( salt_time_sign.empty() )
+            {
+                get_logger()->print_could_not_parse_received_data(curl_data);
+                return GenericError;
+            }
 
-        iter = salt_time_sign.find("sign");
-        if ( iter != salt_time_sign.end() )
-            sign = iter->second;
+            // at this point we have salt, time and sign parsed successfully
+            string salt, sign_time, sign;
 
-        if ( salt.empty() || time.empty() || sign.empty() )
-            get_logger()->print_could_not_get_initial_gnudip_data();
+            map<string,string>::iterator iter = salt_time_sign.find("salt");
+            if ( iter != salt_time_sign.end() )
+                salt = iter->second;
 
-        // compute md5 sum from users password and get the HEX representation
-        string pw_md5_hex = Util::compute_md5_digest(get_password());
+            iter = salt_time_sign.find("time");
+            if ( iter != salt_time_sign.end() )
+                sign_time = iter->second;
 
-        // append "." and salt and compute md5 sum and get the HEX representation
-        pw_md5_hex.append(".");
-        pw_md5_hex.append(salt);
-        string secret = Util::compute_md5_digest(pw_md5_hex);
+            iter = salt_time_sign.find("sign");
+            if ( iter != salt_time_sign.end() )
+                sign = iter->second;
 
-        // Now its time to issue the second http_get operation
-        string url = assemble_update_url(salt, time, sign, secret, ip);
+            if ( salt.empty() || sign_time.empty() || sign.empty() )
+                get_logger()->print_could_not_get_initial_gnudip_data();
 
-        // perform the update operation
-        http_status_code = HTTPHelp->http_get(url);
+            // compute md5 sum from users password and get the HEX representation
+            string pw_md5_hex;
+            try
+            {
+                pw_md5_hex = Util::compute_md5_digest(get_password());
+            }
+            catch ( exception& e )
+            {
+                get_logger()->print_exception_md5_sum(e.what());
+                return GenericError;
+            }
+            catch ( ... )
+            {
+                get_logger()->print_exception_md5_sum("Unknown exception");
+                return GenericError;
+            }
 
-        get_logger()->print_http_status_code(url,http_status_code);
+            // append "." and salt and compute md5 sum and get the HEX representation
+            pw_md5_hex.append(".");
+            pw_md5_hex.append(salt);
 
-        if ( http_status_code == 200 )
-        {
-            // parse the update request return code
-            string update_return_code = HTTPHelp->get_curl_data();
-            if ( update_return_code == "0" )
+            string secret;
+            try
+            {
+                secret = Util::compute_md5_digest(pw_md5_hex);
+            }
+            catch ( exception& e )
+            {
+                get_logger()->print_exception_md5_sum(e.what());
+                return GenericError;
+            }
+            catch ( ... )
             {
-                return 0;
+                get_logger()->print_exception_md5_sum("Unknown exception");
+                return GenericError;
             }
-            else if ( update_return_code == "1" )
+
+            // Now its time to issue the second http_get operation
+            string url = this->assemble_update_url(salt, sign_time, sign, secret, ip);
+
+            // perform the update operation
+            http_status_code = HTTPHelp->http_get(url);
+
+            get_logger()->print_http_status_code(url,http_status_code);
+
+            if ( http_status_code == 200 )
             {
-                get_logger()->print_http_not_authorized(url,get_login(),get_password());
+                // parse the update request return code
+                curl_data = HTTPHelp->get_curl_data();
+                string update_return_code = parse_return_code(curl_data);
+                if ( update_return_code == "0" )
+                {
+                    return UpdateOk;
+                }
+                else if ( update_return_code == "1" )
+                {
+                    get_logger()->print_service_not_authorized(url,get_login(),get_password());
+                    return NotAuth;
+                }
+                else
+                {
+                    get_logger()->print_update_failure(url,update_return_code);
+                    return UpdateError;
+                }
             }
             else
             {
-                get_logger()->print_update_failure(url,update_return_code);
+                // second http get operation (update) was not successful
+                get_logger()->print_update_failure(url,http_status_code);
             }
         }
         else
         {
-            // second http get operation (update) was not successful
-            get_logger()->print_update_failure(url,http_status_code);
+            // first http get operation was not successful
+            get_logger()->print_update_failure(BaseUrl,http_status_code);
         }
     }
     else
     {
-        // first http get operation was not successful
-        get_logger()->print_update_failure(BaseUrl,http_status_code);
+        get_logger()->print_httphelper_not_initialized();
+        HTTPHelp->re_initialize();
     }
-
-    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 ServiceGnudip::serialize(Archive & ar, const unsigned int version)
-{
-    ar & boost::serialization::base_object<Service>(*this);
+    return GenericError;
 }