summaryrefslogtreecommitdiff
path: root/native/android/net.c
diff options
context:
space:
mode:
authorScott Lobdell <slobdell@google.com>2021-03-29 16:12:49 +0000
committerScott Lobdell <slobdell@google.com>2021-04-02 22:35:29 +0000
commit21cdef883cc867db55340b25d5c95e19b12ab383 (patch)
tree93d1444ebe783f53f5f0ae2647592723b27b3fb8 /native/android/net.c
parent7deab3736bb5f3a92be8ac820096926dce2366ad (diff)
parentd1d45f856fdf68835f5b42eacecab44e6dfa8545 (diff)
Merge SP1A.210329.001
Change-Id: I1e21c5890b5b2e2f2855f09960bc8eec8aa922bf
Diffstat (limited to 'native/android/net.c')
-rw-r--r--native/android/net.c56
1 files changed, 53 insertions, 3 deletions
diff --git a/native/android/net.c b/native/android/net.c
index a8104fc23041..e2f36a77b7c6 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,49 @@ 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_setprocdns(net_handle_t network) {
+ unsigned netid;
+ if (!getnetidfromhandle(network, &netid)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ int rval = setNetworkForResolv(netid);
+ if (rval < 0) {
+ errno = -rval;
+ rval = -1;
+ }
+ return rval;
+}
+
+int android_getprocdns(net_handle_t *network) {
+ if (network == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ unsigned netid;
+ int rval = getNetworkForDns(&netid);
+ if (rval < 0) {
+ errno = -rval;
+ return -1;
+ }
+
+ *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) {