diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index 82546d6..c9e4a7f 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -1,9 +1,9 @@ require 'ipaddress/prefix' -module IPAddress; - # +module IPAddress; + # # =Name - # + # # IPAddress::IPv4 - IP version 4 address manipulation library # # =Synopsis @@ -11,19 +11,19 @@ module IPAddress; # require 'ipaddress' # # =Description - # - # Class IPAddress::IPv4 is used to handle IPv4 type addresses. + # + # Class IPAddress::IPv4 is used to handle IPv4 type addresses. # class IPv4 - + include IPAddress - include Enumerable - include Comparable - + include Enumerable + include Comparable + # # This Hash contains the prefix values for Classful networks # - # Note that classes C, D and E will all have a default + # Note that classes C, D and E will all have a default # prefix of /24 or 255.255.255.0 # CLASSFUL = { @@ -36,26 +36,26 @@ class IPv4 # Regular expression to match an IPv4 address # REGEXP = Regexp.new(/((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)/) - + # # Creates a new IPv4 address object. # # An IPv4 address can be expressed in any of the following forms: - # + # # * "10.1.1.1/24": ip +address+ and +prefix+. This is the common and # suggested way to create an object . # * "10.1.1.1/255.255.255.0": ip +address+ and +netmask+. Although # convenient sometimes, this format is less clear than the previous - # one. + # one. # * "10.1.1.1": if the address alone is specified, the prefix will be - # set as default 32, also known as the host prefix + # set as default 32, also known as the host prefix # # Examples: # # # These two are the same # ip = IPAddress::IPv4.new("10.0.0.1/24") # ip = IPAddress("10.0.0.1/24") - # + # # # These two are the same # IPAddress::IPv4.new "10.0.0.1/8" # IPAddress::IPv4.new "10.0.0.1/255.0.0.0" @@ -63,18 +63,18 @@ class IPv4 def initialize(str) raise ArgumentError, "Nil IP" unless str ip, netmask = str.split("/") - + # Check the ip and remove white space if IPAddress.valid_ipv4?(ip) @address = ip.strip else raise ArgumentError, "Invalid IP #{ip.inspect}" end - + # Check the netmask if netmask # netmask is defined netmask.strip! - if netmask =~ /^\d{1,2}$/ # netmask in cidr format + if netmask =~ /^\d{1,2}$/ # netmask in cidr format @prefix = Prefix32.new(netmask.to_i) elsif IPAddress.valid_ipv4_netmask?(netmask) # netmask in IP format @prefix = Prefix32.parse_netmask(netmask) @@ -89,7 +89,7 @@ def initialize(str) @octets = @address.split(".").map{|i| i.to_i} # 32 bits interger containing the address @u32 = (@octets[0]<< 24) + (@octets[1]<< 16) + (@octets[2]<< 8) + (@octets[3]) - + @allocator = 0 end # def initialize @@ -134,7 +134,7 @@ def prefix # # puts ip # #=> 172.16.100.4/16 - # + # # ip.prefix = 22 # # puts ip @@ -144,7 +144,7 @@ def prefix=(num) @prefix = Prefix32.new(num) end - # + # # Returns the address as an array of decimal values # # ip = IPAddress("172.16.100.4") @@ -155,9 +155,9 @@ def prefix=(num) def octets @octets end - + # - # Returns a string with the address portion of + # Returns a string with the address portion of # the IPv4 object # # ip = IPAddress("172.16.100.4/22") @@ -182,7 +182,7 @@ def to_string "#@address/#@prefix" end - # + # # Returns the prefix as a string in IP format # # ip = IPAddress("172.16.100.4/22") @@ -195,7 +195,7 @@ def netmask end # - # Like IPv4#prefix=, this method allow you to + # Like IPv4#prefix=, this method allow you to # change the prefix / netmask of an IP address # object. # @@ -218,8 +218,8 @@ def netmask=(addr) # 32 bits integer format. # # This method is identical to the C function - # inet_pton to create a 32 bits address family - # structure. + # inet_pton to create a 32 bits address family + # structure. # # ip = IPAddress("10.0.0.0/8") # @@ -231,9 +231,9 @@ def u32 end alias_method :to_i, :u32 alias_method :to_u32, :u32 - + # - # Returns the address portion in + # Returns the address portion in # hex # # ip = IPAddress("10.0.0.0") @@ -245,7 +245,7 @@ def hex(space=true) "%.4x%.4x" % [to_u32].pack("N").unpack("nn") end alias_method :to_h, :hex - alias_method :to_hex, :hex + alias_method :to_hex, :hex # # Returns the address portion of an IPv4 object @@ -261,8 +261,8 @@ def hex(space=true) # # a = Socket.open(params) # socket details here # ip = IPAddress("10.1.1.0/24") - # binary_data = ["Address: "].pack("a*") + ip.data - # + # binary_data = ["Address: "].pack("a*") + ip.data + # # # Send binary data # a.puts binary_data # @@ -295,7 +295,7 @@ def [](index) # ip = IPAddress("172.16.100.50/24") # ip[2] = 200 # - # #=> # # @prefix=32, @octets=[172, 16, 200, 1], @u32=2886780929> # def []=(index, value) @@ -303,7 +303,7 @@ def []=(index, value) initialize("#{@octets.join('.')}/#{prefix}") end alias_method :octet=, :[]= - + # # Returns the address portion of an IP in binary format, # as a string containing a sequence of 0 and 1 @@ -335,7 +335,7 @@ def broadcast return self end end - + # # Checks if the IP address is actually a network # @@ -343,7 +343,7 @@ def broadcast # # ip.network? # #=> false - # + # # ip = IPAddress("172.16.10.64/26") # # ip.network? @@ -354,7 +354,7 @@ def network? end # - # Returns a new IPv4 object with the network number + # Returns a new IPv4 object with the network number # for the given IP. # # ip = IPAddress("172.16.10.64/24") @@ -369,7 +369,7 @@ def network # # Returns a new IPv4 object with the # first host IP address in the range. - # + # # Example: given the 192.168.100.0/24 network, the first # host IP address is 192.168.100.1. # @@ -398,10 +398,10 @@ def first end # - # Like its sibling method IPv4#first, this method - # returns a new IPv4 object with the + # Like its sibling method IPv4#first, this method + # returns a new IPv4 object with the # last host IP address in the range. - # + # # Example: given the 192.168.100.0/24 network, the last # host IP address is 192.168.100.254 # @@ -482,15 +482,15 @@ def each # Spaceship operator to compare IPv4 objects # # Comparing IPv4 addresses is useful to ordinate - # them into lists that match our intuitive + # them into lists that match our intuitive # perception of ordered IP addresses. - # + # # The first comparison criteria is the u32 value. - # For example, 10.100.100.1 will be considered + # For example, 10.100.100.1 will be considered # to be less than 172.16.0.1, because, in a ordered list, # we expect 10.100.100.1 to come before 172.16.0.1. # - # The second criteria, in case two IPv4 objects + # The second criteria, in case two IPv4 objects # have identical addresses, is the prefix. An higher # prefix will be considered greater than a lower # prefix. This is because we expect to see @@ -512,11 +512,11 @@ def each # def <=>(oth) return nil unless oth.is_a?(self.class) - return prefix <=> oth.prefix if to_u32 == oth.to_u32 + return prefix <=> oth.prefix if to_u32 == oth.to_u32 to_u32 <=> oth.to_u32 end alias eql? == - + # # Returns the number of IP addresses included # in the network. It also counts the network @@ -534,7 +534,7 @@ def size # # Returns an array with the IP addresses of # all the hosts in the network. - # + # # ip = IPAddress("10.0.0.1/29") # # ip.hosts.map {|i| i.address} @@ -548,7 +548,7 @@ def size def hosts to_a[1..-2] end - + # # Returns the network number in Unsigned 32bits format # @@ -593,7 +593,7 @@ def include?(oth) end # - # Checks whether a subnet includes all the + # Checks whether a subnet includes all the # given IPv4 objects. # # ip = IPAddress("192.168.10.100/24") @@ -607,7 +607,7 @@ def include?(oth) def include_all?(*others) others.all? {|oth| include?(oth)} end - + # # Checks if an IPv4 address objects belongs # to a private network RFC1918 @@ -633,7 +633,7 @@ def private? # ip = IPAddress "224.0.0.0/4" # ip.multicast? # #=> true - # + # def multicast? [self.class.new("224.0.0.0/4")].any? {|i| i.include? self} end @@ -647,7 +647,7 @@ def multicast? # ip = IPAddress "127.0.0.1" # ip.loopback? # #=> true - # + # def loopback? [self.class.new("127.0.0.0/8")].any? {|i| i.include? self} end @@ -679,7 +679,7 @@ def reverse @octets.reverse.join(".") + ".in-addr.arpa" end alias_method :arpa, :reverse - + # # Return a list of IP's between @address # and the supplied IP @@ -708,7 +708,7 @@ def to(e) # method will calculate the network from the IP and then # subnet it. # - # If +subnets+ is an power of two number, the resulting + # If +subnets+ is an power of two number, the resulting # networks will be divided evenly from the supernet. # # network = IPAddress("172.16.10.0/24") @@ -719,7 +719,7 @@ def to(e) # #=> "172.16.10.128/26", # #=> "172.16.10.192/26"] # - # If +num+ is any other number, the supernet will be + # If +num+ is any other number, the supernet will be # divided into some networks with a even number of hosts and # other networks with the remaining addresses. # @@ -734,7 +734,7 @@ def to(e) # def split(subnets=2) unless (1..(2**@prefix.host_prefix)).include? subnets - raise ArgumentError, "Value #{subnets} out of range" + raise ArgumentError, "Value #{subnets} out of range" end networks = subnet(newprefix(subnets)) until networks.size == subnets @@ -775,7 +775,7 @@ def supernet(new_prefix) end # - # This method implements the subnetting function + # This method implements the subnetting function # similar to the one described in RFC3531. # # By specifying a new prefix, the method calculates @@ -790,7 +790,7 @@ def supernet(new_prefix) # we can calculate the subnets with a /26 prefix # # ip.subnet(26).map{&:to_string) - # #=> ["172.16.10.0/26", "172.16.10.64/26", + # #=> ["172.16.10.0/26", "172.16.10.64/26", # "172.16.10.128/26", "172.16.10.192/26"] # # The resulting number of subnets will of course always be @@ -808,7 +808,7 @@ def subnet(subprefix) # # Returns the difference between two IP addresses # in unsigned int 32 bits format - # + # # Example: # # ip1 = IPAddress("172.16.10.0/24") @@ -822,8 +822,8 @@ def -(oth) end # - # Returns a new IPv4 object which is the result - # of the summarization, if possible, of the two + # Returns a new IPv4 object which is the result + # of the summarization, if possible, of the two # objects # # Example: @@ -848,12 +848,12 @@ def +(oth) end # - # Checks whether the ip address belongs to a + # Checks whether the ip address belongs to a # RFC 791 CLASS A network, no matter # what the subnet mask is. # # Example: - # + # # ip = IPAddress("10.0.0.1/24") # # ip.a? @@ -862,7 +862,7 @@ def +(oth) def a? CLASSFUL.key(8) === bits end - + # # Checks whether the ip address belongs to a # RFC 791 CLASS B network, no matter @@ -898,7 +898,7 @@ def c? # # Return the ip address in a format compatible # with the IPv6 Mapped IPv4 addresses - # + # # Example: # # ip = IPAddress("172.16.10.1/24") @@ -934,10 +934,10 @@ def self.parse_u32(u32, prefix=32) # # Creates a new IPv4 object from binary data, # like the one you get from a network stream. - # + # # For example, on a network stream the IP 172.16.0.1 # is represented with the binary "\254\020\n\001". - # + # # ip = IPAddress::IPv4::parse_data "\254\020\n\001" # ip.prefix = 24 # @@ -949,7 +949,7 @@ def self.parse_data(str, prefix=32) end # - # Extract an IPv4 address from a string and + # Extract an IPv4 address from a string and # returns a new object # # Example: @@ -963,7 +963,7 @@ def self.parse_data(str, prefix=32) def self.extract(str) self.new REGEXP.match(str).to_s end - + # # Summarization (or aggregation) is the process when two or more # networks are taken together to check if a supernet, including all @@ -994,7 +994,7 @@ def self.extract(str) # # We note how the network "172.16.10.0/23" includes all the addresses # specified in the above networks, and (more important) includes - # ONLY those addresses. + # ONLY those addresses. # # If we summarized +ip1+ and +ip2+ with the following network: # @@ -1014,7 +1014,7 @@ def self.extract(str) # ip4 = IPAddress("10.0.3.1/24") # # IPAddress::IPv4::summarize(ip1,ip2,ip3,ip4).to_string - # #=> "10.0.0.0/22", + # #=> "10.0.0.0/22", # # But the following networks can't be summarized in a single network: # @@ -1029,7 +1029,7 @@ def self.extract(str) def self.summarize(*args) # one network? no need to summarize return [args.first.network] if args.size == 1 - + i = 0 result = args.dup.sort.map{|ip| ip.network} while i < result.size-1 @@ -1037,7 +1037,7 @@ def self.summarize(*args) result[i..i+1] = sum.first if sum.size == 1 i += 1 end - + result.flatten! if result.size == args.size # nothing more to summarize @@ -1049,10 +1049,10 @@ def self.summarize(*args) end # - # Creates a new IPv4 address object by parsing the + # Creates a new IPv4 address object by parsing the # address in a classful way. # - # Classful addresses have a fixed netmask based on the + # Classful addresses have a fixed netmask based on the # class they belong to: # # * Class A, from 0.0.0.0 to 127.255.255.255 @@ -1063,7 +1063,7 @@ def self.summarize(*args) # # ip = IPAddress::IPv4.parse_classful "10.0.0.1" # - # ip.netmask + # ip.netmask # #=> "255.0.0.0" # ip.a? # #=> true @@ -1120,7 +1120,7 @@ def allocate(skip=0) def newprefix(num) return @prefix + (Math::log2(num).ceil ) end - + def sum_first_found(arr) dup = arr.dup.reverse dup.each_with_index do |obj,i| diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb index 1601a01..49564a8 100644 --- a/lib/ipaddress/ipv6.rb +++ b/lib/ipaddress/ipv6.rb @@ -1,9 +1,9 @@ require 'ipaddress/prefix' -module IPAddress; - # +module IPAddress; + # # =Name - # + # # IPAddress::IPv6 - IP version 6 address manipulation library # # =Synopsis @@ -11,8 +11,8 @@ module IPAddress; # require 'ipaddress' # # =Description - # - # Class IPAddress::IPv6 is used to handle IPv6 type addresses. + # + # Class IPAddress::IPv6 is used to handle IPv6 type addresses. # # == IPv6 addresses # @@ -58,25 +58,25 @@ module IPAddress; # portion. # # - class IPv6 - + class IPv6 + include IPAddress - include Enumerable - include Comparable + include Enumerable + include Comparable + - # # Format string to pretty print IPv6 addresses # IN6FORMAT = ("%.4x:"*8).chop - + # # Creates a new IPv6 address object. # # An IPv6 address can be expressed in any of the following forms: - # + # # * "2001:0db8:0000:0000:0008:0800:200C:417A": IPv6 address with no compression - # * "2001:db8:0:0:8:800:200C:417A": IPv6 address with leading zeros compression + # * "2001:db8:0:0:8:800:200C:417A": IPv6 address with leading zeros compression # * "2001:db8::8:800:200C:417A": IPv6 address with full compression # # In all these 3 cases, a new IPv6 address object will be created, using the default @@ -93,7 +93,7 @@ def initialize(str) if str =~ /:.+\./ raise ArgumentError, "Please use #{self.class}::Mapped for IPv4 mapped addresses" end - + if IPAddress.valid_ipv6?(ip) @groups = self.class.groups(ip) @address = IN6FORMAT % @groups @@ -120,7 +120,7 @@ def address end # - # Returns an array with the 16 bits groups in decimal + # Returns an array with the 16 bits groups in decimal # format: # # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" @@ -132,7 +132,7 @@ def groups @groups end - # + # # Returns an instance of the prefix object # # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" @@ -165,8 +165,8 @@ def prefix=(num) @prefix = Prefix128.new(num) end - # - # Unlike its counterpart IPv6#to_string method, IPv6#to_string_uncompressed + # + # Unlike its counterpart IPv6#to_string method, IPv6#to_string_uncompressed # returns the whole IPv6 address and prefix in an uncompressed form # # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" @@ -263,8 +263,8 @@ def []=(index, value) end alias_method :group=, :[]= - # - # Returns a Base16 number representing the IPv6 + # + # Returns a Base16 number representing the IPv6 # address # # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" @@ -299,7 +299,7 @@ def data end # - # Returns an array of the 16 bits groups in hexdecimal + # Returns an array of the 16 bits groups in hexdecimal # format: # # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" @@ -316,9 +316,9 @@ def hexs # # Returns the IPv6 address in a DNS reverse lookup # string, as per RFC3172 and RFC2874. - # + # # ip6 = IPAddress "3ffe:505:2::f" - # + # # ip6.reverse # #=> "f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa" # @@ -348,7 +348,7 @@ def network_u128 # #=> 42540766411282592875350729025363378175 # # Please note that there is no Broadcast concept in IPv6 - # addresses as in IPv4 addresses, and this method is just + # addresses as in IPv4 addresses, and this method is just # an helper to other functions. # def broadcast_u128 @@ -406,18 +406,18 @@ def link_local? @groups[0] == 0xfe80 end - # + # # Returns true if the address is an unspecified address - # + # # See IPAddress::IPv6::Unspecified for more information # def unspecified? @prefix == 128 and @compressed == "::" end - # + # # Returns true if the address is a loopback address - # + # # See IPAddress::IPv6::Loopback for more information # def loopback? @@ -452,9 +452,9 @@ def unique_local? [self.class.new("fc00::/7")].any? {|i| i.include? self} end - # + # # Returns true if the address is a mapped address - # + # # See IPAddress::IPv6::Mapped for more information # def mapped? @@ -482,7 +482,7 @@ def mapped? # #=> "2001:db8::6" # #=> "2001:db8::7" # - # WARNING: if the host portion is very large, this method + # WARNING: if the host portion is very large, this method # can be very slow and possibly hang your system! # def each @@ -495,15 +495,15 @@ def each # Spaceship operator to compare IPv6 objects # # Comparing IPv6 addresses is useful to ordinate - # them into lists that match our intuitive + # them into lists that match our intuitive # perception of ordered IP addresses. - # + # # The first comparison criteria is the u128 value. - # For example, 2001:db8:1::1 will be considered + # For example, 2001:db8:1::1 will be considered # to be less than 2001:db8:2::1, because, in a ordered list, # we expect 2001:db8:1::1 to come before 2001:db8:2::1. # - # The second criteria, in case two IPv6 objects + # The second criteria, in case two IPv6 objects # have identical addresses, is the prefix. An higher # prefix will be considered greater than a lower # prefix. This is because we expect to see @@ -525,7 +525,7 @@ def each # def <=>(oth) return nil unless oth.is_a?(self.class) - return prefix <=> oth.prefix if to_u128 == oth.to_u128 + return prefix <=> oth.prefix if to_u128 == oth.to_u128 to_u128 <=> oth.to_u128 end alias eql? == @@ -536,13 +536,13 @@ def <=>(oth) # # ip6 = IPAddress("2001:db8::8:800:200c:417a") # - # ip6.bits + # ip6.bits # #=> "0010000000000001000011011011100000 [...] " # def bits data.unpack("B*").first end - + # # Expands an IPv6 address in the canocical form # @@ -563,23 +563,23 @@ def self.compress(str) self.new(str).compressed end - # + # # Literal version of the IPv6 address # # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" # # ip6.literal # #=> "2001-0db8-0000-0000-0008-0800-200c-417a.ipv6-literal.net" - # + # def literal @address.gsub(":","-") + ".ipv6-literal.net" end # - # Returns a new IPv6 object with the network number + # Returns a new IPv6 object with the network number # for the given IP. # - # ip = IPAddress "2001:db8:1:1:1:1:1:1/32" + # ip = IPAddress "2001:db8:1:1:1:1:1:1/32" # # ip.network.to_string # #=> "2001:db8::/32" @@ -603,9 +603,9 @@ def self.groups(str) # # Creates a new IPv6 object from binary data, # like the one you get from a network stream. - # - # For example, on a network stream the IP - # + # + # For example, on a network stream the IP + # # "2001:db8::8:800:200c:417a" # # is represented with the binary data @@ -696,7 +696,7 @@ def allocate(skip=0) end self.class.parse_u128(next_ip, @prefix) end - + private def compress_address @@ -713,7 +713,7 @@ def compress_address end str.sub(/:{3,}/, '::') end - + end # class IPv6 # @@ -772,7 +772,7 @@ def initialize @groups = Array.new(8,0) @prefix = Prefix128.new(128) @compressed = compress_address - end + end end # class IPv6::Unspecified # @@ -822,7 +822,7 @@ class IPAddress::IPv6::Loopback < IPAddress::IPv6 # def initialize @address = ("0000:"*7)+"0001" - @groups = Array.new(7,0).push(1) + @groups = Array.new(7,0).push(1) @prefix = Prefix128.new(128) @compressed = compress_address end @@ -901,7 +901,7 @@ class IPAddress::IPv6::Mapped < IPAddress::IPv6 # ipv6.ipv4.class # #=> IPAddress::IPv4 # - # An IPv6 IPv4-mapped address can also be created using the + # An IPv6 IPv4-mapped address can also be created using the # IPv6 only format of the address: # # ip6 = IPAddress::IPv6::Mapped.new "::0d01:4403" @@ -920,8 +920,8 @@ def initialize(str) super("::ffff:#{@ipv4.to_ipv6}/#{netmask}") end - # - # Similar to IPv6#to_s, but prints out the IPv4 address + # + # Similar to IPv6#to_s, but prints out the IPv4 address # in dotted decimal format # # ip6 = IPAddress "::ffff:172.16.10.1/128" @@ -933,8 +933,8 @@ def to_s "::ffff:#{@ipv4.address}" end - # - # Similar to IPv6#to_string, but prints out the IPv4 address + # + # Similar to IPv6#to_string, but prints out the IPv4 address # in dotted decimal format # # diff --git a/lib/ipaddress/prefix.rb b/lib/ipaddress/prefix.rb index e819800..e1d45c8 100644 --- a/lib/ipaddress/prefix.rb +++ b/lib/ipaddress/prefix.rb @@ -1,24 +1,24 @@ module IPAddress - + # # =NAME - # + # # IPAddress::Prefix # # =SYNOPSIS - # + # # Parent class for Prefix32 and Prefix128 # # =DESCRIPTION # - # IPAddress::Prefix is the parent class for IPAddress::Prefix32 + # IPAddress::Prefix is the parent class for IPAddress::Prefix32 # and IPAddress::Prefix128, defining some modules in common for # both the subclasses. # # IPAddress::Prefix shouldn't be accesses directly, unless # for particular needs. # - class Prefix + class Prefix include Comparable @@ -32,21 +32,21 @@ def initialize(num) end # - # Returns a string with the prefix + # Returns a string with the prefix # def to_s "#@prefix" end alias_method :inspect, :to_s - # + # # Returns the prefix # def to_i @prefix end - # + # # Compare the prefix # def <=>(oth) @@ -54,7 +54,7 @@ def <=>(oth) end # - # Sums two prefixes or a prefix to a + # Sums two prefixes or a prefix to a # number, returns a Integer # def +(oth) @@ -77,14 +77,14 @@ def -(oth) (self.prefix - oth.prefix).abs end end - + end # class Prefix class Prefix32 < Prefix IN4MASK = 0xffffffff - + # # Creates a new prefix object for 32 bits IPv4 addresses # @@ -100,7 +100,7 @@ def initialize(num) # # Returns the length of the host portion - # of a netmask. + # of a netmask. # # prefix = Prefix32.new 24 # @@ -110,14 +110,14 @@ def initialize(num) def host_prefix 32 - @prefix end - + # # Transforms the prefix into a string of bits # representing the netmask # # prefix = IPAddress::Prefix32.new 24 - # - # prefix.bits + # + # prefix.bits # #=> "11111111111111111111111100000000" # def bits @@ -125,7 +125,7 @@ def bits end # - # Gives the prefix in IPv4 dotted decimal format, + # Gives the prefix in IPv4 dotted decimal format, # i.e. the canonical netmask we're all used to # # prefix = IPAddress::Prefix32.new 24 @@ -138,8 +138,8 @@ def to_ip end # - # An array of octets of the IPv4 dotted decimal - # format + # An array of octets of the IPv4 dotted decimal + # format # # prefix = IPAddress::Prefix32.new 24 # @@ -162,9 +162,9 @@ def octets def to_u32 (IN4MASK >> host_prefix) << host_prefix end - + # - # Shortcut for the octecs in the dotted decimal + # Shortcut for the octecs in the dotted decimal # representation # # prefix = IPAddress::Prefix32.new 24 @@ -189,9 +189,9 @@ def [](index) def hostmask [~to_u32].pack("N").unpack("CCCC").join(".") end - + # - # Creates a new prefix by parsing a netmask in + # Creates a new prefix by parsing a netmask in # dotted decimal form # # prefix = IPAddress::Prefix32::parse_netmask "255.255.255.0" @@ -202,7 +202,7 @@ def self.parse_netmask(netmask) num = octets.pack("C"*octets.size).unpack("B*").first.count "1" return self.new(num) end - + end # class Prefix32 < Prefix class Prefix128 < Prefix @@ -249,7 +249,7 @@ def to_u128 # # Returns the length of the host portion - # of a netmask. + # of a netmask. # # prefix = Prefix128.new 96 # diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb index 5e8d5a9..1e5bd1f 100644 --- a/test/ipaddress/ipv4_test.rb +++ b/test/ipaddress/ipv4_test.rb @@ -1,5 +1,5 @@ require 'test_helper' - + class IPv4Test < Minitest::Test def setup @@ -11,7 +11,7 @@ def setup "10.0.0.1" => ["10.0.0.1", 32], "10.0.0.1/24" => ["10.0.0.1", 24], "10.0.0.1/255.255.255.0" => ["10.0.0.1", 24]} - + @invalid_ipv4 = ["10.0.0.256", "10.0.0.0.0", "10.0.0", @@ -29,7 +29,7 @@ def setup "192.168.100.4/30" => "255.255.255.252", "192.168.12.4/32" => "255.255.255.255"} - @decimal_values ={ + @decimal_values ={ "0.0.0.0/0" => 0, "10.0.0.0/8" => 167772160, "172.16.0.0/16" => 2886729728, @@ -44,7 +44,7 @@ def setup @ip = @klass.new("172.16.10.1/24") @network = @klass.new("172.16.10.0/24") - + @broadcast = { "10.0.0.0/8" => "10.255.255.255/8", "172.16.0.0/16" => "172.16.255.255/16", @@ -52,7 +52,7 @@ def setup "192.168.100.4/30" => "192.168.100.7/30", "192.168.12.3/31" => "255.255.255.255/31", "10.0.0.1/32" => "10.0.0.1/32"} - + @networks = { "10.5.4.3/8" => "10.0.0.0/8", "172.16.5.4/16" => "172.16.0.0/16", @@ -81,7 +81,7 @@ def setup "169.254.12.34", "169.254.0.0/16", "169.254.0.0/17"] - + @not_link_local = [ "127.0.0.1", "127.0.1.1", @@ -90,7 +90,7 @@ def setup "169.254.0.0/15", "0.0.0.0", "255.255.255.255"] - + end def test_initialize @@ -100,7 +100,7 @@ def test_initialize end assert_instance_of IPAddress::Prefix32, @ip.prefix assert_raises(ArgumentError) do - @klass.new + @klass.new end end @@ -130,7 +130,7 @@ def test_octets ip = @klass.new("10.1.2.3/8") assert_equal ip.octets, [10,1,2,3] end - + def test_initialize_should_require_ip assert_raises(ArgumentError) { @klass.new } end @@ -142,7 +142,7 @@ def test_method_data assert_equal "\xAC\x10\n\x01".b, @ip.data end end - + def test_method_to_string @valid_ipv4.each do |arg,attr| ip = @klass.new(arg) @@ -182,7 +182,7 @@ def test_method_network? assert_equal true, @network.network? assert_equal false, @ip.network? end - + def test_one_address_network network = @klass.new("172.16.10.1/32") assert_equal false, network.network? @@ -195,7 +195,7 @@ def test_method_broadcast assert_equal bcast, ip.broadcast.to_string end end - + def test_method_network @networks.each do |addr,net| ip = @klass.new addr @@ -238,7 +238,7 @@ def test_method_last assert_instance_of @klass, ip.last assert_equal "192.168.100.51", ip.last.to_s end - + def test_method_each_host ip = @klass.new("10.0.0.1/29") arr = [] @@ -273,7 +273,7 @@ def test_method_hosts def test_method_network_u32 assert_equal 2886732288, @ip.network_u32 end - + def test_method_broadcast_u32 assert_equal 2886732543, @ip.broadcast_u32 end @@ -292,13 +292,13 @@ def test_method_include? assert_equal false, ip.include?(@klass.new("5.5.5.5/32")) assert_equal false, ip.include?(@klass.new("11.0.0.0/8")) ip = @klass.new("13.13.0.0/13") - assert_equal false, ip.include?(@klass.new("13.16.0.0/32")) + assert_equal false, ip.include?(@klass.new("13.16.0.0/32")) end def test_method_include_all? ip = @klass.new("192.168.10.100/24") addr1 = @klass.new("192.168.10.102/24") - addr2 = @klass.new("192.168.10.103/24") + addr2 = @klass.new("192.168.10.103/24") assert_equal true, ip.include_all?(addr1,addr2) assert_equal false, ip.include_all?(addr1, @klass.new("13.16.0.0/32")) end @@ -306,11 +306,11 @@ def test_method_include_all? def test_method_ipv4? assert_equal true, @ip.ipv4? end - + def test_method_ipv6? assert_equal false, @ip.ipv6? end - + def test_method_private? assert_equal true, @klass.new("192.168.10.50/24").private? assert_equal true, @klass.new("192.168.10.50/16").private? @@ -363,11 +363,11 @@ def test_method_c? def test_method_to_ipv6 assert_equal "ac10:0a01", @ip.to_ipv6 end - + def test_method_reverse assert_equal "1.10.16.172.in-addr.arpa", @ip.reverse end - + def test_method_compare ip1 = @klass.new("10.1.1.1/8") ip2 = @klass.new("10.1.1.1/16") @@ -377,7 +377,7 @@ def test_method_compare # ip2 should be greater than ip1 assert_equal true, ip1 < ip2 assert_equal false, ip1 > ip2 - assert_equal false, ip2 < ip1 + assert_equal false, ip2 < ip1 # ip2 should be less than ip3 assert_equal true, ip2 < ip3 assert_equal false, ip2 > ip3 @@ -410,7 +410,7 @@ def test_method_compare def test_method_minus ip1 = @klass.new("10.1.1.1/8") - ip2 = @klass.new("10.1.1.10/8") + ip2 = @klass.new("10.1.1.10/8") assert_equal 9, ip2 - ip1 assert_equal 9, ip1 - ip2 end @@ -421,7 +421,7 @@ def test_method_plus assert_equal ["172.16.10.0/23"], (ip1+ip2).map{|i| i.to_string} ip2 = @klass.new("172.16.12.2/24") - assert_equal [ip1.network.to_string, ip2.network.to_string], + assert_equal [ip1.network.to_string, ip2.network.to_string], (ip1 + ip2).map{|i| i.to_string} ip1 = @klass.new("10.0.0.0/23") @@ -441,7 +441,7 @@ def test_method_plus assert_equal ["10.0.0.0/23","10.1.0.0/24"], (ip1+ip2).map{|i| i.to_string} end - + def test_method_netmask_equal ip = @klass.new("10.1.1.1/16") assert_equal 16, ip.prefix.to_i @@ -452,24 +452,24 @@ def test_method_netmask_equal def test_method_split assert_raises(ArgumentError) {@ip.split(0)} assert_raises(ArgumentError) {@ip.split(257)} - + assert_equal @ip.network, @ip.split(1).first - - arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", - "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", + + arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", + "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", "172.16.10.192/27", "172.16.10.224/27"] assert_equal arr, @network.split(8).map {|s| s.to_string} - arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", - "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", + arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", + "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", "172.16.10.192/26"] assert_equal arr, @network.split(7).map {|s| s.to_string} - arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", + arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", "172.16.10.96/27", "172.16.10.128/26", "172.16.10.192/26"] assert_equal arr, @network.split(6).map {|s| s.to_string} - arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", + arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", "172.16.10.96/27", "172.16.10.128/25"] assert_equal arr, @network.split(5).map {|s| s.to_string} - arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", + arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", "172.16.10.192/26"] assert_equal arr, @network.split(4).map {|s| s.to_string} arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/25"] @@ -483,7 +483,7 @@ def test_method_split def test_method_subnet assert_raises(ArgumentError) {@network.subnet(23)} assert_raises(ArgumentError) {@network.subnet(33)} - arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", + arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", "172.16.10.192/26"] assert_equal arr, @network.subnet(26).map {|s| s.to_string} arr = ["172.16.10.0/25", "172.16.10.128/25"] @@ -491,9 +491,9 @@ def test_method_subnet arr = ["172.16.10.0/24"] assert_equal arr, @network.subnet(24).map {|s| s.to_string} end - + def test_method_supernet - assert_raises(ArgumentError) {@ip.supernet(24)} + assert_raises(ArgumentError) {@ip.supernet(24)} assert_equal "0.0.0.0/0", @ip.supernet(0).to_string assert_equal "0.0.0.0/0", @ip.supernet(-2).to_string assert_equal "172.16.10.0/23", @ip.supernet(23).to_string @@ -514,7 +514,7 @@ def test_classhmethod_extract end def test_classmethod_summarize - + # Should return self if only one network given assert_equal [@ip.network], @klass.summarize(@ip) @@ -547,7 +547,7 @@ def test_classmethod_summarize ip2 = @klass.new("10.0.2.0/23") ip3 = @klass.new("10.0.4.0/24") ip4 = @klass.new("10.0.6.0/24") - assert_equal ["10.0.0.0/22","10.0.4.0/24","10.0.6.0/24"], + assert_equal ["10.0.0.0/22","10.0.4.0/24","10.0.6.0/24"], @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string} ip1 = @klass.new("10.0.1.1/24") @@ -573,12 +573,12 @@ def test_classmethod_summarize ips = [@klass.new("172.16.0.0/31"), @klass.new("10.10.2.1/32")] result = ["10.10.2.1/32", "172.16.0.0/31"] - assert_equal result, @klass.summarize(*ips).map{|i| i.to_string} - + assert_equal result, @klass.summarize(*ips).map{|i| i.to_string} + ips = [@klass.new("172.16.0.0/32"), @klass.new("10.10.2.1/32")] result = ["10.10.2.1/32", "172.16.0.0/32"] - assert_equal result, @klass.summarize(*ips).map{|i| i.to_string} + assert_equal result, @klass.summarize(*ips).map{|i| i.to_string} end @@ -597,10 +597,10 @@ def test_classmethod_parse_classful end assert_raises(ArgumentError){ @klass.parse_classful("192.168.256.257") } end - + def test_network_split @classful.each do |ip,net| - x = @klass.new("#{ip}/#{net}") + x = @klass.new("#{ip}/#{net}") assert_equal x.split(1).length, 1 assert_equal x.split(2).length, 2 assert_equal x.split(32).length, 32 @@ -611,7 +611,7 @@ def test_network_split def test_in_range @in_range.each do |s,d| ip = @klass.new(s) - assert_equal ip.to(d[0]).length, d[1] + assert_equal ip.to(d[0]).length, d[1] end end @@ -655,4 +655,4 @@ def test_allocate_will_raise_stopiteration end # class IPv4Test - + diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb index 294d5fa..cf1a712 100644 --- a/test/ipaddress/ipv6_test.rb +++ b/test/ipaddress/ipv6_test.rb @@ -1,11 +1,11 @@ require 'test_helper' - + class IPv6Test < Minitest::Test - + def setup @klass = IPAddress::IPv6 - - @compress_addr = { + + @compress_addr = { "2001:db8:0000:0000:0008:0800:200c:417a" => "2001:db8::8:800:200c:417a", "2001:db8:0:0:8:800:200c:417a" => "2001:db8::8:800:200c:417a", "ff01:0:0:0:0:0:0:101" => "ff01::101", @@ -27,7 +27,7 @@ def setup "0:0:0:0:0:0:0:0" => 0, "0:0:0::0:0:0" => 0, "::" => 0} - + @invalid_ipv6 = [":1:2:3:4:5:6:7", ":1:2:3:4:5:6:7", "2002:516:2:200", @@ -37,7 +37,7 @@ def setup "2001:db8:1:1:1:1:1:1/32" => "2001:db8::/32", "2001:db8:1:1:1:1:1::/32" => "2001:db8::/32", "2001:db8::1/64" => "2001:db8::/64"} - + @ip = @klass.new "2001:db8::8:800:200c:417a/64" @network = @klass.new "2001:db8:8:800::/64" @arr = [8193,3512,0,0,8,2048,8204,16762] @@ -49,7 +49,7 @@ def setup "fe80::208:74ff:feda:625c", "fe80::/64", "fe80::/65"] - + @not_link_local = [ "::", "::1", @@ -72,9 +72,9 @@ def setup "fe80::", "fe80::1", "fe80::/64"] - + end - + def test_attribute_address addr = "2001:0db8:0000:0000:0008:0800:200c:417a" assert_equal addr, @ip.address @@ -92,7 +92,7 @@ def test_initialize @klass.new "::10.1.1.1" } end - + def test_attribute_groups assert_equal @arr, @ip.groups end @@ -101,7 +101,7 @@ def test_method_hexs arr = "2001:0db8:0000:0000:0008:0800:200c:417a".split(":") assert_equal arr, @ip.hexs end - + def test_method_to_i @valid_ipv6.each do |ip,num| assert_equal num, @klass.new(ip).to_i @@ -110,7 +110,7 @@ def test_method_to_i def test_method_bits bits = "0010000000000001000011011011100000000000000000000" + - "000000000000000000000000000100000001000000000000010000" + + "000000000000000000000000000100000001000000000000010000" + "0000011000100000101111010" assert_equal bits, @ip.bits end @@ -143,7 +143,7 @@ def test_method_group def test_method_ipv4? assert_equal false, @ip.ipv4? end - + def test_method_ipv6? assert_equal true, @ip.ipv6? end @@ -179,7 +179,7 @@ def test_method_include? not_included = @klass.new "2001:db8::8:800:200c:417a/46" assert_equal true, @ip.include?(included) assert_equal false, @ip.include?(not_included) - # test address on same prefix + # test address on same prefix included = @klass.new "2001:db8::8:800:200c:0/64" not_included = @klass.new "2001:db8:1::8:800:200c:417a/64" assert_equal true, @ip.include?(included) @@ -190,11 +190,11 @@ def test_method_include? assert_equal true, @ip.include?(included) assert_equal false, @ip.include?(not_included) end - + def test_method_to_hex assert_equal @hex, @ip.to_hex end - + def test_method_to_s assert_equal "2001:db8::8:800:200c:417a", @ip.to_s end @@ -204,10 +204,10 @@ def test_method_to_string end def test_method_to_string_uncompressed - str = "2001:0db8:0000:0000:0008:0800:200c:417a/64" + str = "2001:0db8:0000:0000:0008:0800:200c:417a/64" assert_equal str, @ip.to_string_uncompressed end - + def test_method_data if RUBY_VERSION < "2.0" str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz" @@ -229,7 +229,7 @@ def test_method_compressed assert_equal "1::1:0:0:1", @klass.new("1:0:0:0:1:0:0:1").compressed assert_equal "1::1", @klass.new("1:0:0:0:0:0:0:1").compressed end - + def test_method_link_local? assert_equal true, @klass.new("fe80::1").link_local? assert_equal true, @klass.new("fe80:ffff::1").link_local? @@ -238,12 +238,12 @@ def test_method_link_local? def test_method_unspecified? assert_equal true, @klass.new("::").unspecified? - assert_equal false, @ip.unspecified? + assert_equal false, @ip.unspecified? end - + def test_method_loopback? assert_equal true, @klass.new("::1").loopback? - assert_equal false, @ip.loopback? + assert_equal false, @ip.loopback? end def test_method_link_local? @@ -315,7 +315,7 @@ def test_method_compare # ip2 should be greater than ip1 assert_equal true, ip2 > ip1 assert_equal false, ip1 > ip2 - assert_equal false, ip2 < ip1 + assert_equal false, ip2 < ip1 # ip3 should be less than ip2 assert_equal true, ip2 > ip3 assert_equal false, ip2 < ip3 @@ -353,7 +353,7 @@ def test_classmethod_expand refute_equal expanded, @klass.expand("2001:0db8::cd30") refute_equal expanded, @klass.expand("2001:0db8::cd3") end - + def test_classmethod_compress compressed = "2001:db8:0:cd30::" expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000" @@ -390,7 +390,7 @@ def test_group_updates end # class IPv6Test class IPv6UnspecifiedTest < Minitest::Test - + def setup @klass = IPAddress::IPv6::Unspecified @ip = @klass.new @@ -418,12 +418,12 @@ def test_attributes def test_method_ipv6? assert_equal true, @ip.ipv6? end - + end # class IPv6UnspecifiedTest class IPv6LoopbackTest < Minitest::Test - + def setup @klass = IPAddress::IPv6::Loopback @ip = @klass.new @@ -451,11 +451,11 @@ def test_attributes def test_method_ipv6? assert_equal true, @ip.ipv6? end - + end # class IPv6LoopbackTest class IPv6MappedTest < Minitest::Test - + def setup @klass = IPAddress::IPv6::Mapped @ip = @klass.new("::172.16.10.1") diff --git a/test/ipaddress/prefix_test.rb b/test/ipaddress/prefix_test.rb index f0a3d8c..e7f7dad 100644 --- a/test/ipaddress/prefix_test.rb +++ b/test/ipaddress/prefix_test.rb @@ -1,5 +1,5 @@ require 'test_helper' - + class Prefix32Test < Minitest::Test def setup @@ -9,7 +9,7 @@ def setup @netmask24 = "255.255.255.0" @netmask30 = "255.255.255.252" @netmasks = [@netmask0,@netmask8,@netmask16,@netmask24,@netmask30] - + @prefix_hash = { "0.0.0.0" => 0, "255.0.0.0" => 8, @@ -30,7 +30,7 @@ def setup 16 => 4294901760, 24 => 4294967040, 30 => 4294967292} - + @klass = IPAddress::Prefix32 end @@ -55,12 +55,12 @@ def test_method_to_ip assert_equal netmask, prefix.to_ip end end - + def test_method_to_s prefix = @klass.new(8) assert_equal "8", prefix.to_s end - + def test_method_bits prefix = @klass.new(16) str = "1"*16 + "0"*16 @@ -115,19 +115,19 @@ def test_method_hostmask prefix = @klass.new(8) assert_equal "0.255.255.255", prefix.hostmask end - + end # class Prefix32Test - + class Prefix128Test < Minitest::Test - + def setup @u128_hash = { 32 => 340282366841710300949110269838224261120, 64 => 340282366920938463444927863358058659840, 96 => 340282366920938463463374607427473244160, 126 => 340282366920938463463374607431768211452} - + @klass = IPAddress::Prefix128 end