From 4146dd8c584acace9b39c0ca70abfe1ddcb5c8f8 Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Mon, 11 Feb 2013 19:15:07 +0000 Subject: [PATCH] HTTPCLIENT-1317 InetAddressUtils should handle IPv6 Addresses with Embedded IPv4 Addresses git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1444926 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 2 ++ .../org/apache/http/conn/util/InetAddressUtils.java | 13 +++++++++++-- .../apache/http/conn/util/TestInetAddressUtils.java | 12 ++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 8486ce0a5..c0a55ad22 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,5 +1,7 @@ Changes since 4.3 ALPHA1 ------------------- +* [HTTPCLIENT-1317] InetAddressUtils should handle IPv6 Addresses with Embedded IPv4 Addresses + Contributed Sebastian Bazley . * [HTTPCLIENT-1320] SSLSocketFactory#createSystemSSLContext causes UnrecoverableKeyException 'Password verification failed' when a truststore is specified with 'javax.net.ssl.trustStore' diff --git a/httpclient/src/main/java/org/apache/http/conn/util/InetAddressUtils.java b/httpclient/src/main/java/org/apache/http/conn/util/InetAddressUtils.java index 707dc4cc6..bb9189d84 100644 --- a/httpclient/src/main/java/org/apache/http/conn/util/InetAddressUtils.java +++ b/httpclient/src/main/java/org/apache/http/conn/util/InetAddressUtils.java @@ -42,9 +42,14 @@ public class InetAddressUtils { private InetAddressUtils() { } + private static final String IPV4_BASIC_PATTERN_STRING = + "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; + private static final Pattern IPV4_PATTERN = - Pattern.compile( - "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"); + Pattern.compile("^" + IPV4_BASIC_PATTERN_STRING + "$"); + + private static final Pattern IPV4_MAPPED_IPV6_PATTERN = // TODO does not allow for redundant leading zeros + Pattern.compile("^::[fF]{4}:" + IPV4_BASIC_PATTERN_STRING + "$"); private static final Pattern IPV6_STD_PATTERN = Pattern.compile( @@ -58,6 +63,10 @@ public class InetAddressUtils { return IPV4_PATTERN.matcher(input).matches(); } + public static boolean isIPv4MappedIPv64Address(final String input) { + return IPV4_MAPPED_IPV6_PATTERN.matcher(input).matches(); + } + public static boolean isIPv6StdAddress(final String input) { return IPV6_STD_PATTERN.matcher(input).matches(); } diff --git a/httpclient/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java b/httpclient/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java index 4478143d7..54e263658 100644 --- a/httpclient/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java +++ b/httpclient/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java @@ -79,4 +79,16 @@ public class TestInetAddressUtils { Assert.assertFalse(InetAddressUtils.isIPv6HexCompressedAddress("1::2:3:4:5:6:7:8")); // too many fields } + @Test + public void testValidIPv4MappedIPv6Address() { + Assert.assertTrue(InetAddressUtils.isIPv4MappedIPv64Address("::FFFF:1.2.3.4")); + Assert.assertTrue(InetAddressUtils.isIPv4MappedIPv64Address("::ffff:255.255.255.255")); + } + + @Test + public void testInValidIPv4MappedIPv6Address() { + Assert.assertFalse(InetAddressUtils.isIPv4MappedIPv64Address("2001:0db8:0000:0000:0000:0000:1428:57ab")); + Assert.assertFalse(InetAddressUtils.isIPv4MappedIPv64Address("::ffff:1:2:3:4")); + } + }