This is an automated email from the ASF dual-hosted git repository. tigerlee pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/rocketmq.git
The following commit(s) were added to refs/heads/develop by this push: new aa788e8796 [ISSUE #8289] Fixed network bugs and merged networkutil code (#8290) aa788e8796 is described below commit aa788e87966d85176d682fc6a84d144a1aa20707 Author: zekai-li <58294989+zekai...@users.noreply.github.com> AuthorDate: Fri Aug 16 00:33:35 2024 +0800 [ISSUE #8289] Fixed network bugs and merged networkutil code (#8290) * [ISSUE #8289] Fixed network bugs and merged networkutil code Fix the following three as 1,TraceBean: private static final String LOCAL_ADDRESS = UtilAll.ipToIPv4Str(UtilAll.getIP()); getIP mayby ipv6 2,NetworkUtil:if (ip.startsWith("127.0") || ip.startsWith("192.168") || ip.startsWith("0."));Check whether Intranet errors exist * [ISSUE #8289] Fixed network bugs and merged networkutil code Fix the following three as 1,TraceBean: private static final String LOCAL_ADDRESS = UtilAll.ipToIPv4Str(UtilAll.getIP()); getIP mayby ipv6 2,NetworkUtil:if (ip.startsWith("127.0") || ip.startsWith("192.168") || ip.startsWith("0."));Check whether Intranet errors exist --- .../apache/rocketmq/client/trace/TraceBean.java | 11 ++- .../java/org/apache/rocketmq/common/UtilAll.java | 35 +++---- .../apache/rocketmq/common/utils/NetworkUtil.java | 109 +++++++++++++-------- 3 files changed, 92 insertions(+), 63 deletions(-) diff --git a/client/src/main/java/org/apache/rocketmq/client/trace/TraceBean.java b/client/src/main/java/org/apache/rocketmq/client/trace/TraceBean.java index 70c147e1eb..17db1fbfa1 100644 --- a/client/src/main/java/org/apache/rocketmq/client/trace/TraceBean.java +++ b/client/src/main/java/org/apache/rocketmq/client/trace/TraceBean.java @@ -21,7 +21,7 @@ import org.apache.rocketmq.common.UtilAll; import org.apache.rocketmq.common.message.MessageType; public class TraceBean { - private static final String LOCAL_ADDRESS = UtilAll.ipToIPv4Str(UtilAll.getIP()); + private static final String LOCAL_ADDRESS; private String topic = ""; private String msgId = ""; private String offsetMsgId = ""; @@ -37,6 +37,15 @@ public class TraceBean { private String transactionId; private boolean fromTransactionCheck; + static { + byte[] ip = UtilAll.getIP(); + if (ip.length == 4) { + LOCAL_ADDRESS = UtilAll.ipToIPv4Str(ip); + } else { + LOCAL_ADDRESS = UtilAll.ipToIPv6Str(ip); + } + } + public MessageType getMsgType() { return msgType; } diff --git a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java index 3629ae648b..a42ac3f364 100644 --- a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java @@ -326,16 +326,14 @@ public class UtilAll { } public static void writeInt(char[] buffer, int pos, int value) { - char[] hexArray = HEX_ARRAY; for (int moveBits = 28; moveBits >= 0; moveBits -= 4) { - buffer[pos++] = hexArray[(value >>> moveBits) & 0x0F]; + buffer[pos++] = HEX_ARRAY[(value >>> moveBits) & 0x0F]; } } public static void writeShort(char[] buffer, int pos, int value) { - char[] hexArray = HEX_ARRAY; for (int moveBits = 12; moveBits >= 0; moveBits -= 4) { - buffer[pos++] = hexArray[(value >>> moveBits) & 0x0F]; + buffer[pos++] = HEX_ARRAY[(value >>> moveBits) & 0x0F]; } } @@ -536,25 +534,18 @@ public class UtilAll { } else if (ip[0] == (byte) 127) { return true; } else if (ip[0] == (byte) 172) { - if (ip[1] >= (byte) 16 && ip[1] <= (byte) 31) { - return true; - } + return ip[1] >= (byte) 16 && ip[1] <= (byte) 31; } else if (ip[0] == (byte) 192) { - if (ip[1] == (byte) 168) { - return true; - } + return ip[1] == (byte) 168; } return false; } public static boolean isInternalV6IP(InetAddress inetAddr) { - if (inetAddr.isAnyLocalAddress() // Wild card ipv6 + return inetAddr.isAnyLocalAddress() // Wild card ipv6 || inetAddr.isLinkLocalAddress() // Single broadcast ipv6 address: fe80:xx:xx... || inetAddr.isLoopbackAddress() //Loopback ipv6 address - || inetAddr.isSiteLocalAddress()) { // Site local ipv6 address: fec0:xx:xx... - return true; - } - return false; + || inetAddr.isSiteLocalAddress();// Site local ipv6 address: fec0:xx:xx... } private static boolean ipCheck(byte[] ip) { @@ -605,15 +596,15 @@ public class UtilAll { public static byte[] getIP() { try { - Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress ip = null; + Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces(); + InetAddress ip; byte[] internalIP = null; while (allNetInterfaces.hasMoreElements()) { - NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); - Enumeration addresses = netInterface.getInetAddresses(); + NetworkInterface netInterface = allNetInterfaces.nextElement(); + Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); while (addresses.hasMoreElements()) { - ip = (InetAddress) addresses.nextElement(); - if (ip != null && ip instanceof Inet4Address) { + ip = addresses.nextElement(); + if (ip instanceof Inet4Address) { byte[] ipByte = ip.getAddress(); if (ipByte.length == 4) { if (ipCheck(ipByte)) { @@ -624,7 +615,7 @@ public class UtilAll { } } } - } else if (ip != null && ip instanceof Inet6Address) { + } else if (ip instanceof Inet6Address) { byte[] ipByte = ip.getAddress(); if (ipByte.length == 16) { if (ipV6Check(ipByte)) { diff --git a/common/src/main/java/org/apache/rocketmq/common/utils/NetworkUtil.java b/common/src/main/java/org/apache/rocketmq/common/utils/NetworkUtil.java index 7dd83e6179..a7a9a7c796 100644 --- a/common/src/main/java/org/apache/rocketmq/common/utils/NetworkUtil.java +++ b/common/src/main/java/org/apache/rocketmq/common/utils/NetworkUtil.java @@ -19,15 +19,21 @@ package org.apache.rocketmq.common.utils; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; +import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.NetworkInterface; import java.net.SocketAddress; +import java.net.SocketException; import java.nio.channels.Selector; import java.nio.channels.spi.SelectorProvider; import java.util.ArrayList; import java.util.Enumeration; +import java.util.List; + +import org.apache.commons.validator.routines.InetAddressValidator; +import org.apache.rocketmq.common.UtilAll; import org.apache.rocketmq.common.constant.LoggerName; import org.apache.rocketmq.logging.org.slf4j.Logger; import org.apache.rocketmq.logging.org.slf4j.LoggerFactory; @@ -59,18 +65,14 @@ public class NetworkUtil { if (isLinuxPlatform()) { try { final Class<?> providerClazz = Class.forName("sun.nio.ch.EPollSelectorProvider"); - if (providerClazz != null) { - try { - final Method method = providerClazz.getMethod("provider"); - if (method != null) { - final SelectorProvider selectorProvider = (SelectorProvider) method.invoke(null); - if (selectorProvider != null) { - result = selectorProvider.openSelector(); - } - } - } catch (final Exception e) { - log.warn("Open ePoll Selector for linux platform exception", e); + try { + final Method method = providerClazz.getMethod("provider"); + final SelectorProvider selectorProvider = (SelectorProvider) method.invoke(null); + if (selectorProvider != null) { + result = selectorProvider.openSelector(); } + } catch (final Exception e) { + log.warn("Open ePoll Selector for linux platform exception", e); } } catch (final Exception e) { // ignore @@ -88,48 +90,71 @@ public class NetworkUtil { return isLinuxPlatform; } - public static String getLocalAddress() { - try { - // Traversal Network interface to get the first non-loopback and non-private address - Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); - ArrayList<String> ipv4Result = new ArrayList<>(); - ArrayList<String> ipv6Result = new ArrayList<>(); - while (enumeration.hasMoreElements()) { - final NetworkInterface nif = enumeration.nextElement(); - if (isBridge(nif) || nif.isVirtual() || nif.isPointToPoint() || !nif.isUp()) { - continue; - } - - final Enumeration<InetAddress> en = nif.getInetAddresses(); - while (en.hasMoreElements()) { - final InetAddress address = en.nextElement(); - if (!address.isLoopbackAddress()) { - if (address instanceof Inet6Address) { - ipv6Result.add(normalizeHostAddress(address)); - } else { - ipv4Result.add(normalizeHostAddress(address)); + public static List<InetAddress> getLocalInetAddressList() throws SocketException { + Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); + List<InetAddress> inetAddressList = new ArrayList<>(); + // Traversal Network interface to get the non-bridge and non-virtual and non-ppp and up address + while (enumeration.hasMoreElements()) { + final NetworkInterface nif = enumeration.nextElement(); + if (isBridge(nif) || nif.isVirtual() || nif.isPointToPoint() || !nif.isUp()) { + continue; + } + InetAddressValidator validator = InetAddressValidator.getInstance(); + final Enumeration<InetAddress> en = nif.getInetAddresses(); + while (en.hasMoreElements()) { + final InetAddress address = en.nextElement(); + if (address instanceof Inet4Address) { + byte[] ipByte = address.getAddress(); + if (ipByte.length == 4) { + if (validator.isValidInet4Address(UtilAll.ipToIPv4Str(ipByte))) { + inetAddressList.add(address); + } + } + } else if (address instanceof Inet6Address) { + byte[] ipByte = address.getAddress(); + if (ipByte.length == 16) { + if (validator.isValidInet6Address(UtilAll.ipToIPv6Str(ipByte))) { + inetAddressList.add(address); } } } } + } + return inetAddressList; + } - // prefer ipv4 + public static InetAddress getLocalInetAddress() { + try { + ArrayList<InetAddress> ipv4Result = new ArrayList<>(); + ArrayList<InetAddress> ipv6Result = new ArrayList<>(); + List<InetAddress> localInetAddressList = getLocalInetAddressList(); + for (InetAddress inetAddress : localInetAddressList) { + if (inetAddress instanceof Inet6Address) { + ipv6Result.add(inetAddress); + } else { + ipv4Result.add(inetAddress); + } + } + // prefer ipv4 and prefer external ip if (!ipv4Result.isEmpty()) { - for (String ip : ipv4Result) { - if (ip.startsWith("127.0") || ip.startsWith("192.168") || ip.startsWith("0.")) { + for (InetAddress ip : ipv4Result) { + if (UtilAll.isInternalIP(ip.getAddress())) { continue; } - return ip; } - return ipv4Result.get(ipv4Result.size() - 1); } else if (!ipv6Result.isEmpty()) { + for (InetAddress ip : ipv6Result) { + if (UtilAll.isInternalV6IP(ip)) { + continue; + } + return ip; + } return ipv6Result.get(0); } //If failed to find,fall back to localhost - final InetAddress localHost = InetAddress.getLocalHost(); - return normalizeHostAddress(localHost); + return InetAddress.getLocalHost(); } catch (Exception e) { log.error("Failed to obtain local address", e); } @@ -137,6 +162,11 @@ public class NetworkUtil { return null; } + public static String getLocalAddress() { + InetAddress localHost = getLocalInetAddress(); + return normalizeHostAddress(localHost); + } + public static String normalizeHostAddress(final InetAddress localHost) { if (localHost instanceof Inet6Address) { return "[" + localHost.getHostAddress() + "]"; @@ -149,8 +179,7 @@ public class NetworkUtil { int split = addr.lastIndexOf(":"); String host = addr.substring(0, split); String port = addr.substring(split + 1); - InetSocketAddress isa = new InetSocketAddress(host, Integer.parseInt(port)); - return isa; + return new InetSocketAddress(host, Integer.parseInt(port)); } public static String socketAddress2String(final SocketAddress addr) {