From 3bd915ea994b7c56a13bb65975cb4b00d14810c6 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Sat, 9 Jul 2011 22:57:00 -0300 Subject: [PATCH] Moved the IP classes to its own directory. --- src/CMakeLists.txt | 2 +- src/icmp/icmppacket.h | 2 +- src/icmp/icmppinger.cpp | 2 +- src/icmp/ipv4header.cpp | 140 ----------------------------------------------- src/icmp/ipv4header.h | 87 ----------------------------- src/ip/ipv4header.cpp | 140 +++++++++++++++++++++++++++++++++++++++++++++++ src/ip/ipv4header.h | 87 +++++++++++++++++++++++++++++ 7 files changed, 230 insertions(+), 230 deletions(-) delete mode 100644 src/icmp/ipv4header.cpp delete mode 100644 src/icmp/ipv4header.h create mode 100644 src/ip/ipv4header.cpp create mode 100644 src/ip/ipv4header.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 96e656a..6442b0f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -44,7 +44,7 @@ set(SOURCES icmp/icmpmessagepayload.cpp icmp/icmppacket.cpp icmp/icmppinger.cpp - icmp/ipv4header.cpp + ip/ipv4header.cpp link/linkstatusanalyzer.cpp link/statusnotifiercommand.cpp tcp/tcppinger.cpp diff --git a/src/icmp/icmppacket.h b/src/icmp/icmppacket.h index afc4a11..8a2d96a 100644 --- a/src/icmp/icmppacket.h +++ b/src/icmp/icmppacket.h @@ -14,10 +14,10 @@ #include -#include "icmp/ipv4header.h" #include "icmp/icmpheader.h" #include "icmp/icmpdata.h" #include "icmp/icmptype.h" +#include "ip/ipv4header.h" //----------------------------------------------------------------------------- // IcmpPacket diff --git a/src/icmp/icmppinger.cpp b/src/icmp/icmppinger.cpp index 85fb14d..3722a48 100644 --- a/src/icmp/icmppinger.cpp +++ b/src/icmp/icmppinger.cpp @@ -24,7 +24,7 @@ #include "icmp/icmpheader.h" #include "icmp/icmppacket.h" #include "icmp/icmptype.h" -#include "icmp/ipv4header.h" +#include "ip/ipv4header.h" using namespace std; using boost::asio::const_buffers_1; diff --git a/src/icmp/ipv4header.cpp b/src/icmp/ipv4header.cpp deleted file mode 100644 index fd4a194..0000000 --- a/src/icmp/ipv4header.cpp +++ /dev/null @@ -1,140 +0,0 @@ -// 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/ipv4header.h" - -#include - -using namespace std; -using boost::asio::ip::address_v4; - -//----------------------------------------------------------------------------- -// Ipv4Header -//----------------------------------------------------------------------------- - -const int Ipv4Header::HeaderSizeInBytes = 20; - -Ipv4Header::Ipv4Header() -{ - fill( Payload, Payload + sizeof(Payload), 0 ); -} - -uint8_t Ipv4Header::get_version() const -{ - return ( Payload[ 0 ] >> 4 ) & 0xF; -} - -uint16_t Ipv4Header::get_header_length() const -{ - return static_cast ( (Payload[ 0 ] & 0xF) * 4 ); -} - -uint8_t Ipv4Header::get_type_of_service() const -{ - return Payload[ 1 ]; -} - -uint16_t Ipv4Header::get_total_length() const -{ - return decode( 2, 3 ); -} - -uint16_t Ipv4Header::get_identification() const -{ - return decode( 4, 5 ); -} - -bool Ipv4Header::dont_fragment() const -{ - return ( ( Payload[ 6 ] & 0x40 ) != 0 ); -} - -bool Ipv4Header::more_fragments() const -{ - return ( ( Payload[ 6 ] & 0x20 ) != 0 ); -} - -uint16_t Ipv4Header::get_fragment_offset() const -{ - return decode( 6, 7 ) & 0x1FFF; -} - -uint8_t Ipv4Header::get_time_to_live() const -{ - return Payload[ 8 ]; -} - -uint8_t Ipv4Header::get_protocol() const -{ - return Payload[ 9 ]; -} - -uint16_t Ipv4Header::get_header_checksum() const -{ - return decode( 10, 11 ); -} - -address_v4 Ipv4Header::get_source_address() const -{ - address_v4::bytes_type bytes = { { - Payload[ 12 ], - Payload[ 13 ], - Payload[ 14 ], - Payload[ 15 ] - } }; - return address_v4( bytes ); -} - -address_v4 Ipv4Header::get_destination_address() const -{ - address_v4::bytes_type bytes = { { - Payload[ 16 ], - Payload[ 17 ], - Payload[ 18 ], - Payload[ 19 ] - } }; - return address_v4( bytes ); -} - -istream &operator>>( - istream &is, - Ipv4Header &header -) -{ - (void) is.read( - reinterpret_cast ( header.Payload ), - Ipv4Header::HeaderSizeInBytes - ); - - if ( header.get_version() != 4 ) - is.setstate( ios::failbit ); - streamsize options_length = - (streamsize) header.get_header_length() - - Ipv4Header::HeaderSizeInBytes; - if ( options_length < 0 || 40 < options_length ) - { - is.setstate( ios::failbit ); - } - else - { - (void) is.read( - reinterpret_cast ( header.Payload ) + - Ipv4Header::HeaderSizeInBytes, - options_length - ); - } - - return is; -} - -uint16_t Ipv4Header::decode( int left_byte, int right_byte ) const -{ - uint32_t value = ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; - - BOOST_ASSERT( value <= numeric_limits::max() ); - - return static_cast ( value ); -} diff --git a/src/icmp/ipv4header.h b/src/icmp/ipv4header.h deleted file mode 100644 index d8bb3a4..0000000 --- a/src/icmp/ipv4header.h +++ /dev/null @@ -1,87 +0,0 @@ -// 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 IPV4_HEADER_HPP -#define IPV4_HEADER_HPP - -#include - -#include - -//----------------------------------------------------------------------------- -// Ipv4Header -//----------------------------------------------------------------------------- -// IPv4 header format: -// -// 0 8 16 31 -// +-------+-------+---------------+------------------------------+ --- -// | | | | | ^ -// |version|header | type of | total length in bytes | | -// | (4) | length| service | | | -// +-------+-------+---------------+-+-+-+------------------------+ | -// | | | | | | | -// | identification |0|D|M| fragment offset | | -// | | |F|F| | | -// +---------------+---------------+-+-+-+------------------------+ | -// | | | | | -// | time to live | protocol | header checksum | 20 bytes -// | | | | | -// +---------------+---------------+------------------------------+ | -// | | | -// | source IPv4 address | | -// | | | -// +--------------------------------------------------------------+ | -// | | | -// | destination IPv4 address | | -// | | v -// +--------------------------------------------------------------+ --- -// | | ^ -// | | | -// / options (if any) / 0 - 40 -// / / bytes -// | | | -// | | v -// +--------------------------------------------------------------+ --- -// -//----------------------------------------------------------------------------- - -class Ipv4Header -{ -public: - Ipv4Header(); - - uint8_t get_version() const; - uint16_t get_header_length() const; - uint8_t get_type_of_service() const; - uint16_t get_total_length() const; - - uint16_t get_identification() const; - bool dont_fragment() const; - bool more_fragments() const; - uint16_t get_fragment_offset() const; - - uint8_t get_time_to_live() const; - 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; - - friend std::istream &operator>>( - std::istream &is, - Ipv4Header &header - ); - -private: - uint16_t decode( int left_byte, int right_byte ) const; - -private: - const static int HeaderSizeInBytes; - uint8_t Payload[ 60 ]; - -}; - -#endif // IPV4_HEADER_H diff --git a/src/ip/ipv4header.cpp b/src/ip/ipv4header.cpp new file mode 100644 index 0000000..7e304b4 --- /dev/null +++ b/src/ip/ipv4header.cpp @@ -0,0 +1,140 @@ +// 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/ipv4header.h" + +#include + +using namespace std; +using boost::asio::ip::address_v4; + +//----------------------------------------------------------------------------- +// Ipv4Header +//----------------------------------------------------------------------------- + +const int Ipv4Header::HeaderSizeInBytes = 20; + +Ipv4Header::Ipv4Header() +{ + fill( Payload, Payload + sizeof(Payload), 0 ); +} + +uint8_t Ipv4Header::get_version() const +{ + return ( Payload[ 0 ] >> 4 ) & 0xF; +} + +uint16_t Ipv4Header::get_header_length() const +{ + return static_cast ( (Payload[ 0 ] & 0xF) * 4 ); +} + +uint8_t Ipv4Header::get_type_of_service() const +{ + return Payload[ 1 ]; +} + +uint16_t Ipv4Header::get_total_length() const +{ + return decode( 2, 3 ); +} + +uint16_t Ipv4Header::get_identification() const +{ + return decode( 4, 5 ); +} + +bool Ipv4Header::dont_fragment() const +{ + return ( ( Payload[ 6 ] & 0x40 ) != 0 ); +} + +bool Ipv4Header::more_fragments() const +{ + return ( ( Payload[ 6 ] & 0x20 ) != 0 ); +} + +uint16_t Ipv4Header::get_fragment_offset() const +{ + return decode( 6, 7 ) & 0x1FFF; +} + +uint8_t Ipv4Header::get_time_to_live() const +{ + return Payload[ 8 ]; +} + +uint8_t Ipv4Header::get_protocol() const +{ + return Payload[ 9 ]; +} + +uint16_t Ipv4Header::get_header_checksum() const +{ + return decode( 10, 11 ); +} + +address_v4 Ipv4Header::get_source_address() const +{ + address_v4::bytes_type bytes = { { + Payload[ 12 ], + Payload[ 13 ], + Payload[ 14 ], + Payload[ 15 ] + } }; + return address_v4( bytes ); +} + +address_v4 Ipv4Header::get_destination_address() const +{ + address_v4::bytes_type bytes = { { + Payload[ 16 ], + Payload[ 17 ], + Payload[ 18 ], + Payload[ 19 ] + } }; + return address_v4( bytes ); +} + +istream &operator>>( + istream &is, + Ipv4Header &header +) +{ + (void) is.read( + reinterpret_cast ( header.Payload ), + Ipv4Header::HeaderSizeInBytes + ); + + if ( header.get_version() != 4 ) + is.setstate( ios::failbit ); + streamsize options_length = + (streamsize) header.get_header_length() - + Ipv4Header::HeaderSizeInBytes; + if ( options_length < 0 || 40 < options_length ) + { + is.setstate( ios::failbit ); + } + else + { + (void) is.read( + reinterpret_cast ( header.Payload ) + + Ipv4Header::HeaderSizeInBytes, + options_length + ); + } + + return is; +} + +uint16_t Ipv4Header::decode( int left_byte, int right_byte ) const +{ + uint32_t value = ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; + + BOOST_ASSERT( value <= numeric_limits::max() ); + + return static_cast ( value ); +} diff --git a/src/ip/ipv4header.h b/src/ip/ipv4header.h new file mode 100644 index 0000000..d8bb3a4 --- /dev/null +++ b/src/ip/ipv4header.h @@ -0,0 +1,87 @@ +// 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 IPV4_HEADER_HPP +#define IPV4_HEADER_HPP + +#include + +#include + +//----------------------------------------------------------------------------- +// Ipv4Header +//----------------------------------------------------------------------------- +// IPv4 header format: +// +// 0 8 16 31 +// +-------+-------+---------------+------------------------------+ --- +// | | | | | ^ +// |version|header | type of | total length in bytes | | +// | (4) | length| service | | | +// +-------+-------+---------------+-+-+-+------------------------+ | +// | | | | | | | +// | identification |0|D|M| fragment offset | | +// | | |F|F| | | +// +---------------+---------------+-+-+-+------------------------+ | +// | | | | | +// | time to live | protocol | header checksum | 20 bytes +// | | | | | +// +---------------+---------------+------------------------------+ | +// | | | +// | source IPv4 address | | +// | | | +// +--------------------------------------------------------------+ | +// | | | +// | destination IPv4 address | | +// | | v +// +--------------------------------------------------------------+ --- +// | | ^ +// | | | +// / options (if any) / 0 - 40 +// / / bytes +// | | | +// | | v +// +--------------------------------------------------------------+ --- +// +//----------------------------------------------------------------------------- + +class Ipv4Header +{ +public: + Ipv4Header(); + + uint8_t get_version() const; + uint16_t get_header_length() const; + uint8_t get_type_of_service() const; + uint16_t get_total_length() const; + + uint16_t get_identification() const; + bool dont_fragment() const; + bool more_fragments() const; + uint16_t get_fragment_offset() const; + + uint8_t get_time_to_live() const; + 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; + + friend std::istream &operator>>( + std::istream &is, + Ipv4Header &header + ); + +private: + uint16_t decode( int left_byte, int right_byte ) const; + +private: + const static int HeaderSizeInBytes; + uint8_t Payload[ 60 ]; + +}; + +#endif // IPV4_HEADER_H -- 1.7.1