diff options
| author | Oscar Shu <xshu@google.com> | 2019-09-24 15:41:00 -0700 |
|---|---|---|
| committer | android-build-merger <android-build-merger@google.com> | 2019-09-24 15:41:00 -0700 |
| commit | 7015d3f7560cdb0ae4319f4ef8bcb1a2d232d252 (patch) | |
| tree | 533aedc3cb28315f9c54eb0848280d7ff5dac4de /core/java | |
| parent | 8f473fb241e3e36e19b267d4d0f92cbce558c9be (diff) | |
| parent | 23d581fe2cf482a40e97f34d39c78429c35f3d84 (diff) | |
Merge "Followup to CL 1103896" am: 54bb8a1625
am: 23d581fe2c
Change-Id: I7625bcd13f7ce9064e71645d21af39e8034d1a11
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/net/MacAddress.java | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/core/java/android/net/MacAddress.java b/core/java/android/net/MacAddress.java index a809b28c9204..2cf2a6514e77 100644 --- a/core/java/android/net/MacAddress.java +++ b/core/java/android/net/MacAddress.java @@ -85,6 +85,9 @@ public final class MacAddress implements Parcelable { private static final long OUI_MASK = MacAddress.fromString("ff:ff:ff:0:0:0").mAddr; private static final long NIC_MASK = MacAddress.fromString("0:0:0:ff:ff:ff").mAddr; private static final MacAddress BASE_GOOGLE_MAC = MacAddress.fromString("da:a1:19:0:0:0"); + /** Default wifi MAC address used for a special purpose **/ + private static final MacAddress DEFAULT_MAC_ADDRESS = + MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS); // Internal representation of the MAC address as a single 8 byte long. // The encoding scheme sets the two most significant bytes to 0. The 6 bytes of the @@ -361,16 +364,7 @@ public final class MacAddress implements Parcelable { * @hide */ public static @NonNull MacAddress createRandomUnicastAddress() { - SecureRandom r = new SecureRandom(); - long addr = r.nextLong() & VALID_LONG_MASK; - addr |= LOCALLY_ASSIGNED_MASK; - addr &= ~MULTICAST_MASK; - MacAddress mac = new MacAddress(addr); - // WifiInfo.DEFAULT_MAC_ADDRESS is being used for another purpose, so do not use it here. - if (mac.toString().equals(WifiInfo.DEFAULT_MAC_ADDRESS)) { - return createRandomUnicastAddress(); - } - return mac; + return createRandomUnicastAddress(null, new SecureRandom()); } /** @@ -380,18 +374,23 @@ public final class MacAddress implements Parcelable { * The locally assigned bit is always set to 1. The multicast bit is always set to 0. * * @param base a base MacAddress whose OUI is used for generating the random address. + * If base == null then the OUI will also be randomized. * @param r a standard Java Random object used for generating the random address. * @return a random locally assigned MacAddress. * * @hide */ public static @NonNull MacAddress createRandomUnicastAddress(MacAddress base, Random r) { - long addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong()); + long addr; + if (base == null) { + addr = r.nextLong() & VALID_LONG_MASK; + } else { + addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong()); + } addr |= LOCALLY_ASSIGNED_MASK; addr &= ~MULTICAST_MASK; MacAddress mac = new MacAddress(addr); - // WifiInfo.DEFAULT_MAC_ADDRESS is being used for another purpose, so do not use it here. - if (mac.toString().equals(WifiInfo.DEFAULT_MAC_ADDRESS)) { + if (mac.equals(DEFAULT_MAC_ADDRESS)) { return createRandomUnicastAddress(base, r); } return mac; |
