Skip the legacy stats recording if DNS requests are denied by network policy

In order to prevent the false INTERNAL_ERROR event which is caused by
network policy fill up the legacy DNS stats, skip adding new legacy DNS
stats when we got errno == EPERM.

Bug: 151166599
Test: atest
Merged-In: I4c1d2a5c9f43650a5346a5ee0bb88eb36f850096

Change-Id: I5486638556c0a8fdfabbc37a1b29ed2b42f9b399
(cherry picked from commit 3991974b8cf5671b49f39531a6deb9f6fe81ebf0)
diff --git a/res_send.cpp b/res_send.cpp
index 2b84e82..6646310 100644
--- a/res_send.cpp
+++ b/res_send.cpp
@@ -411,6 +411,15 @@
     return event->mutable_dns_query_events()->add_dns_query_event();
 }
 
+static bool isNetworkRestricted(int terrno) {
+    // It's possible that system was in some network restricted mode, which blocked
+    // the operation of sending packet and resulted in EPERM errno.
+    // It would be no reason to keep retrying on that case.
+    // TODO: Check the system status to know if network restricted mode is
+    // enabled.
+    return (terrno == EPERM);
+}
+
 int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int anssiz, int* rcode,
               uint32_t flags, std::chrono::milliseconds sleepTimeMs) {
     LOG(DEBUG) << __func__;
@@ -537,14 +546,14 @@
                     // TCP fallback retry and current server does not support TCP connectin
                     useTcp = false;
                 }
-                LOG(INFO) << __func__ << ": used send_vc " << resplen;
+                LOG(INFO) << __func__ << ": used send_vc " << resplen << " terrno: " << terrno;
             } else {
                 // UDP
                 resplen = send_dg(statp, &params, buf, buflen, ans, anssiz, &terrno, &actualNs,
                                   &useTcp, &gotsomewhere, &query_time, rcode, &delay);
                 fallbackTCP = useTcp ? true : false;
                 retry_count_for_event = attempt;
-                LOG(INFO) << __func__ << ": used send_dg " << resplen;
+                LOG(INFO) << __func__ << ": used send_dg " << resplen << " terrno: " << terrno;
             }
 
             const IPSockAddr& receivedServerAddr = statp->nsaddrs[actualNs];
@@ -568,13 +577,19 @@
             // queries that deterministically fail (e.g., a name that always returns
             // SERVFAIL or times out) do not unduly affect the stats.
             if (shouldRecordStats) {
-                res_sample sample;
-                res_stats_set_sample(&sample, query_time, *rcode, delay);
-                // KeepListening UDP mechanism is incompatible with usable_servers of legacy stats,
-                // so keep the old logic for now.
-                // TODO: Replace usable_servers of legacy stats with new one.
-                resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, serverSockAddr,
-                                                       sample, params.max_samples);
+                // (b/151166599): This is a workaround to prevent that DnsResolver calculates the
+                // reliability of DNS servers from being broken when network restricted mode is
+                // enabled.
+                // TODO: Introduce the new server selection instead of skipping stats recording.
+                if (!isNetworkRestricted(terrno)) {
+                    res_sample sample;
+                    res_stats_set_sample(&sample, query_time, *rcode, delay);
+                    // KeepListening UDP mechanism is incompatible with usable_servers of legacy
+                    // stats, so keep the old logic for now.
+                    // TODO: Replace usable_servers of legacy stats with new one.
+                    resolv_cache_add_resolver_stats_sample(
+                            statp->netid, revision_id, serverSockAddr, sample, params.max_samples);
+                }
                 resolv_stats_add(statp->netid, receivedServerAddr, dnsQueryEvent);
             }