Class: PacketGen::Header::TCP

Inherits:
Base show all
Defined in:
lib/packetgen/header/tcp.rb,
lib/packetgen/header/tcp.rb,
lib/packetgen/header/tcp/option.rb,
lib/packetgen/header/tcp/options.rb

Overview

TCP header (RFC 793)

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data |           |U|A|P|R|S|F|                               |
| Offset| Reserved  |R|C|S|S|Y|I|            Window             |
|       |           |G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           Checksum            |         Urgent Pointer        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             data                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

A TCP header consists of:

Create a TCP header

# standalone
tcph = PacketGen::Header::TCP.new
# in a IP packet
pkt = PacketGen.gen('IP').add('TCP')
# access to TCP header
pkt.tcp   # => PacketGen::Header::TCP

TCP attributes

tcph.sport = 4500
tcph.dport = 80
tcph.seqnum = 43
tcph.acknum = 0x45678925
tcph.wsize = 0x240
tcph.urg_pointer = 0x40
tcph.body.read 'this is a body'

Flags

TCP flags may be accesed as a 9-bit integer:

tcph.flags = 0x1002

Each flag may be accessed independently:

tcph.flag_syn?    # => Boolean
tcph.flag_rst = true

Options

#options TCP attribute is a Options. Option may added to it:

tcph.options << PacketGen::Header::TCP::MSS.new(1250)

or:

tcph.options << { opt: 'MSS', self[attr]: 1250 }

Author:

  • Sylvain Daubert

Defined Under Namespace

Classes: ECHO, ECHOREPLY, EOL, MSS, NOP, Option, Options, SACK, SACKOK, TS, WS

Constant Summary collapse

IP_PROTOCOL =

IP protocol number for TCP

6

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

bind, calculate_and_set_length, #header_id, inherited, #ip_header, #ll_header

Methods included from PacketGen::Headerable

#added_to_packet, included, #method_name, #packet, #packet=, #parse?, #protocol_name, #read

Methods inherited from Types::Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #offset_of, #optional?, #optional_fields, #present?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field

Constructor Details

#initialize(options = {}) ⇒ TCP

Call {Base#initialize), then handle specific options to set u16 by part:

  • :data_offset

  • :hlen

  • :reserved

  • :flags

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :sport (Integer)
  • :dport (Integer)
  • :seqnum (Integer)
  • :acknum (Integer)
  • :data_offset (Integer)
  • :reserved (Integer)
  • :flags (Integer)
  • :window (Integer)
  • :checksum (Integer)
  • :urg_pointer (Integer)
  • :body (String)

157
158
159
160
# File 'lib/packetgen/header/tcp.rb', line 157

def initialize(options={})
  opts = { data_offset: 5 }.merge!(options)
  super(opts)
end

Instance Attribute Details

#acknumInteger Also known as: acknowledgement_number

32-bit TCP acknowledgement number

Returns:

  • (Integer)

105
# File 'lib/packetgen/header/tcp.rb', line 105

define_field :acknum, Types::Int32

#bodyTypes::String, Header::Base


127
# File 'lib/packetgen/header/tcp.rb', line 127

define_field :body, Types::String

#checksumInteger

16-bit TCP checksum

Returns:

  • (Integer)

116
# File 'lib/packetgen/header/tcp.rb', line 116

define_field :checksum, Types::Int16

#data_offsetInteger Also known as: hlen

Returns 4-bit data offset from #u16.

Returns:

  • (Integer)

    4-bit data offset from #u16


168
# File 'lib/packetgen/header/tcp.rb', line 168

define_bit_fields_on :u16, :data_offset, 4, :reserved, 3, :flags, 9

#dportInteger Also known as: destination_port

16-bit TCP destination port

Returns:

  • (Integer)

97
# File 'lib/packetgen/header/tcp.rb', line 97

define_field :dport, Types::Int16

#flag_ackBoolean

Returns 1-bit ACK flag.

Returns:

  • (Boolean)

    1-bit ACK flag


190
191
# File 'lib/packetgen/header/tcp.rb', line 190

define_bit_fields_on :u16, :_, 7, :flag_ns, :flag_cwr, :flag_ece, :flag_urg,
:flag_ack, :flag_psh, :flag_rst, :flag_syn, :flag_fin

#flag_cwrBoolean

Returns 1-bit CWR flag.

Returns:

  • (Boolean)

    1-bit CWR flag


190
191
# File 'lib/packetgen/header/tcp.rb', line 190

define_bit_fields_on :u16, :_, 7, :flag_ns, :flag_cwr, :flag_ece, :flag_urg,
:flag_ack, :flag_psh, :flag_rst, :flag_syn, :flag_fin

#flag_eceBoolean

Returns 1-bit ECE flag.

Returns:

  • (Boolean)

    1-bit ECE flag


190
191
# File 'lib/packetgen/header/tcp.rb', line 190

define_bit_fields_on :u16, :_, 7, :flag_ns, :flag_cwr, :flag_ece, :flag_urg,
:flag_ack, :flag_psh, :flag_rst, :flag_syn, :flag_fin

#flag_finBoolean

Returns 1-bit FIN flag.

Returns:

  • (Boolean)

    1-bit FIN flag


190
191
# File 'lib/packetgen/header/tcp.rb', line 190

define_bit_fields_on :u16, :_, 7, :flag_ns, :flag_cwr, :flag_ece, :flag_urg,
:flag_ack, :flag_psh, :flag_rst, :flag_syn, :flag_fin

#flag_nsBoolean

Returns 1-bit NS flag.

Returns:

  • (Boolean)

    1-bit NS flag


190
191
# File 'lib/packetgen/header/tcp.rb', line 190

define_bit_fields_on :u16, :_, 7, :flag_ns, :flag_cwr, :flag_ece, :flag_urg,
:flag_ack, :flag_psh, :flag_rst, :flag_syn, :flag_fin

#flag_pshBoolean

Returns 1-bit PSH flag.

Returns:

  • (Boolean)

    1-bit PSH flag


190
191
# File 'lib/packetgen/header/tcp.rb', line 190

define_bit_fields_on :u16, :_, 7, :flag_ns, :flag_cwr, :flag_ece, :flag_urg,
:flag_ack, :flag_psh, :flag_rst, :flag_syn, :flag_fin

#flag_rstBoolean

Returns 1-bit RST flag.

Returns:

  • (Boolean)

    1-bit RST flag


190
191
# File 'lib/packetgen/header/tcp.rb', line 190

define_bit_fields_on :u16, :_, 7, :flag_ns, :flag_cwr, :flag_ece, :flag_urg,
:flag_ack, :flag_psh, :flag_rst, :flag_syn, :flag_fin

#flag_synBoolean

Returns 1-bit SYN flag.

Returns:

  • (Boolean)

    1-bit SYN flag


190
191
# File 'lib/packetgen/header/tcp.rb', line 190

define_bit_fields_on :u16, :_, 7, :flag_ns, :flag_cwr, :flag_ece, :flag_urg,
:flag_ack, :flag_psh, :flag_rst, :flag_syn, :flag_fin

#flag_urgBoolean

Returns 1-bit URG flag.

Returns:

  • (Boolean)

    1-bit URG flag


190
191
# File 'lib/packetgen/header/tcp.rb', line 190

define_bit_fields_on :u16, :_, 7, :flag_ns, :flag_cwr, :flag_ece, :flag_urg,
:flag_ack, :flag_psh, :flag_rst, :flag_syn, :flag_fin

#flagsInteger

Returns 9-bit flags from #u16.

Returns:

  • (Integer)

    9-bit flags from #u16


168
# File 'lib/packetgen/header/tcp.rb', line 168

define_bit_fields_on :u16, :data_offset, 4, :reserved, 3, :flags, 9

#optionsOptions

TCP options

Returns:


124
# File 'lib/packetgen/header/tcp.rb', line 124

define_field :options, TCP::Options, builder: ->(h, t) { t.new(length_from: -> { h.data_offset > 5 ? (h.data_offset - 5) * 4 : 0 }) }

#reservedInteger

Returns 3-bit reserved from #u16.

Returns:

  • (Integer)

    3-bit reserved from #u16


168
# File 'lib/packetgen/header/tcp.rb', line 168

define_bit_fields_on :u16, :data_offset, 4, :reserved, 3, :flags, 9

#seqnumInteger Also known as: sequence_number

32-bit TCP sequence number

Returns:

  • (Integer)

101
# File 'lib/packetgen/header/tcp.rb', line 101

define_field :seqnum, Types::Int32, default: ->(_) { rand(2**32) }

#sportInteger Also known as: source_port

16-bit TCP source port

Returns:

  • (Integer)

93
# File 'lib/packetgen/header/tcp.rb', line 93

define_field :sport, Types::Int16

#u16Integer

Returns 16-bit word used by flags and bit fields.

Returns:

  • (Integer)

    16-bit word used by flags and bit fields


108
# File 'lib/packetgen/header/tcp.rb', line 108

define_field :u16, Types::Int16

#urg_pointerInteger

16-bit TCP urgent data pointer

Returns:

  • (Integer)

120
# File 'lib/packetgen/header/tcp.rb', line 120

define_field :urg_pointer, Types::Int16

#windowInteger Also known as: wsize

16-bit TCP window size

Returns:

  • (Integer)

112
# File 'lib/packetgen/header/tcp.rb', line 112

define_field :window, Types::Int16

Instance Method Details

#calc_checksumInteger

Compute checksum and set checksum field

Returns:

  • (Integer)

195
196
197
198
199
200
201
# File 'lib/packetgen/header/tcp.rb', line 195

def calc_checksum
  sum = ip_header(self).pseudo_header_checksum
  sum += IP_PROTOCOL
  sum += self.sz
  sum += IP.sum16(self)
  self.checksum = IP.reduce_checksum(sum)
end

#calc_lengthInteger

Compute header length and set data_offset field

Returns:

  • (Integer)

205
206
207
# File 'lib/packetgen/header/tcp.rb', line 205

def calc_length
  self[:data_offset] = 5 + self[:options].sz / 4
end

#inspectString

Returns:

  • (String)

210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/packetgen/header/tcp.rb', line 210

def inspect
  super do |attr|
    next unless attr == :u16

    shift = Inspect.shift_level
    str = Inspect.inspect_attribute(attr, self[attr])
    doff = Inspect.int_dec_hex(data_offset, 1)
    str << shift << Inspect::FMT_ATTR % ['', 'data_offset', doff]
    str << shift << Inspect::FMT_ATTR % ['', 'reserved', reserved]
    str << shift << Inspect::FMT_ATTR % ['', 'flags', flags2string]
  end
end

#reply!self

Invert source and destination port numbers

Returns:

  • (self)

Since:

  • 2.7.0


226
227
228
229
# File 'lib/packetgen/header/tcp.rb', line 226

def reply!
  self[:sport], self[:dport] = self[:dport], self[:sport]
  self
end