From 5d9d28086590c7a2cf79ca54ae4807e6acb80c05 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 12 Mar 2015 10:53:22 +0100 Subject: [PATCH] created abstract base class IpHeader of Ipv4Header and Ipv6Header --- src/ip/ipheader.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ip/ipv4header.cpp | 11 +++++---- src/ip/ipv4header.h | 7 +++-- src/ip/ipv6header.cpp | 5 ++- src/ip/ipv6header.h | 17 +++++++++++++-- 5 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 src/ip/ipheader.h diff --git a/src/ip/ipheader.h b/src/ip/ipheader.h new file mode 100644 index 0000000..1fff074 --- /dev/null +++ b/src/ip/ipheader.h @@ -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 +#include +#include +#include + +/** + * 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 IpHeaderPtr; + +#endif + +// (created using vim -- the world's best text editor) + diff --git a/src/ip/ipv4header.cpp b/src/ip/ipv4header.cpp index 821f0e7..299b4e4 100644 --- a/src/ip/ipv4header.cpp +++ b/src/ip/ipv4header.cpp @@ -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( header.get_header_length() ) - - static_cast( Ipv4HeaderSizeInBytes ); + static_cast( Ipv4HeaderSizeInBytes_withoutOptions ); if ( ( options_length < 0 ) || ( 40 < options_length ) ) { GlobalLogger.error() << "Invalid IP options length value:" << options_length << endl; diff --git a/src/ip/ipv4header.h b/src/ip/ipv4header.h index aa9d9cc..71f8290 100644 --- a/src/ip/ipv4header.h +++ b/src/ip/ipv4header.h @@ -11,6 +11,7 @@ #include +#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, diff --git a/src/ip/ipv6header.cpp b/src/ip/ipv6header.cpp index c154a78..ca03580 100644 --- a/src/ip/ipv6header.cpp +++ b/src/ip/ipv6header.cpp @@ -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() ); diff --git a/src/ip/ipv6header.h b/src/ip/ipv6header.h index cd287e3..57db286 100644 --- a/src/ip/ipv6header.h +++ b/src/ip/ipv6header.h @@ -11,6 +11,7 @@ #include +#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, -- 1.7.1