From: Guilherme Maciel Ferreira Date: Thu, 24 Feb 2011 14:23:00 +0000 (+0100) Subject: The header classes operate on specific data sizes, thus using types with named sizes... X-Git-Tag: v1.0~182 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=fd6ef587624914dc30c0c9a5732f817dd8e0e459;p=pingcheck The header classes operate on specific data sizes, thus using types with named sizes on it states clear which size of the data is expected --- diff --git a/src/ping/icmp_header.cpp b/src/ping/icmp_header.cpp index 477a865..791ddb5 100644 --- a/src/ping/icmp_header.cpp +++ b/src/ping/icmp_header.cpp @@ -9,52 +9,52 @@ IcmpHeader::IcmpHeader() std::fill( Rep, Rep + sizeof(Rep), 0 ); } -unsigned char IcmpHeader::get_type() const +uint8_t IcmpHeader::get_type() const { return Rep[ 0 ]; } -void IcmpHeader::set_type( unsigned char n ) +void IcmpHeader::set_type( uint8_t n ) { Rep[ 0 ] = n; } -unsigned char IcmpHeader::get_code() const +uint8_t IcmpHeader::get_code() const { return Rep[ 1 ]; } -void IcmpHeader::set_code( unsigned char n ) +void IcmpHeader::set_code( uint8_t n ) { Rep[ 1 ] = n; } -unsigned short IcmpHeader::get_checksum() const +uint16_t IcmpHeader::get_checksum() const { return decode( 2, 3 ); } -void IcmpHeader::set_checksum( unsigned short n ) +void IcmpHeader::set_checksum( uint16_t n ) { encode( 2, 3, n ); } -unsigned short IcmpHeader::get_identifier() const +uint16_t IcmpHeader::get_identifier() const { return decode( 4, 5 ); } -void IcmpHeader::set_identifier( unsigned short n ) +void IcmpHeader::set_identifier( uint16_t n ) { encode( 4, 5, n ); } -unsigned short IcmpHeader::get_sequence_number() const +uint16_t IcmpHeader::get_sequence_number() const { return decode( 6, 7 ); } -void IcmpHeader::set_sequence_number( unsigned short n ) +void IcmpHeader::set_sequence_number( uint16_t n ) { encode( 6, 7, n ); } @@ -69,13 +69,13 @@ std::ostream& operator<<( std::ostream& os, const IcmpHeader& header ) return os.write( reinterpret_cast ( header.Rep ), 8 ); } -unsigned short IcmpHeader::decode( int a, int b ) const +uint16_t IcmpHeader::decode( int a, int b ) const { return (Rep[ a ] << 8) + Rep[ b ]; } -void IcmpHeader::encode( int a, int b, unsigned short n ) +void IcmpHeader::encode( int a, int b, uint16_t n ) { - Rep[ a ] = static_cast ( n >> 8 ); - Rep[ b ] = static_cast ( n & 0xFF ); + Rep[ a ] = static_cast ( n >> 8 ); + Rep[ b ] = static_cast ( n & 0xFF ); } diff --git a/src/ping/icmp_header.h b/src/ping/icmp_header.h index af2a44d..113e0bc 100644 --- a/src/ping/icmp_header.h +++ b/src/ping/icmp_header.h @@ -1,9 +1,10 @@ #ifndef ICMP_HEADER_HPP #define ICMP_HEADER_HPP +#include #include #include -#include +#include //----------------------------------------------------------------------------- // IcmpHeader @@ -45,20 +46,20 @@ public: IcmpHeader(); - unsigned char get_type() const; - void set_type( const unsigned char n ); + uint8_t get_type() const; + void set_type( const uint8_t n ); - unsigned char get_code() const; - void set_code( const unsigned char n ); + uint8_t get_code() const; + void set_code( const uint8_t n ); - unsigned short get_checksum() const; - void set_checksum( const unsigned short n ); + uint16_t get_checksum() const; + void set_checksum( const uint16_t n ); - unsigned short get_identifier() const; - void set_identifier( const unsigned short n ); + uint16_t get_identifier() const; + void set_identifier( const uint16_t n ); - unsigned short get_sequence_number() const; - void set_sequence_number( const unsigned short n ); + uint16_t get_sequence_number() const; + void set_sequence_number( const uint16_t n ); friend std::istream& operator>>( std::istream& is, @@ -68,10 +69,10 @@ public: const IcmpHeader& header ); private: - unsigned short decode( int a, int b ) const; - void encode( int a, int b, unsigned short n ); + uint16_t decode( int a, int b ) const; + void encode( int a, int b, uint16_t n ); - unsigned char Rep[ 8 ]; + uint8_t Rep[ 8 ]; }; //----------------------------------------------------------------------------- @@ -90,14 +91,14 @@ template Iterator body_iter = body_begin; while ( body_iter != body_end ) { - sum += (static_cast ( *body_iter++ ) << 8); + sum += (static_cast ( *body_iter++ ) << 8); if (body_iter != body_end) - sum += static_cast ( *body_iter++ ); + sum += static_cast ( *body_iter++ ); } sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); - header.set_checksum( static_cast ( ~sum ) ); + header.set_checksum( static_cast ( ~sum ) ); } #endif // ICMP_HEADER_H diff --git a/src/ping/ipv4_header.cpp b/src/ping/ipv4_header.cpp index 4532afc..682ea8c 100644 --- a/src/ping/ipv4_header.cpp +++ b/src/ping/ipv4_header.cpp @@ -9,27 +9,27 @@ Ipv4Header::Ipv4Header() std::fill( Rep, Rep + sizeof(Rep), 0 ); } -unsigned char Ipv4Header::get_version() const +uint8_t Ipv4Header::get_version() const { return (Rep[ 0 ] >> 4) & 0xF; } -unsigned short Ipv4Header::get_header_length() const +uint16_t Ipv4Header::get_header_length() const { return (Rep[ 0 ] & 0xF) * 4; } -unsigned char Ipv4Header::get_type_of_service() const +uint8_t Ipv4Header::get_type_of_service() const { return Rep[ 1 ]; } -unsigned short Ipv4Header::get_total_length() const +uint16_t Ipv4Header::get_total_length() const { return decode( 2, 3 ); } -unsigned short Ipv4Header::get_identification() const +uint16_t Ipv4Header::get_identification() const { return decode( 4, 5 ); } @@ -44,7 +44,7 @@ bool Ipv4Header::have_more_fragments() const return (Rep[ 6 ] & 0x20) != 0; } -unsigned short Ipv4Header::get_fragment_offset() const +uint16_t Ipv4Header::get_fragment_offset() const { return decode( 6, 7 ) & 0x1FFF; } @@ -54,12 +54,12 @@ unsigned int Ipv4Header::get_time_to_live() const return Rep[ 8 ]; } -unsigned char Ipv4Header::get_protocol() const +uint8_t Ipv4Header::get_protocol() const { return Rep[ 9 ]; } -unsigned short Ipv4Header::get_header_checksum() const +uint16_t Ipv4Header::get_header_checksum() const { return decode( 10, 11 ); } @@ -97,7 +97,7 @@ std::istream& operator>>( std::istream& is, Ipv4Header& header ) return is; } -unsigned short Ipv4Header::decode( int a, int b ) const +uint16_t Ipv4Header::decode( int a, int b ) const { return ((Rep[ a ] << 8) + Rep[ b ]); } diff --git a/src/ping/ipv4_header.h b/src/ping/ipv4_header.h index 11ca6a7..638a08a 100644 --- a/src/ping/ipv4_header.h +++ b/src/ping/ipv4_header.h @@ -3,6 +3,7 @@ #include #include +#include //----------------------------------------------------------------------------- // Ipv4Header @@ -46,18 +47,18 @@ class Ipv4Header public: Ipv4Header(); - unsigned char get_version() const; - unsigned short get_header_length() const; - unsigned char get_type_of_service() const; - unsigned short get_total_length() const; - unsigned short get_identification() const; + 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 have_more_fragments() const; - unsigned short get_fragment_offset() const; + uint16_t get_fragment_offset() const; unsigned int get_time_to_live() const; - unsigned char get_protocol() const; - unsigned short get_header_checksum() 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; @@ -65,9 +66,9 @@ public: friend std::istream& operator>>( std::istream& is, Ipv4Header& header ); private: - unsigned short decode( int a, int b ) const; + uint16_t decode( int a, int b ) const; - unsigned char Rep[ 60 ]; + uint8_t Rep[ 60 ]; }; #endif // IPV4_HEADER_H