summaryrefslogtreecommitdiff
path: root/native/android
diff options
context:
space:
mode:
authorRemi NGUYEN VAN <reminv@google.com>2021-03-21 14:30:38 +0000
committerRemi NGUYEN VAN <reminv@google.com>2021-03-23 01:01:06 +0000
commit3d50059bbcc4700495ce5178ac54c051672d45b7 (patch)
tree59e36c739996d0f6c4761116c88633ef76f735a4 /native/android
parent1fa5fc98f707f7d789414961d4beaca3dcf0112b (diff)
Add NDK API for getprocnetwork
The API is the getter couterpart for setprocnetwork. Use it in NetworkUtils so that the NDK API can be the source of truth for the process network. Bug: 171540887 Test: atest CtsNetTestCases Tests in change I311b58585033c2ca50ce5477ea9cd94b6f127507 Change-Id: Ie8f68cf1fa57deddb63324c1abf3d6fd5b0ef500
Diffstat (limited to 'native/android')
-rw-r--r--native/android/libandroid.map.txt3
-rw-r--r--native/android/libandroid_net.map.txt2
-rw-r--r--native/android/net.c24
3 files changed, 25 insertions, 4 deletions
diff --git a/native/android/libandroid.map.txt b/native/android/libandroid.map.txt
index b01878b3070b..85513cad489c 100644
--- a/native/android/libandroid.map.txt
+++ b/native/android/libandroid.map.txt
@@ -285,6 +285,7 @@ LIBANDROID {
ATrace_endAsyncSection; # introduced=29
ATrace_setCounter; # introduced=29
android_getaddrinfofornetwork; # introduced=23
+ android_getprocnetwork; # introduced=31
android_setprocnetwork; # introduced=23
android_setsocknetwork; # introduced=23
android_res_cancel; # introduced=29
@@ -309,4 +310,4 @@ LIBANDROID_PLATFORM {
ASurfaceControlStats_getAcquireTime*;
ASurfaceControlStats_getFrameNumber*;
};
-} LIBANDROID; \ No newline at end of file
+} LIBANDROID;
diff --git a/native/android/libandroid_net.map.txt b/native/android/libandroid_net.map.txt
index 8d4e9009cc56..cc8dd727408f 100644
--- a/native/android/libandroid_net.map.txt
+++ b/native/android/libandroid_net.map.txt
@@ -14,6 +14,8 @@ LIBANDROID_NET {
android_res_nquery; # llndk
android_res_nresult; # llndk
android_res_nsend; # llndk
+ # These functions have been part of the NDK since API 31.
+ android_getprocnetwork; # llndk
local:
*;
};
diff --git a/native/android/net.c b/native/android/net.c
index a8104fc23041..d4b888845b27 100644
--- a/native/android/net.c
+++ b/native/android/net.c
@@ -22,12 +22,13 @@
#include <stdlib.h>
#include <sys/limits.h>
+// This value MUST be kept in sync with the corresponding value in
+// the android.net.Network#getNetworkHandle() implementation.
+static const uint32_t kHandleMagic = 0xcafed00d;
+static const uint32_t kHandleMagicSize = 32;
static int getnetidfromhandle(net_handle_t handle, unsigned *netid) {
static const uint32_t k32BitMask = 0xffffffff;
- // This value MUST be kept in sync with the corresponding value in
- // the android.net.Network#getNetworkHandle() implementation.
- static const uint32_t kHandleMagic = 0xcafed00d;
// Check for minimum acceptable version of the API in the low bits.
if (handle != NETWORK_UNSPECIFIED &&
@@ -41,6 +42,12 @@ static int getnetidfromhandle(net_handle_t handle, unsigned *netid) {
return 1;
}
+static net_handle_t gethandlefromnetid(unsigned netid) {
+ if (netid == NETID_UNSET) {
+ return NETWORK_UNSPECIFIED;
+ }
+ return (((net_handle_t) netid) << kHandleMagicSize) | kHandleMagic;
+}
int android_setsocknetwork(net_handle_t network, int fd) {
unsigned netid;
@@ -72,6 +79,17 @@ int android_setprocnetwork(net_handle_t network) {
return rval;
}
+int android_getprocnetwork(net_handle_t *network) {
+ if (network == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ unsigned netid = getNetworkForProcess();
+ *network = gethandlefromnetid(netid);
+ return 0;
+}
+
int android_getaddrinfofornetwork(net_handle_t network,
const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res) {