Class: IPAddr::Base

Inherits:
IPAddr show all
Includes:
BetterIpaddr::Constants, BetterIpaddr::InstanceMethods, Comparable, Enumerable
Defined in:
lib/better_ipaddr/classes.rb

Direct Known Subclasses

MAC, V4, V6

Constant Summary

Constants included from BetterIpaddr::Constants

BetterIpaddr::Constants::FAMILY_TO_BIT_LENGTH, BetterIpaddr::Constants::NETMASK_TO_PREFIX_LENGTH, BetterIpaddr::Constants::PREFIX_LENGTH_TO_NETMASK, BetterIpaddr::Constants::SYMBOL_TO_FAMILY

Instance Attribute Summary

Attributes included from BetterIpaddr::InstanceMethods

#family, #mask_addr

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BetterIpaddr::InstanceMethods

#+, #-, #<=>, #==, #[], #base, #cidr, #cover?, #each, #first, #grow, #host?, #last, #netmask, #overlap?, #shrink, #size, #summarize_with, #to_range, #wildcard

Methods included from BetterIpaddr::ClassMethods

#[]

Class Method Details

.[](address, mask = nil, family: self::FAMILY) ⇒ IPAddr, Nil

Create an IPAddr from the given object.

Returns nil if the object is of a type that can’t be converted to an IPAddr.

Parameters:

  • address (Integer, IPAddr, String)
  • mask (Integer, IPAddr, String, Nil) (defaults to: nil)
  • family (Integer) (defaults to: self::FAMILY)

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/better_ipaddr/classes.rb', line 23

def self.[](address, mask = nil, family: self::FAMILY)
  prefix_length = mask && object_to_prefix_length(mask, family)

  case address
  when Integer
    from_integer(address, prefix_length, family: family)
  when IPAddr
    from_ipaddr(address, prefix_length, family: family)
  when String
    from_string(address, prefix_length, family: family)
  end
end

.from_integer(address, prefix_length, family: self::FAMILY) ⇒ IPAddr

Create an IPAddr from an Integer.

Parameters:

  • address (Integer)
  • mask (Integer, String)

    a netmask or prefix length

  • family (Integer, Nil) (defaults to: self::FAMILY)

Returns:



42
43
44
# File 'lib/better_ipaddr/classes.rb', line 42

def self.from_integer(address, prefix_length, family: self::FAMILY)
  new(address, family).mask(prefix_length || FAMILY_TO_BIT_LENGTH[family])
end

.from_ipaddr(address, prefix_length, family: self::FAMILY) ⇒ IPAddr

Create an IPAddr from an IPAddr.

Parameters:

  • address (IPAddr)
  • mask (Integer, String)

    a netmask or prefix length

  • family (Integer, Nil) (defaults to: self::FAMILY)

Returns:



52
53
54
# File 'lib/better_ipaddr/classes.rb', line 52

def self.from_ipaddr(address, prefix_length, family: self::FAMILY)
  new(address.to_i, family).mask(prefix_length || address.prefix_length)
end

.from_string(address, mask = nil, family: self::FAMILY) ⇒ IPAddr

Create an IPAddr from a String.

Parameters:

  • address (String)
  • mask (Integer, String) (defaults to: nil)

    a netmask or prefix length

  • family (Integer, Nil) (defaults to: self::FAMILY)

Returns:



62
63
64
65
66
67
68
# File 'lib/better_ipaddr/classes.rb', line 62

def self.from_string(address, mask = nil, family: self::FAMILY)
  if mask
    new(address, family).mask(mask)
  else
    new(address, family)
  end
end

.integer_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer

Convert an integer to a prefix length.

If the integer is within the range of possible prefix lengths, returns the same integer. Otherwise it assumes that the given integer is the integer representation of a netmask.

Returns nil if the integer can’t be converted.

Parameters:

  • mask (Integer)

Returns:

  • (Integer)


98
99
100
101
102
103
104
105
# File 'lib/better_ipaddr/classes.rb', line 98

def self.integer_to_prefix_length(mask, family = self::FAMILY)
  if valid_prefix_length?(mask)
    mask
  else
    NETMASK_TO_PREFIX_LENGTH[family][mask] ||
      (raise ArgumentError, "Can't convert #{mask} to prefix length")
  end
end

.ipaddr_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer

Convert a netmask represented as an IPAddr to a prefix length.

Returns nil if the IPAddr can’t be converted.

Parameters:

Returns:

  • (Integer)


113
114
115
# File 'lib/better_ipaddr/classes.rb', line 113

def self.ipaddr_to_prefix_length(mask, family = self::FAMILY)
  NETMASK_TO_PREFIX_LENGTH[family][mask.to_i]
end

.object_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer

Convert an object to a prefix length.

Parameters:

  • mask (Integer, String)
  • family (Integer, Nil) (defaults to: self::FAMILY)

Returns:

  • (Integer)


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/better_ipaddr/classes.rb', line 75

def self.object_to_prefix_length(mask, family = self::FAMILY)
  case mask
  when Integer
    integer_to_prefix_length(mask, family)
  when String
    string_to_prefix_length(mask, family)
  when IPAddr
    ipaddr_to_prefix_length(mask, family)
  else
    raise ArgumentError, "Can't convert #{mask.class} to prefix length"
  end
end

.parse(address) ⇒ IPAddr::V4, ...

Convert the given string to an IPAddr subclass.

Parameters:

  • address (String)

    the string to convert

Returns:



146
147
148
# File 'lib/better_ipaddr/classes.rb', line 146

def self.parse(address)
  specialize IPAddr.new(address)
end

.specialize(address) ⇒ IPAddr::V4, ...

Return the given address as an instance of a class specific to its address family.

Parameters:

  • address (IPAddr)

    the address to convert

Returns:



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/better_ipaddr/classes.rb', line 155

def self.specialize(address)
  return address unless address.class == IPAddr
  case address.family
  when Family::IPV4
    IPAddr::V4[address.to_i, address.instance_variable_get(:@mask_addr)]
  when Family::IPV6
    IPAddr::V6[address.to_i, address.instance_variable_get(:@mask_addr)]
  when Family::EUI48
    IPAddr::MAC[address.to_i, address.instance_variable_get(:@mask_addr)]
  end
end

.specialize_constants(family) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/better_ipaddr/classes.rb', line 167

def self.specialize_constants(family)
  const_set(:FAMILY, family)
  const_set(:BIT_LENGTH, FAMILY_TO_BIT_LENGTH.fetch(self::FAMILY))
  const_set(:NETMASK_TO_PREFIX_LENGTH,
            NETMASK_TO_PREFIX_LENGTH.fetch(self::FAMILY))
  const_set(:PREFIX_LENGTH_TO_NETMASK,
            PREFIX_LENGTH_TO_NETMASK.fetch(self::FAMILY))
end

.string_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer

Convert a string to a prefix length.

Accepts the decimal representations of integers as well as netmasks in dotted quad notation.

Returns nil if the string can’t be converted.

Parameters:

  • mask (String)

Returns:

  • (Integer)


126
127
128
129
130
131
132
# File 'lib/better_ipaddr/classes.rb', line 126

def self.string_to_prefix_length(mask, family = self::FAMILY)
  if mask =~ /^\d+$/
    integer_to_prefix_length(mask.to_i, family)
  else
    NETMASK_TO_PREFIX_LENGTH[family][new(mask).to_i]
  end
end

.valid_prefix_length?(prefix_length, family: self::FAMILY) ⇒ Boolean

Return true if the given number is a valid prefix length, false otherwise.

Parameters:

  • prefix_length (Integer)

Returns:

  • (Boolean)


138
139
140
# File 'lib/better_ipaddr/classes.rb', line 138

def self.valid_prefix_length?(prefix_length, family: self::FAMILY)
  0 <= prefix_length && prefix_length <= FAMILY_TO_BIT_LENGTH[family]
end

Instance Method Details

#address_family_bit_lengthObject



176
177
178
# File 'lib/better_ipaddr/classes.rb', line 176

def address_family_bit_length
  self.class::BIT_LENGTH
end

#inherited(cls) ⇒ Object



10
11
12
# File 'lib/better_ipaddr/classes.rb', line 10

def inherited(cls)
  cls.extend BetterIpaddr::ClassMethods
end

#network?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/better_ipaddr/classes.rb', line 180

def network?
  prefix_length < self.class::BIT_LENGTH
end

#prefix_lengthObject



184
185
186
# File 'lib/better_ipaddr/classes.rb', line 184

def prefix_length
  self.class::NETMASK_TO_PREFIX_LENGTH[mask_addr]
end