dig.c

来自「非常好的dns解析软件」· C语言 代码 · 共 1,806 行 · 第 1/4 页

C
1,806
字号
	result = ISC_R_SUCCESS;	result = isc_buffer_allocate(mctx, &buf, len);	check_result(result, "isc_buffer_allocate");	if (query->lookup->comments && !short_form) {		if (query->lookup->cmdline[0] != 0)			printf("; %s\n", query->lookup->cmdline);		if (msg == query->lookup->sendmsg)			printf(";; Sending:\n");		else			printf(";; Got answer:\n");		if (headers) {			printf(";; ->>HEADER<<- opcode: %s, status: %s, "			       "id: %u\n",			       opcodetext[msg->opcode], rcodetext[msg->rcode],			       msg->id);			printf(";; flags:");			if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)				printf(" qr");			if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)				printf(" aa");			if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)				printf(" tc");			if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)				printf(" rd");			if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)				printf(" ra");			if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)				printf(" ad");			if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)				printf(" cd");			printf("; QUERY: %u, ANSWER: %u, "			       "AUTHORITY: %u, ADDITIONAL: %u\n",			       msg->counts[DNS_SECTION_QUESTION],			       msg->counts[DNS_SECTION_ANSWER],			       msg->counts[DNS_SECTION_AUTHORITY],			       msg->counts[DNS_SECTION_ADDITIONAL]);			if (msg != query->lookup->sendmsg &&			    (msg->flags & DNS_MESSAGEFLAG_RD) != 0 &&			    (msg->flags & DNS_MESSAGEFLAG_RA) == 0)				printf(";; WARNING: recursion requested "				       "but not available\n");		}		if (msg != query->lookup->sendmsg && extrabytes != 0U)			printf(";; WARNING: Messages has %u extra byte%s at "			       "end\n", extrabytes, extrabytes != 0 ? "s" : "");	}repopulate_buffer:	if (query->lookup->comments && headers && !short_form) {		result = dns_message_pseudosectiontotext(msg,			 DNS_PSEUDOSECTION_OPT,			 style, flags, buf);		if (result == ISC_R_NOSPACE) {buftoosmall:			len += OUTPUTBUF;			isc_buffer_free(&buf);			result = isc_buffer_allocate(mctx, &buf, len);			if (result == ISC_R_SUCCESS)				goto repopulate_buffer;			else				goto cleanup;		}		check_result(result,		     "dns_message_pseudosectiontotext");	}	if (query->lookup->section_question && headers) {		if (!short_form) {			result = dns_message_sectiontotext(msg,						       DNS_SECTION_QUESTION,						       style, flags, buf);			if (result == ISC_R_NOSPACE)				goto buftoosmall;			check_result(result, "dns_message_sectiontotext");		}	}	if (query->lookup->section_answer) {		if (!short_form) {			result = dns_message_sectiontotext(msg,						       DNS_SECTION_ANSWER,						       style, flags, buf);			if (result == ISC_R_NOSPACE)				goto buftoosmall;			check_result(result, "dns_message_sectiontotext");		} else {			result = short_answer(msg, flags, buf, query);			if (result == ISC_R_NOSPACE)				goto buftoosmall;			check_result(result, "short_answer");		}	}	if (query->lookup->section_authority) {		if (!short_form) {			result = dns_message_sectiontotext(msg,						       DNS_SECTION_AUTHORITY,						       style, flags, buf);			if (result == ISC_R_NOSPACE)				goto buftoosmall;			check_result(result, "dns_message_sectiontotext");		}	}	if (query->lookup->section_additional) {		if (!short_form) {			result = dns_message_sectiontotext(msg,						      DNS_SECTION_ADDITIONAL,						      style, flags, buf);			if (result == ISC_R_NOSPACE)				goto buftoosmall;			check_result(result, "dns_message_sectiontotext");			/*			 * Only print the signature on the first record.			 */			if (headers) {				result = dns_message_pseudosectiontotext(						   msg,						   DNS_PSEUDOSECTION_TSIG,						   style, flags, buf);				if (result == ISC_R_NOSPACE)					goto buftoosmall;				check_result(result,					  "dns_message_pseudosectiontotext");				result = dns_message_pseudosectiontotext(						   msg,						   DNS_PSEUDOSECTION_SIG0,						   style, flags, buf);				if (result == ISC_R_NOSPACE)					goto buftoosmall;				check_result(result,					   "dns_message_pseudosectiontotext");			}		}	}	if (headers && query->lookup->comments && !short_form)		printf("\n");	printf("%.*s", (int)isc_buffer_usedlength(buf),	       (char *)isc_buffer_base(buf));	isc_buffer_free(&buf);cleanup:	if (style != NULL)		dns_master_styledestroy(&style, mctx);	return (result);}/*% * print the greeting message when the program first starts up. */static voidprintgreeting(int argc, char **argv, dig_lookup_t *lookup) {	int i;	int remaining;	static isc_boolean_t first = ISC_TRUE;	char append[MXNAME];	if (printcmd) {		lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;		snprintf(lookup->cmdline, sizeof(lookup->cmdline),			 "%s; <<>> DiG " VERSION " <<>>",			 first?"\n":"");		i = 1;		while (i < argc) {			snprintf(append, sizeof(append), " %s", argv[i++]);			remaining = sizeof(lookup->cmdline) -				    strlen(lookup->cmdline) - 1;			strncat(lookup->cmdline, append, remaining);		}		remaining = sizeof(lookup->cmdline) -			    strlen(lookup->cmdline) - 1;		strncat(lookup->cmdline, "\n", remaining);		if (first && addresscount != 0) {			snprintf(append, sizeof(append),				 "; (%d server%s found)\n",				 addresscount,				 addresscount > 1 ? "s" : "");			remaining = sizeof(lookup->cmdline) -				    strlen(lookup->cmdline) - 1;			strncat(lookup->cmdline, append, remaining);		}		if (first) {			snprintf(append, sizeof(append), 				 ";; global options: %s %s\n",			       short_form ? "short_form" : "",			       printcmd ? "printcmd" : "");			first = ISC_FALSE;			remaining = sizeof(lookup->cmdline) -				    strlen(lookup->cmdline) - 1;			strncat(lookup->cmdline, append, remaining);		}	}}/*% * Reorder an argument list so that server names all come at the end. * This is a bit of a hack, to allow batch-mode processing to properly * handle the server options. */static voidreorder_args(int argc, char *argv[]) {	int i, j;	char *ptr;	int end;	debug("reorder_args()");	end = argc - 1;	while (argv[end][0] == '@') {		end--;		if (end == 0)			return;	}	debug("arg[end]=%s", argv[end]);	for (i = 1; i < end - 1; i++) {		if (argv[i][0] == '@') {			debug("arg[%d]=%s", i, argv[i]);			ptr = argv[i];			for (j = i + 1; j < end; j++) {				debug("Moving %s to %d", argv[j], j - 1);				argv[j - 1] = argv[j];			}			debug("moving %s to end, %d", ptr, end - 1);			argv[end - 1] = ptr;			end--;			if (end < 1)				return;		}	}}static isc_uint32_tparse_uint(char *arg, const char *desc, isc_uint32_t max) {	isc_result_t result;	isc_uint32_t tmp;	result = isc_parse_uint32(&tmp, arg, 10);	if (result == ISC_R_SUCCESS && tmp > max)		result = ISC_R_RANGE;	if (result != ISC_R_SUCCESS)		fatal("%s '%s': %s", desc, arg, isc_result_totext(result));	return (tmp);}/*% * We're not using isc_commandline_parse() here since the command line * syntax of dig is quite a bit different from that which can be described * by that routine. * XXX doc options */static voidplus_option(char *option, isc_boolean_t is_batchfile,	    dig_lookup_t *lookup){	char option_store[256];	char *cmd, *value, *ptr;	isc_boolean_t state = ISC_TRUE;#ifdef DIG_SIGCHASE	size_t n;#endif	strncpy(option_store, option, sizeof(option_store));	option_store[sizeof(option_store)-1]=0;	ptr = option_store;	cmd = next_token(&ptr,"=");	if (cmd == NULL) {		printf(";; Invalid option %s\n", option_store);		return;	}	value = ptr;	if (strncasecmp(cmd, "no", 2)==0) {		cmd += 2;		state = ISC_FALSE;	}#define FULLCHECK(A) \	do { \		size_t _l = strlen(cmd); \		if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \			goto invalid_option; \	} while (0)#define FULLCHECK2(A, B) \	do { \		size_t _l = strlen(cmd); \		if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \		    (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \			goto invalid_option; \	} while (0)	switch (cmd[0]) {	case 'a':		switch (cmd[1]) {		case 'a': /* aaonly / aaflag */			FULLCHECK2("aaonly", "aaflag");			lookup->aaonly = state;			break;		case 'd': 			switch (cmd[2]) {			case 'd': /* additional */				FULLCHECK("additional");				lookup->section_additional = state;				break;			case 'f': /* adflag */				FULLCHECK("adflag");				lookup->adflag = state;				break;			default:				goto invalid_option;			}			break;		case 'l': /* all */			FULLCHECK("all");			lookup->section_question = state;			lookup->section_authority = state;			lookup->section_answer = state;			lookup->section_additional = state;			lookup->comments = state;			lookup->stats = state;			printcmd = state;			break;		case 'n': /* answer */			FULLCHECK("answer");			lookup->section_answer = state;			break;		case 'u': /* authority */			FULLCHECK("authority");			lookup->section_authority = state;			break;		default:			goto invalid_option;		}		break;	case 'b':		switch (cmd[1]) {		case 'e':/* besteffort */			FULLCHECK("besteffort");			lookup->besteffort = state;			break;		case 'u':/* bufsize */			FULLCHECK("bufsize");			if (value == NULL)				goto need_value;			if (!state)				goto invalid_option;			lookup->udpsize = (isc_uint16_t) parse_uint(value,						    "buffer size", COMMSIZE);			break;		default:			goto invalid_option;		}		break;	case 'c':		switch (cmd[1]) {		case 'd':/* cdflag */			FULLCHECK("cdflag");			lookup->cdflag = state;			break;		case 'l': /* cl */			FULLCHECK("cl");			noclass = ISC_TF(!state);			break;		case 'm': /* cmd */			FULLCHECK("cmd");			printcmd = state;			break;		case 'o': /* comments */			FULLCHECK("comments");			lookup->comments = state;			if (lookup == default_lookup)				pluscomm = state;			break;		default:			goto invalid_option;		}		break;	case 'd':		switch (cmd[1]) {		case 'e': /* defname */			FULLCHECK("defname");			usesearch = state;			break;		case 'n': /* dnssec */				FULLCHECK("dnssec");			if (state && lookup->edns == -1)				lookup->edns = 0;			lookup->dnssec = state;			break;		case 'o': /* domain */				FULLCHECK("domain");			if (value == NULL)				goto need_value;			if (!state)				goto invalid_option;			strncpy(domainopt, value, sizeof(domainopt));			domainopt[sizeof(domainopt)-1] = '\0';			break;		default:			goto invalid_option;		}		break;	case 'e':		FULLCHECK("edns");		if (!state) {			lookup->edns = -1;			break;		}		if (value == NULL)			goto need_value;		lookup->edns = (isc_int16_t) parse_uint(value, "edns", 255);		break;	case 'f': /* fail */		FULLCHECK("fail");		lookup->servfail_stops = state;		break;	case 'i':		switch (cmd[1]) {		case 'd': /* identify */			FULLCHECK("identify");			lookup->identify = state;			break;		case 'g': /* ignore */		default: /* Inherets default for compatibility */			FULLCHECK("ignore");			lookup->ignore = ISC_TRUE;		}		break;	case 'm': /* multiline */		FULLCHECK("multiline");		multiline = state;		break;	case 'n':		switch (cmd[1]) {		case 'd': /* ndots */			FULLCHECK("ndots");			if (value == NULL)				goto need_value;			if (!state)				goto invalid_option;			ndots = parse_uint(value, "ndots", MAXNDOTS);			break;		case 's': /* nssearch */			FULLCHECK("nssearch");			lookup->ns_search_only = state;			if (state) {				lookup->trace_root = ISC_TRUE;				lookup->recurse = ISC_TRUE;

⌨️ 快捷键说明

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