/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.net; import java.io.FileDescriptor; import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Collection; import java.util.Locale; import android.os.Parcel; import android.util.Log; import android.util.Pair; /** * Native methods for managing network interfaces. * * {@hide} */ public class NetworkUtils { private static final String TAG = "NetworkUtils"; /** Setting bit 0 indicates reseting of IPv4 addresses required */ public static final int RESET_IPV4_ADDRESSES = 0x01; /** Setting bit 1 indicates reseting of IPv4 addresses required */ public static final int RESET_IPV6_ADDRESSES = 0x02; /** Reset all addresses */ public static final int RESET_ALL_ADDRESSES = RESET_IPV4_ADDRESSES | RESET_IPV6_ADDRESSES; /** * Reset IPv6 or IPv4 sockets that are connected via the named interface. * * @param interfaceName is the interface to reset * @param mask {@see #RESET_IPV4_ADDRESSES} and {@see #RESET_IPV6_ADDRESSES} */ public native static int resetConnections(String interfaceName, int mask); /** * Start the DHCP client daemon, in order to have it request addresses * for the named interface. This returns {@code true} if the DHCPv4 daemon * starts, {@code false} otherwise. This call blocks until such time as a * result is available or the default discovery timeout has been reached. * Callers should check {@link #getDhcpResults} to determine whether DHCP * succeeded or failed, and if it succeeded, to fetch the {@link DhcpResults}. * @param interfaceName the name of the interface to configure * @return {@code true} for success, {@code false} for failure */ public native static boolean startDhcp(String interfaceName); /** * Initiate renewal on the DHCP client daemon for the named interface. This * returns {@code true} if the DHCPv4 daemon has been notified, {@code false} * otherwise. This call blocks until such time as a result is available or * the default renew timeout has been reached. Callers should check * {@link #getDhcpResults} to determine whether DHCP succeeded or failed, * and if it succeeded, to fetch the {@link DhcpResults}. * @param interfaceName the name of the interface to configure * @return {@code true} for success, {@code false} for failure */ public native static boolean startDhcpRenew(String interfaceName); /** * 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. * @param interfaceName the name of the interface to configure * @param dhcpResults if the request succeeds, this object is filled in with * the IP address information. * @return {@code true} for success, {@code false} for failure */ public static boolean runDhcp(String interfaceName, DhcpResults dhcpResults) { return startDhcp(interfaceName) && getDhcpResults(interfaceName, dhcpResults); } /** * Initiate renewal on the DHCP client daemon. This call blocks until it obtains * a result (either success or failure) from the daemon. * @param interfaceName the name of the interface to configure * @param dhcpResults if the request succeeds, this object is filled in with * the IP address information. * @return {@code true} for success, {@code false} for failure */ public static boolean runDhcpRenew(String interfaceName, DhcpResults dhcpResults) { return startDhcpRenew(interfaceName) && getDhcpResults(interfaceName, dhcpResults); } /** * Fetch results from the DHCP client daemon. This call returns {@code true} if * if there are results available to be read, {@code false} otherwise. * @param interfaceName the name of the interface to configure * @param dhcpResults if the request succeeds, this object is filled in with * the IP address information. * @return {@code true} for success, {@code false} for failure */ public native static boolean getDhcpResults(String interfaceName, DhcpResults dhcpResults); /** * Shut down the DHCP client daemon. * @param interfaceName the name of the interface for which the daemon * should be stopped * @return {@code true} for success, {@code false} for failure */ public native static boolean stopDhcp(String interfaceName); /** * Release the current DHCP lease. * @param interfaceName the name of the interface for which the lease should * be released * @return {@code true} for success, {@code false} for failure */ public native static boolean releaseDhcpLease(String interfaceName); /** * Return the last DHCP-related error message that was recorded. *
NOTE: This string is not localized, but currently it is only * used in logging. * @return the most recent error message, if any */ public native static String getDhcpError(); /** * Attaches a socket filter that accepts DHCP packets to the given socket. */ public native static void attachDhcpFilter(FileDescriptor fd) throws SocketException; /** * 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 native static boolean bindProcessToNetwork(int netId); /** * Return the netId last passed to {@link #bindProcessToNetwork}, or NETID_UNSET if * {@link #unbindProcessToNetwork} has been called since {@link #bindProcessToNetwork}. */ public native static int getBoundNetworkForProcess(); /** * 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. * * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature(). */ public native static boolean bindProcessToNetworkForHostResolution(int netId); /** * Explicitly binds {@code socketfd} to the network designated by {@code netId}. This * overrides any binding via {@link #bindProcessToNetwork}. * @return 0 on success or negative errno on failure. */ public native static int bindSocketToNetwork(int socketfd, int netId); /** * Protect {@code fd} 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 boolean protectFromVpn(FileDescriptor fd) { return protectFromVpn(fd.getInt$()); } /** * 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 native static boolean protectFromVpn(int socketfd); /** * Determine if {@code uid} can access network designated by {@code netId}. * @return {@code true} if {@code uid} can access network, {@code false} otherwise. */ public native static boolean queryUserAccess(int uid, int netId); /** * Convert a IPv4 address from an integer to an InetAddress. * @param hostAddress an int corresponding to the IPv4 address in network byte order */ public static InetAddress intToInetAddress(int hostAddress) { 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(); } } /** * Convert a IPv4 address from an InetAddress to an integer * @param inetAddr is an InetAddress corresponding to the IPv4 address * @return the IP address as an integer in network byte order */ public static int inetAddressToInt(Inet4Address inetAddr) throws IllegalArgumentException { byte [] addr = inetAddr.getAddress(); return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) | ((addr[1] & 0xff) << 8) | (addr[0] & 0xff); } /** * Convert a network prefix length to an IPv4 netmask integer * @param prefixLength * @return the IPv4 netmask as an integer in network byte order */ public static int prefixLengthToNetmaskInt(int prefixLength) throws IllegalArgumentException { if (prefixLength < 0 || prefixLength > 32) { throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)"); } int value = 0xffffffff << (32 - prefixLength); return Integer.reverseBytes(value); } /** * Convert a IPv4 netmask integer to a prefix length * @param netmask as an integer in network byte order * @return the network prefix length */ public static int netmaskIntToPrefixLength(int netmask) { return Integer.bitCount(netmask); } /** * Convert an IPv4 netmask to a prefix length, checking that the netmask is contiguous. * @param netmask as a {@code Inet4Address}. * @return the network prefix length * @throws IllegalArgumentException the specified netmask was not contiguous. * @hide */ public static int netmaskToPrefixLength(Inet4Address netmask) { // inetAddressToInt returns an int in *network* byte order. int i = Integer.reverseBytes(inetAddressToInt(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; } /** * 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. * @param addrString * @return the InetAddress * @hide */ public static InetAddress numericToInetAddress(String addrString) throws IllegalArgumentException { return InetAddress.parseNumericAddress(addrString); } /** * Writes an InetAddress to a parcel. The address may be null. This is likely faster than * calling writeSerializable. */ protected static void parcelInetAddress(Parcel parcel, InetAddress address, int flags) { byte[] addressArray = (address != null) ? address.getAddress() : null; parcel.writeByteArray(addressArray); } /** * Reads an InetAddress from a parcel. Returns null if the address that was written was null * or if the data is invalid. */ protected static InetAddress unparcelInetAddress(Parcel in) { byte[] addressArray = in.createByteArray(); if (addressArray == null) { return null; } try { return InetAddress.getByAddress(addressArray); } catch (UnknownHostException e) { return null; } } /** * 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. */ public static int getImplicitNetmask(Inet4Address address) { int firstByte = address.getAddress()[0] & 0xff; // Convert to an unsigned value. if (firstByte < 128) { 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. } } /** * Utility method to parse strings such as "192.0.2.5/24" or "2001:db8::cafe:d00d/64". * @hide */ public static Pair