diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/IPV6Util.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/IPV6Util.java index 395eeeb481..486c699fbc 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/IPV6Util.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/IPV6Util.java @@ -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; + } } diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/IPv6UtilTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/IPv6UtilTest.java new file mode 100644 index 0000000000..3709731d72 --- /dev/null +++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/IPv6UtilTest.java @@ -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")); + } +} diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnector.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnector.java index 99d84479c4..38289ec901 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnector.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnector.java @@ -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);