Merge "Add a netcontext variant of gethostbyname"
diff --git a/libc/dns/include/resolv_netid.h b/libc/dns/include/resolv_netid.h
index 711792b..3b2f4da 100644
--- a/libc/dns/include/resolv_netid.h
+++ b/libc/dns/include/resolv_netid.h
@@ -86,6 +86,7 @@
  * explore_fqdn() dispatch table method, with the below function only making DNS calls.
  */
 struct hostent *android_gethostbyaddrfornetcontext(const void *, socklen_t, int, const struct android_net_context *) __used_in_netd;
+struct hostent *android_gethostbynamefornetcontext(const char *, int, const struct android_net_context *) __used_in_netd;
 int android_getaddrinfofornetcontext(const char *, const char *, const struct addrinfo *,
     const struct android_net_context *, struct addrinfo **) __used_in_netd;
 
diff --git a/libc/dns/net/gethnamaddr.c b/libc/dns/net/gethnamaddr.c
index 036ad1d..a22d718 100644
--- a/libc/dns/net/gethnamaddr.c
+++ b/libc/dns/net/gethnamaddr.c
@@ -160,7 +160,7 @@
 static int _dns_gethtbyname(void *, void *, va_list);
 
 static struct hostent *gethostbyname_internal(const char *, int, res_state,
-    struct hostent *, char *, size_t, int *, unsigned, unsigned);
+    struct hostent *, char *, size_t, int *, const struct android_net_context *);
 static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t,
     int, struct hostent *, char *, size_t, int *, const struct android_net_context *);
 
@@ -529,15 +529,15 @@
 	_DIAGASSERT(name != NULL);
 
 	if (res->options & RES_USE_INET6) {
-		*result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp, NETID_UNSET,
-		                                 MARK_UNSET);
+		*result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
+		                                 &NETCONTEXT_UNSET);
 		if (*result) {
 			__res_put_state(res);
 			return 0;
 		}
 	}
-	*result = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, NETID_UNSET,
-	                                 MARK_UNSET);
+	*result = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp,
+	                                 &NETCONTEXT_UNSET);
 	__res_put_state(res);
 	if (!*result && errno == ENOSPC) {
 	  errno = ERANGE;
@@ -558,8 +558,8 @@
 		*errorp = NETDB_INTERNAL;
 		return -1;
 	}
-	*result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, NETID_UNSET,
-	                                 MARK_UNSET);
+	*result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp,
+	                                 &NETCONTEXT_UNSET);
 	__res_put_state(res);
 	if (!*result && errno == ENOSPC) {
 		errno = ERANGE;
@@ -822,17 +822,17 @@
 // very similar in proxy-ness to android_getaddrinfo_proxy
 static struct hostent *
 gethostbyname_internal(const char *name, int af, res_state res, struct hostent *hp, char *hbuf,
-                       size_t hbuflen, int *errorp, unsigned netid, unsigned mark)
+                       size_t hbuflen, int *errorp, const struct android_net_context *netcontext)
 {
 	FILE* proxy = android_open_proxy();
 	if (proxy == NULL) {
 		// Either we're not supposed to be using the proxy or the proxy is unavailable.
-		res_setnetid(res, netid);
-		res_setmark(res, mark);
+		res_setnetid(res, netcontext->dns_netid);
+		res_setmark(res, netcontext->dns_mark);
 		return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
 	}
 
-	netid = __netdClientDispatch.netIdForResolv(netid);
+	unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
 
 	// This is writing to system/netd/server/DnsProxyListener.cpp and changes
 	// here need to be matched there.
@@ -1611,13 +1611,21 @@
 struct hostent *
 android_gethostbynamefornet(const char *name, int af, unsigned netid, unsigned mark)
 {
+	const struct android_net_context netcontext = make_context(netid, mark);
+	return android_gethostbynamefornetcontext(name, af, &netcontext);
+}
+
+struct hostent *
+android_gethostbynamefornetcontext(const char *name, int af,
+	const struct android_net_context *netcontext)
+{
 	struct hostent *hp;
 	res_state res = __res_get_state();
 	if (res == NULL)
 		return NULL;
 	res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
 	hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
-	                            &h_errno, netid, mark);
+	                            &h_errno, netcontext);
 	__res_put_state(res);
 	return hp;
 }
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index a74027a..b32f769 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1496,6 +1496,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     arc4random_addrandom; # arm x86 mips
     arc4random_stir; # arm x86 mips
     atexit; # arm
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 34d3429..afcf8bb 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1244,6 +1244,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index 90929f5..f05e7eb 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1522,6 +1522,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     arc4random_addrandom; # arm x86 mips
     arc4random_stir; # arm x86 mips
     atexit; # arm
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index aac876d..a472214 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1339,6 +1339,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     arc4random_addrandom; # arm x86 mips
     arc4random_stir; # arm x86 mips
     bcopy; # arm x86 mips
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 34d3429..afcf8bb 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1244,6 +1244,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index 682f2c4..27a597f 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1338,6 +1338,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     arc4random_addrandom; # arm x86 mips
     arc4random_stir; # arm x86 mips
     bcopy; # arm x86 mips
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 34d3429..afcf8bb 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1244,6 +1244,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;