int
getrrtype(host, hbsize, rrtype, cnamelevel)
	char *host;
	int hbsize;
	u_short rrtype;
	int cnamelevel;
{

	HEADER *hp;
	msgdata *eom, *cp;
	querybuf buf, answer;
	int qlen, n, ancount, qdcount, ok;
	u_short type;
	msgdata nbuf[BUFSIZ];
	int first;

	qlen = res_mkquery(QUERY, host, C_IN, rrtype, (char *)NULL, 0, NULL,
			   (char *)&buf, sizeof(buf));
	if (qlen < 0) {
	  fprintf(stderr, "res_mkquery failed\n");
	  h_errno = NO_RECOVERY;
	  return -2;
	}
	n = res_send((char *)&buf, qlen, (char *)&answer, sizeof(answer));
	if (n < 0) {
	  h_errno = TRY_AGAIN;
	  return -1;
	}
	eom = (msgdata *)&answer + n;
	/*
	 * find first satisfactory answer
	 */
	hp = (HEADER *) &answer;
	ancount = ntohs(hp->ancount);
	qdcount = ntohs(hp->qdcount);
	h_errno = 0;
	/*
	 * We don't care about errors here, only if we got an answer
	 */
	if (ancount == 0) {
	  if (rrtype == T_CNAME && hp->rcode == NOERROR) {
	    if (qdcount > 0 && strchr(host, '.') == NULL) {
	      cp = (msgdata *)&answer + sizeof(HEADER);
	      if (dn_expand((msgdata *)&answer,
			    eom, cp, (u_char*)host, hbsize) >= 0) {
		if (host[0] == '\0') {
		  host[0] = '.';
		  host[1] = '\0';
		}
	      }
	    }
	    return 1;
	  }
	  return 0;
	}
	cp = (msgdata *)&answer + sizeof(HEADER);
	for (; qdcount > 0; --qdcount) {
#if	defined(BIND_VER) && (BIND_VER >= 473)
	  cp += dn_skipname(cp, eom) + QFIXEDSZ;
#else	/* !defined(BIND_VER) || (BIND_VER < 473) */
	  cp += dn_skip(cp) + QFIXEDSZ;
#endif	/* defined(BIND_VER) && (BIND_VER >= 473) */
	}
	first = 1;
	ok = rrtype != T_WKS;
	while (--ancount >= 0 && cp < eom) {
	  if ((n = dn_expand((msgdata *)&answer, eom, cp, nbuf,
			     sizeof(nbuf))) < 0)
	    break;
	  if (first) {
	    strncpy(host, nbuf, hbsize);
	    host[hbsize - 1] = '\0';
	    first = 0;
	  }
	  cp += n;
	  type = _getshort(cp);
	  cp += sizeof(u_short); /* type */
	  cp += sizeof(u_short); /* class */
	  cp += sizeof(u_long);	/* ttl */
	  n = _getshort(cp);
	  cp += sizeof(u_short); /* dlen */
	  if (type != rrtype) {
	    cp += n;
	    continue;
	  }
	  /*
	   * Assume that only one rrtype will be found.  More
	   * than one is undefined.
	   */
	  if (type == T_WKS) {
	    msgdata *nextcp = cp + n;
	    /* If we have seen a WKS, it had better have SMTP,
	     * however in absence of a WKS, assume SMTP.
	     */
	    if (n < sizeof (u_long) + 1) {
	      cp = nextcp;	/* ? */
	      continue;
	    }
	    ok = 0;
	    cp += sizeof(u_long); /* skip IP address */
	    if (*cp++ == IPPROTO_TCP) {	/* check protocol */
	      if (cp + (IPPORT_SMTP/8) < nextcp
		  && (*(cp+(IPPORT_SMTP/8))
		      & (0x80>>IPPORT_SMTP%8)))
		return 1;
	    }
	    cp = nextcp;
	    continue;
	  } else {
	    /* Special processing on T_CNAME ??? */
	    if ((n = dn_expand((msgdata *)&answer, eom, cp, nbuf,
			       sizeof(nbuf))) < 0)
	      break;
	    strncpy(host, nbuf, hbsize);
	    host[hbsize - 1] = '\0';
	  }
	  return ok;
	}
	return 0;
}
