001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.util; 018 019import java.io.File; 020import java.net.InetAddress; 021import java.net.MalformedURLException; 022import java.net.NetworkInterface; 023import java.net.SocketException; 024import java.net.URI; 025import java.net.URISyntaxException; 026import java.net.URL; 027import java.net.UnknownHostException; 028import java.util.Arrays; 029import java.util.Enumeration; 030 031import org.apache.logging.log4j.Logger; 032import org.apache.logging.log4j.status.StatusLogger; 033 034/** 035 * Networking-related convenience methods. 036 */ 037public final class NetUtils { 038 039 private static final Logger LOGGER = StatusLogger.getLogger(); 040 private static final String UNKNOWN_LOCALHOST = "UNKNOWN_LOCALHOST"; 041 042 private NetUtils() { 043 // empty 044 } 045 046 /** 047 * This method gets the network name of the machine we are running on. Returns "UNKNOWN_LOCALHOST" in the unlikely 048 * case where the host name cannot be found. 049 * 050 * @return String the name of the local host 051 */ 052 public static String getLocalHostname() { 053 try { 054 final InetAddress addr = InetAddress.getLocalHost(); 055 return addr.getHostName(); 056 } catch (final UnknownHostException uhe) { 057 try { 058 final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); 059 while (interfaces.hasMoreElements()) { 060 final NetworkInterface nic = interfaces.nextElement(); 061 final Enumeration<InetAddress> addresses = nic.getInetAddresses(); 062 while (addresses.hasMoreElements()) { 063 final InetAddress address = addresses.nextElement(); 064 if (!address.isLoopbackAddress()) { 065 final String hostname = address.getHostName(); 066 if (hostname != null) { 067 return hostname; 068 } 069 } 070 } 071 } 072 } catch (final SocketException se) { 073 LOGGER.error("Could not determine local host name", uhe); 074 return UNKNOWN_LOCALHOST; 075 } 076 LOGGER.error("Could not determine local host name", uhe); 077 return UNKNOWN_LOCALHOST; 078 } 079 } 080 081 /** 082 * Returns the local network interface's MAC address if possible. The local network interface is defined here as 083 * the {@link java.net.NetworkInterface} that is both up and not a loopback interface. 084 * 085 * @return the MAC address of the local network interface or {@code null} if no MAC address could be determined. 086 */ 087 public static byte[] getMacAddress() { 088 byte[] mac = null; 089 try { 090 final InetAddress localHost = InetAddress.getLocalHost(); 091 try { 092 final NetworkInterface localInterface = NetworkInterface.getByInetAddress(localHost); 093 if (isUpAndNotLoopback(localInterface)) { 094 mac = localInterface.getHardwareAddress(); 095 } 096 if (mac == null) { 097 final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); 098 while (networkInterfaces.hasMoreElements() && mac == null) { 099 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}