Class: PcapParser::Proto::TCP

Inherits:
Object
  • Object
show all
Defined in:
lib/pcap_parser/proto/tcp.rb

Overview

TCP protocol read. Segment structure: en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_segment_structure Read values are big-endian (network byte order)

Instance Method Summary collapse

Constructor Details

#initialize(bin_header) ⇒ TCP


7
8
9
# File 'lib/pcap_parser/proto/tcp.rb', line 7

def initialize(bin_header)
  @binhdr = bin_header
end

Instance Method Details

#[](flag) ⇒ true, false

Flags (9 bits) (aka Control bits)


43
44
45
46
# File 'lib/pcap_parser/proto/tcp.rb', line 43

def [](flag)
  bit = { NS: 8, CWR: 1, ECE: 2, URG: 3, ACK: 4, PSH: 5, RST: 6, SYN: 7, FIN: 8 }
  Stream::bit_set?(@binhdr[flag.eql?(:NS) ? 12 : 13], bit[flag])
end

#acknumInteger

Acknowledgment number (if ACK set)


31
32
33
# File 'lib/pcap_parser/proto/tcp.rb', line 31

def acknum
  @binhdr[8, 4].unpack("N").pop
end

#chsumInteger

Checksum (16 bits)


56
57
58
# File 'lib/pcap_parser/proto/tcp.rb', line 56

def chsum
  @binhdr[16, 2].unpack("n").pop
end

#dataString

TCP segment data.


68
69
70
# File 'lib/pcap_parser/proto/tcp.rb', line 68

def data
  @binhdr[header_len..-1].unpack("a*").pop
end

#has_opts?true, false

Check if options set in TCP segment.


62
63
64
# File 'lib/pcap_parser/proto/tcp.rb', line 62

def has_opts?
  header_len > 20
end

#header_lenInteger

Data offset (4 bits)


37
38
39
# File 'lib/pcap_parser/proto/tcp.rb', line 37

def header_len
  (@binhdr[12, 1].unpack("C").pop >> 4) << 2
end

#port_dstInteger

Destination port.


19
20
21
# File 'lib/pcap_parser/proto/tcp.rb', line 19

def port_dst
  @binhdr[2, 2].unpack("n").pop
end

#port_srcInteger

Source port.


13
14
15
# File 'lib/pcap_parser/proto/tcp.rb', line 13

def port_src
  @binhdr[0, 2].unpack("n").pop
end

#seqInteger

Sequence number


25
26
27
# File 'lib/pcap_parser/proto/tcp.rb', line 25

def seq
  @binhdr[4, 4].unpack("N").pop
end

#win_sizeInteger

Window size (16 bits)


50
51
52
# File 'lib/pcap_parser/proto/tcp.rb', line 50

def win_size
  @binhdr[14, 2].unpack("n").pop
end