blob: e2f36a77b7c6d39a23690c3fdbcb0131afcee0b8 [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
Remi NGUYEN VANba43fb82021-03-23 00:57:38 +000093int android_setprocdns(net_handle_t network) {
94 unsigned netid;
95 if (!getnetidfromhandle(network, &netid)) {
96 errno = EINVAL;
97 return -1;
98 }
99
100 int rval = setNetworkForResolv(netid);
101 if (rval < 0) {
102 errno = -rval;
103 rval = -1;
104 }
105 return rval;
106}
107
108int android_getprocdns(net_handle_t *network) {
109 if (network == NULL) {
110 errno = EINVAL;
111 return -1;
112 }
113
114 unsigned netid;
115 int rval = getNetworkForDns(&netid);
116 if (rval < 0) {
117 errno = -rval;
118 return -1;
119 }
120
121 *network = gethandlefromnetid(netid);
122 return 0;
123}
124
Erik Kline25f3b7b2015-03-05 15:13:37 +0900125int android_getaddrinfofornetwork(net_handle_t network,
126 const char *node, const char *service,
127 const struct addrinfo *hints, struct addrinfo **res) {
128 unsigned netid;
129 if (!getnetidfromhandle(network, &netid)) {
130 errno = EINVAL;
131 return EAI_SYSTEM;
132 }
133
134 return android_getaddrinfofornet(node, service, hints, netid, 0, res);
135}
Luke Huangc17821c2018-11-20 11:38:23 +0800136
Luke Huangf3cc2b62018-12-20 14:53:29 +0800137int android_res_nquery(net_handle_t network, const char *dname,
138 int ns_class, int ns_type, enum ResNsendFlags flags) {
Luke Huangc17821c2018-11-20 11:38:23 +0800139 unsigned netid;
140 if (!getnetidfromhandle(network, &netid)) {
141 return -ENONET;
142 }
143
Luke Huangf3cc2b62018-12-20 14:53:29 +0800144 return resNetworkQuery(netid, dname, ns_class, ns_type, flags);
Luke Huangc17821c2018-11-20 11:38:23 +0800145}
146
Luke Huangd0c47e62018-12-17 15:54:18 +0800147int android_res_nresult(int fd, int *rcode, uint8_t *answer, size_t anslen) {
Luke Huangc17821c2018-11-20 11:38:23 +0800148 return resNetworkResult(fd, rcode, answer, anslen);
149}
150
Luke Huangf3cc2b62018-12-20 14:53:29 +0800151int android_res_nsend(net_handle_t network, const uint8_t *msg, size_t msglen,
152 enum ResNsendFlags flags) {
Luke Huangc17821c2018-11-20 11:38:23 +0800153 unsigned netid;
154 if (!getnetidfromhandle(network, &netid)) {
155 return -ENONET;
156 }
157
Luke Huangf3cc2b62018-12-20 14:53:29 +0800158 return resNetworkSend(netid, msg, msglen, flags);
Luke Huangc17821c2018-11-20 11:38:23 +0800159}
160
161void android_res_cancel(int nsend_fd) {
162 resNetworkCancel(nsend_fd);
163}