Added the host.source-network-interface configuration option.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 13 Mar 2012 23:36:55 +0000 (20:36 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 13 Mar 2012 23:46:55 +0000 (20:46 -0300)
src/CMakeLists.txt
src/config/configurationoptions.cpp
src/config/host.cpp
src/config/host.h
src/config/option/hostsourcenetworkinterfaceoption.cpp [new file with mode: 0644]
src/config/option/hostsourcenetworkinterfaceoption.h [new file with mode: 0644]

index 4f678e8..f67af5b 100644 (file)
@@ -46,6 +46,7 @@ set(SOURCES
     config/option/hostpingprotocoloption.cpp
     config/option/hostportoption.cpp
     config/option/hostnameoption.cpp
+    config/option/hostsourcenetworkinterfaceoption.cpp
     config/option/hostsdownlimitoption.cpp
     config/option/linkdownintervaloption.cpp
     config/option/linkupintervaloption.cpp
index 09d455c..c994bba 100644 (file)
@@ -34,6 +34,7 @@
 #include "config/option/hostportoption.h"
 #include "config/option/hostpingprotocoloption.h"
 #include "config/option/hostpingintervaloption.h"
+#include "config/option/hostsourcenetworkinterfaceoption.h"
 #include "config/option/linkdownintervaloption.h"
 #include "config/option/linkupintervaloption.h"
 #include "config/option/logleveloption.h"
@@ -104,6 +105,9 @@ ConfigurationOptions::ConfigurationOptions() :
     HostConfigurationOptionItem host_port( new HostPortOption );
     HostOptions.push_back( host_port );
 
+    HostConfigurationOptionItem host_source_network_interface( new HostSourceNetworkInterfaceOption );
+    HostOptions.push_back( host_source_network_interface );
+
     HostConfigurationOptionItem host_protocol( new HostPingProtocolOption );
     HostOptions.push_back( host_protocol );
 
index 9757205..6c599bc 100644 (file)
@@ -37,6 +37,7 @@ using namespace std;
 Host::Host( string address ) :
     Address( address ),
     Port( 0 ),
+    SourceNetworkInterface( "" ),
     Protocol( PingProtocol_ICMP ),
     IntervalInSec( 0 )
 {
@@ -86,6 +87,24 @@ void Host::set_port( const uint16_t port )
 }
 
 /**
+ * @return The network interface from where the pings will depart.
+ */
+string Host::get_source_network_interface() const
+{
+    return SourceNetworkInterface;
+}
+
+/**
+ * @param source_network_interface The network interface from where the pings will depart.
+ */
+void Host::set_source_network_interface(
+        const string &source_network_interface
+)
+{
+    SourceNetworkInterface = source_network_interface;
+}
+
+/**
  * @return The protocol used to ping this host.
  */
 PingProtocol Host::get_ping_protocol() const
index 7f59aff..6a77d4f 100644 (file)
@@ -48,6 +48,11 @@ public:
     uint16_t get_port() const;
     void set_port( const uint16_t port );
 
+    std::string get_source_network_interface() const;
+    void set_source_network_interface(
+            const std::string &source_network_interface
+    );
+
     PingProtocol get_ping_protocol() const;
     void set_ping_protocol( const PingProtocol ping_protocol );
 
@@ -59,6 +64,8 @@ private:
     std::string Address;
     /// The port of the host
     uint16_t Port;
+    /// The network interface from where the pings will depart
+    std::string SourceNetworkInterface;
     /// The protocol to ping
     PingProtocol Protocol;
     /// The interval between each ping to the host
diff --git a/src/config/option/hostsourcenetworkinterfaceoption.cpp b/src/config/option/hostsourcenetworkinterfaceoption.cpp
new file mode 100644 (file)
index 0000000..f18e19d
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "config/option/hostsourcenetworkinterfaceoption.h"
+
+#include <string>
+
+#include <boost/assert.hpp>
+#include <boost/foreach.hpp>
+
+#include <logfunc.hpp>
+
+using namespace std;
+using boost::program_options::value;
+using boost::program_options::variables_map;
+using I2n::Logger::GlobalLogger;
+
+//-----------------------------------------------------------------------------
+// HostSourceNetworkInterfaceOption
+//-----------------------------------------------------------------------------
+
+HostSourceNetworkInterfaceOption::HostSourceNetworkInterfaceOption() :
+    HostConfigurationOption(
+        "host.source-network-interface",
+        value< vector<string> >(),
+        "The network interface from where the packets will be received and originated"
+    )
+{
+}
+
+HostSourceNetworkInterfaceOption::~HostSourceNetworkInterfaceOption()
+{
+}
+
+bool HostSourceNetworkInterfaceOption::parse(
+        const variables_map& vm,
+        Configuration *configuration
+)
+{
+    size_t hosts_count = 0;
+    size_t hosts_source_interface_count = 0;
+    bool parsed_success = false;
+
+    // [host] source-network-interface
+    if ( 1 <= vm.count( get_command_string() ) )
+    {
+        HostList hosts_list = configuration->get_hosts();
+        HostList::iterator hosts_list_iterator = hosts_list.begin();
+        hosts_count = hosts_list.size();
+
+        vector<string> host_source_interface_list = vm[ get_command_string() ].as< vector<string> >();
+        hosts_source_interface_count = host_source_interface_list.size();
+
+        BOOST_ASSERT( hosts_count >= hosts_source_interface_count );
+
+        BOOST_FOREACH( string host_source_interface, host_source_interface_list )
+        {
+            BOOST_ASSERT( !host_source_interface.empty() );
+
+            HostItem host_item = *hosts_list_iterator;
+            host_item->set_source_network_interface( host_source_interface );
+            ++hosts_list_iterator;
+
+            GlobalLogger.info() << get_command_string() << "=" << host_source_interface << endl;
+        }
+
+        parsed_success = true;
+    }
+
+    set_hosts_count( hosts_source_interface_count );
+
+    BOOST_ASSERT( hosts_count == hosts_source_interface_count );
+
+    return parsed_success;
+}
diff --git a/src/config/option/hostsourcenetworkinterfaceoption.h b/src/config/option/hostsourcenetworkinterfaceoption.h
new file mode 100644 (file)
index 0000000..4a78f50
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef HOST_SOURCE_NETWORK_INTERFACE_OPTION_H
+#define HOST_SOURCE_NETWORK_INTERFACE_OPTION_H
+
+#include <boost/program_options.hpp>
+
+#include "config/option/hostconfigurationoption.h"
+
+//-----------------------------------------------------------------------------
+// HostSourceNetworkInterfaceOption
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief This class represents the "[host] source-network-interface" configuration
+ * option.
+ */
+class HostSourceNetworkInterfaceOption : public HostConfigurationOption
+{
+public:
+    HostSourceNetworkInterfaceOption();
+    virtual ~HostSourceNetworkInterfaceOption();
+
+    virtual bool parse(
+            const boost::program_options::variables_map &vm,
+            Configuration *configuration
+    );
+
+};
+
+#endif // HOST_SOURCE_NETWORK_INTERFACE_OPTION_H