Support enable/disable dns forwarding of dnsmasq

Bugs: 128782815
Test: built, flashed, booted
      cd systen/netd && atest

Change-Id: Id72341885d828e30296f20590c64ff614df63cee
diff --git a/server/TetherController.cpp b/server/TetherController.cpp
index 9d56b3e..c987d63 100644
--- a/server/TetherController.cpp
+++ b/server/TetherController.cpp
@@ -190,8 +190,15 @@
     return mForwardingRequests;
 }
 
-int TetherController::startTethering(int num_addrs, char **dhcp_ranges) {
-    if (mDaemonPid != 0) {
+int TetherController::startTethering(bool usingLegacyDnsProxy, int num_addrs, char** dhcp_ranges) {
+    if (!usingLegacyDnsProxy && num_addrs == 0) {
+        // Both DHCP and DnsProxy are disabled, we don't need to start dnsmasq
+        configureForTethering(true);
+        mIsTetheringStarted = true;
+        return 0;
+    }
+
+    if (mIsTetheringStarted) {
         ALOGE("Tethering already started");
         errno = EBUSY;
         return -errno;
@@ -230,8 +237,11 @@
             kDnsmasqUsername,
     };
 
-    // DHCP server will be disabled if num_addrs == 0 and no --dhcp-range is
-    // passed.
+    if (!usingLegacyDnsProxy) {
+        argVector.push_back("--port=0");
+    }
+
+    // DHCP server will be disabled if num_addrs == 0 and no --dhcp-range is passed.
     for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) {
         argVector.push_back(StringPrintf("--dhcp-range=%s,%s,1h", dhcp_ranges[addrIndex],
                                          dhcp_ranges[addrIndex + 1]));
@@ -286,6 +296,7 @@
     mDaemonPid = pid;
     mDaemonFd = pipeWrite.release();
     configureForTethering(true);
+    mIsTetheringStarted = true;
     applyDnsInterfaces();
     ALOGD("Tethering services running");
 
@@ -301,7 +312,8 @@
     return addrsCstrVec;
 }
 
-int TetherController::startTethering(const std::vector<std::string>& dhcpRanges) {
+int TetherController::startTethering(bool usingLegacyDnsProxy,
+                                     const std::vector<std::string>& dhcpRanges) {
     struct in_addr v4_addr;
     for (const auto& dhcpRange : dhcpRanges) {
         if (!inet_aton(dhcpRange.c_str(), &v4_addr)) {
@@ -309,17 +321,23 @@
         }
     }
     auto dhcp_ranges = toCstrVec(dhcpRanges);
-    return startTethering(dhcp_ranges.size(), dhcp_ranges.data());
+    return startTethering(usingLegacyDnsProxy, dhcp_ranges.size(), dhcp_ranges.data());
 }
 
 int TetherController::stopTethering() {
     configureForTethering(false);
 
-    if (mDaemonPid == 0) {
+    if (!mIsTetheringStarted) {
         ALOGE("Tethering already stopped");
         return 0;
     }
 
+    mIsTetheringStarted = false;
+    // dnsmasq is not started
+    if (mDaemonPid == 0) {
+        return 0;
+    }
+
     ALOGD("Stopping tethering services");
 
     kill(mDaemonPid, SIGTERM);
@@ -333,7 +351,7 @@
 }
 
 bool TetherController::isTetheringStarted() {
-    return (mDaemonPid == 0 ? false : true);
+    return mIsTetheringStarted;
 }
 
 // dnsmasq can't parse commands larger than this due to the fixed-size buffer