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 | |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 93 | int 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 Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 104 | |
Luke Huang | f3cc2b6 | 2018-12-20 14:53:29 +0800 | [diff] [blame] | 105 | int android_res_nquery(net_handle_t network, const char *dname, |
| 106 | int ns_class, int ns_type, enum ResNsendFlags flags) { |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 107 | unsigned netid; |
| 108 | if (!getnetidfromhandle(network, &netid)) { |
| 109 | return -ENONET; |
| 110 | } |
| 111 | |
Luke Huang | f3cc2b6 | 2018-12-20 14:53:29 +0800 | [diff] [blame] | 112 | return resNetworkQuery(netid, dname, ns_class, ns_type, flags); |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Luke Huang | d0c47e6 | 2018-12-17 15:54:18 +0800 | [diff] [blame] | 115 | 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] | 116 | return resNetworkResult(fd, rcode, answer, anslen); |
| 117 | } |
| 118 | |
Luke Huang | f3cc2b6 | 2018-12-20 14:53:29 +0800 | [diff] [blame] | 119 | int android_res_nsend(net_handle_t network, const uint8_t *msg, size_t msglen, |
| 120 | enum ResNsendFlags flags) { |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 121 | unsigned netid; |
| 122 | if (!getnetidfromhandle(network, &netid)) { |
| 123 | return -ENONET; |
| 124 | } |
| 125 | |
Luke Huang | f3cc2b6 | 2018-12-20 14:53:29 +0800 | [diff] [blame] | 126 | return resNetworkSend(netid, msg, msglen, flags); |
Luke Huang | c17821c | 2018-11-20 11:38:23 +0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void android_res_cancel(int nsend_fd) { |
| 130 | resNetworkCancel(nsend_fd); |
| 131 | } |