1 /*
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
25 // This value MUST be kept in sync with the corresponding value in
26 // the android.net.Network#getNetworkHandle() implementation.
27 static const uint32_t kHandleMagic = 0xcafed00d;
28 static const uint32_t kHandleMagicSize = 32;
29
getnetidfromhandle(net_handle_t handle,unsigned * netid)30 static int getnetidfromhandle(net_handle_t handle, unsigned *netid) {
31 static const uint32_t k32BitMask = 0xffffffff;
32
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
gethandlefromnetid(unsigned netid)45 static 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 }
51
android_setsocknetwork(net_handle_t network,int fd)52 int 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
android_setprocnetwork(net_handle_t network)67 int 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
android_getprocnetwork(net_handle_t * network)82 int 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
android_setprocdns(net_handle_t network)93 int 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
android_getprocdns(net_handle_t * network)108 int 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
android_getaddrinfofornetwork(net_handle_t network,const char * node,const char * service,const struct addrinfo * hints,struct addrinfo ** res)125 int 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 }
136
android_res_nquery(net_handle_t network,const char * dname,int ns_class,int ns_type,enum ResNsendFlags flags)137 int android_res_nquery(net_handle_t network, const char *dname,
138 int ns_class, int ns_type, enum ResNsendFlags flags) {
139 unsigned netid;
140 if (!getnetidfromhandle(network, &netid)) {
141 return -ENONET;
142 }
143
144 return resNetworkQuery(netid, dname, ns_class, ns_type, flags);
145 }
146
android_res_nresult(int fd,int * rcode,uint8_t * answer,size_t anslen)147 int android_res_nresult(int fd, int *rcode, uint8_t *answer, size_t anslen) {
148 return resNetworkResult(fd, rcode, answer, anslen);
149 }
150
android_res_nsend(net_handle_t network,const uint8_t * msg,size_t msglen,enum ResNsendFlags flags)151 int android_res_nsend(net_handle_t network, const uint8_t *msg, size_t msglen,
152 enum ResNsendFlags flags) {
153 unsigned netid;
154 if (!getnetidfromhandle(network, &netid)) {
155 return -ENONET;
156 }
157
158 return resNetworkSend(netid, msg, msglen, flags);
159 }
160
android_res_cancel(int nsend_fd)161 void android_res_cancel(int nsend_fd) {
162 resNetworkCancel(nsend_fd);
163 }
164