Class Racket::L3::IPv4
In: lib/racket/l3/ipv4.rb  (CVS)
Parent: RacketPart

Internet Protcol Version 4 (IPV4)

RFC791 (www.ietf.org/rfc/rfc791.txt)

Methods

add_option   checksum!   checksum?   fix!   new  

Attributes

Checksum    Checksum
checksum    Checksum
dst_ip    Destination IP address, passed as four octets separated by periods (192.168.1.2)
flags    Flags
foffset    Fragmentation offset
hlen    Header length in multiples of 4 octets (defaults to 5)
id    Identifier
len    Datagram length
payload    Payload
protocol    Protocol
src_ip    Source IP address, passed as four octets separated by periods (192.168.1.2)
tos    Type of Service
ttl    Time-to-live
version    Version (defaults to 4)

Public Class methods

[Source]

# File lib/racket/l3/ipv4.rb, line 63
  def initialize(*args)
    @options = []
    super
  end

Public Instance methods

Add an IPv4 option to this IPv4 object. All rejiggering will happen when the call to fix! happens automagically

[Source]

# File lib/racket/l3/ipv4.rb, line 71
  def add_option(number, value)
    t = Racket::Misc::TLV.new(1,1)
    t.type = number
    t.value = value
    t.length = value.length + 2
    @options << t.encode
  end

Compute and set the checksum for this IP datagram

[Source]

# File lib/racket/l3/ipv4.rb, line 85
  def checksum!
    self.checksum = compute_checksum
  end

Check the checksum for this IP datagram

[Source]

# File lib/racket/l3/ipv4.rb, line 80
  def checksum?
    self.checksum == compute_checksum
  end

Perform all the niceties necessary prior to sending this IP datagram out. Append the options, update len and hlen, and fix the checksum.

[Source]

# File lib/racket/l3/ipv4.rb, line 92
  def fix!
    newpayload = @options.join
    
    # pad to a multiple of 32 bits
    if (newpayload.length % 4 != 0) 
      # fill the beginning as needed with NOPs
      while (newpayload.length % 4 != 3)
        newpayload = "\x01#{newpayload}"
      end

      # make sure the last byte is an EOL
      if (newpayload.length % 4 == 3)
        newpayload += "\x00"
      end
    end

    self.payload = newpayload + self.payload
    self.hlen += newpayload.length/4
    self.len = self.payload.length + self.class.bit_length/8
    self.checksum!
  end

[Validate]