Class: Lib::DHCP::Packet

Inherits:
BOOTP::Packet
  • Object
show all
Defined in:
lib/lib/dhcp/packet.rb

Direct Known Subclasses

Message

Constant Summary collapse

0x63825363

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unpack(packet) ⇒ Object

Raises:

  • (RuntimeError)


60
61
62
63
64
65
66
# File 'lib/lib/dhcp/packet.rb', line 60

def self.unpack(packet)
  bootp, cookie, options = packet.unpack('a236Na*')
  raise RuntimeError, "Magick-Cookie mismatch #{cookie.to_i.to_s(16)}" unless cookie.to_i == MAGICK_COOKIE.to_i
  dhcp = super bootp
  dhcp.instance_variable_set(:@options, Lib::DHCP::Options.unpack(options))
  dhcp
end

Instance Method Details

#option0Object



32
33
34
# File 'lib/lib/dhcp/packet.rb', line 32

def option0
  self.options.select 0
end

#option255Object



36
37
38
# File 'lib/lib/dhcp/packet.rb', line 36

def option255
  self.options.select 255
end

#optionsObject



56
57
58
# File 'lib/lib/dhcp/packet.rb', line 56

def options
  @options ||= Lib::DHCP::Options.new
end

#packObject

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lib/dhcp/packet.rb', line 68

def pack
  # TODO Max Message Size support
  dhcp_message = super # BOOTP Header
  dhcp_message += [MAGICK_COOKIE].pack('N')
  raise ArgumentError, "Can't pack DHCP Packet without DHCP Message Type" if self.option53.nil?
  dhcp_message += self.option53.pack # Option 53 (DHCP MESSAGE TYPE) must be first in message

  len = 240 # 236 BOOTP HEADER + 3 (Option 53) + 1 (Option 255)
  self.options.each do |option|
    unless option.oid.to_i == 0 or option.oid.to_i == 255 or option.oid.to_i == 53
      # TODO Pack RAW Option
      # TODO Need to test RAW Option packing
      # next if option.is_a? Lib::DHCP::Option::Type::Raw # Don't pack not implemented Option type
      dhcp_message += option.pack
      len += (option.len.to_i + 2) # Option ID + Option LEN + Option PAYLOAD
    end
  end
  dhcp_message += Lib::DHCP::Option255.new.pack
  while len < 300 # Minimal DHCP MESSAGE SIZE
    dhcp_message += Lib::DHCP::Option0.new.pack
    len += 1
  end
  dhcp_message
end

#to_sObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lib/dhcp/packet.rb', line 40

def to_s
  str = super()
  str += "\nMAGICK-COOKIE      : 0x#{MAGICK_COOKIE.to_s(16)}"
  str += "\n\n  --- DHCP OPTIONS ---\n"
  options.each do |o|
    if o.value.is_a? Array
      value = o.value.map(&:to_s).join(',')
    else
      value = o.value
    end
    str += "\nOption #{o.oid.to_s.ljust(3,' ')} : LEN #{o.len.to_s.ljust(3, ' ')} #{o.name.to_s.ljust(40, ' ')}  : #{value}"
  end
  str += "\n\n"
  str
end