diff --git a/nis/Versions b/nis/Versions
index ef9a512417..537f0aa3c0 100644
--- a/nis/Versions
+++ b/nis/Versions
@@ -96,6 +96,7 @@ libnss_nis {
     _nss_nis_setnetgrent; _nss_nis_setprotoent; _nss_nis_setpwent;
     _nss_nis_setrpcent; _nss_nis_setservent; _nss_nis_setspent;
     _nss_nis_initgroups_dyn; _nss_nis_gethostbyname4_r;
+    _nss_nis_gethostbyname5_r;
   }
 }
 
@@ -126,6 +127,6 @@ libnss_nisplus {
     _nss_nisplus_setnetent; _nss_nisplus_setnetgrent; _nss_nisplus_setprotoent;
     _nss_nisplus_setpwent; _nss_nisplus_setrpcent; _nss_nisplus_setservent;
     _nss_nisplus_setspent; _nss_nisplus_initgroups_dyn;
-    _nss_nisplus_gethostbyname4_r;
+    _nss_nisplus_gethostbyname4_r; _nss_nisplus_gethostbyname5_r;
   }
 }
diff --git a/nis/nss_nis/nis-hosts.c b/nis/nss_nis/nis-hosts.c
index f64dbdaecb..3a156aa7ee 100644
--- a/nis/nss_nis/nis-hosts.c
+++ b/nis/nss_nis/nis-hosts.c
@@ -454,7 +454,7 @@ _nss_nis_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af,
 
 
 enum nss_status
-_nss_nis_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
+_nss_nis_gethostbyname5_r (const char *name, int af, struct gaih_addrtuple **pat,
 			   char *buffer, size_t buflen, int *errnop,
 			   int *herrnop, int32_t *ttlp)
 {
@@ -530,7 +530,7 @@ _nss_nis_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
   buflen -= pad;
 
   struct hostent host;
-  int parse_res = parse_line (result, &host, data, buflen, errnop, AF_UNSPEC,
+  int parse_res = parse_line (result, &host, data, buflen, errnop, af,
 			      0);
   if (__glibc_unlikely (parse_res < 1))
     {
@@ -565,3 +565,12 @@ _nss_nis_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
 
   return NSS_STATUS_SUCCESS;
 }
+
+enum nss_status
+_nss_nis_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
+			   char *buffer, size_t buflen, int *errnop,
+			   int *herrnop, int32_t *ttlp)
+{
+  return _nss_nis_gethostbyname5_r (name, AF_UNSPEC, pat, buffer, buflen,
+				    errnop, herrnop, ttlp);
+}
diff --git a/nis/nss_nisplus/nisplus-hosts.c b/nis/nss_nisplus/nisplus-hosts.c
index 31dfd31fbd..d9c5d7a974 100644
--- a/nis/nss_nisplus/nisplus-hosts.c
+++ b/nis/nss_nisplus/nisplus-hosts.c
@@ -579,13 +579,13 @@ _nss_nisplus_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af,
 
 
 enum nss_status
-_nss_nisplus_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
+_nss_nisplus_gethostbyname5_r (const char *name, int af, struct gaih_addrtuple **pat,
 			       char *buffer, size_t buflen, int *errnop,
 			       int *herrnop, int32_t *ttlp)
 {
   struct hostent host;
 
-  enum nss_status status = internal_gethostbyname2_r (name, AF_UNSPEC, &host,
+  enum nss_status status = internal_gethostbyname2_r (name, af, &host,
 						      buffer, buflen,
 						      errnop, herrnop, 0);
   if (__glibc_likely (status == NSS_STATUS_SUCCESS))
@@ -617,3 +617,12 @@ _nss_nisplus_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
 
   return status;
 }
+
+enum nss_status
+_nss_nisplus_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
+			       char *buffer, size_t buflen, int *errnop,
+			       int *herrnop, int32_t *ttlp)
+{
+  return _nss_nisplus_gethostbyname5_r (name, AF_UNSPEC, pat, buffer,
+					buflen, errnop, herrnop, ttlp);
+}
diff --git a/nss/Versions b/nss/Versions
index 50268ed9b5..72adf4c806 100644
--- a/nss/Versions
+++ b/nss/Versions
@@ -44,6 +44,7 @@ libnss_files {
     _nss_files_gethostbyname2_r;
     _nss_files_gethostbyname3_r;
     _nss_files_gethostbyname4_r;
+    _nss_files_gethostbyname5_r;
     _nss_files_gethostbyname_r;
     _nss_files_gethostent_r;
 
diff --git a/nss/nss_files/files-hosts.c b/nss/nss_files/files-hosts.c
index 28eb73fb2d..c6067cc641 100644
--- a/nss/nss_files/files-hosts.c
+++ b/nss/nss_files/files-hosts.c
@@ -404,7 +404,7 @@ _nss_files_gethostbyname2_r (const char *name, int af, struct hostent *result,
 }
 
 enum nss_status
-_nss_files_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
+_nss_files_gethostbyname5_r (const char *name, int af, struct gaih_addrtuple **pat,
 			     char *buffer, size_t buflen, int *errnop,
 			     int *herrnop, int32_t *ttlp)
 {
@@ -428,7 +428,7 @@ _nss_files_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
 
 	  struct hostent result;
 	  status = internal_getent (stream, &result, buffer, buflen, errnop,
-				    herrnop, AF_UNSPEC, 0, &in6_zone_id);
+				    herrnop, af, 0, &in6_zone_id);
 	  if (status != NSS_STATUS_SUCCESS)
 	    break;
 
@@ -518,3 +518,12 @@ _nss_files_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
 
   return status;
 }
+
+enum nss_status
+_nss_files_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
+			     char *buffer, size_t buflen, int *errnop,
+			     int *herrnop, int32_t *ttlp)
+{
+  return _nss_files_gethostbyname5_r (name, AF_UNSPEC, pat, buffer, buflen,
+				      errnop, herrnop, ttlp);
+}
diff --git a/resolv/Versions b/resolv/Versions
index b05778d965..2ae1048908 100644
--- a/resolv/Versions
+++ b/resolv/Versions
@@ -96,7 +96,7 @@ libnss_dns {
     _nss_dns_gethostbyname_r; _nss_dns_getnetbyaddr_r;
     _nss_dns_getnetbyname_r; _nss_dns_getcanonname_r;
     _nss_dns_gethostbyaddr2_r;
-    _nss_dns_gethostbyname4_r;
+    _nss_dns_gethostbyname4_r; _nss_dns_gethostbyname5_r;
   }
 }
 
diff --git a/resolv/nss_dns/dns-host.c b/resolv/nss_dns/dns-host.c
index 7cd54ab504..dddb8f4bef 100644
--- a/resolv/nss_dns/dns-host.c
+++ b/resolv/nss_dns/dns-host.c
@@ -308,7 +308,7 @@ _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
 
 
 enum nss_status
-_nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
+_nss_dns_gethostbyname5_r (const char *name, int af, struct gaih_addrtuple **pat,
 			   char *buffer, size_t buflen, int *errnop,
 			   int *herrnop, int32_t *ttlp)
 {
@@ -344,10 +344,28 @@ _nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
   int nans2p = 0;
   int resplen2 = 0;
   int ans2p_malloced = 0;
+  int type;
 
   int olderr = errno;
   enum nss_status status;
-  int n = __res_context_search (ctx, name, C_IN, T_QUERY_A_AND_AAAA,
+
+  switch (af) {
+  case AF_UNSPEC:
+    type = T_QUERY_A_AND_AAAA;
+    break;
+  case AF_INET:
+    type = T_A;
+    break;
+  case AF_INET6:
+    type = T_AAAA;
+    break;
+  default:
+    *herrnop = NO_DATA;
+    *errnop = EAFNOSUPPORT;
+    return NSS_STATUS_UNAVAIL;
+  }
+
+  int n = __res_context_search (ctx, name, C_IN, type,
 				host_buffer.buf->buf, 2048, &host_buffer.ptr,
 				&ans2p, &nans2p, &resplen2, &ans2p_malloced);
   if (n >= 0)
@@ -397,6 +415,16 @@ _nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
 }
 
 
+enum nss_status
+_nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
+			   char *buffer, size_t buflen, int *errnop,
+			   int *herrnop, int32_t *ttlp)
+{
+  return _nss_dns_gethostbyname5_r (name, AF_UNSPEC, pat, buffer, buflen,
+				    errnop, herrnop,ttlp);
+}
+
+
 extern enum nss_status _nss_dns_gethostbyaddr2_r (const void *addr,
 						  socklen_t len, int af,
 						  struct hostent *result,
