blob: 87295142bec4664342751691bde330242cc44adc [file] [log] [blame]
Hugo Benichi59c8e422017-10-12 21:33:40 +09001/*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090019import android.annotation.IntDef;
Hugo Benichi48872c62018-01-12 09:46:29 +090020import android.annotation.NonNull;
Etan Cohena4824cf2018-11-02 15:07:20 -070021import android.annotation.Nullable;
Mathew Inwood53f089f2018-08-08 14:44:44 +010022import android.annotation.UnsupportedAppUsage;
xshu683b7562019-08-16 10:20:22 -070023import android.net.wifi.WifiInfo;
Hugo Benichiac52e402017-11-09 00:22:25 +090024import android.os.Parcel;
25import android.os.Parcelable;
26
27import com.android.internal.util.BitUtils;
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090028import com.android.internal.util.Preconditions;
Hugo Benichi59c8e422017-10-12 21:33:40 +090029
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090030import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
Etan Cohena4824cf2018-11-02 15:07:20 -070032import java.net.Inet6Address;
33import java.net.UnknownHostException;
Jong Wook Kimf0a55cc2018-01-31 19:03:19 -080034import java.security.SecureRandom;
Hugo Benichi59c8e422017-10-12 21:33:40 +090035import java.util.Arrays;
Hugo Benichiac52e402017-11-09 00:22:25 +090036import java.util.Random;
Hugo Benichi59c8e422017-10-12 21:33:40 +090037
38/**
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090039 * Representation of a MAC address.
40 *
41 * This class only supports 48 bits long addresses and does not support 64 bits long addresses.
42 * Instances of this class are immutable.
Hugo Benichi59c8e422017-10-12 21:33:40 +090043 */
Hugo Benichiac52e402017-11-09 00:22:25 +090044public final class MacAddress implements Parcelable {
Hugo Benichi59c8e422017-10-12 21:33:40 +090045
46 private static final int ETHER_ADDR_LEN = 6;
Hugo Benichiac52e402017-11-09 00:22:25 +090047 private static final byte[] ETHER_ADDR_BROADCAST = addr(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
Hugo Benichi59c8e422017-10-12 21:33:40 +090048
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090049 /**
50 * The MacAddress representing the unique broadcast MAC address.
51 */
52 public static final MacAddress BROADCAST_ADDRESS = MacAddress.fromBytes(ETHER_ADDR_BROADCAST);
Hugo Benichiac52e402017-11-09 00:22:25 +090053
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090054 /**
55 * The MacAddress zero MAC address.
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +090056 *
57 * <p>Not publicly exposed or treated specially since the OUI 00:00:00 is registered.
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090058 * @hide
59 */
Mathew Inwood53f089f2018-08-08 14:44:44 +010060 @UnsupportedAppUsage
Hugo Benichiac52e402017-11-09 00:22:25 +090061 public static final MacAddress ALL_ZEROS_ADDRESS = new MacAddress(0);
62
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090063 /** @hide */
64 @Retention(RetentionPolicy.SOURCE)
65 @IntDef(prefix = { "TYPE_" }, value = {
66 TYPE_UNKNOWN,
67 TYPE_UNICAST,
68 TYPE_MULTICAST,
69 TYPE_BROADCAST,
70 })
71 public @interface MacAddressType { }
Hugo Benichi59c8e422017-10-12 21:33:40 +090072
Hugo Benichi48872c62018-01-12 09:46:29 +090073 /** @hide Indicates a MAC address of unknown type. */
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090074 public static final int TYPE_UNKNOWN = 0;
75 /** Indicates a MAC address is a unicast address. */
76 public static final int TYPE_UNICAST = 1;
77 /** Indicates a MAC address is a multicast address. */
78 public static final int TYPE_MULTICAST = 2;
79 /** Indicates a MAC address is the broadcast address. */
80 public static final int TYPE_BROADCAST = 3;
Hugo Benichiac52e402017-11-09 00:22:25 +090081
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090082 private static final long VALID_LONG_MASK = (1L << 48) - 1;
83 private static final long LOCALLY_ASSIGNED_MASK = MacAddress.fromString("2:0:0:0:0:0").mAddr;
84 private static final long MULTICAST_MASK = MacAddress.fromString("1:0:0:0:0:0").mAddr;
85 private static final long OUI_MASK = MacAddress.fromString("ff:ff:ff:0:0:0").mAddr;
86 private static final long NIC_MASK = MacAddress.fromString("0:0:0:ff:ff:ff").mAddr;
87 private static final MacAddress BASE_GOOGLE_MAC = MacAddress.fromString("da:a1:19:0:0:0");
xshub4836072019-08-21 13:40:18 -070088 /** Default wifi MAC address used for a special purpose **/
89 private static final MacAddress DEFAULT_MAC_ADDRESS =
90 MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS);
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090091
92 // Internal representation of the MAC address as a single 8 byte long.
Hugo Benichiac52e402017-11-09 00:22:25 +090093 // The encoding scheme sets the two most significant bytes to 0. The 6 bytes of the
Hugo Benichi84bb7fc2017-11-16 14:40:16 +090094 // MAC address are encoded in the 6 least significant bytes of the long, where the first
Hugo Benichiac52e402017-11-09 00:22:25 +090095 // byte of the array is mapped to the 3rd highest logical byte of the long, the second
96 // byte of the array is mapped to the 4th highest logical byte of the long, and so on.
97 private final long mAddr;
98
99 private MacAddress(long addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900100 mAddr = (VALID_LONG_MASK & addr);
Hugo Benichiac52e402017-11-09 00:22:25 +0900101 }
102
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900103 /**
104 * Returns the type of this address.
105 *
106 * @return the int constant representing the MAC address type of this MacAddress.
107 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900108 public @MacAddressType int getAddressType() {
Hugo Benichiac52e402017-11-09 00:22:25 +0900109 if (equals(BROADCAST_ADDRESS)) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900110 return TYPE_BROADCAST;
Hugo Benichiac52e402017-11-09 00:22:25 +0900111 }
112 if (isMulticastAddress()) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900113 return TYPE_MULTICAST;
Hugo Benichiac52e402017-11-09 00:22:25 +0900114 }
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900115 return TYPE_UNICAST;
Hugo Benichiac52e402017-11-09 00:22:25 +0900116 }
117
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900118 /**
119 * @return true if this MacAddress is a multicast address.
120 * @hide
121 */
Hugo Benichiac52e402017-11-09 00:22:25 +0900122 public boolean isMulticastAddress() {
123 return (mAddr & MULTICAST_MASK) != 0;
124 }
125
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900126 /**
127 * @return true if this MacAddress is a locally assigned address.
128 */
Hugo Benichiac52e402017-11-09 00:22:25 +0900129 public boolean isLocallyAssigned() {
130 return (mAddr & LOCALLY_ASSIGNED_MASK) != 0;
131 }
132
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900133 /**
134 * @return a byte array representation of this MacAddress.
135 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900136 public @NonNull byte[] toByteArray() {
Hugo Benichiac52e402017-11-09 00:22:25 +0900137 return byteAddrFromLongAddr(mAddr);
138 }
139
140 @Override
Hugo Benichi48872c62018-01-12 09:46:29 +0900141 public @NonNull String toString() {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900142 return stringAddrFromLongAddr(mAddr);
143 }
144
145 /**
Hugo Benichia0ecf382017-12-15 10:07:35 +0900146 * @return a String representation of the OUI part of this MacAddress made of 3 hexadecimal
147 * numbers in [0,ff] joined by ':' characters.
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900148 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900149 public @NonNull String toOuiString() {
Hugo Benichia0ecf382017-12-15 10:07:35 +0900150 return String.format(
151 "%02x:%02x:%02x", (mAddr >> 40) & 0xff, (mAddr >> 32) & 0xff, (mAddr >> 24) & 0xff);
Hugo Benichiac52e402017-11-09 00:22:25 +0900152 }
153
154 @Override
155 public int hashCode() {
156 return (int) ((mAddr >> 32) ^ mAddr);
157 }
158
159 @Override
160 public boolean equals(Object o) {
161 return (o instanceof MacAddress) && ((MacAddress) o).mAddr == mAddr;
162 }
163
164 @Override
165 public void writeToParcel(Parcel out, int flags) {
166 out.writeLong(mAddr);
167 }
168
169 @Override
170 public int describeContents() {
171 return 0;
172 }
173
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700174 public static final @android.annotation.NonNull Parcelable.Creator<MacAddress> CREATOR =
Hugo Benichiac52e402017-11-09 00:22:25 +0900175 new Parcelable.Creator<MacAddress>() {
176 public MacAddress createFromParcel(Parcel in) {
177 return new MacAddress(in.readLong());
178 }
179
180 public MacAddress[] newArray(int size) {
181 return new MacAddress[size];
182 }
183 };
184
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900185 /**
186 * Returns true if the given byte array is an valid MAC address.
187 * A valid byte array representation for a MacAddress is a non-null array of length 6.
188 *
189 * @param addr a byte array.
190 * @return true if the given byte array is not null and has the length of a MAC address.
191 *
192 * @hide
193 */
Hugo Benichi59c8e422017-10-12 21:33:40 +0900194 public static boolean isMacAddress(byte[] addr) {
195 return addr != null && addr.length == ETHER_ADDR_LEN;
196 }
197
198 /**
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900199 * Returns the MAC address type of the MAC address represented by the given byte array,
200 * or null if the given byte array does not represent a MAC address.
201 * A valid byte array representation for a MacAddress is a non-null array of length 6.
202 *
203 * @param addr a byte array representing a MAC address.
204 * @return the int constant representing the MAC address type of the MAC address represented
205 * by the given byte array, or type UNKNOWN if the byte array is not a valid MAC address.
206 *
207 * @hide
Hugo Benichiac52e402017-11-09 00:22:25 +0900208 */
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900209 public static int macAddressType(byte[] addr) {
Hugo Benichi59c8e422017-10-12 21:33:40 +0900210 if (!isMacAddress(addr)) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900211 return TYPE_UNKNOWN;
Hugo Benichi59c8e422017-10-12 21:33:40 +0900212 }
Hugo Benichi48872c62018-01-12 09:46:29 +0900213 return MacAddress.fromBytes(addr).getAddressType();
Hugo Benichiac52e402017-11-09 00:22:25 +0900214 }
215
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900216 /**
217 * Converts a String representation of a MAC address to a byte array representation.
218 * A valid String representation for a MacAddress is a series of 6 values in the
219 * range [0,ff] printed in hexadecimal and joined by ':' characters.
220 *
221 * @param addr a String representation of a MAC address.
222 * @return the byte representation of the MAC address.
223 * @throws IllegalArgumentException if the given String is not a valid representation.
224 *
225 * @hide
226 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900227 public static @NonNull byte[] byteAddrFromStringAddr(String addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900228 Preconditions.checkNotNull(addr);
Hugo Benichiac52e402017-11-09 00:22:25 +0900229 String[] parts = addr.split(":");
230 if (parts.length != ETHER_ADDR_LEN) {
231 throw new IllegalArgumentException(addr + " was not a valid MAC address");
Hugo Benichi59c8e422017-10-12 21:33:40 +0900232 }
Hugo Benichiac52e402017-11-09 00:22:25 +0900233 byte[] bytes = new byte[ETHER_ADDR_LEN];
234 for (int i = 0; i < ETHER_ADDR_LEN; i++) {
235 int x = Integer.valueOf(parts[i], 16);
236 if (x < 0 || 0xff < x) {
237 throw new IllegalArgumentException(addr + "was not a valid MAC address");
238 }
239 bytes[i] = (byte) x;
240 }
241 return bytes;
242 }
243
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900244 /**
245 * Converts a byte array representation of a MAC address to a String representation made
246 * of 6 hexadecimal numbers in [0,ff] joined by ':' characters.
247 * A valid byte array representation for a MacAddress is a non-null array of length 6.
248 *
249 * @param addr a byte array representation of a MAC address.
250 * @return the String representation of the MAC address.
251 * @throws IllegalArgumentException if the given byte array is not a valid representation.
252 *
253 * @hide
254 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900255 public static @NonNull String stringAddrFromByteAddr(byte[] addr) {
Hugo Benichiac52e402017-11-09 00:22:25 +0900256 if (!isMacAddress(addr)) {
257 return null;
258 }
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900259 return String.format("%02x:%02x:%02x:%02x:%02x:%02x",
260 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
Hugo Benichiac52e402017-11-09 00:22:25 +0900261 }
262
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900263 private static byte[] byteAddrFromLongAddr(long addr) {
Hugo Benichiac52e402017-11-09 00:22:25 +0900264 byte[] bytes = new byte[ETHER_ADDR_LEN];
265 int index = ETHER_ADDR_LEN;
266 while (index-- > 0) {
267 bytes[index] = (byte) addr;
268 addr = addr >> 8;
269 }
270 return bytes;
271 }
272
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900273 private static long longAddrFromByteAddr(byte[] addr) {
274 Preconditions.checkNotNull(addr);
Hugo Benichiac52e402017-11-09 00:22:25 +0900275 if (!isMacAddress(addr)) {
276 throw new IllegalArgumentException(
277 Arrays.toString(addr) + " was not a valid MAC address");
278 }
279 long longAddr = 0;
280 for (byte b : addr) {
281 longAddr = (longAddr << 8) + BitUtils.uint8(b);
282 }
283 return longAddr;
284 }
285
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900286 // Internal conversion function equivalent to longAddrFromByteAddr(byteAddrFromStringAddr(addr))
287 // that avoids the allocation of an intermediary byte[].
288 private static long longAddrFromStringAddr(String addr) {
289 Preconditions.checkNotNull(addr);
Hugo Benichiac52e402017-11-09 00:22:25 +0900290 String[] parts = addr.split(":");
291 if (parts.length != ETHER_ADDR_LEN) {
292 throw new IllegalArgumentException(addr + " was not a valid MAC address");
293 }
294 long longAddr = 0;
Hugo Benichid2c5b192017-12-05 13:14:08 +0900295 for (int i = 0; i < parts.length; i++) {
296 int x = Integer.valueOf(parts[i], 16);
Hugo Benichiac52e402017-11-09 00:22:25 +0900297 if (x < 0 || 0xff < x) {
298 throw new IllegalArgumentException(addr + "was not a valid MAC address");
299 }
300 longAddr = x + (longAddr << 8);
301 }
302 return longAddr;
303 }
304
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900305 // Internal conversion function equivalent to stringAddrFromByteAddr(byteAddrFromLongAddr(addr))
306 // that avoids the allocation of an intermediary byte[].
Hugo Benichi48872c62018-01-12 09:46:29 +0900307 private static @NonNull String stringAddrFromLongAddr(long addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900308 return String.format("%02x:%02x:%02x:%02x:%02x:%02x",
309 (addr >> 40) & 0xff,
310 (addr >> 32) & 0xff,
311 (addr >> 24) & 0xff,
312 (addr >> 16) & 0xff,
313 (addr >> 8) & 0xff,
314 addr & 0xff);
Hugo Benichiac52e402017-11-09 00:22:25 +0900315 }
316
317 /**
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900318 * Creates a MacAddress from the given String representation. A valid String representation
319 * for a MacAddress is a series of 6 values in the range [0,ff] printed in hexadecimal
320 * and joined by ':' characters.
321 *
322 * @param addr a String representation of a MAC address.
323 * @return the MacAddress corresponding to the given String representation.
324 * @throws IllegalArgumentException if the given String is not a valid representation.
Hugo Benichiac52e402017-11-09 00:22:25 +0900325 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900326 public static @NonNull MacAddress fromString(@NonNull String addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900327 return new MacAddress(longAddrFromStringAddr(addr));
Hugo Benichiac52e402017-11-09 00:22:25 +0900328 }
329
330 /**
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900331 * Creates a MacAddress from the given byte array representation.
332 * A valid byte array representation for a MacAddress is a non-null array of length 6.
333 *
334 * @param addr a byte array representation of a MAC address.
335 * @return the MacAddress corresponding to the given byte array representation.
336 * @throws IllegalArgumentException if the given byte array is not a valid representation.
Hugo Benichiac52e402017-11-09 00:22:25 +0900337 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900338 public static @NonNull MacAddress fromBytes(@NonNull byte[] addr) {
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900339 return new MacAddress(longAddrFromByteAddr(addr));
340 }
341
342 /**
343 * Returns a generated MAC address whose 24 least significant bits constituting the
Jong Wook Kimf0a55cc2018-01-31 19:03:19 -0800344 * NIC part of the address are randomly selected and has Google OUI base.
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900345 *
346 * The locally assigned bit is always set to 1. The multicast bit is always set to 0.
347 *
Jong Wook Kimf0a55cc2018-01-31 19:03:19 -0800348 * @return a random locally assigned, unicast MacAddress with Google OUI.
349 *
350 * @hide
351 */
352 public static @NonNull MacAddress createRandomUnicastAddressWithGoogleBase() {
353 return createRandomUnicastAddress(BASE_GOOGLE_MAC, new SecureRandom());
354 }
355
356 /**
357 * Returns a generated MAC address whose 46 bits, excluding the locally assigned bit and the
358 * unicast bit, are randomly selected.
359 *
360 * The locally assigned bit is always set to 1. The multicast bit is always set to 0.
361 *
362 * @return a random locally assigned, unicast MacAddress.
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900363 *
364 * @hide
365 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900366 public static @NonNull MacAddress createRandomUnicastAddress() {
xshub4836072019-08-21 13:40:18 -0700367 return createRandomUnicastAddress(null, new SecureRandom());
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900368 }
369
370 /**
371 * Returns a randomly generated MAC address using the given Random object and the same
372 * OUI values as the given MacAddress.
373 *
374 * The locally assigned bit is always set to 1. The multicast bit is always set to 0.
375 *
376 * @param base a base MacAddress whose OUI is used for generating the random address.
xshub4836072019-08-21 13:40:18 -0700377 * If base == null then the OUI will also be randomized.
Hugo Benichi84bb7fc2017-11-16 14:40:16 +0900378 * @param r a standard Java Random object used for generating the random address.
379 * @return a random locally assigned MacAddress.
380 *
381 * @hide
382 */
Hugo Benichi48872c62018-01-12 09:46:29 +0900383 public static @NonNull MacAddress createRandomUnicastAddress(MacAddress base, Random r) {
xshub4836072019-08-21 13:40:18 -0700384 long addr;
385 if (base == null) {
386 addr = r.nextLong() & VALID_LONG_MASK;
387 } else {
388 addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong());
389 }
Jong Wook Kimf0a55cc2018-01-31 19:03:19 -0800390 addr |= LOCALLY_ASSIGNED_MASK;
391 addr &= ~MULTICAST_MASK;
xshu683b7562019-08-16 10:20:22 -0700392 MacAddress mac = new MacAddress(addr);
xshub4836072019-08-21 13:40:18 -0700393 if (mac.equals(DEFAULT_MAC_ADDRESS)) {
xshu683b7562019-08-16 10:20:22 -0700394 return createRandomUnicastAddress(base, r);
395 }
396 return mac;
Hugo Benichiac52e402017-11-09 00:22:25 +0900397 }
398
399 // Convenience function for working around the lack of byte literals.
400 private static byte[] addr(int... in) {
401 if (in.length != ETHER_ADDR_LEN) {
402 throw new IllegalArgumentException(Arrays.toString(in)
403 + " was not an array with length equal to " + ETHER_ADDR_LEN);
404 }
405 byte[] out = new byte[ETHER_ADDR_LEN];
406 for (int i = 0; i < ETHER_ADDR_LEN; i++) {
407 out[i] = (byte) in[i];
408 }
409 return out;
Hugo Benichi59c8e422017-10-12 21:33:40 +0900410 }
Roshan Pius99cfe092018-10-05 09:42:19 -0700411
412 /**
413 * Checks if this MAC Address matches the provided range.
414 *
415 * @param baseAddress MacAddress representing the base address to compare with.
416 * @param mask MacAddress representing the mask to use during comparison.
417 * @return true if this MAC Address matches the given range.
418 *
Roshan Pius99cfe092018-10-05 09:42:19 -0700419 */
420 public boolean matches(@NonNull MacAddress baseAddress, @NonNull MacAddress mask) {
421 Preconditions.checkNotNull(baseAddress);
422 Preconditions.checkNotNull(mask);
423 return (mAddr & mask.mAddr) == (baseAddress.mAddr & mask.mAddr);
424 }
Etan Cohena4824cf2018-11-02 15:07:20 -0700425
426 /**
427 * Create a link-local Inet6Address from the MAC address. The EUI-48 MAC address is converted
428 * to an EUI-64 MAC address per RFC 4291. The resulting EUI-64 is used to construct a link-local
429 * IPv6 address per RFC 4862.
430 *
431 * @return A link-local Inet6Address constructed from the MAC address.
Etan Cohena4824cf2018-11-02 15:07:20 -0700432 */
433 public @Nullable Inet6Address getLinkLocalIpv6FromEui48Mac() {
434 byte[] macEui48Bytes = toByteArray();
435 byte[] addr = new byte[16];
436
437 addr[0] = (byte) 0xfe;
438 addr[1] = (byte) 0x80;
439 addr[8] = (byte) (macEui48Bytes[0] ^ (byte) 0x02); // flip the link-local bit
440 addr[9] = macEui48Bytes[1];
441 addr[10] = macEui48Bytes[2];
442 addr[11] = (byte) 0xff;
443 addr[12] = (byte) 0xfe;
444 addr[13] = macEui48Bytes[3];
445 addr[14] = macEui48Bytes[4];
446 addr[15] = macEui48Bytes[5];
447
448 try {
449 return Inet6Address.getByAddress(null, addr, 0);
450 } catch (UnknownHostException e) {
451 return null;
452 }
453 }
Hugo Benichi59c8e422017-10-12 21:33:40 +0900454}