Bring aboard IPv6 header class (not tested yet)
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 18 Oct 2011 23:00:32 +0000 (21:00 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 18 Oct 2011 23:00:32 +0000 (21:00 -0200)
src/ip/ipv6header.cpp [new file with mode: 0644]
src/ip/ipv6header.h [new file with mode: 0644]

diff --git a/src/ip/ipv6header.cpp b/src/ip/ipv6header.cpp
new file mode 100644 (file)
index 0000000..b0f34bd
--- /dev/null
@@ -0,0 +1,113 @@
+// 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 "ip/ipv6header.h"
+
+#include <limits>
+
+#include <boost/assert.hpp>
+#include <boost/foreach.hpp>
+#include <boost/scoped_array.hpp>
+
+using namespace std;
+using boost::asio::ip::address_v6;
+using boost::scoped_array;
+
+//-----------------------------------------------------------------------------
+// Ipv6Header
+//-----------------------------------------------------------------------------
+
+static const size_t Ipv6HeaderSizeInBytes = 40;
+
+Ipv6Header::Ipv6Header() :
+    Payload( Ipv6HeaderSizeInBytes )
+{
+}
+
+uint8_t Ipv6Header::get_version() const
+{
+    return ( Payload[ 0 ] >> 4 ) & 0x0F;
+}
+
+uint8_t Ipv6Header::get_traffic_class() const
+{
+    uint16_t first_word = static_cast<uint16_t>( (Payload[ 0 ] << 8) | Payload[ 1 ] );
+    uint8_t traffic_class = static_cast<uint8_t>( (first_word & 0x0FF0) >> 4 );
+
+    return traffic_class;
+}
+
+uint32_t Ipv6Header::get_flow_label() const
+{
+    uint32_t flow_label = Payload.decode32( 0, 3 );
+    return ( flow_label & 0xFFFFF );
+}
+
+uint16_t Ipv6Header::get_payload_length() const
+{
+    return Payload.decode16( 4, 5 );
+}
+
+uint8_t Ipv6Header::get_next_header() const
+{
+    return Payload[ 6 ];
+}
+
+uint8_t Ipv6Header::get_hop_limit() const
+{
+    return Payload[ 7 ];
+}
+
+address_v6 Ipv6Header::get_source_address() const
+{
+    address_v6::bytes_type address; // 16 bytes
+    BOOST_ASSERT( 16 == address.size() );
+
+    int header_byte_index = 8;
+    BOOST_FOREACH( unsigned char &byte, address )
+    {
+        byte = Payload[ header_byte_index ];
+        header_byte_index++;
+    }
+
+    BOOST_ASSERT( 24 == header_byte_index );
+
+    return address_v6( address );
+}
+
+address_v6 Ipv6Header::get_destination_address() const
+{
+    address_v6::bytes_type address; // 16 bytes
+    BOOST_ASSERT( 16 == address.size() );
+
+    int header_byte_index = 24;
+    BOOST_FOREACH( unsigned char &byte, address )
+    {
+        byte = Payload[ header_byte_index ];
+        header_byte_index++;
+    }
+
+    BOOST_ASSERT( 40 == header_byte_index );
+
+    return address_v6( address );
+}
+
+istream &operator>>(
+        istream &is,
+        Ipv6Header &header
+)
+{
+    // read the first 40 bytes (mandatory for IPv6 header) from the input stream
+    // and stores in the buffer object
+    header.Payload.read( is );
+
+    if ( header.get_version() != 6 )
+    {
+        is.setstate( ios::failbit );
+    }
+
+    return is;
+}
diff --git a/src/ip/ipv6header.h b/src/ip/ipv6header.h
new file mode 100644 (file)
index 0000000..076bc8b
--- /dev/null
@@ -0,0 +1,90 @@
+// 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 IPV6_HEADER_H
+#define IPV6_HEADER_H
+
+#include <stdint.h>
+
+#include <boost/asio/ip/address_v6.hpp>
+
+#include "host/messagepayload.h"
+
+//-----------------------------------------------------------------------------
+// Ipv6Header
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief This class represents the IP version 6 Packet Header.
+ *
+ * The IPv6 header 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
+ * +--------------------------------------------------------------+      ---
+ * @endcode
+ */
+class Ipv6Header
+{
+public:
+    Ipv6Header();
+
+    uint8_t get_version() const;
+    uint8_t get_traffic_class() const;
+    uint32_t get_flow_label() const;
+
+    uint16_t get_payload_length() const;
+    uint8_t get_next_header() const;
+    uint8_t get_hop_limit() const;
+
+    boost::asio::ip::address_v6 get_source_address() const;
+    boost::asio::ip::address_v6 get_destination_address() const;
+
+    friend std::istream &operator>>(
+            std::istream &is,
+            Ipv6Header &header
+    );
+
+private:
+    MessagePayload Payload;
+
+};
+
+#endif // IPV6_HEADER_H