@@ -67,6 +67,10 @@ private NetUtils() {
6767 * @return addr
6868 */
6969 public static String getAddr (String host , int port ) {
70+ String normalizedHost = unwrapIpv6Literal (host );
71+ if (isIpv6Literal (normalizedHost )) {
72+ return String .format ("[%s]:%d" , normalizedHost , port );
73+ }
7074 return String .format ("%s:%d" , host , port );
7175 }
7276
@@ -92,6 +96,9 @@ public static String getHost(InetAddress inetAddress) {
9296 }
9397 return canonicalHost ;
9498 }
99+ if (inetAddress instanceof Inet6Address ) {
100+ return normalizeV6Address ((Inet6Address ) inetAddress ).getHostAddress ();
101+ }
95102 return inetAddress .getHostAddress ();
96103 }
97104 return null ;
@@ -134,10 +141,10 @@ private static synchronized InetAddress getLocalAddress0() {
134141
135142 private static InetAddress normalizeV6Address (Inet6Address address ) {
136143 String addr = address .getHostAddress ();
137- int i = addr . lastIndexOf ( '%' );
138- if (i > 0 ) {
144+ String normalizedAddr = stripIpv6Scope ( addr );
145+ if (! StringUtils . equals ( addr , normalizedAddr ) ) {
139146 try {
140- return InetAddress .getByName (addr . substring ( 0 , i ) + '%' + address . getScopeId () );
147+ return InetAddress .getByName (normalizedAddr );
141148 } catch (UnknownHostException e ) {
142149 log .debug ("Unknown IPV6 address: " , e );
143150 }
@@ -160,11 +167,12 @@ protected static boolean isValidV6Address(InetAddress address) {
160167 if (!(address instanceof Inet6Address )) {
161168 return false ;
162169 }
163- String name = address .getHostAddress ();
170+ String name = stripIpv6Scope ( address .getHostAddress () );
164171 return (name != null
165172 && InetAddressUtils .isIPv6Address (name )
166173 && !address .isAnyLocalAddress ()
167- && !address .isLoopbackAddress ());
174+ && !address .isLoopbackAddress ()
175+ && !address .isLinkLocalAddress ());
168176 }
169177
170178 /**
@@ -313,6 +321,31 @@ private static List<NetworkInterface> getAllNetworkInterfaces() throws SocketExc
313321 return validNetworkInterfaces ;
314322 }
315323
324+ private static boolean isIpv6Literal (String host ) {
325+ return StringUtils .isNotEmpty (host ) && InetAddressUtils .isIPv6Address (stripIpv6Scope (host ));
326+ }
327+
328+ private static String stripIpv6Scope (String host ) {
329+ if (StringUtils .isEmpty (host )) {
330+ return host ;
331+ }
332+ int index = host .lastIndexOf ('%' );
333+ if (index > 0 ) {
334+ return host .substring (0 , index );
335+ }
336+ return host ;
337+ }
338+
339+ private static String unwrapIpv6Literal (String host ) {
340+ if (StringUtils .isEmpty (host )) {
341+ return host ;
342+ }
343+ if (host .startsWith ("[" ) && host .endsWith ("]" )) {
344+ return host .substring (1 , host .length () - 1 );
345+ }
346+ return host ;
347+ }
348+
316349 private static String specifyNetworkInterfaceName () {
317350 return PropertyUtils .getString (DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED ,
318351 System .getProperty (DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED ));
0 commit comments