+ hap = h_addr_ptrs;
+ *hap = NULL;
+#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
+ host.h_addr_list = h_addr_ptrs;
+#endif
+ haveanswer = 0;
+ while (--ancount >= 0 && cp < eom) {
+ if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom,
+ (u_char *)cp, (u_char *)bp, buflen)) < 0)
+ break;
+ cp += n;
+ type = _getshort(cp);
+ cp += sizeof(u_short);
+ class = _getshort(cp);
+ cp += sizeof(u_short) + sizeof(u_long);
+ n = _getshort(cp);
+ cp += sizeof(u_short);
+ if (type == T_CNAME) {
+ cp += n;
+ if (ap >= &host_aliases[MAXALIASES-1])
+ continue;
+ *ap++ = bp;
+ n = strlen(bp) + 1;
+ bp += n;
+ buflen -= n;
+ continue;
+ }
+ if (iquery && type == T_PTR) {
+ if ((n = dn_expand((u_char *)answer->buf,
+ (u_char *)eom, (u_char *)cp, (u_char *)bp,
+ buflen)) < 0) {
+ cp += n;
+ continue;
+ }
+ cp += n;
+ host.h_name = bp;
+ return(&host);
+ }
+ if (iquery || type != T_A) {
+#ifdef DEBUG
+ if (_res.options & RES_DEBUG)
+ printf("unexpected answer type %d, size %d\n",
+ type, n);
+#endif
+ cp += n;
+ continue;
+ }
+ if (haveanswer) {
+ if (n != host.h_length) {
+ cp += n;
+ continue;
+ }
+ if (class != getclass) {
+ cp += n;
+ continue;
+ }
+ } else {
+ host.h_length = n;
+ getclass = class;
+ host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
+ if (!iquery) {
+ host.h_name = bp;
+ bp += strlen(bp) + 1;
+ }
+ }
+
+ bp += sizeof(align) - ((u_long)bp % sizeof(align));
+
+ if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
+#ifdef DEBUG
+ if (_res.options & RES_DEBUG)
+ printf("size (%d) too big\n", n);
+#endif
+ break;
+ }
+ bcopy(cp, *hap++ = bp, n);
+ bp +=n;
+ cp += n;
+ haveanswer++;
+ }
+ if (haveanswer) {
+ *ap = NULL;
+#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
+ *hap = NULL;
+#else
+ host.h_addr = h_addr_ptrs[0];
+#endif
+ return (&host);
+ } else {
+ h_errno = TRY_AGAIN;
+ return ((struct hostent *) NULL);
+ }