Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b483607a authored by xshu's avatar xshu
Browse files

Followup to CL 1103896

Optimizes the logic that ensures the default randomized MAC is not
returned.

Bug: 137796328
Test: atest MacAddressTest
Change-Id: Ie95d7a020bfac8850b8a67e4d396de77db22e1db
parent bc637172
Loading
Loading
Loading
Loading
+12 −13
Original line number Diff line number Diff line
@@ -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;