ARTEMIS-1701 strip zone id from IPv6 host

This commit is contained in:
Justin Bertram 2018-02-23 15:05:00 -06:00 committed by Clebert Suconic
parent 619b2cb2a4
commit a294dc9493
3 changed files with 46 additions and 14 deletions

View File

@ -43,4 +43,19 @@ public class IPV6Util {
return host;
}
public static String stripBracketsAndZoneID(String host) {
// if the host contains a ':' then we know it's not IPv4
if (host != null && host.length() > 2 && host.contains(":")) {
// Strip opening/closing brackets
if (host.startsWith("[") && host.endsWith("]")) {
host = host.substring(1, host.length() - 1);
}
if (host.contains("%")) {
return host.substring(0, host.indexOf("%"));
}
}
return host;
}
}

View File

@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.utils;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class IPv6UtilTest {
@Test
public void testStripBracketsAndZoneID() {
assertEquals("fd00::d7dc:b4cc:2e2a:ea1", IPV6Util.stripBracketsAndZoneID("[fd00::d7dc:b4cc:2e2a:ea1%enp0s3]"));
assertEquals("127.0.0.1", IPV6Util.stripBracketsAndZoneID("127.0.0.1"));
}
}

View File

@ -18,7 +18,6 @@ package org.apache.activemq.artemis.core.remoting.impl.netty;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
@ -709,19 +708,7 @@ public class NettyConnector extends AbstractConnector {
return null;
}
// HORNETQ-907 - strip off IPv6 scope-id (if necessary)
SocketAddress remoteDestination = new InetSocketAddress(host, port);
InetAddress inetAddress = ((InetSocketAddress) remoteDestination).getAddress();
if (inetAddress instanceof Inet6Address) {
Inet6Address inet6Address = (Inet6Address) inetAddress;
if (inet6Address.getScopeId() != 0) {
try {
remoteDestination = new InetSocketAddress(InetAddress.getByAddress(inet6Address.getAddress()), ((InetSocketAddress) remoteDestination).getPort());
} catch (UnknownHostException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
}
InetSocketAddress remoteDestination = new InetSocketAddress(IPV6Util.stripBracketsAndZoneID(host), port);
logger.debug("Remote destination: " + remoteDestination);