From d21f681c4af4271af73580a8fe68590797483cd1 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 2 Nov 2011 03:02:14 -0200 Subject: [PATCH] The packet's type check is performed by methods in IcmpPacket - Because ICMP v4 and v6 have different values to echo reply --- src/icmp/icmppacket.h | 14 ++++++++++++++ src/icmp/icmppinger.cpp | 4 ++-- src/icmp/icmpv4packet.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/icmp/icmpv4packet.h | 16 ++++++++++++++-- src/icmp/icmpv6packet.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/icmp/icmpv6packet.h | 16 ++++++++++++++-- 6 files changed, 122 insertions(+), 6 deletions(-) diff --git a/src/icmp/icmppacket.h b/src/icmp/icmppacket.h index 7caf7ae..76110db 100644 --- a/src/icmp/icmppacket.h +++ b/src/icmp/icmppacket.h @@ -21,8 +21,11 @@ #ifndef ICMP_PACKET_H #define ICMP_PACKET_H +#include + #include +#include #include //----------------------------------------------------------------------------- @@ -38,6 +41,17 @@ public: IcmpPacket(); virtual ~IcmpPacket(); + virtual bool is_echo_reply( + const uint16_t identifier, + const uint16_t sequence_number, + const boost::asio::ip::address &source_address + ) const = 0; + virtual bool is_destination_unreachable( + const uint16_t identifier, + const uint16_t sequence_number, + const boost::asio::ip::address &source_address + ) const = 0; + virtual void print_echo_reply( const std::size_t &bytes_transferred, const boost::posix_time::ptime &time_packet_sent diff --git a/src/icmp/icmppinger.cpp b/src/icmp/icmppinger.cpp index ee2591a..e1c8f8d 100644 --- a/src/icmp/icmppinger.cpp +++ b/src/icmp/icmppinger.cpp @@ -260,7 +260,7 @@ 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( Icmpv4Type_EchoReply, + if ( icmp_packet.is_echo_reply( Identifier, SequenceNumber, DestinationEndpoint.address() ) ) { @@ -272,7 +272,7 @@ void IcmpPinger::handle_receive_icmp_packet( const size_t &bytes_transferred ) IcmpPacketReceiveTimer.cancel(); } - else if ( icmp_packet.match( Icmpv4Type_DestinationUnreachable, + else if ( icmp_packet.is_destination_unreachable( Identifier, SequenceNumber, DestinationEndpoint.address() ) ) { diff --git a/src/icmp/icmpv4packet.cpp b/src/icmp/icmpv4packet.cpp index 85b7e73..0d4c9f4 100644 --- a/src/icmp/icmpv4packet.cpp +++ b/src/icmp/icmpv4packet.cpp @@ -85,6 +85,45 @@ IcmpData Icmpv4Packet::get_icmp_data() const } /** + * @brief Convenience method to check if this packet, matching the arguments, + * is a echo reply. + * + * @param identifier The identifier. + * @param sequence_number The sequence number. + * @param source_address The source address. + * + * @return @c true if this packet is a echo reply, or @c false otherwise. + */ +bool Icmpv4Packet::is_echo_reply( + const uint16_t identifier, + const uint16_t sequence_number, + const address &source_address +) const +{ + return match( Icmpv4Type_EchoReply, identifier, sequence_number, source_address ); +} + +/** + * @brief Convenience method to check if this packet, matching the arguments, + * is a destination unreachable. + * + * @param identifier The identifier. + * @param sequence_number The sequence number. + * @param source_address The source address. + * + * @return @c true if this packet is a destination unreachable, or @c false + * otherwise. + */ +bool Icmpv4Packet::is_destination_unreachable( + const uint16_t identifier, + const uint16_t sequence_number, + const address &source_address +) const +{ + return match( Icmpv4Type_DestinationUnreachable, identifier, sequence_number, source_address ); +} + +/** * @brief Check if this object matches with all the parameters. * * @param type The type of ICMP message. diff --git a/src/icmp/icmpv4packet.h b/src/icmp/icmpv4packet.h index e7d5634..499c77b 100644 --- a/src/icmp/icmpv4packet.h +++ b/src/icmp/icmpv4packet.h @@ -80,8 +80,12 @@ public: Icmpv4Header get_icmp_header() const; IcmpData get_icmp_data() const; - bool match( - const Icmpv4Type type, + bool is_echo_reply( + const uint16_t identifier, + const uint16_t sequence_number, + const boost::asio::ip::address &source_address + ) const; + bool is_destination_unreachable( const uint16_t identifier, const uint16_t sequence_number, const boost::asio::ip::address &source_address @@ -103,6 +107,14 @@ public: ); private: + bool match( + const Icmpv4Type type, + const uint16_t identifier, + const uint16_t sequence_number, + const boost::asio::ip::address &source_address + ) const; + +private: /// The IP header. Ipv4Header IpHeader; /// The ICMP packet header. diff --git a/src/icmp/icmpv6packet.cpp b/src/icmp/icmpv6packet.cpp index b7d9853..bd716dd 100644 --- a/src/icmp/icmpv6packet.cpp +++ b/src/icmp/icmpv6packet.cpp @@ -85,6 +85,45 @@ IcmpData Icmpv6Packet::get_icmp_data() const } /** + * @brief Convenience method to check if this packet, matching the arguments, + * is a echo reply. + * + * @param identifier The identifier. + * @param sequence_number The sequence number. + * @param source_address The source address. + * + * @return @c true if this packet is a echo reply, or @c false otherwise. + */ +bool Icmpv6Packet::is_echo_reply( + const uint16_t identifier, + const uint16_t sequence_number, + const address &source_address +) const +{ + return match( Icmpv6Type_EchoReply, identifier, sequence_number, source_address ); +} + +/** + * @brief Convenience method to check if this packet, matching the arguments, + * is a destination unreachable. + * + * @param identifier The identifier. + * @param sequence_number The sequence number. + * @param source_address The source address. + * + * @return @c true if this packet is a destination unreachable, or @c false + * otherwise. + */ +bool Icmpv6Packet::is_destination_unreachable( + const uint16_t identifier, + const uint16_t sequence_number, + const address &source_address +) const +{ + return match( Icmpv6Type_DestinationUnreachable, identifier, sequence_number, source_address ); +} + +/** * @brief Check if this object matches with all the parameters. * * @param type The type of ICMP message. diff --git a/src/icmp/icmpv6packet.h b/src/icmp/icmpv6packet.h index bd4b6a6..d3b8b28 100644 --- a/src/icmp/icmpv6packet.h +++ b/src/icmp/icmpv6packet.h @@ -94,8 +94,12 @@ public: Icmpv6Header get_icmp_header() const; IcmpData get_icmp_data() const; - bool match( - const Icmpv6Type type, + bool is_echo_reply( + const uint16_t identifier, + const uint16_t sequence_number, + const boost::asio::ip::address &source_address + ) const; + bool is_destination_unreachable( const uint16_t identifier, const uint16_t sequence_number, const boost::asio::ip::address &source_address @@ -117,6 +121,14 @@ public: ); private: + bool match( + const Icmpv6Type type, + const uint16_t identifier, + const uint16_t sequence_number, + const boost::asio::ip::address &source_address + ) const; + +private: /// The IP header. Ipv6Header IpHeader; /// The ICMP packet header. -- 1.7.1