fill( rep_, rep_ + sizeof(rep_), 0 );
}
-uint16_t TcpHeader::source_port() const
+uint16_t TcpHeader::get_source_port() const
{
return decode( 0, 1 );
}
-uint16_t TcpHeader::destination_port() const
+uint16_t TcpHeader::get_destination_port() const
{
return decode( 2, 3 );
}
-uint32_t TcpHeader::sequence_number() const
+uint32_t TcpHeader::get_sequence_number() const
{
uint32_t seq_num = (uint32_t) (
rep_[4] << 24 |
return seq_num;
}
-uint32_t TcpHeader::acknowledgment_number() const
+uint32_t TcpHeader::get_acknowledgment_number() const
{
uint32_t ack_num = (uint32_t) (
rep_[8] << 24 |
return ack_num;
}
-uint8_t TcpHeader::header_length() const
+uint8_t TcpHeader::get_header_length() const
{
return (rep_[12] & 0xF0) >> 4;
}
-bool TcpHeader::congestion_window_reduced() const
+bool TcpHeader::get_congestion_window_reduced() const
{
return (rep_[13] & 0x80);
}
-bool TcpHeader::ecn_echo() const
+bool TcpHeader::get_ecn_echo() const
{
return (rep_[13] & 0x40);
}
-bool TcpHeader::urgent() const
+bool TcpHeader::get_urgent() const
{
return (rep_[13] & 0x20);
}
-bool TcpHeader::acknowledgment() const
+bool TcpHeader::get_acknowledgment() const
{
return (rep_[13] & 0x10);
}
-bool TcpHeader::push() const
+bool TcpHeader::get_push() const
{
return (rep_[13] & 0x08);
}
-bool TcpHeader::reset() const
+bool TcpHeader::get_reset() const
{
return (rep_[13] & 0x04);
}
-bool TcpHeader::synchronize() const
+bool TcpHeader::get_synchronize() const
{
return (rep_[13] & 0x02);
}
-bool TcpHeader::finish() const
+bool TcpHeader::get_finish() const
{
return (rep_[13] & 0x01);
}
-uint16_t TcpHeader::window_size() const
+uint16_t TcpHeader::get_window_size() const
{
return decode( 14, 15 );
}
-uint16_t TcpHeader::checksum() const
+uint16_t TcpHeader::get_checksum() const
{
return decode( 16, 17 );
}
const char* tcp_payload_data,
const uint16_t tcp_payload_size_in_bytes )
{
- checksum( 0 );
+ 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
return cksum;
}
-void TcpHeader::source_port( const uint16_t port )
+void TcpHeader::set_source_port( const uint16_t port )
{
encode( 0, 1, port );
}
-void TcpHeader::destination_port( const uint16_t port )
+void TcpHeader::set_destination_port( const uint16_t port )
{
encode( 2, 3, port );
}
-void TcpHeader::sequence_number( const uint32_t seq_num )
+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_[7] = (uint8_t) seq_num & 0xFF;
}
-void TcpHeader::acknowledgment_number( const uint32_t ack_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_[11] = (uint8_t) ack_num & 0xFF;
}
-void TcpHeader::header_length( const uint8_t offset )
+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;
}
-void TcpHeader::congestion_window_reduced( bool bit )
+void TcpHeader::set_congestion_window_reduced( bool bit )
{
rep_[13] = rep_[13] | bit ? 0x80 : 0x0;
}
-void TcpHeader::ecn_echo( bool bit )
+void TcpHeader::set_ecn_echo( bool bit )
{
rep_[13] = rep_[13] | bit ? 0x40 : 0x0;
}
-void TcpHeader::urgent( bool bit )
+void TcpHeader::set_urgent( bool bit )
{
rep_[13] = rep_[13] | bit ? 0x20 : 0x0;
}
-void TcpHeader::acknowledgment( bool bit )
+void TcpHeader::set_acknowledgment( bool bit )
{
rep_[13] = rep_[13] | bit ? 0x10 : 0x0;
}
-void TcpHeader::push( bool bit )
+void TcpHeader::set_push( bool bit )
{
rep_[13] = rep_[13] | bit ? 0x08 : 0x0;
}
-void TcpHeader::reset( bool bit )
+void TcpHeader::set_reset( bool bit )
{
rep_[13] = rep_[13] | bit ? 0x04 : 0x0;
}
-void TcpHeader::synchronize( bool bit )
+void TcpHeader::set_synchronize( bool bit )
{
rep_[13] = rep_[13] | bit ? 0x02 : 0x0;
}
-void TcpHeader::finish( bool bit )
+void TcpHeader::set_finish( bool bit )
{
rep_[13] = rep_[13] | bit ? 0x01 : 0x0;
}
-void TcpHeader::window_size( const uint16_t wnd_size )
+void TcpHeader::set_window_size( const uint16_t wnd_size )
{
encode( 14, 15, wnd_size );
}
-void TcpHeader::checksum( const uint16_t sum )
+void TcpHeader::set_checksum( const uint16_t sum )
{
encode( 16, 17, sum );
}
public:
TcpHeader();
- uint16_t source_port() const;
- uint16_t destination_port() const;
- uint32_t sequence_number() const;
- uint32_t acknowledgment_number() const;
- uint8_t header_length() const;
- bool congestion_window_reduced() const;
- bool ecn_echo() const;
- bool urgent() const;
- bool acknowledgment() const;
- bool push() const;
- bool reset() const;
- bool synchronize() const;
- bool finish() const;
- uint16_t window_size() const;
- uint16_t checksum() const;
+ uint16_t get_source_port() const;
+ uint16_t get_destination_port() const;
+ uint32_t get_sequence_number() const;
+ uint32_t get_acknowledgment_number() const;
+ uint8_t get_header_length() const;
+ bool get_congestion_window_reduced() const;
+ bool get_ecn_echo() const;
+ bool get_urgent() const;
+ bool get_acknowledgment() const;
+ bool get_push() const;
+ bool get_reset() const;
+ bool get_synchronize() const;
+ bool get_finish() const;
+ uint16_t get_window_size() const;
+ uint16_t get_checksum() const;
uint16_t calculate_tcp_checksum(
const uint32_t src_addr,
const uint16_t tcp_payload_size_in_bytes
);
- void source_port( const uint16_t port );
- void destination_port( const uint16_t port );
- void sequence_number( const uint32_t seq_num );
- void acknowledgment_number( const uint32_t ack_num );
- void header_length( const uint8_t offset );
- void congestion_window_reduced( bool bit );
- void ecn_echo( bool bit );
- void urgent( bool bit );
- void acknowledgment( bool bit );
- void push( bool bit );
- void reset( bool bit );
- void synchronize( bool bit );
- void finish( bool bit );
- void window_size( const uint16_t wnd_size );
- void checksum( const uint16_t sum );
+ void set_source_port( const uint16_t port );
+ void set_destination_port( const uint16_t port );
+ void set_sequence_number( const uint32_t seq_num );
+ void set_acknowledgment_number( const uint32_t ack_num );
+ void set_header_length( const uint8_t offset );
+ void set_congestion_window_reduced( bool bit );
+ void set_ecn_echo( bool bit );
+ void set_urgent( bool bit );
+ void set_acknowledgment( bool bit );
+ void set_push( bool bit );
+ void set_reset( bool bit );
+ void set_synchronize( bool bit );
+ void set_finish( bool bit );
+ void set_window_size( const uint16_t wnd_size );
+ void set_checksum( const uint16_t sum );
- friend std::istream& operator>>( std::istream& is, TcpHeader& header );
- friend std::ostream& operator<<( std::ostream& os, const TcpHeader& header );
+ friend std::istream& operator>>(
+ std::istream &is,
+ TcpHeader &header
+ );
+ friend std::ostream& operator<<(
+ std::ostream &os,
+ const TcpHeader &header
+ );
private:
uint16_t decode( int a, int b ) const;
uint8_t rep_[60];
};
-#endif /* TCP_HEADER_H */
+#endif // TCP_HEADER_H
uint16_t cksum = tcp_header.calculate_tcp_checksum(
source_address, destination_address, NULL, 0
);
- tcp_header.checksum( cksum );
+ tcp_header.set_checksum( cksum );
send_ack_request( tcp_header );
}
// Create an TCP header for an ACK request.
TcpHeader tcp_header;
- tcp_header.source_port( source_port ); // assign an random ephemeral port number
- tcp_header.destination_port( destination_port );
- tcp_header.sequence_number( sequence_number );
- tcp_header.header_length( header_size_in_words );
- tcp_header.acknowledgment( true );
- tcp_header.window_size( window_size_in_octets ); // window size
+ tcp_header.set_source_port( source_port ); // assign a random ephemeral port number
+ tcp_header.set_destination_port( destination_port );
+ tcp_header.set_sequence_number( sequence_number );
+ tcp_header.set_header_length( header_size_in_words );
+ tcp_header.set_acknowledgment( true );
+ tcp_header.set_window_size( window_size_in_octets ); // window size
return tcp_header;
}
// filter out only the TCP reset (RST) replies. Note that the sequence
// number from RST does not match the sent ACK's sequence number.
- if ( is &&
- tcp_header.reset() &&
+ if ( tcp_header.get_reset() &&
ipv4_header.get_source_address() == DestinationEndpoint.address() )
{
ReceivedReply = true;
{
ptime now = microsec_clock::universal_time();
GlobalLogger.info() << "RST from " << ipv4_header.get_source_address()
- << ": tcp_seq=" << tcp_header.sequence_number()
+ << ": tcp_seq=" << tcp_header.get_sequence_number()
<< ", ttl=" << ipv4_header.get_time_to_live()
<< " time=" << (now - TimeSent).total_milliseconds() << " ms" << endl;
}