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.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() {