⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 host.c

📁 操作系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
	}	n = res_send((char *)&buf, n, (char *)&answer, sizeof(answer));	if (n < 0) {		if (_res.options & RES_DEBUG)			printf("res_send failed\n");		h_errno = TRY_AGAIN;		return (0);	}	eom = (u8_t *)&answer + n;	return(printinfo(&answer, eom, T_ANY, 0));}static intprintinfo(answer, eom, filter, isls)	querybuf_t *answer;	u8_t *eom;        int filter;        int isls;{	dns_hdr_t *hp;	u8_t *bp, *cp;	int n, n1, i, j, nmx, ancount, nscount, arcount, qdcount, buflen;	u_short pref, class;	/*	 * find first satisfactory answer	 */	hp = (dns_hdr_t *) answer;	ancount = ntohs(hp->dh_ancount);	qdcount = ntohs(hp->dh_qdcount);	nscount = ntohs(hp->dh_nscount);	arcount = ntohs(hp->dh_arcount);	if (_res.options & RES_DEBUG || (verbose && isls == 0))		printf("rcode = %d (%s), ancount=%d\n", 		       hp->dh_flag2 & DHF_RCODE,		       DecodeError(hp->dh_flag2 & DHF_RCODE), ancount);	if (hp->dh_flag2 & DHF_RCODE != NOERROR || 		(ancount+nscount+arcount) == 0) {		switch (hp->dh_flag2 & DHF_RCODE) {			case NXDOMAIN:				/* Check if it's an authoritive answer */				if (hp->dh_flag1 & DHF_AA) {					h_errno = HOST_NOT_FOUND;					return(0);				} else {					h_errno = TRY_AGAIN;					return(0);				}			case SERVFAIL:				h_errno = TRY_AGAIN;				return(0);#ifdef OLDJEEVES			/*			 * Jeeves (TOPS-20 server) still does not			 * support MX records.  For the time being,			 * we must accept FORMERRs as the same as			 * NOERROR.			 */			case FORMERR:#endif /* OLDJEEVES */			case NOERROR:/* TpB - set a return error for this case. NO_DATA */				h_errno = NO_DATA;				return(0); /* was 1,but now indicates exception */#ifndef OLDJEEVES			case FORMERR:#endif /* OLDJEEVES */			case NOTIMP:			case REFUSED:				h_errno = NO_RECOVERY;				return(0);		}		return (0);	}	bp = hostbuf;	nmx = 0;	buflen = sizeof(hostbuf);	cp = (u8_t *)answer + sizeof(dns_hdr_t);	if (qdcount) {		cp += dn_skipname((u8_t *)cp,(u8_t *)eom) + QFIXEDSZ;		while (--qdcount > 0)			cp += dn_skipname((u8_t *)cp,(u8_t *)eom) + QFIXEDSZ;	}	if (ancount) {	  if (!(hp->dh_flag1 & DHF_AA))	    if (verbose && isls == 0)	      printf("The following answer is not authoritative:\n");	  while (--ancount >= 0 && cp && cp < eom) {	    cp = pr_rr(cp, (u8_t *)answer, stdout, filter);/* * When we ask for address and there is a CNAME, it seems to return * both the CNAME and the address.  Since we trace down the CNAME * chain ourselves, we don't really want to print the address at * this point. */	    if (cname && ! verbose)	      return (1);	  }	}	if (! verbose)	  return (1);	if (nscount) {	  printf("For authoritative answers, see:\n");	  while (--nscount >= 0 && cp && cp < eom) {	    cp = pr_rr(cp, (u8_t *)answer, stdout, filter);	  }	}	if (arcount) {	  printf("Additional information:\n");	  while (--arcount >= 0 && cp && cp < eom) {	    cp = pr_rr(cp, (u8_t *)answer, stdout, filter);	  }	}	return(1); }static u8_t cnamebuf[MAXDNAME];/* * Print resource record fields in human readable form. */static u8_t *pr_rr(cp, msg, file, filter)	u8_t *cp, *msg;	FILE *file;        int filter;{	int type, class, dlen, n, c, proto, ttl;	ipaddr_t inaddr;	u8_t *cp1;	struct protoent *protop;	struct servent *servp;	char punc;	int doprint;	u8_t name[MAXDNAME];	if ((cp = pr_cdname(cp, msg, name, sizeof(name))) == NULL)		return (NULL);			/* compression error */	type = _getshort(cp);	cp += sizeof(u_short);	class = _getshort(cp);	cp += sizeof(u_short);	ttl = _getlong(cp);	cp += sizeof(u_long);	if (filter == type || filter == T_ANY ||	    (filter == T_A && (type == T_PTR || type == T_NS)))	  doprint = 1;	else	  doprint = 0;	if (doprint)	  if (verbose)	    fprintf(file,"%s\t%d%s\t%s",		    name, ttl, pr_class(class), pr_type(type));	  else	    fprintf(file,"%s%s %s",name, pr_class(class), pr_type(type));	if (verbose)	  punc = '\t';	else	  punc = ' ';	dlen = _getshort(cp);	cp += sizeof(u_short);	cp1 = cp;	/*	 * Print type specific data, if appropriate	 */	switch (type) {	case T_A:		switch (class) {		case C_IN:			bcopy((char *)cp, (char *)&inaddr, sizeof(inaddr));			if (dlen == 4) {			        if (doprint)				  fprintf(file,"%c%s", punc,					inet_ntoa(inaddr));				cp += dlen;			} else if (dlen == 7) {			        if (doprint) {				  fprintf(file,"%c%s", punc,					  inet_ntoa(inaddr));				  fprintf(file,", protocol = %d", cp[4]);				  fprintf(file,", port = %d",					  (cp[5] << 8) + cp[6]);				}				cp += dlen;			}			break;		}		break;	case T_CNAME:		if (dn_expand(msg, msg + 512, cp, cnamebuf, 			      sizeof(cnamebuf)) >= 0)		  cname = cnamebuf;					case T_MB:#ifdef OLDRR	case T_MD:	case T_MF:#endif /* OLDRR */	case T_MG:	case T_MR:	case T_NS:	case T_PTR:		cp = pr_cdname(cp, msg, name, sizeof(name));		if (doprint)		  fprintf(file,"%c%s",punc, name);		break;	case T_HINFO:		if (n = *cp++) {			if (doprint)			  fprintf(file,"%c%.*s", punc, n, cp);			cp += n;		}		if (n = *cp++) {			if (doprint)			  fprintf(file,"%c%.*s", punc, n, cp);			cp += n;		}		break;	case T_SOA:		cp = pr_cdname(cp, msg, name, sizeof(name));		if (doprint)		  fprintf(file,"\t%s", name);		cp = pr_cdname(cp, msg, name, sizeof(name));		if (doprint)		  fprintf(file," %s", name);		if (doprint)		  fprintf(file,"(\n\t\t\t%ld\t;serial (version)", _getlong(cp));		cp += sizeof(u_long);		if (doprint)		  fprintf(file,"\n\t\t\t%ld\t;refresh period", _getlong(cp));		cp += sizeof(u_long);		if (doprint)		  fprintf(file,"\n\t\t\t%ld\t;retry refresh this often", _getlong(cp));		cp += sizeof(u_long);		if (doprint)		  fprintf(file,"\n\t\t\t%ld\t;expiration period", _getlong(cp));		cp += sizeof(u_long);		if (doprint)		  fprintf(file,"\n\t\t\t%ld\t;minimum TTL\n\t\t\t)", _getlong(cp));		cp += sizeof(u_long);		break;	case T_MX:		if (doprint)		  if (verbose)		    fprintf(file,"\t%ld ",_getshort(cp));		  else		    fprintf(file," ");		cp += sizeof(u_short);		cp = pr_cdname(cp, msg, name, sizeof(name));		if (doprint)		  fprintf(file, "%s", name);		break;	case T_MINFO:		cp = pr_cdname(cp, msg, name, sizeof(name));		if (doprint)		  fprintf(file,"%c%s",punc, name);		cp = pr_cdname(cp, msg, name, sizeof(name));		if (doprint)		  fprintf(file," %s", name);		break;		/* Roy start */	case T_TXT:		if (n = *cp++) {			if (doprint)			  fprintf(file,"%c%.*s", punc, n, cp);			cp += n;		}		break;		/* Roy end */	case T_UINFO:		if (doprint)		  fprintf(file,"%c%s", punc, cp);		cp += dlen;		break;	case T_UID:	case T_GID:		if (dlen == 4) {			if (doprint)			  fprintf(file,"%c%ld", punc, _getlong(cp));			cp += sizeof(int);		}		break;	case T_WKS:		if (dlen < sizeof(u_long) + 1)			break;		bcopy((char *)cp, (char *)&inaddr, sizeof(inaddr));		cp += sizeof(u_long);		proto = *cp++;		protop = getprotobynumber(proto);		if (doprint)		  if (protop)		    fprintf(file,"%c%s %s", punc,			    inet_ntoa(inaddr), protop->p_name);		  else		    fprintf(file,"%c%s %d", punc,			    inet_ntoa(inaddr), proto);		n = 0;		while (cp < cp1 + dlen) {			c = *cp++;			do { 				if (c & 0200) {				  servp = NULL;				  if (protop)				    servp = getservbyport (htons(n),							   protop->p_name);				  if (doprint)				    if (servp)				      fprintf(file, " %s", servp->s_name);				    else				      fprintf(file, " %d", n);				} 				c <<= 1;			} while (++n & 07);		}		break;	default:		if (doprint)		  fprintf(file,"%c???", punc);		cp += dlen;	}	if (cp != cp1 + dlen)		fprintf(file,"packet size error (%#x != %#x)\n", cp, cp1+dlen);	if (doprint)	  fprintf(file,"\n");	return (cp);}static	char nbuf[20];/* * Return a string for the type */static char *pr_type(type)	int type;{	switch (type) {	case T_A:		return(verbose? "A" : "has address");	case T_NS:		/* authoritative server */		return("NS");#ifdef OLDRR	case T_MD:		/* mail destination */		return("MD");	case T_MF:		/* mail forwarder */		return("MF");#endif /* OLDRR */	case T_CNAME:		/* connonical name */		return(verbose? "CNAME" : "is a nickname for");	case T_SOA:		/* start of authority zone */		return("SOA");	case T_MB:		/* mailbox domain name */		return("MB");	case T_MG:		/* mail group member */		return("MG");	case T_MX:		/* mail routing info */		return(verbose? "MX" : "mail is handled by");	/* Roy start */	case T_TXT:		/* TXT - descriptive info */		return(verbose? "TXT" : "descriptive text");	/* Roy end */	case T_MR:		/* mail rename name */		return("MR");	case T_NULL:		/* null resource record */		return("NULL");	case T_WKS:		/* well known service */		return("WKS");	case T_PTR:		/* domain name pointer */		return("PTR");	case T_HINFO:		/* host information */		return("HINFO");	case T_MINFO:		/* mailbox information */		return("MINFO");	case T_AXFR:		/* zone transfer */		return("AXFR");	case T_MAILB:		/* mail box */		return("MAILB");	case T_MAILA:		/* mail address */		return("MAILA");	case T_ANY:		/* matches any type */		return("ANY");	case T_UINFO:		return("UINFO");	case T_UID:		return("UID");	case T_GID:		return("GID");	default:		return (sprintf(nbuf, "%d", type) == EOF ? NULL : nbuf);	}}/* * Return a mnemonic for class */static char *pr_class(class)	int class;{	switch (class) {	case C_IN:		/* internet class */		return(verbose? " IN" : "");	case C_HS:		/* internet class */		return(verbose? " HS" : "");	case C_ANY:		/* matches any class */		return(" ANY");	default:		return (sprintf(nbuf," %d", class) == EOF ? NULL : nbuf);	}}static u8_t *pr_cdname(cp, msg, name, namelen)	u8_t *cp, *msg;        u8_t *name;        int namelen;{	int n;	if ((n = dn_expand(msg, msg + 512, cp, name, namelen - 2)) < 0)		return (NULL);	if (name[0] == '\0') {		name[0] = '.';		name[1] = '\0';	}	return (cp + n);}char *resultcodes[] = {	"NOERROR",	"FORMERR",	"SERVFAIL",	"NXDOMAIN",	"NOTIMP",	"REFUSED",	"6",	"7",	"8",	"9",	"10",	"11",	"12",	"13",	"14",	"NOCHANGE",};/* ****************************************************************************** *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -