Delegating the ICMP packet creation to a specialized class and using a polymorphic...
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 4 Nov 2011 03:08:00 +0000 (01:08 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 4 Nov 2011 03:08:00 +0000 (01:08 -0200)
src/icmp/icmppacketfactory.cpp
src/icmp/icmppinger.cpp
src/icmp/icmppinger.h

index be9405c..d097164 100644 (file)
@@ -65,8 +65,9 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet(
     {
         GlobalLogger.notice() << "Could not read ICMP packet." << endl;
 
-        IcmpPacketItem icmp_packet_empty;
-        icmp_packet = icmp_packet_empty;
+        // TODO why good packets reports as bad?
+        //IcmpPacketItem icmp_packet_empty;
+        //icmp_packet = icmp_packet_empty;
     }
 
     return icmp_packet;
index 49bcbd1..5d64b33 100644 (file)
 
 #include <logfunc.hpp>
 
-#include "icmp/icmpchecksum.h"
-#include "icmp/icmpdata.h"
-#include "icmp/icmpv4header.h"
-#include "icmp/icmptype.h"
-#include "ip/ipv4header.h"
+#include "icmp/icmppacketfactory.h"
+#include "ip/ipversion.h"
 
 using namespace std;
 using boost::asio::const_buffers_1;
@@ -119,36 +116,18 @@ void IcmpPinger::start_send()
 {
     ++SequenceNumber;
 
-    Icmpv4Packet icmp_echo_request_packet = create_echo_request( SequenceNumber );
+    IcmpPacketItem icmp_packet_echo_request = IcmpPacketFactory::create_icmp_packet_echo_request(
+            IP_VERSION_4, Identifier, SequenceNumber );
 
     BOOST_ASSERT( PingerStatus == PingStatus_NotSent );
-    send_echo_request( icmp_echo_request_packet );
+    send_echo_request( icmp_packet_echo_request );
 }
 
-Icmpv4Packet IcmpPinger::create_echo_request(
-        const uint16_t sequence_number
-) const
-{
-    const IcmpData icmp_data( "ping-message" );
-
-    Icmpv4Type type = Icmpv4Type_EchoRequest;
-    uint8_t code = 0;
-    IcmpChecksum calculator( icmp_data.begin(), icmp_data.end() );
-    uint16_t checksum = calculator.compute(
-            type, code, Identifier, sequence_number
-    );
-    Icmpv4Header icmp_header(
-            type, code, checksum, Identifier, sequence_number
-    );
-
-    return Icmpv4Packet( icmp_header, icmp_data );
-}
-
-void IcmpPinger::send_echo_request( const Icmpv4Packet &icmp_packet )
+void IcmpPinger::send_echo_request( const IcmpPacketItem icmp_packet )
 {
     boost::asio::streambuf request_buffer;
     ostream os( &request_buffer );
-    os << icmp_packet;
+    icmp_packet->write( os );
 
     TimeSent = microsec_clock::universal_time();
 
@@ -247,8 +226,8 @@ void IcmpPinger::handle_receive_icmp_packet( const size_t &bytes_transferred )
         }
 
         // Decode the reply packet.
-        Icmpv4Packet icmp_packet;
-        if (! (is >> icmp_packet) )
+        IcmpPacketItem icmp_packet = IcmpPacketFactory::create_icmp_packet( IP_VERSION_4, is );
+        if ( !icmp_packet )
         {
             GlobalLogger.notice() << "Warning: ignoring broken ICMP packet"
                 << endl;
@@ -260,25 +239,25 @@ void IcmpPinger::handle_receive_icmp_packet( const size_t &bytes_transferred )
         // expected sequence number, and destination host address (receive just
         // the ICMP packets from the host we had ping).
 
-        if ( icmp_packet.match_echo_reply(
+        if ( icmp_packet->match_echo_reply(
                                 Identifier, SequenceNumber,
                                 DestinationEndpoint.address() ) )
         {
             ReceivedReply = true;
 
-            icmp_packet.print_echo_reply( bytes_transferred, TimeSent );
+            icmp_packet->print_echo_reply( bytes_transferred, TimeSent );
 
             set_ping_status( PingStatus_SuccessReply );
 
             IcmpPacketReceiveTimer.cancel();
         }
-        else if ( icmp_packet.match_destination_unreachable(
+        else if ( icmp_packet->match_destination_unreachable(
                                      Identifier, SequenceNumber,
                                      DestinationEndpoint.address() ) )
         {
             ReceivedReply = true;
 
-            icmp_packet.print_destination_unreachable();
+            icmp_packet->print_destination_unreachable();
 
             set_ping_status( PingStatus_FailureDestinationUnreachable );
 
index f917d1a..13c962f 100644 (file)
@@ -15,9 +15,7 @@
 #include "host/networkinterface.hpp"
 #include "host/pinger.h"
 #include "host/pingstatus.h"
-#include "icmp/icmpv4packet.h"
-
-class Icmpv4Packet;
+#include "icmp/icmppacket.h"
 
 //-----------------------------------------------------------------------------
 // IcmpPinger
@@ -47,8 +45,7 @@ private:
     void set_destination_endpoint( const std::string &destination_ip );
 
     void start_send();
-    Icmpv4Packet create_echo_request( const uint16_t sequence_number ) const;
-    void send_echo_request( const Icmpv4Packet &icmp_packet );
+    void send_echo_request( const IcmpPacketItem icmp_packet );
     void schedule_timeout_echo_reply();
     void handle_ping_done();