Class: DHCP::OptSubList

Inherits:
OptListData show all
Defined in:
lib/dhcp/options.rb

Overview

Class for DHCP options that contain sublists (like vendor specific information or relay agent information)

Instance Attribute Summary

Attributes inherited from OptData

#data

Attributes inherited from Opt

#name, #opt

Instance Method Summary collapse

Methods inherited from OptListData

#append, #data=, #each, #get, #initialize, #set, #size

Methods inherited from OptData

#get, #initialize, #opt_header, #set, #to_opt

Methods inherited from Opt

#initialize, #opt_header, #to_opt

Constructor Details

This class inherits a constructor from DHCP::OptListData

Instance Method Details

#bin_to_data(data) ⇒ Object



262
263
264
265
# File 'lib/dhcp/options.rb', line 262

def bin_to_data(data)
  raise "Invalid data size" unless data.size >= 2 && data.size == data[1,1].ord + 2
  [ data[0,1].ord, data[2,data.size-2] ]
end

#data_to_bin(data) ⇒ Object



267
268
269
270
271
# File 'lib/dhcp/options.rb', line 267

def data_to_bin(data)
  raise "Invalid data" unless data.is_a?(Array) && data.size == 2 && data[0].is_a?(Fixnum) && data[1].is_a?(String)
  raise "Invalid data size" unless data[1].size < 256
  data[0].chr + data[1].size.chr + data[1]
end

#is_list?(list) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
243
244
245
246
247
# File 'lib/dhcp/options.rb', line 240

def is_list?(list)
  raise "Invalid suboption list/entry" unless list.is_a?(Array)
  return false if list.size == 2 && list[0].is_a?(Fixnum) && list[1].is_a?(String)
  list.each do |item|
    raise "Invalid suboption list" unless item.is_a?(Array) && item.size == 2 && item[0].is_a?(Fixnum) && item[1].is_a?(String)
  end
  return true
end

#split_data(data) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/dhcp/options.rb', line 249

def split_data(data)
  data = data.dup
  list = []
  while data.size > 0
    raise "Invalid data size" unless data.size >= 2
    len = data[1,1].ord
    raise "Invalid data size" unless data.size >= len + 2
    list << [ data[0,1].ord, data[2,len] ]
    data[0,len+2] = ''
  end
  list
end

#to_sObject



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/dhcp/options.rb', line 273

def to_s
  opt_header + "(#{@size})=[" + map do |i|
    val = ''
    name = case i[0]
    when 1
      val = i[1].scan(/./m).map{|b| b.unpack('H2')[0].upcase}.join(':')
      'AgentCircuitID'
    when 2
      val = i[1].scan(/./m).map{|b| b.unpack('H2')[0].upcase}.join(':')
      'AgentRemoteID'
    when 9
      val = (SubOptVSRInfo.new(9, :vendor_specific_relay_suboption).data=i[1]).to_s
      'VendorSpecificRelaySuboption'
    else
      val = i[1].scan(/./m).map{|b| b.unpack('H2')[0].upcase}.join(':')
      'Unknown'
    end
    "#{name}:#{i[0]}(#{i[1].size})='#{val}'"
  end.join(',') + ']'
end