From fc3754b075f81710d64ef652b89654a8da8c00b4 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 8 Nov 2011 07:45:39 -0200 Subject: [PATCH] Using Boost.Asio protocol type instead of the enumeration and adding ICMPv6 ping --- src/host/pingerfactory.cpp | 9 ++++- src/host/pingprotocol.h | 3 +- src/icmp/icmppacketfactory.cpp | 61 ++++++++++++++++++++++++--------------- src/icmp/icmppacketfactory.h | 7 ++-- src/icmp/icmppinger.cpp | 8 ++-- src/icmp/icmppinger.h | 1 + src/ip/ipversion.h | 30 ------------------- src/main.cpp | 8 ++-- 8 files changed, 59 insertions(+), 68 deletions(-) delete mode 100644 src/ip/ipversion.h diff --git a/src/host/pingerfactory.cpp b/src/host/pingerfactory.cpp index 767d58a..7669d2e 100644 --- a/src/host/pingerfactory.cpp +++ b/src/host/pingerfactory.cpp @@ -26,6 +26,7 @@ using namespace std; using boost::shared_ptr; using boost::asio::io_service; +using boost::asio::ip::icmp; //----------------------------------------------------------------------------- // PingerFactory @@ -64,14 +65,18 @@ shared_ptr PingerFactory::createPinger( { case PingProtocol_ICMP: return shared_ptr( - new IcmpPinger( io_serv, network_interface, ping_reply_timeout_in_sec ) + new IcmpPinger( io_serv, icmp::v4(), network_interface, ping_reply_timeout_in_sec ) + ); + case PingProtocol_ICMPv6: + return shared_ptr( + new IcmpPinger( io_serv, icmp::v6(), network_interface, ping_reply_timeout_in_sec ) ); case PingProtocol_TCP: return shared_ptr( new TcpPinger( io_serv, network_interface, ping_reply_timeout_in_sec ) ); default: - BOOST_ASSERT( false ); + BOOST_ASSERT( !"Try to create a pinger from an invalid protocol" ); return shared_ptr(); } } diff --git a/src/host/pingprotocol.h b/src/host/pingprotocol.h index ebbcccc..4ec8715 100644 --- a/src/host/pingprotocol.h +++ b/src/host/pingprotocol.h @@ -26,7 +26,8 @@ enum PingProtocol { PingProtocol_First = 0, - PingProtocol_ICMP = 0, + PingProtocol_ICMP = PingProtocol_First, + PingProtocol_ICMPv6, PingProtocol_TCP, PingProtocol_Last = PingProtocol_TCP }; diff --git a/src/icmp/icmppacketfactory.cpp b/src/icmp/icmppacketfactory.cpp index d097164..37a3825 100644 --- a/src/icmp/icmppacketfactory.cpp +++ b/src/icmp/icmppacketfactory.cpp @@ -33,32 +33,36 @@ #include "icmp/icmpv6packet.h" using namespace std; +using boost::asio::ip::icmp; using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- // IcmpPacketFactory //----------------------------------------------------------------------------- +/** + * @brief + */ IcmpPacketItem IcmpPacketFactory::create_icmp_packet( - const IpVersion version, + const icmp::socket::protocol_type &protocol, istream &is ) { - BOOST_ASSERT( (IP_VERSION_4 == version) || (IP_VERSION_6 == version) ); + BOOST_ASSERT( (icmp::v4() == protocol) || (icmp::v6() == protocol) ); IcmpPacketItem icmp_packet; - switch ( version ) + if ( icmp::v4() == protocol ) + { + icmp_packet.reset( new Icmpv4Packet() ); + } + else if ( icmp::v6() == protocol ) { - case IP_VERSION_4: - icmp_packet.reset(new Icmpv4Packet()); - break; - case IP_VERSION_6: - icmp_packet.reset(new Icmpv6Packet()); - break; - default: - BOOST_ASSERT( !"Invalid ICMP Packet Type." ); - break; + icmp_packet.reset( new Icmpv6Packet() ); + } + else + { + BOOST_ASSERT( !"Invalid ICMP Packet Type." ); } if ( !icmp_packet->read( is ) ) @@ -73,32 +77,38 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet( return icmp_packet; } +/** + * @brief + */ IcmpPacketItem IcmpPacketFactory::create_icmp_packet_echo_request( - const IpVersion version, + const icmp::socket::protocol_type &protocol, const uint16_t identifier, const uint16_t sequence_number ) { - BOOST_ASSERT( (IP_VERSION_4 == version) || (IP_VERSION_6 == version) ); + BOOST_ASSERT( (icmp::v4() == protocol) || (icmp::v6() == protocol) ); IcmpPacketItem icmp_packet; - switch (version) + if ( icmp::v4() == protocol ) { - case IP_VERSION_4: - icmp_packet = create_icmpv4_packet_echo_request( identifier, sequence_number ); - break; - case IP_VERSION_6: - icmp_packet = create_icmpv6_packet_echo_request( identifier, sequence_number ); - break; - default: - BOOST_ASSERT( !"Invalid ICMP Packet Type." ); - break; + icmp_packet = create_icmpv4_packet_echo_request( identifier, sequence_number ); + } + else if ( icmp::v6() == protocol ) + { + icmp_packet = create_icmpv6_packet_echo_request( identifier, sequence_number ); + } + else + { + BOOST_ASSERT( !"Invalid ICMP Packet Type." ); } return icmp_packet; } +/** + * @brief + */ IcmpPacketItem IcmpPacketFactory::create_icmpv4_packet_echo_request( const uint16_t identifier, const uint16_t sequence_number @@ -120,6 +130,9 @@ IcmpPacketItem IcmpPacketFactory::create_icmpv4_packet_echo_request( return icmp_packet; } +/** + * @brief + */ IcmpPacketItem IcmpPacketFactory::create_icmpv6_packet_echo_request( const uint16_t identifier, const uint16_t sequence_number diff --git a/src/icmp/icmppacketfactory.h b/src/icmp/icmppacketfactory.h index 2d774ee..60483be 100644 --- a/src/icmp/icmppacketfactory.h +++ b/src/icmp/icmppacketfactory.h @@ -25,8 +25,9 @@ #include +#include + #include "icmp/icmppacket.h" -#include "ip/ipversion.h" //----------------------------------------------------------------------------- // IcmpPacketFactory @@ -40,11 +41,11 @@ class IcmpPacketFactory { public: static IcmpPacketItem create_icmp_packet( - const IpVersion version, + const boost::asio::ip::icmp::socket::protocol_type &protocol, std::istream &is ); static IcmpPacketItem create_icmp_packet_echo_request( - const IpVersion type, + const boost::asio::ip::icmp::socket::protocol_type &protocol, const uint16_t identifier, const uint16_t sequence_number ); diff --git a/src/icmp/icmppinger.cpp b/src/icmp/icmppinger.cpp index d0fa09a..f03057f 100644 --- a/src/icmp/icmppinger.cpp +++ b/src/icmp/icmppinger.cpp @@ -22,7 +22,6 @@ #include #include "icmp/icmppacketfactory.h" -#include "ip/ipversion.h" using namespace std; using boost::asio::const_buffers_1; @@ -43,12 +42,13 @@ using I2n::Logger::GlobalLogger; */ IcmpPinger::IcmpPinger( io_service &io_serv, + const icmp::socket::protocol_type &protocol, const string &source_network_interface, const int echo_reply_timeout_in_sec ) : IoService( io_serv ), DestinationEndpoint(), - Socket( IoService, icmp::v4() ), + Socket( IoService, protocol ), NetInterface( source_network_interface, Socket ), IcmpPacketReceiveTimer( IoService ), Identifier( 0 ), @@ -123,7 +123,7 @@ void IcmpPinger::start_send() ++SequenceNumber; IcmpPacketItem icmp_packet_echo_request = IcmpPacketFactory::create_icmp_packet_echo_request( - IP_VERSION_4, Identifier, SequenceNumber ); + icmp::v4(), Identifier, SequenceNumber ); BOOST_ASSERT( PingerStatus == PingStatus_NotSent ); send_echo_request( icmp_packet_echo_request ); @@ -232,7 +232,7 @@ void IcmpPinger::handle_receive_icmp_packet( const size_t &bytes_transferred ) } // Decode the reply packet. - IcmpPacketItem icmp_packet = IcmpPacketFactory::create_icmp_packet( IP_VERSION_4, is ); + IcmpPacketItem icmp_packet = IcmpPacketFactory::create_icmp_packet( icmp::v4(), is ); if ( !icmp_packet ) { GlobalLogger.notice() << "Warning: ignoring broken ICMP packet" diff --git a/src/icmp/icmppinger.h b/src/icmp/icmppinger.h index 4fdbcca..baeaf37 100644 --- a/src/icmp/icmppinger.h +++ b/src/icmp/icmppinger.h @@ -30,6 +30,7 @@ class IcmpPinger : public Pinger public: IcmpPinger( boost::asio::io_service &io_serv, + const boost::asio::ip::icmp::socket::protocol_type &protocol, const std::string &source_network_interface, const int echo_reply_timeout_in_sec ); diff --git a/src/ip/ipversion.h b/src/ip/ipversion.h deleted file mode 100644 index feab94b..0000000 --- a/src/ip/ipversion.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -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 IP_VERSION_H -#define IP_VERSION_H - -enum IpVersion -{ - IP_VERSION_4, - IP_VERSION_6 -}; - -#endif // IP_VERSION_H diff --git a/src/main.cpp b/src/main.cpp index 6518c73..15df1a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,8 +84,8 @@ LinkStatusAnalyzerItem get_status_notifier( void init_logger() { - I2n::Logger::enable_syslog( I2n::Logger::Facility::User ); //lint !e1786 - I2n::Logger::set_log_level( default_log_level ); //lint !e534 + I2n::Logger::enable_syslog( I2n::Logger::Facility::User ); //lint !e1786 + I2n::Logger::set_log_level( default_log_level ); //lint !e534 } void init_pingers( @@ -94,7 +94,7 @@ void init_pingers( PingSchedulerList *scheduler_list ) { - PingProtocol protocol = configuration->get_ping_protocol(); + PingProtocol ping_protocol = configuration->get_ping_protocol(); string local_interface = configuration->get_source_network_interface(); string nameserver = configuration->get_nameserver(); int ping_fail_limit = configuration->get_ping_fail_limit(); @@ -110,7 +110,7 @@ void init_pingers( local_interface, destination_address, destination_port, - protocol, + ping_protocol, ping_interval_in_sec, ping_fail_limit, nameserver, -- 1.7.1