Using Boost.Asio protocol type instead of the enumeration and adding ICMPv6 ping
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 8 Nov 2011 09:45:39 +0000 (07:45 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 8 Nov 2011 09:56:48 +0000 (07:56 -0200)
src/host/pingerfactory.cpp
src/host/pingprotocol.h
src/icmp/icmppacketfactory.cpp
src/icmp/icmppacketfactory.h
src/icmp/icmppinger.cpp
src/icmp/icmppinger.h
src/ip/ipversion.h [deleted file]
src/main.cpp

index 767d58a..7669d2e 100644 (file)
@@ -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<Pinger> PingerFactory::createPinger(
     {
     case PingProtocol_ICMP:
         return shared_ptr<Pinger>(
-                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<Pinger>(
+                new IcmpPinger( io_serv, icmp::v6(), network_interface, ping_reply_timeout_in_sec )
         );
     case PingProtocol_TCP:
         return shared_ptr<Pinger>(
                 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<Pinger>();
     }
 }
index ebbcccc..4ec8715 100644 (file)
@@ -26,7 +26,8 @@
 enum PingProtocol
 {
     PingProtocol_First = 0,
-    PingProtocol_ICMP = 0,
+    PingProtocol_ICMP = PingProtocol_First,
+    PingProtocol_ICMPv6,
     PingProtocol_TCP,
     PingProtocol_Last = PingProtocol_TCP
 };
index d097164..37a3825 100644 (file)
 #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
index 2d774ee..60483be 100644 (file)
@@ -25,8 +25,9 @@
 
 #include <iostream>
 
+#include <boost/asio.hpp>
+
 #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
     );
index d0fa09a..f03057f 100644 (file)
@@ -22,7 +22,6 @@
 #include <logfunc.hpp>
 
 #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"
index 4fdbcca..baeaf37 100644 (file)
@@ -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 (file)
index feab94b..0000000
+++ /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
index 6518c73..15df1a7 100644 (file)
@@ -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,