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)


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

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)


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

define_field :acknum, Types::Int32

#bodyTypes::String, Header::Base



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

define_field :body, Types::String

#checksumInteger

16-bit TCP checksum

Returns:

  • (Integer)


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

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



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

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)


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

define_field :dport, Types::Int16

#flag_ackBoolean

Returns 1-bit ACK flag.

Returns:

  • (Boolean)

    1-bit ACK flag



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#optionsOptions

TCP options

Returns:



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

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



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

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)


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

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

#sportInteger Also known as: source_port

16-bit TCP source port

Returns:

  • (Integer)


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

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



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

define_field :u16, Types::Int16

#urg_pointerInteger

16-bit TCP urgent data pointer

Returns:

  • (Integer)


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

define_field :urg_pointer, Types::Int16

#windowInteger Also known as: wsize

16-bit TCP window size

Returns:

  • (Integer)


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

define_field :window, Types::Int16

Instance Method Details

#calc_checksumInteger

Compute checksum and set checksum field

Returns:

  • (Integer)


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

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)


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

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

#inspectString

Returns:

  • (String)


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

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



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

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