TCP (Transmission Control Protocol) segment: Ruby parsing library

TCP is one of the core Internet protocols on transport layer (AKA OSI layer 4), providing stateful connections with error checking, guarantees of delivery, order of segments and avoidance of duplicate delivery.

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of TCP (Transmission Control Protocol) segment using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for Ruby generated by Kaitai Struct depends on the Ruby runtime library. You have to install it before you can parse data.

The Ruby runtime library can be installed from RubyGems:

gem install kaitai-struct

Code

Parse a local file and get structure in memory:

data = TcpSegment.from_file("path/to/local/file.TCP (Transmission Control Protocol) segment")

Or parse structure from a string of bytes:

bytes = "\x00\x01\x02..."
data = TcpSegment.new(Kaitai::Struct::Stream.new(bytes))

After that, one can get various attributes from the structure by invoking getter methods like:

data.src_port # => get src port

Ruby source code to parse TCP (Transmission Control Protocol) segment

tcp_segment.rb

# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

require 'kaitai/struct/struct'

unless Gem::Version.new(Kaitai::Struct::VERSION) >= Gem::Version.new('0.9')
  raise "Incompatible Kaitai Struct Ruby API: 0.9 or later is required, but you have #{Kaitai::Struct::VERSION}"
end


##
# TCP is one of the core Internet protocols on transport layer (AKA
# OSI layer 4), providing stateful connections with error checking,
# guarantees of delivery, order of segments and avoidance of duplicate
# delivery.
class TcpSegment < Kaitai::Struct::Struct
  def initialize(_io, _parent = nil, _root = self)
    super(_io, _parent, _root)
    _read
  end

  def _read
    @src_port = @_io.read_u2be
    @dst_port = @_io.read_u2be
    @seq_num = @_io.read_u4be
    @ack_num = @_io.read_u4be
    @b12 = @_io.read_u1
    @b13 = @_io.read_u1
    @window_size = @_io.read_u2be
    @checksum = @_io.read_u2be
    @urgent_pointer = @_io.read_u2be
    @body = @_io.read_bytes_full
    self
  end
  attr_reader :src_port
  attr_reader :dst_port
  attr_reader :seq_num
  attr_reader :ack_num
  attr_reader :b12
  attr_reader :b13
  attr_reader :window_size
  attr_reader :checksum
  attr_reader :urgent_pointer
  attr_reader :body
end