1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.util;
16  
17  import java.net.InetAddress;
18  import java.net.NetworkInterface;
19  import java.net.SocketException;
20  import java.net.UnknownHostException;
21  import java.util.Enumeration;
22  
23  import ch.qos.logback.core.Context;
24  import ch.qos.logback.core.CoreConstants;
25  import ch.qos.logback.core.spi.ContextAwareBase;
26  
27  public class NetworkAddressUtil extends ContextAwareBase {
28  
29      public NetworkAddressUtil(Context context) {
30          setContext(context);
31      }
32  
33      public static String getLocalHostName() throws UnknownHostException, SocketException {
34          try {
35              InetAddress localhost = InetAddress.getLocalHost();
36              return localhost.getHostName();
37          } catch (UnknownHostException e) {
38              return getLocalAddressAsString();
39          }
40      }
41  
42      public static String getCanonicalLocalHostName() throws UnknownHostException, SocketException {
43          try {
44              InetAddress localhost = InetAddress.getLocalHost();
45              return localhost.getCanonicalHostName();
46          } catch (UnknownHostException e) {
47              return getLocalAddressAsString();
48          }
49      }
50  
51      private static String getLocalAddressAsString() throws UnknownHostException, SocketException {
52          Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
53          while (interfaces != null && interfaces.hasMoreElements()) {
54              Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
55              while (addresses != null && addresses.hasMoreElements()) {
56                  InetAddress address = addresses.nextElement();
57                  if (acceptableAddress(address)) {
58                      return address.getHostAddress();
59                  }
60              }
61          }
62          throw new UnknownHostException();
63      }
64  
65      private static boolean acceptableAddress(InetAddress address) {
66          return address != null && !address.isLoopbackAddress() && !address.isAnyLocalAddress()
67                  && !address.isLinkLocalAddress();
68      }
69  
70      /**
71       * Add the local host's name as a property
72       */
73      public String safelyGetLocalHostName() {
74          try {
75              String localhostName = getLocalHostName();
76              return localhostName;
77          } catch (UnknownHostException | SocketException | SecurityException e) {
78              addError("Failed to get local hostname", e);
79          }
80          return CoreConstants.UNKNOWN_LOCALHOST;
81      }
82  
83      public String safelyGetCanonicalLocalHostName() {
84          try {
85              String localhostName = getCanonicalLocalHostName();
86              return localhostName;
87          } catch (UnknownHostException | SocketException | SecurityException e) {
88              addError("Failed to get canonical local hostname", e);
89          }
90          return CoreConstants.UNKNOWN_LOCALHOST;
91  
92      }
93  
94  }