From: Guilherme Maciel Ferreira Date: Thu, 24 Feb 2011 15:13:33 +0000 (+0100) Subject: Added a constructor with arguments and reinforce the "ICMP type" variable's type X-Git-Tag: v1.0~181 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=d78ef4831ac25f893dde4a96d5135bc57b4a5e78;p=pingcheck Added a constructor with arguments and reinforce the "ICMP type" variable's type --- diff --git a/src/ping/boostpinger.cpp b/src/ping/boostpinger.cpp index 73a2df6..605b43f 100644 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@ -50,12 +50,13 @@ void BoostPinger::start_send() std::string body( "ping message" ); // Create an ICMP header for an echo request. - IcmpHeader echo_request; - echo_request.set_type( IcmpHeader::EchoRequest ); - echo_request.set_code( 0 ); - echo_request.set_identifier( get_identifier() ); SequenceNumber++; - echo_request.set_sequence_number( SequenceNumber ); + IcmpHeader echo_request( + IcmpHeader::EchoRequest, + 0, + get_identifier(), + SequenceNumber + ); compute_checksum( echo_request, body.begin(), body.end() ); // Encode the request packet. diff --git a/src/ping/icmp_header.cpp b/src/ping/icmp_header.cpp index 791ddb5..f4078c7 100644 --- a/src/ping/icmp_header.cpp +++ b/src/ping/icmp_header.cpp @@ -9,13 +9,33 @@ IcmpHeader::IcmpHeader() std::fill( Rep, Rep + sizeof(Rep), 0 ); } -uint8_t IcmpHeader::get_type() const +IcmpHeader::IcmpHeader( + IcmpHeader::IcmpType type, + uint8_t code, + uint16_t identifier, + uint16_t sequence_number +) { - return Rep[ 0 ]; + std::fill( Rep, Rep + sizeof(Rep), 0 ); + + set_type( type ); + set_code( code ); + set_identifier( identifier ); + set_sequence_number( sequence_number ); } -void IcmpHeader::set_type( uint8_t n ) +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; } @@ -24,9 +44,9 @@ uint8_t IcmpHeader::get_code() const return Rep[ 1 ]; } -void IcmpHeader::set_code( uint8_t n ) +void IcmpHeader::set_code( uint8_t code ) { - Rep[ 1 ] = n; + Rep[ 1 ] = code; } uint16_t IcmpHeader::get_checksum() const @@ -34,9 +54,9 @@ uint16_t IcmpHeader::get_checksum() const return decode( 2, 3 ); } -void IcmpHeader::set_checksum( uint16_t n ) +void IcmpHeader::set_checksum( uint16_t checksum ) { - encode( 2, 3, n ); + encode( 2, 3, checksum ); } uint16_t IcmpHeader::get_identifier() const @@ -44,9 +64,9 @@ uint16_t IcmpHeader::get_identifier() const return decode( 4, 5 ); } -void IcmpHeader::set_identifier( uint16_t n ) +void IcmpHeader::set_identifier( uint16_t identifier ) { - encode( 4, 5, n ); + encode( 4, 5, identifier ); } uint16_t IcmpHeader::get_sequence_number() const @@ -54,9 +74,9 @@ uint16_t IcmpHeader::get_sequence_number() const return decode( 6, 7 ); } -void IcmpHeader::set_sequence_number( uint16_t n ) +void IcmpHeader::set_sequence_number( uint16_t sequence_number ) { - encode( 6, 7, n ); + encode( 6, 7, sequence_number ); } std::istream& operator>>( std::istream& is, IcmpHeader& header ) diff --git a/src/ping/icmp_header.h b/src/ping/icmp_header.h index 113e0bc..7a7984f 100644 --- a/src/ping/icmp_header.h +++ b/src/ping/icmp_header.h @@ -45,21 +45,27 @@ public: }; IcmpHeader(); + IcmpHeader( + IcmpType type, + uint8_t code, + uint16_t identifier, + uint16_t sequence_number + ); - uint8_t get_type() const; - void set_type( const uint8_t n ); + IcmpType get_type() const; + void set_type( const IcmpType type ); uint8_t get_code() const; - void set_code( const uint8_t n ); + void set_code( const uint8_t code ); uint16_t get_checksum() const; - void set_checksum( const uint16_t n ); + void set_checksum( const uint16_t checksum ); uint16_t get_identifier() const; - void set_identifier( const uint16_t n ); + void set_identifier( const uint16_t identifier ); uint16_t get_sequence_number() const; - void set_sequence_number( const uint16_t n ); + void set_sequence_number( const uint16_t sequence_number ); friend std::istream& operator>>( std::istream& is,