001package ch.qos.logback.core.util;
002
003import java.net.InetAddress;
004import java.net.NetworkInterface;
005import java.net.SocketException;
006import java.net.UnknownHostException;
007import java.util.Enumeration;
008
009import ch.qos.logback.core.Context;
010import ch.qos.logback.core.CoreConstants;
011import ch.qos.logback.core.spi.ContextAwareBase;
012
013public class NetworkAddressUtil extends ContextAwareBase {
014
015    public NetworkAddressUtil(Context context) {
016        setContext(context);
017    }
018
019    public static String getLocalHostName() throws UnknownHostException, SocketException {
020        try {
021            InetAddress localhost = InetAddress.getLocalHost();
022            return localhost.getHostName();
023        } catch (UnknownHostException e) {
024            return getLocalAddressAsString();
025        }
026    }
027
028    public static String getCanonicalLocalHostName() throws UnknownHostException, SocketException {
029        try {
030            InetAddress localhost = InetAddress.getLocalHost();
031            return localhost.getCanonicalHostName();
032        } catch (UnknownHostException e) {
033            return getLocalAddressAsString();
034        }
035    }
036
037    private static String getLocalAddressAsString() throws UnknownHostException, SocketException {
038        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
039        while (interfaces != null && interfaces.hasMoreElements()) {
040            Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
041            while (addresses != null && addresses.hasMoreElements()) {
042                InetAddress address = addresses.nextElement();
043                if (acceptableAddress(address)) {
044                    return address.getHostAddress();
045                }
046            }
047        }
048        throw new UnknownHostException();
049    }
050
051    private static boolean acceptableAddress(InetAddress address) {
052        return address != null && !address.isLoopbackAddress() && !address.isAnyLocalAddress()
053                && !address.isLinkLocalAddress();
054    }
055
056    /**
057     * Add the local host's name as a property
058     */
059    public String safelyGetLocalHostName() {
060        try {
061            String localhostName = getLocalHostName();
062            return localhostName;
063        } catch (UnknownHostException | SocketException | SecurityException e) {
064            addError("Failed to get local hostname", e);
065        }
066        return CoreConstants.UNKNOWN_LOCALHOST;
067    }
068
069    public String safelyGetCanonicalLocalHostName() {
070        try {
071            String localhostName = getCanonicalLocalHostName();
072            return localhostName;
073        } catch (UnknownHostException | SocketException | SecurityException e) {
074            addError("Failed to get canonical local hostname", e);
075        }
076        return CoreConstants.UNKNOWN_LOCALHOST;
077
078    }
079
080}