created abstract base class IpHeader of Ipv4Header and Ipv6Header
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Mar 2015 09:53:22 +0000 (10:53 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Mar 2015 09:53:22 +0000 (10:53 +0100)
src/ip/ipheader.h [new file with mode: 0644]
src/ip/ipv4header.cpp
src/ip/ipv4header.h
src/ip/ipv6header.cpp
src/ip/ipv6header.h

diff --git a/src/ip/ipheader.h b/src/ip/ipheader.h
new file mode 100644 (file)
index 0000000..1fff074
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+
+ Christian Herdtweck, Intra2net AG 2015
+ */
+
+#ifndef IPHEADER_H
+#define IPHEADER_H
+
+#include <stdint.h>
+#include <istream>
+#include <boost/shared_ptr.hpp>
+#include <boost/asio/ip/address.hpp>
+
+/**
+ * abstract base class of IPv4Header and IPv6Header
+ */
+class IpHeader
+{
+public:
+    virtual boost::asio::ip::address get_source_address() const = 0;
+    virtual boost::asio::ip::address get_destination_address() const = 0;
+    virtual uint8_t get_version() const = 0;
+    virtual uint16_t get_header_length() const = 0;
+    virtual uint8_t get_time_to_live() const = 0;
+    virtual uint16_t get_total_length() const = 0;
+
+    virtual ~IpHeader() {}
+
+};
+
+typedef boost::shared_ptr<IpHeader> IpHeaderPtr;
+
+#endif
+
+// (created using vim -- the world's best text editor)
+
index 821f0e7..299b4e4 100644 (file)
@@ -12,6 +12,7 @@
 
 using namespace std;
 using boost::asio::ip::address_v4;
+using boost::asio::ip::address;
 using boost::scoped_array;
 using I2n::Logger::GlobalLogger;
 
@@ -19,13 +20,13 @@ using I2n::Logger::GlobalLogger;
 // Ipv4Header
 //-----------------------------------------------------------------------------
 
-static const size_t Ipv4HeaderSizeInBytes = 20;
+static const size_t Ipv4HeaderSizeInBytes_withoutOptions = 20;
 
 /**
  * @brief Default constructor.
  */
 Ipv4Header::Ipv4Header() :
-    Payload( Ipv4HeaderSizeInBytes )
+    Payload( Ipv4HeaderSizeInBytes_withoutOptions )
 {
 }
 
@@ -187,7 +188,7 @@ uint16_t Ipv4Header::get_header_checksum() const
  *
  * @brief The source address.
  */
-address_v4 Ipv4Header::get_source_address() const
+address Ipv4Header::get_source_address() const
 {
     uint32_t address = Payload.decode32( 12, 15 );
 
@@ -201,7 +202,7 @@ address_v4 Ipv4Header::get_source_address() const
  *
  * @return The destination address.
  */
-address_v4 Ipv4Header::get_destination_address() const
+address Ipv4Header::get_destination_address() const
 {
     uint32_t address = Payload.decode32( 16, 19 );
 
@@ -229,7 +230,7 @@ istream &operator>>(
     // read the consecutive N bytes (for options field) from the input stream
     // and stores in the buffer object
     streamsize options_length = static_cast<streamsize>( header.get_header_length() ) -
-                                static_cast<streamsize>( Ipv4HeaderSizeInBytes );
+                                static_cast<streamsize>( Ipv4HeaderSizeInBytes_withoutOptions );
     if ( ( options_length < 0 ) || ( 40 < options_length ) )
     {
         GlobalLogger.error() << "Invalid IP options length value:" << options_length << endl;
index aa9d9cc..71f8290 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <boost/asio/ip/address_v4.hpp>
 
+#include "ip/ipheader.h"
 #include "host/messagepayload.h"
 
 //-----------------------------------------------------------------------------
@@ -54,7 +55,7 @@
  * +--------------------------------------------------------------+      ---
  * @endcode
  */
-class Ipv4Header
+class Ipv4Header : public IpHeader
 {
 public:
     Ipv4Header();
@@ -73,8 +74,8 @@ public:
     uint8_t get_protocol() const;
     uint16_t get_header_checksum() const;
 
-    boost::asio::ip::address_v4 get_source_address() const;
-    boost::asio::ip::address_v4 get_destination_address() const;
+    boost::asio::ip::address get_source_address() const;
+    boost::asio::ip::address get_destination_address() const;
 
     friend std::istream &operator>>(
             std::istream &is,
index c154a78..ca03580 100644 (file)
@@ -13,6 +13,7 @@
 
 using namespace std;
 using boost::asio::ip::address_v6;
+using boost::asio::ip::address;
 using boost::scoped_array;
 
 //-----------------------------------------------------------------------------
@@ -120,7 +121,7 @@ uint8_t Ipv6Header::get_hop_limit() const
  *
  * @brief The source address.
  */
-address_v6 Ipv6Header::get_source_address() const
+address Ipv6Header::get_source_address() const
 {
     address_v6::bytes_type address; // 16 bytes
     BOOST_ASSERT( 16 == address_v6::bytes_type::size() );
@@ -145,7 +146,7 @@ address_v6 Ipv6Header::get_source_address() const
  *
  * @return The destination address.
  */
-address_v6 Ipv6Header::get_destination_address() const
+address Ipv6Header::get_destination_address() const
 {
     address_v6::bytes_type address; // 16 bytes
     BOOST_ASSERT( 16 == address_v6::bytes_type::size() );
index cd287e3..57db286 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <boost/asio/ip/address_v6.hpp>
 
+#include "ip/ipheader.h"
 #include "host/messagepayload.h"
 
 //-----------------------------------------------------------------------------
@@ -61,7 +62,7 @@
  * +--------------------------------------------------------------+      ---
  * @endcode
  */
-class Ipv6Header
+class Ipv6Header : public IpHeader
 {
 public:
     Ipv6Header();
@@ -74,8 +75,18 @@ public:
     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;
+    uint16_t get_header_length() const
+    {  return 40;   }
+
+    uint16_t get_total_length() const
+    {  return get_payload_length() + 40;  }
+
+    // in IPv6, this corresponds to the "HOP limit"
+    uint8_t get_time_to_live() const
+    {   return get_hop_limit();    }
+
+    boost::asio::ip::address get_source_address() const;
+    boost::asio::ip::address get_destination_address() const;
 
     friend std::istream &operator>>(
             std::istream &is,