Bring aboard ICMPv6 packet class
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 25 Oct 2011 00:31:20 +0000 (22:31 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 25 Oct 2011 00:31:20 +0000 (22:31 -0200)
src/icmp/icmpv6packet.cpp [new file with mode: 0644]
src/icmp/icmpv6packet.h [new file with mode: 0644]

diff --git a/src/icmp/icmpv6packet.cpp b/src/icmp/icmpv6packet.cpp
new file mode 100644 (file)
index 0000000..1b93a63
--- /dev/null
@@ -0,0 +1,127 @@
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff
+// Modifications (c) 2011 by Guilherme Maciel Ferreira / Intra2net AG
+//
+// Distributed under the Boost Software License, Version 1.0.
+//    (See accompanying file LICENSE_1_0.txt or copy at
+//          http://www.boost.org/LICENSE_1_0.txt)
+#include "icmp/icmpv6packet.h"
+
+#include <iostream>
+
+using namespace std;
+using boost::asio::ip::address;
+
+//-----------------------------------------------------------------------------
+// Icmpv6Packet
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Default constructor.
+ */
+Icmpv6Packet::Icmpv6Packet() :
+    IpHeader(),
+    IcmpPayloadHeader(),
+    IcmpPayloadData()
+{
+}
+
+/**
+ * @brief Parameterized constructor.
+ *
+ * @param icmp_header The ICMP header.
+ * @param icmp_data The ICMP payload data.
+ */
+Icmpv6Packet::Icmpv6Packet(
+        const Icmpv6Header &icmp_header,
+        const IcmpData &icmp_data
+) :
+    IpHeader(),
+    IcmpPayloadHeader( icmp_header ),
+    IcmpPayloadData( icmp_data )
+{
+}
+
+/**
+ * @brief Destructor.
+ */
+Icmpv6Packet::~Icmpv6Packet()
+{
+}
+
+/**
+ * @brief Obtain the IP header.
+ *
+ * @return The IP header object.
+ */
+Ipv6Header Icmpv6Packet::get_ip_header() const
+{
+    return IpHeader;
+}
+
+/**
+ * @brief Obtain the ICMP header.
+ *
+ * @return The ICMP header object.
+ */
+Icmpv6Header Icmpv6Packet::get_icmp_header() const
+{
+    return IcmpPayloadHeader;
+}
+
+/**
+ * @brief Obtain the ICMP payload data.
+ *
+ * @return The ICMP data.
+ */
+IcmpData Icmpv6Packet::get_icmp_data() const
+{
+    return IcmpPayloadData;
+}
+
+/**
+ * @brief Check if this object matches with all the parameters.
+ *
+ * @param type The type of ICMP message.
+ * @param identifier The identifier.
+ * @param sequence_number The sequence number.
+ * @param source_address The source address.
+ *
+ * @return @c true if this matches all the parameters, or @c false if at least
+ * one does not match.
+ */
+bool Icmpv6Packet::match(
+        const Icmpv6Type type,
+        const uint16_t identifier,
+        const uint16_t sequence_number,
+        const address &source_address
+) const
+{
+    bool type_match = IcmpPayloadHeader.get_type() == type ? true : false;
+    bool identifier_match = IcmpPayloadHeader.get_identifier() == identifier ? true: false;
+    bool seq_num_match = IcmpPayloadHeader.get_sequence_number() == sequence_number ? true : false;
+    bool address_match = IpHeader.get_source_address() == source_address ? true : false;
+
+    return ( type_match && identifier_match && seq_num_match && address_match );
+}
+
+istream& operator>>(
+        istream &is,
+        Icmpv6Packet &packet
+)
+{
+    //TODO WHY IPv6 does not come like IPv4????
+    //is >> packet.IpHeader >> packet.IcmpPayloadHeader >> packet.IcmpPayloadData;
+    is >> packet.IcmpPayloadHeader >> packet.IcmpPayloadData;
+
+    return is;
+}
+
+ostream& operator<<(
+        ostream& os,
+        const Icmpv6Packet& packet
+)
+{
+    os << packet.IcmpPayloadHeader << packet.IcmpPayloadData;
+
+    return os;
+}
diff --git a/src/icmp/icmpv6packet.h b/src/icmp/icmpv6packet.h
new file mode 100644 (file)
index 0000000..c263b32
--- /dev/null
@@ -0,0 +1,121 @@
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff
+// Modifications (c) 2011 by Guilherme Maciel Ferreira / Intra2net AG
+//
+// Distributed under the Boost Software License, Version 1.0.
+//    (See accompanying file LICENSE_1_0.txt or copy at
+//          http://www.boost.org/LICENSE_1_0.txt)
+#ifndef ICMPV6_PACKET_H
+#define ICMPV6_PACKET_H
+
+#include <stdint.h>
+
+#include <istream>
+#include <ostream>
+
+#include <boost/asio.hpp>
+
+#include "icmp/icmpv6header.h"
+#include "icmp/icmpdata.h"
+#include "icmp/icmptype.h"
+#include "ip/ipv6header.h"
+
+//-----------------------------------------------------------------------------
+// Icmpv6Packet
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief This class represents the ICMP version 6 Packet.
+ *
+ * The ICMP Packet format is:
+ *
+ * @code
+ * 0      3 4            11 12   15 16          23 24            31
+ * +-------+---------------+--------------------------------------+      ---
+ * |       |               |                                      |       ^
+ * |version| traffic class |              flow label              |       |
+ * |  (6)  |               |                                      |       |
+ * +-------+---------------+-------+--------------+---------------+       |
+ * |                               |              |               |       |
+ * |        payload length         | next header  |   hop limit   |       |
+ * |                               |              |               |       |
+ * +---------------+---------------+--------------+---------------+       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                      source IPv6 address                     |       |
+ * |                                                              |   320 bytes
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * +--------------------------------------------------------------+       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                   destination IPv6 address                   |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       |
+ * |                                                              |       v
+ * +---------------+---------------+------------------------------+      ---
+ * |               |               |                              |       ^
+ * |     type      |     code      |          checksum            |       |
+ * |               |               |                              |       |
+ * +---------------+---------------+------------------------------+       |
+ * |                               |                              | ICMP Payload
+ * |          identifier           |       sequence number        | (header +
+ * |                               |                              |       data)
+ * +-------------------------------+------------------------------+   8+ bytes
+ * |                                                              |       |
+ * |                      data (optional)                         |       |
+ * |                                                              |       v
+ * +-------------------------------+------------------------------+      ---
+ * @endcode
+ */
+class Icmpv6Packet
+{
+public:
+    Icmpv6Packet();
+    Icmpv6Packet(
+            const Icmpv6Header &icmp_header,
+            const IcmpData &icmp_data
+    );
+    virtual ~Icmpv6Packet();
+
+    Ipv6Header get_ip_header() const;
+    Icmpv6Header get_icmp_header() const;
+    IcmpData get_icmp_data() const;
+
+    bool match(
+            const Icmpv6Type type,
+            const uint16_t identifier,
+            const uint16_t sequence_number,
+            const boost::asio::ip::address &source_address
+    ) const;
+
+    friend std::istream& operator>>(
+            std::istream &is,
+            Icmpv6Packet &packet
+    );
+    friend std::ostream& operator<<(
+            std::ostream &os,
+            const Icmpv6Packet &packet
+    );
+
+private:
+    /// The IP header.
+    Ipv6Header IpHeader;
+    /// The ICMP packet header.
+    Icmpv6Header IcmpPayloadHeader;
+    /// The ICMP packet payload (data).
+    IcmpData IcmpPayloadData;
+};
+
+#endif // ICMPV6_PACKET_H