Class: DHCP::OptRouteList

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

Overview

Class for DHCP options containing lists of IPv4 CIDR routes (like option 121 or MS’s 249) See RFC 3442 “compact encoding” of destination

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, #is_list?, #set, #size, #to_s

Methods inherited from OptData

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

Methods inherited from Opt

#initialize, #opt_header, #to_opt, #to_s

Constructor Details

This class inherits a constructor from DHCP::OptListData

Instance Method Details

#bin_to_data(data) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
# File 'lib/dhcp/options.rb', line 417

def bin_to_data(data)
  raise "Invalid binary classless route data" unless data.size > 4 || data[0,1].ord > 32
  maskbits = data[0,1].ord
  octets   = (maskbits+7)/8
  raise "Invalid binary classless route data" unless data.size == octets + 5
  dest = IPAddress::IPv4.parse_data(data[1,octets] + 0.chr * (4 - octets))
  dest.prefix = maskbits
  gateway = IPAddress::IPv4.parse_data(data[octets+1,4])
  gateway.prefix = maskbits  ## Unnecessary...
  [dest.to_string, gateway.to_s]
end

#data_to_bin(data) ⇒ Object



406
407
408
409
410
411
412
413
414
415
# File 'lib/dhcp/options.rb', line 406

def data_to_bin(data)
  raise "Invalid classless static route" unless data.is_a?(Array) && data.size == 2
  net, gateway = *data
  raise "Invalid classless static route network" if net.index('/').nil?
  net = IPAddress::IPv4.new(net)
  raise "Invalid classless static route network" unless net.network?
  gateway = IPAddress::IPv4.new("#{gateway}/#{net.prefix}")
  raise "Invalid classless static route gateway" unless gateway.member?(net)
  net.prefix.to_i.chr + net.data[0,(net.prefix+7)/8] + gateway.data
end

#split_data(data) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
# File 'lib/dhcp/options.rb', line 394

def split_data(data)
  data = data.dup
  list = []
  while data.size > 0
    raise "Invalid binary data" unless data.size > 4 || data[0,1].ord > 32
    octets = (data[0,1].ord + 7)/8
    raise "Invalid binary data" unless data.size >= octets + 5
    list << bin_to_data(data.slice!(0,octets+5))
 end
 list
end