Revert "Remove framework code that has moved to frameworks/libs/net"

Revert "Use createRandomUnicastAddress from MacAddressUtils"

Revert "Add net-utils-framework-net to telephony-common"

Revert submission 1191997-net-utils-framework-net

Reason for revert: Droidcop-triggered revert due to breakage https://android-build.googleplex.com/builds/quarterdeck?branch=git_qt-qpr1-dev-plus-aosp&target=aosp_taimen-userdebug&lkgb=6208131&lkbb=6208273&fkbb=6208273, bug b/149551544
Reverted Changes:
Ib1c807d64:Use createRandomUnicastAddress from MacAddressUtil...
I9e0f297e0:Add net-utils-framework-net to telephony-common
Ieb8927f9a:Remove framework code that has moved to frameworks...

Change-Id: I2824f781babd9f7e0bb9df524dadf6b8397dcaa1
This commit is contained in:
Isabelle Taylor 2020-02-14 13:09:16 +00:00
commit 175627fafb

View file

@ -20,11 +20,11 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import android.net.util.MacAddressUtils;
import android.net.wifi.WifiInfo;
import android.os.Parcel;
import android.os.Parcelable;
import com.android.internal.util.BitUtils;
import com.android.internal.util.Preconditions;
import java.lang.annotation.Retention;
@ -33,6 +33,7 @@ import java.net.Inet6Address;
import java.net.UnknownHostException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;
/**
* Representation of a MAC address.
@ -108,12 +109,20 @@ public final class MacAddress implements Parcelable {
if (equals(BROADCAST_ADDRESS)) {
return TYPE_BROADCAST;
}
if ((mAddr & MULTICAST_MASK) != 0) {
if (isMulticastAddress()) {
return TYPE_MULTICAST;
}
return TYPE_UNICAST;
}
/**
* @return true if this MacAddress is a multicast address.
* @hide
*/
public boolean isMulticastAddress() {
return (mAddr & MULTICAST_MASK) != 0;
}
/**
* @return true if this MacAddress is a locally assigned address.
*/
@ -183,7 +192,7 @@ public final class MacAddress implements Parcelable {
* @hide
*/
public static boolean isMacAddress(byte[] addr) {
return MacAddressUtils.isMacAddress(addr);
return addr != null && addr.length == ETHER_ADDR_LEN;
}
/**
@ -252,11 +261,26 @@ public final class MacAddress implements Parcelable {
}
private static byte[] byteAddrFromLongAddr(long addr) {
return MacAddressUtils.byteAddrFromLongAddr(addr);
byte[] bytes = new byte[ETHER_ADDR_LEN];
int index = ETHER_ADDR_LEN;
while (index-- > 0) {
bytes[index] = (byte) addr;
addr = addr >> 8;
}
return bytes;
}
private static long longAddrFromByteAddr(byte[] addr) {
return MacAddressUtils.longAddrFromByteAddr(addr);
Preconditions.checkNotNull(addr);
if (!isMacAddress(addr)) {
throw new IllegalArgumentException(
Arrays.toString(addr) + " was not a valid MAC address");
}
long longAddr = 0;
for (byte b : addr) {
longAddr = (longAddr << 8) + BitUtils.uint8(b);
}
return longAddr;
}
// Internal conversion function equivalent to longAddrFromByteAddr(byteAddrFromStringAddr(addr))
@ -326,7 +350,50 @@ public final class MacAddress implements Parcelable {
* @hide
*/
public static @NonNull MacAddress createRandomUnicastAddressWithGoogleBase() {
return MacAddressUtils.createRandomUnicastAddress(BASE_GOOGLE_MAC, new SecureRandom());
return createRandomUnicastAddress(BASE_GOOGLE_MAC, new SecureRandom());
}
/**
* Returns a generated MAC address whose 46 bits, excluding the locally assigned bit and the
* unicast bit, are randomly selected.
*
* The locally assigned bit is always set to 1. The multicast bit is always set to 0.
*
* @return a random locally assigned, unicast MacAddress.
*
* @hide
*/
public static @NonNull MacAddress createRandomUnicastAddress() {
return createRandomUnicastAddress(null, new SecureRandom());
}
/**
* Returns a randomly generated MAC address using the given Random object and the same
* OUI values as the given MacAddress.
*
* 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;
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);
if (mac.equals(DEFAULT_MAC_ADDRESS)) {
return createRandomUnicastAddress(base, r);
}
return mac;
}
// Convenience function for working around the lack of byte literals.