in commons-jcs3-core/src/main/java/org/apache/commons/jcs3/utils/net/HostNameUtil.java [115:185]
public static List<InetAddress> getLocalHostLANAddresses()
throws UnknownHostException
{
final List<InetAddress> addresses = new ArrayList<>();
try
{
InetAddress candidateAddress = null;
// Iterate all NICs (network interface cards)...
final Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while ( ifaces.hasMoreElements() )
{
final NetworkInterface iface = ifaces.nextElement();
// Skip loopback interfaces
if (iface.isLoopback() || !iface.isUp())
{
continue;
}
// Iterate all IP addresses assigned to each card...
for ( final Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); )
{
final InetAddress inetAddr = inetAddrs.nextElement();
if ( !inetAddr.isLoopbackAddress() )
{
if (inetAddr.isSiteLocalAddress())
{
// Found non-loopback site-local address.
addresses.add(inetAddr);
}
if ( candidateAddress == null )
{
// Found non-loopback address, but not necessarily site-local.
// Store it as a candidate to be returned if site-local address is not subsequently found...
candidateAddress = inetAddr;
// Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,
// only the first. For subsequent iterations, candidate will be non-null.
}
}
}
}
if (candidateAddress != null && addresses.isEmpty())
{
// We did not find a site-local address, but we found some other non-loopback address.
// Server might have a non-site-local address assigned to its NIC (or it might be running
// IPv6 which deprecates the "site-local" concept).
addresses.add(candidateAddress);
}
// At this point, we did not find a non-loopback address.
// Fall back to returning whatever InetAddress.getLocalHost() returns...
if (addresses.isEmpty())
{
final InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
if ( jdkSuppliedAddress == null )
{
throw new UnknownHostException( "The JDK InetAddress.getLocalHost() method unexpectedly returned null." );
}
addresses.add(jdkSuppliedAddress);
}
}
catch ( final SocketException e )
{
final UnknownHostException unknownHostException = new UnknownHostException( "Failed to determine LAN address: "
+ e );
unknownHostException.initCause( e );
throw unknownHostException;
}
return addresses;
}