Class: DHCP::OptInt8

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

Overview

Class for single-byte unsigned integer value DHCP options Also acts as parent class for fixed-sized multi-byte value DHCP options

Direct Known Subclasses

OptInt16, OptInt32

Instance Attribute Summary

Attributes inherited from OptData

#data

Attributes inherited from Opt

#name, #opt

Instance Method Summary collapse

Methods inherited from OptFixedData

#data=, #initialize

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::OptFixedData

Instance Method Details

#bin_to_data(data) ⇒ Object



463
464
465
# File 'lib/dhcp/options.rb', line 463

def bin_to_data(data)
  data.each_byte.inject(0){|sum,byte| sum<<8|byte}
end

#data_to_bin(data) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
# File 'lib/dhcp/options.rb', line 451

def data_to_bin(data)
  raise "Invalid numeric data" unless data.is_a?(Fixnum) && data >= 0
  raise "Invalid number" unless data == data & ([0xff] * self.class.size).inject(0){|sum,byte| sum<<8|byte}
  bytes = ''
  while data != 0
    bytes = (data & 0xff).chr + bytes
    data >>= 8
  end
  raise "Impossible: Numeric byte size #{bytes.size} exceeds #{self.class.size}" if bytes.size > self.class.size
  0.chr * (self.class.size - bytes.size) + bytes
end

#to_sObject



467
468
469
# File 'lib/dhcp/options.rb', line 467

def to_s
  opt_header + "=#{self.get}"
end