First steps in fine tuning and improving error handling.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 12 May 2010 12:16:05 +0000 (14:16 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 12 May 2010 12:16:05 +0000 (14:16 +0200)
15 files changed:
src/config.cpp
src/config.h
src/httphelper.cpp
src/httphelper.h
src/logger.cpp
src/logger.h
src/service_dhs.cpp
src/service_dyndns.cpp
src/service_dyns.cpp
src/service_easydns.cpp
src/service_gnudip.cpp
src/service_tzo.cpp
src/service_zoneedit.cpp
src/serviceholder.cpp
src/serviceholder.h

index af543b2..149d5c8 100644 (file)
@@ -36,9 +36,82 @@ typedef boost::shared_ptr<boost::program_options::options_description> Options_d
 
 using namespace std;
 
+
 /**
  * Default Constructor. Available command line and config file options with their default values are defined here.
  */
+Config::Config()
+    : Log(new Logger)
+    , ServiceHolder(new Serviceholder)
+    , DaemonMode(false)
+    , Syslog(false)
+    , EnableIPv6(false)
+    , Loglevel(0)
+    , ConfigPath("/etc/bpdyndnsd")
+    , WebcheckInterval(0)
+    , ProxyPort(0)
+    , ExternalWarningLog("")
+    , ExternalWarningLevel(0)
+{
+    // Available service description config options
+    po::options_description opt_desc_service("Service description options");
+    opt_desc_service.add_options()
+        ("protocol",po::value<string>(),"The service protocol.")
+        ("server",po::value<string>(),"Servername needed for gnudip protocol.")
+        ("host",po::value<string>(),"The hostname to update.")
+        ("login",po::value<string>(),"Login name.")
+        ("password",po::value<string>(),"Corresponding password.")
+        ("update_interval",po::value<int>()->default_value(-1),"Update interval in minutes.")
+        ("max_updates_within_interval",po::value<int>()->default_value(-1),"How many updates can be made in one interval.")
+        ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
+    ;
+
+    // Available command line only options
+    po::options_description opt_desc_cmd_only("Command line only options");
+    opt_desc_cmd_only.add_options()
+        ("help,?","Show help.")
+        ("version,v","Show version.")
+        ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
+    ;
+
+     // Available generic options. Valid on cmd or in config file.
+    po::options_description opt_desc_generic("Generic config options");
+    opt_desc_generic.add_options()
+        ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
+        ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
+        ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
+        ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
+        ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
+        ("webcheck_url_alt",po::value<string>()->default_value(""),"Use this alternative URL to determine IP.")
+        ("webcheck_interval",po::value<int>()->default_value(10),"The webcheck interval in minutes.")
+        ("http_proxy",po::value<string>(),"Use this proxy for all http requests.")
+        ("http_proxy_port",po::value<int>(),"Port of the proxy.")
+        ("external_warning_log",po::value<string>()->default_value(""),"External programm to pass warning log messages to.")
+        ("external_warning_level",po::value<int>()->default_value(0),"Warning messages of which loglevel should be passed to external programm.")
+    ;
+
+    // Define valid command line parameters
+    Options_descriptionPtr _opt_desc_cmd(new po::options_description("Command line options"));
+    OptDescCmd.swap(_opt_desc_cmd);
+    OptDescCmd->add(opt_desc_cmd_only);
+    OptDescCmd->add(opt_desc_generic);
+    OptDescCmd->add(opt_desc_service);
+
+    // Define valid config file options
+    Options_descriptionPtr _opt_desc_conf_main(new po::options_description("Config file options"));
+    OptDescConfMain.swap(_opt_desc_conf_main);
+    OptDescConfMain->add(opt_desc_generic);
+
+    // Define valid service file options
+    Options_descriptionPtr _opt_desc_conf_service(new po::options_description("Service file options"));
+    OptDescConfService.swap(_opt_desc_conf_service);
+    OptDescConfService->add(opt_desc_service);
+}
+
+
+/**
+ * Constructor with Logger and Serviceholder objects. Available command line and config file options with their default values are defined here.
+ */
 Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
     : Log(_log)
     , ServiceHolder(_serviceholder)
@@ -235,19 +308,19 @@ int Config::parse_cmd_line(int argc, char *argv[])
             ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
 
     }
-    catch(po::unknown_option e)
+    catch( po::unknown_option& e )
     {
         Log->print_unknown_cmd_option(e.what());
         Log->print_usage(OptDescCmd);
         return -1;
     }
-    catch(po::multiple_occurrences e)
+    catch( po::multiple_occurrences& e )
     {
         Log->print_multiple_cmd_option(e.what());
         Log->print_usage(OptDescCmd);
         return -1;
     }
-    catch( po::error e )
+    catch( po::error& e )
     {
         Log->print_error_parsing_cmd(e.what());
         Log->print_usage(OptDescCmd);
@@ -397,20 +470,20 @@ int Config::load_service_config_file(const string& full_filename)
                 return -1;
             }
         }
-        catch ( po::unknown_option e )
+        catch ( po::unknown_option& e )
         {
             // unknown option in config file detected
             service_config_file.close();
             Log->print_unknown_service_conf_option(full_filename,e.what());
             return -1;
         }
-        catch( po::multiple_occurrences e )
+        catch( po::multiple_occurrences& e )
         {
             service_config_file.close();
             Log->print_multiple_service_conf_option(full_filename,e.what());
             return -1;
         }
-        catch( po::error e )
+        catch( po::error& e )
         {
             service_config_file.close();
             Log->print_error_parsing_config_file(full_filename,e.what());
@@ -486,14 +559,14 @@ int Config::load_main_config_file(const string& full_filename)
                 ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
 
         }
-        catch ( po::unknown_option e )      // at the moment 04-08-2009 this exception is never thrown :-(
+        catch ( po::unknown_option& e )      // at the moment 04-08-2009 this exception is never thrown :-(
         {
             // unknown option in main config file detected
             main_config_file.close();
             Log->print_unknown_main_conf_option(e.what());
             return -1;
         }
-        catch(po::multiple_occurrences e)
+        catch(po::multiple_occurrences& e)
         {
             main_config_file.close();
             Log->print_multiple_main_conf_option(full_filename,e.what());
index 51590a5..8de595b 100644 (file)
@@ -55,6 +55,8 @@ public:
 
     typedef boost::shared_ptr<Config> Ptr;
 
+    Config();
+
     Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder);
 
     ~Config();
index 8f7d183..645a99d 100644 (file)
 
 using namespace std;
 
+
+/**
+ * Default Constructor.
+ */
+HTTPHelper::HTTPHelper()
+    : Log(new Logger)
+    , ProxyPort(0)
+    , CurlError(CURLE_OK)
+    , CurlEasyHandle(NULL)
+{
+}
+
+
 /**
  * Constructor. Use this constructor if HTTP AUTH should be used. Username and password will then be set as HTTP auth options.
  * @param _log Logger Object 
@@ -23,9 +36,13 @@ HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_
     : Log(_log)
     , Proxy(_proxy)
     , ProxyPort(_proxy_port)
+    , CurlError(CURLE_OK)
 {
     CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
-    set_curl_auth(_username,_password);
+    if ( CurlEasyHandle != NULL )
+    {
+        set_curl_auth(_username,_password);
+    }
 }
 
 
@@ -39,6 +56,7 @@ HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_
     : Log(_log)
     , Proxy(_proxy)
     , ProxyPort(_proxy_port)
+    , CurlError(CURLE_OK)
 {
     CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
 }
@@ -50,7 +68,11 @@ HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_
 HTTPHelper::~HTTPHelper()
 {
     // Free memory
-    curl_easy_cleanup(CurlEasyHandle);
+    if ( CurlEasyHandle != NULL )
+    {
+        curl_easy_cleanup(CurlEasyHandle);
+        CurlEasyHandle = NULL;
+    }
 }
 
 
@@ -61,32 +83,36 @@ HTTPHelper::~HTTPHelper()
  */
 long HTTPHelper::http_get(const string& url)
 {
-    int curl_err_code;
+    CURLcode curl_err_code;
     long curl_info;
 
-    set_curl_url(url);
-
-    if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != 0 )
+    if ( CurlEasyHandle != NULL )
     {
-        Log->print_curl_error(url,curl_err_code,CurlErrBuff);
+        set_curl_url(url);
+
+        if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != CURLE_OK )
+        {
+            Log->print_curl_error(url,curl_err_code,CurlErrBuff);
+            CurlWritedataBuff.clear();
+            return -1;
+        }
+        if ( (curl_err_code = curl_easy_getinfo(CurlEasyHandle,CURLINFO_RESPONSE_CODE,&curl_info)) != CURLE_OK )
+        {
+            Log->print_curl_error(url,curl_err_code);
+            CurlWritedataBuff.clear();
+            return -1;
+        }
+
+        Log->print_curl_data(CurlWritedataBuff);
+
+        // Copy the received data received via curl from the curl data buffer member to the received data member. This is needed because curl appends data to the buffer rather than overrites it.
+        ReceivedCurlData = CurlWritedataBuff;
         CurlWritedataBuff.clear();
-        return -1;
-    }
-    if ( (curl_err_code = curl_easy_getinfo(CurlEasyHandle,CURLINFO_RESPONSE_CODE,&curl_info)) != 0 )
-    {
-        Log->print_curl_error(url,curl_err_code);
-        CurlWritedataBuff.clear();
-        return -1;
-    }
-
-    Log->print_curl_data(CurlWritedataBuff);
-
-    // Copy the received data received via curl from the curl data buffer member to the received data member. This is needed because curl appends data to the buffer rather than overrites it.
-    ReceivedCurlData = CurlWritedataBuff;
-    CurlWritedataBuff.clear();
 
-    // Operation performed without any problems so we can return the curl_info
-    return curl_info;
+        // Operation performed without any problems so we can return the curl_info
+        return curl_info;
+    }
+    return -1;
 }
 
 
@@ -106,25 +132,50 @@ string HTTPHelper::get_curl_data() const
  * @param curl_err_buff A pointer to an char array which will be filled with error message.
  * @return A pointer to the easy curl handle.
  */
-CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const
+CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff)
 {
     string user_agent = "Bullet Proof DYNDNS Daemon 0.1.1 - Intra2net AG 2009";
 
     CURL *curl_easy_handle = curl_easy_init();
+    if ( curl_easy_handle == NULL )
+    {
+        // something went wrong.
+        CurlError = CURLE_FAILED_INIT;
+        Log->print_curl_error_init("Could not initialize CURL object.",CURLE_FAILED_INIT);
+        return NULL;
+    }
 
-    curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
-    curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
-    curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
-    curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
-    curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
-    curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
-    curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
-    curl_easy_setopt(curl_easy_handle,CURLOPT_USERAGENT,&user_agent);
+    if ( CurlError == CURLE_OK )
+        CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
+    if ( CurlError == CURLE_OK)
+        CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
+    if ( CurlError == CURLE_OK)
+        CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
+    if ( CurlError == CURLE_OK)
+        CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
+    if ( CurlError == CURLE_OK)
+        CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
+    if ( CurlError == CURLE_OK)
+        CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
+    if ( CurlError == CURLE_OK)
+        CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
+    if ( CurlError == CURLE_OK)
+        CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_USERAGENT,&user_agent);
 
     if ( !Proxy.empty() )
     {
-        curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
-        curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
+        if ( CurlError == CURLE_OK)
+            CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
+        if ( CurlError == CURLE_OK)
+            CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
+    }
+
+    if ( CurlError != CURLE_OK )
+    {
+        // Some options could not be set, so destroy the CURL handle.
+        Log->print_curl_error_init("Could not set CURL options properly.",CurlError);
+        curl_easy_cleanup(CurlEasyHandle);
+        curl_easy_handle = NULL;
     }
 
     return curl_easy_handle;
@@ -132,12 +183,29 @@ CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) con
 
 
 /**
+ * Test if the curl handle is initialized correctly.
+ * @return True if correctly initialized, false if something went wrong during initialization.
+ */
+bool HTTPHelper::is_initialized()
+{
+    if ( (CurlError == CURLE_OK) && (CurlEasyHandle != NULL) )
+    {
+        return true;
+    }
+    return false;
+}
+
+
+/**
  * Sets a url to the easy curl handle
  * @param url The url to set.
  */
 void HTTPHelper::set_curl_url(const string& url)
 {
-    curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
+    if ( CurlEasyHandle != NULL )
+    {
+        curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
+    }
 }
 
 
@@ -148,8 +216,11 @@ void HTTPHelper::set_curl_url(const string& url)
  */
 void HTTPHelper::set_curl_auth(const string& username, const string& password)
 {
-    curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
-    curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
+    if ( CurlEasyHandle != NULL )
+    {
+        curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
+        curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
+    }
 }
 
 
index bbec3ec..6436ac8 100644 (file)
@@ -24,6 +24,7 @@ private:
     Logger::Ptr Log;
     std::string Proxy;
     int ProxyPort;
+    CURLcode CurlError;
     CURL* CurlEasyHandle;
     std::string CurlWritedataBuff;
     std::string ReceivedCurlData;
@@ -31,6 +32,7 @@ private:
 
     void set_curl_url(const std::string& url);
     void set_curl_auth(const std::string& username, const std::string& password);
+    CURL* init_curl(std::string& curl_writedata_buff, char* curl_err_buff);
 
 public:
 
@@ -44,12 +46,12 @@ public:
 
     ~HTTPHelper();
 
-    CURL* init_curl(std::string& curl_writedata_buff, char* curl_err_buff) const;
-
     long http_get(const std::string& ip);
 
     std::string get_curl_data() const;
 
+    bool is_initialized();
+
     // libcurl is a C library, so we have to make the callback member function static :-(
     static int http_receive(char *inBuffer, size_t size, size_t nmemb, std::string *outBuffer);
 };
index 1bc189e..fa3fdf9 100644 (file)
@@ -1141,19 +1141,59 @@ void Logger::print_no_domain_part(const string& hostname) const
 
 
 /**
+ * Service is not initialized properly.
+ * @param service The service.
+ */
+void Logger::print_service_not_initialized(const std::string& service) const
+{
+    int level = 0;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Service: " << service << " is not initialized properly." << endl;
+        log_warning(msg.str(),level);
+    }
+}
+
+
+/**
+ * An curl error occured.
+ * @param msg The error message
+ * @param curl_err_code The resulting curl error code
+ */
+void Logger::print_curl_error_init(const std::string& msg, const CURLcode curl_err_code) const
+{
+    string curl_err = "";
+
+    if ( curl_err_code == CURLE_FAILED_INIT )
+        curl_err = "CURLE_FAILED_INIT";
+    else
+        curl_err = "UNKNOWN";
+
+    int level = 0;
+    if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
+    {
+        ostringstream msg;
+        msg << "Curl error: " << msg << " Curl error code: " << curl_err_code << " "<< curl_err << endl;
+        log_error(msg.str(),level);
+    }
+}
+
+
+/**
  * An curl error occured.
  * @param url The url requested by the curl operation
  * @param curl_err_code The resulting curl error code
  */
-void Logger::print_curl_error(const string& url, const int curl_err_code) const
+void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) const
 {
     string curl_err = "";
 
-    if ( curl_err_code == 3 )
+    if ( curl_err_code == CURLE_URL_MALFORMAT )
         curl_err = "CURLE_URL_MALFORMAT";
-    else if ( curl_err_code == 6 )
+    else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
         curl_err = "CURLE_COULDNT_RESOLVE_HOST";
-    else if ( curl_err_code == 7 )
+    else if ( curl_err_code == CURLE_COULDNT_CONNECT )
         curl_err = "CURLE_COULDNT_CONNECT";
     else
         curl_err = "UNKNOWN";
index 85f6d99..0367832 100644 (file)
@@ -16,6 +16,8 @@
 #include <boost/program_options.hpp>
 #include <boost/shared_ptr.hpp>
 
+#include <curl/curl.h>
+
 class Logger
 {
 
@@ -178,7 +180,9 @@ public:
 
     void print_no_domain_part(const std::string& hostname) const;
 
-    void print_curl_error(const std::string& url, const int curl_err_code) const;
+    void print_curl_error_init(const std::string& msg, const CURLcode curl_err_code) const;
+
+    void print_curl_error(const std::string& url, const CURLcode curl_err_code) const;
 
     void print_curl_error(const std::string& url, const int curl_err_code, const char * curl_err_buff) const;
 
@@ -186,6 +190,8 @@ public:
 
     void print_service_not_authorized(const std::string& service, const std::string& username, const std::string& password) const;
 
+    void print_service_not_initialized(const std::string& service) const;
+
     void print_http_status_code(const std::string& url, const long http_code) const;
 
     void print_update_failure(const std::string& url, const std::string& curl_data) const;
index df107a7..578a0a9 100644 (file)
@@ -139,37 +139,43 @@ int ServiceDhs::perform_update(const std::string& ip)
     string url = BaseUrl;
     url.append(ip);
 
-    // Perform curl operation on given url.
-    long http_status_code = HTTPHelp->http_get(url);
-
-    get_logger()->print_http_status_code(url,http_status_code);
-
-    // Check the status code for protocol errors.
-    if ( http_status_code == 200 )
+    if ( HTTPHelp->is_initialized() == true )
     {
-        // Get the received http data.
-        string curl_data = HTTPHelp->get_curl_data();
+        // Perform curl operation on given url.
+        long http_status_code = HTTPHelp->http_get(url);
 
-        // Search for the following string in the received http data
-        boost::regex expr_done("Updating ip on .*: done");
-        if ( boost::regex_search(curl_data,expr_done) )
+        get_logger()->print_http_status_code(url,http_status_code);
+
+        // Check the status code for protocol errors.
+        if ( http_status_code == 200 )
+        {
+            // Get the received http data.
+            string curl_data = HTTPHelp->get_curl_data();
+
+            // Search for the following string in the received http data
+            boost::regex expr_done("Updating ip on .*: done");
+            if ( boost::regex_search(curl_data,expr_done) )
+            {
+                // Update successful
+                return 0;
+            }
+            else
+            {
+                get_logger()->print_update_failure(url,curl_data);
+            }
+        }
+        else if ( http_status_code == 401 )
         {
-            // Update successful
-            return 0;
+            get_logger()->print_service_not_authorized(url,get_login(),get_password());
         }
         else
         {
-            get_logger()->print_update_failure(url,curl_data);
+            get_logger()->print_update_failure(url,http_status_code);
         }
     }
-    else if ( http_status_code == 401 )
-    {
-        get_logger()->print_service_not_authorized(url,get_login(),get_password());
-    }
     else
     {
-        get_logger()->print_update_failure(url,http_status_code);
+        get_logger()->print_service_not_initialized(url);
     }
-
     return -1;
 }
index a867211..3905e36 100644 (file)
@@ -100,36 +100,42 @@ int ServiceDyndns::perform_update(const std::string& ip)
     string url = BaseUrl;
     url.append(ip);
 
-    // Perform curl operation on given url
-    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 dyndns update errors
-    if ( http_status_code == 200 )
+    if ( HTTPHelp->is_initialized() == true )
     {
-        // Get the received http data.
-        string curl_data = HTTPHelp->get_curl_data();
+        // Perform curl operation on given url
+        long http_status_code = HTTPHelp->http_get(url);
 
-        if ( curl_data == good )
-        {
-            return 0;
-        }
-        else if ( curl_data == "badauth" )
+        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 dyndns update errors
+        if ( http_status_code == 200 )
         {
-            get_logger()->print_service_not_authorized(url,get_login(),get_password());
+            // Get the received http data.
+            string curl_data = HTTPHelp->get_curl_data();
+
+            if ( curl_data == good )
+            {
+                return 0;
+            }
+            else if ( curl_data == "badauth" )
+            {
+                get_logger()->print_service_not_authorized(url,get_login(),get_password());
+            }
+            else
+            {
+                get_logger()->print_update_failure(url, curl_data);
+            }
         }
         else
         {
-            get_logger()->print_update_failure(url, curl_data);
+            get_logger()->print_update_failure(url,http_status_code);
         }
     }
     else
     {
-        get_logger()->print_update_failure(url,http_status_code);
+        get_logger()->print_service_not_initialized(url);
     }
-
     return -1;
 }
index b3a68cd..d3d917c 100644 (file)
@@ -106,36 +106,42 @@ int ServiceDyns::perform_update(const std::string& ip)
     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 )
+    if ( HTTPHelp->is_initialized() == true )
     {
-        // Get the received http data and parse the status code.
-        string curl_data = HTTPHelp->get_curl_data();
-        string status_code = Util::parse_status_code(curl_data);
+        long http_status_code = HTTPHelp->http_get(url);
 
-        if ( status_code == "200" )
-        {
-            return 0;
-        }
-        else if ( status_code == "401" )
+        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_logger()->print_service_not_authorized(url,get_login(),get_password());
+            // Get the received http data and parse the status code.
+            string curl_data = HTTPHelp->get_curl_data();
+            string status_code = Util::parse_status_code(curl_data);
+
+            if ( status_code == "200" )
+            {
+                return 0;
+            }
+            else if ( status_code == "401" )
+            {
+                get_logger()->print_service_not_authorized(url,get_login(),get_password());
+            }
+            else
+            {
+                get_logger()->print_update_failure(url,curl_data);
+            }
         }
         else
         {
-            get_logger()->print_update_failure(url,curl_data);
+            get_logger()->print_update_failure(url,http_status_code);
         }
     }
     else
     {
-        get_logger()->print_update_failure(url,http_status_code);
+        get_logger()->print_service_not_initialized(url);
     }
-
     return -1;
 }
index 7a8904c..776277b 100644 (file)
@@ -60,7 +60,7 @@ ServiceEasydns::ServiceEasydns(const string& _protocol, const string& _hostname,
     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));
-    
+
     // extract domain part from hostname
     list<string> host_domain_part = separate_domain_and_host_part(get_hostname());
 
@@ -185,36 +185,42 @@ int ServiceEasydns::perform_update(const std::string& ip)
     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 easydns update errors
-    if ( http_status_code == 200 )
+    if ( HTTPHelp->is_initialized() == true )
     {
-        // Get the received http data.
-        string curl_data = HTTPHelp->get_curl_data();
-        string status_code = Util::parse_status_code(curl_data,"\n");
+        long http_status_code = HTTPHelp->http_get(url);
 
-        if ( status_code == "NOERROR" )
-        {
-            return 0;
-        }
-        else if ( status_code == "NOACCESS" )
+        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 easydns update errors
+        if ( http_status_code == 200 )
         {
-            get_logger()->print_service_not_authorized(url,get_login(),get_password());
+            // Get the received http data.
+            string curl_data = HTTPHelp->get_curl_data();
+            string status_code = Util::parse_status_code(curl_data,"\n");
+
+            if ( status_code == "NOERROR" )
+            {
+                return 0;
+            }
+            else if ( status_code == "NOACCESS" )
+            {
+                get_logger()->print_service_not_authorized(url,get_login(),get_password());
+            }
+            else
+            {
+                get_logger()->print_update_failure(url, curl_data);
+            }
         }
         else
         {
-            get_logger()->print_update_failure(url, curl_data);
+            get_logger()->print_update_failure(url,http_status_code);
         }
     }
     else
     {
-        get_logger()->print_update_failure(url,http_status_code);
+        get_logger()->print_service_not_initialized(url);
     }
-
     return -1;
 }
index aaba8e5..17c24b5 100644 (file)
@@ -188,106 +188,112 @@ string ServiceGnudip::assemble_update_url(const string& salt, const string& time
  */
 int 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() == true )
     {
-        // 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;
-        }
+            // Get the received http data which should contain the salt, time and sign
+            string curl_data = HTTPHelp->get_curl_data();
 
-        // at this point we have salt, time and sign parsed successfully
-        string salt, time, sign;
+            // Parse salt, time and sign out of the received data
+            map<string,string> salt_time_sign = parse_initial_request(curl_data);
 
-        map<string,string>::iterator iter = salt_time_sign.find("salt");
-        if ( iter != salt_time_sign.end() )
-            salt = iter->second;
+            if ( salt_time_sign.empty() )
+            {
+                get_logger()->print_could_not_parse_received_data(curl_data);
+                return -1;
+            }
 
-        iter = salt_time_sign.find("time");
-        if ( iter != salt_time_sign.end() )
-            time = iter->second;
+            // at this point we have salt, time and sign parsed successfully
+            string salt, time, sign;
 
-        iter = salt_time_sign.find("sign");
-        if ( iter != salt_time_sign.end() )
-            sign = iter->second;
+            map<string,string>::iterator iter = salt_time_sign.find("salt");
+            if ( iter != salt_time_sign.end() )
+                salt = iter->second;
 
-        if ( salt.empty() || time.empty() || sign.empty() )
-            get_logger()->print_could_not_get_initial_gnudip_data();
+            iter = salt_time_sign.find("time");
+            if ( iter != salt_time_sign.end() )
+                time = iter->second;
 
-        // 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 ( invalid_argument e )
-        {
-            get_logger()->print_exception_md5_sum(e.what());
-            return -1;
-        }
+            iter = salt_time_sign.find("sign");
+            if ( iter != salt_time_sign.end() )
+                sign = 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;
-        try
-        {
-            secret = Util::compute_md5_digest(pw_md5_hex);
-        }
-        catch ( invalid_argument e )
-        {
-            get_logger()->print_exception_md5_sum(e.what());
-            return -1;
-        }
+            if ( salt.empty() || time.empty() || sign.empty() )
+                get_logger()->print_could_not_get_initial_gnudip_data();
 
-        // Now its time to issue the second http_get operation
-        string url = assemble_update_url(salt, time, sign, secret, ip);
-
-        // 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 ( invalid_argument e )
+            {
+                get_logger()->print_exception_md5_sum(e.what());
+                return -1;
+            }
 
-        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
             {
-                return 0;
+                secret = Util::compute_md5_digest(pw_md5_hex);
             }
-            else if ( update_return_code == "1" )
+            catch ( invalid_argument e )
             {
-                get_logger()->print_service_not_authorized(url,get_login(),get_password());
+                get_logger()->print_exception_md5_sum(e.what());
+                return -1;
+            }
+
+            // Now its time to issue the second http_get operation
+            string url = assemble_update_url(salt, 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 )
+            {
+                // parse the update request return code
+                string update_return_code = HTTPHelp->get_curl_data();
+                if ( update_return_code == "0" )
+                {
+                    return 0;
+                }
+                else if ( update_return_code == "1" )
+                {
+                    get_logger()->print_service_not_authorized(url,get_login(),get_password());
+                }
+                else
+                {
+                    get_logger()->print_update_failure(url,update_return_code);
+                }
             }
             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_service_not_initialized(BaseUrl);
     }
-
     return -1;
 }
index 99c12a8..27e062f 100644 (file)
@@ -106,36 +106,42 @@ int ServiceTzo::perform_update(const std::string& ip)
     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 )
+    if ( HTTPHelp->is_initialized() == true )
     {
-        // Get the received http data and parse the status code.
-        string curl_data = HTTPHelp->get_curl_data();
-        string status_code = Util::parse_status_code(curl_data,"\r\n");
+        long http_status_code = HTTPHelp->http_get(url);
 
-        if ( status_code == "200" )
-        {
-            return 0;
-        }
-        else if ( status_code == "401" )
+        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_logger()->print_service_not_authorized(url,get_login(),get_password());
+            // Get the received http data and parse the status code.
+            string curl_data = HTTPHelp->get_curl_data();
+            string status_code = Util::parse_status_code(curl_data,"\r\n");
+
+            if ( status_code == "200" )
+            {
+                return 0;
+            }
+            else if ( status_code == "401" )
+            {
+                get_logger()->print_service_not_authorized(url,get_login(),get_password());
+            }
+            else
+            {
+                get_logger()->print_update_failure(url,curl_data);
+            }
         }
         else
         {
-            get_logger()->print_update_failure(url,curl_data);
+            get_logger()->print_update_failure(url,http_status_code);
         }
     }
     else
     {
-        get_logger()->print_update_failure(url,http_status_code);
+        get_logger()->print_service_not_initialized(url);
     }
-
     return -1;
 }
index 75ef63a..ccae82b 100644 (file)
@@ -100,36 +100,42 @@ int ServiceZoneedit::perform_update(const std::string& ip)
     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 )
+    if ( HTTPHelp->is_initialized() == true )
     {
-        // Get the received http data and parse the status code.
-        string curl_data = HTTPHelp->get_curl_data();
-        string status_code = Util::parse_status_code(curl_data);
+        long http_status_code = HTTPHelp->http_get(url);
 
-        if ( status_code == "200" )
+        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 = Util::parse_status_code(curl_data);
+
+            if ( status_code == "200" )
+            {
+                return 0;
+            }
+            else
+            {
+                get_logger()->print_update_failure(url,curl_data);
+            }
+        }
+        else if ( http_status_code == 401 )
         {
-            return 0;
+            get_logger()->print_service_not_authorized(url,get_login(),get_password());
         }
         else
         {
-            get_logger()->print_update_failure(url,curl_data);
+            get_logger()->print_update_failure(url,http_status_code);
         }
     }
-    else if ( http_status_code == 401 )
-    {
-        get_logger()->print_service_not_authorized(url,get_login(),get_password());
-    }
     else
     {
-        get_logger()->print_update_failure(url,http_status_code);
+        get_logger()->print_service_not_initialized(url);
     }
-
     return -1;
 }
index 66876a0..7dfadc6 100644 (file)
@@ -22,7 +22,17 @@ namespace fs = boost::filesystem;
 
 
 /**
- * Default constructor with Logger object.
+ * Default constructor.
+ */
+Serviceholder::Serviceholder()
+    :Log(new Logger)
+    ,IPAddrHelp()
+{
+}
+
+
+/**
+ * Constructor with Logger object.
  */
 Serviceholder::Serviceholder(Logger::Ptr _log)
     :Log(_log)
index 348e90a..c267966 100644 (file)
@@ -35,6 +35,8 @@ public:
 
     typedef boost::shared_ptr<Serviceholder> Ptr;
 
+    Serviceholder();
+
     Serviceholder(Logger::Ptr _log);
 
     ~Serviceholder();