Class: PacketGen::Header::TCP
- Inherits:
-
Base
- Object
- Types::Fields
- Base
- PacketGen::Header::TCP
- 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:
-
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), -
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. << PacketGen::Header::TCP::MSS.new(1250)
or:
tcph.options << { opt: 'MSS', self[attr]: 1250 }
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
-
#acknum ⇒ Integer
(also: #acknowledgement_number)
32-bit TCP acknowledgement number.
- #body ⇒ Types::String, Header::Base
-
#checksum ⇒ Integer
16-bit TCP checksum.
-
#data_offset ⇒ Integer
(also: #hlen)
4-bit data offset from #u16.
-
#dport ⇒ Integer
(also: #destination_port)
16-bit TCP destination port.
-
#flag_ack ⇒ Boolean
1-bit ACK flag.
-
#flag_cwr ⇒ Boolean
1-bit CWR flag.
-
#flag_ece ⇒ Boolean
1-bit ECE flag.
-
#flag_fin ⇒ Boolean
1-bit FIN flag.
-
#flag_ns ⇒ Boolean
1-bit NS flag.
-
#flag_psh ⇒ Boolean
1-bit PSH flag.
-
#flag_rst ⇒ Boolean
1-bit RST flag.
-
#flag_syn ⇒ Boolean
1-bit SYN flag.
-
#flag_urg ⇒ Boolean
1-bit URG flag.
-
#flags ⇒ Integer
9-bit flags from #u16.
-
#options ⇒ Options
TCP options.
-
#reserved ⇒ Integer
3-bit reserved from #u16.
-
#seqnum ⇒ Integer
(also: #sequence_number)
32-bit TCP sequence number.
-
#sport ⇒ Integer
(also: #source_port)
16-bit TCP source port.
-
#u16 ⇒ Integer
16-bit word used by flags and bit fields.
-
#urg_pointer ⇒ Integer
16-bit TCP urgent data pointer.
-
#window ⇒ Integer
(also: #wsize)
16-bit TCP window size.
Instance Method Summary collapse
-
#calc_checksum ⇒ Integer
Compute checksum and set
checksum
field. -
#calc_length ⇒ Integer
Compute header length and set
data_offset
field. -
#initialize(options = {}) ⇒ TCP
constructor
Call {Base#initialize), then handle specific options to set
u16
by part: *:data_offset
*:hlen
*:reserved
*:flags
. - #inspect ⇒ String
-
#reply! ⇒ self
Invert source and destination port numbers.
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
158 159 160 161 |
# File 'lib/packetgen/header/tcp.rb', line 158 def initialize(={}) opts = { data_offset: 5 }.merge!() super(opts) end |
Instance Attribute Details
#acknum ⇒ Integer Also known as: acknowledgement_number
32-bit TCP acknowledgement number
106 |
# File 'lib/packetgen/header/tcp.rb', line 106 define_field :acknum, Types::Int32 |
#body ⇒ Types::String, Header::Base
128 |
# File 'lib/packetgen/header/tcp.rb', line 128 define_field :body, Types::String |
#checksum ⇒ Integer
16-bit TCP checksum
117 |
# File 'lib/packetgen/header/tcp.rb', line 117 define_field :checksum, Types::Int16 |
#data_offset ⇒ Integer Also known as: hlen
Returns 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 |
#dport ⇒ Integer Also known as: destination_port
16-bit TCP destination port
98 |
# File 'lib/packetgen/header/tcp.rb', line 98 define_field :dport, Types::Int16 |
#flag_ack ⇒ Boolean
Returns 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_cwr ⇒ Boolean
Returns 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_ece ⇒ Boolean
Returns 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_fin ⇒ Boolean
Returns 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_ns ⇒ Boolean
Returns 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_psh ⇒ Boolean
Returns 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_rst ⇒ Boolean
Returns 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_syn ⇒ Boolean
Returns 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_urg ⇒ Boolean
Returns 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 |
#flags ⇒ Integer
Returns 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 |
#options ⇒ Options
TCP options
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 }) } |
#reserved ⇒ Integer
Returns 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 |
#seqnum ⇒ Integer Also known as: sequence_number
32-bit TCP sequence number
102 |
# File 'lib/packetgen/header/tcp.rb', line 102 define_field :seqnum, Types::Int32, default: ->(_) { rand(2**32) } |
#sport ⇒ Integer Also known as: source_port
16-bit TCP source port
94 |
# File 'lib/packetgen/header/tcp.rb', line 94 define_field :sport, Types::Int16 |
#u16 ⇒ Integer
Returns 16-bit word used by flags and bit fields.
109 |
# File 'lib/packetgen/header/tcp.rb', line 109 define_field :u16, Types::Int16 |
Instance Method Details
#calc_checksum ⇒ Integer
Compute checksum and set checksum
field
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_length ⇒ Integer
Compute header length and set data_offset
field
206 207 208 |
# File 'lib/packetgen/header/tcp.rb', line 206 def calc_length self[:data_offset] = 5 + self[:options].sz / 4 end |
#inspect ⇒ 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
227 228 229 230 |
# File 'lib/packetgen/header/tcp.rb', line 227 def reply! self[:sport], self[:dport] = self[:dport], self[:sport] self end |