Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libc/net/getaddrinfo.c,v rcsdiff: /ftp/cvs/cvsroot/src/lib/libc/net/getaddrinfo.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.42.4.5 retrieving revision 1.43 diff -u -p -r1.42.4.5 -r1.43 --- src/lib/libc/net/getaddrinfo.c 2002/09/04 02:30:49 1.42.4.5 +++ src/lib/libc/net/getaddrinfo.c 2000/07/05 12:41:16 1.43 @@ -1,5 +1,5 @@ -/* $NetBSD: getaddrinfo.c,v 1.42.4.5 2002/09/04 02:30:49 itojun Exp $ */ -/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */ +/* $NetBSD: getaddrinfo.c,v 1.43 2000/07/05 12:41:16 itojun Exp $ */ +/* $KAME: getaddrinfo.c,v 1.22 2000/07/05 02:31:36 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -79,7 +79,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: getaddrinfo.c,v 1.42.4.5 2002/09/04 02:30:49 itojun Exp $"); +__RCSID("$NetBSD: getaddrinfo.c,v 1.43 2000/07/05 12:41:16 itojun Exp $"); #endif /* LIBC_SCCS and not lint */ #include "namespace.h" @@ -90,16 +90,15 @@ __RCSID("$NetBSD: getaddrinfo.c,v 1.42.4 #include #include #include -#include -#include -#include #include #include -#include -#include -#include #include +#include +#include +#include #include +#include +#include #include #include @@ -194,7 +193,11 @@ static const ns_src default_dns_files[] { 0 } }; -#define MAXPACKET (64*1024) +#if PACKETSZ > 1024 +#define MAXPACKET PACKETSZ +#else +#define MAXPACKET 1024 +#endif typedef union { HEADER hdr; @@ -230,7 +233,7 @@ static const struct afd *find_afd __P((i static int addrconfig __P((const struct addrinfo *)); #endif #ifdef INET6 -static int ip6_str2scopeid __P((char *, struct sockaddr_in6 *, u_int32_t *)); +static int ip6_str2scopeid __P((char *, struct sockaddr_in6 *)); #endif static struct addrinfo *getanswer __P((const querybuf *, int, const char *, int, @@ -338,17 +341,13 @@ static int str_isnumber(p) const char *p; { - char *ep; - - if (*p == '\0') - return NO; - ep = NULL; - errno = 0; - (void)strtoul(p, &ep, 10); - if (errno == 0 && ep && *ep == '\0') - return YES; - else - return NO; + const char *q = (const char *)p; + while (*q) { + if (!isdigit(*q)) + return NO; + q++; + } + return YES; } int @@ -814,13 +813,13 @@ explore_numeric_scope(pai, hostname, ser error = explore_numeric(pai, addr, servname, res); if (error == 0) { - u_int32_t scopeid; + int scopeid; for (cur = *res; cur; cur = cur->ai_next) { if (cur->ai_family != AF_INET6) continue; sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; - if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { + if ((scopeid = ip6_str2scopeid(scope, sin6)) == -1) { free(hostname2); return(EAI_NODATA); /* XXX: is return OK? */ } @@ -925,10 +924,9 @@ get_port(ai, servname, matchonly) if (str_isnumber(servname)) { if (!allownumeric) return EAI_SERVICE; - port = atoi(servname); + port = htons(atoi(servname)); if (port < 0 || port > 65535) return EAI_SERVICE; - port = htons(port); } else { switch (ai->ai_socktype) { case SOCK_DGRAM: @@ -1005,31 +1003,24 @@ addrconfig(pai) #ifdef INET6 /* convert a string to a scope identifier. XXX: IPv6 specific */ static int -ip6_str2scopeid(scope, sin6, scopeid) +ip6_str2scopeid(scope, sin6) char *scope; struct sockaddr_in6 *sin6; - u_int32_t *scopeid; { - u_long lscopeid; - struct in6_addr *a6; + int scopeid; + struct in6_addr *a6 = &sin6->sin6_addr; char *ep; - a6 = &sin6->sin6_addr; - - /* empty scopeid portion is invalid */ - if (*scope == '\0') - return -1; - if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { /* * We currently assume a one-to-one mapping between links * and interfaces, so we simply use interface indices for * like-local scopes. */ - *scopeid = if_nametoindex(scope); - if (*scopeid == 0) + scopeid = if_nametoindex(scope); + if (scopeid == 0) goto trynumeric; - return 0; + return(scopeid); } /* still unclear about literal, allow numeric only - placeholder */ @@ -1042,11 +1033,9 @@ ip6_str2scopeid(scope, sin6, scopeid) /* try to convert to a numeric id as a last resort */ trynumeric: - errno = 0; - lscopeid = strtoul(scope, &ep, 10); - *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); - if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) - return 0; + scopeid = (int)strtoul(scope, &ep, 10); + if (*ep == '\0') + return scopeid; else return -1; } @@ -1074,8 +1063,8 @@ getanswer(answer, anslen, qname, qtype, const u_char *cp; int n; const u_char *eom; - char *bp, *ep; - int type, class, ancount, qdcount; + char *bp; + int type, class, buflen, ancount, qdcount; int haveanswer, had_error; char tbuf[MAXDNAME]; int (*name_ok) __P((const char *)); @@ -1102,13 +1091,13 @@ getanswer(answer, anslen, qname, qtype, ancount = ntohs(hp->ancount); qdcount = ntohs(hp->qdcount); bp = hostbuf; - ep = hostbuf + sizeof hostbuf; + buflen = sizeof hostbuf; cp = answer->buf + HFIXEDSZ; if (qdcount != 1) { h_errno = NO_RECOVERY; return (NULL); } - n = dn_expand(answer->buf, eom, cp, bp, ep - bp); + n = dn_expand(answer->buf, eom, cp, bp, buflen); if ((n < 0) || !(*name_ok)(bp)) { h_errno = NO_RECOVERY; return (NULL); @@ -1126,13 +1115,14 @@ getanswer(answer, anslen, qname, qtype, } canonname = bp; bp += n; + buflen -= n; /* The qname can be abbreviated, but h_name is now absolute. */ qname = canonname; } haveanswer = 0; had_error = 0; while (ancount-- > 0 && cp < eom && !had_error) { - n = dn_expand(answer->buf, eom, cp, bp, ep - bp); + n = dn_expand(answer->buf, eom, cp, bp, buflen); if ((n < 0) || !(*name_ok)(bp)) { had_error++; continue; @@ -1159,13 +1149,14 @@ getanswer(answer, anslen, qname, qtype, cp += n; /* Get canonical name. */ n = strlen(tbuf) + 1; /* for the \0 */ - if (n > ep - bp || n >= MAXHOSTNAMELEN) { + if (n > buflen || n >= MAXHOSTNAMELEN) { had_error++; continue; } strcpy(bp, tbuf); canonname = bp; bp += n; + buflen -= n; continue; } if (qtype == T_ANY) { @@ -1199,20 +1190,13 @@ getanswer(answer, anslen, qname, qtype, cp += n; continue; } - if (type == T_AAAA) { - struct in6_addr in6; - memcpy(&in6, cp, IN6ADDRSZ); - if (IN6_IS_ADDR_V4MAPPED(&in6)) { - cp += n; - continue; - } - } if (!haveanswer) { int nn; canonname = bp; nn = strlen(bp) + 1; /* for the \0 */ bp += nn; + buflen -= nn; } /* don't overwrite pai */ @@ -1257,7 +1241,7 @@ _dns_getaddrinfo(rv, cb_data, ap) va_list ap; { struct addrinfo *ai; - querybuf *buf, *buf2; + querybuf buf, buf2; const char *name; const struct addrinfo *pai; struct addrinfo sentinel, *cur; @@ -1271,70 +1255,47 @@ _dns_getaddrinfo(rv, cb_data, ap) memset(&sentinel, 0, sizeof(sentinel)); cur = &sentinel; - buf = malloc(sizeof(*buf)); - if (buf == NULL) { - h_errno = NETDB_INTERNAL; - return NS_NOTFOUND; - } - buf2 = malloc(sizeof(*buf2)); - if (buf2 == NULL) { - free(buf); - h_errno = NETDB_INTERNAL; - return NS_NOTFOUND; - } - switch (pai->ai_family) { case AF_UNSPEC: /* prefer IPv6 */ - q.name = name; q.qclass = C_IN; q.qtype = T_AAAA; - q.answer = buf->buf; - q.anslen = sizeof(buf->buf); + q.answer = buf.buf; + q.anslen = sizeof(buf); q.next = &q2; - q2.name = name; q2.qclass = C_IN; q2.qtype = T_A; - q2.answer = buf2->buf; - q2.anslen = sizeof(buf2->buf); + q2.answer = buf2.buf; + q2.anslen = sizeof(buf2); break; case AF_INET: - q.name = name; q.qclass = C_IN; q.qtype = T_A; - q.answer = buf->buf; - q.anslen = sizeof(buf->buf); + q.answer = buf.buf; + q.anslen = sizeof(buf); break; case AF_INET6: - q.name = name; q.qclass = C_IN; q.qtype = T_AAAA; - q.answer = buf->buf; - q.anslen = sizeof(buf->buf); + q.answer = buf.buf; + q.anslen = sizeof(buf); break; default: - free(buf); - free(buf2); return NS_UNAVAIL; } - if (res_searchN(name, &q) < 0) { - free(buf); - free(buf2); + if (res_searchN(name, &q) < 0) return NS_NOTFOUND; - } - ai = getanswer(buf, q.n, q.name, q.qtype, pai); + ai = getanswer(&buf, q.n, q.name, q.qtype, pai); if (ai) { cur->ai_next = ai; while (cur && cur->ai_next) cur = cur->ai_next; } if (q.next) { - ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); + ai = getanswer(&buf2, q2.n, q2.name, q2.qtype, pai); if (ai) cur->ai_next = ai; } - free(buf); - free(buf2); if (sentinel.ai_next == NULL) switch (h_errno) { case HOST_NOT_FOUND: @@ -1351,7 +1312,6 @@ _dns_getaddrinfo(rv, cb_data, ap) static void _sethtent() { - if (!hostf) hostf = fopen(_PATH_HOSTS, "r" ); else @@ -1361,7 +1321,6 @@ _sethtent() static void _endhtent() { - if (hostf) { (void) fclose(hostf); hostf = NULL; @@ -1374,7 +1333,7 @@ _gethtent(name, pai) const struct addrinfo *pai; { char *p; - char *cp, *tname, *cname; + char *cp, *tname; struct addrinfo hints, *res0, *res; int error; const char *addr; @@ -1395,14 +1354,11 @@ _gethtent(name, pai) *cp++ = '\0'; addr = p; /* if this is not something we're looking for, skip it. */ - cname = NULL; while (cp && *cp) { if (*cp == ' ' || *cp == '\t') { cp++; continue; } - if (!cname) - cname = cp; tname = cp; if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; @@ -1422,7 +1378,7 @@ found: res->ai_flags = pai->ai_flags; if (pai->ai_flags & AI_CANONNAME) { - if (get_canonname(pai, res, cname) != 0) { + if (get_canonname(pai, res, name) != 0) { freeaddrinfo(res0); goto again; } @@ -1613,6 +1569,9 @@ _yp_getaddrinfo(rv, cb_data, ap) /* resolver logic */ +extern const char *__hostalias __P((const char *)); +extern int h_errno; + /* * Formulate a normal query, send, and await answer. * Returned answer is placed in supplied buffer "answer". @@ -1687,7 +1646,7 @@ res_queryN(name, target) rcode = hp->rcode; /* record most recent error */ #ifdef DEBUG if (_res.options & RES_DEBUG) - printf(";; rcode = %u, ancount=%u\n", hp->rcode, + printf(";; rcode = %d, ancount=%d\n", hp->rcode, ntohs(hp->ancount)); #endif continue; @@ -1890,7 +1849,7 @@ res_querydomainN(name, domain, target) * copy without '.' if present. */ n = strlen(name); - if (n + 1 > sizeof(nbuf)) { + if (n >= MAXDNAME) { h_errno = NO_RECOVERY; return (-1); } @@ -1902,11 +1861,11 @@ res_querydomainN(name, domain, target) } else { n = strlen(name); d = strlen(domain); - if (n + 1 + d + 1 > sizeof(nbuf)) { + if (n + d + 1 >= MAXDNAME) { h_errno = NO_RECOVERY; return (-1); } - snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); + sprintf(nbuf, "%s.%s", name, domain); } return (res_queryN(longname, target)); }