dns cache per interface iteration 2

name server addresses are read from the dns
cache associated wih the interface on which
the request shall be done.

processes which has requested to issue dns request
using specific interface are now proxied to netd.

added methods to attach/detach a process to a specific
dns cache/interface.

added getaddrinfoforinface method which takes an
interface as an argument.

bug:4815099
bug:5465296
Change-Id: I7a8fe1980cdf99d4d296ddc5c6411f0c72162263
diff --git a/libc/netbsd/resolv/res_send.c b/libc/netbsd/resolv/res_send.c
index f3ee539..ceb2c77 100644
--- a/libc/netbsd/resolv/res_send.c
+++ b/libc/netbsd/resolv/res_send.c
@@ -370,10 +370,13 @@
         ResolvCacheStatus     cache_status = RESOLV_CACHE_UNSUPPORTED;
 #endif
 
+#if !USE_RESOLV_CACHE
 	if (statp->nscount == 0) {
 		errno = ESRCH;
 		return (-1);
 	}
+#endif
+
 	if (anssiz < HFIXEDSZ) {
 		errno = EINVAL;
 		return (-1);
@@ -385,17 +388,27 @@
 	terrno = ETIMEDOUT;
 
 #if USE_RESOLV_CACHE
-        cache = __get_res_cache();
-        if (cache != NULL) {
-            int  anslen = 0;
-            cache_status = _resolv_cache_lookup(
-                                cache, buf, buflen,
-                                ans, anssiz, &anslen);
+	// get the cache associated with the interface
+	cache = __get_res_cache(statp->iface);
+	if (cache != NULL) {
+		int  anslen = 0;
+		cache_status = _resolv_cache_lookup(
+				cache, buf, buflen,
+				ans, anssiz, &anslen);
 
-            if (cache_status == RESOLV_CACHE_FOUND) {
-                return anslen;
-            }
-        }
+		if (cache_status == RESOLV_CACHE_FOUND) {
+			return anslen;
+		} else {
+			// had a cache miss for a known interface, so populate the thread private
+			// data so the normal resolve path can do its thing
+			_resolv_populate_res_for_iface(statp);
+		}
+	}
+
+	if (statp->nscount == 0) {
+		errno = ESRCH;
+		return (-1);
+	}
 #endif
 
 	/*