Correctly handle the stripping of the zoneId / scopeId in all cases. See #267

This commit is contained in:
Norman Maurer 2012-04-19 16:40:02 +02:00
parent a0b7c4aff2
commit 5dbc207b5b

View File

@ -18,7 +18,6 @@ package org.jboss.netty.util.internal;
import java.net.Inet6Address; import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.UnknownHostException;
public final class SocketUtil { 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 * 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. * versions < 7.
* *
* See https://github.com/netty/netty/issues/267 * 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 // If we have a java version which is >= 7 we can just return the given
// InetSocketAddress as this bug only seems // InetSocketAddress as this bug only seems
// to exist in java 6 (and maybe also versions before) // to exist in java 6 (and maybe also versions before)
if (DetectionUtil.javaVersion() >= 7) { if (DetectionUtil.javaVersion() >= 7) {
return socketAddress; return socketAddress;
} }
return new InetSocketAddress(stripZoneId(socketAddress.getAddress()), socketAddress.getPort()); InetAddress address = socketAddress.getAddress();
} if (address instanceof Inet6Address) {
Inet6Address inet6Address = (Inet6Address) address;
/**
* 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;
}
if (address instanceof Inet6Address) { // Check if its a LinkLocalAddress as this is the only one which is
Inet6Address inet6Address = (Inet6Address) address; // affected
if (inet6Address.isLinkLocalAddress()) {
String hostaddress = inet6Address.getHostAddress();
// Check if its a LinkLocalAddress as this is the only one which is int separator = hostaddress.indexOf(ZONE_ID_SEPARATOR);
// affected
if (inet6Address.isLinkLocalAddress()) { // check if the address contains a zoneId /scopeId and if so strip it
String hostaddress = inet6Address.getHostAddress(); if (separator != -1) {
// strip of the zoneId / scopeId
int separator = hostaddress.indexOf(ZONE_ID_SEPARATOR); String withoutZonedId = inet6Address.getHostAddress().substring(0, separator);
return new InetSocketAddress(withoutZonedId, socketAddress.getPort());
// strip of the zoneId
String withoutZonedId = inet6Address.getHostAddress().substring(0, separator);
return InetAddress.getByName(withoutZonedId);
} }
} }
return address; }
return socketAddress;
} }
private SocketUtil() { private SocketUtil() {