summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEarlopain <14981592+Earlopain@users.noreply.github.com>2023-12-16 13:58:58 +0100
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2023-12-25 21:12:49 +0900
commit86fa418dea646e52929b9df9163902c4728442d1 (patch)
treed4d1e3b815069f71f81ce874390adc168653276e /lib
parentda77c79d80a615130bc67b42d3795db2a3fdd3fe (diff)
[ruby/ipaddr] Consider IPv4-mapped IPv6 addresses link local/loopback if IPV4 address is private
Same as #57 https://github.com/ruby/ipaddr/commit/d56acecb80
Diffstat (limited to 'lib')
-rw-r--r--lib/ipaddr.rb19
1 files changed, 14 insertions, 5 deletions
diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb
index 70f885fe17..1e4fd711ca 100644
--- a/lib/ipaddr.rb
+++ b/lib/ipaddr.rb
@@ -252,12 +252,17 @@ class IPAddr
end
# Returns true if the ipaddr is a loopback address.
+ # Loopback IPv4 addresses in the IPv4-mapped IPv6
+ # address range are also considered as loopback addresses.
def loopback?
case @family
when Socket::AF_INET
- @addr & 0xff000000 == 0x7f000000
+ @addr & 0xff000000 == 0x7f000000 # 127.0.0.1/8
when Socket::AF_INET6
- @addr == 1
+ @addr == 1 || # ::1
+ (@addr & 0xffff_0000_0000 == 0xffff_0000_0000 && (
+ @addr & 0xff000000 == 0x7f000000 # ::ffff:127.0.0.1/8
+ ))
else
raise AddressFamilyError, "unsupported address family"
end
@@ -287,15 +292,19 @@ class IPAddr
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
+ # 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.
+ # considered link-local. Link-local IPv4 addresses in the
+ # IPv4-mapped IPv6 address range are also 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
+ @addr & 0xffc0_0000_0000_0000_0000_0000_0000_0000 == 0xfe80_0000_0000_0000_0000_0000_0000_0000 || # fe80::/10
+ (@addr & 0xffff_0000_0000 == 0xffff_0000_0000 && (
+ @addr & 0xffff0000 == 0xa9fe0000 # ::ffff:169.254.0.0/16
+ ))
else
raise AddressFamilyError, "unsupported address family"
end