From ec409751e109256bd872c0d80c4ab1cbafc1a6de Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 19 Apr 2012 16:42:55 +0200 Subject: [PATCH] Correctly handle the stripping of the zoneId / scopeId in all cases. See #267 --- .../io/netty/util/internal/SocketUtil.java | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/common/src/main/java/io/netty/util/internal/SocketUtil.java b/common/src/main/java/io/netty/util/internal/SocketUtil.java index da0548b1b5..98ffd8b23e 100644 --- a/common/src/main/java/io/netty/util/internal/SocketUtil.java +++ b/common/src/main/java/io/netty/util/internal/SocketUtil.java @@ -18,7 +18,6 @@ package io.netty.util.internal; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.UnknownHostException; public final class SocketUtil { @@ -27,49 +26,40 @@ public final class SocketUtil { /** - * Takes care of stripping the zone id from the {@link InetAddress} if its an {@link Inet6Address} and the java + * Takes care of stripping the zone id / scope Id from the {@link InetAddress} if its an {@link Inet6Address} and the java * version is < 7. After that a new {@link InetSocketAddress} is created based on the old one. This is needed because of a bug that exists in java * versions < 7. * * See https://github.com/netty/netty/issues/267 * */ - public static InetSocketAddress stripZoneId(InetSocketAddress socketAddress) throws UnknownHostException { - return new InetSocketAddress(stripZoneId(socketAddress.getAddress()), socketAddress.getPort()); - } - - /** - * Takes care of stripping the zone id from the {@link InetAddress} if its an {@link Inet6Address} and the java - * version is < 7. This is needed because of a bug that exists in java versions < 7. - * - * See https://github.com/netty/netty/issues/267 - * - */ - public static InetAddress stripZoneId(InetAddress address) throws UnknownHostException { - // If we have a java version which is >= 7 we can just return the given - // InetSocketAddress as this bug only seems - // to exist in java 6 (and maybe also versions before) - if (DetectionUtil.javaVersion() >= 7) { - return address; - } + public static InetSocketAddress stripZoneId(InetSocketAddress socketAddress) { + // If we have a java version which is >= 7 we can just return the given + // InetSocketAddress as this bug only seems + // to exist in java 6 (and maybe also versions before) + if (DetectionUtil.javaVersion() >= 7) { + return socketAddress; + } + InetAddress address = socketAddress.getAddress(); + if (address instanceof Inet6Address) { + Inet6Address inet6Address = (Inet6Address) address; - if (address instanceof Inet6Address) { - Inet6Address inet6Address = (Inet6Address) address; + // Check if its a LinkLocalAddress as this is the only one which is + // affected + if (inet6Address.isLinkLocalAddress()) { + String hostaddress = inet6Address.getHostAddress(); - // Check if its a LinkLocalAddress as this is the only one which is - // affected - if (inet6Address.isLinkLocalAddress()) { - String hostaddress = inet6Address.getHostAddress(); - - int separator = hostaddress.indexOf(ZONE_ID_SEPARATOR); - - // strip of the zoneId - String withoutZonedId = inet6Address.getHostAddress().substring(0, separator); - return InetAddress.getByName(withoutZonedId); + int separator = hostaddress.indexOf(ZONE_ID_SEPARATOR); + + // check if the address contains a zoneId /scopeId and if so strip it + if (separator != -1) { + // strip of the zoneId / scopeId + String withoutZonedId = inet6Address.getHostAddress().substring(0, separator); + return new InetSocketAddress(withoutZonedId, socketAddress.getPort()); } } - return address; - + } + return socketAddress; } private SocketUtil() {