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

📄 dig.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
			} else if (strncmp(*argv,"-st",3) == 0) {				sticky++;				continue;			} else if (strncmp(*argv,"-envsa",6) == 0) {				envsave++;				continue;			} else if (strncmp(*argv,"-envse",6) == 0) {				envset++;				continue;			}			if (**argv == '-') {				switch (argv[0][1]) { 				case 'T': wait = atoi(*++argv);					break;				case 'c': 					if ((tmp = atoi(*++argv))					    || *argv[0]=='0') {						qclass = tmp;					} else if (tmp = StringToClass(*argv,								       0, NULL)						   ) {						qclass = tmp;					} else {						printf(						  "; invalid class specified\n"						       );					}					break;				case 't': 					if ((tmp = atoi(*++argv))					    || *argv[0]=='0') {						qtype = tmp;					} else if (tmp = StringToClass(*argv,								       0, NULL)						   ) {						qtype = tmp;					} else {						printf(						   "; invalid type specified\n"						       );						}					break;				case 'x':					if (qtype == T_A)						qtype = T_ANY;					if (!(addrc = *++argv)) {						printf(						       "; no arg for -x?\n"						       );						break;					}					addri = addrj = 0;					while (*addrc) {					    if (*addrc == '.') {						revaddr[addri][addrj++] = '.';						revaddr[addri][addrj] =(char)0;						addri++; addrj=0;					    } else {						revaddr[addri][addrj++] =							*addrc;					    }					    addrc++;					}					if (*(addrc-1) == '.') {						addri--;					} else {						revaddr[addri][addrj++] = '.';						revaddr[addri][addrj] =(char)0;					}					*domain = (char) 0;					for (addrj=addri; addrj>=0; addrj--)						strcat(domain,revaddr[addrj]);					strcat(domain,"in-addr.arpa.");					break;				case 'p': port = htons(atoi(*++argv)); break;				case 'P':					if (argv[0][2] != '\0')						strcpy(pingstr,&argv[0][2]);					else						strcpy(pingstr,"ping -s");					break;#if defined(__RES) && (__RES >= 19931104)				case 'n':					_res.ndots = atoi(&argv[0][2]);					break;#endif /*__RES*/				} /* switch - */				continue;			} /* if '-'   */			if ((tmp = StringToType(*argv, -1, NULL)) != -1) { 				if ((T_ANY == tmp) && anyflag++) {  					qclass = C_ANY; 						continue; 				}				if (T_AXFR == tmp) {					_res.pfcode = PRF_ZONE;					zone++;				} else {					qtype = tmp; 				}			} else if ((tmp = StringToClass(*argv, -1, NULL))				   != -1) { 				qclass = tmp; 			} else {				bzero(domain, (sizeof domain));				sprintf(domain,"%s",*argv);			}		} /* while argv remains */		if (_res.pfcode & 0x80000)			printf("; pfcode: %08x, options: %08x\n",			       _res.pfcode, _res.options);	  /* * Current env. (after this parse) is to become the * new "working environmnet. Used in conj. with sticky. */		if (envset) {			res_x = _res;			envset = 0;		}/* * Current env. (after this parse) is to become the * new default saved environmnet. Save in user specified * file if exists else is SAVEENV (== "DiG.env"). */		if (envsave) {			afile = (char *) getenv("LOCALDEF");			if ((afile &&			     ((fp = open(afile,					 O_WRONLY|O_CREAT|O_TRUNC,					 S_IREAD|S_IWRITE)) > 0))			    ||			    ((fp = open(SAVEENV,					O_WRONLY|O_CREAT|O_TRUNC,					S_IREAD|S_IWRITE)) > 0)) {				write(fp, &_res, (sizeof _res));				close(fp);			}			envsave = 0;		}		if (_res.pfcode & RES_PRF_CMD)			printf("%s\n", cmd);		addrflag = anyflag = 0;/* * Find address of server to query. If not dot-notation, then * try to resolve domain-name (if so, save and turn off print  * options, this domain-query is not the one we want. Restore * user options when done. * Things get a bit wierd since we need to use resolver to be * able to "put the resolver to work". */		srvbuf[0] = 0;		srvmsg = defsrv;		if (srv != NULL) {			struct in_addr addr;			if (inet_aton(srv, &addr)) {				_res.nscount = 1;				_res.nsaddr.sin_addr = addr;				srvmsg = strcat(srvbuf, srv);			} else {				res_t = _res;				_res.pfcode = 0;				_res.options = RES_DEFAULT;				res_init();				hp = gethostbyname(srv);				_res = res_t;				if (hp == NULL				    || hp->h_addr_list == NULL				    || *hp->h_addr_list == NULL) {					fflush(stdout);					fprintf(stderr,		"; Bad server: %s -- using default server and timer opts\n",						srv);					fflush(stderr);					srvmsg = defsrv;					srv = NULL;				} else {					u_int32_t **addr;					_res.nscount = 0;					for (addr = (u_int32_t**)hp->h_addr_list;					     *addr && (_res.nscount < MAXNS);					     addr++) {						_res.nsaddr_list[							_res.nscount++						].sin_addr.s_addr = **addr;					}					srvmsg = strcat(srvbuf,srv);					strcat(srvbuf, "  ");					strcat(srvmsg,					       inet_ntoa(_res.nsaddr.sin_addr)					       );				}			}			printf("; (%d server%s found)\n",			       _res.nscount, (_res.nscount==1)?"":"s");			_res.id += _res.retry;		}		{			int i;			for (i = 0;  i < _res.nscount;  i++) {				_res.nsaddr_list[i].sin_family = AF_INET;				_res.nsaddr_list[i].sin_port = port;			}			_res.id += _res.retry;		}		if (zone) {			int i;			for (i = 0;  i < _res.nscount;  i++) {				int x = printZone(domain,						  &_res.nsaddr_list[i]);				if (_res.pfcode & RES_PRF_STATS) {					struct timeval exectime;					gettimeofday(&exectime,NULL);					printf(";; FROM: %s to SERVER: %s\n",					       myhostname,					       inet_ntoa(_res.nsaddr_list[i]							 .sin_addr));					printf(";; WHEN: %s",					       ctime(&(exectime.tv_sec)));				}				if (!x)					break;	/* success */			}			fflush(stdout);			continue;		}		bytes_out = n = res_mkquery(QUERY, domain, qclass, qtype,					    (char *)0, 0, NULL,					    packet, sizeof(packet));		if (n < 0) {			fflush(stderr);			printf(";; res_mkquery: buffer too small\n\n");			continue;		}		eecode = 0;		__fp_resstat(NULL, stdout);		if ((bytes_in = n = res_send(packet, n,					     answer, sizeof(answer))) < 0) {			fflush(stdout);			n = 0 - n;			msg[0]=0;			strcat(msg,";; res_send to server ");			strcat(msg,srvmsg);			perror(msg);			fflush(stderr);			if (!dofile) {				if (eecode)					exit(eecode);				else					exit(9);			}		}		if (_res.pfcode & RES_PRF_STATS) {			gettimeofday(&exectime,NULL);			printf(";; FROM: %s to SERVER: %s\n",			       myhostname, srvmsg);			printf(";; WHEN: %s",			       ctime(&(exectime.tv_sec)));			printf(";; MSG SIZE  sent: %d  rcvd: %d\n",			       bytes_out, bytes_in);		}	  		fflush(stdout);/* *   Argh ... not particularly elegant. Should put in *real* ping code. *   Would necessitate root priviledges for icmp port though! */		if (*pingstr) {			sprintf(doping,"%s %s 56 3 | tail -3",pingstr,				(srv==NULL)?(defsrv+10):srv);			system(doping);		}		putchar('\n');/* * Fairly crude method and low overhead method of keeping two * batches started at different sites somewhat synchronized. */		gettimeofday(&tv2, NULL);		delay = (int)(tv2.tv_sec - tv1.tv_sec);		if (delay < wait) {			sleep(wait - delay);		}	}	return(eecode);}static voidUsage(){	fputs("\usage:  dig [@server] domain [q-type] [q-class] {q-opt} {d-opt} [%comment]\n\where:	server,\n\	domain	are names in the Domain Name System\n\	q-class	is one of (in,any,...) [default: in]\n\	q-type	is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default: a]\n\", stderr);	fputs("\	q-opt	is one of:\n\		-x dot-notation-address	(shortcut to in-addr.arpa lookups)\n\		-f file			(batch mode input file name)\n\		-T time			(batch mode time delay, per query)\n\		-p port			(nameserver is on this port) [53]\n\		-Pping-string		(see man page)\n\		-t query-type		(synonym for q-type)\n\		-c query-class		(synonym for q-class)\n\		-envsav,-envset		(see man page)\n\		-[no]stick		(see man page)\n\", stderr);	fputs("\	d-opt	is of the form ``+keyword=value'' where keyword is one of:\n\		[no]debug [no]d2 [no]recurse retry=# time=# [no]ko [no]vc\n\		[no]defname [no]search domain=NAME [no]ignore [no]primary\n\		[no]aaonly [no]sort [no]cmd [no]stats [no]Header [no]header\n\		[no]ttlid [no]cl [no]qr [no]reply [no]ques [no]answer\n\		[no]author [no]addit pfdef pfmin pfset=# pfand=# pfor=#\n\", stderr);	fputs("\notes:	defname and search don't work; use fully-qualified names.\n\", stderr);}static intSetOption(string)    char *string;{    char 	option[NAME_LEN];    char 	type[NAME_LEN];    char 	*ptr;    int 	i;    i = sscanf(string, " %s", option);    if (i != 1) {	fprintf(stderr, ";*** Invalid option: %s\n",  option);	return(ERROR);    }        if (strncmp(option, "aa", 2) == 0) {	/* aaonly */	    _res.options |= RES_AAONLY;	} else if (strncmp(option, "noaa", 4) == 0) {	    _res.options &= ~RES_AAONLY;	} else if (strncmp(option, "deb", 3) == 0) {	/* debug */	    _res.options |= RES_DEBUG;	} else if (strncmp(option, "nodeb", 5) == 0) {	    _res.options &= ~(RES_DEBUG | RES_DEBUG2);	} else if (strncmp(option, "ko", 2) == 0) {	/* keepopen */	    _res.options |= (RES_STAYOPEN | RES_USEVC);	} else if (strncmp(option, "noko", 4) == 0) {	    _res.options &= ~RES_STAYOPEN;	} else if (strncmp(option, "d2", 2) == 0) {	/* d2 (more debug) */	    _res.options |= (RES_DEBUG | RES_DEBUG2);	} else if (strncmp(option, "nod2", 4) == 0) {	    _res.options &= ~RES_DEBUG2;	} else if (strncmp(option, "def", 3) == 0) {	/* defname */	    _res.options |= RES_DEFNAMES;	} else if (strncmp(option, "nodef", 5) == 0) {	    _res.options &= ~RES_DEFNAMES;	} else if (strncmp(option, "sea", 3) == 0) {	/* search list */	    _res.options |= RES_DNSRCH;	} else if (strncmp(option, "nosea", 5) == 0) {	    _res.options &= ~RES_DNSRCH;	} else if (strncmp(option, "do", 2) == 0) {	/* domain */	    ptr = strchr(option, '=');	    if (ptr != NULL) {		sscanf(++ptr, "%s", _res.defdname);	    }	  } else if (strncmp(option, "ti", 2) == 0) {      /* timeout */	    ptr = strchr(option, '=');	    if (ptr != NULL) {

⌨️ 快捷键说明

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