Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 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 | |
Remi NGUYEN VAN | c2f03da | 2021-03-21 14:30:38 +0000 | [diff] [blame] | 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; |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 29 | |
| 30 | static int getnetidfromhandle(net_handle_t handle, unsigned *netid) { |
| 31 | static const uint32_t k32BitMask = 0xffffffff; |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 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 | |
Remi NGUYEN VAN | c2f03da | 2021-03-21 14:30:38 +0000 | [diff] [blame] | 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 | } |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 51 | |
| 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 | |
| 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 | |
Remi NGUYEN VAN | c2f03da | 2021-03-21 14:30:38 +0000 | [diff] [blame] | 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 | |
Remi NGUYEN VAN | ba43fb8 | 2021-03-23 00:57:38 +0000 | [diff] [blame] | 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 | |
| 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 | |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 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 | } |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 136 | |
Luke Huang | f3cc2b6 | 2018-12-20 14:53:29 +0800 | [diff] [blame] | 137 | int android_res_nquery(net_handle_t network, const char *dname, |
| 138 | int ns_class, int ns_type, enum ResNsendFlags flags) { |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 139 | unsigned netid; |
| 140 | if (!getnetidfromhandle(network, &netid)) { |
| 141 | return -ENONET; |
| 142 | } |
| 143 | |
Luke Huang | f3cc2b6 | 2018-12-20 14:53:29 +0800 | [diff] [blame] | 144 | return resNetworkQuery(netid, dname, ns_class, ns_type, flags); |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 145 | } |
| 146 | |
Luke Huang | d0c47e6 | 2018-12-17 15:54:18 +0800 | [diff] [blame] | 147 | int android_res_nresult(int fd, int *rcode, uint8_t *answer, size_t anslen) { |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 148 | return resNetworkResult(fd, rcode, answer, anslen); |
| 149 | } |
| 150 | |
Luke Huang | f3cc2b6 | 2018-12-20 14:53:29 +0800 | [diff] [blame] | 151 | int android_res_nsend(net_handle_t network, const uint8_t *msg, size_t msglen, |
| 152 | enum ResNsendFlags flags) { |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 153 | unsigned netid; |
| 154 | if (!getnetidfromhandle(network, &netid)) { |
| 155 | return -ENONET; |
| 156 | } |
| 157 | |
Luke Huang | f3cc2b6 | 2018-12-20 14:53:29 +0800 | [diff] [blame] | 158 | return resNetworkSend(netid, msg, msglen, flags); |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void android_res_cancel(int nsend_fd) { |
| 162 | resNetworkCancel(nsend_fd); |
| 163 | } |