mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Import ipaddr 1.2.0
- Add IPAddr#prefix - Add IPAddr#loopback? - Add IPAddr#private? [Feature #11666] - Add IPAddr#link_local? [Feature #10912] - Reject invalid address mask [Bug #13399] - Warn that IPAddr#ipv4_compat and #ipv4_compat? are deprecated [#Bug 13769] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60270 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
31cc5b1505
commit
c2db917b3d
4 changed files with 202 additions and 5 deletions
9
NEWS
9
NEWS
|
@ -144,6 +144,15 @@ with all sufficient information, see the ChangeLog file or Redmine
|
|||
* Carriage returns are changed to be trimmed properly if trim_mode is specified
|
||||
and used. Duplicated newlines will be removed on Windows. [Bug #5339] [Bug #11464]
|
||||
|
||||
* IPAddr
|
||||
* New methods are added:
|
||||
* IPAddr#prefix
|
||||
* IPAddr#loopback?
|
||||
* IPAddr#private? [Feature #11666]
|
||||
* IPAddr#link_local? [Feature #10912]
|
||||
* IPAddr now rejects invalid address mask. [Bug #13399]
|
||||
* IPAddr#ipv4_compat and #ipv4_compat? are deprecated. [#Bug 13769]
|
||||
|
||||
* Net::HTTP
|
||||
* Add more HTTP status classes
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|||
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = "ipaddr"
|
||||
spec.version = "1.0.0"
|
||||
spec.version = "1.2.0"
|
||||
spec.authors = ["Akinori MUSHA", "Hajimu UMEMOTO"]
|
||||
spec.email = ["knu@idaemons.org", "ume@mahoroba.org"]
|
||||
|
||||
|
|
|
@ -259,6 +259,50 @@ class IPAddr
|
|||
return @family == Socket::AF_INET6
|
||||
end
|
||||
|
||||
# Returns true if the ipaddr is a loopback address.
|
||||
def loopback?
|
||||
case @family
|
||||
when Socket::AF_INET
|
||||
@addr & 0xff000000 == 0x7f000000
|
||||
when Socket::AF_INET6
|
||||
@addr == 1
|
||||
else
|
||||
raise AddressFamilyError, "unsupported address family"
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if the ipaddr is a private address. IPv4 addresses
|
||||
# in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC
|
||||
# 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC
|
||||
# 4193 are considered private.
|
||||
def private?
|
||||
case @family
|
||||
when Socket::AF_INET
|
||||
@addr & 0xff000000 == 0x0a000000 || # 10.0.0.0/8
|
||||
@addr & 0xfff00000 == 0xac100000 || # 172.16.0.0/12
|
||||
@addr & 0xffff0000 == 0xc0a80000 # 192.168.0.0/16
|
||||
when Socket::AF_INET6
|
||||
@addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000
|
||||
else
|
||||
raise AddressFamilyError, "unsupported address family"
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if the ipaddr is a link-local address. IPv4
|
||||
# addresses in 169.254.0.0/16 reserved by RFC 3927 and Link-Local
|
||||
# IPv6 Unicast Addresses in fe80::/10 reserved by RFC 4291 are
|
||||
# considered link-local.
|
||||
def link_local?
|
||||
case @family
|
||||
when Socket::AF_INET
|
||||
@addr & 0xffff0000 == 0xa9fe0000 # 169.254.0.0/16
|
||||
when Socket::AF_INET6
|
||||
@addr & 0xffc0_0000_0000_0000_0000_0000_0000_0000 == 0xfe80_0000_0000_0000_0000_0000_0000_0000
|
||||
else
|
||||
raise AddressFamilyError, "unsupported address family"
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if the ipaddr is an IPv4-mapped IPv6 address.
|
||||
def ipv4_mapped?
|
||||
return ipv6? && (@addr >> 32) == 0xffff
|
||||
|
@ -266,6 +310,11 @@ class IPAddr
|
|||
|
||||
# Returns true if the ipaddr is an IPv4-compatible IPv6 address.
|
||||
def ipv4_compat?
|
||||
warn "#{caller(1)[0]}: warning: IPAddr\##{__callee__} is obsolete" if $VERBOSE
|
||||
_ipv4_compat?
|
||||
end
|
||||
|
||||
def _ipv4_compat?
|
||||
if !ipv6? || (@addr >> 32) != 0
|
||||
return false
|
||||
end
|
||||
|
@ -273,6 +322,8 @@ class IPAddr
|
|||
return a != 0 && a != 1
|
||||
end
|
||||
|
||||
private :_ipv4_compat?
|
||||
|
||||
# Returns a new ipaddr built by converting the native IPv4 address
|
||||
# into an IPv4-mapped IPv6 address.
|
||||
def ipv4_mapped
|
||||
|
@ -285,6 +336,7 @@ class IPAddr
|
|||
# Returns a new ipaddr built by converting the native IPv4 address
|
||||
# into an IPv4-compatible IPv6 address.
|
||||
def ipv4_compat
|
||||
warn "#{caller(1)[0]}: warning: IPAddr\##{__callee__} is obsolete" if $VERBOSE
|
||||
if !ipv4?
|
||||
raise InvalidAddressError, "not an IPv4 address"
|
||||
end
|
||||
|
@ -295,7 +347,7 @@ class IPAddr
|
|||
# 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?
|
||||
if !ipv4_mapped? && !_ipv4_compat?
|
||||
return self
|
||||
end
|
||||
return self.clone.set(@addr & IN4MASK, Socket::AF_INET)
|
||||
|
@ -371,6 +423,35 @@ class IPAddr
|
|||
return clone.set(begin_addr, @family)..clone.set(end_addr, @family)
|
||||
end
|
||||
|
||||
# Returns the prefix length in bits for the ipaddr.
|
||||
def prefix
|
||||
case @family
|
||||
when Socket::AF_INET
|
||||
n = IN4MASK ^ @mask_addr
|
||||
i = 32
|
||||
when Socket::AF_INET6
|
||||
n = IN6MASK ^ @mask_addr
|
||||
i = 128
|
||||
else
|
||||
raise AddressFamilyError, "unsupported address family"
|
||||
end
|
||||
while n.positive?
|
||||
n >>= 1
|
||||
i -= 1
|
||||
end
|
||||
i
|
||||
end
|
||||
|
||||
# Sets the prefix length in bits
|
||||
def prefix=(prefix)
|
||||
case prefix
|
||||
when Integer
|
||||
mask!(prefix)
|
||||
else
|
||||
raise InvalidPrefixError, "prefix must be an integer"
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a string containing a human-readable representation of the
|
||||
# ipaddr. ("#<IPAddr: family:address/mask>")
|
||||
def inspect
|
||||
|
@ -413,7 +494,8 @@ class IPAddr
|
|||
|
||||
# Set current netmask to given mask.
|
||||
def mask!(mask)
|
||||
if mask.kind_of?(String)
|
||||
case mask
|
||||
when String
|
||||
if mask =~ /\A\d+\z/
|
||||
prefixlen = mask.to_i
|
||||
else
|
||||
|
@ -422,6 +504,10 @@ class IPAddr
|
|||
raise InvalidPrefixError, "address family is not same"
|
||||
end
|
||||
@mask_addr = m.to_i
|
||||
n = @mask_addr ^ m.instance_variable_get(:@mask_addr)
|
||||
unless ((n + 1) & n).zero?
|
||||
raise InvalidPrefixError, "invalid mask #{mask}"
|
||||
end
|
||||
@addr &= @mask_addr
|
||||
return self
|
||||
end
|
||||
|
|
|
@ -21,11 +21,13 @@ class TC_IPAddr < Test::Unit::TestCase
|
|||
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)
|
||||
assert_equal(128, a.prefix)
|
||||
|
||||
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)
|
||||
assert_equal(128, a.prefix)
|
||||
|
||||
a = IPAddr.new("3ffe:505:2::/48")
|
||||
assert_equal("3ffe:505:2::", a.to_s)
|
||||
|
@ -34,16 +36,19 @@ class TC_IPAddr < Test::Unit::TestCase
|
|||
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)
|
||||
assert_equal(48, a.prefix)
|
||||
|
||||
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)
|
||||
assert_equal(48, a.prefix)
|
||||
|
||||
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)
|
||||
assert_equal(32, a.prefix)
|
||||
|
||||
a = IPAddr.new("192.168.1.2")
|
||||
assert_equal("192.168.1.2", a.to_s)
|
||||
|
@ -51,17 +56,27 @@ class TC_IPAddr < Test::Unit::TestCase
|
|||
assert_equal(Socket::AF_INET, a.family)
|
||||
assert_equal(true, a.ipv4?)
|
||||
assert_equal(false, a.ipv6?)
|
||||
assert_equal(32, a.prefix)
|
||||
|
||||
a = IPAddr.new("192.168.1.2/24")
|
||||
a = IPAddr.new("192.168.1.2/26")
|
||||
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)
|
||||
assert_equal("#<IPAddr: IPv4:192.168.1.0/255.255.255.192>", a.inspect)
|
||||
assert_equal(26, a.prefix)
|
||||
|
||||
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(24, a.prefix)
|
||||
|
||||
(0..32).each do |prefix|
|
||||
assert_equal(prefix, IPAddr.new("10.20.30.40/#{prefix}").prefix)
|
||||
end
|
||||
(0..128).each do |prefix|
|
||||
assert_equal(prefix, IPAddr.new("1:2:3:4:5:6:7:8/#{prefix}").prefix)
|
||||
end
|
||||
|
||||
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)
|
||||
|
@ -79,6 +94,7 @@ class TC_IPAddr < Test::Unit::TestCase
|
|||
assert_raise(IPAddr::InvalidPrefixError) { IPAddr.new("::1/255.255.255.0") }
|
||||
assert_raise(IPAddr::InvalidPrefixError) { IPAddr.new("::1/129") }
|
||||
assert_raise(IPAddr::InvalidPrefixError) { IPAddr.new("192.168.0.1/33") }
|
||||
assert_raise(IPAddr::InvalidPrefixError) { IPAddr.new("192.168.0.1/255.255.255.1") }
|
||||
assert_raise(IPAddr::AddressFamilyError) { IPAddr.new(1) }
|
||||
assert_raise(IPAddr::AddressFamilyError) { IPAddr.new("::ffff:192.168.1.2/120", Socket::AF_INET) }
|
||||
end
|
||||
|
@ -153,6 +169,27 @@ class TC_IPAddr < Test::Unit::TestCase
|
|||
}
|
||||
end
|
||||
|
||||
def test_prefix_writer
|
||||
a = IPAddr.new("192.168.1.2")
|
||||
["1", "255.255.255.0", "ffff:ffff:ffff:ffff::", nil, 1.0, -1, 33].each { |x|
|
||||
assert_raise(IPAddr::InvalidPrefixError) { a.prefix = x }
|
||||
}
|
||||
a = IPAddr.new("1:2:3:4:5:6:7:8")
|
||||
["1", "255.255.255.0", "ffff:ffff:ffff:ffff::", nil, 1.0, -1, 129].each { |x|
|
||||
assert_raise(IPAddr::InvalidPrefixError) { a.prefix = x }
|
||||
}
|
||||
|
||||
a = IPAddr.new("192.168.1.2")
|
||||
a.prefix = 26
|
||||
assert_equal(26, a.prefix)
|
||||
assert_equal("192.168.1.0", a.to_s)
|
||||
|
||||
a = IPAddr.new("1:2:3:4:5:6:7:8")
|
||||
a.prefix = 52
|
||||
assert_equal(52, a.prefix)
|
||||
assert_equal("1:2:3::", a.to_s)
|
||||
end
|
||||
|
||||
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)
|
||||
|
@ -234,7 +271,14 @@ class TC_Operator < Test::Unit::TestCase
|
|||
def test_mask
|
||||
a = @a.mask(32)
|
||||
assert_equal("3ffe:505::", a.to_s)
|
||||
assert_equal("3ffe:505::", @a.mask("ffff:ffff::").to_s)
|
||||
assert_equal("3ffe:505:2::", @a.to_s)
|
||||
a = IPAddr.new("192.168.2.0/24")
|
||||
assert_equal("192.168.0.0", a.mask(16).to_s)
|
||||
assert_equal("192.168.0.0", a.mask("255.255.0.0").to_s)
|
||||
assert_equal("192.168.2.0", a.to_s)
|
||||
assert_raise(IPAddr::InvalidPrefixError) {a.mask("255.255.0.255")}
|
||||
assert_raise(IPAddr::InvalidPrefixError) {@a.mask("ffff:1::")}
|
||||
end
|
||||
|
||||
def test_include?
|
||||
|
@ -253,6 +297,64 @@ class TC_Operator < Test::Unit::TestCase
|
|||
|
||||
end
|
||||
|
||||
def test_loopback?
|
||||
assert_equal(true, IPAddr.new('127.0.0.1').loopback?)
|
||||
assert_equal(true, IPAddr.new('127.127.1.1').loopback?)
|
||||
assert_equal(false, IPAddr.new('0.0.0.0').loopback?)
|
||||
assert_equal(false, IPAddr.new('192.168.2.0').loopback?)
|
||||
assert_equal(false, IPAddr.new('255.0.0.0').loopback?)
|
||||
assert_equal(true, IPAddr.new('::1').loopback?)
|
||||
assert_equal(false, IPAddr.new('::').loopback?)
|
||||
assert_equal(false, IPAddr.new('3ffe:505:2::1').loopback?)
|
||||
end
|
||||
|
||||
def test_private?
|
||||
assert_equal(false, IPAddr.new('0.0.0.0').private?)
|
||||
assert_equal(false, IPAddr.new('127.0.0.1').private?)
|
||||
|
||||
assert_equal(false, IPAddr.new('8.8.8.8').private?)
|
||||
assert_equal(true, IPAddr.new('10.0.0.0').private?)
|
||||
assert_equal(true, IPAddr.new('10.255.255.255').private?)
|
||||
assert_equal(false, IPAddr.new('11.255.1.1').private?)
|
||||
|
||||
assert_equal(false, IPAddr.new('172.15.255.255').private?)
|
||||
assert_equal(true, IPAddr.new('172.16.0.0').private?)
|
||||
assert_equal(true, IPAddr.new('172.31.255.255').private?)
|
||||
assert_equal(false, IPAddr.new('172.32.0.0').private?)
|
||||
|
||||
assert_equal(false, IPAddr.new('190.168.0.0').private?)
|
||||
assert_equal(true, IPAddr.new('192.168.0.0').private?)
|
||||
assert_equal(true, IPAddr.new('192.168.255.255').private?)
|
||||
assert_equal(false, IPAddr.new('192.169.0.0').private?)
|
||||
|
||||
assert_equal(false, IPAddr.new('169.254.0.1').private?)
|
||||
|
||||
assert_equal(false, IPAddr.new('::1').private?)
|
||||
assert_equal(false, IPAddr.new('::').private?)
|
||||
|
||||
assert_equal(false, IPAddr.new('fb84:8bf7:e905::1').private?)
|
||||
assert_equal(true, IPAddr.new('fc84:8bf7:e905::1').private?)
|
||||
assert_equal(true, IPAddr.new('fd84:8bf7:e905::1').private?)
|
||||
assert_equal(false, IPAddr.new('fe84:8bf7:e905::1').private?)
|
||||
end
|
||||
|
||||
def test_link_local?
|
||||
assert_equal(false, IPAddr.new('0.0.0.0').link_local?)
|
||||
assert_equal(false, IPAddr.new('127.0.0.1').link_local?)
|
||||
assert_equal(false, IPAddr.new('10.0.0.0').link_local?)
|
||||
assert_equal(false, IPAddr.new('172.16.0.0').link_local?)
|
||||
assert_equal(false, IPAddr.new('192.168.0.0').link_local?)
|
||||
|
||||
assert_equal(true, IPAddr.new('169.254.1.1').link_local?)
|
||||
assert_equal(true, IPAddr.new('169.254.254.255').link_local?)
|
||||
|
||||
assert_equal(false, IPAddr.new('::1').link_local?)
|
||||
assert_equal(false, IPAddr.new('::').link_local?)
|
||||
assert_equal(false, IPAddr.new('fb84:8bf7:e905::1').link_local?)
|
||||
|
||||
assert_equal(true, IPAddr.new('fe80::dead:beef:cafe:1234').link_local?)
|
||||
end
|
||||
|
||||
def test_hash
|
||||
a1 = IPAddr.new('192.168.2.0')
|
||||
a2 = IPAddr.new('192.168.2.0')
|
||||
|
|
Loading…
Reference in a new issue