Methods Summary |
---|
public static boolean | addressTypeMatches(java.net.InetAddress left, java.net.InetAddress right)Check if IP address type is consistent between two InetAddress.
return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) ||
((left instanceof Inet6Address) && (right instanceof Inet6Address)));
|
public static native boolean | bindProcessToNetwork(int netId)Binds the current process to the network designated by {@code netId}. All sockets created
in the future (and not explicitly bound via a bound {@link SocketFactory} (see
{@link Network#getSocketFactory}) will be bound to this network. Note that if this
{@code Network} ever disconnects all sockets created in this way will cease to work. This
is by design so an application doesn't accidentally use sockets it thinks are still bound to
a particular {@code Network}. Passing NETID_UNSET clears the binding.
|
public static native boolean | bindProcessToNetworkForHostResolution(int netId)Binds host resolutions performed by this process to the network designated by {@code netId}.
{@link #bindProcessToNetwork} takes precedence over this setting. Passing NETID_UNSET clears
the binding.
|
public static native int | bindSocketToNetwork(int socketfd, int netId)Explicitly binds {@code socketfd} to the network designated by {@code netId}. This
overrides any binding via {@link #bindProcessToNetwork}.
|
public static native java.lang.String | getDhcpError()Return the last DHCP-related error message that was recorded.
NOTE: This string is not localized, but currently it is only
used in logging.
|
public static native int | getNetworkBoundToProcess()Return the netId last passed to {@link #bindProcessToNetwork}, or NETID_UNSET if
{@link #unbindProcessToNetwork} has been called since {@link #bindProcessToNetwork}.
|
public static java.net.InetAddress | getNetworkPart(java.net.InetAddress address, int prefixLength)Get InetAddress masked with prefixLength. Will never return null.
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;
|
public static java.net.InetAddress | hexToInet6Address(java.lang.String addrHexString)Convert a 32 char hex string into a Inet6Address.
throws a runtime exception if the string isn't 32 chars, isn't hex or can't be
made into an Inet6Address
try {
return numericToInetAddress(String.format(Locale.US, "%s:%s:%s:%s:%s:%s:%s:%s",
addrHexString.substring(0,4), addrHexString.substring(4,8),
addrHexString.substring(8,12), addrHexString.substring(12,16),
addrHexString.substring(16,20), addrHexString.substring(20,24),
addrHexString.substring(24,28), addrHexString.substring(28,32)));
} catch (Exception e) {
Log.e("NetworkUtils", "error in hexToInet6Address(" + addrHexString + "): " + e);
throw new IllegalArgumentException(e);
}
|
public static int | inetAddressToInt(java.net.Inet4Address inetAddr)Convert a IPv4 address from an InetAddress to an integer
byte [] addr = inetAddr.getAddress();
return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |
((addr[1] & 0xff) << 8) | (addr[0] & 0xff);
|
public static java.net.InetAddress | intToInetAddress(int hostAddress)Convert a IPv4 address from an integer to an InetAddress.
byte[] addressBytes = { (byte)(0xff & hostAddress),
(byte)(0xff & (hostAddress >> 8)),
(byte)(0xff & (hostAddress >> 16)),
(byte)(0xff & (hostAddress >> 24)) };
try {
return InetAddress.getByAddress(addressBytes);
} catch (UnknownHostException e) {
throw new AssertionError();
}
|
public static java.lang.String[] | makeStrings(java.util.Collection addrs)Create a string array of host addresses from a collection of InetAddresses
String[] result = new String[addrs.size()];
int i = 0;
for (InetAddress addr : addrs) {
result[i++] = addr.getHostAddress();
}
return result;
|
public static void | maskRawAddress(byte[] array, int prefixLength)Masks a raw IP address byte array with the specified prefix length.
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;
}
|
public static int | netmaskIntToPrefixLength(int netmask)Convert a IPv4 netmask integer to a prefix length
return Integer.bitCount(netmask);
|
public static java.net.InetAddress | numericToInetAddress(java.lang.String addrString)Create an InetAddress from a string where the string must be a standard
representation of a V4 or V6 address. Avoids doing a DNS lookup on failure
but it will throw an IllegalArgumentException in that case.
return InetAddress.parseNumericAddress(addrString);
|
protected static void | parcelInetAddress(android.os.Parcel parcel, java.net.InetAddress address, int flags)Writes an InetAddress to a parcel. The address may be null. This is likely faster than
calling writeSerializable.
byte[] addressArray = (address != null) ? address.getAddress() : null;
parcel.writeByteArray(addressArray);
|
public static android.util.Pair | parseIpAndMask(java.lang.String ipAndMaskString)Utility method to parse strings such as "192.0.2.5/24" or "2001:db8::cafe:d00d/64".
InetAddress address = null;
int prefixLength = -1;
try {
String[] pieces = ipAndMaskString.split("/", 2);
prefixLength = Integer.parseInt(pieces[1]);
address = InetAddress.parseNumericAddress(pieces[0]);
} catch (NullPointerException e) { // Null string.
} catch (ArrayIndexOutOfBoundsException e) { // No prefix length.
} catch (NumberFormatException e) { // Non-numeric prefix.
} catch (IllegalArgumentException e) { // Invalid IP address.
}
if (address == null || prefixLength == -1) {
throw new IllegalArgumentException("Invalid IP address and mask " + ipAndMaskString);
}
return new Pair<InetAddress, Integer>(address, prefixLength);
|
public static int | prefixLengthToNetmaskInt(int prefixLength)Convert a network prefix length to an IPv4 netmask integer
if (prefixLength < 0 || prefixLength > 32) {
throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)");
}
int value = 0xffffffff << (32 - prefixLength);
return Integer.reverseBytes(value);
|
public static native boolean | protectFromVpn(int socketfd)Protect {@code socketfd} from VPN connections. After protecting, data sent through
this socket will go directly to the underlying network, so its traffic will not be
forwarded through the VPN.
|
public static native boolean | releaseDhcpLease(java.lang.String interfaceName)Release the current DHCP lease.
|
public static native int | resetConnections(java.lang.String interfaceName, int mask)Reset IPv6 or IPv4 sockets that are connected via the named interface.
|
public static native boolean | runDhcp(java.lang.String interfaceName, DhcpResults dhcpResults)Start the DHCP client daemon, in order to have it request addresses
for the named interface, and then configure the interface with those
addresses. This call blocks until it obtains a result (either success
or failure) from the daemon.
|
public static native boolean | runDhcpRenew(java.lang.String interfaceName, DhcpResults dhcpResults)Initiate renewal on the Dhcp client daemon. This call blocks until it obtains
a result (either success or failure) from the daemon.
|
public static native boolean | stopDhcp(java.lang.String interfaceName)Shut down the DHCP client daemon.
|
public static java.lang.String | trimV4AddrZeros(java.lang.String addr)Trim leading zeros from IPv4 address strings
Our base libraries will interpret that as octel..
Must leave non v4 addresses and host names alone.
For example, 192.168.000.010 -> 192.168.0.10
TODO - fix base libraries and remove this function
if (addr == null) return null;
String[] octets = addr.split("\\.");
if (octets.length != 4) return addr;
StringBuilder builder = new StringBuilder(16);
String result = null;
for (int i = 0; i < 4; i++) {
try {
if (octets[i].length() > 3) return addr;
builder.append(Integer.parseInt(octets[i]));
} catch (NumberFormatException e) {
return addr;
}
if (i < 3) builder.append('.");
}
result = builder.toString();
return result;
|
protected static java.net.InetAddress | unparcelInetAddress(android.os.Parcel in)Reads an InetAddress from a parcel. Returns null if the address that was written was null
or if the data is invalid.
byte[] addressArray = in.createByteArray();
if (addressArray == null) {
return null;
}
try {
return InetAddress.getByAddress(addressArray);
} catch (UnknownHostException e) {
return null;
}
|