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 | 33f9217 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
xshu | 683b756 | 2019-08-16 10:20:22 -0700 | [diff] [blame] | 23 | import android.net.wifi.WifiInfo; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 24 | import android.os.Parcel; |
| 25 | import android.os.Parcelable; |
| 26 | |
| 27 | import com.android.internal.util.BitUtils; |
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; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 36 | import java.util.Random; |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 37 | |
| 38 | /** |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 39 | * 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 Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 43 | */ |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 44 | public final class MacAddress implements Parcelable { |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 45 | |
| 46 | private static final int ETHER_ADDR_LEN = 6; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 47 | 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] | 48 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 49 | /** |
| 50 | * The MacAddress representing the unique broadcast MAC address. |
| 51 | */ |
| 52 | public static final MacAddress BROADCAST_ADDRESS = MacAddress.fromBytes(ETHER_ADDR_BROADCAST); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 53 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 54 | /** |
| 55 | * The MacAddress zero MAC address. |
Remi NGUYEN VAN | 31f1d0c | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 56 | * |
| 57 | * <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] | 58 | * @hide |
| 59 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 60 | @UnsupportedAppUsage |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 61 | public static final MacAddress ALL_ZEROS_ADDRESS = new MacAddress(0); |
| 62 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 63 | /** @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 Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 72 | |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 73 | /** @hide Indicates a MAC address of unknown type. */ |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 74 | 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 Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 81 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 82 | 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"); |
xshu | b483607 | 2019-08-21 13:40:18 -0700 | [diff] [blame] | 88 | /** 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 Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 91 | |
| 92 | // Internal representation of the MAC address as a single 8 byte long. |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 93 | // 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] | 94 | // 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] | 95 | // 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 Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 100 | mAddr = (VALID_LONG_MASK & addr); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 101 | } |
| 102 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 103 | /** |
| 104 | * Returns the type of this address. |
| 105 | * |
| 106 | * @return the int constant representing the MAC address type of this MacAddress. |
| 107 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 108 | public @MacAddressType int getAddressType() { |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 109 | if (equals(BROADCAST_ADDRESS)) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 110 | return TYPE_BROADCAST; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 111 | } |
| 112 | if (isMulticastAddress()) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 113 | return TYPE_MULTICAST; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 114 | } |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 115 | return TYPE_UNICAST; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 116 | } |
| 117 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 118 | /** |
| 119 | * @return true if this MacAddress is a multicast address. |
| 120 | * @hide |
| 121 | */ |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 122 | public boolean isMulticastAddress() { |
| 123 | return (mAddr & MULTICAST_MASK) != 0; |
| 124 | } |
| 125 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 126 | /** |
| 127 | * @return true if this MacAddress is a locally assigned address. |
| 128 | */ |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 129 | public boolean isLocallyAssigned() { |
| 130 | return (mAddr & LOCALLY_ASSIGNED_MASK) != 0; |
| 131 | } |
| 132 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 133 | /** |
| 134 | * @return a byte array representation of this MacAddress. |
| 135 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 136 | public @NonNull byte[] toByteArray() { |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 137 | return byteAddrFromLongAddr(mAddr); |
| 138 | } |
| 139 | |
| 140 | @Override |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 141 | public @NonNull String toString() { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 142 | return stringAddrFromLongAddr(mAddr); |
| 143 | } |
| 144 | |
| 145 | /** |
Hugo Benichi | a0ecf38 | 2017-12-15 10:07:35 +0900 | [diff] [blame] | 146 | * @return a String representation of the OUI part of this MacAddress made of 3 hexadecimal |
| 147 | * numbers in [0,ff] joined by ':' characters. |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 148 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 149 | public @NonNull String toOuiString() { |
Hugo Benichi | a0ecf38 | 2017-12-15 10:07:35 +0900 | [diff] [blame] | 150 | return String.format( |
| 151 | "%02x:%02x:%02x", (mAddr >> 40) & 0xff, (mAddr >> 32) & 0xff, (mAddr >> 24) & 0xff); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 152 | } |
| 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 Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 174 | public static final @android.annotation.NonNull Parcelable.Creator<MacAddress> CREATOR = |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 175 | 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 Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 185 | /** |
| 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 Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 194 | public static boolean isMacAddress(byte[] addr) { |
| 195 | return addr != null && addr.length == ETHER_ADDR_LEN; |
| 196 | } |
| 197 | |
| 198 | /** |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 199 | * 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 Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 208 | */ |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 209 | public static int macAddressType(byte[] addr) { |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 210 | if (!isMacAddress(addr)) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 211 | return TYPE_UNKNOWN; |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 212 | } |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 213 | return MacAddress.fromBytes(addr).getAddressType(); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 214 | } |
| 215 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 216 | /** |
| 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 Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 227 | public static @NonNull byte[] byteAddrFromStringAddr(String addr) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 228 | Preconditions.checkNotNull(addr); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 229 | String[] parts = addr.split(":"); |
| 230 | if (parts.length != ETHER_ADDR_LEN) { |
| 231 | throw new IllegalArgumentException(addr + " was not a valid MAC address"); |
Hugo Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 232 | } |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 233 | 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 Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 244 | /** |
| 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 Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 255 | public static @NonNull String stringAddrFromByteAddr(byte[] addr) { |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 256 | if (!isMacAddress(addr)) { |
| 257 | return null; |
| 258 | } |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 259 | return String.format("%02x:%02x:%02x:%02x:%02x:%02x", |
| 260 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 261 | } |
| 262 | |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 263 | private static byte[] byteAddrFromLongAddr(long addr) { |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 264 | 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 Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 273 | private static long longAddrFromByteAddr(byte[] addr) { |
| 274 | Preconditions.checkNotNull(addr); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 275 | 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 Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 286 | // 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 Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 290 | 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 Benichi | d2c5b19 | 2017-12-05 13:14:08 +0900 | [diff] [blame] | 295 | for (int i = 0; i < parts.length; i++) { |
| 296 | int x = Integer.valueOf(parts[i], 16); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 297 | 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 Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 305 | // Internal conversion function equivalent to stringAddrFromByteAddr(byteAddrFromLongAddr(addr)) |
| 306 | // that avoids the allocation of an intermediary byte[]. |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 307 | private static @NonNull String stringAddrFromLongAddr(long addr) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 308 | 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 Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 318 | * 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 Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 325 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 326 | public static @NonNull MacAddress fromString(@NonNull String addr) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 327 | return new MacAddress(longAddrFromStringAddr(addr)); |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 331 | * 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 Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 337 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 338 | public static @NonNull MacAddress fromBytes(@NonNull byte[] addr) { |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 339 | return new MacAddress(longAddrFromByteAddr(addr)); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * 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] | 344 | * 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] | 345 | * |
| 346 | * The locally assigned bit is always set to 1. The multicast bit is always set to 0. |
| 347 | * |
Jong Wook Kim | f0a55cc | 2018-01-31 19:03:19 -0800 | [diff] [blame] | 348 | * @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 Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 363 | * |
| 364 | * @hide |
| 365 | */ |
Hugo Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 366 | public static @NonNull MacAddress createRandomUnicastAddress() { |
xshu | b483607 | 2019-08-21 13:40:18 -0700 | [diff] [blame] | 367 | return createRandomUnicastAddress(null, new SecureRandom()); |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 368 | } |
| 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. |
xshu | b483607 | 2019-08-21 13:40:18 -0700 | [diff] [blame] | 377 | * If base == null then the OUI will also be randomized. |
Hugo Benichi | 84bb7fc | 2017-11-16 14:40:16 +0900 | [diff] [blame] | 378 | * @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 Benichi | 48872c6 | 2018-01-12 09:46:29 +0900 | [diff] [blame] | 383 | public static @NonNull MacAddress createRandomUnicastAddress(MacAddress base, Random r) { |
xshu | b483607 | 2019-08-21 13:40:18 -0700 | [diff] [blame] | 384 | 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 Kim | f0a55cc | 2018-01-31 19:03:19 -0800 | [diff] [blame] | 390 | addr |= LOCALLY_ASSIGNED_MASK; |
| 391 | addr &= ~MULTICAST_MASK; |
xshu | 683b756 | 2019-08-16 10:20:22 -0700 | [diff] [blame] | 392 | MacAddress mac = new MacAddress(addr); |
xshu | b483607 | 2019-08-21 13:40:18 -0700 | [diff] [blame] | 393 | if (mac.equals(DEFAULT_MAC_ADDRESS)) { |
xshu | 683b756 | 2019-08-16 10:20:22 -0700 | [diff] [blame] | 394 | return createRandomUnicastAddress(base, r); |
| 395 | } |
| 396 | return mac; |
Hugo Benichi | ac52e40 | 2017-11-09 00:22:25 +0900 | [diff] [blame] | 397 | } |
| 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 Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 410 | } |
Roshan Pius | 99cfe09 | 2018-10-05 09:42:19 -0700 | [diff] [blame] | 411 | |
| 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 Pius | 99cfe09 | 2018-10-05 09:42:19 -0700 | [diff] [blame] | 419 | */ |
| 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 Cohen | a4824cf | 2018-11-02 15:07:20 -0700 | [diff] [blame] | 425 | |
| 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 Cohen | a4824cf | 2018-11-02 15:07:20 -0700 | [diff] [blame] | 432 | */ |
| 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 Benichi | 59c8e42 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 454 | } |