From 4939ed75d0d371b7a46a5bc94c8366eaeb58f75a Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Sat, 13 Aug 2011 13:39:36 -0300 Subject: [PATCH] Renamed IcmpMessagePayload to MessagePayload, so it can be used by other messages types --- src/CMakeLists.txt | 2 +- src/host/messagepayload.cpp | 130 ++++++++++++++++++++++++++ src/host/messagepayload.h | 63 +++++++++++++ src/icmp/icmpdestinationunreachablemessage.h | 4 +- src/icmp/icmpechoreplymessage.h | 4 +- src/icmp/icmpechorequestmessage.h | 4 +- src/icmp/icmpmessagepayload.cpp | 129 ------------------------- src/icmp/icmpmessagepayload.h | 63 ------------- 8 files changed, 200 insertions(+), 199 deletions(-) create mode 100644 src/host/messagepayload.cpp create mode 100644 src/host/messagepayload.h delete mode 100644 src/icmp/icmpmessagepayload.cpp delete mode 100644 src/icmp/icmpmessagepayload.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0f02a6a..eeb9734 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,6 +35,7 @@ set(SOURCES dns/hostaddress.cpp dns/timetolive.cpp host/hoststatusanalyzer.cpp + host/messagepayload.cpp host/pinger.cpp host/pingerfactory.cpp host/pinginterval.cpp @@ -46,7 +47,6 @@ set(SOURCES icmp/icmpgenericmessage.cpp icmp/icmpheader.cpp icmp/icmpmessage.cpp - icmp/icmpmessagepayload.cpp icmp/icmppacket.cpp icmp/icmppinger.cpp ip/ipv4header.cpp diff --git a/src/host/messagepayload.cpp b/src/host/messagepayload.cpp new file mode 100644 index 0000000..00c9a6f --- /dev/null +++ b/src/host/messagepayload.cpp @@ -0,0 +1,130 @@ +// 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 "host/messagepayload.h" + +#include + +#include + +#include + +using namespace std; +using boost::scoped_array; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// MessagePayload +//----------------------------------------------------------------------------- + +/** + * @param payload_size_in_bytes the size of the payload (ie the internal buffer) + */ +MessagePayload::MessagePayload( + const std::size_t &payload_size_in_bytes +) : + PayloadSizeInBytes( payload_size_in_bytes ), + Payload( new uint8_t[ payload_size_in_bytes ] ) +{ + BOOST_ASSERT( 0 < payload_size_in_bytes ); + + fill( Payload.get(), Payload.get() + PayloadSizeInBytes, 0 ); +} + +MessagePayload::~MessagePayload() +{ + // Payload automatically delete by smart pointer scope end +} + +/** + * @brief The element access operator to provide access syntax like regular + * arrays. + */ +const uint8_t& MessagePayload::operator[]( size_t offset ) const +{ + BOOST_ASSERT( offset < PayloadSizeInBytes ); + + return Payload[ offset ]; +} + +/** + * @brief The element access operator to provide access syntax like regular + * arrays. + */ +uint8_t& MessagePayload::operator[]( size_t offset ) +{ + BOOST_ASSERT( offset < PayloadSizeInBytes ); + + return Payload[ offset ]; +} + +/** + * @brief Retrieve 16 bits from the payload buffer. + * + * @param left_byte the index of the left byte + * @param right_byte the index of the right byte + * + * @return a concatenation of the byte indexed by left_byte with the byte + * indexed by right_byte + */ +uint16_t MessagePayload::decode16( + const int left_byte, + const int right_byte +) const +{ + uint32_t value = ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; + + BOOST_ASSERT( value <= numeric_limits::max() ); + + return static_cast ( value ); +} + +/** + * @brief Store 16 bits in the payload buffer. + * + * @param left_byte the index of the left byte + * @param right_byte the index of the right byte + * @param value a 16 bits data be saved in the bytes indexed by left_byte and + * right_byte. + * + * @return void + */ +void MessagePayload::encode16( + const int left_byte, + const int right_byte, + const uint16_t value +) +{ + Payload[ left_byte ] = static_cast ( value >> 8 ); + Payload[ right_byte ] = static_cast ( value & 0xFF ); +} + +/** + * @brief Read all the data from the payload and stores in the istream. + */ +istream& MessagePayload::read( istream &is ) +{ + char *payload_data_array = reinterpret_cast ( Payload.get() ); + (void) is.read( payload_data_array, PayloadSizeInBytes ); + + size_t data_received_in_bytes = static_cast( is.gcount() ); + if ( data_received_in_bytes != PayloadSizeInBytes ) + { + GlobalLogger.error() << "Error: expecting " << PayloadSizeInBytes + << " bytes, but received " << is.gcount() << " bytes" << endl; + } + + return is; +} + +/** + * @brief Writes all the data present in the ostream to the payload buffer. + */ +ostream& MessagePayload::write( ostream &os ) const +{ + const char *data_array = reinterpret_cast ( Payload.get() ); + return os.write( data_array, PayloadSizeInBytes ); +} diff --git a/src/host/messagepayload.h b/src/host/messagepayload.h new file mode 100644 index 0000000..5bc19e2 --- /dev/null +++ b/src/host/messagepayload.h @@ -0,0 +1,63 @@ +// 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 MESSAGE_PAYLOAD_H +#define MESSAGE_PAYLOAD_H + +#include + +#include +#include + +#include +#include + +#define NONCOPYABLE( Class ) \ + private: \ + Class( const Class & ); \ + Class & operator= ( const Class & ); + +//----------------------------------------------------------------------------- +// MessagePayload +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the contents of the network messages. It + * provides means for encode and decode (i.e. deal with endianness), and also + * can be treated like an ordinary array. + */ +class MessagePayload +{ +public: + explicit MessagePayload( const std::size_t &payload_size_in_bytes ); + ~MessagePayload(); + + const uint8_t& operator[]( std::size_t offset ) const; + uint8_t& operator[]( std::size_t offset ); + + uint16_t decode16( + const int left_byte, + const int right_byte + ) const; + void encode16( + const int left_byte, + const int right_byte, + const uint16_t value + ); + + std::istream& read( std::istream &is ); + std::ostream& write( std::ostream &os ) const; + +private: + /// the size of the payload buffer + const std::size_t PayloadSizeInBytes; + /// the payload buffer + boost::scoped_array Payload; + + NONCOPYABLE( MessagePayload ) +}; + +#endif // MESSAGE_PAYLOAD_H diff --git a/src/icmp/icmpdestinationunreachablemessage.h b/src/icmp/icmpdestinationunreachablemessage.h index ae8dbbf..f7754f1 100644 --- a/src/icmp/icmpdestinationunreachablemessage.h +++ b/src/icmp/icmpdestinationunreachablemessage.h @@ -13,7 +13,7 @@ #include #include "icmp/icmpmessage.h" -#include "icmp/icmpmessagepayload.h" +#include "host/messagepayload.h" //----------------------------------------------------------------------------- // IcmpDestinationUnreachableMessage @@ -68,7 +68,7 @@ public: private: /// packet payload object - IcmpMessagePayload Payload; + MessagePayload Payload; }; diff --git a/src/icmp/icmpechoreplymessage.h b/src/icmp/icmpechoreplymessage.h index 6f47f9e..b5f9525 100644 --- a/src/icmp/icmpechoreplymessage.h +++ b/src/icmp/icmpechoreplymessage.h @@ -13,7 +13,7 @@ #include #include "icmp/icmpmessage.h" -#include "icmp/icmpmessagepayload.h" +#include "host/messagepayload.h" //----------------------------------------------------------------------------- // IcmpEchoReplyMessage @@ -64,7 +64,7 @@ public: private: /// packet payload object - IcmpMessagePayload Payload; + MessagePayload Payload; }; diff --git a/src/icmp/icmpechorequestmessage.h b/src/icmp/icmpechorequestmessage.h index 6546087..e9ce57f 100644 --- a/src/icmp/icmpechorequestmessage.h +++ b/src/icmp/icmpechorequestmessage.h @@ -13,7 +13,7 @@ #include #include "icmp/icmpmessage.h" -#include "icmp/icmpmessagepayload.h" +#include "host/messagepayload.h" //----------------------------------------------------------------------------- // IcmpEchoRequestMessage @@ -64,7 +64,7 @@ public: private: /// packet payload object - IcmpMessagePayload Payload; + MessagePayload Payload; }; diff --git a/src/icmp/icmpmessagepayload.cpp b/src/icmp/icmpmessagepayload.cpp deleted file mode 100644 index 7b07335..0000000 --- a/src/icmp/icmpmessagepayload.cpp +++ /dev/null @@ -1,129 +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/icmpmessagepayload.h" - -#include - -#include - -#include - -using namespace std; -using boost::scoped_array; -using I2n::Logger::GlobalLogger; - -//----------------------------------------------------------------------------- -// IcmpMessagePayload -//----------------------------------------------------------------------------- - -/** - * @param payload_size_in_bytes the size of the payload (ie the internal buffer) - */ -IcmpMessagePayload::IcmpMessagePayload( - const std::size_t &payload_size_in_bytes -) : - PayloadSizeInBytes( payload_size_in_bytes ), - Payload( new uint8_t[ payload_size_in_bytes ] ) -{ - BOOST_ASSERT( 0 < payload_size_in_bytes ); - - fill( Payload.get(), Payload.get() + PayloadSizeInBytes, 0 ); -} - -IcmpMessagePayload::~IcmpMessagePayload() -{ - // Payload automatically delete by smart pointer scope end -} - -/** - * @brief The element access operator to provide access syntax like regular - * arrays. - */ -const uint8_t& IcmpMessagePayload::operator[]( size_t offset ) const -{ - BOOST_ASSERT( offset < PayloadSizeInBytes ); - - return Payload[ offset ]; -} - -/** - * @brief The element access operator to provide access syntax like regular - * arrays. - */ -uint8_t& IcmpMessagePayload::operator[]( size_t offset ) -{ - BOOST_ASSERT( offset < PayloadSizeInBytes ); - - return Payload[ offset ]; -} - -/** - * @brief Retrieve 16 bits from the payload buffer. - * - * @param left_byte the index of the left byte - * @param right_byte the index of the right byte - * - * @return a concatenation of the byte indexed by left_byte with the byte - * indexed by right_byte - */ -uint16_t IcmpMessagePayload::decode16( - const int left_byte, - const int right_byte -) const -{ - uint32_t value = ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; - - BOOST_ASSERT( value <= numeric_limits::max() ); - - return static_cast ( value ); -} - -/** - * @brief Store 16 bits in the payload buffer. - * - * @param left_byte the index of the left byte - * @param right_byte the index of the right byte - * - * @param value a 16 bits data be saved in the bytes indexed by left_byte and - * right_byte. - */ -void IcmpMessagePayload::encode16( - const int left_byte, - const int right_byte, - const uint16_t value -) -{ - Payload[ left_byte ] = static_cast ( value >> 8 ); - Payload[ right_byte ] = static_cast ( value & 0xFF ); -} - -/** - * @brief Read all the data from the payload and stores in the istream. - */ -istream& IcmpMessagePayload::read( istream &is ) -{ - char *payload_data_array = reinterpret_cast ( Payload.get() ); - (void) is.read( payload_data_array, PayloadSizeInBytes ); - - size_t data_received_in_bytes = static_cast( is.gcount() ); - if ( data_received_in_bytes != PayloadSizeInBytes ) - { - GlobalLogger.error() << "Error: expecting " << PayloadSizeInBytes - << " bytes, but received " << is.gcount() << " bytes" << endl; - } - - return is; -} - -/** - * @brief Writes all the data present in the ostream to the payload buffer. - */ -ostream& IcmpMessagePayload::write( ostream &os ) const -{ - const char *data_array = reinterpret_cast ( Payload.get() ); - return os.write( data_array, PayloadSizeInBytes ); -} diff --git a/src/icmp/icmpmessagepayload.h b/src/icmp/icmpmessagepayload.h deleted file mode 100644 index 5388092..0000000 --- a/src/icmp/icmpmessagepayload.h +++ /dev/null @@ -1,63 +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 ICMPMESSAGEPAYLOAD_H -#define ICMPMESSAGEPAYLOAD_H - -#include - -#include -#include - -#include -#include - -#define NONCOPYABLE( Class ) \ - private: \ - Class( const Class & ); \ - Class & operator= ( const Class & ); - -//----------------------------------------------------------------------------- -// IcmpMessagePayload -//----------------------------------------------------------------------------- - -/** - * @brief This class represents the contents of the network messages. It - * provides means for encode and decode, and also can be treated like an - * ordinary array. - */ -class IcmpMessagePayload -{ -public: - explicit IcmpMessagePayload( const std::size_t &payload_size_in_bytes ); - ~IcmpMessagePayload(); - - const uint8_t& operator[]( std::size_t offset ) const; - uint8_t& operator[]( std::size_t offset ); - - uint16_t decode16( - const int left_byte, - const int right_byte - ) const; - void encode16( - const int left_byte, - const int right_byte, - const uint16_t value - ); - - std::istream& read( std::istream &is ); - std::ostream& write( std::ostream &os ) const; - -private: - /// the size of the payload buffer - const std::size_t PayloadSizeInBytes; - /// the payload buffer - boost::scoped_array Payload; - - NONCOPYABLE( IcmpMessagePayload ) -}; - -#endif /* ICMPMESSAGEPAYLOAD_H */ -- 1.7.1