Merge "Fix SocketUtils API"
This commit is contained in:
@@ -3516,19 +3516,16 @@ package android.net.metrics {
|
||||
package android.net.util {
|
||||
|
||||
public class SocketUtils {
|
||||
method public static void addArpEntry(java.net.Inet4Address, android.net.MacAddress, String, java.io.FileDescriptor) throws java.io.IOException;
|
||||
method public static void attachControlPacketFilter(java.io.FileDescriptor, int) throws java.net.SocketException;
|
||||
method public static void attachDhcpFilter(java.io.FileDescriptor) throws java.net.SocketException;
|
||||
method public static void attachRaFilter(java.io.FileDescriptor, int) throws java.net.SocketException;
|
||||
method public static void bindSocket(java.io.FileDescriptor, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
|
||||
method public static void bindSocketToInterface(java.io.FileDescriptor, String) throws android.system.ErrnoException;
|
||||
method public static void closeSocket(java.io.FileDescriptor) throws java.io.IOException;
|
||||
method public static void connectSocket(java.io.FileDescriptor, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
|
||||
method public static void addArpEntry(@NonNull java.net.Inet4Address, @NonNull android.net.MacAddress, @NonNull String, @NonNull java.io.FileDescriptor) throws java.io.IOException;
|
||||
method public static void attachControlPacketFilter(@NonNull java.io.FileDescriptor, int) throws java.net.SocketException;
|
||||
method public static void attachDhcpFilter(@NonNull java.io.FileDescriptor) throws java.net.SocketException;
|
||||
method public static void attachRaFilter(@NonNull java.io.FileDescriptor, int) throws java.net.SocketException;
|
||||
method public static void bindSocketToInterface(@NonNull java.io.FileDescriptor, @NonNull String) throws android.system.ErrnoException;
|
||||
method public static void closeSocket(@Nullable java.io.FileDescriptor) throws java.io.IOException;
|
||||
method public static java.net.SocketAddress makeNetlinkSocketAddress(int, int);
|
||||
method public static java.net.SocketAddress makePacketSocketAddress(short, int);
|
||||
method public static java.net.SocketAddress makePacketSocketAddress(int, byte[]);
|
||||
method public static void sendTo(java.io.FileDescriptor, byte[], int, int, int, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
|
||||
method public static void setSocketTimeValueOption(java.io.FileDescriptor, int, int, long) throws android.system.ErrnoException;
|
||||
method public static java.net.SocketAddress makePacketSocketAddress(int, @NonNull byte[]);
|
||||
method public static void setSocketTimeValueOption(@NonNull java.io.FileDescriptor, int, int, long) throws android.system.ErrnoException;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -917,19 +917,16 @@ package android.net.metrics {
|
||||
package android.net.util {
|
||||
|
||||
public class SocketUtils {
|
||||
method public static void addArpEntry(java.net.Inet4Address, android.net.MacAddress, String, java.io.FileDescriptor) throws java.io.IOException;
|
||||
method public static void attachControlPacketFilter(java.io.FileDescriptor, int) throws java.net.SocketException;
|
||||
method public static void attachDhcpFilter(java.io.FileDescriptor) throws java.net.SocketException;
|
||||
method public static void attachRaFilter(java.io.FileDescriptor, int) throws java.net.SocketException;
|
||||
method public static void bindSocket(java.io.FileDescriptor, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
|
||||
method public static void bindSocketToInterface(java.io.FileDescriptor, String) throws android.system.ErrnoException;
|
||||
method public static void closeSocket(java.io.FileDescriptor) throws java.io.IOException;
|
||||
method public static void connectSocket(java.io.FileDescriptor, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
|
||||
method public static void addArpEntry(@NonNull java.net.Inet4Address, @NonNull android.net.MacAddress, @NonNull String, @NonNull java.io.FileDescriptor) throws java.io.IOException;
|
||||
method public static void attachControlPacketFilter(@NonNull java.io.FileDescriptor, int) throws java.net.SocketException;
|
||||
method public static void attachDhcpFilter(@NonNull java.io.FileDescriptor) throws java.net.SocketException;
|
||||
method public static void attachRaFilter(@NonNull java.io.FileDescriptor, int) throws java.net.SocketException;
|
||||
method public static void bindSocketToInterface(@NonNull java.io.FileDescriptor, @NonNull String) throws android.system.ErrnoException;
|
||||
method public static void closeSocket(@Nullable java.io.FileDescriptor) throws java.io.IOException;
|
||||
method public static java.net.SocketAddress makeNetlinkSocketAddress(int, int);
|
||||
method public static java.net.SocketAddress makePacketSocketAddress(short, int);
|
||||
method public static java.net.SocketAddress makePacketSocketAddress(int, byte[]);
|
||||
method public static void sendTo(java.io.FileDescriptor, byte[], int, int, int, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
|
||||
method public static void setSocketTimeValueOption(java.io.FileDescriptor, int, int, long) throws android.system.ErrnoException;
|
||||
method public static java.net.SocketAddress makePacketSocketAddress(int, @NonNull byte[]);
|
||||
method public static void setSocketTimeValueOption(@NonNull java.io.FileDescriptor, int, int, long) throws android.system.ErrnoException;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ package android.net.util;
|
||||
import static android.system.OsConstants.SOL_SOCKET;
|
||||
import static android.system.OsConstants.SO_BINDTODEVICE;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SystemApi;
|
||||
import android.annotation.TestApi;
|
||||
import android.net.MacAddress;
|
||||
@@ -49,7 +51,7 @@ public class SocketUtils {
|
||||
*
|
||||
* <p>Data sent through the socket will go directly to the underlying network, ignoring VPNs.
|
||||
*/
|
||||
public static void bindSocketToInterface(FileDescriptor socket, String iface)
|
||||
public static void bindSocketToInterface(@NonNull FileDescriptor socket, @NonNull String iface)
|
||||
throws ErrnoException {
|
||||
// SO_BINDTODEVICE actually takes a string. This works because the first member
|
||||
// of struct ifreq is a NULL-terminated interface name.
|
||||
@@ -75,7 +77,7 @@ public class SocketUtils {
|
||||
/**
|
||||
* Make a socket address that packet socket can send packets to.
|
||||
*/
|
||||
public static SocketAddress makePacketSocketAddress(int ifIndex, byte[] hwAddr) {
|
||||
public static SocketAddress makePacketSocketAddress(int ifIndex, @NonNull byte[] hwAddr) {
|
||||
return new PacketSocketAddress(ifIndex, hwAddr);
|
||||
}
|
||||
|
||||
@@ -83,45 +85,21 @@ public class SocketUtils {
|
||||
* Set an option on a socket that takes a time value argument.
|
||||
*/
|
||||
public static void setSocketTimeValueOption(
|
||||
FileDescriptor fd, int level, int option, long millis) throws ErrnoException {
|
||||
@NonNull FileDescriptor fd, int level, int option, long millis) throws ErrnoException {
|
||||
Os.setsockoptTimeval(fd, level, option, StructTimeval.fromMillis(millis));
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind a socket to the specified address.
|
||||
*/
|
||||
public static void bindSocket(FileDescriptor fd, SocketAddress addr)
|
||||
throws ErrnoException, SocketException {
|
||||
Os.bind(fd, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect a socket to the specified address.
|
||||
*/
|
||||
public static void connectSocket(FileDescriptor fd, SocketAddress addr)
|
||||
throws ErrnoException, SocketException {
|
||||
Os.connect(fd, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message on a socket, using the specified SocketAddress.
|
||||
*/
|
||||
public static void sendTo(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount,
|
||||
int flags, SocketAddress addr) throws ErrnoException, SocketException {
|
||||
Os.sendto(fd, bytes, byteOffset, byteCount, flags, addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see IoBridge#closeAndSignalBlockedThreads(FileDescriptor)
|
||||
*/
|
||||
public static void closeSocket(FileDescriptor fd) throws IOException {
|
||||
public static void closeSocket(@Nullable FileDescriptor fd) throws IOException {
|
||||
IoBridge.closeAndSignalBlockedThreads(fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a socket filter that accepts DHCP packets to the given socket.
|
||||
*/
|
||||
public static void attachDhcpFilter(FileDescriptor fd) throws SocketException {
|
||||
public static void attachDhcpFilter(@NonNull FileDescriptor fd) throws SocketException {
|
||||
NetworkUtils.attachDhcpFilter(fd);
|
||||
}
|
||||
|
||||
@@ -130,7 +108,8 @@ public class SocketUtils {
|
||||
* @param fd the socket's {@link FileDescriptor}.
|
||||
* @param packetType the hardware address type, one of ARPHRD_*.
|
||||
*/
|
||||
public static void attachRaFilter(FileDescriptor fd, int packetType) throws SocketException {
|
||||
public static void attachRaFilter(@NonNull FileDescriptor fd, int packetType)
|
||||
throws SocketException {
|
||||
NetworkUtils.attachRaFilter(fd, packetType);
|
||||
}
|
||||
|
||||
@@ -142,7 +121,7 @@ public class SocketUtils {
|
||||
* @param fd the socket's {@link FileDescriptor}.
|
||||
* @param packetType the hardware address type, one of ARPHRD_*.
|
||||
*/
|
||||
public static void attachControlPacketFilter(FileDescriptor fd, int packetType)
|
||||
public static void attachControlPacketFilter(@NonNull FileDescriptor fd, int packetType)
|
||||
throws SocketException {
|
||||
NetworkUtils.attachControlPacketFilter(fd, packetType);
|
||||
}
|
||||
@@ -150,8 +129,8 @@ public class SocketUtils {
|
||||
/**
|
||||
* Add an entry into the ARP cache.
|
||||
*/
|
||||
public static void addArpEntry(Inet4Address ipv4Addr, MacAddress ethAddr, String ifname,
|
||||
FileDescriptor fd) throws IOException {
|
||||
public static void addArpEntry(@NonNull Inet4Address ipv4Addr, @NonNull MacAddress ethAddr,
|
||||
@NonNull String ifname, @NonNull FileDescriptor fd) throws IOException {
|
||||
NetworkUtils.addArpEntry(ipv4Addr, ethAddr, ifname, fd);
|
||||
}
|
||||
|
||||
|
||||
@@ -475,7 +475,7 @@ public class ApfFilter {
|
||||
socket = Os.socket(AF_PACKET, SOCK_RAW, ETH_P_IPV6);
|
||||
SocketAddress addr = makePacketSocketAddress(
|
||||
(short) ETH_P_IPV6, mInterfaceParams.index);
|
||||
SocketUtils.bindSocket(socket, addr);
|
||||
Os.bind(socket, addr);
|
||||
SocketUtils.attachRaFilter(socket, mApfCapabilities.apfPacketFormat);
|
||||
} catch(SocketException|ErrnoException e) {
|
||||
Log.e(TAG, "Error starting filter", e);
|
||||
|
||||
@@ -317,7 +317,7 @@ public class DhcpClient extends StateMachine {
|
||||
try {
|
||||
mPacketSock = Os.socket(AF_PACKET, SOCK_RAW, ETH_P_IP);
|
||||
SocketAddress addr = makePacketSocketAddress((short) ETH_P_IP, mIface.index);
|
||||
SocketUtils.bindSocket(mPacketSock, addr);
|
||||
Os.bind(mPacketSock, addr);
|
||||
SocketUtils.attachDhcpFilter(mPacketSock);
|
||||
} catch(SocketException|ErrnoException e) {
|
||||
Log.e(TAG, "Error creating packet socket", e);
|
||||
@@ -412,8 +412,7 @@ public class DhcpClient extends StateMachine {
|
||||
try {
|
||||
if (encap == DhcpPacket.ENCAP_L2) {
|
||||
if (DBG) Log.d(TAG, "Broadcasting " + description);
|
||||
SocketUtils.sendTo(
|
||||
mPacketSock, buf.array(), 0, buf.limit(), 0, mInterfaceBroadcastAddr);
|
||||
Os.sendto(mPacketSock, buf.array(), 0, buf.limit(), 0, mInterfaceBroadcastAddr);
|
||||
} else if (encap == DhcpPacket.ENCAP_BOOTP && to.equals(INADDR_BROADCAST)) {
|
||||
if (DBG) Log.d(TAG, "Broadcasting " + description);
|
||||
// We only send L3-encapped broadcasts in DhcpRebindingState,
|
||||
|
||||
@@ -104,8 +104,7 @@ public class ConnectivityPacketTracker {
|
||||
try {
|
||||
s = Os.socket(AF_PACKET, SOCK_RAW | SOCK_NONBLOCK, 0);
|
||||
SocketUtils.attachControlPacketFilter(s, ARPHRD_ETHER);
|
||||
SocketUtils.bindSocket(
|
||||
s, makePacketSocketAddress((short) ETH_P_ALL, mInterface.index));
|
||||
Os.bind(s, makePacketSocketAddress((short) ETH_P_ALL, mInterface.index));
|
||||
} catch (ErrnoException | IOException e) {
|
||||
logError("Failed to create packet tracking socket: ", e);
|
||||
closeFd(s);
|
||||
|
||||
@@ -34,7 +34,6 @@ import android.net.netlink.StructNdMsg;
|
||||
import android.net.util.NetworkStackUtils;
|
||||
import android.net.util.PacketReader;
|
||||
import android.net.util.SharedLog;
|
||||
import android.net.util.SocketUtils;
|
||||
import android.os.Handler;
|
||||
import android.os.SystemClock;
|
||||
import android.system.ErrnoException;
|
||||
@@ -150,7 +149,7 @@ public class IpNeighborMonitor extends PacketReader {
|
||||
|
||||
try {
|
||||
fd = Os.socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK, NETLINK_ROUTE);
|
||||
SocketUtils.bindSocket(fd, makeNetlinkSocketAddress(0, OsConstants.RTMGRP_NEIGH));
|
||||
Os.bind(fd, makeNetlinkSocketAddress(0, OsConstants.RTMGRP_NEIGH));
|
||||
NetlinkSocket.connectToKernel(fd);
|
||||
|
||||
if (VDBG) {
|
||||
|
||||
@@ -109,7 +109,7 @@ public class NetlinkSocket {
|
||||
}
|
||||
|
||||
public static void connectToKernel(FileDescriptor fd) throws ErrnoException, SocketException {
|
||||
SocketUtils.connectSocket(fd, makeNetlinkSocketAddress(0, 0));
|
||||
Os.connect(fd, makeNetlinkSocketAddress(0, 0));
|
||||
}
|
||||
|
||||
private static void checkTimeout(long timeoutMs) {
|
||||
|
||||
Reference in New Issue
Block a user