From af68f845b5e561c66da74343ae8221debc0b6172 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Fri, 4 Nov 2011 01:08:00 -0200 Subject: [PATCH] Delegating the ICMP packet creation to a specialized class and using a polymorphic packet type --- src/icmp/icmppacketfactory.cpp | 5 ++- src/icmp/icmppinger.cpp | 47 +++++++++++---------------------------- src/icmp/icmppinger.h | 7 +---- 3 files changed, 18 insertions(+), 41 deletions(-) diff --git a/src/icmp/icmppacketfactory.cpp b/src/icmp/icmppacketfactory.cpp index be9405c..d097164 100644 --- a/src/icmp/icmppacketfactory.cpp +++ b/src/icmp/icmppacketfactory.cpp @@ -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; diff --git a/src/icmp/icmppinger.cpp b/src/icmp/icmppinger.cpp index 49bcbd1..5d64b33 100644 --- a/src/icmp/icmppinger.cpp +++ b/src/icmp/icmppinger.cpp @@ -21,11 +21,8 @@ #include -#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 ); diff --git a/src/icmp/icmppinger.h b/src/icmp/icmppinger.h index f917d1a..13c962f 100644 --- a/src/icmp/icmppinger.h +++ b/src/icmp/icmppinger.h @@ -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(); -- 1.7.1