From 5dbc207b5b975266e35ec8ccf57a07ac42ee11dd Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 19 Apr 2012 16:40:02 +0200 Subject: [PATCH] Correctly handle the stripping of the zoneId / scopeId in all cases. See #267 --- .../jboss/netty/util/internal/SocketUtil.java | 52 +++++++------------ 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/jboss/netty/util/internal/SocketUtil.java b/src/main/java/org/jboss/netty/util/internal/SocketUtil.java index 950310e24b..b0378d6376 100644 --- a/src/main/java/org/jboss/netty/util/internal/SocketUtil.java +++ b/src/main/java/org/jboss/netty/util/internal/SocketUtil.java @@ -18,7 +18,6 @@ package org.jboss.netty.util.internal; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.UnknownHostException; public final class SocketUtil { @@ -27,55 +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 { + 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; } - 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; - } + 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() {