public class NetworkUtils extends Object
Constructor and Description |
---|
NetworkUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
addressTypeMatches(InetAddress left,
InetAddress right)
Check if IP address type is consistent between two InetAddress.
|
static void |
attachDhcpFilter(FileDescriptor fd)
Attaches a socket filter that accepts DHCP packets to the given socket.
|
static void |
attachRaFilter(FileDescriptor fd,
int packetType)
Attaches a socket filter that accepts ICMPv6 router advertisements to the given socket.
|
static boolean |
bindProcessToNetwork(int netId)
Binds the current process to the network designated by
netId . |
static boolean |
bindProcessToNetworkForHostResolution(int netId)
Deprecated.
This is strictly for legacy usage to support startUsingNetworkFeature().
|
static int |
bindSocketToNetwork(int socketfd,
int netId)
Explicitly binds
socketfd to the network designated by netId . |
static int |
getBoundNetworkForProcess()
Return the netId last passed to
bindProcessToNetwork(int) , or NETID_UNSET if
#unbindProcessToNetwork has been called since bindProcessToNetwork(int) . |
static int |
getImplicitNetmask(Inet4Address address)
Returns the implicit netmask of an IPv4 address, as was the custom before 1993.
|
static InetAddress |
getNetworkPart(InetAddress address,
int prefixLength)
Get InetAddress masked with prefixLength.
|
static InetAddress |
hexToInet6Address(String addrHexString)
Convert a 32 char hex string into a Inet6Address.
|
static int |
inetAddressToInt(Inet4Address inetAddr)
Convert a IPv4 address from an InetAddress to an integer
|
static InetAddress |
intToInetAddress(int hostAddress)
Convert a IPv4 address from an integer to an InetAddress.
|
static String[] |
makeStrings(Collection<InetAddress> addrs)
Create a string array of host addresses from a collection of InetAddresses
|
static void |
maskRawAddress(byte[] array,
int prefixLength)
Masks a raw IP address byte array with the specified prefix length.
|
static int |
netmaskIntToPrefixLength(int netmask)
Convert a IPv4 netmask integer to a prefix length
|
static int |
netmaskToPrefixLength(Inet4Address netmask)
Convert an IPv4 netmask to a prefix length, checking that the netmask is contiguous.
|
static InetAddress |
numericToInetAddress(String addrString)
Create an InetAddress from a string where the string must be a standard
representation of a V4 or V6 address.
|
protected static void |
parcelInetAddress(Parcel parcel,
InetAddress address,
int flags)
Writes an InetAddress to a parcel.
|
static Pair<InetAddress,Integer> |
parseIpAndMask(String ipAndMaskString)
Utility method to parse strings such as "192.0.2.5/24" or "2001:db8::cafe:d00d/64".
|
static int |
prefixLengthToNetmaskInt(int prefixLength)
Convert a network prefix length to an IPv4 netmask integer
|
static boolean |
protectFromVpn(FileDescriptor fd)
Protect
fd from VPN connections. |
static boolean |
protectFromVpn(int socketfd)
Protect
socketfd from VPN connections. |
static boolean |
queryUserAccess(int uid,
int netId)
Determine if
uid can access network designated by netId . |
static void |
setupRaSocket(FileDescriptor fd,
int ifIndex)
Configures a socket for receiving ICMPv6 router solicitations and sending advertisements.
|
static String |
trimV4AddrZeros(String addr)
Trim leading zeros from IPv4 address strings
Our base libraries will interpret that as octel..
|
protected static InetAddress |
unparcelInetAddress(Parcel in)
Reads an InetAddress from a parcel.
|
public static void attachDhcpFilter(FileDescriptor fd) throws SocketException
SocketException
public static void attachRaFilter(FileDescriptor fd, int packetType) throws SocketException
fd
- the socket's FileDescriptor
.packetType
- the hardware address type, one of ARPHRD_*.SocketException
public static void setupRaSocket(FileDescriptor fd, int ifIndex) throws SocketException
fd
- the socket's FileDescriptor
.ifIndex
- the interface index.SocketException
public static boolean bindProcessToNetwork(int netId)
netId
. All sockets created
in the future (and not explicitly bound via a bound SocketFactory
(see
Network.getSocketFactory()
) will be bound to this network. Note that if this
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 Network
. Passing NETID_UNSET clears the binding.public static int getBoundNetworkForProcess()
bindProcessToNetwork(int)
, or NETID_UNSET if
#unbindProcessToNetwork
has been called since bindProcessToNetwork(int)
.public static boolean bindProcessToNetworkForHostResolution(int netId)
netId
.
bindProcessToNetwork(int)
takes precedence over this setting. Passing NETID_UNSET clears
the binding.public static int bindSocketToNetwork(int socketfd, int netId)
socketfd
to the network designated by netId
. This
overrides any binding via bindProcessToNetwork(int)
.public static boolean protectFromVpn(FileDescriptor fd)
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(int socketfd)
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 boolean queryUserAccess(int uid, int netId)
uid
can access network designated by netId
.true
if uid
can access network, false
otherwise.public static InetAddress intToInetAddress(int hostAddress)
hostAddress
- an int corresponding to the IPv4 address in network byte orderpublic static int inetAddressToInt(Inet4Address inetAddr) throws IllegalArgumentException
inetAddr
- is an InetAddress corresponding to the IPv4 addressIllegalArgumentException
public static int prefixLengthToNetmaskInt(int prefixLength) throws IllegalArgumentException
prefixLength
- IllegalArgumentException
public static int netmaskIntToPrefixLength(int netmask)
netmask
- as an integer in network byte orderpublic static int netmaskToPrefixLength(Inet4Address netmask)
netmask
- as a Inet4Address
.IllegalArgumentException
- the specified netmask was not contiguous.public static InetAddress numericToInetAddress(String addrString) throws IllegalArgumentException
addrString
- IllegalArgumentException
protected static void parcelInetAddress(Parcel parcel, InetAddress address, int flags)
protected static InetAddress unparcelInetAddress(Parcel in)
public static void maskRawAddress(byte[] array, int prefixLength)
public static InetAddress getNetworkPart(InetAddress address, int prefixLength)
address
- the IP address to mask withprefixLength
- the prefixLength used to mask the IPpublic static int getImplicitNetmask(Inet4Address address)
public static Pair<InetAddress,Integer> parseIpAndMask(String ipAndMaskString)
public static boolean addressTypeMatches(InetAddress left, InetAddress right)
public static InetAddress hexToInet6Address(String addrHexString) throws IllegalArgumentException
addrHexString
- a 32 character hex string representing an IPv6 addrIllegalArgumentException
public static String[] makeStrings(Collection<InetAddress> addrs)
addrs
- a Collection of InetAddressespublic static String trimV4AddrZeros(String addr)
addr
- a string representing an ip addr