View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.util;
18  
19  import java.io.File;
20  import java.net.InetAddress;
21  import java.net.MalformedURLException;
22  import java.net.NetworkInterface;
23  import java.net.SocketException;
24  import java.net.URI;
25  import java.net.URISyntaxException;
26  import java.net.URL;
27  import java.net.UnknownHostException;
28  import java.util.Arrays;
29  import java.util.Enumeration;
30  
31  import org.apache.logging.log4j.Logger;
32  import org.apache.logging.log4j.status.StatusLogger;
33  
34  /**
35   * Networking-related convenience methods.
36   */
37  public final class NetUtils {
38  
39      private static final Logger LOGGER = StatusLogger.getLogger();
40      private static final String UNKNOWN_LOCALHOST = "UNKNOWN_LOCALHOST";
41  
42      private NetUtils() {
43          // empty
44      }
45  
46      /**
47       * This method gets the network name of the machine we are running on. Returns "UNKNOWN_LOCALHOST" in the unlikely
48       * case where the host name cannot be found.
49       *
50       * @return String the name of the local host
51       */
52      public static String getLocalHostname() {
53          try {
54              final InetAddress addr = InetAddress.getLocalHost();
55              return addr.getHostName();
56          } catch (final UnknownHostException uhe) {
57              try {
58                  final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
59                  while (interfaces.hasMoreElements()) {
60                      final NetworkInterface nic = interfaces.nextElement();
61                      final Enumeration<InetAddress> addresses = nic.getInetAddresses();
62                      while (addresses.hasMoreElements()) {
63                          final InetAddress address = addresses.nextElement();
64                          if (!address.isLoopbackAddress()) {
65                              final String hostname = address.getHostName();
66                              if (hostname != null) {
67                                  return hostname;
68                              }
69                          }
70                      }
71                  }
72              } catch (final SocketException se) {
73                  LOGGER.error("Could not determine local host name", uhe);
74                  return UNKNOWN_LOCALHOST;
75              }
76              LOGGER.error("Could not determine local host name", uhe);
77              return UNKNOWN_LOCALHOST;
78          }
79      }
80  
81      /**
82       *  Returns the local network interface's MAC address if possible. The local network interface is defined here as
83       *  the {@link java.net.NetworkInterface} that is both up and not a loopback interface.
84       *
85       * @return the MAC address of the local network interface or {@code null} if no MAC address could be determined.
86       */
87      public static byte[] getMacAddress() {
88          byte[] mac = null;
89          try {
90              final InetAddress localHost = InetAddress.getLocalHost();
91              try {
92                  final NetworkInterface localInterface = NetworkInterface.getByInetAddress(localHost);
93                  if (isUpAndNotLoopback(localInterface)) {
94                      mac = localInterface.getHardwareAddress();
95                  }
96                  if (mac == null) {
97                      final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
98                      while (networkInterfaces.hasMoreElements() && mac == null) {
99                          final NetworkInterface nic = networkInterfaces.nextElement();
100                         if (isUpAndNotLoopback(nic)) {
101                             mac = nic.getHardwareAddress();
102                         }
103                     }
104                 }
105             } catch (final SocketException e) {
106                 LOGGER.catching(e);
107             }
108             if (mac == null || mac.length == 0) {
109                 // Emulate a mac address with an IP v4 or v6
110                 final byte[] address = localHost.getAddress();
111                 // Take only 6 bytes if the address is an IPv6 otherwise will pad with two zero bytes
112                 mac = Arrays.copyOf(address, 6);
113             }
114         } catch (final UnknownHostException ignored) {
115             // ignored
116         }
117         return mac;
118     }
119 
120     /**
121      * Returns the mac address, if it is available, as a string with each byte separated by a ":" character.
122      * @return the mac address String or null.
123      */
124     public static String getMacAddressString() {
125         final byte[] macAddr = getMacAddress();
126         if (macAddr != null && macAddr.length > 0) {
127             StringBuilder sb = new StringBuilder(String.format("%02x", macAddr[0]));
128             for (int i = 1; i < macAddr.length; ++i) {
129                 sb.append(":").append(String.format("%02x", macAddr[i]));
130             }
131             return sb.toString();
132 
133         }
134         return null;
135     }
136 
137     private static boolean isUpAndNotLoopback(final NetworkInterface ni) throws SocketException {
138         return ni != null && !ni.isLoopback() && ni.isUp();
139     }
140 
141     /**
142      * Converts a URI string or file path to a URI object.
143      *
144      * @param path the URI string or path
145      * @return the URI object
146      */
147     public static URI toURI(final String path) {
148         try {
149             // Resolves absolute URI
150             return new URI(path);
151         } catch (final URISyntaxException e) {
152             // A file path or a Apache Commons VFS URL might contain blanks.
153             // A file path may start with a driver letter
154             try {
155                 final URL url = new URL(path);
156                 return new URI(url.getProtocol(), url.getHost(), url.getPath(), null);
157             } catch (MalformedURLException | URISyntaxException nestedEx) {
158                 return new File(path).toURI();
159             }
160         }
161     }
162 
163 }