Commit | Line | Data |
---|---|---|
3537c4ea | 1 | #ifndef lint |
444a708d | 2 | static char sccsid[] = "@(#)route.c 4.4 82/10/07"; |
3537c4ea SL |
3 | #endif |
4 | ||
5 | #include <sys/types.h> | |
6 | #include <sys/socket.h> | |
7 | #include <sys/mbuf.h> | |
8 | #include <net/if.h> | |
9 | #include <net/in.h> | |
10 | #define KERNEL /* to get routehash and RTHASHSIZ */ | |
11 | #include <net/route.h> | |
12 | #include <netdb.h> | |
13 | ||
14 | extern int kmem; | |
15 | extern int nflag; | |
16 | extern char *routename(); | |
17 | ||
18 | /* | |
19 | * Definitions for showing gateway flags. | |
20 | */ | |
21 | struct bits { | |
22 | short b_mask; | |
23 | char b_val; | |
24 | } bits[] = { | |
25 | { RTF_UP, 'U' }, | |
26 | { RTF_GATEWAY, 'G' }, | |
27 | { RTF_HOST, 'H' }, | |
28 | { 0 } | |
29 | }; | |
30 | ||
31 | /* | |
32 | * Print routing tables. | |
33 | */ | |
34 | routepr(hostaddr, netaddr) | |
35 | off_t hostaddr, netaddr; | |
36 | { | |
37 | struct mbuf mb; | |
38 | register struct rtentry *rt; | |
39 | register struct mbuf *m; | |
40 | register struct bits *p; | |
41 | struct netent *np; | |
42 | struct hostent *hp; | |
43 | char name[16], *flags; | |
44 | struct mbuf *routehash[RTHASHSIZ]; | |
45 | struct ifnet ifnet; | |
46 | int first = 1, i, doinghost = 1; | |
47 | ||
48 | if (hostaddr == 0) { | |
49 | printf("rthost: symbol not in namelist\n"); | |
50 | return; | |
51 | } | |
52 | if (netaddr == 0) { | |
53 | printf("rtnet: symbol not in namelist\n"); | |
54 | return; | |
55 | } | |
56 | klseek(kmem, hostaddr, 0); | |
57 | read(kmem, routehash, sizeof (routehash)); | |
58 | printf("Routing tables\n"); | |
59 | printf("%-15.15s %-15.15s %-8.8s %-6.6s %-10.10s %s\n", | |
60 | "Destination", "Gateway", | |
61 | "Flags", "Refcnt", "Use", "Interface"); | |
62 | again: | |
63 | for (i = 0; i < RTHASHSIZ; i++) { | |
64 | if (routehash[i] == 0) | |
65 | continue; | |
66 | m = routehash[i]; | |
67 | while (m) { | |
68 | struct sockaddr_in *sin; | |
69 | ||
70 | klseek(kmem, m, 0); | |
71 | read(kmem, &mb, sizeof (mb)); | |
72 | rt = mtod(&mb, struct rtentry *); | |
73 | sin = (struct sockaddr_in *)&rt->rt_dst; | |
74 | printf("%-15.15s ", routename(sin->sin_addr)); | |
75 | sin = (struct sockaddr_in *)&rt->rt_gateway; | |
76 | printf("%-15.15s ", routename(sin->sin_addr)); | |
77 | for (flags = name, p = bits; p->b_mask; p++) | |
78 | if (p->b_mask & rt->rt_flags) | |
79 | *flags++ = p->b_val; | |
80 | *flags = '\0'; | |
81 | printf("%-8.8s %-6d %-10d ", name, | |
82 | rt->rt_refcnt, rt->rt_use); | |
83 | if (rt->rt_ifp == 0) { | |
84 | putchar('\n'); | |
85 | m = mb.m_next; | |
86 | continue; | |
87 | } | |
88 | klseek(kmem, rt->rt_ifp, 0); | |
89 | read(kmem, &ifnet, sizeof (ifnet)); | |
90 | klseek(kmem, (int)ifnet.if_name, 0); | |
91 | read(kmem, name, 16); | |
92 | printf("%s%d\n", name, ifnet.if_unit); | |
93 | m = mb.m_next; | |
94 | } | |
95 | } | |
96 | if (doinghost) { | |
97 | klseek(kmem, netaddr, 0); | |
98 | read(kmem, routehash, sizeof (routehash)); | |
99 | doinghost = 0; | |
100 | goto again; | |
101 | } | |
102 | } | |
103 | ||
104 | char * | |
105 | routename(in) | |
106 | struct in_addr in; | |
107 | { | |
108 | char *cp = 0; | |
109 | static char line[50]; | |
110 | int lna, net; | |
111 | ||
444a708d SL |
112 | net = inet_netof(in); |
113 | lna = inet_lnaof(in); | |
3537c4ea SL |
114 | if (!nflag) { |
115 | if (lna == INADDR_ANY) { | |
3394f0b5 | 116 | struct netent *np = getnetbyaddr(net, AF_INET); |
3537c4ea SL |
117 | |
118 | if (np) | |
119 | cp = np->n_name; | |
120 | } else { | |
9bce7b73 | 121 | struct hostent *hp; |
3537c4ea | 122 | |
3394f0b5 SL |
123 | hp = gethostbyaddr(&in, sizeof (struct in_addr), |
124 | AF_INET); | |
3537c4ea SL |
125 | if (hp) |
126 | cp = hp->h_name; | |
127 | } | |
128 | } | |
129 | if (cp) | |
130 | strcpy(line, cp); | |
131 | else { | |
132 | u_char *ucp = (u_char *)∈ | |
133 | if (lna == INADDR_ANY) | |
134 | sprintf(line, "%u.%u.%u", ucp[0], ucp[1], ucp[2]); | |
135 | else | |
136 | sprintf(line, "%u.%u.%u.%u", ucp[0], ucp[1], | |
137 | ucp[2], ucp[3]); | |
138 | } | |
139 | return (line); | |
140 | } |