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