Class: Racket::L3::IPv4

Inherits:
RacketPart show all
Defined in:
lib/racket/l3/ipv4.rb

Overview

Instance Attribute Summary

Attributes inherited from RacketPart

#autofix

Instance Method Summary collapse

Methods inherited from RacketPart

#autofix?, #pretty

Constructor Details

#initialize(*args) ⇒ IPv4

Returns a new instance of IPv4.



63
64
65
66
# File 'lib/racket/l3/ipv4.rb', line 63

def initialize(*args)
  @options = []
  super
end

Instance Method Details

#add_option(number, value) ⇒ Object

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



71
72
73
74
75
76
77
# 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

#checksum!Object

Compute and set the checksum for this IP datagram



85
86
87
# File 'lib/racket/l3/ipv4.rb', line 85

def checksum!
  self.checksum = compute_checksum
end

#checksum?Boolean

Check the checksum for this IP datagram

Returns:

  • (Boolean)


80
81
82
# File 'lib/racket/l3/ipv4.rb', line 80

def checksum?
  self.checksum == compute_checksum
end

#fix!Object

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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