blob: d4b888845b27c8cbbdd16cddba09c7490e9f5aea [file] [log] [blame]
Erik Kline25f3b7b2015-03-05 15:13:37 +09001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include <android/multinetwork.h>
19#include <errno.h>
20#include <NetdClient.h> // the functions that communicate with netd
21#include <resolv_netid.h> // android_getaddrinfofornet()
22#include <stdlib.h>
23#include <sys/limits.h>
24
Remi NGUYEN VANc2f03da2021-03-21 14:30:38 +000025// This value MUST be kept in sync with the corresponding value in
26// the android.net.Network#getNetworkHandle() implementation.
27static const uint32_t kHandleMagic = 0xcafed00d;
28static const uint32_t kHandleMagicSize = 32;
Erik Kline25f3b7b2015-03-05 15:13:37 +090029
30static int getnetidfromhandle(net_handle_t handle, unsigned *netid) {
31 static const uint32_t k32BitMask = 0xffffffff;
Erik Kline25f3b7b2015-03-05 15:13:37 +090032
33 // Check for minimum acceptable version of the API in the low bits.
34 if (handle != NETWORK_UNSPECIFIED &&
35 (handle & k32BitMask) != kHandleMagic) {
36 return 0;
37 }
38
39 if (netid != NULL) {
40 *netid = ((handle >> (CHAR_BIT * sizeof(k32BitMask))) & k32BitMask);
41 }
42 return 1;
43}
44
Remi NGUYEN VANc2f03da2021-03-21 14:30:38 +000045static net_handle_t gethandlefromnetid(unsigned netid) {
46 if (netid == NETID_UNSET) {
47 return NETWORK_UNSPECIFIED;
48 }
49 return (((net_handle_t) netid) << kHandleMagicSize) | kHandleMagic;
50}
Erik Kline25f3b7b2015-03-05 15:13:37 +090051
52int android_setsocknetwork(net_handle_t network, int fd) {
53 unsigned netid;
54 if (!getnetidfromhandle(network, &netid)) {
55 errno = EINVAL;
56 return -1;
57 }
58
59 int rval = setNetworkForSocket(netid, fd);
60 if (rval < 0) {
61 errno = -rval;
62 rval = -1;
63 }
64 return rval;
65}
66
67int android_setprocnetwork(net_handle_t network) {
68 unsigned netid;
69 if (!getnetidfromhandle(network, &netid)) {
70 errno = EINVAL;
71 return -1;
72 }
73
74 int rval = setNetworkForProcess(netid);
75 if (rval < 0) {
76 errno = -rval;
77 rval = -1;
78 }
79 return rval;
80}
81
Remi NGUYEN VANc2f03da2021-03-21 14:30:38 +000082int android_getprocnetwork(net_handle_t *network) {
83 if (network == NULL) {
84 errno = EINVAL;
85 return -1;
86 }
87
88 unsigned netid = getNetworkForProcess();
89 *network = gethandlefromnetid(netid);
90 return 0;
91}
92
Erik Kline25f3b7b2015-03-05 15:13:37 +090093int android_getaddrinfofornetwork(net_handle_t network,
94 const char *node, const char *service,
95 const struct addrinfo *hints, struct addrinfo **res) {
96 unsigned netid;
97 if (!getnetidfromhandle(network, &netid)) {
98 errno = EINVAL;
99 return EAI_SYSTEM;
100 }
101
102 return android_getaddrinfofornet(node, service, hints, netid, 0, res);
103}
Luke Huangc17821c2018-11-20 11:38:23 +0800104
Luke Huangf3cc2b62018-12-20 14:53:29 +0800105int android_res_nquery(net_handle_t network, const char *dname,
106 int ns_class, int ns_type, enum ResNsendFlags flags) {
Luke Huangc17821c2018-11-20 11:38:23 +0800107 unsigned netid;
108 if (!getnetidfromhandle(network, &netid)) {
109 return -ENONET;
110 }
111
Luke Huangf3cc2b62018-12-20 14:53:29 +0800112 return resNetworkQuery(netid, dname, ns_class, ns_type, flags);
Luke Huangc17821c2018-11-20 11:38:23 +0800113}
114
Luke Huangd0c47e62018-12-17 15:54:18 +0800115int android_res_nresult(int fd, int *rcode, uint8_t *answer, size_t anslen) {
Luke Huangc17821c2018-11-20 11:38:23 +0800116 return resNetworkResult(fd, rcode, answer, anslen);
117}
118
Luke Huangf3cc2b62018-12-20 14:53:29 +0800119int android_res_nsend(net_handle_t network, const uint8_t *msg, size_t msglen,
120 enum ResNsendFlags flags) {
Luke Huangc17821c2018-11-20 11:38:23 +0800121 unsigned netid;
122 if (!getnetidfromhandle(network, &netid)) {
123 return -ENONET;
124 }
125
Luke Huangf3cc2b62018-12-20 14:53:29 +0800126 return resNetworkSend(netid, msg, msglen, flags);
Luke Huangc17821c2018-11-20 11:38:23 +0800127}
128
129void android_res_cancel(int nsend_fd) {
130 resNetworkCancel(nsend_fd);
131}