Class: IPAddr::V4

Inherits:
Base show all
Defined in:
lib/better_ipaddr/classes.rb

Overview

An IPv4 address, 32 bits

Direct Known Subclasses

Host

Defined Under Namespace

Classes: Host

Constant Summary collapse

REGEX =
Regex::IPV4
NETWORK_CLASSES =
{
  new('0.0.0.0/1')   =>  8, # A
  new('128.0.0.0/2') => 16, # B
  new('192.0.0.0/3') => 24  # C
}.freeze

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

Instance Method Summary collapse

Methods inherited from Base

[], #address_family_bit_length, class_convert, class_converter, class_converter?, class_converters, from, from_integer, from_ipaddr, from_string, from_string_representable, host_from, integer_to_prefix_length, ipaddr_to_prefix_length, #network?, object_to_prefix_length, parse, #prefix_length, specialize, specialize_constants, string_to_prefix_length, #to_s, valid_prefix_length?

Methods included from BetterIpaddr::InstanceMethods

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

Methods inherited from IPAddr

Host

Instance Method Details

#classfulIPAddr::V4?

If the address falls in one of the address classes defined in rfc791, return a new IPAddr with the appropriate prefix length, otherwise return nil.

  • Class A: networks of 16,777,216 addresses each, from 0.0.0.0/8 to 127.0.0.0/8

  • Class B: networks of 65,537 addresses each, from 128.0.0.0/16 to 191.255.0.0/16

  • Class C: networks of 256 addresses each, from 192.0.0.0/24 to 223.255.255.0/24

Returns:



369
370
371
372
# File 'lib/better_ipaddr/classes.rb', line 369

def classful
  prefix_length = classful_prefix_length || return
  mask(prefix_length)
end

#classful_prefix_lengthInteger?

If the address falls in one of the address classes defined in rfc791, return the corresponding prefix length, otherwise return nil.

Returns:

  • (Integer, nil)


378
379
380
381
382
383
# File 'lib/better_ipaddr/classes.rb', line 378

def classful_prefix_length
  key = NETWORK_CLASSES.keys.find do |block|
    block.to_range(&:to_i).cover?(to_i)
  end
  NETWORK_CLASSES[key]
end