From 0a9cc41e8d3505667f175022c479f8aa486f011e Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Fri, 25 Feb 2011 11:32:02 +0100 Subject: [PATCH] Renamed the icmp_header files to icmpheader, removed the '_' to keep the project standard --- src/CMakeLists.txt | 2 +- src/ping/icmp_header.cpp | 101 ------------------------------------------ src/ping/icmp_header.h | 110 ---------------------------------------------- src/ping/icmpheader.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++ src/ping/icmpheader.h | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 214 insertions(+), 212 deletions(-) delete mode 100644 src/ping/icmp_header.cpp delete mode 100644 src/ping/icmp_header.h create mode 100644 src/ping/icmpheader.cpp create mode 100644 src/ping/icmpheader.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2a03d02..8cdd690 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,7 +11,7 @@ set( SOURCES config/configurationreader.cpp ping/boostpinger.cpp ping/host.cpp - ping/icmp_header.cpp + ping/icmpheader.cpp ping/ipv4_header.cpp ping/pingcheck.cpp ping/pinger.cpp diff --git a/src/ping/icmp_header.cpp b/src/ping/icmp_header.cpp deleted file mode 100644 index f4078c7..0000000 --- a/src/ping/icmp_header.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include "icmp_header.h" - -//----------------------------------------------------------------------------- -// IcmpHeader -//----------------------------------------------------------------------------- - -IcmpHeader::IcmpHeader() -{ - std::fill( Rep, Rep + sizeof(Rep), 0 ); -} - -IcmpHeader::IcmpHeader( - IcmpHeader::IcmpType type, - uint8_t code, - uint16_t identifier, - uint16_t sequence_number -) -{ - std::fill( Rep, Rep + sizeof(Rep), 0 ); - - set_type( type ); - set_code( code ); - set_identifier( identifier ); - set_sequence_number( sequence_number ); -} - -IcmpHeader::IcmpType IcmpHeader::get_type() const -{ - uint8_t n = Rep[ 0 ]; - IcmpHeader::IcmpType type = static_cast ( n ); - - return type; -} - -void IcmpHeader::set_type( IcmpHeader::IcmpType type ) -{ - uint8_t n = type; - - Rep[ 0 ] = n; -} - -uint8_t IcmpHeader::get_code() const -{ - return Rep[ 1 ]; -} - -void IcmpHeader::set_code( uint8_t code ) -{ - Rep[ 1 ] = code; -} - -uint16_t IcmpHeader::get_checksum() const -{ - return decode( 2, 3 ); -} - -void IcmpHeader::set_checksum( uint16_t checksum ) -{ - encode( 2, 3, checksum ); -} - -uint16_t IcmpHeader::get_identifier() const -{ - return decode( 4, 5 ); -} - -void IcmpHeader::set_identifier( uint16_t identifier ) -{ - encode( 4, 5, identifier ); -} - -uint16_t IcmpHeader::get_sequence_number() const -{ - return decode( 6, 7 ); -} - -void IcmpHeader::set_sequence_number( uint16_t sequence_number ) -{ - encode( 6, 7, sequence_number ); -} - -std::istream& operator>>( std::istream& is, IcmpHeader& header ) -{ - return is.read( reinterpret_cast ( header.Rep ), 8 ); -} - -std::ostream& operator<<( std::ostream& os, const IcmpHeader& header ) -{ - return os.write( reinterpret_cast ( header.Rep ), 8 ); -} - -uint16_t IcmpHeader::decode( int a, int b ) const -{ - return (Rep[ a ] << 8) + Rep[ b ]; -} - -void IcmpHeader::encode( int a, int b, uint16_t n ) -{ - 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 deleted file mode 100644 index 7a7984f..0000000 --- a/src/ping/icmp_header.h +++ /dev/null @@ -1,110 +0,0 @@ -#ifndef ICMP_HEADER_HPP -#define ICMP_HEADER_HPP - -#include -#include -#include -#include - -//----------------------------------------------------------------------------- -// IcmpHeader -//----------------------------------------------------------------------------- -// ICMP header format -// -// 0 8 16 31 -// +---------------+---------------+------------------------------+ --- -// | | | | ^ -// | type | code | checksum | | -// | | | | | -// +---------------+---------------+------------------------------+ 8 bytes -// | | | | -// | identifier | sequence number | | -// | | | v -// +-------------------------------+------------------------------+ --- -// -//----------------------------------------------------------------------------- - -class IcmpHeader -{ -public: - enum IcmpType - { - EchoReply = 0, - DestinationUnreachable = 3, - SourceQuench = 4, - Redirect = 5, - EchoRequest = 8, - TimeExceeded = 11, - ParameterProblem = 12, - TimestampRequest = 13, - TimestampReply = 14, - InfoRequest = 15, - InfoReply = 16, - AddressRequest = 17, - AddressReply = 18 - }; - - IcmpHeader(); - IcmpHeader( - IcmpType type, - uint8_t code, - 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 ); - - friend std::istream& operator>>( - std::istream& is, - IcmpHeader& header ); - friend std::ostream& operator<<( - std::ostream& os, - const IcmpHeader& header ); - -private: - uint16_t decode( int a, int b ) const; - void encode( int a, int b, uint16_t n ); - - uint8_t Rep[ 8 ]; -}; - -//----------------------------------------------------------------------------- -// compute_checksum -//----------------------------------------------------------------------------- - -template - void compute_checksum( - IcmpHeader& header, - Iterator body_begin, - Iterator body_end ) - { - unsigned int sum = (header.get_type() << 8) + header.get_code() - + header.get_identifier() + header.get_sequence_number(); - - Iterator body_iter = body_begin; - while ( body_iter != body_end ) - { - sum += (static_cast ( *body_iter++ ) << 8); - if (body_iter != body_end) - sum += static_cast ( *body_iter++ ); - } - - sum = (sum >> 16) + (sum & 0xFFFF); - sum += (sum >> 16); - header.set_checksum( static_cast ( ~sum ) ); - } - -#endif // ICMP_HEADER_H diff --git a/src/ping/icmpheader.cpp b/src/ping/icmpheader.cpp new file mode 100644 index 0000000..d1ce175 --- /dev/null +++ b/src/ping/icmpheader.cpp @@ -0,0 +1,103 @@ +#include "icmpheader.h" + +//----------------------------------------------------------------------------- +// IcmpHeader +//----------------------------------------------------------------------------- + +IcmpHeader::IcmpHeader() +{ + std::fill( Rep, Rep + sizeof(Rep), 0 ); +} + +IcmpHeader::IcmpHeader( + IcmpHeader::IcmpType type, + uint8_t code, + uint16_t checksum, + uint16_t identifier, + uint16_t sequence_number +) +{ + std::fill( Rep, Rep + sizeof(Rep), 0 ); + + set_type( type ); + set_code( code ); + set_checksum( checksum ); + set_identifier( identifier ); + set_sequence_number( sequence_number ); +} + +IcmpHeader::IcmpType IcmpHeader::get_type() const +{ + uint8_t n = Rep[ 0 ]; + IcmpHeader::IcmpType type = static_cast ( n ); + + return type; +} + +void IcmpHeader::set_type( IcmpHeader::IcmpType type ) +{ + uint8_t n = type; + + Rep[ 0 ] = n; +} + +uint8_t IcmpHeader::get_code() const +{ + return Rep[ 1 ]; +} + +void IcmpHeader::set_code( uint8_t code ) +{ + Rep[ 1 ] = code; +} + +uint16_t IcmpHeader::get_checksum() const +{ + return decode( 2, 3 ); +} + +void IcmpHeader::set_checksum( uint16_t checksum ) +{ + encode( 2, 3, checksum ); +} + +uint16_t IcmpHeader::get_identifier() const +{ + return decode( 4, 5 ); +} + +void IcmpHeader::set_identifier( uint16_t identifier ) +{ + encode( 4, 5, identifier ); +} + +uint16_t IcmpHeader::get_sequence_number() const +{ + return decode( 6, 7 ); +} + +void IcmpHeader::set_sequence_number( uint16_t sequence_number ) +{ + encode( 6, 7, sequence_number ); +} + +std::istream& operator>>( std::istream& is, IcmpHeader& header ) +{ + return is.read( reinterpret_cast ( header.Rep ), 8 ); +} + +std::ostream& operator<<( std::ostream& os, const IcmpHeader& header ) +{ + return os.write( reinterpret_cast ( header.Rep ), 8 ); +} + +uint16_t IcmpHeader::decode( int a, int b ) const +{ + return (Rep[ a ] << 8) + Rep[ b ]; +} + +void IcmpHeader::encode( int a, int b, uint16_t n ) +{ + Rep[ a ] = static_cast ( n >> 8 ); + Rep[ b ] = static_cast ( n & 0xFF ); +} diff --git a/src/ping/icmpheader.h b/src/ping/icmpheader.h new file mode 100644 index 0000000..7a7984f --- /dev/null +++ b/src/ping/icmpheader.h @@ -0,0 +1,110 @@ +#ifndef ICMP_HEADER_HPP +#define ICMP_HEADER_HPP + +#include +#include +#include +#include + +//----------------------------------------------------------------------------- +// IcmpHeader +//----------------------------------------------------------------------------- +// ICMP header format +// +// 0 8 16 31 +// +---------------+---------------+------------------------------+ --- +// | | | | ^ +// | type | code | checksum | | +// | | | | | +// +---------------+---------------+------------------------------+ 8 bytes +// | | | | +// | identifier | sequence number | | +// | | | v +// +-------------------------------+------------------------------+ --- +// +//----------------------------------------------------------------------------- + +class IcmpHeader +{ +public: + enum IcmpType + { + EchoReply = 0, + DestinationUnreachable = 3, + SourceQuench = 4, + Redirect = 5, + EchoRequest = 8, + TimeExceeded = 11, + ParameterProblem = 12, + TimestampRequest = 13, + TimestampReply = 14, + InfoRequest = 15, + InfoReply = 16, + AddressRequest = 17, + AddressReply = 18 + }; + + IcmpHeader(); + IcmpHeader( + IcmpType type, + uint8_t code, + 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 ); + + friend std::istream& operator>>( + std::istream& is, + IcmpHeader& header ); + friend std::ostream& operator<<( + std::ostream& os, + const IcmpHeader& header ); + +private: + uint16_t decode( int a, int b ) const; + void encode( int a, int b, uint16_t n ); + + uint8_t Rep[ 8 ]; +}; + +//----------------------------------------------------------------------------- +// compute_checksum +//----------------------------------------------------------------------------- + +template + void compute_checksum( + IcmpHeader& header, + Iterator body_begin, + Iterator body_end ) + { + unsigned int sum = (header.get_type() << 8) + header.get_code() + + header.get_identifier() + header.get_sequence_number(); + + Iterator body_iter = body_begin; + while ( body_iter != body_end ) + { + sum += (static_cast ( *body_iter++ ) << 8); + if (body_iter != body_end) + sum += static_cast ( *body_iter++ ); + } + + sum = (sum >> 16) + (sum & 0xFFFF); + sum += (sum >> 16); + header.set_checksum( static_cast ( ~sum ) ); + } + +#endif // ICMP_HEADER_H -- 1.7.1