Merge "Move shared methods to net shared lib"
This commit is contained in:
@@ -24,6 +24,8 @@ import android.os.Parcel;
|
|||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
|
||||||
|
import com.android.net.module.util.NetUtils;
|
||||||
|
|
||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.Inet6Address;
|
import java.net.Inet6Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@@ -59,7 +61,7 @@ public final class IpPrefix implements Parcelable {
|
|||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"IpPrefix has " + address.length + " bytes which is neither 4 nor 16");
|
"IpPrefix has " + address.length + " bytes which is neither 4 nor 16");
|
||||||
}
|
}
|
||||||
NetworkUtils.maskRawAddress(address, prefixLength);
|
NetUtils.maskRawAddress(address, prefixLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -190,7 +192,7 @@ public final class IpPrefix implements Parcelable {
|
|||||||
if (addrBytes == null || addrBytes.length != this.address.length) {
|
if (addrBytes == null || addrBytes.length != this.address.length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
NetworkUtils.maskRawAddress(addrBytes, prefixLength);
|
NetUtils.maskRawAddress(addrBytes, prefixLength);
|
||||||
return Arrays.equals(this.address, addrBytes);
|
return Arrays.equals(this.address, addrBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,7 +206,7 @@ public final class IpPrefix implements Parcelable {
|
|||||||
public boolean containsPrefix(@NonNull IpPrefix otherPrefix) {
|
public boolean containsPrefix(@NonNull IpPrefix otherPrefix) {
|
||||||
if (otherPrefix.getPrefixLength() < prefixLength) return false;
|
if (otherPrefix.getPrefixLength() < prefixLength) return false;
|
||||||
final byte[] otherAddress = otherPrefix.getRawAddress();
|
final byte[] otherAddress = otherPrefix.getRawAddress();
|
||||||
NetworkUtils.maskRawAddress(otherAddress, prefixLength);
|
NetUtils.maskRawAddress(otherAddress, prefixLength);
|
||||||
return Arrays.equals(otherAddress, address);
|
return Arrays.equals(otherAddress, address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ import java.math.BigInteger;
|
|||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
@@ -231,46 +230,6 @@ public class NetworkUtils {
|
|||||||
return InetAddress.parseNumericAddress(addrString);
|
return InetAddress.parseNumericAddress(addrString);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Masks a raw IP address byte array with the specified prefix length.
|
|
||||||
*/
|
|
||||||
public static void maskRawAddress(byte[] array, int prefixLength) {
|
|
||||||
if (prefixLength < 0 || prefixLength > array.length * 8) {
|
|
||||||
throw new RuntimeException("IP address with " + array.length +
|
|
||||||
" bytes has invalid prefix length " + prefixLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
int offset = prefixLength / 8;
|
|
||||||
int remainder = prefixLength % 8;
|
|
||||||
byte mask = (byte)(0xFF << (8 - remainder));
|
|
||||||
|
|
||||||
if (offset < array.length) array[offset] = (byte)(array[offset] & mask);
|
|
||||||
|
|
||||||
offset++;
|
|
||||||
|
|
||||||
for (; offset < array.length; offset++) {
|
|
||||||
array[offset] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get InetAddress masked with prefixLength. Will never return null.
|
|
||||||
* @param address the IP address to mask with
|
|
||||||
* @param prefixLength the prefixLength used to mask the IP
|
|
||||||
*/
|
|
||||||
public static InetAddress getNetworkPart(InetAddress address, int prefixLength) {
|
|
||||||
byte[] array = address.getAddress();
|
|
||||||
maskRawAddress(array, prefixLength);
|
|
||||||
|
|
||||||
InetAddress netPart = null;
|
|
||||||
try {
|
|
||||||
netPart = InetAddress.getByAddress(array);
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
throw new RuntimeException("getNetworkPart error - " + e.toString());
|
|
||||||
}
|
|
||||||
return netPart;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the implicit netmask of an IPv4 address, as was the custom before 1993.
|
* Returns the implicit netmask of an IPv4 address, as was the custom before 1993.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user