Indroduced new config options for use of ipv6 and ip webcheck url's.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 12 Aug 2009 14:43:27 +0000 (16:43 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 12 Aug 2009 14:43:27 +0000 (16:43 +0200)
src/config.cpp
src/config.h
src/iphelper.cpp
src/iphelper.h
src/logger.cpp
src/logger.h
src/ods.cpp
src/ods.h
src/updater.cpp

index 45ab17c..98a4235 100644 (file)
@@ -46,6 +46,9 @@ Config::Config(Logger::Ptr _log)
     , Loglevel(0)
     , Syslog(false)
     , ConfigPath("/etc/bpdyndnsd")
+    , EnableIPv6(false)
+    , WebcheckIpUrl("")
+    , WebcheckIpUrlAlt("")
 {
     // initialize Logger
     Log = _log;
@@ -73,6 +76,9 @@ Config::Config(Logger::Ptr _log)
         ("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.")
     ;
 
     // Define valid command line parameters
@@ -287,12 +293,24 @@ int Config::parse_cmd_line(int argc, char *argv[])
             }
         }
 
+        if ( VariablesMap.count("daemon_mode") )
+            DaemonMode = VariablesMap["daemon_mode"].as<bool>();
+
         if ( VariablesMap.count("loglevel") )
             Loglevel = VariablesMap["loglevel"].as<int>();
 
         if ( VariablesMap.count("syslog") )
             Syslog = VariablesMap["syslog"].as<bool>();
 
+        if ( VariablesMap.count("enable_ipv6") )
+            EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
+
+        if ( VariablesMap.count("webcheck_url") )
+            WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
+
+        if ( VariablesMap.count("webcheck_url_alt") )
+            WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
+
     }
     catch(po::unknown_option e)
     {
@@ -305,11 +323,11 @@ int Config::parse_cmd_line(int argc, char *argv[])
 
 
 /**
- * 
- * @param protocol 
- * @param host 
- * @param login 
- * @param password 
+ * Creates a Service object from the given parameters.
+ * @param protocol Protocol to use.
+ * @param host Hostname to update.
+ * @param login Login.
+ * @param password Password.
  * @return A pointer to the created Service object.
  */
 Service::Ptr Config::create_service(const string &protocol,const string &hostname, const string &login, const string &password)
@@ -407,12 +425,24 @@ int Config::load_main_config_file(const string& full_filename)
             po::store(parsed_main_options,VariablesMap);
             po::notify(VariablesMap);
 
-            if(VariablesMap.count("daemon_mode") && VariablesMap.count("loglevel") && VariablesMap.count("syslog"))
-            {
-                DaemonMode = VariablesMap["daemon_mode"].as<bool>();
-                Loglevel = VariablesMap["loglevel"].as<int>();
-                Syslog = VariablesMap["syslog"].as<bool>();
-            }
+        if ( VariablesMap.count("daemon_mode") )
+            DaemonMode = VariablesMap["daemon_mode"].as<bool>();
+
+        if ( VariablesMap.count("loglevel") )
+            Loglevel = VariablesMap["loglevel"].as<int>();
+
+        if ( VariablesMap.count("syslog") )
+            Syslog = VariablesMap["syslog"].as<bool>();
+
+        if ( VariablesMap.count("enable_ipv6") )
+            EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
+
+        if ( VariablesMap.count("webcheck_url") )
+            WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
+
+        if ( VariablesMap.count("webcheck_url_alt") )
+            WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
+
         }
         catch ( po::unknown_option e )      // at the moment 04-08-2009 this exception is never thrown :-(
         {
@@ -572,3 +602,33 @@ bool Config::get_syslog() const
 {
     return Syslog;
 }
+
+
+/**
+ * Getter for member EnableIPv6
+ * @return Wether IPv6 should be used or not.
+ */
+bool Config::get_enable_ipv6() const
+{
+    return EnableIPv6;
+}
+
+
+/**
+ * Getter for member WebcheckIpUrl
+ * @return The primary IP Webcheck URL
+ */
+string Config::get_webcheck_ip_url() const
+{
+    return WebcheckIpUrl;
+}
+
+
+/**
+ * Getter for member WebcheckIpUrlAlt
+ * @return The alternative IP Webcheck URL
+ */
+string Config::get_webcheck_ip_url_alt() const
+{
+    return WebcheckIpUrlAlt;
+}
index bca6199..ef08f53 100644 (file)
@@ -40,6 +40,9 @@ private:
     int Loglevel;
     bool Syslog;
     std::string ConfigPath;
+    bool EnableIPv6;
+    std::string WebcheckIpUrl;
+    std::string WebcheckIpUrlAlt;
 
     Service::Ptr create_service(const std::string&,const std::string&,const std::string&,const std::string&);
     int load_main_config_file(const std::string&);
@@ -78,6 +81,12 @@ public:
 
     bool get_syslog() const;
 
+    bool get_enable_ipv6() const;
+
+    std::string get_webcheck_ip_url() const;
+
+    std::string get_webcheck_ip_url_alt() const;
+
     void delete_variables_map();
 
 };
index bc114ed..634cc5f 100644 (file)
@@ -19,8 +19,8 @@ namespace net = boost::asio;
  */
 IPHelper::IPHelper()
     : Hostname("")
-    , WebcheckUrl("")
-    , WebcheckUrlAlt("")
+    , WebcheckIpUrl("")
+    , WebcheckIpUrlAlt("")
     , UseIPv6(false)
     , Log(new Logger)
 {
@@ -30,14 +30,16 @@ IPHelper::IPHelper()
 /**
  * Constructor.
  */
-IPHelper::IPHelper(Logger::Ptr _log)
+IPHelper::IPHelper(const Logger::Ptr _log, const string& _webcheck_url, const string& _webcheck_url_alt, const bool _use_ipv6)
     : Hostname("")
-    , WebcheckUrl("")
-    , WebcheckUrlAlt("")
-    , UseIPv6(false)
-    , Log(new Logger)
 {
     Log = _log;
+    WebcheckIpUrl = _webcheck_url;
+    WebcheckIpUrlAlt = _webcheck_url_alt;
+    UseIPv6 = _use_ipv6;
+    Hostname = net::ip::host_name();
+
+    Log->print_hostname(Hostname);
 }
 
 
@@ -50,25 +52,12 @@ IPHelper::~IPHelper()
 
 
 /**
- * Initializes the ip helper.
- */
-int IPHelper::init_hostname()
-{
-    Hostname = net::ip::host_name();
-
-    Log->print_hostname(Hostname);
-
-    return 0;
-}
-
-
-/**
  * Get the actual IP of this host through a conventional DNS query or through a IP webcheck URL if configured so.
  * @return A string representation of the actual IP in dotted format or an empty string if something went wrong.
  */
 string IPHelper::get_actual_ip() const
 {
-    if ( WebcheckUrl == "" )
+    if ( WebcheckIpUrl == "" )
     {
         return dns_query();
     }
@@ -79,11 +68,6 @@ string IPHelper::get_actual_ip() const
     return "";
 }
 
-void IPHelper::set_hostname(const string& _hostname)
-{
-    Hostname = _hostname;
-}
-
 
 /**
  * Get the actual IP of this host through a DNS query.
@@ -104,13 +88,15 @@ string IPHelper::dns_query() const
         net::ip::tcp::resolver::iterator end;
         while(endpoint_iterator != end)
         {
-            net::ip::tcp::endpoint endpoint = endpoint_iterator->endpoint();
-            net::ip::address ip = endpoint.address();
+            net::ip::tcp::endpoint endpoint = endpoint_iterator->endpoint();    // this ends up in a compiler warning: cc1plus: warning: dereferencing pointer 'pretmp.37188' does break strict-aliasing rules
+            net::ip::address ip = endpoint.address();                           // but why?
             if ( ip.is_v4() )
                 ip_addr_v4 = ip.to_string();
             else if ( ip.is_v6() )
                 ip_addr_v6 = ip.to_string();
-            Log->print_own_ip(ip_addr_v4, ip_addr_v6);
+            Log->print_own_ipv4(ip_addr_v4);
+            if ( UseIPv6 == true )
+                Log->print_own_ipv6(ip_addr_v6);
             endpoint_iterator++;
         }
         io_service.reset();
@@ -138,14 +124,3 @@ string IPHelper::webcheck_ip() const
 
     return ip_addr;
 }
-
-
-/**
- * Setter for both members WebcheckUrl and WebcheckUrlAlt
- * @param _webcheck_url The primary webcheck URL.
- * @param _webcheck_url_alt The fallback webcheck URL.
- */
-void IPHelper::set_webcheck_urls(std::string& _webcheck_url, std::string& _webcheck_url_alt)
-{
-
-}
index 00163ea..0cf4787 100644 (file)
@@ -21,8 +21,8 @@ class IPHelper
 {
 private:
     std::string Hostname;
-    std::string WebcheckUrl;
-    std::string WebcheckUrlAlt;
+    std::string WebcheckIpUrl;
+    std::string WebcheckIpUrlAlt;
 
     bool UseIPv6;
 
@@ -34,21 +34,15 @@ public:
 
     IPHelper();
 
-    IPHelper(Logger::Ptr);
+    IPHelper(const Logger::Ptr, const std::string&, const std::string&, const bool);
 
     ~IPHelper();
 
-    void set_hostname(const std::string&);
-
     std::string get_actual_ip() const;
 
     std::string dns_query() const;
 
     std::string webcheck_ip() const;
-
-    void set_webcheck_urls(std::string&, std::string&);
-
-    int init_hostname();
 };
 
 #endif
index 37d994c..cab7b5e 100644 (file)
@@ -715,15 +715,29 @@ void Logger::print_hostname(const std::string& hostname) const
 
 
 /**
- * Prints out the detected own ip address
+ * Prints out the detected own ipv4 address
  * @param ip_addr String representation of the detected ip.
  */
-void Logger::print_own_ip(const std::string& ip_addr_v4, const std::string& ip_addr_v6) const
+void Logger::print_own_ipv4(const std::string& ip_addr_v4) const
 {
     if ( 1 <= Loglevel )
     {
         ostringstream msg;
         msg << "Detected following IPv4-Address of this host: " << ip_addr_v4 << endl;
+        log_notice(msg.str());
+    }
+}
+
+
+/**
+ * Prints out the detected own ipv5 address
+ * @param ip_addr String representation of the detected ip.
+ */
+void Logger::print_own_ipv6(const std::string& ip_addr_v6) const
+{
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
         msg << "Detected following IPv6-Address of this host: " << ip_addr_v6 << endl;
         log_notice(msg.str());
     }
index cc767c7..a7fd2df 100644 (file)
@@ -127,7 +127,9 @@ public:
 
     void print_hostname(const std::string&) const;
 
-    void print_own_ip(const std::string&, const std::string&) const;
+    void print_own_ipv4(const std::string&) const;
+
+    void print_own_ipv6(const std::string&) const;
 
     void print_error_hostname_to_ip(const std::string&, const std::string&) const;
 
index c8fad25..94ce1f3 100644 (file)
@@ -59,11 +59,29 @@ ODS::~ODS()
  */
 void ODS::update(const string& ip)
 {
-    // if update was successful, we need to set the lastupdated base member.
-    time_t actual_time = time(NULL);
-    set_lastupdated(actual_time);
+    if ( this->get_actual_ip() != ip )
+    {
+        this->get_logger()->print_update_service("DHS");
+
+        if ( perform_update(ip) == 0 )
+        {
+            // if update was successful, we need to set the Lastupdated and ActualIP base member.
+            this->set_lastupdated(time(NULL));
+            this->set_actual_ip(ip);
+            this->get_logger()->print_update_service_successful("DHS");
+        }
+    }
+}
+
 
-    get_logger()->print_update_service("ODS");
+/**
+ * Performs the Service update.
+ * @param ip IP Address to set.
+ * @return 0 if all is fine, -1 otherwise.
+ */
+int ODS::perform_update(const std::string& ip)
+{
+    return 0;
 }
 
 
index 242be85..1843dd5 100644 (file)
--- a/src/ods.h
+++ b/src/ods.h
@@ -49,6 +49,8 @@ public:
     int get_max_updates_within_timeout() const;
 
     void update(const std::string&);
+
+    int perform_update(const std::string&);
 };
 
 #endif
index 947dcbb..bc46127 100644 (file)
@@ -17,6 +17,7 @@ using namespace std;
  * Default constructor which initializes the member Conf.
  */
 Updater::Updater()
+    : IPHelp(new IPHelper)
 {
     // Initialize program wide Logger, at this point we don't know where to log and with which loglevel.
     Logger::Ptr _log(new Logger);
@@ -27,11 +28,6 @@ Updater::Updater()
     Config::Ptr _config(new Config(Log));
     Conf = _config;
     _config.reset();
-
-    // initialize IPHelper
-    IPHelper::Ptr _iphelp(new IPHelper(Log));
-    IPHelp = _iphelp;
-    _iphelp.reset();
 }
 
 
@@ -114,7 +110,10 @@ Logger::Ptr Updater::get_logger() const
  */
 int Updater::init_ip_helper()
 {
-    IPHelp->init_hostname();
+    // initialize IPHelper
+    IPHelper::Ptr _iphelp(new IPHelper(Log,Conf->get_webcheck_ip_url(),Conf->get_webcheck_ip_url_alt(),Conf->get_enable_ipv6()));
+    IPHelp = _iphelp;
+    _iphelp.reset();
 
     return 0;
 }