From 74dd30e10ce63fb1ffa368b14420f7ac7e0c0cab Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 13 Mar 2012 20:36:55 -0300 Subject: [PATCH] Added the host.source-network-interface configuration option. --- src/CMakeLists.txt | 1 + src/config/configurationoptions.cpp | 4 + src/config/host.cpp | 19 ++++ src/config/host.h | 7 ++ .../option/hostsourcenetworkinterfaceoption.cpp | 92 ++++++++++++++++++++ .../option/hostsourcenetworkinterfaceoption.h | 49 +++++++++++ 6 files changed, 172 insertions(+), 0 deletions(-) create mode 100644 src/config/option/hostsourcenetworkinterfaceoption.cpp create mode 100644 src/config/option/hostsourcenetworkinterfaceoption.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4f678e8..f67af5b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/config/configurationoptions.cpp b/src/config/configurationoptions.cpp index 09d455c..c994bba 100644 --- a/src/config/configurationoptions.cpp +++ b/src/config/configurationoptions.cpp @@ -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 ); diff --git a/src/config/host.cpp b/src/config/host.cpp index 9757205..6c599bc 100644 --- a/src/config/host.cpp +++ b/src/config/host.cpp @@ -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 diff --git a/src/config/host.h b/src/config/host.h index 7f59aff..6a77d4f 100644 --- a/src/config/host.h +++ b/src/config/host.h @@ -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 index 0000000..f18e19d --- /dev/null +++ b/src/config/option/hostsourcenetworkinterfaceoption.cpp @@ -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 + +#include +#include + +#include + +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 >(), + "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 host_source_interface_list = vm[ get_command_string() ].as< vector >(); + 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 index 0000000..4a78f50 --- /dev/null +++ b/src/config/option/hostsourcenetworkinterfaceoption.h @@ -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 + +#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 -- 1.7.1