Class Racket::L4::TCP
In: lib/racket/l4/tcp.rb  (CVS)
Parent: RacketPart

Transmission Control Protocol: TCP

RFC793 (



ack    Acknowledgement number
checksum    Checksum
dst_port    Destination port
flag_ack    ACK
flag_cwr    CWR
flag_ece    ECE
flag_fin    FIN
flag_psh    PSH
flag_rst    RST
flag_syn    SYN
flag_urg    URG
offset    Data Offset
payload    Payload
reserved    Reserved
seq    Sequence number
src_port    Source port
urg    Urgent pointer
window    Window size

Public Class methods


# File lib/racket/l4/tcp.rb, line 122
  def initialize(*args)
    @options = []
    @autofix = false

Public Instance methods

Add an TCP option to this TCP object. All rejiggering will happen when the call to fix! happens automagically. If the result is not on a 32-bit boundry, pad with NOPs.


# File lib/racket/l4/tcp.rb, line 75
  def add_option(number, value)
    t =,1)
    t.type = number
    t.value = value
    t.length = value.length + 2
    opts = t.encode
    opts += ("\x01" * (4-(opts.size % 4))) if (opts.size % 4) != 0
    @options << opts

Add a raw TCP option to this packet. It is entirely your responsibility (or irresponsibility, as the case may be) to properly pad or otherwise define this


# File lib/racket/l4/tcp.rb, line 88
  def add_raw_option(value)
    @options << value

Compute and set the checksum for this TCP packet


# File lib/racket/l4/tcp.rb, line 98
  def checksum!(ip_src, ip_dst)
    self.checksum = compute_checksum(ip_src, ip_dst)

Check the checksum for this TCP packet


# File lib/racket/l4/tcp.rb, line 93
  def checksum?(ip_src, ip_dst)
    self.checksum == compute_checksum(ip_src, ip_dst)

Fix this packet up for proper sending. Sets the length and checksum properly.


# File lib/racket/l4/tcp.rb, line 104
  def fix!(ip_src, ip_dst, next_payload)
    newpayload = @options.join
    # pad to a multiple of 32 bits
    if ((self.class.bit_length/8 + newpayload.length) % 4 != 0) 
      # fill the beginning as needed with NOPs
      while ((self.class.bit_length/8 + newpayload.length) % 4 != 4)
        puts (self.class.bit_length/8 + newpayload.length) % 4
        newpayload = "\x01#{newpayload}"

    self.payload = newpayload + self.payload + next_payload
    self.offset = self.class.bit_length/32 + newpayload.length/4
    self.checksum!(ip_src, ip_dst)
    self.payload = newpayload