001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.core.util;
016
017import java.net.InetAddress;
018import java.net.NetworkInterface;
019import java.net.SocketException;
020import java.net.UnknownHostException;
021import java.util.Enumeration;
022
023import ch.qos.logback.core.Context;
024import ch.qos.logback.core.CoreConstants;
025import ch.qos.logback.core.spi.ContextAwareBase;
026
027public class NetworkAddressUtil extends ContextAwareBase {
028
029    public NetworkAddressUtil(Context context) {
030        setContext(context);
031    }
032
033    public static String getLocalHostName() throws UnknownHostException, SocketException {
034        try {
035            InetAddress localhost = InetAddress.getLocalHost();
036            return localhost.getHostName();
037        } catch (UnknownHostException e) {
038            return getLocalAddressAsString();
039        }
040    }
041
042    public static String getCanonicalLocalHostName() throws UnknownHostException, SocketException {
043        try {
044            InetAddress localhost = InetAddress.getLocalHost();
045            return localhost.getCanonicalHostName();
046        } catch (UnknownHostException e) {
047            return getLocalAddressAsString();
048        }
049    }
050
051    private static String getLocalAddressAsString() throws UnknownHostException, SocketException {
052        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
053        while (interfaces != null && interfaces.hasMoreElements()) {
054            Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
055            while (addresses != null && addresses.hasMoreElements()) {
056                InetAddress address = addresses.nextElement();
057                if (acceptableAddress(address)) {
058                    return address.getHostAddress();
059                }
060            }
061        }
062        throw new UnknownHostException();
063    }
064
065    private static boolean acceptableAddress(InetAddress address) {
066        return address != null && !address.isLoopbackAddress() && !address.isAnyLocalAddress()
067                && !address.isLinkLocalAddress();
068    }
069
070    /**
071     * Add the local host's name as a property
072     */
073    public String safelyGetLocalHostName() {
074        try {
075            String localhostName = getLocalHostName();
076            return localhostName;
077        } catch (UnknownHostException | SocketException | SecurityException e) {
078            addError("Failed to get local hostname", e);
079        }
080        return CoreConstants.UNKNOWN_LOCALHOST;
081    }
082
083    public String safelyGetCanonicalLocalHostName() {
084        try {
085            String localhostName = getCanonicalLocalHostName();
086            return localhostName;
087        } catch (UnknownHostException | SocketException | SecurityException e) {
088            addError("Failed to get canonical local hostname", e);
089        }
090        return CoreConstants.UNKNOWN_LOCALHOST;
091
092    }
093
094}