Move NetworkUtils used by NetworkStack

Depending on usage move into NetworkStackUtils or shared
Inet4AddressUtils.

Test: atest FrameworksNetTests NetworkStackTests
Bug: 112869080
Merged-In: Ie20dcee375b377236004a7689890729493aca857
Change-Id: Ib8d000529872796022706a35050fdc2c7141f2ab
This commit is contained in:
Remi NGUYEN VAN
2019-01-20 09:35:10 +09:00
parent 455df13823
commit e167e117c1
2 changed files with 23 additions and 321 deletions

View File

@@ -17,8 +17,10 @@
package android.net; package android.net;
import android.annotation.UnsupportedAppUsage; import android.annotation.UnsupportedAppUsage;
import android.net.shared.Inet4AddressUtils;
import android.os.Build; import android.os.Build;
import android.os.Parcel; import android.os.Parcel;
import android.system.ErrnoException;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
@@ -34,8 +36,6 @@ import java.util.Collection;
import java.util.Locale; import java.util.Locale;
import java.util.TreeSet; import java.util.TreeSet;
import android.system.ErrnoException;
/** /**
* Native methods for managing network interfaces. * Native methods for managing network interfaces.
* *
@@ -172,119 +172,37 @@ public class NetworkUtils {
FileDescriptor fd) throws IOException; FileDescriptor fd) throws IOException;
/** /**
* @see #intToInet4AddressHTL(int) * @see Inet4AddressUtils#intToInet4AddressHTL(int)
* @deprecated Use either {@link #intToInet4AddressHTH(int)} * @deprecated Use either {@link Inet4AddressUtils#intToInet4AddressHTH(int)}
* or {@link #intToInet4AddressHTL(int)} * or {@link Inet4AddressUtils#intToInet4AddressHTL(int)}
*/ */
@Deprecated @Deprecated
@UnsupportedAppUsage @UnsupportedAppUsage
public static InetAddress intToInetAddress(int hostAddress) { public static InetAddress intToInetAddress(int hostAddress) {
return intToInet4AddressHTL(hostAddress); return Inet4AddressUtils.intToInet4AddressHTL(hostAddress);
} }
/** /**
* Convert a IPv4 address from an integer to an InetAddress (0x04030201 -> 1.2.3.4) * @see Inet4AddressUtils#inet4AddressToIntHTL(Inet4Address)
* * @deprecated Use either {@link Inet4AddressUtils#inet4AddressToIntHTH(Inet4Address)}
* <p>This method uses the higher-order int bytes as the lower-order IPv4 address bytes, * or {@link Inet4AddressUtils#inet4AddressToIntHTL(Inet4Address)}
* which is an unusual convention. Consider {@link #intToInet4AddressHTH(int)} instead.
* @param hostAddress an int coding for an IPv4 address, where higher-order int byte is
* lower-order IPv4 address byte
*/
public static Inet4Address intToInet4AddressHTL(int hostAddress) {
return intToInet4AddressHTH(Integer.reverseBytes(hostAddress));
}
/**
* Convert a IPv4 address from an integer to an InetAddress (0x01020304 -> 1.2.3.4)
* @param hostAddress an int coding for an IPv4 address
*/
public static Inet4Address intToInet4AddressHTH(int hostAddress) {
byte[] addressBytes = { (byte) (0xff & (hostAddress >> 24)),
(byte) (0xff & (hostAddress >> 16)),
(byte) (0xff & (hostAddress >> 8)),
(byte) (0xff & hostAddress) };
try {
return (Inet4Address) InetAddress.getByAddress(addressBytes);
} catch (UnknownHostException e) {
throw new AssertionError();
}
}
/**
* @see #inet4AddressToIntHTL(Inet4Address)
* @deprecated Use either {@link #inet4AddressToIntHTH(Inet4Address)}
* or {@link #inet4AddressToIntHTL(Inet4Address)}
*/ */
@Deprecated @Deprecated
public static int inetAddressToInt(Inet4Address inetAddr) public static int inetAddressToInt(Inet4Address inetAddr)
throws IllegalArgumentException { throws IllegalArgumentException {
return inet4AddressToIntHTL(inetAddr); return Inet4AddressUtils.inet4AddressToIntHTL(inetAddr);
} }
/** /**
* Convert an IPv4 address from an InetAddress to an integer (1.2.3.4 -> 0x01020304) * @see Inet4AddressUtils#prefixLengthToV4NetmaskIntHTL(int)
* * @deprecated Use either {@link Inet4AddressUtils#prefixLengthToV4NetmaskIntHTH(int)}
* <p>This conversion can help order IP addresses: considering the ordering * or {@link Inet4AddressUtils#prefixLengthToV4NetmaskIntHTL(int)}
* 192.0.2.1 < 192.0.2.2 < ..., resulting ints will follow that ordering if read as unsigned
* integers with {@link Integer#toUnsignedLong}.
* @param inetAddr is an InetAddress corresponding to the IPv4 address
* @return the IP address as integer
*/
public static int inet4AddressToIntHTH(Inet4Address inetAddr)
throws IllegalArgumentException {
byte [] addr = inetAddr.getAddress();
return ((addr[0] & 0xff) << 24) | ((addr[1] & 0xff) << 16)
| ((addr[2] & 0xff) << 8) | (addr[3] & 0xff);
}
/**
* Convert a IPv4 address from an InetAddress to an integer (1.2.3.4 -> 0x04030201)
*
* <p>This method stores the higher-order IPv4 address bytes in the lower-order int bytes,
* which is an unusual convention. Consider {@link #inet4AddressToIntHTH(Inet4Address)} instead.
* @param inetAddr is an InetAddress corresponding to the IPv4 address
* @return the IP address as integer
*/
public static int inet4AddressToIntHTL(Inet4Address inetAddr) {
return Integer.reverseBytes(inet4AddressToIntHTH(inetAddr));
}
/**
* @see #prefixLengthToV4NetmaskIntHTL(int)
* @deprecated Use either {@link #prefixLengthToV4NetmaskIntHTH(int)}
* or {@link #prefixLengthToV4NetmaskIntHTL(int)}
*/ */
@Deprecated @Deprecated
@UnsupportedAppUsage @UnsupportedAppUsage
public static int prefixLengthToNetmaskInt(int prefixLength) public static int prefixLengthToNetmaskInt(int prefixLength)
throws IllegalArgumentException { throws IllegalArgumentException {
return prefixLengthToV4NetmaskIntHTL(prefixLength); return Inet4AddressUtils.prefixLengthToV4NetmaskIntHTL(prefixLength);
}
/**
* Convert a network prefix length to an IPv4 netmask integer (prefixLength 17 -> 0xffff8000)
* @return the IPv4 netmask as an integer
*/
public static int prefixLengthToV4NetmaskIntHTH(int prefixLength)
throws IllegalArgumentException {
if (prefixLength < 0 || prefixLength > 32) {
throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)");
}
// (int)a << b is equivalent to a << (b & 0x1f): can't shift by 32 (-1 << 32 == -1)
return prefixLength == 0 ? 0 : 0xffffffff << (32 - prefixLength);
}
/**
* Convert a network prefix length to an IPv4 netmask integer (prefixLength 17 -> 0x0080ffff).
*
* <p>This method stores the higher-order IPv4 address bytes in the lower-order int bytes,
* which is an unusual convention. Consider {@link #prefixLengthToV4NetmaskIntHTH(int)} instead.
* @return the IPv4 netmask as an integer
*/
public static int prefixLengthToV4NetmaskIntHTL(int prefixLength)
throws IllegalArgumentException {
return Integer.reverseBytes(prefixLengthToV4NetmaskIntHTH(prefixLength));
} }
/** /**
@@ -302,17 +220,13 @@ public class NetworkUtils {
* @return the network prefix length * @return the network prefix length
* @throws IllegalArgumentException the specified netmask was not contiguous. * @throws IllegalArgumentException the specified netmask was not contiguous.
* @hide * @hide
* @deprecated use {@link Inet4AddressUtils#netmaskToPrefixLength(Inet4Address)}
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
@Deprecated
public static int netmaskToPrefixLength(Inet4Address netmask) { public static int netmaskToPrefixLength(Inet4Address netmask) {
// inetAddressToInt returns an int in *network* byte order. // This is only here because some apps seem to be using it (@UnsupportedAppUsage).
int i = Integer.reverseBytes(inetAddressToInt(netmask)); return Inet4AddressUtils.netmaskToPrefixLength(netmask);
int prefixLength = Integer.bitCount(i);
int trailingZeros = Integer.numberOfTrailingZeros(i);
if (trailingZeros != 32 - prefixLength) {
throw new IllegalArgumentException("Non-contiguous netmask: " + Integer.toHexString(i));
}
return prefixLength;
} }
@@ -403,16 +317,8 @@ public class NetworkUtils {
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public static int getImplicitNetmask(Inet4Address address) { public static int getImplicitNetmask(Inet4Address address) {
int firstByte = address.getAddress()[0] & 0xff; // Convert to an unsigned value. // Only here because it seems to be used by apps
if (firstByte < 128) { return Inet4AddressUtils.getImplicitNetmask(address);
return 8;
} else if (firstByte < 192) {
return 16;
} else if (firstByte < 224) {
return 24;
} else {
return 32; // Will likely not end well for other reasons.
}
} }
/** /**
@@ -439,28 +345,6 @@ public class NetworkUtils {
return new Pair<InetAddress, Integer>(address, prefixLength); return new Pair<InetAddress, Integer>(address, prefixLength);
} }
/**
* Get a prefix mask as Inet4Address for a given prefix length.
*
* <p>For example 20 -> 255.255.240.0
*/
public static Inet4Address getPrefixMaskAsInet4Address(int prefixLength)
throws IllegalArgumentException {
return intToInet4AddressHTH(prefixLengthToV4NetmaskIntHTH(prefixLength));
}
/**
* Get the broadcast address for a given prefix.
*
* <p>For example 192.168.0.1/24 -> 192.168.0.255
*/
public static Inet4Address getBroadcastAddress(Inet4Address addr, int prefixLength)
throws IllegalArgumentException {
final int intBroadcastAddr = inet4AddressToIntHTH(addr)
| ~prefixLengthToV4NetmaskIntHTH(prefixLength);
return intToInet4AddressHTH(intBroadcastAddr);
}
/** /**
* Check if IP address type is consistent between two InetAddress. * Check if IP address type is consistent between two InetAddress.
* @return true if both are the same type. False otherwise. * @return true if both are the same type. False otherwise.

View File

@@ -16,161 +16,19 @@
package android.net; package android.net;
import static android.net.NetworkUtils.getImplicitNetmask;
import static android.net.NetworkUtils.inet4AddressToIntHTH;
import static android.net.NetworkUtils.inet4AddressToIntHTL;
import static android.net.NetworkUtils.intToInet4AddressHTH;
import static android.net.NetworkUtils.intToInet4AddressHTL;
import static android.net.NetworkUtils.netmaskToPrefixLength;
import static android.net.NetworkUtils.prefixLengthToV4NetmaskIntHTH;
import static android.net.NetworkUtils.prefixLengthToV4NetmaskIntHTL;
import static android.net.NetworkUtils.getBroadcastAddress;
import static android.net.NetworkUtils.getPrefixMaskAsInet4Address;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.fail;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.TreeSet;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.math.BigInteger;
import java.util.TreeSet;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@android.support.test.filters.SmallTest @android.support.test.filters.SmallTest
public class NetworkUtilsTest { public class NetworkUtilsTest {
private InetAddress Address(String addr) {
return InetAddress.parseNumericAddress(addr);
}
private Inet4Address IPv4Address(String addr) {
return (Inet4Address) Address(addr);
}
@Test
public void testGetImplicitNetmask() {
assertEquals(8, getImplicitNetmask(IPv4Address("4.2.2.2")));
assertEquals(8, getImplicitNetmask(IPv4Address("10.5.6.7")));
assertEquals(16, getImplicitNetmask(IPv4Address("173.194.72.105")));
assertEquals(16, getImplicitNetmask(IPv4Address("172.23.68.145")));
assertEquals(24, getImplicitNetmask(IPv4Address("192.0.2.1")));
assertEquals(24, getImplicitNetmask(IPv4Address("192.168.5.1")));
assertEquals(32, getImplicitNetmask(IPv4Address("224.0.0.1")));
assertEquals(32, getImplicitNetmask(IPv4Address("255.6.7.8")));
}
private void assertInvalidNetworkMask(Inet4Address addr) {
try {
netmaskToPrefixLength(addr);
fail("Invalid netmask " + addr.getHostAddress() + " did not cause exception");
} catch (IllegalArgumentException expected) {
}
}
@Test
public void testInet4AddressToIntHTL() {
assertEquals(0, inet4AddressToIntHTL(IPv4Address("0.0.0.0")));
assertEquals(0x000080ff, inet4AddressToIntHTL(IPv4Address("255.128.0.0")));
assertEquals(0x0080ff0a, inet4AddressToIntHTL(IPv4Address("10.255.128.0")));
assertEquals(0x00feff0a, inet4AddressToIntHTL(IPv4Address("10.255.254.0")));
assertEquals(0xfeffa8c0, inet4AddressToIntHTL(IPv4Address("192.168.255.254")));
assertEquals(0xffffa8c0, inet4AddressToIntHTL(IPv4Address("192.168.255.255")));
}
@Test
public void testIntToInet4AddressHTL() {
assertEquals(IPv4Address("0.0.0.0"), intToInet4AddressHTL(0));
assertEquals(IPv4Address("255.128.0.0"), intToInet4AddressHTL(0x000080ff));
assertEquals(IPv4Address("10.255.128.0"), intToInet4AddressHTL(0x0080ff0a));
assertEquals(IPv4Address("10.255.254.0"), intToInet4AddressHTL(0x00feff0a));
assertEquals(IPv4Address("192.168.255.254"), intToInet4AddressHTL(0xfeffa8c0));
assertEquals(IPv4Address("192.168.255.255"), intToInet4AddressHTL(0xffffa8c0));
}
@Test
public void testInet4AddressToIntHTH() {
assertEquals(0, inet4AddressToIntHTH(IPv4Address("0.0.0.0")));
assertEquals(0xff800000, inet4AddressToIntHTH(IPv4Address("255.128.0.0")));
assertEquals(0x0aff8000, inet4AddressToIntHTH(IPv4Address("10.255.128.0")));
assertEquals(0x0afffe00, inet4AddressToIntHTH(IPv4Address("10.255.254.0")));
assertEquals(0xc0a8fffe, inet4AddressToIntHTH(IPv4Address("192.168.255.254")));
assertEquals(0xc0a8ffff, inet4AddressToIntHTH(IPv4Address("192.168.255.255")));
}
@Test
public void testIntToInet4AddressHTH() {
assertEquals(IPv4Address("0.0.0.0"), intToInet4AddressHTH(0));
assertEquals(IPv4Address("255.128.0.0"), intToInet4AddressHTH(0xff800000));
assertEquals(IPv4Address("10.255.128.0"), intToInet4AddressHTH(0x0aff8000));
assertEquals(IPv4Address("10.255.254.0"), intToInet4AddressHTH(0x0afffe00));
assertEquals(IPv4Address("192.168.255.254"), intToInet4AddressHTH(0xc0a8fffe));
assertEquals(IPv4Address("192.168.255.255"), intToInet4AddressHTH(0xc0a8ffff));
}
@Test
public void testNetmaskToPrefixLength() {
assertEquals(0, netmaskToPrefixLength(IPv4Address("0.0.0.0")));
assertEquals(9, netmaskToPrefixLength(IPv4Address("255.128.0.0")));
assertEquals(17, netmaskToPrefixLength(IPv4Address("255.255.128.0")));
assertEquals(23, netmaskToPrefixLength(IPv4Address("255.255.254.0")));
assertEquals(31, netmaskToPrefixLength(IPv4Address("255.255.255.254")));
assertEquals(32, netmaskToPrefixLength(IPv4Address("255.255.255.255")));
assertInvalidNetworkMask(IPv4Address("0.0.0.1"));
assertInvalidNetworkMask(IPv4Address("255.255.255.253"));
assertInvalidNetworkMask(IPv4Address("255.255.0.255"));
}
@Test
public void testPrefixLengthToV4NetmaskIntHTL() {
assertEquals(0, prefixLengthToV4NetmaskIntHTL(0));
assertEquals(0x000080ff /* 255.128.0.0 */, prefixLengthToV4NetmaskIntHTL(9));
assertEquals(0x0080ffff /* 255.255.128.0 */, prefixLengthToV4NetmaskIntHTL(17));
assertEquals(0x00feffff /* 255.255.254.0 */, prefixLengthToV4NetmaskIntHTL(23));
assertEquals(0xfeffffff /* 255.255.255.254 */, prefixLengthToV4NetmaskIntHTL(31));
assertEquals(0xffffffff /* 255.255.255.255 */, prefixLengthToV4NetmaskIntHTL(32));
}
@Test
public void testPrefixLengthToV4NetmaskIntHTH() {
assertEquals(0, prefixLengthToV4NetmaskIntHTH(0));
assertEquals(0xff800000 /* 255.128.0.0 */, prefixLengthToV4NetmaskIntHTH(9));
assertEquals(0xffff8000 /* 255.255.128.0 */, prefixLengthToV4NetmaskIntHTH(17));
assertEquals(0xfffffe00 /* 255.255.254.0 */, prefixLengthToV4NetmaskIntHTH(23));
assertEquals(0xfffffffe /* 255.255.255.254 */, prefixLengthToV4NetmaskIntHTH(31));
assertEquals(0xffffffff /* 255.255.255.255 */, prefixLengthToV4NetmaskIntHTH(32));
}
@Test(expected = IllegalArgumentException.class)
public void testPrefixLengthToV4NetmaskIntHTH_NegativeLength() {
prefixLengthToV4NetmaskIntHTH(-1);
}
@Test(expected = IllegalArgumentException.class)
public void testPrefixLengthToV4NetmaskIntHTH_LengthTooLarge() {
prefixLengthToV4NetmaskIntHTH(33);
}
private void checkAddressMasking(String expectedAddr, String addr, int prefixLength) {
final int prefix = prefixLengthToV4NetmaskIntHTH(prefixLength);
final int addrInt = inet4AddressToIntHTH(IPv4Address(addr));
assertEquals(IPv4Address(expectedAddr), intToInet4AddressHTH(prefix & addrInt));
}
@Test
public void testPrefixLengthToV4NetmaskIntHTH_MaskAddr() {
checkAddressMasking("192.168.0.0", "192.168.128.1", 16);
checkAddressMasking("255.240.0.0", "255.255.255.255", 12);
checkAddressMasking("255.255.255.255", "255.255.255.255", 32);
checkAddressMasking("0.0.0.0", "255.255.255.255", 0);
}
@Test @Test
public void testRoutedIPv4AddressCount() { public void testRoutedIPv4AddressCount() {
final TreeSet<IpPrefix> set = new TreeSet<>(IpPrefix.lengthComparator()); final TreeSet<IpPrefix> set = new TreeSet<>(IpPrefix.lengthComparator());
@@ -267,44 +125,4 @@ public class NetworkUtilsTest {
assertEquals(BigInteger.valueOf(7l - 4 + 4 + 16 + 65536), assertEquals(BigInteger.valueOf(7l - 4 + 4 + 16 + 65536),
NetworkUtils.routedIPv6AddressCount(set)); NetworkUtils.routedIPv6AddressCount(set));
} }
@Test
public void testGetPrefixMaskAsAddress() {
assertEquals("255.255.240.0", getPrefixMaskAsInet4Address(20).getHostAddress());
assertEquals("255.0.0.0", getPrefixMaskAsInet4Address(8).getHostAddress());
assertEquals("0.0.0.0", getPrefixMaskAsInet4Address(0).getHostAddress());
assertEquals("255.255.255.255", getPrefixMaskAsInet4Address(32).getHostAddress());
}
@Test(expected = IllegalArgumentException.class)
public void testGetPrefixMaskAsAddress_PrefixTooLarge() {
getPrefixMaskAsInet4Address(33);
}
@Test(expected = IllegalArgumentException.class)
public void testGetPrefixMaskAsAddress_NegativePrefix() {
getPrefixMaskAsInet4Address(-1);
}
@Test
public void testGetBroadcastAddress() {
assertEquals("192.168.15.255",
getBroadcastAddress(IPv4Address("192.168.0.123"), 20).getHostAddress());
assertEquals("192.255.255.255",
getBroadcastAddress(IPv4Address("192.168.0.123"), 8).getHostAddress());
assertEquals("192.168.0.123",
getBroadcastAddress(IPv4Address("192.168.0.123"), 32).getHostAddress());
assertEquals("255.255.255.255",
getBroadcastAddress(IPv4Address("192.168.0.123"), 0).getHostAddress());
}
@Test(expected = IllegalArgumentException.class)
public void testGetBroadcastAddress_PrefixTooLarge() {
getBroadcastAddress(IPv4Address("192.168.0.123"), 33);
}
@Test(expected = IllegalArgumentException.class)
public void testGetBroadcastAddress_NegativePrefix() {
getBroadcastAddress(IPv4Address("192.168.0.123"), -1);
}
} }