Skip to content

Commit 1cfb14f

Browse files
committed
refactor: remove unique local addresses special case, prefer IPv4
1 parent 9719907 commit 1cfb14f

File tree

1 file changed

+10
-11
lines changed
  • modules/kernel/src/org/apache/axis2/util

1 file changed

+10
-11
lines changed

modules/kernel/src/org/apache/axis2/util/Utils.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
import java.util.Map;
7777
import java.util.List;
7878
import java.util.ArrayList;
79+
import java.util.Comparator;
80+
import java.util.function.Function;
7981

8082
public class Utils {
8183
private static final Log log = LogFactory.getLog(Utils.class);
@@ -612,16 +614,9 @@ public static List<InetAddress> getLocalHostLANAddresses() throws SocketExceptio
612614
if (!inetAddr.isLinkLocalAddress())
613615
{
614616
if (inetAddr instanceof Inet6Address) {
615-
Inet6Address inet6Addr = (Inet6Address) inetAddr;
616-
if ((inet6Addr.getAddress()[0] ^ 0xfc) > 1)
617-
{
618-
// we ignore the site-local attribute for IPv6 because
619-
// it has been deprecated, see https://www.ietf.org/rfc/rfc3879.txt
620-
// instead we verify that this is not a unique local address,
621-
// this check is unfortunately not in the standard library (yet)
622-
// https://en.wikipedia.org/wiki/Unique_local_address
623-
addresses.add(inetAddr);
624-
}
617+
// we ignore the site-local attribute for IPv6 because
618+
// it has been deprecated, see https://www.ietf.org/rfc/rfc3879.txt
619+
addresses.add(inetAddr);
625620
} else if (inetAddr instanceof Inet4Address && inetAddr.isSiteLocalAddress()) {
626621
// check site-local
627622
addresses.add(inetAddr);
@@ -683,7 +678,11 @@ public static List<InetAddress> getLocalHostLANAddresses() throws SocketExceptio
683678
* @return Returns String.
684679
*/
685680
public static String getIpAddress() throws SocketException {
686-
return getLocalHostLANAddresses().stream().findFirst().map(InetAddress::getHostAddress).orElse("127.0.0.1");
681+
//prefer ipv4 for backwards compatibility, we used to only consider ipv4 addresses
682+
Function<InetAddress,Integer> preferIpv4 = (i) -> i instanceof Inet4Address ? 1 : 0;
683+
return getLocalHostLANAddresses().stream()
684+
.min(Comparator.comparing(preferIpv4))
685+
.map(InetAddress::getHostAddress).orElse("127.0.0.1");
687686
}
688687

689688
/**

0 commit comments

Comments
 (0)