TcpHeader class uses the MessagePayload class to handle network byte handling
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sun, 21 Aug 2011 01:15:52 +0000 (22:15 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sun, 21 Aug 2011 01:15:52 +0000 (22:15 -0300)
src/tcp/tcpheader.cpp
src/tcp/tcpheader.h

index ec8540f..4464efe 100644 (file)
@@ -33,98 +33,86 @@ using namespace std;
 // TcpHeader
 //-----------------------------------------------------------------------------
 
-TcpHeader::TcpHeader()
+static const int TcpHeaderSizeInBytes = 20;
+
+TcpHeader::TcpHeader() :
+    Payload( TcpHeaderSizeInBytes )
 {
-    fill( rep_, rep_ + sizeof(rep_), 0 );
 }
 
 uint16_t TcpHeader::get_source_port() const
 {
-    return decode( 0, 1 );
+    return Payload.decode16( 0, 1 );
 }
 
 uint16_t TcpHeader::get_destination_port() const
 {
-    return decode( 2, 3 );
+    return Payload.decode16( 2, 3 );
 }
 
 uint32_t TcpHeader::get_sequence_number() const
 {
-    uint32_t seq_num = (uint32_t) (
-            rep_[4] << 24 |
-            rep_[5] << 16 |
-            rep_[6] << 8 |
-            rep_[7]
-    );
-
-    return seq_num;
+    return Payload.decode32( 4, 7 );
 }
 
 uint32_t TcpHeader::get_acknowledgment_number() const
 {
-    uint32_t ack_num = (uint32_t) (
-            rep_[8] << 24 |
-            rep_[9] << 16 |
-            rep_[10] << 8 |
-            rep_[11]
-    );
-
-    return ack_num;
+    return Payload.decode32( 8, 11 );
 }
 
 uint8_t TcpHeader::get_header_length() const
 {
-    return (rep_[12] & 0xF0) >> 4;
+    return (Payload[12] & 0xF0) >> 4;
 }
 
 bool TcpHeader::get_congestion_window_reduced() const
 {
-    return (rep_[13] & 0x80);
+    return (Payload[13] & 0x80);
 }
 
 bool TcpHeader::get_ecn_echo() const
 {
-    return (rep_[13] & 0x40);
+    return (Payload[13] & 0x40);
 }
 
 bool TcpHeader::get_urgent() const
 {
-    return (rep_[13] & 0x20);
+    return (Payload[13] & 0x20);
 }
 
 bool TcpHeader::get_acknowledgment() const
 {
-    return (rep_[13] & 0x10);
+    return (Payload[13] & 0x10);
 }
 
 bool TcpHeader::get_push() const
 {
-    return (rep_[13] & 0x08);
+    return (Payload[13] & 0x08);
 }
 
 bool TcpHeader::get_reset() const
 {
-    return (rep_[13] & 0x04);
+    return (Payload[13] & 0x04);
 }
 
 bool TcpHeader::get_synchronize() const
 {
-    return (rep_[13] & 0x02);
+    return (Payload[13] & 0x02);
 }
 
 bool TcpHeader::get_finish() const
 {
-    return (rep_[13] & 0x01);
+    return (Payload[13] & 0x01);
 }
 
 uint16_t TcpHeader::get_window_size() const
 {
-    return decode( 14, 15 );
+    return Payload.decode16( 14, 15 );
 }
 
 uint16_t TcpHeader::get_checksum() const
 {
-    return decode( 16, 17 );
+    return Payload.decode16( 16, 17 );
 }
 
 uint16_t TcpHeader::calculate_tcp_checksum(
@@ -134,9 +122,8 @@ uint16_t TcpHeader::calculate_tcp_checksum(
         const uint16_t tcp_payload_size_in_bytes )
 {
     set_checksum( 0 );
-    uint16_t tcp_header_size_in_bytes = 20;
     uint16_t tcp_segment_size_in_bytes = static_cast< uint16_t > (
-            tcp_header_size_in_bytes + tcp_payload_size_in_bytes
+            TcpHeaderSizeInBytes + tcp_payload_size_in_bytes
     );
 
     PseudoIpv4Header pseudo_header;
@@ -152,14 +139,14 @@ uint16_t TcpHeader::calculate_tcp_checksum(
     memset( tcp_buffer, 0, sizeof(tcp_buffer) );
 
     memcpy( tcp_buffer, &pseudo_header, pseudo_header_size_in_bytes );
-    memcpy( tcp_buffer + pseudo_header_size_in_bytes, rep_,
-            tcp_header_size_in_bytes );
-    memcpy( tcp_buffer + pseudo_header_size_in_bytes + tcp_header_size_in_bytes,
+    memcpy( tcp_buffer + pseudo_header_size_in_bytes, Payload.get(),
+            TcpHeaderSizeInBytes );
+    memcpy( tcp_buffer + pseudo_header_size_in_bytes + TcpHeaderSizeInBytes,
             tcp_payload_data, tcp_payload_size_in_bytes );
 
     uint16_t cksum = calculate_checksum(
             (uint16_t *) tcp_buffer,
-            pseudo_header_size_in_bytes + tcp_header_size_in_bytes + tcp_payload_size_in_bytes
+            pseudo_header_size_in_bytes + TcpHeaderSizeInBytes + tcp_payload_size_in_bytes
     );
 
     cksum = ntohs( cksum );
@@ -169,106 +156,89 @@ uint16_t TcpHeader::calculate_tcp_checksum(
 
 void TcpHeader::set_source_port( const uint16_t port )
 {
-    encode( 0, 1, port );
+    Payload.encode16( 0, 1, port );
 }
 
 void TcpHeader::set_destination_port( const uint16_t port )
 {
-    encode( 2, 3, port );
+    Payload.encode16( 2, 3, port );
 }
 
 void TcpHeader::set_sequence_number( const uint32_t seq_num )
 {
-    rep_[4] = (uint8_t) (seq_num >> 24) & 0xFF;
-    rep_[5] = (uint8_t) (seq_num >> 16) & 0xFF;
-    rep_[6] = (uint8_t) (seq_num >> 8) & 0xFF;
-    rep_[7] = (uint8_t) seq_num & 0xFF;
+    Payload.encode32( 4, 7, seq_num );
 }
 
 void TcpHeader::set_acknowledgment_number( const uint32_t ack_num )
 {
-    rep_[8] = (uint8_t) (ack_num >> 24) & 0xFF;
-    rep_[9] = (uint8_t) (ack_num >> 16) & 0xFF;
-    rep_[10] = (uint8_t) (ack_num >> 8) & 0xFF;
-    rep_[11] = (uint8_t) ack_num & 0xFF;
+    Payload.encode32( 8, 11, ack_num );
 }
 
 void TcpHeader::set_header_length( const uint8_t offset )
 {
     uint8_t high_nimble = static_cast<uint8_t>( ( offset << 4 ) & 0xF0 );
-    uint8_t lower_nimble = ( rep_[12] & 0x0F );
-    rep_[12] = high_nimble | lower_nimble;
+    uint8_t lower_nimble = ( Payload[12] & 0x0F );
+    Payload[12] = high_nimble | lower_nimble;
 }
 
 void TcpHeader::set_congestion_window_reduced( bool bit )
 {
-    rep_[13] = rep_[13] | bit ? 0x80 : 0x0;
+    Payload[13] = Payload[13] | bit ? 0x80 : 0x0;
 }
 
 void TcpHeader::set_ecn_echo( bool bit )
 {
-    rep_[13] = rep_[13] | bit ? 0x40 : 0x0;
+    Payload[13] = Payload[13] | bit ? 0x40 : 0x0;
 }
 
 void TcpHeader::set_urgent( bool bit )
 {
-    rep_[13] = rep_[13] | bit ? 0x20 : 0x0;
+    Payload[13] = Payload[13] | bit ? 0x20 : 0x0;
 }
 
 void TcpHeader::set_acknowledgment( bool bit )
 {
-    rep_[13] = rep_[13] | bit ? 0x10 : 0x0;
+    Payload[13] = Payload[13] | bit ? 0x10 : 0x0;
 }
 
 void TcpHeader::set_push( bool bit )
 {
-    rep_[13] = rep_[13] | bit ? 0x08 : 0x0;
+    Payload[13] = Payload[13] | bit ? 0x08 : 0x0;
 }
 
 void TcpHeader::set_reset( bool bit )
 {
-    rep_[13] = rep_[13] | bit ? 0x04 : 0x0;
+    Payload[13] = Payload[13] | bit ? 0x04 : 0x0;
 }
 
 void TcpHeader::set_synchronize( bool bit )
 {
-    rep_[13] = rep_[13] | bit ? 0x02 : 0x0;
+    Payload[13] = Payload[13] | bit ? 0x02 : 0x0;
 }
 
 void TcpHeader::set_finish( bool bit )
 {
-    rep_[13] = rep_[13] | bit ? 0x01 : 0x0;
+    Payload[13] = Payload[13] | bit ? 0x01 : 0x0;
 }
 
 void TcpHeader::set_window_size( const uint16_t wnd_size )
 {
-    encode( 14, 15, wnd_size );
+    Payload.encode16( 14, 15, wnd_size );
 }
 
 void TcpHeader::set_checksum( const uint16_t sum )
 {
-    encode( 16, 17, sum );
+    Payload.encode16( 16, 17, sum );
 }
 
 istream& operator>>( istream& is, TcpHeader& header )
 {
-    return is.read( reinterpret_cast< char* > ( header.rep_ ), 20 );
+    return header.Payload.read( is );
 }
 
 ostream& operator<<( ostream& os, const TcpHeader& header )
 {
-    return os.write( reinterpret_cast< const char* > ( header.rep_ ), 20 );
-}
-
-uint16_t TcpHeader::decode( int a, int b ) const
-{
-    return static_cast<uint16_t>( (rep_[a] << 8) + rep_[b] );
-}
-
-void TcpHeader::encode( int a, int b, const uint16_t n )
-{
-    rep_[a] = static_cast< uint8_t > ( n >> 8 );
-    rep_[b] = static_cast< uint8_t > ( n & 0xFF );
+    return header.Payload.write( os );
 }
 
 uint16_t TcpHeader::calculate_checksum( const uint16_t *word_array, int size )
index 6fe562d..2c2fa52 100644 (file)
@@ -25,6 +25,8 @@ on this file might be covered by the GNU General Public License.
 #include <istream>
 #include <ostream>
 
+#include "host/messagepayload.h"
+
 //-----------------------------------------------------------------------------
 // TcpHeader
 //-----------------------------------------------------------------------------
@@ -119,11 +121,9 @@ public:
     );
 
 private:
-    uint16_t decode( int a, int b ) const;
-    void encode( int a, int b, const uint16_t n );
     uint16_t calculate_checksum( const uint16_t *word_array, int size );
 
-    uint8_t rep_[60];
+    MessagePayload Payload;
 };
 
 #endif // TCP_HEADER_H