Added a constructor with arguments and reinforce the "ICMP type" variable's type
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Thu, 24 Feb 2011 15:13:33 +0000 (16:13 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Thu, 24 Feb 2011 15:13:33 +0000 (16:13 +0100)
src/ping/boostpinger.cpp
src/ping/icmp_header.cpp
src/ping/icmp_header.h

index 73a2df6..605b43f 100644 (file)
@@ -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.
index 791ddb5..f4078c7 100644 (file)
@@ -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<IcmpHeader::IcmpType> ( 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 )
index 113e0bc..7a7984f 100644 (file)
@@ -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,