cp = answer->buf + HFIXEDSZ;
cp = answer->buf; BOUNDED_INCR(HFIXEDSZ);
getanswer(answer, anslen, qname, qtype)	const querybuf *answer;	int anslen;	const char *qname;	int qtype;{	register const HEADER *hp;	register const u_char *cp;	register int n;	const u_char *eom;	char *bp, **ap, **hap;	int type, class, buflen, ancount, qdcount;	int haveanswer, had_error;	int toobig = 0;	char tbuf[MAXDNAME];	const char *tname;	int (*name_ok) __P((const char *));	tname = qname;	host.h_name = NULL;	eom = answer->buf + anslen;	switch (qtype) {	case T_A:	case T_AAAA:		name_ok = res_hnok;		break;	case T_PTR:		name_ok = res_dnok;		break;	default:		return (NULL);	/* XXX should be abort(); */	}	/*	 * find first satisfactory answer	 */	hp = &answer->hdr;	ancount = ntohs(hp->ancount);	qdcount = ntohs(hp->qdcount);	bp = hostbuf;	buflen = sizeof hostbuf;	cp = answer->buf + HFIXEDSZ;	if (qdcount != 1) {		__set_h_errno (NO_RECOVERY);		return (NULL);	}	n = dn_expand(answer->buf, eom, cp, bp, buflen);	if ((n < 0) || !(*name_ok)(bp)) {		__set_h_errno (NO_RECOVERY);		return (NULL);	}	cp += n + QFIXEDSZ;	if (qtype == T_A || qtype == T_AAAA) {		/* res_send() has already verified that the query name is the		 * same as the one we sent; this just gets the expanded name		 * (i.e., with the succeeding search-domain tacked on).		 */		n = strlen(bp) + 1;		/* for the \0 */		if (n >= MAXHOSTNAMELEN) {			__set_h_errno (NO_RECOVERY);			return (NULL);		}		host.h_name = bp;		bp += n;		buflen -= n;		/* The qname can be abbreviated, but h_name is now absolute. */		qname = host.h_name;	}	ap = host_aliases;	*ap = NULL;	host.h_aliases = host_aliases;	hap = h_addr_ptrs;	*hap = NULL;	host.h_addr_list = h_addr_ptrs;	haveanswer = 0;	had_error = 0;	while (ancount-- > 0 && cp < eom && !had_error) {		n = dn_expand(answer->buf, eom, cp, bp, buflen);		if ((n < 0) || !(*name_ok)(bp)) {			had_error++;			continue;		}		cp += n;			/* name */		type = _getshort(cp); 		cp += INT16SZ;			/* type */		class = _getshort(cp); 		cp += INT16SZ + INT32SZ;	/* class, TTL */		n = _getshort(cp);		cp += INT16SZ;			/* len */		if (class != C_IN) {			/* XXX - debug? syslog? */			cp += n;			continue;		/* XXX - had_error++ ? */		}		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {			if (ap >= &host_aliases[MAXALIASES-1])				continue;			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);			if ((n < 0) || !(*name_ok)(tbuf)) {				had_error++;				continue;			}			cp += n;			/* Store alias. */			*ap++ = bp;			n = strlen(bp) + 1;	/* for the \0 */			if (n >= MAXHOSTNAMELEN) {				had_error++;				continue;			}			bp += n;			buflen -= n;			/* Get canonical name. */			n = strlen(tbuf) + 1;	/* for the \0 */			if (n > buflen || n >= MAXHOSTNAMELEN) {				had_error++;				continue;			}			strcpy(bp, tbuf);			host.h_name = bp;			bp += n;			buflen -= n;			continue;		}		if (qtype == T_PTR && type == T_CNAME) {			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);			if (n < 0 || !res_dnok(tbuf)) {				had_error++;				continue;			}			cp += n;			/* Get canonical name. */			n = strlen(tbuf) + 1;	/* for the \0 */			if (n > buflen || n >= MAXHOSTNAMELEN) {				had_error++;				continue;			}			strcpy(bp, tbuf);			tname = bp;			bp += n;			buflen -= n;			continue;		}		if (type != qtype) {			syslog(LOG_NOTICE|LOG_AUTH,	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",			       qname, p_class(C_IN), p_type(qtype),			       p_type(type));			cp += n;			continue;		/* XXX - had_error++ ? */		}		switch (type) {		case T_PTR:			if (strcasecmp(tname, bp) != 0) {				syslog(LOG_NOTICE|LOG_AUTH,				       AskedForGot, qname, bp);				cp += n;				continue;	/* XXX - had_error++ ? */			}			n = dn_expand(answer->buf, eom, cp, bp, buflen);			if ((n < 0) || !res_hnok(bp)) {				had_error++;				break;			}#if MULTI_PTRS_ARE_ALIASES			cp += n;			if (!haveanswer)				host.h_name = bp;			else if (ap < &host_aliases[MAXALIASES-1])				*ap++ = bp;			else				n = -1;			if (n != -1) {				n = strlen(bp) + 1;	/* for the \0 */				if (n >= MAXHOSTNAMELEN) {					had_error++;					break;				}				bp += n;				buflen -= n;			}			break;#else			host.h_name = bp;			if (_res.options & RES_USE_INET6) {				n = strlen(bp) + 1;	/* for the \0 */				if (n >= MAXHOSTNAMELEN) {					had_error++;					break;				}				bp += n;				buflen -= n;				map_v4v6_hostent(&host, &bp, &buflen);			}			__set_h_errno (NETDB_SUCCESS);			return (&host);#endif		case T_A:		case T_AAAA:			if (strcasecmp(host.h_name, bp) != 0) {				syslog(LOG_NOTICE|LOG_AUTH,				       AskedForGot, host.h_name, bp);				cp += n;				continue;	/* XXX - had_error++ ? */			}			if (n != host.h_length) {				cp += n;				continue;			}			if (!haveanswer) {				register int nn;				host.h_name = bp;				nn = strlen(bp) + 1;	/* for the \0 */				bp += nn;				buflen -= nn;			}			bp += sizeof(align) - ((u_long)bp % sizeof(align));			if (bp + n >= &hostbuf[sizeof hostbuf]) {				dprintf("size (%d) too big\n", n);				had_error++;				continue;			}			if (hap >= &h_addr_ptrs[MAXADDRS-1]) {				if (!toobig++) {					dprintf("Too many addresses (%d)\n",						MAXADDRS);				}				cp += n;				continue;			}			bcopy(cp, *hap++ = bp, n);			bp += n;			buflen -= n;			cp += n;			break;		default:			abort();		}		if (!had_error)			haveanswer++;	}	if (haveanswer) {		*ap = NULL;		*hap = NULL;# if defined(RESOLVSORT)		/*		 * Note: we sort even if host can take only one address		 * in its return structures - should give it the "best"		 * address in that case, not some random one		 */		if (_res.nsort && haveanswer > 1 && qtype == T_A)			addrsort(h_addr_ptrs, haveanswer);# endif /*RESOLVSORT*/		if (!host.h_name) {			n = strlen(qname) + 1;	/* for the \0 */			if (n > buflen || n >= MAXHOSTNAMELEN)				goto no_recovery;			strcpy(bp, qname);			host.h_name = bp;			bp += n;			buflen -= n;		}		if (_res.options & RES_USE_INET6)			map_v4v6_hostent(&host, &bp, &buflen);		__set_h_errno (NETDB_SUCCESS);		return (&host);	} no_recovery:	__set_h_errno (NO_RECOVERY);	return (NULL);}