Class: PacketGen::Header::IP::Options

Inherits:
Types::Array show all
Defined in:
lib/packetgen/header/ip/options.rb

Overview

Class to handle IP options

Author:

  • Sylvain Daubert

Constant Summary collapse

HUMAN_SEPARATOR =
';'

Instance Method Summary collapse

Methods inherited from Types::Array

#<<, #==, #[], #clear, #clear!, #delete, #delete_at, #each, #empty?, #first, #force_binary, #initialize, #initialize_copy, #last, #push, set_of, #size, #sz, #to_a, #to_human

Constructor Details

This class inherits a constructor from PacketGen::Types::Array

Instance Method Details

#read(str) ⇒ self

Read IP header options from a string



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/packetgen/header/ip/options.rb', line 21

def read(str)
  clear
  return self if str.nil?
  PacketGen.force_binary str

  i = 0
  types = Option.types
  while i < str.to_s.length
    type = str[i, 1].unpack('C').first
    this_option = if types[type].nil?
                    Option.new
                  else
                    types[type].new
                  end
    this_option.read str[i, str.size]
    self << this_option
    i += this_option.sz
  end
  self
end

#to_sString

Get binary string



44
45
46
47
48
49
50
# File 'lib/packetgen/header/ip/options.rb', line 44

def to_s
  str = super
  if str.length % 4 != 0
    str += ([0] * (4 - (str.length % 4))).pack('C*')
  end
  str
end