From e84892eb74b92e04c93beb0755687760351864b8 Mon Sep 17 00:00:00 2001 From: Hajimu UMEMOTO Date: Sat, 30 Apr 2005 20:07:01 +0000 Subject: [PATCH] oops, gethostbyaddr(3) must return h_addr as an IPv4-mapped IPv6 address when RES_USE_INET6 was set, according to RFC 2133 section 6.2. --- lib/libc/net/gethostbyht.c | 9 ++++++++- lib/libc/net/gethostbynis.c | 9 ++++----- lib/libc/net/gethostnamadr.c | 5 +++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/libc/net/gethostbyht.c b/lib/libc/net/gethostbyht.c index 452a6da1d032..d1ef23bc6477 100644 --- a/lib/libc/net/gethostbyht.c +++ b/lib/libc/net/gethostbyht.c @@ -246,8 +246,15 @@ _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap) sethostent_r(0, hed); while ((error = gethostent_p(he, hed, 0)) == 0) - if (he->h_addrtype == af && !bcmp(he->h_addr, addr, len)) + if (he->h_addrtype == af && !bcmp(he->h_addr, addr, len)) { + if (he->h_addrtype == AF_INET && + _res.options & RES_USE_INET6) { + _map_v4v6_address(he->h_addr, he->h_addr); + he->h_length = IN6ADDRSZ; + he->h_addrtype = AF_INET6; + } break; + } endhostent_r(hed); return (error == 0) ? NS_SUCCESS : NS_NOTFOUND; diff --git a/lib/libc/net/gethostbynis.c b/lib/libc/net/gethostbynis.c index 45aed338ef4f..ca1a9a56ad87 100644 --- a/lib/libc/net/gethostbynis.c +++ b/lib/libc/net/gethostbynis.c @@ -50,7 +50,7 @@ __FBSDID("$FreeBSD$"); #ifdef YP static int _gethostbynis(const char *name, char *map, int af, struct hostent *he, - struct hostent_data *hed, int mapped) + struct hostent_data *hed) { char *p, *bp, *ep; char *cp, **q; @@ -101,7 +101,7 @@ _gethostbynis(const char *name, char *map, int af, struct hostent *he, addrok = inet_aton(result, (struct in_addr *)hed->host_addr); if (addrok != 1) break; - if (mapped) { + if (_res.options & RES_USE_INET6) { _map_v4v6_address((char *)hed->host_addr, (char *)hed->host_addr); af = AF_INET6; @@ -172,8 +172,7 @@ _gethostbynisname_r(const char *name, int af, struct hostent *he, map = "ipnodes.byname"; break; } - return _gethostbynis(name, map, af, he, hed, - _res.options & RES_USE_INET6); + return _gethostbynis(name, map, af, he, hed); } static int @@ -193,7 +192,7 @@ _gethostbynisaddr_r(const char *addr, int len, int af, struct hostent *he, } if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL) return -1; - return _gethostbynis(numaddr, map, af, he, hed, 0); + return _gethostbynis(numaddr, map, af, he, hed); } #endif /* YP */ diff --git a/lib/libc/net/gethostnamadr.c b/lib/libc/net/gethostnamadr.c index 8aae1922458d..0646b0cb4b4b 100644 --- a/lib/libc/net/gethostnamadr.c +++ b/lib/libc/net/gethostnamadr.c @@ -264,6 +264,11 @@ gethostbyaddr_r(const char *addr, int len, int af, struct hostent *he, { 0 } }; + if ((_res.options & RES_INIT) == 0 && res_init() == -1) { + h_errno = NETDB_INTERNAL; + return -1; + } + if (af == AF_INET6 && len == IN6ADDRSZ) { addr6 = (const struct in6_addr *)(const void *)uaddr; if (IN6_IS_ADDR_LINKLOCAL(addr6)) {