1 | /* |
---|
2 | * Test RTEMS networking |
---|
3 | * |
---|
4 | * This program may be distributed and used for any purpose. |
---|
5 | * I ask only that you: |
---|
6 | * 1. Leave this author information intact. |
---|
7 | * 2. Document any changes you make. |
---|
8 | * |
---|
9 | * W. Eric Norum |
---|
10 | * Saskatchewan Accelerator Laboratory |
---|
11 | * University of Saskatchewan |
---|
12 | * Saskatoon, Saskatchewan, CANADA |
---|
13 | * eric@skatter.usask.ca |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #include <stdio.h> |
---|
19 | #include <string.h> |
---|
20 | #include <errno.h> |
---|
21 | #include <ctype.h> |
---|
22 | #include <rtems.h> |
---|
23 | #include <rtems/rtems_bsdnet.h> |
---|
24 | #include <rtems/error.h> |
---|
25 | #include <sys/types.h> |
---|
26 | #include <sys/socket.h> |
---|
27 | #include <netinet/in.h> |
---|
28 | #include <netdb.h> |
---|
29 | #include <arpa/inet.h> |
---|
30 | |
---|
31 | /* |
---|
32 | * Show network-related statistics |
---|
33 | */ |
---|
34 | static void |
---|
35 | showStatistics (void) |
---|
36 | { |
---|
37 | rtems_bsdnet_show_inet_routes (); |
---|
38 | rtems_bsdnet_show_mbuf_stats (); |
---|
39 | rtems_bsdnet_show_if_stats (); |
---|
40 | rtems_bsdnet_show_ip_stats (); |
---|
41 | rtems_bsdnet_show_icmp_stats (); |
---|
42 | rtems_bsdnet_show_udp_stats (); |
---|
43 | rtems_bsdnet_show_tcp_stats (); |
---|
44 | } |
---|
45 | |
---|
46 | /* |
---|
47 | * Show host information |
---|
48 | */ |
---|
49 | static void |
---|
50 | showhost (struct hostent *hp) |
---|
51 | { |
---|
52 | char **ap; |
---|
53 | struct in_addr in; |
---|
54 | |
---|
55 | if (hp == NULL) { |
---|
56 | printf ("Host information not available.\n"); |
---|
57 | return; |
---|
58 | } |
---|
59 | printf ("Official name: %s\n", hp->h_name); |
---|
60 | ap = hp->h_aliases; |
---|
61 | if (ap && *ap) { |
---|
62 | printf ("Alias%s:\n", ap[1] ? "es" : ""); |
---|
63 | while (*ap) |
---|
64 | printf (" %s\n", *ap++); |
---|
65 | } |
---|
66 | if ((hp->h_addrtype == AF_INET) && (hp->h_length == sizeof in)) { |
---|
67 | ap = hp->h_addr_list; |
---|
68 | if (ap && *ap) { |
---|
69 | printf ("Address%s:", ap[1] ? "es" : ""); |
---|
70 | while (*ap) { |
---|
71 | memcpy (&in, *ap++, sizeof in); |
---|
72 | printf (" %s", inet_ntoa (in)); |
---|
73 | } |
---|
74 | printf ("\n"); |
---|
75 | } |
---|
76 | } |
---|
77 | else { |
---|
78 | printf ("Address type: %d\n", hp->h_addrtype); |
---|
79 | printf ("Address length: %d\n", hp->h_length); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | /* |
---|
84 | * Test Domain Name Servers |
---|
85 | */ |
---|
86 | void |
---|
87 | testDNS (void) |
---|
88 | { |
---|
89 | char namebuf[100]; |
---|
90 | char *name, *cp; |
---|
91 | struct hostent *hp; |
---|
92 | |
---|
93 | for (;;) { |
---|
94 | printf ("\nhost? "); |
---|
95 | if (fgets (namebuf, sizeof namebuf, stdin) == NULL) |
---|
96 | return; |
---|
97 | cp = namebuf; |
---|
98 | while (isspace ((int) *cp)) |
---|
99 | cp++; |
---|
100 | if (cp[0] == '\0') { |
---|
101 | showStatistics (); |
---|
102 | continue; |
---|
103 | } |
---|
104 | name = cp; |
---|
105 | while (isgraph ((int) *cp)) |
---|
106 | cp++; |
---|
107 | *cp = '\0'; |
---|
108 | if (isdigit((int) *name)) { |
---|
109 | struct in_addr addr; |
---|
110 | |
---|
111 | addr.s_addr = inet_addr (name); |
---|
112 | hp = gethostbyaddr ((char *)&addr, sizeof addr, AF_INET); |
---|
113 | } |
---|
114 | else { |
---|
115 | hp = gethostbyname (name); |
---|
116 | } |
---|
117 | showhost (hp); |
---|
118 | } |
---|
119 | } |
---|