dnscheck.c

来自「RADIUS协议的认证计费服务」· C语言 代码 · 共 1,020 行 · 第 1/2 页

C
1,020
字号
} /* end of make_my_hostent () *//***************************************************************************** * *	Function: my_strdup * *	Purpose: Because strdup (), despite being so useful, isn't *		 available everywhere. * ****************************************************************************/static char *my_strdup (src)CONST char     *src;{	int             len;	char           *new = NULL;	if (src)	{		len = strlen (src) + 1;		if ((new = (char *) malloc (len)))		{			strcpy (new, src);		}	}	return new;} /* end of my_strdup () *//***************************************************************************** * *	Function: report * *	Purpose: Make a report * *	Returns: Number of problems found. * ****************************************************************************/static intreport (prefix, hostname, root)char           *prefix;char           *hostname;my_hostent_t   *root;{	int             problems = 0;	int             c;	my_hostent_t  **subs;	char          **tabs;	if (root == (my_hostent_t *) NULL)	{		return (1);	/* Nothing there, 1 problem. */	}	/* Analyze this. */	if (strcmp (hostname, root->h_name) != 0)	{		printf ("%s '%s' isn't the CANONICAL DNS name '%s'\n",			prefix, hostname, root->h_name);		problems++;	}	for (subs = root->m_addr_list, c = 0, tabs = root->h_addr_list;		tabs[c];		c++)	{		if (!subs[c])		{			printf ("REVERSE MAP of [%s] back to '%s' not found\n",				inet_ntoa (*(struct in_addr *) tabs[c]),				root->h_name);			problems++;		}		else if (subs[c]->m_name != root)		{			printf ("REVERSE MAP of [%s] back to '%s' incorrect\n",				inet_ntoa (*(struct in_addr *) tabs[c]),				root->h_name);			if (subs[c]->m_name)			{				printf ("\treverse map points to \'%s\'\n",					subs[c]->m_name->h_name);			}			else			{				printf ("\treverse map doesn't exist!\n");			}			problems++;		}	}	if (terse == 0)	{		if (problems == 0)		{			printf ("No problems found with '%s'\n", hostname);		}		else		{			printf ("%d problems found with '%s'\n",				problems, hostname);		}	}	return problems;} /* end of report () *//***************************************************************************** * *	Function: report_addr * *	Purpose: Look up an entry by address and display it. * ****************************************************************************/static my_hostent_t *report_addr (lvl, sib, hostaddr)int             lvl;int             sib;char           *hostaddr;{	my_hostent_t   *np;	my_hostent_t   *mp = NULL;	char          **p;	int             i;	if (terse == 0)	{		if (lvl > 0)		{			printf ("#%d.%d", lvl, sib);		}		for (i = 0; i < lvl; i++)		{			putchar ('\t');		}	}	sib = 0;	if ((mp = already_addrd (hostaddr)))	{		if (terse == 0)		{			printf ("([%s])\t\t# Shown earlier (by addr)\n",				inet_ntoa (*(struct in_addr *) hostaddr));		}		return mp;	}	if ((mp = make_my_hostent (gethostbyaddr (hostaddr, 4, AF_INET))))	{		if ((np = cache_hostent_addr (mp)) != mp)		{			free_my_hostent (mp);			mp = np;		}		if (terse == 0)		{			printf ("[%s] => %s",				inet_ntoa (*(struct in_addr *) mp->h_addr),				mp->h_name);			printf (" [");			for (p = mp->h_addr_list; *p; p++)			{				if (memcmp (*p, hostaddr, 4) != 0)				{					printf ("%s",					  inet_ntoa (*(struct in_addr *) * p));				}				else				{					printf ("==");				}				if (p[1])				{					printf (", ");				}			}			printf ("]\n");		}		np = report_name (lvl + 1, sib++, mp->h_name);		if (np)		{			mp->m_name = np;		}		for (p = mp->h_aliases, i = 0; p[i]; i++)		{			np = report_name (lvl + 1, sib++, p[i]);		}		for (p = mp->h_addr_list, i = 0; p[i]; i++)		{			np = report_addr (lvl + 1, sib++, p[i]);		}	}	else /* make_my_hostent() found errors */	{		printf ("%s: [%s]\t",			progname, inet_ntoa (*(struct in_addr *) hostaddr));		switch (h_errno)		{		    case HOST_NOT_FOUND:			printf ("*** Not in DNS!\n");			break;		    case TRY_AGAIN:			printf ("Try again later.\n");			break;		    case NO_DATA:			printf ("Not a host addr.\n");			break;		    case NO_RECOVERY:			printf ("*** DNS server error\n");			break;		    default:			printf ("*** Unknown h_errno=%d\n", h_errno);			break;		}	}	return mp;} /* end of report_addr () *//***************************************************************************** * *	Function: report_name * *	Purpose: Wrapper for gethostbyname() and the internal cache of names. * ****************************************************************************/static my_hostent_t *report_name (lvl, sib, hostname)int             lvl;int             sib;char           *hostname;{	int             i;	struct hostent *hp;	my_hostent_t   *np;	my_hostent_t   *mp = NULL;	char          **p;	char            etc[200];	if (debug > 0)	{		fprintf (stderr, "%s-debug: report_name(%d, %d, \'%s\')\n",			progname, lvl, sib, hostname);	}	if (terse == 0)	{		if (lvl > 0)		{			printf ("#%d.%d", lvl, sib);		}		for (i = 0; i < lvl; i++)		{			putchar ('\t');		}	}	sib = 1;	if ((mp = already_named (hostname)))	{		if (terse == 0)		{			printf ("(%s)\t\t# Shown earlier (by name)\n",				hostname);		}		return mp;	}	hp = gethostbyname (hostname);	if ((mp = make_my_hostent (hp)))	{		if ((np = cache_hostent_name (mp)) != mp)		{			free_my_hostent (mp);			mp = np;		}		if (terse == 0)		{			printf ("%s\t", mp->h_name);			etc[0] = '\0';			if (strcasecmp (hostname, mp->h_name) != 0)			{				strcat (etc, "\t <--- Warning, not canonical");				printf ("( != %s)", hostname);			}			printf (" => [");			for (p = mp->h_addr_list; *p; p++)			{				printf ("%s",					inet_ntoa (*(struct in_addr *) * p));				if (p[1])				{					printf (", ");				}			}			printf ("]%s\n", etc);		}		for (p = mp->h_aliases, i = 0; p[i]; i++)		{			np = report_name (lvl + 1, sib++, p[i]);		}		for (p = mp->h_addr_list, i = 0; p[i]; i++)		{			np = report_addr (lvl + 1, sib++, p[i]);			if (np)			{				mp->m_addr_list[i] = np;			}		}	}	else /* make_my_hostent() found errors */	{		printf ("%s: %s\t", progname, hostname);		switch (h_errno)		{		    case HOST_NOT_FOUND:			printf ("*** Not in DNS!\n");			break;		    case TRY_AGAIN:			printf ("Try again later.\n");			break;		    case NO_DATA:			printf ("Not a host name.\n");			break;		    case NO_RECOVERY:			printf ("*** DNS server error\n");			break;		    default:			printf ("*** Unknown h_errno=%d\n", h_errno);			break;		}	} /* if ((mp == make_my_hostent (gethostbyname (hostname)))) */	return mp;} /* end of report_name () *//***************************************************************************** * *	Function: main * *	Purpose: Start the game * ****************************************************************************/intmain (argc, argv)int             argc;char          **argv;{	int             c;	int             problems;	char           *p;	my_hostent_t   *root;	char            our_hostname[MAXHOSTNAMELEN + 1];	extern char    *optarg;	extern int      optind;	memset (our_hostname, 0, sizeof (our_hostname));	gethostname (our_hostname, sizeof (our_hostname) - 1);	our_hostname[sizeof (our_hostname) - 1] = '\0';	/* Safety. */	progname = *argv;	for (p = progname; *p; p++)	{		if (*p == '/')		{			progname = p + 1;		}	}	while ((c = getopt (argc, argv, "ctvxh0")) != -1)	{		switch (c)		{		    case 'c':			check = (check + 1) % 2;			break;		    case 'h':	/* HELP ME OUT */			copyright ();			dns_usage ();			exit (1);		    case 't':			terse++;			break;		    case 'v':			copyright ();			fprintf (stderr, "version %s\n", rcsid);			exit (0);		    case 'x':			debug++;			break;		    case '0':			notice = (notice == 0) ? 1 : 0;			if (notice)			{				copyright ();			}			break;		    default:			copyright ();			fprintf (stderr, "unsupported option\n");			dns_usage ();			exit (20);		}	}	copyright ();	if (debug > 0)	{		fprintf (stderr, "%s-debug : ourhostname = \'%s\'\n",			progname, our_hostname);	}	if ((terse > 0) && (check == 0))	{		fprintf (stderr,	  "%s: use of -c without -t (or vice-versa) may result in no output\n",			progname);	}	if (optind < argc)	{		for (; optind < argc; optind++)		{			problems = 0;			root = report_name (0, 0, argv[optind]);			if (check > 0)			{				problems = report ("The name ", argv[optind],									root);			}			if ((optind < argc - 1) && (terse == 0))			{				printf ("\n---------------------------\n");			}		}	}	else	{		problems = 0;		if (!(root = report_name (0, 0, our_hostname)))		{			printf ("SYSTEM'S HOSTNAME ('%s') IS NOT IN THE DNS!\n",				our_hostname);			exit (1);		}		else if (check > 0)		{			problems = report ("WARNING, your system's hostname",					   our_hostname, root);		}	}	exit (0);} /* end of main () */

⌨️ 快捷键说明

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