Module: Lib::DHCP::Option::Type::IPAddress

Included in:
Lib::DHCP::Option122::SubOption::Option1, Lib::DHCP::Option122::SubOption::Option2
Defined in:
lib/lib/dhcp/options/type/ip_address.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
# File 'lib/lib/dhcp/options/type/ip_address.rb', line 13

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#initialize(*address) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lib/dhcp/options/type/ip_address.rb', line 17

def initialize(*address)
  raise ArgumentError, 'To few argument - nil' if address.nil?
  oid = self.class.name.split('::').last.sub(/Option/, '').to_i
  if address.size == 1
    address = address.first.split(',') if address.first.is_a? ::String
  end
  case address.size
    when 0
      raise ArgumentError, 'To few argument - 0'
    when 1
      address = address.first
      super oid, (address.is_a? Net::Address::IPv4) ? address : Net::Address::IPv4.new(address)
    else
      super oid, address.map {|ip| (ip.is_a? Net::Address::IPv4) ? ip : Net::Address::IPv4.new(ip)}
  end
end

#lenObject



48
49
50
51
52
53
54
# File 'lib/lib/dhcp/options/type/ip_address.rb', line 48

def len
  if @payload.is_a? Array
    @payload.size * 4
  else
    4
  end
end

#packObject



38
39
40
41
42
43
44
45
46
# File 'lib/lib/dhcp/options/type/ip_address.rb', line 38

def pack
  if @payload.is_a? Array
    #len = @payload.size * 4
    [@oid, len, @payload.map(&:to_i)].flatten.pack('C2N*')
  else
    raise ArgumentError, 'Argument must be a IP ' unless @payload.is_a? Net::Address::IPv4
    [@oid, 4, @payload.to_i].pack('C2N')
  end
end

#payload=(address) ⇒ Object



56
57
58
# File 'lib/lib/dhcp/options/type/ip_address.rb', line 56

def payload=(address)
  (address.is_a? Net::Address::IPv4) ? @payload = address : @payload = Net::Address::IPv4.new(address)
end

#to_sObject



34
35
36
# File 'lib/lib/dhcp/options/type/ip_address.rb', line 34

def to_s
  @payload.to_s
end