View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.util.internal;
18  
19  import io.netty.util.NetUtil;
20  import io.netty.util.internal.logging.InternalLogger;
21  import io.netty.util.internal.logging.InternalLoggerFactory;
22  
23  import java.net.InetAddress;
24  import java.net.NetworkInterface;
25  import java.net.SocketException;
26  import java.util.Arrays;
27  import java.util.Enumeration;
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  import java.util.Map.Entry;
31  
32  import static io.netty.util.internal.EmptyArrays.EMPTY_BYTES;
33  
34  public final class MacAddressUtil {
35      private static final InternalLogger logger = InternalLoggerFactory.getInstance(MacAddressUtil.class);
36  
37      private static final int EUI64_MAC_ADDRESS_LENGTH = 8;
38      private static final int EUI48_MAC_ADDRESS_LENGTH = 6;
39  
40      /**
41       * Obtains the best MAC address found on local network interfaces.
42       * Generally speaking, an active network interface used on public
43       * networks is better than a local network interface.
44       *
45       * @return byte array containing a MAC. null if no MAC can be found.
46       */
47      public static byte[] bestAvailableMac() {
48          // Find the best MAC address available.
49          byte[] bestMacAddr = EMPTY_BYTES;
50          InetAddress bestInetAddr = NetUtil.LOCALHOST4;
51  
52          // Retrieve the list of available network interfaces.
53          Map<NetworkInterface, InetAddress> ifaces = new LinkedHashMap<NetworkInterface, InetAddress>();
54          for (NetworkInterface iface: NetUtil.NETWORK_INTERFACES) {
55              // Use the interface with proper INET addresses only.
56              Enumeration<InetAddress> addrs = SocketUtils.addressesFromNetworkInterface(iface);
57              if (addrs.hasMoreElements()) {
58                  InetAddress a = addrs.nextElement();
59                  if (!a.isLoopbackAddress()) {
60                      ifaces.put(iface, a);
61                  }
62              }
63          }
64  
65          for (Entry<NetworkInterface, InetAddress> entry: ifaces.entrySet()) {
66              NetworkInterface iface = entry.getKey();
67              InetAddress inetAddr = entry.getValue();
68              if (iface.isVirtual()) {
69                  continue;
70              }
71  
72              byte[] macAddr;
73              try {
74                  macAddr = SocketUtils.hardwareAddressFromNetworkInterface(iface);
75              } catch (SocketException e) {
76                  logger.debug("Failed to get the hardware address of a network interface: {}", iface, e);
77                  continue;
78              }
79  
80              boolean replace = false;
81              int res = compareAddresses(bestMacAddr, macAddr);
82              if (res < 0) {
83                  // Found a better MAC address.
84                  replace = true;
85              } else if (res == 0) {
86                  // Two MAC addresses are of pretty much same quality.
87                  res = compareAddresses(bestInetAddr, inetAddr);
88                  if (res < 0) {
89                      // Found a MAC address with better INET address.
90                      replace = true;
91                  } else if (res == 0) {
92                      // Cannot tell the difference.  Choose the longer one.
93                      if (bestMacAddr.length < macAddr.length) {
94                          replace = true;
95                      }
96                  }
97              }
98  
99              if (replace) {
100                 bestMacAddr = macAddr;
101                 bestInetAddr = inetAddr;
102             }
103         }
104 
105         if (bestMacAddr == EMPTY_BYTES) {
106             return null;
107         }
108 
109         if (bestMacAddr.length == EUI48_MAC_ADDRESS_LENGTH) { // EUI-48 - convert to EUI-64
110             byte[] newAddr = new byte[EUI64_MAC_ADDRESS_LENGTH];
111             System.arraycopy(bestMacAddr, 0, newAddr, 0, 3);
112             newAddr[3] = (byte) 0xFF;
113             newAddr[4] = (byte) 0xFE;
114             System.arraycopy(bestMacAddr, 3, newAddr, 5, 3);
115             bestMacAddr = newAddr;
116         } else {
117             // Unknown
118             bestMacAddr = Arrays.copyOf(bestMacAddr, EUI64_MAC_ADDRESS_LENGTH);
119         }
120 
121         return bestMacAddr;
122     }
123 
124     /**
125      * Returns the result of {@link #bestAvailableMac()} if non-{@code null} otherwise returns a random EUI-64 MAC
126      * address.
127      */
128     public static byte[] defaultMachineId() {
129         byte[] bestMacAddr = bestAvailableMac();
130         if (bestMacAddr == null) {
131             bestMacAddr = new byte[EUI64_MAC_ADDRESS_LENGTH];
132             PlatformDependent.threadLocalRandom().nextBytes(bestMacAddr);
133             logger.warn(
134                     "Failed to find a usable hardware address from the network interfaces; using random bytes: {}",
135                     formatAddress(bestMacAddr));
136         }
137         return bestMacAddr;
138     }
139 
140     /**
141      * Parse a EUI-48, MAC-48, or EUI-64 MAC address from a {@link String} and return it as a {@code byte[]}.
142      * @param value The string representation of the MAC address.
143      * @return The byte representation of the MAC address.
144      */
145     public static byte[] parseMAC(String value) {
146         final byte[] machineId;
147         final char separator;
148         switch (value.length()) {
149             case 17:
150                 separator = value.charAt(2);
151                 validateMacSeparator(separator);
152                 machineId = new byte[EUI48_MAC_ADDRESS_LENGTH];
153                 break;
154             case 23:
155                 separator = value.charAt(2);
156                 validateMacSeparator(separator);
157                 machineId = new byte[EUI64_MAC_ADDRESS_LENGTH];
158                 break;
159             default:
160                 throw new IllegalArgumentException("value is not supported [MAC-48, EUI-48, EUI-64]");
161         }
162 
163         final int end = machineId.length - 1;
164         int j = 0;
165         for (int i = 0; i < end; ++i, j += 3) {
166             final int sIndex = j + 2;
167             machineId[i] = StringUtil.decodeHexByte(value, j);
168             if (value.charAt(sIndex) != separator) {
169                 throw new IllegalArgumentException("expected separator '" + separator + " but got '" +
170                         value.charAt(sIndex) + "' at index: " + sIndex);
171             }
172         }
173 
174         machineId[end] = StringUtil.decodeHexByte(value, j);
175 
176         return machineId;
177     }
178 
179     private static void validateMacSeparator(char separator) {
180         if (separator != ':' && separator != '-') {
181             throw new IllegalArgumentException("unsupported separator: " + separator + " (expected: [:-])");
182         }
183     }
184 
185     /**
186      * @param addr byte array of a MAC address.
187      * @return hex formatted MAC address.
188      */
189     public static String formatAddress(byte[] addr) {
190         StringBuilder buf = new StringBuilder(24);
191         for (byte b: addr) {
192             buf.append(String.format("%02x:", b & 0xff));
193         }
194         return buf.substring(0, buf.length() - 1);
195     }
196 
197     /**
198      * @return positive - current is better, 0 - cannot tell from MAC addr, negative - candidate is better.
199      */
200     // visible for testing
201     static int compareAddresses(byte[] current, byte[] candidate) {
202         if (candidate == null || candidate.length < EUI48_MAC_ADDRESS_LENGTH) {
203             return 1;
204         }
205 
206         // Must not be filled with only 0 and 1.
207         boolean onlyZeroAndOne = true;
208         for (byte b: candidate) {
209             if (b != 0 && b != 1) {
210                 onlyZeroAndOne = false;
211                 break;
212             }
213         }
214 
215         if (onlyZeroAndOne) {
216             return 1;
217         }
218 
219         // Must not be a multicast address
220         if ((candidate[0] & 1) != 0) {
221             return 1;
222         }
223 
224         // Prefer globally unique address.
225         if ((candidate[0] & 2) == 0) {
226             if (current.length != 0 && (current[0] & 2) == 0) {
227                 // Both current and candidate are globally unique addresses.
228                 return 0;
229             } else {
230                 // Only candidate is globally unique.
231                 return -1;
232             }
233         } else {
234             if (current.length != 0 && (current[0] & 2) == 0) {
235                 // Only current is globally unique.
236                 return 1;
237             } else {
238                 // Both current and candidate are non-unique.
239                 return 0;
240             }
241         }
242     }
243 
244     /**
245      * @return positive - current is better, 0 - cannot tell, negative - candidate is better
246      */
247     private static int compareAddresses(InetAddress current, InetAddress candidate) {
248         return scoreAddress(current) - scoreAddress(candidate);
249     }
250 
251     private static int scoreAddress(InetAddress addr) {
252         if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) {
253             return 0;
254         }
255         if (addr.isMulticastAddress()) {
256             return 1;
257         }
258         if (addr.isLinkLocalAddress()) {
259             return 2;
260         }
261         if (addr.isSiteLocalAddress()) {
262             return 3;
263         }
264 
265         return 4;
266     }
267 
268     private MacAddressUtil() { }
269 }