From e2a7289787934776028e394ed8e68eaf9c13e601 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 18 Oct 2011 21:50:05 -0200 Subject: [PATCH] Renamed IcmpHeader class to Icmpv4Header, in order to distinguish from ICMP v6 --- src/CMakeLists.txt | 3 +- src/icmp/icmpheader.cpp | 179 --------------------------------------------- src/icmp/icmpheader.h | 88 ---------------------- src/icmp/icmppacket.cpp | 5 +- src/icmp/icmppacket.h | 8 +- src/icmp/icmppinger.cpp | 8 +- src/icmp/icmpv4header.cpp | 179 +++++++++++++++++++++++++++++++++++++++++++++ src/icmp/icmpv4header.h | 88 ++++++++++++++++++++++ 8 files changed, 280 insertions(+), 278 deletions(-) delete mode 100644 src/icmp/icmpheader.cpp delete mode 100644 src/icmp/icmpheader.h create mode 100644 src/icmp/icmpv4header.cpp create mode 100644 src/icmp/icmpv4header.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 10fb523..536a5da 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,11 +48,12 @@ set(SOURCES icmp/icmpechoreplymessage.cpp icmp/icmpechorequestmessage.cpp icmp/icmpgenericmessage.cpp - icmp/icmpheader.cpp + icmp/icmpv4header.cpp icmp/icmpmessage.cpp icmp/icmppacket.cpp icmp/icmppinger.cpp ip/ipv4header.cpp + ip/ipv6header.cpp link/linkstatusanalyzer.cpp link/statusnotifiercommand.cpp tcp/tcpheader.cpp diff --git a/src/icmp/icmpheader.cpp b/src/icmp/icmpheader.cpp deleted file mode 100644 index 54f4523..0000000 --- a/src/icmp/icmpheader.cpp +++ /dev/null @@ -1,179 +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/icmpheader.h" - -#include "icmp/icmpdestinationunreachablemessage.h" -#include "icmp/icmpechoreplymessage.h" -#include "icmp/icmpechorequestmessage.h" -#include "icmp/icmpgenericmessage.h" - -using namespace std; -using boost::shared_ptr; - -//----------------------------------------------------------------------------- -// IcmpHeader -//----------------------------------------------------------------------------- - -IcmpHeader::IcmpHeader() : - MessageFormat() -{ -} - -IcmpHeader::IcmpHeader( - IcmpType type, - uint8_t code, - uint16_t checksum, - uint16_t identifier, - uint16_t sequence_number -) : - MessageFormat() -{ - set_icmp_message_format( type ); - - set_type( type ); - set_code( code ); - set_checksum( checksum ); - set_identifier( identifier ); - set_sequence_number( sequence_number ); -} - -IcmpType IcmpHeader::get_type() const -{ - return get_icmp_message_format()->get_type(); -} - -void IcmpHeader::set_type( IcmpType type ) -{ - get_icmp_message_format()->set_type( type ); -} - -uint8_t IcmpHeader::get_code() const -{ - return get_icmp_message_format()->get_code(); -} - -void IcmpHeader::set_code( uint8_t code ) -{ - get_icmp_message_format()->set_code( code ); -} - -uint16_t IcmpHeader::get_checksum() const -{ - return get_icmp_message_format()->get_checksum(); -} - -void IcmpHeader::set_checksum( uint16_t checksum ) -{ - get_icmp_message_format()->set_checksum( checksum ); -} - -uint16_t IcmpHeader::get_identifier() const -{ - return get_icmp_message_format()->get_identifier(); -} - -void IcmpHeader::set_identifier( uint16_t identifier ) -{ - get_icmp_message_format()->set_identifier( identifier ); -} - -uint16_t IcmpHeader::get_sequence_number() const -{ - return get_icmp_message_format()->get_sequence_number(); -} - -void IcmpHeader::set_sequence_number( uint16_t sequence_number ) -{ - get_icmp_message_format()->set_sequence_number( sequence_number ); -} - -shared_ptr IcmpHeader::get_icmp_message_format() const -{ - BOOST_ASSERT( MessageFormat.get() != NULL ); - - return MessageFormat; -} - -void IcmpHeader::set_icmp_message_format( IcmpType type ) -{ - BOOST_ASSERT( MessageFormat.get() == NULL ); - - if ( MessageFormat.get() == NULL ) - { - switch ( type ) - { - case IcmpType_EchoReply: - MessageFormat.reset( new IcmpEchoReplyMessage ); - break; - case IcmpType_EchoRequest: - MessageFormat.reset( new IcmpEchoRequestMessage ); - break; - case IcmpType_DestinationUnreachable: - MessageFormat.reset( - new IcmpDestinationUnreachableMessage - ); - break; - case IcmpType_SourceQuench: - case IcmpType_Redirect: - case IcmpType_TimeExceeded: - case IcmpType_ParameterProblem: - case IcmpType_TimestampRequest: - case IcmpType_TimestampReply: - case IcmpType_InfoRequest: - case IcmpType_InfoReply: - case IcmpType_AddressRequest: - case IcmpType_AddressReply: - case IcmpType_Generic: - MessageFormat.reset( new IcmpGenericMessage ); - break; - case IcmpType_InvalidLast: - default: - BOOST_ASSERT( false ); - break; - } - - MessageFormat->init( type ); - } - - BOOST_ASSERT( MessageFormat.get() != NULL ); - BOOST_ASSERT( MessageFormat->get_type() != IcmpType_InvalidLast ); -} - -void IcmpHeader::set_icmp_message_format( std::istream &is ) -{ - // read the first byte, which contains the type of the ICMP message - char first_byte; - (void) is.read( &first_byte, 1 ); - - // must keep the stream intact, so place the read byte back - (void) is.putback( first_byte ); - - // now select the message format for the given type - IcmpType header_type = static_cast( first_byte ); - set_icmp_message_format( header_type ); -} - -std::istream& operator>>( - std::istream &is, - IcmpHeader &header -) -{ - // select the message format which will parse the fields - header.set_icmp_message_format( is ); - - // delegate the parsing of the fields to a specialized decoder - return header.get_icmp_message_format()->read( is ); -} - -std::ostream& operator<<( - std::ostream &os, - const IcmpHeader &header -) -{ - // delegate the writing of the fields to a specialized encoder - return header.get_icmp_message_format()->write( os ); -} diff --git a/src/icmp/icmpheader.h b/src/icmp/icmpheader.h deleted file mode 100644 index d4dd221..0000000 --- a/src/icmp/icmpheader.h +++ /dev/null @@ -1,88 +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 ICMP_HEADER_H -#define ICMP_HEADER_H - -#include - -#include -#include - -#include - -#include "icmp/icmpmessage.h" -#include "icmp/icmptype.h" - -//----------------------------------------------------------------------------- -// IcmpHeader -//----------------------------------------------------------------------------- - -/** - * @brief This class represents the ICMP Packet Header. - * - * The ICMP Generic Header Format is: - * - * @code - * 0 8 16 31 - * +---------------+---------------+------------------------------+ --- - * | | | | ^ - * | type | code | checksum | 4 bytes - * | | | | v - * +---------------+---------------+------------------------------+ --- - * | | - * | specific to each message | - * | | - * +-------------------------------+------------------------------+ - * @endcode - */ -class IcmpHeader -{ -public: - IcmpHeader(); - IcmpHeader( - IcmpType type, - uint8_t code, - uint16_t checksum, - uint16_t identifier, - uint16_t sequence_number - ); - - IcmpType get_type() const; - void set_type( const IcmpType type ); - - uint8_t get_code() const; - void set_code( const uint8_t code ); - - uint16_t get_checksum() const; - void set_checksum( const uint16_t checksum ); - - uint16_t get_identifier() const; - void set_identifier( const uint16_t identifier ); - - uint16_t get_sequence_number() const; - void set_sequence_number( const uint16_t sequence_number ); - - boost::shared_ptr get_icmp_message_format() const; - void set_icmp_message_format( IcmpType type ); - void set_icmp_message_format( std::istream &is ); - - friend std::istream& operator>>( - std::istream &is, - IcmpHeader &header - ); - friend std::ostream& operator<<( - std::ostream &os, - const IcmpHeader &header - ); - -private: - /// Changeable pointer to different ICMP messages types. - boost::shared_ptr MessageFormat; - -}; - -#endif // ICMP_HEADER_H diff --git a/src/icmp/icmppacket.cpp b/src/icmp/icmppacket.cpp index 4268dab..bbebfcb 100644 --- a/src/icmp/icmppacket.cpp +++ b/src/icmp/icmppacket.cpp @@ -5,6 +5,7 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include "icmp/icmppacket.h" + #include using namespace std; @@ -31,7 +32,7 @@ IcmpPacket::IcmpPacket() : * @param icmp_data The ICMP payload data. */ IcmpPacket::IcmpPacket( - const IcmpHeader &icmp_header, + const Icmpv4Header &icmp_header, const IcmpData &icmp_data ) : IpHeader(), @@ -62,7 +63,7 @@ Ipv4Header IcmpPacket::get_ip_header() const * * @return The ICMP header object. */ -IcmpHeader IcmpPacket::get_icmp_header() const +Icmpv4Header IcmpPacket::get_icmp_header() const { return IcmpPayloadHeader; } diff --git a/src/icmp/icmppacket.h b/src/icmp/icmppacket.h index dfe9445..1106bc0 100644 --- a/src/icmp/icmppacket.h +++ b/src/icmp/icmppacket.h @@ -14,7 +14,7 @@ #include -#include "icmp/icmpheader.h" +#include "icmp/icmpv4header.h" #include "icmp/icmpdata.h" #include "icmp/icmptype.h" #include "ip/ipv4header.h" @@ -70,13 +70,13 @@ class IcmpPacket public: IcmpPacket(); IcmpPacket( - const IcmpHeader &icmp_header, + const Icmpv4Header &icmp_header, const IcmpData &icmp_data ); virtual ~IcmpPacket(); Ipv4Header get_ip_header() const; - IcmpHeader get_icmp_header() const; + Icmpv4Header get_icmp_header() const; IcmpData get_icmp_data() const; bool match( @@ -99,7 +99,7 @@ private: /// The IP header. Ipv4Header IpHeader; /// The ICMP packet header. - IcmpHeader IcmpPayloadHeader; + Icmpv4Header IcmpPayloadHeader; /// The ICMP packet payload (data). IcmpData IcmpPayloadData; }; diff --git a/src/icmp/icmppinger.cpp b/src/icmp/icmppinger.cpp index 4e45e58..56e9e1b 100644 --- a/src/icmp/icmppinger.cpp +++ b/src/icmp/icmppinger.cpp @@ -23,7 +23,7 @@ #include "icmp/icmpchecksum.h" #include "icmp/icmpdata.h" -#include "icmp/icmpheader.h" +#include "icmp/icmpv4header.h" #include "icmp/icmppacket.h" #include "icmp/icmptype.h" #include "ip/ipv4header.h" @@ -140,7 +140,7 @@ IcmpPacket IcmpPinger::create_echo_request( uint16_t checksum = calculator.compute( type, code, Identifier, sequence_number ); - IcmpHeader icmp_header( + Icmpv4Header icmp_header( type, code, checksum, Identifier, sequence_number ); @@ -316,7 +316,7 @@ void IcmpPinger::print_echo_reply( BOOST_ASSERT( icmp_packet.get_icmp_header().get_type() == IcmpType_EchoReply ); Ipv4Header ipv4_header = icmp_packet.get_ip_header(); - IcmpHeader icmp_header = icmp_packet.get_icmp_header(); + Icmpv4Header icmp_header = icmp_packet.get_icmp_header(); size_t bytes_received = bytes_transferred - ipv4_header.get_header_length(); string remote_address = ipv4_header.get_source_address().to_string(); @@ -347,7 +347,7 @@ void IcmpPinger::print_destination_unreachable( BOOST_ASSERT( icmp_packet.get_icmp_header().get_type() == IcmpType_DestinationUnreachable ); Ipv4Header ipv4_hdr = icmp_packet.get_ip_header(); - IcmpHeader icmp_hdr = icmp_packet.get_icmp_header(); + Icmpv4Header icmp_hdr = icmp_packet.get_icmp_header(); string local_address = ipv4_hdr.get_destination_address().to_string(); uint16_t sequence_number = icmp_hdr.get_sequence_number(); diff --git a/src/icmp/icmpv4header.cpp b/src/icmp/icmpv4header.cpp new file mode 100644 index 0000000..2f45211 --- /dev/null +++ b/src/icmp/icmpv4header.cpp @@ -0,0 +1,179 @@ +// 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/icmpv4header.h" + +#include "icmp/icmpdestinationunreachablemessage.h" +#include "icmp/icmpechoreplymessage.h" +#include "icmp/icmpechorequestmessage.h" +#include "icmp/icmpgenericmessage.h" + +using namespace std; +using boost::shared_ptr; + +//----------------------------------------------------------------------------- +// Icmpv4Header +//----------------------------------------------------------------------------- + +Icmpv4Header::Icmpv4Header() : + MessageFormat() +{ +} + +Icmpv4Header::Icmpv4Header( + IcmpType type, + uint8_t code, + uint16_t checksum, + uint16_t identifier, + uint16_t sequence_number +) : + MessageFormat() +{ + set_icmp_message_format( type ); + + set_type( type ); + set_code( code ); + set_checksum( checksum ); + set_identifier( identifier ); + set_sequence_number( sequence_number ); +} + +IcmpType Icmpv4Header::get_type() const +{ + return get_icmp_message_format()->get_type(); +} + +void Icmpv4Header::set_type( IcmpType type ) +{ + get_icmp_message_format()->set_type( type ); +} + +uint8_t Icmpv4Header::get_code() const +{ + return get_icmp_message_format()->get_code(); +} + +void Icmpv4Header::set_code( uint8_t code ) +{ + get_icmp_message_format()->set_code( code ); +} + +uint16_t Icmpv4Header::get_checksum() const +{ + return get_icmp_message_format()->get_checksum(); +} + +void Icmpv4Header::set_checksum( uint16_t checksum ) +{ + get_icmp_message_format()->set_checksum( checksum ); +} + +uint16_t Icmpv4Header::get_identifier() const +{ + return get_icmp_message_format()->get_identifier(); +} + +void Icmpv4Header::set_identifier( uint16_t identifier ) +{ + get_icmp_message_format()->set_identifier( identifier ); +} + +uint16_t Icmpv4Header::get_sequence_number() const +{ + return get_icmp_message_format()->get_sequence_number(); +} + +void Icmpv4Header::set_sequence_number( uint16_t sequence_number ) +{ + get_icmp_message_format()->set_sequence_number( sequence_number ); +} + +shared_ptr Icmpv4Header::get_icmp_message_format() const +{ + BOOST_ASSERT( MessageFormat.get() != NULL ); + + return MessageFormat; +} + +void Icmpv4Header::set_icmp_message_format( IcmpType type ) +{ + BOOST_ASSERT( MessageFormat.get() == NULL ); + + if ( MessageFormat.get() == NULL ) + { + switch ( type ) + { + case IcmpType_EchoReply: + MessageFormat.reset( new IcmpEchoReplyMessage ); + break; + case IcmpType_EchoRequest: + MessageFormat.reset( new IcmpEchoRequestMessage ); + break; + case IcmpType_DestinationUnreachable: + MessageFormat.reset( + new IcmpDestinationUnreachableMessage + ); + break; + case IcmpType_SourceQuench: + case IcmpType_Redirect: + case IcmpType_TimeExceeded: + case IcmpType_ParameterProblem: + case IcmpType_TimestampRequest: + case IcmpType_TimestampReply: + case IcmpType_InfoRequest: + case IcmpType_InfoReply: + case IcmpType_AddressRequest: + case IcmpType_AddressReply: + case IcmpType_Generic: + MessageFormat.reset( new IcmpGenericMessage ); + break; + case IcmpType_InvalidLast: + default: + BOOST_ASSERT( false ); + break; + } + + MessageFormat->init( type ); + } + + BOOST_ASSERT( MessageFormat.get() != NULL ); + BOOST_ASSERT( MessageFormat->get_type() != IcmpType_InvalidLast ); +} + +void Icmpv4Header::set_icmp_message_format( std::istream &is ) +{ + // read the first byte, which contains the type of the ICMP message + char first_byte; + (void) is.read( &first_byte, 1 ); + + // must keep the stream intact, so place the read byte back + (void) is.putback( first_byte ); + + // now select the message format for the given type + IcmpType header_type = static_cast( first_byte ); + set_icmp_message_format( header_type ); +} + +std::istream& operator>>( + std::istream &is, + Icmpv4Header &header +) +{ + // select the message format which will parse the fields + header.set_icmp_message_format( is ); + + // delegate the parsing of the fields to a specialized decoder + return header.get_icmp_message_format()->read( is ); +} + +std::ostream& operator<<( + std::ostream &os, + const Icmpv4Header &header +) +{ + // delegate the writing of the fields to a specialized encoder + return header.get_icmp_message_format()->write( os ); +} diff --git a/src/icmp/icmpv4header.h b/src/icmp/icmpv4header.h new file mode 100644 index 0000000..5e07ce4 --- /dev/null +++ b/src/icmp/icmpv4header.h @@ -0,0 +1,88 @@ +// 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 ICMPV4_HEADER_H +#define ICMPV4_HEADER_H + +#include + +#include +#include + +#include + +#include "icmp/icmpmessage.h" +#include "icmp/icmptype.h" + +//----------------------------------------------------------------------------- +// Icmpv4Header +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the ICMP version 4 Packet Header. + * + * The ICMP Generic Header Format is: + * + * @code + * 0 8 16 31 + * +---------------+---------------+------------------------------+ --- + * | | | | ^ + * | type | code | checksum | 4 bytes + * | | | | v + * +---------------+---------------+------------------------------+ --- + * | | + * | specific to each message | + * | | + * +-------------------------------+------------------------------+ + * @endcode + */ +class Icmpv4Header +{ +public: + Icmpv4Header(); + Icmpv4Header( + IcmpType type, + uint8_t code, + uint16_t checksum, + uint16_t identifier, + uint16_t sequence_number + ); + + IcmpType get_type() const; + void set_type( const IcmpType type ); + + uint8_t get_code() const; + void set_code( const uint8_t code ); + + uint16_t get_checksum() const; + void set_checksum( const uint16_t checksum ); + + uint16_t get_identifier() const; + void set_identifier( const uint16_t identifier ); + + uint16_t get_sequence_number() const; + void set_sequence_number( const uint16_t sequence_number ); + + boost::shared_ptr get_icmp_message_format() const; + void set_icmp_message_format( IcmpType type ); + void set_icmp_message_format( std::istream &is ); + + friend std::istream& operator>>( + std::istream &is, + Icmpv4Header &header + ); + friend std::ostream& operator<<( + std::ostream &os, + const Icmpv4Header &header + ); + +private: + /// Changeable pointer to different ICMP messages types. + boost::shared_ptr MessageFormat; + +}; + +#endif // ICMPV4_HEADER_H -- 1.7.1