The packet's type check is performed by methods in IcmpPacket
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 2 Nov 2011 05:02:14 +0000 (03:02 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 2 Nov 2011 05:02:14 +0000 (03:02 -0200)
- Because ICMP v4 and v6 have different values to echo reply

src/icmp/icmppacket.h
src/icmp/icmppinger.cpp
src/icmp/icmpv4packet.cpp
src/icmp/icmpv4packet.h
src/icmp/icmpv6packet.cpp
src/icmp/icmpv6packet.h

index 7caf7ae..76110db 100644 (file)
 #ifndef ICMP_PACKET_H
 #define ICMP_PACKET_H
 
+#include <stdint.h>
+
 #include <string>
 
+#include <boost/asio.hpp>
 #include <boost/date_time/posix_time/posix_time_types.hpp>
 
 //-----------------------------------------------------------------------------
@@ -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
index ee2591a..e1c8f8d 100644 (file)
@@ -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() ) )
         {
index 85b7e73..0d4c9f4 100644 (file)
@@ -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.
index e7d5634..499c77b 100644 (file)
@@ -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.
index b7d9853..bd716dd 100644 (file)
@@ -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.
index bd4b6a6..d3b8b28 100644 (file)
@@ -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.