Module: BetterIpaddr::InstanceMethods

Includes:
Constants
Included in:
IPAddr, IPAddr::Base
Defined in:
lib/better_ipaddr/methods.rb

Constant Summary

Constants included from Constants

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#familyInteger (readonly)

Return the magic number representing the address family.

Returns:

  • (Integer)


95
96
97
# File 'lib/better_ipaddr/methods.rb', line 95

def family
  @family
end

#mask_addrInteger (readonly)

Return the integer representation of the netmask.

Returns:

  • (Integer)


99
100
101
# File 'lib/better_ipaddr/methods.rb', line 99

def mask_addr
  @mask_addr
end

Instance Method Details

#+(offset) ⇒ IPAddr

Return the address greater than the original address by the given offset. Note that the result will always be a host address.

Parameters:

  • offset (Integer)

    the difference between the original address and the returned address

Returns:



114
115
116
# File 'lib/better_ipaddr/methods.rb', line 114

def +(offset)
  self.class.new(@addr + offset, family)
end

#-(offset) ⇒ IPAddr

Return the address less than the original address by the given offset. Note that the result will always be a host address.

Parameters:

  • offset (Integer)

    the difference between the original address and the returned address

Returns:



125
126
127
# File 'lib/better_ipaddr/methods.rb', line 125

def -(offset)
  self + (-offset)
end

#<=>(other) ⇒ Integer

Compare this address with the integer representation of another address of the same address family.

Parameters:

  • other (Integer)

Returns:

  • (Integer)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/better_ipaddr/methods.rb', line 141

def <=>(other)
  if other.is_a?(IPAddr)
    family_difference = family <=> other.family
    return family_difference unless family_difference == 0
  elsif !other.is_a?(Integer)
    fail ArgumentError, "Can't compare #{self.class} with #{other.class}"
  end

  address_difference = to_i <=> other.to_i

  if address_difference != 0 || !other.is_a?(IPAddr)
    return address_difference
  end

  other.instance_variable_get(:@mask_addr) <=> @mask_addr
end

#==(other) ⇒ Boolean

Test the equality of two IP addresses, or an IP address an integer representing an address in the same address family.

Parameters:

  • other (IPAddr, Integer)

    the address to compare with

Returns:

  • (Boolean)


163
164
165
166
# File 'lib/better_ipaddr/methods.rb', line 163

def ==(other)
  return false if other.nil?
  (self <=> other) == 0
end

#[](offset) ⇒ IPAddr

The address at the given offset relative to the network address of the network. A negative offset will be used to count backwards from the highest addresses within the network. desired address

Parameters:

  • offset (Integer)

    the index within the network of the

Returns:

  • (IPAddr)

    the address at the given index



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

def [](offset)
  offset2 = offset >= 0 ? offset : size + offset
  self.class[to_i + offset2, family: family]
end

#address_family_bit_lengthInteger

Returns the number of bits allowed by the address family. A more efficient form of this method is available to the specialized IPAddr child classes.

Returns:

  • (Integer)


186
187
188
# File 'lib/better_ipaddr/methods.rb', line 186

def address_family_bit_length
  FAMILY_TO_BIT_LENGTH.fetch(family)
end

#baseString

Returns a string representation of the address without a prefix length.

Returns:

  • (String)


194
195
196
# File 'lib/better_ipaddr/methods.rb', line 194

def base
  _to_string(@addr)
end

#cidrString

Return a string containing the CIDR representation of the address.

Returns:

  • (String)


202
203
204
205
# File 'lib/better_ipaddr/methods.rb', line 202

def cidr
  return _to_string(@addr) unless ipv4? || ipv6?
  "#{_to_string(@addr)}/#{prefixlen}"
end

#cover?(other) ⇒ Boolean

Test whether or not this address completely encloses the other address.

Returns:

  • (Boolean)


209
210
211
# File 'lib/better_ipaddr/methods.rb', line 209

def cover?(other)
  first <= other.first && other.last <= last
end

#eachEnumerator

Return an enumerator with the behavior described above.

Returns:

  • (Enumerator)


224
225
226
227
228
229
230
231
232
233
# File 'lib/better_ipaddr/methods.rb', line 224

def each
  if block_given?
    (0...size).each do |offset|
      yield self[offset]
    end
    self
  else
    enum_for(:each)
  end
end

#firstIPAddr

The first host address in the network.

Returns:



238
239
240
# File 'lib/better_ipaddr/methods.rb', line 238

def first
  self[0]
end

#grow(shift) ⇒ IPAddr

Return a new address with the prefix length reduced by the given amount. The new address will cover the original address.

Parameters:

  • shift (Integer)

    the decrease in the prefix length

Returns:



247
248
249
# File 'lib/better_ipaddr/methods.rb', line 247

def grow(shift)
  mask(prefix_length - shift)
end

#host?Boolean

Return true if the address represents a host (i.e., only one address).

Returns:

  • (Boolean)


253
254
255
# File 'lib/better_ipaddr/methods.rb', line 253

def host?
  prefix_length >= address_family_bit_length
end

#lastIPAddr Also known as: broadcast

Return the last address in the network, which by convention is the broadcast address in IP networks.

Returns:



261
262
263
# File 'lib/better_ipaddr/methods.rb', line 261

def last
  self[-1]
end

#netmaskString

Return the string representation of the netmask.

Returns:

  • (String)


103
104
105
# File 'lib/better_ipaddr/methods.rb', line 103

def netmask
  self.class[mask_addr].to_s
end

#overlap?(other) ⇒ Boolean

Test whether or not two networks have any addresses in common (i.e., if either entirely encloses the other).

Returns:

  • (Boolean)


270
271
272
# File 'lib/better_ipaddr/methods.rb', line 270

def overlap?(other)
  cover?(other) || other.cover?(self)
end

#prefix_lengthInteger Also known as: prefixlen

Return the prefix length. A more efficient form of this method is available to the specialized IPAddr child classes.

Returns:

  • (Integer)


279
280
281
# File 'lib/better_ipaddr/methods.rb', line 279

def prefix_length
  NETMASK_TO_PREFIX_LENGTH[family][mask_addr]
end

#shrink(shift) ⇒ Boolean

Return a new address with the prefix length increased by the given amount. The old address will cover the new address.

Parameters:

  • shift (Integer)

    the increase in the prefix length

Returns:

  • (Boolean)


290
291
292
# File 'lib/better_ipaddr/methods.rb', line 290

def shrink(shift)
  mask(prefix_length + shift)
end

#sizeInteger

Return the number of host addresses representable by the network given its size.

Returns:

  • (Integer)


298
299
300
# File 'lib/better_ipaddr/methods.rb', line 298

def size
  2**(address_family_bit_length - prefix_length)
end

#summarize_with(other) ⇒ IPAddr?

Returns a summary address if the two networks can be combined into a single network without covering any other networks. Returns nil if the two networks can’t be combined this way.

Returns:



307
308
309
310
311
312
313
314
315
316
317
# File 'lib/better_ipaddr/methods.rb', line 307

def summarize_with(other)
  if other.nil?
    nil
  elsif cover?(other)
    self
  elsif other.cover?(self)
    other
  elsif other.grow(1) == grow(1)
    grow(1)
  end
end

#to_rangeRange(IPAddr)

Return a range representing the network. A block can be given to specify a conversion procedure, for example to convert the first and last addresses to integers before building the range.

Returns:



324
325
326
327
328
329
330
# File 'lib/better_ipaddr/methods.rb', line 324

def to_range
  if block_given?
    (yield first)..(yield last)
  else
    first..last
  end
end

#wildcardIPAddr

Return a wildcard mask representing the network.

Returns:



335
336
337
# File 'lib/better_ipaddr/methods.rb', line 335

def wildcard
  _to_string(@mask_addr.to_i ^ (2**address_family_bit_length - 1))
end