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

📄 host.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * ++Copyright++ 1986 * - * Copyright (c) 1986 *    The Regents of the University of California.  All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: * 	This product includes software developed by the University of * 	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. *  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * Portions Copyright (c) 1993 by Digital Equipment Corporation. *  * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies, and that * the name of Digital Equipment Corporation not be used in advertising or * publicity pertaining to distribution of the document or software without * specific, written prior permission. *  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS * SOFTWARE. * - * --Copyright-- */#ifndef lintchar copyright[] ="@(#) Copyright (c) 1986 Regents of the University of California.\n\ portions Copyright (c) 1993 Digital Equipment Corporation\n\ All rights reserved.\n";#endif /* not lint *//* * Actually, this program is from Rutgers University, however it is  * based on nslookup and other pieces of named tools, so it needs * that copyright notice. */#ifndef lintstatic char rcsid[] = "$Id: host.c,v 4.9.1.6 1993/12/06 00:43:14 vixie Exp $";#endif /* not lint */#include <sys/types.h>#include <sys/param.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <arpa/nameser.h>#include <stdio.h>#include <netdb.h>#include <resolv.h>#include <ctype.h>#include "../conf/portability.h"extern int h_errno;#define NUMMX 50#define  SUCCESS		0#define  TIME_OUT		-1#define  NO_INFO 		-2#define  ERROR 			-3#define  NONAUTH 		-4#define NAME_LEN 256#ifndef T_TXT#define T_TXT 16#endif#ifndef NO_DATA#define NO_DATA NO_ADDRESS#endif#ifndef C_HS#define C_HS 4#endifint sockFD;FILE *filePtr;char *DecodeError();static struct __res_state orig;extern struct __res_state _res;static u_char *cname = NULL;int getclass = C_IN;int gettype;int verbose = 0;int list = 0;int server_specified = 0;char *pr_class(), *pr_rr(), *pr_cdname(), *pr_type();extern char *hostalias();main(c, v)	char **v;{	unsigned addr;	register struct hostent *hp;	register char *s;	register inverse = 0;	register waitmode = 0;	u_char *oldcname;	int ncnames;	res_init();	_res.retrans = 5;	if (c < 2) {		fprintf(stderr, "Usage: host [-w] [-v] [-r] [-d] [-t querytype] [-c class] [-a] host [server]\n  -w to wait forever until reply\n  -v for verbose output\n  -r to disable recursive processing\n  -d to turn on debugging output\n  -t querytype to look for a specific type of information\n  -c class to look for non-Internet data\n  -a is equivalent to '-v -t *'\n");		exit(1);	}	while (c > 2 && v[1][0] == '-') {		if (strcmp (v[1], "-w") == 0) {			_res.retry = 1;			_res.retrans = 15;			waitmode = 1;			v++;			c--;		}		else if (strcmp (v[1], "-r") == 0) {			_res.options &= ~RES_RECURSE;			v++;			c--;		}		else if (strcmp (v[1], "-d") == 0) {			_res.options |= RES_DEBUG;			v++;			c--;		}		else if (strcmp (v[1], "-v") == 0) {			verbose = 1;			v++;			c--;		}		else if (strcmp (v[1], "-l") == 0) {			list = 1;			v++;			c--;		}		else if (strncmp (v[1], "-t", 2) == 0) {			v++;			c--;			gettype = parsetype(v[1]);			v++;			c--;		}		else if (strncmp (v[1], "-c", 2) == 0) {			v++;			c--;			getclass = parseclass(v[1]);			v++;			c--;		}		else if (strcmp (v[1], "-a") == 0) {			verbose = 1;			gettype = T_ANY;			v++;			c--;		}		        }	if (c > 2) {		s = v[2];		server_specified++;				addr = inet_addr(s);		if (addr == -1) {		  hp = gethostbyname(s);		  if (hp == NULL) {		    fprintf(stderr,"Error in looking up server name:\n");		    hperror(h_errno);		    exit(1);		  }		  _res.nsaddr.sin_addr = *(struct in_addr *)hp->h_addr;		  printf("Using domain server:\n");		  printanswer(hp);		}		else {		  _res.nsaddr.sin_family = AF_INET;		  _res.nsaddr.sin_addr.s_addr = addr;		  _res.nsaddr.sin_port = htons(NAMESERVER_PORT);		  printf("Using domain server %s:\n",			 inet_ntoa(_res.nsaddr.sin_addr));		}		_res.nscount = 1;		_res.retry = 2;	      }	if (strcmp (v[1], ".") == 0)	  addr = -1;	else	  addr = inet_addr(v[1]);	hp = NULL;	h_errno = TRY_AGAIN;/* * we handle default domains ourselves, thank you */	_res.options &= ~RES_DEFNAMES;        if (list)	  exit(ListHosts(v[1], gettype ? gettype : T_A));	oldcname = NULL;	ncnames = 5;	while (hp == NULL && h_errno == TRY_AGAIN) {	  if (addr == -1) {	      cname = NULL;	      if (oldcname == NULL)		hp = (struct hostent *)gethostinfo(v[1]);	      else		hp = (struct hostent *)gethostinfo(oldcname);	      if (cname) {		if (ncnames-- == 0) {		  printf("Too many cnames.  Possible loop.\n");		  exit(1);		}		oldcname = cname;		hp = NULL;		h_errno = TRY_AGAIN;		continue;	      }	  }	  else {	    hp = gethostbyaddr((char*)&addr, 4, AF_INET);	    if (hp)	      printanswer(hp);	  }	  if (!waitmode)	    break;	}	if (hp == NULL) {	  hperror(h_errno);	  exit(1);	}	exit(0);}parsetype(s)	char *s;{if (strcmp(s,"a") == 0)  return(1);if (strcmp(s,"ns") == 0)  return(2);if (strcmp(s,"md") == 0)  return(3);if (strcmp(s,"mf") == 0)  return(4);if (strcmp(s,"cname") == 0)  return(5);if (strcmp(s,"soa") == 0)  return(6);if (strcmp(s,"mb") == 0)  return(7);if (strcmp(s,"mg") == 0)  return(8);if (strcmp(s,"mr") == 0)  return(9);if (strcmp(s,"null") == 0)  return(10);if (strcmp(s,"wks") == 0)  return(11);if (strcmp(s,"ptr") == 0)  return(12);if (strcmp(s,"hinfo") == 0)  return(13);if (strcmp(s,"minfo") == 0)  return(14);if (strcmp(s,"mx") == 0)  return(15);if (strcmp(s,"txt") == 0)	/* Roy */  return(T_TXT);		/* Roy */if (strcmp(s,"afsdb") == 0)  return(T_AFSDB);if (strcmp(s,"uinfo") == 0)  return(100);if (strcmp(s,"uid") == 0)  return(101);if (strcmp(s,"gid") == 0)  return(102);if (strcmp(s,"unspec") == 0)  return(103);if (strcmp(s,"any") == 0)  return(255);if (strcmp(s,"*") == 0)  return(255);if (atoi(s))  return(atoi(s));fprintf(stderr, "Invalid query type: %s\n", s);exit(2);}parseclass(s)	char *s;{if (strcmp(s,"in") == 0)  return(C_IN);if (strcmp(s,"hs") == 0)  return(C_HS);if (strcmp(s,"any") == 0)  return(C_ANY);if (atoi(s))  return(atoi(s));fprintf(stderr, "Invalid query class: %s\n", s);exit(2);}printanswer(hp)	register struct hostent *hp;{	register char **cp;	register long **hptr;	printf("Name: %s\n", hp->h_name);	printf("Address:");	for (hptr = (long **)hp->h_addr_list; *hptr; hptr++)	  printf(" %s", inet_ntoa(*(struct in_addr *)*hptr));	printf("\nAliases:");	for (cp = hp->h_aliases; cp && *cp && **cp; cp++)		printf(" %s", *cp);	printf("\n\n");}hperror(errno) int errno;{switch(errno) {	case HOST_NOT_FOUND:		fprintf(stderr,"Host not found.\n");		break;	case TRY_AGAIN:		fprintf(stderr,"Host not found, try again.\n");		break;	case NO_RECOVERY:		fprintf(stderr,"No recovery, Host not found.\n");		break;	case NO_ADDRESS:		fprintf(stderr,"There is an entry for this host, but it doesn't have ");		switch (gettype) {			case T_A:				fprintf(stderr,"an Internet address.\n");				break;			case T_NS:				fprintf(stderr,"a Name Server.\n");				break;			case T_MD:				fprintf(stderr,"a Mail Destination.\n");				break;			case T_MF:				fprintf(stderr,"a Mail Forwarder.\n");				break;			case T_CNAME:				fprintf(stderr,"a Canonical Name.\n");				break;			case T_SOA:				fprintf(stderr,"a Start of Authority record.\n");				break;			case T_MB:				fprintf(stderr,"a Mailbox Domain Name.\n");				break;			case T_MG:				fprintf(stderr,"a Mail Group Member.\n");				break;			case T_MR:				fprintf(stderr,"a Mail Rename Name.\n");				break;			case T_NULL:				fprintf(stderr,"a Null Resource record.\n");				break;			case T_WKS:				fprintf(stderr,"any Well Known Service information.\n");				break;			case T_PTR:				fprintf(stderr,"a Pointer record.\n");				break;			case T_HINFO:				fprintf(stderr,"any Host Information.\n");				break;			case T_MINFO:				fprintf(stderr,"any Mailbox Information.\n");				break;			case T_MX:				fprintf(stderr,"a Mail Exchanger record.\n");				break;			case T_TXT:				fprintf(stderr,"a Text record.\n");				break;			case T_UINFO:				fprintf(stderr,"any User Information.\n");				break;			case T_UID:				fprintf(stderr,"a User ID.\n");				break;			case T_GID:				fprintf(stderr,"a Group ID.\n");				break;			case T_UNSPEC:				fprintf(stderr,"any Unspecified Format data.\n");				break;			default:				fprintf(stderr,"the information you requested.\n");				break;			}		break;	}}typedef union {	HEADER qb1;	u_char qb2[PACKETSZ];} querybuf;static u_char hostbuf[BUFSIZ+1];gethostinfo(name)	char *name;{	register char *cp, **domain;	int n;	int hp;	int nDomain;	if (strcmp(name, ".") == 0)		return(getdomaininfo(name, NULL));	for (cp = name, n = 0; *cp; cp++)		if (*cp == '.')			n++;	if (n && cp[-1] == '.') {		if (cp[-1] == '.')			cp[-1] = 0;		hp = getdomaininfo(name, (char *)NULL);		if (cp[-1] == 0)			cp[-1] = '.';		return (hp);	}	if (n == 0 && (cp = hostalias(name))) {	        if (verbose)

⌨️ 快捷键说明

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