--- /dev/null
+/*
+ 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)
+
using namespace std;
using boost::asio::ip::address_v4;
+using boost::asio::ip::address;
using boost::scoped_array;
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 )
{
}
*
* @brief The source address.
*/
-address_v4 Ipv4Header::get_source_address() const
+address Ipv4Header::get_source_address() const
{
uint32_t address = Payload.decode32( 12, 15 );
*
* @return The destination address.
*/
-address_v4 Ipv4Header::get_destination_address() const
+address Ipv4Header::get_destination_address() const
{
uint32_t address = Payload.decode32( 16, 19 );
// 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;
#include <boost/asio/ip/address_v4.hpp>
+#include "ip/ipheader.h"
#include "host/messagepayload.h"
//-----------------------------------------------------------------------------
* +--------------------------------------------------------------+ ---
* @endcode
*/
-class Ipv4Header
+class Ipv4Header : public IpHeader
{
public:
Ipv4Header();
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,
using namespace std;
using boost::asio::ip::address_v6;
+using boost::asio::ip::address;
using boost::scoped_array;
//-----------------------------------------------------------------------------
*
* @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() );
*
* @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() );
#include <boost/asio/ip/address_v6.hpp>
+#include "ip/ipheader.h"
#include "host/messagepayload.h"
//-----------------------------------------------------------------------------
* +--------------------------------------------------------------+ ---
* @endcode
*/
-class Ipv6Header
+class Ipv6Header : public IpHeader
{
public:
Ipv6Header();
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,