This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git
The following commit(s) were added to refs/heads/master by this push:
new fb4968f17 RATIS-2131. Surround with [] only if hostName is a IPv6
string (#1125)
fb4968f17 is described below
commit fb4968f1738172615d9c21f1673e49599dc13766
Author: Mridul Muralidharan <[email protected]>
AuthorDate: Thu Jul 25 01:33:00 2024 -0700
RATIS-2131. Surround with [] only if hostName is a IPv6 string (#1125)
---
ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java
b/ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java
index a6ce5af79..baf6762aa 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java
@@ -17,6 +17,8 @@
*/
package org.apache.ratis.util;
+import org.apache.ratis.thirdparty.com.google.common.net.InetAddresses;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -145,8 +147,12 @@ public interface NetUtils {
if (address == null) {
return null;
}
- final StringBuilder b = new StringBuilder(address.getHostName());
- if (address.getAddress() instanceof Inet6Address) {
+ String hostName = address.getHostName();
+ final StringBuilder b = new StringBuilder(hostName);
+ // Surround with '[', ']' only if it is a IPv6 ip - not for a IPv6 host
+ if (address.getAddress() instanceof Inet6Address &&
+ InetAddresses.isInetAddress(hostName) &&
+ InetAddresses.forString(hostName).getAddress().length == 16) {
b.insert(0, '[').append(']');
}
return b.append(':').append(address.getPort()).toString();