From 249da094eb55df04eac8168483001e3f6f409ecb Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Tue, 29 Jan 2019 15:17:00 +0900 Subject: [PATCH] Add utils connect, setsockopt, bind, sendTo The utilities are not supported as public API but required as SystemApi for the NetworkStack. Test: flashed, boots, WiFi works Bug: 112869080 Change-Id: Ia64b3bf9c6c33cf61bed76469ea9963b550bed2b --- api/system-current.txt | 4 +++ api/test-current.txt | 4 +++ core/java/android/net/util/SocketUtils.java | 33 +++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/api/system-current.txt b/api/system-current.txt index ec1c46c344291..e0801b7acbe18 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -3476,11 +3476,15 @@ package android.net.util { 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 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; } } diff --git a/api/test-current.txt b/api/test-current.txt index c5a2a52aeaba7..af455fb4abb64 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -912,11 +912,15 @@ package android.net.util { 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 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; } } diff --git a/core/java/android/net/util/SocketUtils.java b/core/java/android/net/util/SocketUtils.java index 2652a7f2193fd..fbb15ed693d96 100644 --- a/core/java/android/net/util/SocketUtils.java +++ b/core/java/android/net/util/SocketUtils.java @@ -27,6 +27,7 @@ import android.system.ErrnoException; import android.system.NetlinkSocketAddress; import android.system.Os; import android.system.PacketSocketAddress; +import android.system.StructTimeval; import libcore.io.IoBridge; @@ -78,6 +79,38 @@ public class SocketUtils { return new PacketSocketAddress(ifIndex, hwAddr); } + /** + * 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 { + 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) */