Add new info functions to resolver for use by Netd.

android_net_res_stats_get_info_for_net returns the current name servers, search
domains, parameters and stats for the given network ID.

android_net_res_stats_aggregate provides statistics such as errors counts from
the raw stats data reported by android_net_res_stats_get_info_for_net.

android_net_res_stats_get_usable_servers uses the data returned by
android_net_res_stats_aggregate to determine which of the servers are
considered valid or broken by the resolver.

BUG: 25731675

Change-Id: I6059b68e5e8b809027a4d3135f6081588bee8a7d
diff --git a/libc/dns/resolv/res_cache.c b/libc/dns/resolv/res_cache.c
index 5f51c49..02887f2 100644
--- a/libc/dns/resolv/res_cache.c
+++ b/libc/dns/resolv/res_cache.c
@@ -27,6 +27,7 @@
  */
 
 #include "resolv_cache.h"
+
 #include <resolv.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -1238,8 +1239,7 @@
     int                         revision_id; // # times the nameservers have been replaced
     struct __res_params         params;
     struct __res_stats          nsstats[MAXNS];
-    // TODO: replace with char* defdname[MAXDNSRCH]
-    char                        defdname[256];
+    char                        defdname[MAXDNSRCHPATH];
     int                         dnsrch_offset[MAXDNSRCH+1];  // offsets into defdname
 };
 
@@ -2183,6 +2183,68 @@
 }
 
 int
+android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
+        struct sockaddr_storage servers[MAXNS], int* dcount, char domains[MAXDNSRCH][MAXDNSRCHPATH],
+        struct __res_params* params, struct __res_stats stats[MAXNS]) {
+    int revision_id = -1;
+    pthread_mutex_lock(&_res_cache_list_lock);
+
+    struct resolv_cache_info* info = _find_cache_info_locked(netid);
+    if (info) {
+        if (info->nscount > MAXNS) {
+            pthread_mutex_unlock(&_res_cache_list_lock);
+            XLOG("%s: nscount %d > MAXNS %d", __FUNCTION__, info->nscount, MAXNS);
+            errno = EFAULT;
+            return -1;
+        }
+        int i;
+        for (i = 0; i < info->nscount; i++) {
+            // Verify that the following assumptions are held, failure indicates corruption:
+            //  - getaddrinfo() may never return a sockaddr > sockaddr_storage
+            //  - all addresses are valid
+            //  - there is only one address per addrinfo thanks to numeric resolution
+            int addrlen = info->nsaddrinfo[i]->ai_addrlen;
+            if (addrlen < (int) sizeof(struct sockaddr) ||
+                    addrlen > (int) sizeof(servers[0])) {
+                pthread_mutex_unlock(&_res_cache_list_lock);
+                XLOG("%s: nsaddrinfo[%d].ai_addrlen == %d", __FUNCTION__, i, addrlen);
+                errno = EMSGSIZE;
+                return -1;
+            }
+            if (info->nsaddrinfo[i]->ai_addr == NULL) {
+                pthread_mutex_unlock(&_res_cache_list_lock);
+                XLOG("%s: nsaddrinfo[%d].ai_addr == NULL", __FUNCTION__, i);
+                errno = ENOENT;
+                return -1;
+            }
+            if (info->nsaddrinfo[i]->ai_next != NULL) {
+                pthread_mutex_unlock(&_res_cache_list_lock);
+                XLOG("%s: nsaddrinfo[%d].ai_next != NULL", __FUNCTION__, i);
+                errno = ENOTUNIQ;
+                return -1;
+            }
+        }
+        *nscount = info->nscount;
+        for (i = 0; i < info->nscount; i++) {
+            memcpy(&servers[i], info->nsaddrinfo[i]->ai_addr, info->nsaddrinfo[i]->ai_addrlen);
+            stats[i] = info->nsstats[i];
+        }
+        for (i = 0; i < MAXDNSRCH; i++) {
+            if (info->dnsrch_offset[i] == -1) {
+                break;
+            }
+            strlcpy(domains[i], info->defdname + info->dnsrch_offset[i], MAXDNSRCHPATH);
+        }
+        *dcount = i;
+        *params = info->params;
+        revision_id = info->revision_id;
+    }
+
+    pthread_mutex_unlock(&_res_cache_list_lock);
+    return revision_id;
+}
+
+int
 _resolv_cache_get_resolver_stats( unsigned netid, struct __res_params* params,
         struct __res_stats stats[MAXNS]) {
     int revision_id = -1;
diff --git a/libc/dns/resolv/res_send.c b/libc/dns/resolv/res_send.c
index bfc6e1a..0e30b28 100644
--- a/libc/dns/resolv/res_send.c
+++ b/libc/dns/resolv/res_send.c
@@ -492,7 +492,8 @@
 	    struct __res_params params;
 	    int revision_id = _resolv_cache_get_resolver_stats(statp->netid, &params, stats);
 	    bool usable_servers[MAXNS];
-	    _res_stats_get_usable_servers(&params, stats, statp->nscount, usable_servers);
+	    android_net_res_stats_get_usable_servers(&params, stats, statp->nscount,
+		    usable_servers);
 
 	    for (ns = 0; ns < statp->nscount; ns++) {
 		if (!usable_servers[ns]) continue;
diff --git a/libc/dns/resolv/res_stats.c b/libc/dns/resolv/res_stats.c
index 5cc592e..99c79e4 100644
--- a/libc/dns/resolv/res_stats.c
+++ b/libc/dns/resolv/res_stats.c
@@ -54,8 +54,8 @@
 
 /* Aggregates the reachability statistics for the given server based on on the stored samples. */
 void
-_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors, int* timeouts,
-             int* internal_errors, int* rtt_avg, time_t* last_sample_time)
+android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
+        int* timeouts, int* internal_errors, int* rtt_avg, time_t* last_sample_time)
 {
     int s = 0;   // successes
     int e = 0;   // errors
@@ -123,8 +123,8 @@
     int internal_errors = -1;
     int rtt_avg = -1;
     time_t last_sample_time = 0;
-    _res_stats_aggregate(stats, &successes, &errors, &timeouts, &internal_errors, &rtt_avg,
-            &last_sample_time);
+    android_net_res_stats_aggregate(stats, &successes, &errors, &timeouts, &internal_errors,
+            &rtt_avg, &last_sample_time);
     if (successes >= 0 && errors >= 0 && timeouts >= 0) {
         int total = successes + errors + timeouts;
         if (DBG) {
@@ -164,8 +164,8 @@
 }
 
 void
-_res_stats_get_usable_servers(const struct __res_params* params, struct __res_stats stats[],
-        int nscount, bool usable_servers[]) {
+android_net_res_stats_get_usable_servers(const struct __res_params* params,
+        struct __res_stats stats[], int nscount, bool usable_servers[]) {
     unsigned usable_servers_found = 0;
     for (int ns = 0; ns < nscount; ns++) {
         bool usable = _res_stats_usable_server(params, &stats[ns]);