class PacketGen::Header::TCP
TCP
header ({tools.ietf.org/html/rfc793 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:
-
a source port ({#sport}, {Types::Int16} type),
-
a destination port ({#dport},
Int16
type), -
a sequence number ({#seqnum}, {Types::Int32} type),
-
an acknownledge number ({#acknum},
Int32
type), -
a 16-bit field ({#u16},
Int16
type) composed of:-
a 4-bit {#data_offset} self,
-
a 3-bit {#reserved} field,
-
a 9-bit {#flags} field,
-
-
a {#window} field (
Int16
type), -
a {#checksum} field (
Int16
type), -
a urgent pointer ({#urg_pointer},
Int16
type), -
an optional {#options} field ({Options} type),
-
and a {#body} ({Types::String} type).
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
Constants
Public Class Methods
Call {Base#initialize), then handle specific options to set u16
by part:
-
:data_offset
-
:hlen
-
:reserved
-
:flags
@param [Hash] options @option options [Integer] :sport @option options [Integer] :dport @option options [Integer] :seqnum @option options [Integer] :acknum @option options [Integer] :data_offset @option options [Integer] :reserved @option options [Integer] :flags @option options [Integer] :window @option options [Integer] :checksum @option options [Integer] :urg_pointer @option options [String] :body
PacketGen::Header::Base::new
# File lib/packetgen/header/tcp.rb, line 157 def initialize(options={}) opts = { data_offset: 5 }.merge!(options) super(opts) end
Public Instance Methods
Compute checksum and set checksum
field @return [Integer]
# 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
Compute header length and set data_offset
field @return [Integer]
# File lib/packetgen/header/tcp.rb, line 205 def calc_length self[:data_offset] = 5 + self[:options].sz / 4 end
@return [String]
# 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] flags = +'' %w[ns cwr ece urg ack psh rst syn fin].each do |fl| flags << (send("flag_#{fl}?") ? fl[0].upcase : '.') end str << shift << Inspect::FMT_ATTR % ['', 'flags', flags] end end
Invert source and destination port numbers @return [self] @since 2.7.0
# File lib/packetgen/header/tcp.rb, line 230 def reply! self[:sport], self[:dport] = self[:dport], self[:sport] self end