summaryrefslogtreecommitdiffstats
path: root/dnstest/test.c
blob: a2ab47c05a29558e784abacf3b49213da822e519 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Test RTEMS networking
 *
 * This program may be distributed and used for any purpose.
 * I ask only that you:
 *      1. Leave this author information intact.
 *      2. Document any changes you make.
 *
 * W. Eric Norum
 * Saskatchewan Accelerator Laboratory
 * University of Saskatchewan
 * Saskatoon, Saskatchewan, CANADA
 * eric@skatter.usask.ca
 *
 *  $Id$
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <rtems.h>
#include <rtems/rtems_bsdnet.h>
#include <rtems/error.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

/*
 * Show network-related statistics
 */
static void
showStatistics (void)
{
	rtems_bsdnet_show_inet_routes ();
	rtems_bsdnet_show_mbuf_stats ();
	rtems_bsdnet_show_if_stats ();
	rtems_bsdnet_show_ip_stats ();
	rtems_bsdnet_show_icmp_stats ();
	rtems_bsdnet_show_udp_stats ();
	rtems_bsdnet_show_tcp_stats ();
}

/*
 * Show host information
 */
static void
showhost (struct hostent *hp)
{
	char **ap;
	struct in_addr in;

	if (hp == NULL) {
		printf ("Host information not available.\n");
		return;
	}
	printf ("Official name: %s\n", hp->h_name);
	ap = hp->h_aliases;
	if (ap && *ap) {
		printf ("Alias%s:\n", ap[1] ? "es" : "");
		while (*ap)
			printf ("    %s\n", *ap++);
	}
	if ((hp->h_addrtype == AF_INET) && (hp->h_length == sizeof in)) {
		ap = hp->h_addr_list;
		if (ap && *ap) {
			printf ("Address%s:", ap[1] ? "es" : "");
			while (*ap) {
				memcpy (&in, *ap++, sizeof in);
				printf (" %s", inet_ntoa (in));
			}
			printf ("\n");
		}
	}
	else {
		printf ("Address type: %d\n", hp->h_addrtype);
		printf ("Address length: %d\n", hp->h_length);
	}
}

/*
 * Test Domain Name Servers
 */
void
testDNS (void)
{
	char namebuf[100];
	char *name, *cp;
	struct hostent *hp;

	for (;;) {
		printf ("\nhost? ");
		if (fgets (namebuf, sizeof namebuf, stdin) == NULL)
			return;
		cp = namebuf;
		while (isspace ((int) *cp))
			cp++;
		if (cp[0] == '\0') {
			showStatistics ();
			continue;
		}
		name = cp;
		while (isgraph ((int) *cp))
			cp++;
		*cp = '\0';
		if (isdigit((int) *name)) {
			struct in_addr addr;

			addr.s_addr = inet_addr (name);
			hp = gethostbyaddr ((char *)&addr, sizeof addr, AF_INET);
		}
		else {
			hp = gethostbyname (name);
		}
		showhost (hp);
	}
}