2002-12-23 12:07:49 -05:00
|
|
|
#
|
|
|
|
# ipaddr.rb - A class to manipulate an IP address
|
|
|
|
#
|
|
|
|
# Copyright (c) 2002 Hajimu UMEMOTO <ume@mahoroba.org>.
|
2012-06-02 05:18:32 -04:00
|
|
|
# Copyright (c) 2007, 2009, 2012 Akinori MUSHA <knu@iDaemons.org>.
|
2002-12-23 12:07:49 -05:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# You can redistribute and/or modify it under the same terms as Ruby.
|
|
|
|
#
|
|
|
|
# $Id$
|
2006-08-04 16:39:28 -04:00
|
|
|
#
|
2008-03-21 08:15:06 -04:00
|
|
|
# Contact:
|
|
|
|
# - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
|
|
|
|
#
|
2002-12-23 12:07:49 -05:00
|
|
|
# TODO:
|
|
|
|
# - scope_id support
|
2008-03-21 08:15:06 -04:00
|
|
|
#
|
2002-12-23 12:07:49 -05:00
|
|
|
require 'socket'
|
|
|
|
|
2006-08-04 16:39:28 -04:00
|
|
|
# IPAddr provides a set of methods to manipulate an IP address. Both IPv4 and
|
|
|
|
# IPv6 are supported.
|
|
|
|
#
|
|
|
|
# == Example
|
|
|
|
#
|
|
|
|
# require 'ipaddr'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2006-08-04 16:39:28 -04:00
|
|
|
# ipaddr1 = IPAddr.new "3ffe:505:2::1"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2011-05-18 20:07:25 -04:00
|
|
|
# p ipaddr1 #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2011-05-18 20:07:25 -04:00
|
|
|
# p ipaddr1.to_s #=> "3ffe:505:2::1"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2011-05-18 17:19:18 -04:00
|
|
|
# ipaddr2 = ipaddr1.mask(48) #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000>
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2011-05-18 20:07:25 -04:00
|
|
|
# p ipaddr2.to_s #=> "3ffe:505:2::"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2006-08-04 16:39:28 -04:00
|
|
|
# ipaddr3 = IPAddr.new "192.168.2.0/24"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2011-05-18 20:07:25 -04:00
|
|
|
# p ipaddr3 #=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
|
2006-08-04 16:39:28 -04:00
|
|
|
|
2002-12-23 12:07:49 -05:00
|
|
|
class IPAddr
|
|
|
|
|
2011-05-21 22:21:56 -04:00
|
|
|
# 32 bit mask for IPv4
|
2002-12-23 12:07:49 -05:00
|
|
|
IN4MASK = 0xffffffff
|
2011-05-21 22:21:56 -04:00
|
|
|
# 128 bit mask for IPv4
|
2002-12-23 12:07:49 -05:00
|
|
|
IN6MASK = 0xffffffffffffffffffffffffffffffff
|
2012-06-02 05:18:32 -04:00
|
|
|
# Format string for IPv6
|
2002-12-23 12:07:49 -05:00
|
|
|
IN6FORMAT = (["%.4x"] * 8).join(':')
|
|
|
|
|
2012-06-02 05:18:32 -04:00
|
|
|
# Regexp _internally_ used for parsing IPv4 address.
|
|
|
|
RE_IPV4ADDRLIKE = %r{
|
|
|
|
\A
|
|
|
|
(\d+) \. (\d+) \. (\d+) \. (\d+)
|
|
|
|
\z
|
|
|
|
}x
|
|
|
|
|
|
|
|
# Regexp _internally_ used for parsing IPv6 address.
|
|
|
|
RE_IPV6ADDRLIKE_FULL = %r{
|
|
|
|
\A
|
|
|
|
(?:
|
|
|
|
(?: [\da-f]{1,4} : ){7} [\da-f]{1,4}
|
|
|
|
|
|
|
|
|
( (?: [\da-f]{1,4} : ){6} )
|
|
|
|
(\d+) \. (\d+) \. (\d+) \. (\d+)
|
|
|
|
)
|
|
|
|
\z
|
|
|
|
}xi
|
|
|
|
|
|
|
|
# Regexp _internally_ used for parsing IPv6 address.
|
|
|
|
RE_IPV6ADDRLIKE_COMPRESSED = %r{
|
|
|
|
\A
|
|
|
|
( (?: (?: [\da-f]{1,4} : )* [\da-f]{1,4} )? )
|
|
|
|
::
|
|
|
|
( (?:
|
|
|
|
( (?: [\da-f]{1,4} : )* )
|
|
|
|
(?:
|
|
|
|
[\da-f]{1,4}
|
|
|
|
|
|
|
|
|
(\d+) \. (\d+) \. (\d+) \. (\d+)
|
|
|
|
)
|
|
|
|
)? )
|
|
|
|
\z
|
|
|
|
}xi
|
|
|
|
|
2012-08-31 03:30:22 -04:00
|
|
|
# Generic IPAddr related error. Exceptions raised in this class should
|
|
|
|
# inherit from Error.
|
|
|
|
class Error < ArgumentError; end
|
|
|
|
|
|
|
|
# Raised when the provided IP address is an invalid address.
|
|
|
|
class InvalidAddressError < Error; end
|
|
|
|
|
|
|
|
# Raised when the address family is invalid such as an address with an
|
|
|
|
# unsupported family, an address with an inconsistent family, or an address
|
|
|
|
# who's family cannot be determined.
|
|
|
|
class AddressFamilyError < Error; end
|
|
|
|
|
|
|
|
# Raised when the address is an invalid length.
|
|
|
|
class InvalidPrefixError < InvalidAddressError; end
|
|
|
|
|
2002-12-23 12:07:49 -05:00
|
|
|
# Returns the address family of this IP address.
|
2006-07-20 13:36:36 -04:00
|
|
|
attr_reader :family
|
2002-12-23 12:07:49 -05:00
|
|
|
|
|
|
|
# Creates a new ipaddr containing the given network byte ordered
|
|
|
|
# string form of an IP address.
|
|
|
|
def IPAddr::new_ntoh(addr)
|
|
|
|
return IPAddr.new(IPAddr::ntop(addr))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert a network byte ordered string form of an IP address into
|
|
|
|
# human readable form.
|
|
|
|
def IPAddr::ntop(addr)
|
|
|
|
case addr.size
|
|
|
|
when 4
|
|
|
|
s = addr.unpack('C4').join('.')
|
|
|
|
when 16
|
|
|
|
s = IN6FORMAT % addr.unpack('n8')
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a new ipaddr built by bitwise AND.
|
|
|
|
def &(other)
|
2007-10-04 14:23:22 -04:00
|
|
|
return self.clone.set(@addr & coerce_other(other).to_i)
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a new ipaddr built by bitwise OR.
|
|
|
|
def |(other)
|
2007-10-04 14:23:22 -04:00
|
|
|
return self.clone.set(@addr | coerce_other(other).to_i)
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a new ipaddr built by bitwise right-shift.
|
|
|
|
def >>(num)
|
|
|
|
return self.clone.set(@addr >> num)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a new ipaddr built by bitwise left shift.
|
|
|
|
def <<(num)
|
|
|
|
return self.clone.set(addr_mask(@addr << num))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a new ipaddr built by bitwise negation.
|
|
|
|
def ~
|
|
|
|
return self.clone.set(addr_mask(~@addr))
|
|
|
|
end
|
|
|
|
|
2007-10-04 14:23:22 -04:00
|
|
|
# Returns true if two ipaddrs are equal.
|
2002-12-23 12:07:49 -05:00
|
|
|
def ==(other)
|
2007-10-04 14:23:22 -04:00
|
|
|
other = coerce_other(other)
|
|
|
|
return @family == other.family && @addr == other.to_i
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a new ipaddr built by masking IP address with the given
|
|
|
|
# prefixlen/netmask. (e.g. 8, 64, "255.255.255.0", etc.)
|
|
|
|
def mask(prefixlen)
|
|
|
|
return self.clone.mask!(prefixlen)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the given ipaddr is in the range.
|
|
|
|
#
|
|
|
|
# e.g.:
|
|
|
|
# require 'ipaddr'
|
|
|
|
# net1 = IPAddr.new("192.168.2.0/24")
|
2007-10-04 14:23:22 -04:00
|
|
|
# net2 = IPAddr.new("192.168.2.100")
|
|
|
|
# net3 = IPAddr.new("192.168.3.0")
|
2011-05-18 20:07:25 -04:00
|
|
|
# p net1.include?(net2) #=> true
|
|
|
|
# p net1.include?(net3) #=> false
|
2002-12-23 12:07:49 -05:00
|
|
|
def include?(other)
|
2007-10-04 14:23:22 -04:00
|
|
|
other = coerce_other(other)
|
2002-12-23 12:07:49 -05:00
|
|
|
if ipv4_mapped?
|
|
|
|
if (@mask_addr >> 32) != 0xffffffffffffffffffffffff
|
2011-05-18 17:19:18 -04:00
|
|
|
return false
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
mask_addr = (@mask_addr & IN4MASK)
|
|
|
|
addr = (@addr & IN4MASK)
|
|
|
|
family = Socket::AF_INET
|
|
|
|
else
|
|
|
|
mask_addr = @mask_addr
|
|
|
|
addr = @addr
|
|
|
|
family = @family
|
|
|
|
end
|
2007-10-04 14:23:22 -04:00
|
|
|
if other.ipv4_mapped?
|
|
|
|
other_addr = (other.to_i & IN4MASK)
|
|
|
|
other_family = Socket::AF_INET
|
|
|
|
else
|
|
|
|
other_addr = other.to_i
|
|
|
|
other_family = other.family
|
2003-04-24 02:08:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if family != other_family
|
|
|
|
return false
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
return ((addr & mask_addr) == (other_addr & mask_addr))
|
|
|
|
end
|
|
|
|
alias === include?
|
|
|
|
|
|
|
|
# Returns the integer representation of the ipaddr.
|
|
|
|
def to_i
|
|
|
|
return @addr
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a string containing the IP address representation.
|
|
|
|
def to_s
|
2003-12-18 01:38:30 -05:00
|
|
|
str = to_string
|
|
|
|
return str if ipv4?
|
|
|
|
|
|
|
|
str.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1')
|
|
|
|
loop do
|
2012-06-02 05:18:32 -04:00
|
|
|
break if str.sub!(/\A0:0:0:0:0:0:0:0\z/, '::')
|
2003-12-18 01:38:30 -05:00
|
|
|
break if str.sub!(/\b0:0:0:0:0:0:0\b/, ':')
|
|
|
|
break if str.sub!(/\b0:0:0:0:0:0\b/, ':')
|
|
|
|
break if str.sub!(/\b0:0:0:0:0\b/, ':')
|
|
|
|
break if str.sub!(/\b0:0:0:0\b/, ':')
|
|
|
|
break if str.sub!(/\b0:0:0\b/, ':')
|
|
|
|
break if str.sub!(/\b0:0\b/, ':')
|
|
|
|
break
|
|
|
|
end
|
2005-01-19 04:05:50 -05:00
|
|
|
str.sub!(/:{3,}/, '::')
|
2003-12-18 01:38:30 -05:00
|
|
|
|
2012-06-02 05:18:32 -04:00
|
|
|
if /\A::(ffff:)?([\da-f]{1,4}):([\da-f]{1,4})\z/i =~ str
|
2003-12-18 01:38:30 -05:00
|
|
|
str = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256)
|
|
|
|
end
|
|
|
|
|
|
|
|
str
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a string containing the IP address representation in
|
|
|
|
# canonical form.
|
|
|
|
def to_string
|
|
|
|
return _to_string(@addr)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a network byte ordered string form of the IP address.
|
|
|
|
def hton
|
|
|
|
case @family
|
|
|
|
when Socket::AF_INET
|
|
|
|
return [@addr].pack('N')
|
|
|
|
when Socket::AF_INET6
|
|
|
|
return (0..7).map { |i|
|
2011-05-18 17:19:18 -04:00
|
|
|
(@addr >> (112 - 16 * i)) & 0xffff
|
2002-12-23 12:07:49 -05:00
|
|
|
}.pack('n8')
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the ipaddr is an IPv4 address.
|
|
|
|
def ipv4?
|
|
|
|
return @family == Socket::AF_INET
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the ipaddr is an IPv6 address.
|
|
|
|
def ipv6?
|
|
|
|
return @family == Socket::AF_INET6
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the ipaddr is an IPv4-mapped IPv6 address.
|
|
|
|
def ipv4_mapped?
|
|
|
|
return ipv6? && (@addr >> 32) == 0xffff
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the ipaddr is an IPv4-compatible IPv6 address.
|
|
|
|
def ipv4_compat?
|
|
|
|
if !ipv6? || (@addr >> 32) != 0
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
a = (@addr & IN4MASK)
|
|
|
|
return a != 0 && a != 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a new ipaddr built by converting the native IPv4 address
|
|
|
|
# into an IPv4-mapped IPv6 address.
|
|
|
|
def ipv4_mapped
|
|
|
|
if !ipv4?
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidAddressError, "not an IPv4 address"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
return self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a new ipaddr built by converting the native IPv4 address
|
|
|
|
# into an IPv4-compatible IPv6 address.
|
|
|
|
def ipv4_compat
|
|
|
|
if !ipv4?
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidAddressError, "not an IPv4 address"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
return self.clone.set(@addr, Socket::AF_INET6)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a new ipaddr built by converting the IPv6 address into a
|
|
|
|
# native IPv4 address. If the IP address is not an IPv4-mapped or
|
|
|
|
# IPv4-compatible IPv6 address, returns self.
|
|
|
|
def native
|
|
|
|
if !ipv4_mapped? && !ipv4_compat?
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
return self.clone.set(@addr & IN4MASK, Socket::AF_INET)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a string for DNS reverse lookup. It returns a string in
|
|
|
|
# RFC3172 form for an IPv6 address.
|
|
|
|
def reverse
|
|
|
|
case @family
|
|
|
|
when Socket::AF_INET
|
|
|
|
return _reverse + ".in-addr.arpa"
|
|
|
|
when Socket::AF_INET6
|
|
|
|
return ip6_arpa
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a string for DNS reverse lookup compatible with RFC3172.
|
|
|
|
def ip6_arpa
|
|
|
|
if !ipv6?
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidAddressError, "not an IPv6 address"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
return _reverse + ".ip6.arpa"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a string for DNS reverse lookup compatible with RFC1886.
|
|
|
|
def ip6_int
|
|
|
|
if !ipv6?
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidAddressError, "not an IPv6 address"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
return _reverse + ".ip6.int"
|
|
|
|
end
|
|
|
|
|
2007-10-04 14:56:32 -04:00
|
|
|
# Returns the successor to the ipaddr.
|
|
|
|
def succ
|
|
|
|
return self.clone.set(@addr + 1, @family)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Compares the ipaddr with another.
|
|
|
|
def <=>(other)
|
|
|
|
other = coerce_other(other)
|
|
|
|
|
|
|
|
return nil if other.family != @family
|
|
|
|
|
|
|
|
return @addr <=> other.to_i
|
|
|
|
end
|
|
|
|
include Comparable
|
|
|
|
|
2009-08-04 02:10:42 -04:00
|
|
|
# Checks equality used by Hash.
|
|
|
|
def eql?(other)
|
|
|
|
return self.class == other.class && self.hash == other.hash && self == other
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a hash value used by Hash, Set, and Array classes
|
|
|
|
def hash
|
2009-08-05 11:13:07 -04:00
|
|
|
return ([@addr, @mask_addr].hash << 1) | (ipv4? ? 0 : 1)
|
2009-08-04 02:10:42 -04:00
|
|
|
end
|
|
|
|
|
2007-10-04 14:56:32 -04:00
|
|
|
# Creates a Range object for the network address.
|
|
|
|
def to_range
|
|
|
|
begin_addr = (@addr & @mask_addr)
|
|
|
|
|
|
|
|
case @family
|
|
|
|
when Socket::AF_INET
|
|
|
|
end_addr = (@addr | (IN4MASK ^ @mask_addr))
|
|
|
|
when Socket::AF_INET6
|
|
|
|
end_addr = (@addr | (IN6MASK ^ @mask_addr))
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2007-10-04 14:56:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return clone.set(begin_addr, @family)..clone.set(end_addr, @family)
|
|
|
|
end
|
|
|
|
|
2002-12-23 12:07:49 -05:00
|
|
|
# Returns a string containing a human-readable representation of the
|
|
|
|
# ipaddr. ("#<IPAddr: family:address/mask>")
|
|
|
|
def inspect
|
|
|
|
case @family
|
|
|
|
when Socket::AF_INET
|
|
|
|
af = "IPv4"
|
|
|
|
when Socket::AF_INET6
|
|
|
|
af = "IPv6"
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
return sprintf("#<%s: %s:%s/%s>", self.class.name,
|
2011-05-18 17:19:18 -04:00
|
|
|
af, _to_string(@addr), _to_string(@mask_addr))
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-05-21 22:21:56 -04:00
|
|
|
# Set +@addr+, the internal stored ip address, to given +addr+. The
|
|
|
|
# parameter +addr+ is validated using the first +family+ member,
|
|
|
|
# which is +Socket::AF_INET+ or +Socket::AF_INET6+.
|
2002-12-23 12:07:49 -05:00
|
|
|
def set(addr, *family)
|
|
|
|
case family[0] ? family[0] : @family
|
|
|
|
when Socket::AF_INET
|
|
|
|
if addr < 0 || addr > IN4MASK
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidAddressError, "invalid address"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
when Socket::AF_INET6
|
|
|
|
if addr < 0 || addr > IN6MASK
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidAddressError, "invalid address"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
@addr = addr
|
|
|
|
if family[0]
|
|
|
|
@family = family[0]
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2012-08-21 00:50:18 -04:00
|
|
|
# Set current netmask to given mask.
|
2002-12-23 12:07:49 -05:00
|
|
|
def mask!(mask)
|
|
|
|
if mask.kind_of?(String)
|
|
|
|
if mask =~ /^\d+$/
|
2011-05-18 17:19:18 -04:00
|
|
|
prefixlen = mask.to_i
|
2002-12-23 12:07:49 -05:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
m = IPAddr.new(mask)
|
|
|
|
if m.family != @family
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidPrefixError, "address family is not same"
|
2011-05-18 17:19:18 -04:00
|
|
|
end
|
|
|
|
@mask_addr = m.to_i
|
|
|
|
@addr &= @mask_addr
|
|
|
|
return self
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
prefixlen = mask
|
|
|
|
end
|
|
|
|
case @family
|
|
|
|
when Socket::AF_INET
|
|
|
|
if prefixlen < 0 || prefixlen > 32
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidPrefixError, "invalid length"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
masklen = 32 - prefixlen
|
|
|
|
@mask_addr = ((IN4MASK >> masklen) << masklen)
|
|
|
|
when Socket::AF_INET6
|
|
|
|
if prefixlen < 0 || prefixlen > 128
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidPrefixError, "invalid length"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
masklen = 128 - prefixlen
|
|
|
|
@mask_addr = ((IN6MASK >> masklen) << masklen)
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
@addr = ((@addr >> masklen) << masklen)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2007-10-04 14:23:22 -04:00
|
|
|
# Creates a new ipaddr object either from a human readable IP
|
|
|
|
# address representation in string, or from a packed in_addr value
|
|
|
|
# followed by an address family.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2007-10-04 14:23:22 -04:00
|
|
|
# In the former case, the following are the valid formats that will
|
|
|
|
# be recognized: "address", "address/prefixlen" and "address/mask",
|
|
|
|
# where IPv6 address may be enclosed in square brackets (`[' and
|
|
|
|
# `]'). If a prefixlen or a mask is specified, it returns a masked
|
|
|
|
# IP address. Although the address family is determined
|
|
|
|
# automatically from a specified string, you can specify one
|
|
|
|
# explicitly by the optional second argument.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2010-03-19 23:30:59 -04:00
|
|
|
# Otherwise an IP address is generated from a packed in_addr value
|
2007-10-04 14:23:22 -04:00
|
|
|
# and an address family.
|
2002-12-23 12:07:49 -05:00
|
|
|
#
|
2007-10-04 14:23:22 -04:00
|
|
|
# The IPAddr class defines many methods and operators, and some of
|
|
|
|
# those, such as &, |, include? and ==, accept a string, or a packed
|
|
|
|
# in_addr value instead of an IPAddr object.
|
2002-12-23 12:07:49 -05:00
|
|
|
def initialize(addr = '::', family = Socket::AF_UNSPEC)
|
|
|
|
if !addr.kind_of?(String)
|
2007-10-04 14:23:22 -04:00
|
|
|
case family
|
|
|
|
when Socket::AF_INET, Socket::AF_INET6
|
|
|
|
set(addr.to_i, family)
|
|
|
|
@mask_addr = (family == Socket::AF_INET) ? IN4MASK : IN6MASK
|
|
|
|
return
|
|
|
|
when Socket::AF_UNSPEC
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "address family must be specified"
|
2007-10-04 14:23:22 -04:00
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family: #{family}"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
prefix, prefixlen = addr.split('/')
|
|
|
|
if prefix =~ /^\[(.*)\]$/i
|
|
|
|
prefix = $1
|
|
|
|
family = Socket::AF_INET6
|
|
|
|
end
|
|
|
|
# It seems AI_NUMERICHOST doesn't do the job.
|
|
|
|
#Socket.getaddrinfo(left, nil, Socket::AF_INET6, Socket::SOCK_STREAM, nil,
|
2011-05-18 20:07:25 -04:00
|
|
|
# Socket::AI_NUMERICHOST)
|
2002-12-23 12:07:49 -05:00
|
|
|
@addr = @family = nil
|
|
|
|
if family == Socket::AF_UNSPEC || family == Socket::AF_INET
|
|
|
|
@addr = in_addr(prefix)
|
|
|
|
if @addr
|
2011-05-18 17:19:18 -04:00
|
|
|
@family = Socket::AF_INET
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if !@addr && (family == Socket::AF_UNSPEC || family == Socket::AF_INET6)
|
|
|
|
@addr = in6_addr(prefix)
|
|
|
|
@family = Socket::AF_INET6
|
|
|
|
end
|
|
|
|
if family != Socket::AF_UNSPEC && @family != family
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "address family mismatch"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
if prefixlen
|
|
|
|
mask!(prefixlen)
|
|
|
|
else
|
2008-07-07 04:12:23 -04:00
|
|
|
@mask_addr = (@family == Socket::AF_INET) ? IN4MASK : IN6MASK
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-10-04 14:23:22 -04:00
|
|
|
def coerce_other(other)
|
|
|
|
case other
|
|
|
|
when IPAddr
|
|
|
|
other
|
|
|
|
when String
|
|
|
|
self.class.new(other)
|
|
|
|
else
|
|
|
|
self.class.new(other, @family)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-12-23 12:07:49 -05:00
|
|
|
def in_addr(addr)
|
2012-06-02 05:18:32 -04:00
|
|
|
case addr
|
|
|
|
when Array
|
|
|
|
octets = addr
|
|
|
|
else
|
|
|
|
m = RE_IPV4ADDRLIKE.match(addr) or return nil
|
|
|
|
octets = m.captures
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
2012-06-02 05:18:32 -04:00
|
|
|
octets.inject(0) { |i, s|
|
2012-08-31 03:30:22 -04:00
|
|
|
(n = s.to_i) < 256 or raise InvalidAddressError, "invalid address"
|
|
|
|
s.match(/\A0./) and raise InvalidAddressError, "zero-filled number in IPv4 address is ambiguous"
|
2012-06-02 05:18:32 -04:00
|
|
|
i << 8 | n
|
|
|
|
}
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def in6_addr(left)
|
|
|
|
case left
|
2012-06-02 05:18:32 -04:00
|
|
|
when RE_IPV6ADDRLIKE_FULL
|
|
|
|
if $2
|
|
|
|
addr = in_addr($~[2,4])
|
|
|
|
left = $1 + ':'
|
|
|
|
else
|
|
|
|
addr = 0
|
|
|
|
end
|
2002-12-23 12:07:49 -05:00
|
|
|
right = ''
|
2012-06-02 05:18:32 -04:00
|
|
|
when RE_IPV6ADDRLIKE_COMPRESSED
|
|
|
|
if $4
|
2012-08-31 03:30:22 -04:00
|
|
|
left.count(':') <= 6 or raise InvalidAddressError, "invalid address"
|
2012-06-02 05:18:32 -04:00
|
|
|
addr = in_addr($~[4,4])
|
|
|
|
left = $1
|
|
|
|
right = $3 + '0:0'
|
|
|
|
else
|
2013-02-18 02:31:17 -05:00
|
|
|
left.count(':') <= ($1.empty? || $2.empty? ? 8 : 7) or
|
2013-02-17 22:45:21 -05:00
|
|
|
raise InvalidAddressError, "invalid address"
|
2012-06-02 05:18:32 -04:00
|
|
|
left = $1
|
|
|
|
right = $2
|
|
|
|
addr = 0
|
|
|
|
end
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise InvalidAddressError, "invalid address"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
l = left.split(':')
|
|
|
|
r = right.split(':')
|
|
|
|
rest = 8 - l.size - r.size
|
|
|
|
if rest < 0
|
|
|
|
return nil
|
|
|
|
end
|
2012-06-02 05:18:32 -04:00
|
|
|
(l + Array.new(rest, '0') + r).inject(0) { |i, s|
|
2007-10-04 15:03:56 -04:00
|
|
|
i << 16 | s.hex
|
2012-06-02 05:18:32 -04:00
|
|
|
} | addr
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def addr_mask(addr)
|
|
|
|
case @family
|
|
|
|
when Socket::AF_INET
|
2007-10-04 15:03:56 -04:00
|
|
|
return addr & IN4MASK
|
2002-12-23 12:07:49 -05:00
|
|
|
when Socket::AF_INET6
|
2007-10-04 15:03:56 -04:00
|
|
|
return addr & IN6MASK
|
2002-12-23 12:07:49 -05:00
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _reverse
|
|
|
|
case @family
|
|
|
|
when Socket::AF_INET
|
|
|
|
return (0..3).map { |i|
|
2011-05-18 17:19:18 -04:00
|
|
|
(@addr >> (8 * i)) & 0xff
|
2002-12-23 12:07:49 -05:00
|
|
|
}.join('.')
|
|
|
|
when Socket::AF_INET6
|
|
|
|
return ("%.32x" % @addr).reverse!.gsub!(/.(?!$)/, '\&.')
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _to_string(addr)
|
|
|
|
case @family
|
|
|
|
when Socket::AF_INET
|
|
|
|
return (0..3).map { |i|
|
2011-05-18 17:19:18 -04:00
|
|
|
(addr >> (24 - 8 * i)) & 0xff
|
2002-12-23 12:07:49 -05:00
|
|
|
}.join('.')
|
|
|
|
when Socket::AF_INET6
|
|
|
|
return (("%.32x" % addr).gsub!(/.{4}(?!$)/, '\&:'))
|
|
|
|
else
|
2012-08-31 03:30:22 -04:00
|
|
|
raise AddressFamilyError, "unsupported address family"
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-11-16 11:55:29 -05:00
|
|
|
unless Socket.const_defined? :AF_INET6
|
2012-06-02 05:18:32 -04:00
|
|
|
class Socket < BasicSocket
|
|
|
|
# IPv6 protocol family
|
|
|
|
AF_INET6 = Object.new
|
|
|
|
end
|
|
|
|
|
|
|
|
class << IPSocket
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid_v6?(addr)
|
|
|
|
case addr
|
|
|
|
when IPAddr::RE_IPV6ADDRLIKE_FULL
|
|
|
|
if $2
|
|
|
|
$~[2,4].all? {|i| i.to_i < 256 }
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
when IPAddr::RE_IPV6ADDRLIKE_COMPRESSED
|
|
|
|
if $4
|
|
|
|
addr.count(':') <= 6 && $~[4,4].all? {|i| i.to_i < 256}
|
|
|
|
else
|
|
|
|
addr.count(':') <= 7
|
|
|
|
end
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias getaddress_orig getaddress
|
|
|
|
|
|
|
|
public
|
|
|
|
|
|
|
|
# Returns a +String+ based representation of a valid DNS hostname,
|
|
|
|
# IPv4 or IPv6 address.
|
2012-08-21 00:50:18 -04:00
|
|
|
#
|
2012-06-02 05:18:32 -04:00
|
|
|
# IPSocket.getaddress 'localhost' #=> "::1"
|
|
|
|
# IPSocket.getaddress 'broadcasthost' #=> "255.255.255.255"
|
|
|
|
# IPSocket.getaddress 'www.ruby-lang.org' #=> "221.186.184.68"
|
|
|
|
# IPSocket.getaddress 'www.ccc.de' #=> "2a00:1328:e102:ccc0::122"
|
|
|
|
def getaddress(s)
|
|
|
|
if valid_v6?(s)
|
|
|
|
s
|
|
|
|
else
|
|
|
|
getaddress_orig(s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-12-23 12:07:49 -05:00
|
|
|
if $0 == __FILE__
|
|
|
|
eval DATA.read, nil, $0, __LINE__+4
|
|
|
|
end
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TC_IPAddr < Test::Unit::TestCase
|
|
|
|
def test_s_new
|
2012-06-02 05:18:32 -04:00
|
|
|
[
|
|
|
|
["3FFE:505:ffff::/48"],
|
|
|
|
["0:0:0:1::"],
|
|
|
|
["2001:200:300::/48"],
|
|
|
|
["2001:200:300::192.168.1.2/48"],
|
2013-02-17 22:45:21 -05:00
|
|
|
["1:2:3:4:5:6:7::"],
|
|
|
|
["::2:3:4:5:6:7:8"],
|
2012-06-02 05:18:32 -04:00
|
|
|
].each { |args|
|
|
|
|
assert_nothing_raised {
|
|
|
|
IPAddr.new(*args)
|
|
|
|
}
|
2002-12-23 12:07:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
a = IPAddr.new
|
|
|
|
assert_equal("::", a.to_s)
|
|
|
|
assert_equal("0000:0000:0000:0000:0000:0000:0000:0000", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET6, a.family)
|
|
|
|
|
|
|
|
a = IPAddr.new("0123:4567:89ab:cdef:0ABC:DEF0:1234:5678")
|
|
|
|
assert_equal("123:4567:89ab:cdef:abc:def0:1234:5678", a.to_s)
|
|
|
|
assert_equal("0123:4567:89ab:cdef:0abc:def0:1234:5678", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET6, a.family)
|
|
|
|
|
|
|
|
a = IPAddr.new("3ffe:505:2::/48")
|
|
|
|
assert_equal("3ffe:505:2::", a.to_s)
|
|
|
|
assert_equal("3ffe:0505:0002:0000:0000:0000:0000:0000", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET6, a.family)
|
|
|
|
assert_equal(false, a.ipv4?)
|
|
|
|
assert_equal(true, a.ipv6?)
|
|
|
|
assert_equal("#<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000>", a.inspect)
|
|
|
|
|
|
|
|
a = IPAddr.new("3ffe:505:2::/ffff:ffff:ffff::")
|
|
|
|
assert_equal("3ffe:505:2::", a.to_s)
|
|
|
|
assert_equal("3ffe:0505:0002:0000:0000:0000:0000:0000", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET6, a.family)
|
|
|
|
|
|
|
|
a = IPAddr.new("0.0.0.0")
|
|
|
|
assert_equal("0.0.0.0", a.to_s)
|
|
|
|
assert_equal("0.0.0.0", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET, a.family)
|
|
|
|
|
|
|
|
a = IPAddr.new("192.168.1.2")
|
|
|
|
assert_equal("192.168.1.2", a.to_s)
|
|
|
|
assert_equal("192.168.1.2", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET, a.family)
|
|
|
|
assert_equal(true, a.ipv4?)
|
|
|
|
assert_equal(false, a.ipv6?)
|
|
|
|
|
|
|
|
a = IPAddr.new("192.168.1.2/24")
|
|
|
|
assert_equal("192.168.1.0", a.to_s)
|
|
|
|
assert_equal("192.168.1.0", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET, a.family)
|
|
|
|
assert_equal("#<IPAddr: IPv4:192.168.1.0/255.255.255.0>", a.inspect)
|
|
|
|
|
|
|
|
a = IPAddr.new("192.168.1.2/255.255.255.0")
|
|
|
|
assert_equal("192.168.1.0", a.to_s)
|
|
|
|
assert_equal("192.168.1.0", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET, a.family)
|
|
|
|
|
|
|
|
assert_equal("0:0:0:1::", IPAddr.new("0:0:0:1::").to_s)
|
|
|
|
assert_equal("2001:200:300::", IPAddr.new("2001:200:300::/48").to_s)
|
|
|
|
|
|
|
|
assert_equal("2001:200:300::", IPAddr.new("[2001:200:300::]/48").to_s)
|
2013-02-17 22:45:21 -05:00
|
|
|
assert_equal("1:2:3:4:5:6:7:0", IPAddr.new("1:2:3:4:5:6:7::").to_s)
|
|
|
|
assert_equal("0:2:3:4:5:6:7:8", IPAddr.new("::2:3:4:5:6:7:8").to_s)
|
2002-12-23 12:07:49 -05:00
|
|
|
|
2012-08-31 03:30:22 -04:00
|
|
|
assert_raises(IPAddr::InvalidAddressError) { IPAddr.new("192.168.0.256") }
|
|
|
|
assert_raises(IPAddr::InvalidAddressError) { IPAddr.new("192.168.0.011") }
|
|
|
|
assert_raises(IPAddr::InvalidAddressError) { IPAddr.new("fe80::1%fxp0") }
|
|
|
|
assert_raises(IPAddr::InvalidAddressError) { IPAddr.new("[192.168.1.2]/120") }
|
|
|
|
assert_raises(IPAddr::InvalidPrefixError) { IPAddr.new("::1/255.255.255.0") }
|
|
|
|
assert_raises(IPAddr::InvalidPrefixError) { IPAddr.new("::1/129") }
|
|
|
|
assert_raises(IPAddr::InvalidPrefixError) { IPAddr.new("192.168.0.1/33") }
|
|
|
|
assert_raises(IPAddr::AddressFamilyError) { IPAddr.new(1) }
|
|
|
|
assert_raises(IPAddr::AddressFamilyError) { IPAddr.new("::ffff:192.168.1.2/120", Socket::AF_INET) }
|
2005-01-19 04:14:42 -05:00
|
|
|
end
|
2002-12-23 12:07:49 -05:00
|
|
|
|
|
|
|
def test_s_new_ntoh
|
|
|
|
addr = ''
|
|
|
|
IPAddr.new("1234:5678:9abc:def0:1234:5678:9abc:def0").hton.each_byte { |c|
|
|
|
|
addr += sprintf("%02x", c)
|
|
|
|
}
|
|
|
|
assert_equal("123456789abcdef0123456789abcdef0", addr)
|
|
|
|
addr = ''
|
|
|
|
IPAddr.new("123.45.67.89").hton.each_byte { |c|
|
|
|
|
addr += sprintf("%02x", c)
|
|
|
|
}
|
|
|
|
assert_equal(sprintf("%02x%02x%02x%02x", 123, 45, 67, 89), addr)
|
|
|
|
a = IPAddr.new("3ffe:505:2::")
|
|
|
|
assert_equal("3ffe:505:2::", IPAddr.new_ntoh(a.hton).to_s)
|
|
|
|
a = IPAddr.new("192.168.2.1")
|
|
|
|
assert_equal("192.168.2.1", IPAddr.new_ntoh(a.hton).to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ipv4_compat
|
|
|
|
a = IPAddr.new("::192.168.1.2")
|
|
|
|
assert_equal("::192.168.1.2", a.to_s)
|
|
|
|
assert_equal("0000:0000:0000:0000:0000:0000:c0a8:0102", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET6, a.family)
|
|
|
|
assert_equal(true, a.ipv4_compat?)
|
|
|
|
b = a.native
|
|
|
|
assert_equal("192.168.1.2", b.to_s)
|
|
|
|
assert_equal(Socket::AF_INET, b.family)
|
|
|
|
assert_equal(false, b.ipv4_compat?)
|
|
|
|
|
|
|
|
a = IPAddr.new("192.168.1.2")
|
|
|
|
b = a.ipv4_compat
|
|
|
|
assert_equal("::192.168.1.2", b.to_s)
|
|
|
|
assert_equal(Socket::AF_INET6, b.family)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ipv4_mapped
|
|
|
|
a = IPAddr.new("::ffff:192.168.1.2")
|
|
|
|
assert_equal("::ffff:192.168.1.2", a.to_s)
|
|
|
|
assert_equal("0000:0000:0000:0000:0000:ffff:c0a8:0102", a.to_string)
|
|
|
|
assert_equal(Socket::AF_INET6, a.family)
|
|
|
|
assert_equal(true, a.ipv4_mapped?)
|
|
|
|
b = a.native
|
|
|
|
assert_equal("192.168.1.2", b.to_s)
|
|
|
|
assert_equal(Socket::AF_INET, b.family)
|
|
|
|
assert_equal(false, b.ipv4_mapped?)
|
|
|
|
|
|
|
|
a = IPAddr.new("192.168.1.2")
|
|
|
|
b = a.ipv4_mapped
|
|
|
|
assert_equal("::ffff:192.168.1.2", b.to_s)
|
|
|
|
assert_equal(Socket::AF_INET6, b.family)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reverse
|
|
|
|
assert_equal("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", IPAddr.new("3ffe:505:2::f").reverse)
|
|
|
|
assert_equal("1.2.168.192.in-addr.arpa", IPAddr.new("192.168.2.1").reverse)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ip6_arpa
|
|
|
|
assert_equal("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", IPAddr.new("3ffe:505:2::f").ip6_arpa)
|
2012-08-31 03:30:22 -04:00
|
|
|
assert_raises(IPAddr::InvalidAddressError) {
|
2002-12-23 12:07:49 -05:00
|
|
|
IPAddr.new("192.168.2.1").ip6_arpa
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ip6_int
|
|
|
|
assert_equal("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.int", IPAddr.new("3ffe:505:2::f").ip6_int)
|
2012-08-31 03:30:22 -04:00
|
|
|
assert_raises(IPAddr::InvalidAddressError) {
|
2002-12-23 12:07:49 -05:00
|
|
|
IPAddr.new("192.168.2.1").ip6_int
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-01-19 04:05:50 -05:00
|
|
|
def test_to_s
|
|
|
|
assert_equal("3ffe:0505:0002:0000:0000:0000:0000:0001", IPAddr.new("3ffe:505:2::1").to_string)
|
|
|
|
assert_equal("3ffe:505:2::1", IPAddr.new("3ffe:505:2::1").to_s)
|
|
|
|
end
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class TC_Operator < Test::Unit::TestCase
|
|
|
|
|
|
|
|
IN6MASK32 = "ffff:ffff::"
|
|
|
|
IN6MASK128 = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@in6_addr_any = IPAddr.new()
|
|
|
|
@a = IPAddr.new("3ffe:505:2::/48")
|
|
|
|
@b = IPAddr.new("0:0:0:1::")
|
|
|
|
@c = IPAddr.new(IN6MASK32)
|
|
|
|
end
|
|
|
|
alias set_up setup
|
|
|
|
|
|
|
|
def test_or
|
|
|
|
assert_equal("3ffe:505:2:1::", (@a | @b).to_s)
|
|
|
|
a = @a
|
|
|
|
a |= @b
|
|
|
|
assert_equal("3ffe:505:2:1::", a.to_s)
|
|
|
|
assert_equal("3ffe:505:2::", @a.to_s)
|
|
|
|
assert_equal("3ffe:505:2:1::",
|
2011-05-18 17:19:18 -04:00
|
|
|
(@a | 0x00000000000000010000000000000000).to_s)
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_and
|
|
|
|
assert_equal("3ffe:505::", (@a & @c).to_s)
|
|
|
|
a = @a
|
|
|
|
a &= @c
|
|
|
|
assert_equal("3ffe:505::", a.to_s)
|
|
|
|
assert_equal("3ffe:505:2::", @a.to_s)
|
|
|
|
assert_equal("3ffe:505::", (@a & 0xffffffff000000000000000000000000).to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_shift_right
|
|
|
|
assert_equal("0:3ffe:505:2::", (@a >> 16).to_s)
|
|
|
|
a = @a
|
|
|
|
a >>= 16
|
|
|
|
assert_equal("0:3ffe:505:2::", a.to_s)
|
|
|
|
assert_equal("3ffe:505:2::", @a.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_shift_left
|
|
|
|
assert_equal("505:2::", (@a << 16).to_s)
|
|
|
|
a = @a
|
|
|
|
a <<= 16
|
|
|
|
assert_equal("505:2::", a.to_s)
|
|
|
|
assert_equal("3ffe:505:2::", @a.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_carrot
|
|
|
|
a = ~@in6_addr_any
|
|
|
|
assert_equal(IN6MASK128, a.to_s)
|
|
|
|
assert_equal("::", @in6_addr_any.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_equal
|
2012-06-02 05:18:32 -04:00
|
|
|
assert_equal(true, @a == IPAddr.new("3FFE:505:2::"))
|
|
|
|
assert_equal(true, @a == IPAddr.new("3ffe:0505:0002::"))
|
|
|
|
assert_equal(true, @a == IPAddr.new("3ffe:0505:0002:0:0:0:0:0"))
|
2002-12-23 12:07:49 -05:00
|
|
|
assert_equal(false, @a == IPAddr.new("3ffe:505:3::"))
|
|
|
|
assert_equal(true, @a != IPAddr.new("3ffe:505:3::"))
|
|
|
|
assert_equal(false, @a != IPAddr.new("3ffe:505:2::"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mask
|
|
|
|
a = @a.mask(32)
|
|
|
|
assert_equal("3ffe:505::", a.to_s)
|
|
|
|
assert_equal("3ffe:505:2::", @a.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include?
|
|
|
|
assert_equal(true, @a.include?(IPAddr.new("3ffe:505:2::")))
|
|
|
|
assert_equal(true, @a.include?(IPAddr.new("3ffe:505:2::1")))
|
|
|
|
assert_equal(false, @a.include?(IPAddr.new("3ffe:505:3::")))
|
|
|
|
net1 = IPAddr.new("192.168.2.0/24")
|
|
|
|
assert_equal(true, net1.include?(IPAddr.new("192.168.2.0")))
|
|
|
|
assert_equal(true, net1.include?(IPAddr.new("192.168.2.255")))
|
|
|
|
assert_equal(false, net1.include?(IPAddr.new("192.168.3.0")))
|
2003-04-24 02:08:32 -04:00
|
|
|
# test with integer parameter
|
|
|
|
int = (192 << 24) + (168 << 16) + (2 << 8) + 13
|
|
|
|
|
|
|
|
assert_equal(true, net1.include?(int))
|
|
|
|
assert_equal(false, net1.include?(int+255))
|
|
|
|
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|
|
|
|
|
2009-08-04 02:10:42 -04:00
|
|
|
def test_hash
|
|
|
|
a1 = IPAddr.new('192.168.2.0')
|
|
|
|
a2 = IPAddr.new('192.168.2.0')
|
|
|
|
a3 = IPAddr.new('3ffe:505:2::1')
|
|
|
|
a4 = IPAddr.new('3ffe:505:2::1')
|
|
|
|
a5 = IPAddr.new('127.0.0.1')
|
|
|
|
a6 = IPAddr.new('::1')
|
2009-08-05 11:13:07 -04:00
|
|
|
a7 = IPAddr.new('192.168.2.0/25')
|
|
|
|
a8 = IPAddr.new('192.168.2.0/25')
|
2009-08-04 02:10:42 -04:00
|
|
|
|
2009-08-05 11:13:07 -04:00
|
|
|
h = { a1 => 'ipv4', a2 => 'ipv4', a3 => 'ipv6', a4 => 'ipv6', a5 => 'ipv4', a6 => 'ipv6', a7 => 'ipv4', a8 => 'ipv4'}
|
|
|
|
assert_equal(5, h.size)
|
2009-08-04 02:10:42 -04:00
|
|
|
assert_equal('ipv4', h[a1])
|
|
|
|
assert_equal('ipv4', h[a2])
|
|
|
|
assert_equal('ipv6', h[a3])
|
|
|
|
assert_equal('ipv6', h[a4])
|
|
|
|
|
|
|
|
require 'set'
|
2009-08-05 11:13:07 -04:00
|
|
|
s = Set[a1, a2, a3, a4, a5, a6, a7, a8]
|
|
|
|
assert_equal(5, s.size)
|
2009-08-04 02:10:42 -04:00
|
|
|
assert_equal(true, s.include?(a1))
|
|
|
|
assert_equal(true, s.include?(a2))
|
|
|
|
assert_equal(true, s.include?(a3))
|
|
|
|
assert_equal(true, s.include?(a4))
|
|
|
|
assert_equal(true, s.include?(a5))
|
|
|
|
assert_equal(true, s.include?(a6))
|
|
|
|
end
|
2002-12-23 12:07:49 -05:00
|
|
|
end
|