bionic: Replace iface with opaque netid in resolver.

Replace iface cache key with netid.

Replace _mark with netid.

Mark sockets used to test IPv4/IPv6 support as well as sockets
used to determine source address for rfc6724 sort.

Remove pid/uid mappings (functionality moved to system/netd).

Do not create resolv_cache when it does not exist, but rather only
when DNS configuration is supplied.

Clean up unused functions.

Change-Id: I9ccfda2902cc0943e87c9bc346ad9a2578accdab
diff --git a/libc/dns/gethnamaddr.c b/libc/dns/gethnamaddr.c
index 2234c7c..dbcadee 100644
--- a/libc/dns/gethnamaddr.c
+++ b/libc/dns/gethnamaddr.c
@@ -60,6 +60,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
+#include "resolv_netid.h"
 #include "resolv_private.h"
 #include "resolv_cache.h"
 #include <assert.h>
@@ -129,7 +130,7 @@
 static int _dns_gethtbyaddr(void *, void *, va_list);
 static int _dns_gethtbyname(void *, void *, va_list);
 
-static struct hostent *gethostbyname_internal(const char *, int, res_state, const char *, int);
+static struct hostent *gethostbyname_internal(const char *, int, res_state, unsigned, unsigned);
 
 static const ns_src default_dns_files[] = {
 	{ NSSRC_FILES, 	NS_SUCCESS },
@@ -500,13 +501,13 @@
 
 	/* try IPv6 first - if that fails do IPv4 */
 	if (res->options & RES_USE_INET6) {
-		hp = gethostbyname_internal(name, AF_INET6, res, NULL, 0);
+		hp = gethostbyname_internal(name, AF_INET6, res, NETID_UNSET, MARK_UNSET);
 		if (hp) {
 			__res_put_state(res);
 			return hp;
 		}
 	}
-	hp = gethostbyname_internal(name, AF_INET, res, NULL, 0);
+	hp = gethostbyname_internal(name, AF_INET, res, NETID_UNSET, MARK_UNSET);
 	__res_put_state(res);
 	return hp;
 }
@@ -514,18 +515,18 @@
 struct hostent *
 gethostbyname2(const char *name, int af)
 {
-	return android_gethostbynameforiface(name, af, NULL, 0);
+	return android_gethostbynamefornet(name, af, NETID_UNSET, MARK_UNSET);
 }
 
 struct hostent *
-android_gethostbynameforiface(const char *name, int af, const char *iface, int mark)
+android_gethostbynamefornet(const char *name, int af, unsigned netid, unsigned mark)
 {
 	struct hostent *hp;
 	res_state res = __res_get_state();
 
 	if (res == NULL)
 		return NULL;
-	hp = gethostbyname_internal(name, af, res, iface, mark);
+	hp = gethostbyname_internal(name, af, res, netid, mark);
 	__res_put_state(res);
 	return hp;
 }
@@ -744,14 +745,14 @@
 
 // very similar in proxy-ness to android_getaddrinfo_proxy
 static struct hostent *
-gethostbyname_internal(const char *name, int af, res_state res, const char *iface, int mark)
+gethostbyname_internal(const char *name, int af, res_state res, unsigned netid, unsigned mark)
 {
 	const char *cache_mode = getenv("ANDROID_DNS_MODE");
 	FILE* proxy = NULL;
 	struct hostent *result = NULL;
 
 	if (cache_mode != NULL && strcmp(cache_mode, "local") == 0) {
-		res_setiface(res, iface);
+		res_setnetid(res, netid);
 		res_setmark(res, mark);
 		return gethostbyname_internal_real(name, af, res);
 	}
@@ -761,8 +762,8 @@
 
 	/* This is writing to system/netd/DnsProxyListener.cpp and changes
 	 * here need to be matched there */
-	if (fprintf(proxy, "gethostbyname %s %s %d",
-			iface == NULL ? "^" : iface,
+	if (fprintf(proxy, "gethostbyname %u %s %d",
+			netid,
 			name == NULL ? "^" : name,
 			af) < 0) {
 		goto exit;
@@ -783,8 +784,8 @@
 
 
 struct hostent *
-android_gethostbyaddrforiface_proxy(const void *addr,
-    socklen_t len, int af, const char* iface, int mark)
+android_gethostbyaddrfornet_proxy(const void *addr,
+    socklen_t len, int af, unsigned netid)
 {
 	struct hostent *result = NULL;
 	FILE* proxy = android_open_proxy();
@@ -795,8 +796,8 @@
 	const char * addrStr = inet_ntop(af, addr, buf, sizeof(buf));
 	if (addrStr == NULL) goto exit;
 
-	if (fprintf(proxy, "gethostbyaddr %s %d %d %s",
-			addrStr, len, af, iface == NULL ? "^" : iface) < 0) {
+	if (fprintf(proxy, "gethostbyaddr %s %d %d %u",
+			addrStr, len, af, netid) < 0) {
 		goto exit;
 	}
 
@@ -813,8 +814,8 @@
 }
 
 struct hostent *
-android_gethostbyaddrforiface_real(const void *addr,
-    socklen_t len, int af, const char* iface, int mark)
+android_gethostbyaddrfornet_real(const void *addr,
+    socklen_t len, int af, unsigned netid, unsigned mark)
 {
 	const u_char *uaddr = (const u_char *)addr;
 	socklen_t size;
@@ -862,28 +863,28 @@
 	hp = NULL;
 	h_errno = NETDB_INTERNAL;
 	if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
-		default_dns_files, uaddr, len, af, iface, mark) != NS_SUCCESS)
+		default_dns_files, uaddr, len, af, netid, mark) != NS_SUCCESS)
 		return NULL;
 	h_errno = NETDB_SUCCESS;
 	return hp;
 }
 
 struct hostent *
-android_gethostbyaddrforiface(const void *addr, socklen_t len, int af, const char* iface, int mark)
+android_gethostbyaddrfornet(const void *addr, socklen_t len, int af, unsigned netid, unsigned mark)
 {
 	const char *cache_mode = getenv("ANDROID_DNS_MODE");
 
 	if (cache_mode == NULL || strcmp(cache_mode, "local") != 0) {
-		return android_gethostbyaddrforiface_proxy(addr, len, af, iface, mark);
+		return android_gethostbyaddrfornet_proxy(addr, len, af, netid);
 	} else {
-		return android_gethostbyaddrforiface_real(addr,len, af, iface, mark);
+		return android_gethostbyaddrfornet_real(addr,len, af, netid, mark);
 	}
 }
 
 struct hostent *
 gethostbyaddr(const void *addr, socklen_t len, int af)
 {
-	return android_gethostbyaddrforiface(addr, len, af, NULL, 0);
+	return android_gethostbyaddrfornet(addr, len, af, NETID_UNSET, MARK_UNSET);
 }
 
 
@@ -1318,8 +1319,7 @@
 	const unsigned char *uaddr;
 	int len, af, advance;
 	res_state res;
-	const char* iface;
-	int mark;
+	unsigned netid, mark;
 	res_static rs = __res_get_static();
 
 	assert(rv != NULL);
@@ -1327,8 +1327,8 @@
 	uaddr = va_arg(ap, unsigned char *);
 	len = va_arg(ap, int);
 	af = va_arg(ap, int);
-	iface = va_arg(ap, char *);
-	mark = va_arg(ap, int);
+	netid = va_arg(ap, unsigned);
+	mark = va_arg(ap, unsigned);
 
 	switch (af) {
 	case AF_INET:
@@ -1370,7 +1370,7 @@
 		free(buf);
 		return NS_NOTFOUND;
 	}
-	res_setiface(res, iface);
+	res_setnetid(res, netid);
 	res_setmark(res, mark);
 	n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
 	if (n < 0) {