Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Etan Cohen | a4824cf | 2018-11-02 15:07:20 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
Aaron Huang | 94df39a | 2019-12-17 00:33:18 +0800 | [diff] [blame] | 23 | import android.net.util.MacAddressUtils; |
xshu | 683b756 | 2019-08-16 10:20:22 -0700 | [diff] [blame] | 24 | import android.net.wifi.WifiInfo; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
| 27 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 28 | import com.android.internal.util.Preconditions; |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 29 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 30 | import java.lang.annotation.Retention; |
| 31 | import java.lang.annotation.RetentionPolicy; |
Etan Cohen | a4824cf | 2018-11-02 15:07:20 -0700 | [diff] [blame] | 32 | import java.net.Inet6Address; |
| 33 | import java.net.UnknownHostException; |
Jong Wook Kim | f0a55cc | 2018-01-31 19:03:19 -0800 | [diff] [blame] | 34 | import java.security.SecureRandom; |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 35 | import java.util.Arrays; |
| 36 | |
| 37 | /** |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 38 | * Representation of a MAC address. |
| 39 | * |
| 40 | * This class only supports 48 bits long addresses and does not support 64 bits long addresses. |
| 41 | * Instances of this class are immutable. |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 42 | */ |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 43 | public final class MacAddress implements Parcelable { |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 44 | |
| 45 | private static final int ETHER_ADDR_LEN = 6; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 46 | private static final byte[] ETHER_ADDR_BROADCAST = addr(0xff, 0xff, 0xff, 0xff, 0xff, 0xff); |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 47 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 48 | /** |
| 49 | * The MacAddress representing the unique broadcast MAC address. |
| 50 | */ |
| 51 | public static final MacAddress BROADCAST_ADDRESS = MacAddress.fromBytes(ETHER_ADDR_BROADCAST); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 52 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 53 | /** |
| 54 | * The MacAddress zero MAC address. |
Remi NGUYEN VAN | 31f1d0c | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 55 | * |
| 56 | * <p>Not publicly exposed or treated specially since the OUI 00:00:00 is registered. |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 57 | * @hide |
| 58 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 59 | @UnsupportedAppUsage |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 60 | public static final MacAddress ALL_ZEROS_ADDRESS = new MacAddress(0); |
| 61 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 62 | /** @hide */ |
| 63 | @Retention(RetentionPolicy.SOURCE) |
| 64 | @IntDef(prefix = { "TYPE_" }, value = { |
| 65 | TYPE_UNKNOWN, |
| 66 | TYPE_UNICAST, |
| 67 | TYPE_MULTICAST, |
| 68 | TYPE_BROADCAST, |
| 69 | }) |
| 70 | public @interface MacAddressType { } |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 71 | |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 72 | /** @hide Indicates a MAC address of unknown type. */ |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 73 | public static final int TYPE_UNKNOWN = 0; |
| 74 | /** Indicates a MAC address is a unicast address. */ |
| 75 | public static final int TYPE_UNICAST = 1; |
| 76 | /** Indicates a MAC address is a multicast address. */ |
| 77 | public static final int TYPE_MULTICAST = 2; |
| 78 | /** Indicates a MAC address is the broadcast address. */ |
| 79 | public static final int TYPE_BROADCAST = 3; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 80 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 81 | private static final long VALID_LONG_MASK = (1L << 48) - 1; |
| 82 | private static final long LOCALLY_ASSIGNED_MASK = MacAddress.fromString("2:0:0:0:0:0").mAddr; |
| 83 | private static final long MULTICAST_MASK = MacAddress.fromString("1:0:0:0:0:0").mAddr; |
| 84 | private static final long OUI_MASK = MacAddress.fromString("ff:ff:ff:0:0:0").mAddr; |
| 85 | private static final long NIC_MASK = MacAddress.fromString("0:0:0:ff:ff:ff").mAddr; |
| 86 | private static final MacAddress BASE_GOOGLE_MAC = MacAddress.fromString("da:a1:19:0:0:0"); |
xshu | b483607 | 2019-08-21 13:40:18 -0700 | [diff] [blame] | 87 | /** Default wifi MAC address used for a special purpose **/ |
| 88 | private static final MacAddress DEFAULT_MAC_ADDRESS = |
| 89 | MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS); |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 90 | |
| 91 | // Internal representation of the MAC address as a single 8 byte long. |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 92 | // The encoding scheme sets the two most significant bytes to 0. The 6 bytes of the |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 93 | // MAC address are encoded in the 6 least significant bytes of the long, where the first |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 94 | // byte of the array is mapped to the 3rd highest logical byte of the long, the second |
| 95 | // byte of the array is mapped to the 4th highest logical byte of the long, and so on. |
| 96 | private final long mAddr; |
| 97 | |
| 98 | private MacAddress(long addr) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 99 | mAddr = (VALID_LONG_MASK & addr); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 100 | } |
| 101 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 102 | /** |
| 103 | * Returns the type of this address. |
| 104 | * |
| 105 | * @return the int constant representing the MAC address type of this MacAddress. |
| 106 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 107 | public @MacAddressType int getAddressType() { |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 108 | if (equals(BROADCAST_ADDRESS)) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 109 | return TYPE_BROADCAST; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 110 | } |
Aaron Huang | 94df39a | 2019-12-17 00:33:18 +0800 | [diff] [blame] | 111 | if ((mAddr & MULTICAST_MASK) != 0) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 112 | return TYPE_MULTICAST; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 113 | } |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 114 | return TYPE_UNICAST; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 117 | /** |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 118 | * @return true if this MacAddress is a locally assigned address. |
| 119 | */ |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 120 | public boolean isLocallyAssigned() { |
| 121 | return (mAddr & LOCALLY_ASSIGNED_MASK) != 0; |
| 122 | } |
| 123 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 124 | /** |
| 125 | * @return a byte array representation of this MacAddress. |
| 126 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 127 | public @NonNull byte[] toByteArray() { |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 128 | return byteAddrFromLongAddr(mAddr); |
| 129 | } |
| 130 | |
| 131 | @Override |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 132 | public @NonNull String toString() { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 133 | return stringAddrFromLongAddr(mAddr); |
| 134 | } |
| 135 | |
| 136 | /** |
Hugo Benichi | a0ecf38 | 2017-12-15 10:07:35 +0900 | [diff] [blame] | 137 | * @return a String representation of the OUI part of this MacAddress made of 3 hexadecimal |
| 138 | * numbers in [0,ff] joined by ':' characters. |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 139 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 140 | public @NonNull String toOuiString() { |
Hugo Benichi | a0ecf38 | 2017-12-15 10:07:35 +0900 | [diff] [blame] | 141 | return String.format( |
| 142 | "%02x:%02x:%02x", (mAddr >> 40) & 0xff, (mAddr >> 32) & 0xff, (mAddr >> 24) & 0xff); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public int hashCode() { |
| 147 | return (int) ((mAddr >> 32) ^ mAddr); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean equals(Object o) { |
| 152 | return (o instanceof MacAddress) && ((MacAddress) o).mAddr == mAddr; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void writeToParcel(Parcel out, int flags) { |
| 157 | out.writeLong(mAddr); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public int describeContents() { |
| 162 | return 0; |
| 163 | } |
| 164 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 165 | public static final @android.annotation.NonNull Parcelable.Creator<MacAddress> CREATOR = |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 166 | new Parcelable.Creator<MacAddress>() { |
| 167 | public MacAddress createFromParcel(Parcel in) { |
| 168 | return new MacAddress(in.readLong()); |
| 169 | } |
| 170 | |
| 171 | public MacAddress[] newArray(int size) { |
| 172 | return new MacAddress[size]; |
| 173 | } |
| 174 | }; |
| 175 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 176 | /** |
| 177 | * Returns true if the given byte array is an valid MAC address. |
| 178 | * A valid byte array representation for a MacAddress is a non-null array of length 6. |
| 179 | * |
| 180 | * @param addr a byte array. |
| 181 | * @return true if the given byte array is not null and has the length of a MAC address. |
| 182 | * |
| 183 | * @hide |
| 184 | */ |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 185 | public static boolean isMacAddress(byte[] addr) { |
Aaron Huang | 94df39a | 2019-12-17 00:33:18 +0800 | [diff] [blame] | 186 | return MacAddressUtils.isMacAddress(addr); |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 190 | * Returns the MAC address type of the MAC address represented by the given byte array, |
| 191 | * or null if the given byte array does not represent a MAC address. |
| 192 | * A valid byte array representation for a MacAddress is a non-null array of length 6. |
| 193 | * |
| 194 | * @param addr a byte array representing a MAC address. |
| 195 | * @return the int constant representing the MAC address type of the MAC address represented |
| 196 | * by the given byte array, or type UNKNOWN if the byte array is not a valid MAC address. |
| 197 | * |
| 198 | * @hide |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 199 | */ |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 200 | public static int macAddressType(byte[] addr) { |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 201 | if (!isMacAddress(addr)) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 202 | return TYPE_UNKNOWN; |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 203 | } |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 204 | return MacAddress.fromBytes(addr).getAddressType(); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 205 | } |
| 206 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 207 | /** |
| 208 | * Converts a String representation of a MAC address to a byte array representation. |
| 209 | * A valid String representation for a MacAddress is a series of 6 values in the |
| 210 | * range [0,ff] printed in hexadecimal and joined by ':' characters. |
| 211 | * |
| 212 | * @param addr a String representation of a MAC address. |
| 213 | * @return the byte representation of the MAC address. |
| 214 | * @throws IllegalArgumentException if the given String is not a valid representation. |
| 215 | * |
| 216 | * @hide |
| 217 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 218 | public static @NonNull byte[] byteAddrFromStringAddr(String addr) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 219 | Preconditions.checkNotNull(addr); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 220 | String[] parts = addr.split(":"); |
| 221 | if (parts.length != ETHER_ADDR_LEN) { |
| 222 | throw new IllegalArgumentException(addr + " was not a valid MAC address"); |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 223 | } |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 224 | byte[] bytes = new byte[ETHER_ADDR_LEN]; |
| 225 | for (int i = 0; i < ETHER_ADDR_LEN; i++) { |
| 226 | int x = Integer.valueOf(parts[i], 16); |
| 227 | if (x < 0 || 0xff < x) { |
| 228 | throw new IllegalArgumentException(addr + "was not a valid MAC address"); |
| 229 | } |
| 230 | bytes[i] = (byte) x; |
| 231 | } |
| 232 | return bytes; |
| 233 | } |
| 234 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 235 | /** |
| 236 | * Converts a byte array representation of a MAC address to a String representation made |
| 237 | * of 6 hexadecimal numbers in [0,ff] joined by ':' characters. |
| 238 | * A valid byte array representation for a MacAddress is a non-null array of length 6. |
| 239 | * |
| 240 | * @param addr a byte array representation of a MAC address. |
| 241 | * @return the String representation of the MAC address. |
| 242 | * @throws IllegalArgumentException if the given byte array is not a valid representation. |
| 243 | * |
| 244 | * @hide |
| 245 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 246 | public static @NonNull String stringAddrFromByteAddr(byte[] addr) { |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 247 | if (!isMacAddress(addr)) { |
| 248 | return null; |
| 249 | } |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 250 | return String.format("%02x:%02x:%02x:%02x:%02x:%02x", |
| 251 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 252 | } |
| 253 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 254 | private static byte[] byteAddrFromLongAddr(long addr) { |
Aaron Huang | 94df39a | 2019-12-17 00:33:18 +0800 | [diff] [blame] | 255 | return MacAddressUtils.byteAddrFromLongAddr(addr); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 258 | private static long longAddrFromByteAddr(byte[] addr) { |
Aaron Huang | 94df39a | 2019-12-17 00:33:18 +0800 | [diff] [blame] | 259 | return MacAddressUtils.longAddrFromByteAddr(addr); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 260 | } |
| 261 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 262 | // Internal conversion function equivalent to longAddrFromByteAddr(byteAddrFromStringAddr(addr)) |
| 263 | // that avoids the allocation of an intermediary byte[]. |
| 264 | private static long longAddrFromStringAddr(String addr) { |
| 265 | Preconditions.checkNotNull(addr); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 266 | String[] parts = addr.split(":"); |
| 267 | if (parts.length != ETHER_ADDR_LEN) { |
| 268 | throw new IllegalArgumentException(addr + " was not a valid MAC address"); |
| 269 | } |
| 270 | long longAddr = 0; |
Hugo Benichi | d2c5b19 | 2017-12-05 13:14:08 +0900 | [diff] [blame] | 271 | for (int i = 0; i < parts.length; i++) { |
| 272 | int x = Integer.valueOf(parts[i], 16); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 273 | if (x < 0 || 0xff < x) { |
| 274 | throw new IllegalArgumentException(addr + "was not a valid MAC address"); |
| 275 | } |
| 276 | longAddr = x + (longAddr << 8); |
| 277 | } |
| 278 | return longAddr; |
| 279 | } |
| 280 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 281 | // Internal conversion function equivalent to stringAddrFromByteAddr(byteAddrFromLongAddr(addr)) |
| 282 | // that avoids the allocation of an intermediary byte[]. |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 283 | private static @NonNull String stringAddrFromLongAddr(long addr) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 284 | return String.format("%02x:%02x:%02x:%02x:%02x:%02x", |
| 285 | (addr >> 40) & 0xff, |
| 286 | (addr >> 32) & 0xff, |
| 287 | (addr >> 24) & 0xff, |
| 288 | (addr >> 16) & 0xff, |
| 289 | (addr >> 8) & 0xff, |
| 290 | addr & 0xff); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 294 | * Creates a MacAddress from the given String representation. A valid String representation |
| 295 | * for a MacAddress is a series of 6 values in the range [0,ff] printed in hexadecimal |
| 296 | * and joined by ':' characters. |
| 297 | * |
| 298 | * @param addr a String representation of a MAC address. |
| 299 | * @return the MacAddress corresponding to the given String representation. |
| 300 | * @throws IllegalArgumentException if the given String is not a valid representation. |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 301 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 302 | public static @NonNull MacAddress fromString(@NonNull String addr) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 303 | return new MacAddress(longAddrFromStringAddr(addr)); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 307 | * Creates a MacAddress from the given byte array representation. |
| 308 | * A valid byte array representation for a MacAddress is a non-null array of length 6. |
| 309 | * |
| 310 | * @param addr a byte array representation of a MAC address. |
| 311 | * @return the MacAddress corresponding to the given byte array representation. |
| 312 | * @throws IllegalArgumentException if the given byte array is not a valid representation. |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 313 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 314 | public static @NonNull MacAddress fromBytes(@NonNull byte[] addr) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 315 | return new MacAddress(longAddrFromByteAddr(addr)); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Returns a generated MAC address whose 24 least significant bits constituting the |
Jong Wook Kim | f0a55cc | 2018-01-31 19:03:19 -0800 | [diff] [blame] | 320 | * NIC part of the address are randomly selected and has Google OUI base. |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 321 | * |
| 322 | * The locally assigned bit is always set to 1. The multicast bit is always set to 0. |
| 323 | * |
Jong Wook Kim | f0a55cc | 2018-01-31 19:03:19 -0800 | [diff] [blame] | 324 | * @return a random locally assigned, unicast MacAddress with Google OUI. |
| 325 | * |
| 326 | * @hide |
| 327 | */ |
| 328 | public static @NonNull MacAddress createRandomUnicastAddressWithGoogleBase() { |
Aaron Huang | 94df39a | 2019-12-17 00:33:18 +0800 | [diff] [blame] | 329 | return MacAddressUtils.createRandomUnicastAddress(BASE_GOOGLE_MAC, new SecureRandom()); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Convenience function for working around the lack of byte literals. |
| 333 | private static byte[] addr(int... in) { |
| 334 | if (in.length != ETHER_ADDR_LEN) { |
| 335 | throw new IllegalArgumentException(Arrays.toString(in) |
| 336 | + " was not an array with length equal to " + ETHER_ADDR_LEN); |
| 337 | } |
| 338 | byte[] out = new byte[ETHER_ADDR_LEN]; |
| 339 | for (int i = 0; i < ETHER_ADDR_LEN; i++) { |
| 340 | out[i] = (byte) in[i]; |
| 341 | } |
| 342 | return out; |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 343 | } |
Roshan Pius | 99cfe09 | 2018-10-05 09:42:19 -0700 | [diff] [blame] | 344 | |
| 345 | /** |
| 346 | * Checks if this MAC Address matches the provided range. |
| 347 | * |
| 348 | * @param baseAddress MacAddress representing the base address to compare with. |
| 349 | * @param mask MacAddress representing the mask to use during comparison. |
| 350 | * @return true if this MAC Address matches the given range. |
| 351 | * |
Roshan Pius | 99cfe09 | 2018-10-05 09:42:19 -0700 | [diff] [blame] | 352 | */ |
| 353 | public boolean matches(@NonNull MacAddress baseAddress, @NonNull MacAddress mask) { |
| 354 | Preconditions.checkNotNull(baseAddress); |
| 355 | Preconditions.checkNotNull(mask); |
| 356 | return (mAddr & mask.mAddr) == (baseAddress.mAddr & mask.mAddr); |
| 357 | } |
Etan Cohen | a4824cf | 2018-11-02 15:07:20 -0700 | [diff] [blame] | 358 | |
| 359 | /** |
| 360 | * Create a link-local Inet6Address from the MAC address. The EUI-48 MAC address is converted |
| 361 | * to an EUI-64 MAC address per RFC 4291. The resulting EUI-64 is used to construct a link-local |
| 362 | * IPv6 address per RFC 4862. |
| 363 | * |
| 364 | * @return A link-local Inet6Address constructed from the MAC address. |
Etan Cohen | a4824cf | 2018-11-02 15:07:20 -0700 | [diff] [blame] | 365 | */ |
| 366 | public @Nullable Inet6Address getLinkLocalIpv6FromEui48Mac() { |
| 367 | byte[] macEui48Bytes = toByteArray(); |
| 368 | byte[] addr = new byte[16]; |
| 369 | |
| 370 | addr[0] = (byte) 0xfe; |
| 371 | addr[1] = (byte) 0x80; |
| 372 | addr[8] = (byte) (macEui48Bytes[0] ^ (byte) 0x02); // flip the link-local bit |
| 373 | addr[9] = macEui48Bytes[1]; |
| 374 | addr[10] = macEui48Bytes[2]; |
| 375 | addr[11] = (byte) 0xff; |
| 376 | addr[12] = (byte) 0xfe; |
| 377 | addr[13] = macEui48Bytes[3]; |
| 378 | addr[14] = macEui48Bytes[4]; |
| 379 | addr[15] = macEui48Bytes[5]; |
| 380 | |
| 381 | try { |
| 382 | return Inet6Address.getByAddress(null, addr, 0); |
| 383 | } catch (UnknownHostException e) { |
| 384 | return null; |
| 385 | } |
| 386 | } |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 387 | } |