Class: Lib::BOOTP::Packet::ClientHardwareAddress

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/lib/bootp/packet/client_hardware_address.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chaddr) ⇒ ClientHardwareAddress

Returns a new instance of ClientHardwareAddress.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/lib/bootp/packet/client_hardware_address.rb', line 14

def initialize(chaddr)
  raise ArgumentError, "Can't convert #{chaddr.class.name} to String" unless chaddr.respond_to? :to_s
  clear_chaddr = chaddr.to_s.gsub(/([:\-.,])/, '').to_s
  raise ArgumentError, "Given CHADDR is to long: #{chaddr}" if clear_chaddr.size > 32
  @chaddr = [clear_chaddr.ljust(32, '0')].pack('H32').unpack('C*')
end

Class Method Details

.unpack(packet) ⇒ Object



42
43
44
# File 'lib/lib/bootp/packet/client_hardware_address.rb', line 42

def self.unpack(packet)
  new packet.unpack('H32').first.to_s
end

Instance Method Details

#<=>(other) ⇒ Object



29
30
31
32
# File 'lib/lib/bootp/packet/client_hardware_address.rb', line 29

def <=>(other)
  clear_other = other.to_s.gsub(/([:\-.,])/, '').to_s
  @chaddr <=> [clear_other.ljust(32, '0')].pack('H32').unpack('C*')
end

#packObject



38
39
40
# File 'lib/lib/bootp/packet/client_hardware_address.rb', line 38

def pack
  [@chaddr.map{ |item| item.to_s(16).rjust(2, '0') }.join('')].pack('H32')
end

#to_aObject



34
35
36
# File 'lib/lib/bootp/packet/client_hardware_address.rb', line 34

def to_a
  @chaddr
end

#to_s(len = nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/lib/bootp/packet/client_hardware_address.rb', line 21

def to_s(len = nil)
  unless len.nil? || len.is_a?(Integer)
    raise ArgumentError "Len must be a Integer or nil, #{len.class.name} given"
  end
  raise ArgumentError "Len out of range: #{len}" if len.to_i > 16 || len.to_i < 0
  (len ? @chaddr[0..(len - 1)] : @chaddr).map{ |item| item.to_s(16).rjust(2, '0') }.join(':')
end