From db21a81aca8e534fdc77f202a375c62a91da59dd Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Thu, 7 Apr 2011 15:50:35 +0200 Subject: [PATCH] Given more meaningful names to the variables --- src/icmp/icmpheader.cpp | 26 +++++++++++----------- src/icmp/icmpheader.h | 6 ++-- src/icmp/ipv4header.cpp | 53 ++++++++++++++++++++++++++++------------------ src/icmp/ipv4header.h | 5 ++- 4 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/icmp/icmpheader.cpp b/src/icmp/icmpheader.cpp index 7d7e9e6..54d4124 100644 --- a/src/icmp/icmpheader.cpp +++ b/src/icmp/icmpheader.cpp @@ -6,7 +6,7 @@ IcmpHeader::IcmpHeader() { - std::fill( Rep, Rep + sizeof(Rep), 0 ); + std::fill( Payload, Payload + sizeof(Payload), 0 ); } IcmpHeader::IcmpHeader( @@ -17,7 +17,7 @@ IcmpHeader::IcmpHeader( uint16_t sequence_number ) { - std::fill( Rep, Rep + sizeof(Rep), 0 ); + std::fill( Payload, Payload + sizeof(Payload), 0 ); set_type( type ); set_code( code ); @@ -28,7 +28,7 @@ IcmpHeader::IcmpHeader( IcmpHeader::IcmpType IcmpHeader::get_type() const { - uint8_t n = Rep[ 0 ]; + uint8_t n = Payload[ 0 ]; IcmpHeader::IcmpType type = static_cast ( n ); return type; @@ -38,17 +38,17 @@ void IcmpHeader::set_type( IcmpHeader::IcmpType type ) { uint8_t n = type; - Rep[ 0 ] = n; + Payload[ 0 ] = n; } uint8_t IcmpHeader::get_code() const { - return Rep[ 1 ]; + return Payload[ 1 ]; } void IcmpHeader::set_code( uint8_t code ) { - Rep[ 1 ] = code; + Payload[ 1 ] = code; } uint16_t IcmpHeader::get_checksum() const @@ -86,7 +86,7 @@ std::istream& operator>>( IcmpHeader &header ) { - return is.read( reinterpret_cast ( header.Rep ), 8 ); + return is.read( reinterpret_cast ( header.Payload ), 8 ); } std::ostream& operator<<( @@ -94,16 +94,16 @@ std::ostream& operator<<( const IcmpHeader &header ) { - return os.write( reinterpret_cast ( header.Rep ), 8 ); + return os.write( reinterpret_cast ( header.Payload ), 8 ); } -uint16_t IcmpHeader::decode( int a, int b ) const +uint16_t IcmpHeader::decode( int left_byte, int right_byte ) const { - return ( Rep[ a ] << 8 ) + Rep[ b ]; + return ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; } -void IcmpHeader::encode( int a, int b, uint16_t n ) +void IcmpHeader::encode( int left_byte, int right_byte, uint16_t value ) { - Rep[ a ] = static_cast ( n >> 8 ); - Rep[ b ] = static_cast ( n & 0xFF ); + Payload[ left_byte ] = static_cast ( value >> 8 ); + Payload[ right_byte ] = static_cast ( value & 0xFF ); } diff --git a/src/icmp/icmpheader.h b/src/icmp/icmpheader.h index 85ebc67..1a477d1 100644 --- a/src/icmp/icmpheader.h +++ b/src/icmp/icmpheader.h @@ -79,11 +79,11 @@ public: ); private: - uint16_t decode( int a, int b ) const; - void encode( int a, int b, uint16_t n ); + uint16_t decode( int left_byte, int right_byte ) const; + void encode( int left_byte, int right_byte, uint16_t value ); private: - uint8_t Rep[ 8 ]; + uint8_t Payload[ 8 ]; }; diff --git a/src/icmp/ipv4header.cpp b/src/icmp/ipv4header.cpp index 1a3a0ec..122edaf 100644 --- a/src/icmp/ipv4header.cpp +++ b/src/icmp/ipv4header.cpp @@ -7,24 +7,26 @@ using namespace boost::asio::ip; // Ipv4Header //----------------------------------------------------------------------------- +const int Ipv4Header::HeaderSizeInBytes = 20; + Ipv4Header::Ipv4Header() { - fill( Rep, Rep + sizeof(Rep), 0 ); + fill( Payload, Payload + sizeof(Payload), 0 ); } uint8_t Ipv4Header::get_version() const { - return ( Rep[ 0 ] >> 4 ) & 0xF; + return ( Payload[ 0 ] >> 4 ) & 0xF; } uint16_t Ipv4Header::get_header_length() const { - return ( Rep[ 0 ] & 0xF ) * 4; + return ( Payload[ 0 ] & 0xF ) * 4; } uint8_t Ipv4Header::get_type_of_service() const { - return Rep[ 1 ]; + return Payload[ 1 ]; } uint16_t Ipv4Header::get_total_length() const @@ -39,12 +41,12 @@ uint16_t Ipv4Header::get_identification() const bool Ipv4Header::dont_fragment() const { - return ( ( Rep[ 6 ] & 0x40 ) != 0 ); + return ( ( Payload[ 6 ] & 0x40 ) != 0 ); } bool Ipv4Header::more_fragments() const { - return ( ( Rep[ 6 ] & 0x20 ) != 0 ); + return ( ( Payload[ 6 ] & 0x20 ) != 0 ); } uint16_t Ipv4Header::get_fragment_offset() const @@ -54,12 +56,12 @@ uint16_t Ipv4Header::get_fragment_offset() const uint8_t Ipv4Header::get_time_to_live() const { - return Rep[ 8 ]; + return Payload[ 8 ]; } uint8_t Ipv4Header::get_protocol() const { - return Rep[ 9 ]; + return Payload[ 9 ]; } uint16_t Ipv4Header::get_header_checksum() const @@ -70,20 +72,22 @@ uint16_t Ipv4Header::get_header_checksum() const address_v4 Ipv4Header::get_source_address() const { address_v4::bytes_type bytes = { { - Rep[ 12 ], - Rep[ 13 ], - Rep[ 14 ], - Rep[ 15 ] } }; + Payload[ 12 ], + Payload[ 13 ], + Payload[ 14 ], + Payload[ 15 ] + } }; return address_v4( bytes ); } address_v4 Ipv4Header::get_destination_address() const { address_v4::bytes_type bytes = { { - Rep[ 16 ], - Rep[ 17 ], - Rep[ 18 ], - Rep[ 19 ] } }; + Payload[ 16 ], + Payload[ 17 ], + Payload[ 18 ], + Payload[ 19 ] + } }; return address_v4( bytes ); } @@ -92,10 +96,16 @@ istream &operator>>( Ipv4Header &header ) { - (void) is.read( reinterpret_cast ( header.Rep ), 20 ); + (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() - 20; + streamsize options_length = + (streamsize) header.get_header_length() - + Ipv4Header::HeaderSizeInBytes; if ( options_length < 0 || 40 < options_length ) { is.setstate( ios::failbit ); @@ -103,7 +113,8 @@ istream &operator>>( else { (void) is.read( - reinterpret_cast ( header.Rep ) + 20, + reinterpret_cast ( header.Payload ) + + Ipv4Header::HeaderSizeInBytes, options_length ); } @@ -111,7 +122,7 @@ istream &operator>>( return is; } -uint16_t Ipv4Header::decode( int a, int b ) const +uint16_t Ipv4Header::decode( int left_byte, int right_byte ) const { - return ((Rep[ a ] << 8) + Rep[ b ]); + return ( (Payload[ left_byte ] << 8) + Payload[ right_byte ] ); } diff --git a/src/icmp/ipv4header.h b/src/icmp/ipv4header.h index 828442f..174c265 100644 --- a/src/icmp/ipv4header.h +++ b/src/icmp/ipv4header.h @@ -70,10 +70,11 @@ public: ); private: - uint16_t decode( int a, int b ) const; + uint16_t decode( int left_byte, int right_byte ) const; private: - uint8_t Rep[ 60 ]; + const static int HeaderSizeInBytes; + uint8_t Payload[ 60 ]; }; -- 1.7.1