Make getdnsnetid returning app_netId instead of dns_netId and fix nits
1. getdnsnetid command return app_netId instead of dns_netId.
2. fix nits for ag/7691940
3. Add more tests
Bug: 129530368
Test: built, flashed, booted
system/netd/tests/runtests.sh
Merged-In: I920cbc7b63790c4937acc56d1811fc04026e11fd
(cherry picked from commit 6f61c9c5df158a7e718f1d3add4df4b191e1ec0c)
Change-Id: I3e46bf924067023920387a0e577f01cf672cac89
diff --git a/client/NetdClient.cpp b/client/NetdClient.cpp
index faf1484..df1ece6 100644
--- a/client/NetdClient.cpp
+++ b/client/NetdClient.cpp
@@ -29,16 +29,17 @@
#include <string>
#include <vector>
+#include <android-base/parseint.h>
+#include <android-base/unique_fd.h>
+
#include "Fwmark.h"
#include "FwmarkClient.h"
#include "FwmarkCommand.h"
+#include "netdclient_priv.h"
#include "netdutils/ResponseCode.h"
#include "netdutils/Stopwatch.h"
#include "netid_client.h"
-#include <android-base/parseint.h>
-#include "android-base/unique_fd.h"
-
using android::base::ParseInt;
using android::base::unique_fd;
using android::netdutils::ResponseCode;
@@ -201,6 +202,7 @@
const char* cache_mode = getenv("ANDROID_DNS_MODE");
const bool use_proxy = (cache_mode == NULL || strcmp(cache_mode, "local") != 0);
if (!use_proxy) {
+ errno = ENOSYS;
return -1;
}
@@ -220,7 +222,10 @@
const auto connectFunc = libcConnect ? libcConnect : connect;
if (TEMP_FAILURE_RETRY(
connectFunc(s, (const struct sockaddr*) &proxy_addr, sizeof(proxy_addr))) != 0) {
+ // Store the errno for connect because we only care about why we can't connect to dnsproxyd
+ int storedErrno = errno;
close(s);
+ errno = storedErrno;
return -1;
}
@@ -272,18 +277,18 @@
bool readBE32(int fd, int32_t* result) {
int32_t tmp;
- int n = TEMP_FAILURE_RETRY(read(fd, &tmp, sizeof(tmp)));
- if (n < 0) {
+ ssize_t n = TEMP_FAILURE_RETRY(read(fd, &tmp, sizeof(tmp)));
+ if (n < static_cast<ssize_t>(sizeof(tmp))) {
return false;
}
*result = ntohl(tmp);
return true;
}
-bool readResonseCode(int fd, int* result) {
+bool readResponseCode(int fd, int* result) {
char buf[4];
- int n = TEMP_FAILURE_RETRY(read(fd, &buf, sizeof(buf)));
- if (n < 0) {
+ ssize_t n = TEMP_FAILURE_RETRY(read(fd, &buf, sizeof(buf)));
+ if (n < static_cast<ssize_t>(sizeof(buf))) {
return false;
}
@@ -493,14 +498,24 @@
close(fd);
}
-extern "C" int getNetworkForDns() {
+extern "C" int getNetworkForDns(unsigned* dnsNetId) {
+ if (dnsNetId == nullptr) return -EFAULT;
int fd = dns_open_proxy();
if (fd == -1) {
return -errno;
}
unique_fd ufd(fd);
- unsigned dnsNetId = getNetworkForResolv(NETID_UNSET);
- const std::string cmd = "getdnsnetid " + std::to_string(dnsNetId);
+ return getNetworkForDnsInternal(fd, dnsNetId);
+}
+
+int getNetworkForDnsInternal(int fd, unsigned* dnsNetId) {
+ if (fd == -1) {
+ return -EBADF;
+ }
+
+ unsigned resolvNetId = getNetworkForResolv(NETID_UNSET);
+
+ const std::string cmd = "getdnsnetid " + std::to_string(resolvNetId);
ssize_t rc = sendData(fd, cmd.c_str(), cmd.size() + 1);
if (rc < 0) {
return rc;
@@ -508,7 +523,7 @@
int responseCode = 0;
// Read responseCode
- if (!readResonseCode(fd, &responseCode)) {
+ if (!readResponseCode(fd, &responseCode)) {
// Unexpected behavior, read responseCode fail
return -errno;
}
@@ -524,5 +539,7 @@
return -errno;
}
- return result;
+ *dnsNetId = result;
+
+ return 0;
}