summaryrefslogtreecommitdiffstats
path: root/freebsd/lib/libc/net/getaddrinfo.c
diff options
context:
space:
mode:
Diffstat (limited to 'freebsd/lib/libc/net/getaddrinfo.c')
-rw-r--r--freebsd/lib/libc/net/getaddrinfo.c344
1 files changed, 231 insertions, 113 deletions
diff --git a/freebsd/lib/libc/net/getaddrinfo.c b/freebsd/lib/libc/net/getaddrinfo.c
index 65e905c3..0fe1b5d8 100644
--- a/freebsd/lib/libc/net/getaddrinfo.c
+++ b/freebsd/lib/libc/net/getaddrinfo.c
@@ -32,8 +32,6 @@
*/
/*
- * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
- *
* Issues to be discussed:
* - Return values. There are nonstandard return values defined and used
* in the source code. This is because RFC2553 is silent about which error
@@ -59,7 +57,6 @@
__FBSDID("$FreeBSD$");
#include "namespace.h"
-#include <sys/types.h>
#include <rtems/bsd/sys/param.h>
#include <sys/socket.h>
#include <net/if.h>
@@ -68,7 +65,6 @@ __FBSDID("$FreeBSD$");
#include <ifaddrs.h>
#include <sys/queue.h>
#ifdef INET6
-#include <net/if_var.h>
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <netinet6/in6_var.h>
@@ -98,15 +94,12 @@ __FBSDID("$FreeBSD$");
#include <stdarg.h>
#include <nsswitch.h>
#include "un-namespace.h"
+#include "netdb_private.h"
#include "libc_private.h"
#ifdef NS_CACHING
#include "nscache.h"
#endif
-#if defined(__KAME__) && defined(INET6)
-# define FAITH
-#endif
-
#define ANY 0
#define YES 1
#define NO 0
@@ -146,13 +139,20 @@ static const struct afd {
offsetof(struct sockaddr_in6, sin6_addr),
in6_addrany, in6_loopback, 1},
#define N_INET 1
+#define N_LOCAL 2
#else
#define N_INET 0
+#define N_LOCAL 1
#endif
{PF_INET, sizeof(struct in_addr),
sizeof(struct sockaddr_in),
offsetof(struct sockaddr_in, sin_addr),
in_addrany, in_loopback, 0},
+#define sizeofmember(type, member) (sizeof(((type *)0)->member))
+ {PF_LOCAL, sizeofmember(struct sockaddr_un, sun_path),
+ sizeof(struct sockaddr_un),
+ offsetof(struct sockaddr_un, sun_path),
+ NULL, NULL, 0},
{0, 0, 0, 0, NULL, NULL, 0},
};
@@ -160,30 +160,49 @@ struct explore {
int e_af;
int e_socktype;
int e_protocol;
- const char *e_protostr;
int e_wild;
-#define WILD_AF(ex) ((ex)->e_wild & 0x01)
-#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
-#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
+#define AF_ANY 0x01
+#define SOCKTYPE_ANY 0x02
+#define PROTOCOL_ANY 0x04
+#define WILD_AF(ex) ((ex)->e_wild & AF_ANY)
+#define WILD_SOCKTYPE(ex) ((ex)->e_wild & SOCKTYPE_ANY)
+#define WILD_PROTOCOL(ex) ((ex)->e_wild & PROTOCOL_ANY)
};
static const struct explore explore[] = {
-#if 0
- { PF_LOCAL, ANY, ANY, NULL, 0x01 },
-#endif
#ifdef INET6
- { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
- { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
- { PF_INET6, SOCK_STREAM, IPPROTO_SCTP, "sctp", 0x03 },
- { PF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, "sctp", 0x07 },
- { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
+ { PF_INET6, SOCK_DGRAM, IPPROTO_UDP,
+ AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
+ { PF_INET6, SOCK_STREAM, IPPROTO_TCP,
+ AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
+ { PF_INET6, SOCK_STREAM, IPPROTO_SCTP,
+ AF_ANY | SOCKTYPE_ANY },
+ { PF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP,
+ AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
+ { PF_INET6, SOCK_DGRAM, IPPROTO_UDPLITE,
+ AF_ANY | SOCKTYPE_ANY },
+ { PF_INET6, SOCK_RAW, ANY,
+ AF_ANY | PROTOCOL_ANY },
#endif
- { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
- { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
- { PF_INET, SOCK_STREAM, IPPROTO_SCTP, "sctp", 0x03 },
- { PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP, "sctp", 0x07 },
- { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
- { -1, 0, 0, NULL, 0 },
+ { PF_INET, SOCK_DGRAM, IPPROTO_UDP,
+ AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
+ { PF_INET, SOCK_STREAM, IPPROTO_TCP,
+ AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
+ { PF_INET, SOCK_STREAM, IPPROTO_SCTP,
+ AF_ANY | SOCKTYPE_ANY },
+ { PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP,
+ AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
+ { PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE,
+ AF_ANY | SOCKTYPE_ANY },
+ { PF_INET, SOCK_RAW, ANY,
+ AF_ANY | PROTOCOL_ANY },
+ { PF_LOCAL, SOCK_DGRAM, ANY,
+ AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
+ { PF_LOCAL, SOCK_STREAM, ANY,
+ AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
+ { PF_LOCAL, SOCK_SEQPACKET, ANY,
+ AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
+ { -1, 0, 0, 0 },
};
#ifdef INET6
@@ -380,7 +399,7 @@ getaddrinfo(const char *hostname, const char *servname,
struct addrinfo *pai;
const struct afd *afd;
const struct explore *ex;
- struct addrinfo *afailist[sizeof(afdl)/sizeof(afdl[0])];
+ struct addrinfo *afailist[nitems(afdl)];
struct addrinfo *afai_unspec;
int found;
int numeric = 0;
@@ -416,6 +435,7 @@ getaddrinfo(const char *hostname, const char *servname,
ERR(EAI_BADFLAGS);
switch (hints->ai_family) {
case PF_UNSPEC:
+ case PF_LOCAL:
case PF_INET:
#ifdef INET6
case PF_INET6:
@@ -452,6 +472,24 @@ getaddrinfo(const char *hostname, const char *servname,
}
/*
+ * RFC 3493: AI_ALL and AI_V4MAPPED are effective only against
+ * AF_INET6 query. They need to be ignored if specified in other
+ * occasions.
+ */
+ switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
+ case AI_V4MAPPED:
+ case AI_ALL | AI_V4MAPPED:
+#ifdef INET6
+ if (pai->ai_family != AF_INET6)
+ pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
+ break;
+#endif
+ case AI_ALL:
+ pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
+ break;
+ }
+
+ /*
* check for special cases. (1) numeric servname is disallowed if
* socktype/protocol are left unspecified. (2) servname is disallowed
* for raw and other inet{,6} sockets.
@@ -699,13 +737,13 @@ get_addrselectpolicy(struct policyhead *head)
char *buf;
struct in6_addrpolicy *pol, *ep;
- if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
+ if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0)
return (0);
if (l == 0)
return (0);
if ((buf = malloc(l)) == NULL)
return (0);
- if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
+ if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) {
free(buf);
return (0);
}
@@ -760,10 +798,9 @@ match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
memset(&key, 0, sizeof(key));
key.sin6_family = AF_INET6;
key.sin6_len = sizeof(key);
- key.sin6_addr.s6_addr[10] = 0xff;
- key.sin6_addr.s6_addr[11] = 0xff;
- memcpy(&key.sin6_addr.s6_addr[12],
- &((struct sockaddr_in *)addr)->sin_addr, 4);
+ _map_v4v6_address(
+ (char *)&((struct sockaddr_in *)addr)->sin_addr,
+ (char *)&key.sin6_addr);
break;
default:
return(NULL);
@@ -840,8 +877,19 @@ set_source(struct ai_order *aio, struct policyhead *ph)
get_port(&ai, "1", 0);
/* open a socket to get the source address for the given dst */
- if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0)
+ if ((s = _socket(ai.ai_family, ai.ai_socktype | SOCK_CLOEXEC,
+ ai.ai_protocol)) < 0)
return; /* give up */
+#ifdef INET6
+ if (ai.ai_family == AF_INET6) {
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai.ai_addr;
+ int off = 0;
+
+ if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
+ (void)_setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
+ (char *)&off, sizeof(off));
+ }
+#endif
if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
goto cleanup;
srclen = ai.ai_addrlen;
@@ -1137,11 +1185,14 @@ explore_null(const struct addrinfo *pai, const char *servname,
*res = NULL;
ai = NULL;
+ if (pai->ai_family == PF_LOCAL)
+ return (0);
+
/*
* filter out AFs that are not supported by the kernel
* XXX errno?
*/
- s = _socket(pai->ai_family, SOCK_DGRAM, 0);
+ s = _socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (s < 0) {
if (errno != EMFILE)
return 0;
@@ -1177,10 +1228,13 @@ explore_numeric(const struct addrinfo *pai, const char *hostname,
const char *servname, struct addrinfo **res, const char *canonname)
{
const struct afd *afd;
- struct addrinfo *ai;
+ struct addrinfo *ai, ai0;
int error;
- char pton[PTON_MAX];
+ char pton[PTON_MAX], path[PATH_MAX], *p;
+#ifdef CTASSERT
+ CTASSERT(sizeofmember(struct sockaddr_un, sun_path) <= PATH_MAX);
+#endif
*res = NULL;
ai = NULL;
@@ -1189,6 +1243,15 @@ explore_numeric(const struct addrinfo *pai, const char *hostname,
return 0;
switch (afd->a_af) {
+ case AF_LOCAL:
+ if (hostname[0] != '/')
+ ERR(EAI_NONAME);
+ if (strlen(hostname) > afd->a_addrlen)
+ ERR(EAI_MEMORY);
+ /* NUL-termination does not need to be guaranteed. */
+ strncpy(path, hostname, afd->a_addrlen);
+ p = &path[0];
+ break;
case AF_INET:
/*
* RFC3493 requires getaddrinfo() to accept AF_INET formats
@@ -1199,17 +1262,30 @@ explore_numeric(const struct addrinfo *pai, const char *hostname,
*/
if (inet_aton(hostname, (struct in_addr *)pton) != 1)
return 0;
+ p = pton;
break;
default:
- if (inet_pton(afd->a_af, hostname, pton) != 1)
- return 0;
+ if (inet_pton(afd->a_af, hostname, pton) != 1) {
+ if (pai->ai_family != AF_INET6 ||
+ (pai->ai_flags & AI_V4MAPPED) != AI_V4MAPPED)
+ return 0;
+ if (inet_aton(hostname, (struct in_addr *)pton) != 1)
+ return 0;
+ afd = &afdl[N_INET];
+ ai0 = *pai;
+ ai0.ai_family = AF_INET;
+ pai = &ai0;
+ }
+ p = pton;
break;
}
if (pai->ai_family == afd->a_af) {
- GET_AI(ai, afd, pton);
+ GET_AI(ai, afd, p);
GET_PORT(ai, servname);
- if ((pai->ai_flags & AI_CANONNAME)) {
+ if ((pai->ai_family == AF_INET ||
+ pai->ai_family == AF_INET6) &&
+ (pai->ai_flags & AI_CANONNAME)) {
/*
* Set the numeric address itself as the canonical
* name, based on a clarification in RFC3493.
@@ -1316,45 +1392,13 @@ get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
{
char *p;
struct addrinfo *ai;
-#ifdef FAITH
- struct in6_addr faith_prefix;
- char *fp_str;
- int translate = 0;
-#endif
+#ifdef INET6
+ struct in6_addr mapaddr;
-#ifdef FAITH
- /*
- * Transfrom an IPv4 addr into a special IPv6 addr format for
- * IPv6->IPv4 translation gateway. (only TCP is supported now)
- *
- * +-----------------------------------+------------+
- * | faith prefix part (12 bytes) | embedded |
- * | | IPv4 addr part (4 bytes)
- * +-----------------------------------+------------+
- *
- * faith prefix part is specified as ascii IPv6 addr format
- * in environmental variable GAI.
- * For FAITH to work correctly, routing to faith prefix must be
- * setup toward a machine where a FAITH daemon operates.
- * Also, the machine must enable some mechanizm
- * (e.g. faith interface hack) to divert those packet with
- * faith prefixed destination addr to user-land FAITH daemon.
- */
- fp_str = getenv("GAI");
- if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 &&
- afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) {
- u_int32_t v4a;
- u_int8_t v4a_top;
-
- memcpy(&v4a, addr, sizeof v4a);
- v4a_top = v4a >> IN_CLASSA_NSHIFT;
- if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) &&
- v4a_top != 0 && v4a != IN_LOOPBACKNET) {
- afd = &afdl[N_INET6];
- memcpy(&faith_prefix.s6_addr[12], addr,
- sizeof(struct in_addr));
- translate = 1;
- }
+ if (afd->a_af == AF_INET && (pai->ai_flags & AI_V4MAPPED) != 0) {
+ afd = &afdl[N_INET6];
+ _map_v4v6_address(addr, (char *)&mapaddr);
+ addr = (char *)&mapaddr;
}
#endif
@@ -1368,13 +1412,14 @@ get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
ai->ai_addr->sa_len = afd->a_socklen;
ai->ai_addrlen = afd->a_socklen;
+ if (ai->ai_family == PF_LOCAL) {
+ size_t n = strnlen(addr, afd->a_addrlen);
+
+ ai->ai_addrlen -= afd->a_addrlen - n;
+ ai->ai_addr->sa_len -= afd->a_addrlen - n;
+ }
ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
p = (char *)(void *)(ai->ai_addr);
-#ifdef FAITH
- if (translate == 1)
- memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen);
- else
-#endif
memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
return ai;
}
@@ -1431,6 +1476,9 @@ get_port(struct addrinfo *ai, const char *servname, int matchonly)
if (servname == NULL)
return 0;
switch (ai->ai_family) {
+ case AF_LOCAL:
+ /* AF_LOCAL ignores servname silently. */
+ return (0);
case AF_INET:
#ifdef AF_INET6
case AF_INET6:
@@ -1486,6 +1534,9 @@ get_port(struct addrinfo *ai, const char *servname, int matchonly)
case IPPROTO_SCTP:
proto = "sctp";
break;
+ case IPPROTO_UDPLITE:
+ proto = "udplite";
+ break;
default:
proto = NULL;
break;
@@ -1559,7 +1610,7 @@ addrconfig(struct addrinfo *pai)
if (seen_inet)
continue;
sin = (struct sockaddr_in *)(ifa->ifa_addr);
- if (IN_LOOPBACK(htonl(sin->sin_addr.s_addr)))
+ if (htonl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
continue;
seen_inet = 1;
break;
@@ -1603,7 +1654,7 @@ is_ifdisabled(char *name)
struct in6_ndireq nd;
int fd;
- if ((fd = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
+ if ((fd = _socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
return (-1);
memset(&nd, 0, sizeof(nd));
strlcpy(nd.ifname, name, sizeof(nd.ifname));
@@ -2114,7 +2165,11 @@ getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
return sentinel.ai_next;
}
- RES_SET_H_ERRNO(res, NO_RECOVERY);
+ /*
+ * We could have walked a CNAME chain, but the ultimate target
+ * may not have what we looked for.
+ */
+ RES_SET_H_ERRNO(res, ntohs(hp->ancount) > 0 ? NO_DATA : NO_RECOVERY);
return NULL;
}
@@ -2188,7 +2243,7 @@ addr4sort(struct addrinfo *sentinel, res_state res)
static int
_dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
{
- struct addrinfo *ai;
+ struct addrinfo *ai, ai0;
querybuf *buf, *buf2;
const char *hostname;
const struct addrinfo *pai;
@@ -2196,6 +2251,8 @@ _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
struct res_target q, q2;
res_state res;
+ ai = NULL;
+
hostname = va_arg(ap, char *);
pai = va_arg(ap, const struct addrinfo *);
@@ -2204,6 +2261,8 @@ _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
memset(&sentinel, 0, sizeof(sentinel));
cur = &sentinel;
+ res = __res_state();
+
buf = malloc(sizeof(*buf));
if (!buf) {
RES_SET_H_ERRNO(res, NETDB_INTERNAL);
@@ -2216,6 +2275,13 @@ _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
return NS_NOTFOUND;
}
+ if (pai->ai_family == AF_INET6 &&
+ (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED) {
+ ai0 = *pai;
+ ai0.ai_family = AF_UNSPEC;
+ pai = &ai0;
+ }
+
switch (pai->ai_family) {
case AF_UNSPEC:
q.name = hostname;
@@ -2250,7 +2316,6 @@ _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
return NS_UNAVAIL;
}
- res = __res_state();
if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
RES_SET_H_ERRNO(res, NETDB_INTERNAL);
free(buf);
@@ -2266,20 +2331,24 @@ _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
/* prefer IPv6 */
if (q.next) {
ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res);
- if (ai) {
+ if (ai != NULL) {
cur->ai_next = ai;
while (cur && cur->ai_next)
cur = cur->ai_next;
}
}
- ai = getanswer(buf, q.n, q.name, q.qtype, pai, res);
- if (ai)
- cur->ai_next = ai;
+ if (ai == NULL || pai->ai_family != AF_UNSPEC ||
+ (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) != AI_V4MAPPED) {
+ ai = getanswer(buf, q.n, q.name, q.qtype, pai, res);
+ if (ai != NULL)
+ cur->ai_next = ai;
+ }
free(buf);
free(buf2);
if (sentinel.ai_next == NULL)
switch (res->res_h_errno) {
case HOST_NOT_FOUND:
+ case NO_DATA:
return NS_NOTFOUND;
case TRY_AGAIN:
return NS_TRYAGAIN;
@@ -2294,7 +2363,7 @@ static void
_sethtent(FILE **hostf)
{
if (!*hostf)
- *hostf = fopen(_PATH_HOSTS, "r");
+ *hostf = fopen(_PATH_HOSTS, "re");
else
rewind(*hostf);
}
@@ -2318,7 +2387,7 @@ _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
const char *addr;
char hostbuf[8*1024];
- if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r")))
+ if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re")))
return (NULL);
again:
if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
@@ -2356,6 +2425,9 @@ found:
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_NUMERICHOST;
+ if (pai->ai_family == AF_INET6 &&
+ (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED)
+ hints.ai_flags |= AI_V4MAPPED;
error = getaddrinfo(addr, "0", &hints, &res0);
if (error)
goto again;
@@ -2383,6 +2455,20 @@ found:
return res0;
}
+static struct addrinfo *
+_getht(FILE **hostf, const char *name, const struct addrinfo *pai,
+ struct addrinfo *cur)
+{
+ struct addrinfo *p;
+
+ while ((p = _gethtent(hostf, name, pai)) != NULL) {
+ cur->ai_next = p;
+ while (cur && cur->ai_next)
+ cur = cur->ai_next;
+ }
+ return (cur);
+}
+
/*ARGSUSED*/
static int
_files_getaddrinfo(void *rv, void *cb_data, va_list ap)
@@ -2390,7 +2476,6 @@ _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
const char *name;
const struct addrinfo *pai;
struct addrinfo sentinel, *cur;
- struct addrinfo *p;
FILE *hostf = NULL;
name = va_arg(ap, char *);
@@ -2400,11 +2485,19 @@ _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
cur = &sentinel;
_sethtent(&hostf);
- while ((p = _gethtent(&hostf, name, pai)) != NULL) {
- cur->ai_next = p;
- while (cur && cur->ai_next)
- cur = cur->ai_next;
- }
+ if (pai->ai_family == AF_INET6 &&
+ (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) == AI_V4MAPPED) {
+ struct addrinfo ai0 = *pai;
+
+ ai0.ai_flags &= ~AI_V4MAPPED;
+ cur = _getht(&hostf, name, &ai0, cur);
+ if (sentinel.ai_next == NULL) {
+ _sethtent(&hostf);
+ ai0.ai_flags |= AI_V4MAPPED;
+ cur = _getht(&hostf, name, &ai0, cur);
+ }
+ } else
+ cur = _getht(&hostf, name, pai, cur);
_endhtent(&hostf);
*((struct addrinfo **)rv) = sentinel.ai_next;
@@ -2464,6 +2557,9 @@ nextline:
hints = *pai;
hints.ai_flags = AI_NUMERICHOST;
+ if (pai->ai_family == AF_INET6 &&
+ (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED)
+ hints.ai_flags |= AI_V4MAPPED;
error = getaddrinfo(addr, NULL, &hints, &res0);
if (error == 0) {
for (res = res0; res; res = res->ai_next) {
@@ -2511,15 +2607,46 @@ _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
memset(&sentinel, 0, sizeof(sentinel));
cur = &sentinel;
+ /* ipnodes.byname can hold both IPv4/v6 */
+ r = yp_match(ypdomain, "ipnodes.byname", name,
+ (int)strlen(name), &ypbuf, &ypbuflen);
+ if (r == 0) {
+ ai = _yphostent(ypbuf, pai);
+ if (ai) {
+ cur->ai_next = ai;
+ while (cur && cur->ai_next)
+ cur = cur->ai_next;
+ }
+ free(ypbuf);
+ }
+
+ if (ai != NULL) {
+ struct sockaddr_in6 *sin6;
+
+ switch (ai->ai_family) {
+ case AF_INET:
+ goto done;
+ case AF_INET6:
+ sin6 = (struct sockaddr_in6 *)ai->ai_addr;
+ if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
+ goto done;
+ break;
+ }
+ }
+
/* hosts.byname is only for IPv4 (Solaris8) */
- if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
+ if (pai->ai_family == AF_UNSPEC || pai->ai_family == AF_INET ||
+ ((pai->ai_family == AF_INET6 &&
+ (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED) &&
+ (ai == NULL || (pai->ai_flags & AI_ALL) == AI_ALL))) {
r = yp_match(ypdomain, "hosts.byname", name,
(int)strlen(name), &ypbuf, &ypbuflen);
if (r == 0) {
struct addrinfo ai4;
ai4 = *pai;
- ai4.ai_family = AF_INET;
+ if (pai->ai_family == AF_UNSPEC)
+ ai4.ai_family = AF_INET;
ai = _yphostent(ypbuf, &ai4);
if (ai) {
cur->ai_next = ai;
@@ -2530,16 +2657,7 @@ _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
}
}
- /* ipnodes.byname can hold both IPv4/v6 */
- r = yp_match(ypdomain, "ipnodes.byname", name,
- (int)strlen(name), &ypbuf, &ypbuflen);
- if (r == 0) {
- ai = _yphostent(ypbuf, pai);
- if (ai)
- cur->ai_next = ai;
- free(ypbuf);
- }
-
+done:
if (sentinel.ai_next == NULL) {
RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND);
return NS_NOTFOUND;