diff --git a/lib/resolv.rb b/lib/resolv.rb index d50940ad15..2c4f83d212 100644 --- a/lib/resolv.rb +++ b/lib/resolv.rb @@ -2459,6 +2459,28 @@ class Resolv (\d+)\.(\d+)\.(\d+)\.(\d+) \z/x + ## + # IPv6 link local address format fe80:b:c:d:e:f:g:h%em1 + Regex_8HexLinkLocal = /\A + fe80 + (?::[0-9A-Fa-f]{1,4}){7} + %[0-9A-Za-z]+ + \z/x + + ## + # Compressed IPv6 link local address format fe80::b%em1 + + Regex_CompressedHexLinkLocal = /\A + fe80: + (?: + ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) :: + ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) + | + :((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) + )? + :[0-9A-Fa-f]{1,4}%[0-9A-Za-z]+ + \z/x + ## # A composite IPv6 address Regexp. @@ -2466,7 +2488,10 @@ class Resolv (?:#{Regex_8Hex}) | (?:#{Regex_CompressedHex}) | (?:#{Regex_6Hex4Dec}) | - (?:#{Regex_CompressedHex4Dec})/x + (?:#{Regex_CompressedHex4Dec}) | + (?:#{Regex_8HexLinkLocal}) | + (?:#{Regex_CompressedHexLinkLocal}) + /x ## # Creates a new IPv6 address from +arg+ which may be: diff --git a/test/resolv/test_addr.rb b/test/resolv/test_addr.rb index 14ec2651ab..e72d4ee013 100644 --- a/test/resolv/test_addr.rb +++ b/test/resolv/test_addr.rb @@ -16,6 +16,25 @@ class TestResolvAddr < Test::Unit::TestCase } end + def test_valid_ipv6_link_local_address + bug17112 = "[ruby-core:99539]" + assert_not_match(Resolv::IPv6::Regex, "fe80::1%", bug17112) + assert_not_match(Resolv::IPv6::Regex, "fe80:2:3:4:5:6:7:8%", bug17112) + assert_not_match(Resolv::IPv6::Regex, "fe90::1%em1", bug17112) + assert_not_match(Resolv::IPv6::Regex, "1:2:3:4:5:6:7:8%em1", bug17112) + assert_match(Resolv::IPv6::Regex, "fe80:2:3:4:5:6:7:8%em1", bug17112) + assert_match(Resolv::IPv6::Regex, "fe80::20d:3aff:fe7d:9760%eth0", bug17112) + assert_match(Resolv::IPv6::Regex, "fe80::1%em1", bug17112) + end + + def test_valid_socket_ip_address_list + Socket.ip_address_list.each do |addr| + ip = addr.ip_address + assert_match(Resolv::AddressRegex, ip) + assert_equal(ip, Resolv.getaddress(ip)) + end + end + def test_invalid_byte_comment bug9273 = '[ruby-core:59239] [Bug #9273]' Tempfile.create('resolv_test_addr_') do |tmpfile|