Following commit for asynchronous DNS query API
Test: built, flashed, booted
system/netd/tests/runtests.sh passes
atest CtsNativeNetDnsTestCases
Change-Id: I2b796ba50d42981d295fc4b00654916f67ca346e
diff --git a/client/Android.bp b/client/Android.bp
index db3ac69..4c27117 100644
--- a/client/Android.bp
+++ b/client/Android.bp
@@ -20,7 +20,7 @@
],
header_libs: [
"libnetd_client_headers",
- "libbase_headers",
+ "libbase_headers", // for unique_fd.h
],
export_header_lib_headers: ["libnetd_client_headers"],
include_dirs: [
diff --git a/client/NetdClient.cpp b/client/NetdClient.cpp
index de9b0f5..68aa3c3 100644
--- a/client/NetdClient.cpp
+++ b/client/NetdClient.cpp
@@ -386,18 +386,19 @@
}
extern "C" int resNetworkQuery(unsigned netId, const char* dname, int ns_class, int ns_type) {
- u_char buf[MAX_CMD_SIZE] = {};
- int len = res_mkquery(QUERY, dname, ns_class, ns_type, NULL, 0, NULL, buf, sizeof(buf));
+ std::vector<uint8_t> buf(MAX_CMD_SIZE, 0);
+ int len = res_mkquery(ns_o_query, dname, ns_class, ns_type, nullptr, 0, nullptr, buf.data(),
+ MAX_CMD_SIZE);
- return resNetworkSend(netId, buf, len);
+ return resNetworkSend(netId, buf.data(), len);
}
-extern "C" int resNetworkSend(unsigned netId, const uint8_t* msg, int msglen) {
+extern "C" int resNetworkSend(unsigned netId, const uint8_t* msg, size_t msglen) {
// Encode
// Base 64 encodes every 3 bytes into 4 characters, but then adds padding to the next
// multiple of 4 and a \0
const size_t encodedLen = divCeil(msglen, 3) * 4 + 1;
- auto encodedQuery = std::string(encodedLen - 1, 0);
+ std::string encodedQuery(encodedLen - 1, 0);
int enLen = b64_ntop(msg, msglen, encodedQuery.data(), encodedLen);
if (enLen < 0) {
@@ -425,7 +426,7 @@
return fd;
}
-extern "C" int resNetworkResult(int fd, int* rcode, uint8_t* answer, int anslen) {
+extern "C" int resNetworkResult(int fd, int* rcode, uint8_t* answer, size_t anslen) {
int32_t result = 0;
unique_fd ufd(fd);
// Read -errno/rcode
@@ -446,7 +447,7 @@
// Unexpected behavior, read ans len fail
return -EREMOTEIO;
}
- if (anslen < size) {
+ if (anslen < static_cast<size_t>(size)) {
// Answer buffer is too small
return -EMSGSIZE;
}
diff --git a/include/NetdClient.h b/include/NetdClient.h
index c2c7148..c361802 100644
--- a/include/NetdClient.h
+++ b/include/NetdClient.h
@@ -51,11 +51,9 @@
int resNetworkQuery(unsigned netId, const char* dname, int classType, int type);
-int resNetworkResult(int query_fd, int* rcode, uint8_t* answer, int anslen);
+int resNetworkResult(int query_fd, int* rcode, uint8_t* answer, size_t anslen);
-int resNetworkSend(unsigned netId, const uint8_t* msg, int msglen);
-
-int resNetworkRecv(int send_fd, int* rcode, uint8_t* answer, int anslen);
+int resNetworkSend(unsigned netId, const uint8_t* msg, size_t msglen);
void resNetworkCancel(int nsend_fd);
__END_DECLS
diff --git a/server/DnsProxyListener.cpp b/server/DnsProxyListener.cpp
index 10dab5c..2737814 100644
--- a/server/DnsProxyListener.cpp
+++ b/server/DnsProxyListener.cpp
@@ -185,7 +185,7 @@
if (ns_parserr(&handle, ns_s_an, i, &rr) < 0) {
continue;
}
- const u_char* rdata = ns_rr_rdata(rr);
+ const uint8_t* rdata = ns_rr_rdata(rr);
if (ipType == ns_t_a) {
sockaddr_in sin = {.sin_family = AF_INET};
memcpy(&sin.sin_addr, rdata, sizeof(sin.sin_addr));
@@ -209,7 +209,7 @@
errno = 0;
auto result = strtoul(input, &endPtr, base);
// Check the length in order to ensure there is no "-" sign
- if (!*input || *endPtr || (endPtr - input) != static_cast<long>(strlen(input)) ||
+ if (!*input || *endPtr || (endPtr - input) != static_cast<ptrdiff_t>(strlen(input)) ||
(errno == ERANGE && (result == ULONG_MAX))) {
return false;
}
@@ -217,7 +217,7 @@
return true;
}
-bool parseQuery(const u_char* msg, int msgLen, int* rr_type, std::string* rr_name) {
+bool parseQuery(const uint8_t* msg, size_t msgLen, int* rr_type, std::string* rr_name) {
ns_msg handle;
ns_rr rr;
if (ns_initparse((const uint8_t*) msg, msgLen, &handle) < 0 ||
@@ -782,10 +782,10 @@
maybeFixupNetContext(&mNetContext);
// Decode
- u_char msg[MAXPACKET] = {};
+ std::vector<uint8_t> msg(MAXPACKET, 0);
// Max length of mMsg is less than 1024 since the CMD_BUF_SIZE in FrameworkListener is 1024
- int msgLen = b64_pton(mMsg.c_str(), (u_char*) msg, MAXPACKET);
+ int msgLen = b64_pton(mMsg.c_str(), msg.data(), MAXPACKET);
if (msgLen == -1) {
// Decode fail
sendBE32(mClient, -EILSEQ);
@@ -798,7 +798,7 @@
// TODO: Handle the case which is msg contains more than one query
// Parse and store query type/name
- if (!parseQuery(msg, msgLen, &rr_type, &rr_name)) {
+ if (!parseQuery(msg.data(), msgLen, &rr_type, &rr_name)) {
// If the query couldn't be parsed, block the request.
ALOGW("resnsend: from UID %d, invalid query", uid);
sendBE32(mClient, -EINVAL);
@@ -806,10 +806,11 @@
}
// Send DNS query
- u_char ansBuf[MAXPACKET] = {};
- int arcode = 1, nsendAns = -1;
+ std::vector<uint8_t> ansBuf(MAXPACKET, 0);
+ int arcode, nsendAns = -1;
if (queryLimiter.start(uid)) {
- nsendAns = resolv_res_nsend(&mNetContext, msg, msgLen, ansBuf, sizeof(ansBuf), &arcode);
+ nsendAns = resolv_res_nsend(&mNetContext, msg.data(), msgLen, ansBuf.data(), MAXPACKET,
+ &arcode);
queryLimiter.finish(uid);
} else {
ALOGW("resnsend: from UID %d, max concurrent queries reached", uid);
@@ -831,7 +832,7 @@
}
// Send answer
- if (!sendLenAndData(mClient, nsendAns, ansBuf)) {
+ if (!sendLenAndData(mClient, nsendAns, ansBuf.data())) {
ALOGW("resnsend: failed to send answer to uid %d: %s", uid, strerror(errno));
return;
}
@@ -854,7 +855,7 @@
// Full event info reporting is on. Send full info.
std::vector<String16> ip_addrs;
int total_ip_addr_count =
- extractIpAddressAnswers(ansBuf, nsendAns, rr_type, &ip_addrs);
+ extractIpAddressAnswers(ansBuf.data(), nsendAns, rr_type, &ip_addrs);
mNetdEventListener->onDnsEvent(
mNetContext.dns_netid, INetdEventListener::EVENT_GETHOSTBYNAME,
nsendAns > 0 ? 0 : nsendAns, latencyMs, String16(rr_name.c_str()),