Add netlinkSocketForProto() without buffersize.

Test: atest NetworkStaticLibTests
atest FrameworksNetTests

Change-Id: I34a0a29eb7e78490701872229585e61f7a0c1dd3
This commit is contained in:
Yang Sun 2024-02-01 09:50:52 +08:00
commit 1d8b7cc422
3 changed files with 17 additions and 5 deletions

View file

@ -225,7 +225,7 @@ public class NetlinkUtils {
}
/**
* Create netlink socket with the given netlink protocol type.
* Create netlink socket with the given netlink protocol type and buffersize.
*
* @param nlProto the netlink protocol
* @param bufferSize the receive buffer size to set when the value is not 0
@ -242,11 +242,24 @@ public class NetlinkUtils {
return fd;
}
/**
* Create netlink socket with the given netlink protocol type. Receive buffer size is not set.
*
* @param nlProto the netlink protocol
*
* @return fd the fileDescriptor of the socket.
* @throws ErrnoException if the FileDescriptor not connect to be created successfully
*/
public static FileDescriptor netlinkSocketForProto(int nlProto)
throws ErrnoException {
return netlinkSocketForProto(nlProto, 0);
}
/**
* Construct a netlink inet_diag socket.
*/
public static FileDescriptor createNetLinkInetDiagSocket() throws ErrnoException {
return netlinkSocketForProto(NETLINK_INET_DIAG, 0);
return netlinkSocketForProto(NETLINK_INET_DIAG);
}
/**

View file

@ -212,7 +212,7 @@ public class NetlinkUtilsTest {
@Test @IgnoreUpTo(Build.VERSION_CODES.R) // getsockoptInt requires > R
public void testNetlinkSocketForProto_defaultBufferSize() throws Exception {
final FileDescriptor fd = NetlinkUtils.netlinkSocketForProto(NETLINK_ROUTE, 0);
final FileDescriptor fd = NetlinkUtils.netlinkSocketForProto(NETLINK_ROUTE);
final int bufferSize = Os.getsockoptInt(fd, SOL_SOCKET, SO_RCVBUF) / 2;
assertTrue("bufferSize: " + bufferSize, bufferSize > 0); // whatever the default value is

View file

@ -60,8 +60,7 @@ public class TunInterfaceController {
public void createTunInterface() throws IOException {
mParcelTunFd = ParcelFileDescriptor.adoptFd(nativeCreateTunInterface(mIfName, MTU));
try {
mNetlinkSocket =
NetlinkUtils.netlinkSocketForProto(OsConstants.NETLINK_ROUTE, 0);
mNetlinkSocket = NetlinkUtils.netlinkSocketForProto(OsConstants.NETLINK_ROUTE);
} catch (ErrnoException e) {
throw new IOException("Failed to create netlink socket", e);
}