1 | /* |
---|
2 | * $Id$ |
---|
3 | */ |
---|
4 | |
---|
5 | #include <stdlib.h> |
---|
6 | |
---|
7 | #include <sys/param.h> |
---|
8 | #include <sys/queue.h> |
---|
9 | #include <sys/systm.h> |
---|
10 | #include <sys/kernel.h> |
---|
11 | #include <sys/sysctl.h> |
---|
12 | #include <sys/proc.h> |
---|
13 | #include <sys/mbuf.h> |
---|
14 | #include <sys/socket.h> |
---|
15 | #include <sys/socketvar.h> |
---|
16 | #include <sys/domain.h> |
---|
17 | |
---|
18 | #include <net/if.h> |
---|
19 | #include <net/route.h> |
---|
20 | |
---|
21 | #include <netinet/in.h> |
---|
22 | |
---|
23 | /* |
---|
24 | * We'll use the application versions of malloc and free. |
---|
25 | */ |
---|
26 | #undef malloc |
---|
27 | #undef free |
---|
28 | /* |
---|
29 | * Information per route |
---|
30 | */ |
---|
31 | struct rinfo { |
---|
32 | struct in_addr dst; |
---|
33 | struct in_addr gw_mask; |
---|
34 | unsigned long pksent; |
---|
35 | unsigned long expire; |
---|
36 | int flags; |
---|
37 | char ifname[16]; |
---|
38 | short ifunit; |
---|
39 | short refcnt; |
---|
40 | }; |
---|
41 | |
---|
42 | /* |
---|
43 | * Information per display |
---|
44 | */ |
---|
45 | struct dinfo { |
---|
46 | int capacity; |
---|
47 | int count; |
---|
48 | struct rinfo *routes; |
---|
49 | }; |
---|
50 | |
---|
51 | /* |
---|
52 | * Package everything up before printing it. |
---|
53 | * We don't want to block all network operations till |
---|
54 | * the printing completes! |
---|
55 | */ |
---|
56 | static int |
---|
57 | show_inet_route (rn, vw) |
---|
58 | struct radix_node *rn; |
---|
59 | void *vw; |
---|
60 | { |
---|
61 | struct rtentry *rt = (struct rtentry *)rn; |
---|
62 | struct ifnet *ifp; |
---|
63 | struct dinfo *dp = (struct dinfo *)vw; |
---|
64 | struct rinfo *r; |
---|
65 | |
---|
66 | /* |
---|
67 | * Get a pointer to a new route info structure |
---|
68 | */ |
---|
69 | if (dp->count >= dp->capacity) { |
---|
70 | r = realloc (dp->routes, (sizeof *r) * (dp->capacity + 20)); |
---|
71 | if (r == 0) |
---|
72 | return ENOMEM; |
---|
73 | dp->capacity += 20; |
---|
74 | dp->routes = r; |
---|
75 | } |
---|
76 | r = dp->routes + dp->count++; |
---|
77 | |
---|
78 | /* |
---|
79 | * Fill in the route info structure |
---|
80 | */ |
---|
81 | r->dst = ((struct sockaddr_in *)rt_key(rt))->sin_addr; |
---|
82 | if (rt->rt_flags & RTF_GATEWAY) |
---|
83 | r->gw_mask = ((struct sockaddr_in *)rt->rt_gateway)->sin_addr; |
---|
84 | else if (!(rt->rt_flags & RTF_HOST)) |
---|
85 | r->gw_mask = ((struct sockaddr_in *)rt_mask(rt))->sin_addr; |
---|
86 | r->flags = rt->rt_flags; |
---|
87 | r->refcnt = rt->rt_refcnt; |
---|
88 | r->pksent = rt->rt_rmx.rmx_pksent; |
---|
89 | r->expire = rt->rt_rmx.rmx_expire; |
---|
90 | ifp = rt->rt_ifp; |
---|
91 | strncpy (r->ifname, ifp->if_name, sizeof r->ifname); |
---|
92 | r->ifunit = ifp->if_unit; |
---|
93 | return 0; |
---|
94 | } |
---|
95 | |
---|
96 | /* |
---|
97 | * Reentrant version of inet_ntoa |
---|
98 | */ |
---|
99 | char * |
---|
100 | inet_ntoa_r (struct in_addr ina, char *buf) |
---|
101 | { |
---|
102 | unsigned char *ucp = (unsigned char *)&ina; |
---|
103 | |
---|
104 | sprintf (buf, "%d.%d.%d.%d", |
---|
105 | ucp[0] & 0xff, |
---|
106 | ucp[1] & 0xff, |
---|
107 | ucp[2] & 0xff, |
---|
108 | ucp[3] & 0xff); |
---|
109 | return buf; |
---|
110 | } |
---|
111 | |
---|
112 | void |
---|
113 | rtems_bsdnet_show_inet_routes (void) |
---|
114 | { |
---|
115 | struct radix_node_head *rnh; |
---|
116 | struct dinfo d; |
---|
117 | struct rinfo *r; |
---|
118 | int i, error; |
---|
119 | |
---|
120 | rnh = rt_tables[AF_INET]; |
---|
121 | if (!rnh) |
---|
122 | return; |
---|
123 | d.count = d.capacity = 0; |
---|
124 | d.routes = NULL; |
---|
125 | rtems_bsdnet_semaphore_obtain (); |
---|
126 | error = rnh->rnh_walktree(rnh, show_inet_route, &d); |
---|
127 | rtems_bsdnet_semaphore_release (); |
---|
128 | if (error) { |
---|
129 | printf ("Can't get route info: %s\n", strerror (error)); |
---|
130 | return; |
---|
131 | } |
---|
132 | if (d.count == 0) { |
---|
133 | printf ("No routes!\n"); |
---|
134 | return; |
---|
135 | } |
---|
136 | printf ("Destination Gateway/Mask Flags Refs Use Expire Interface\n"); |
---|
137 | for (i = 0, r = d.routes ; i < d.count ; i++, r++) { |
---|
138 | char buf[30]; |
---|
139 | char *cp, *fc, flagbuf[10]; |
---|
140 | unsigned long flagbit; |
---|
141 | |
---|
142 | if (r->dst.s_addr == INADDR_ANY) |
---|
143 | cp = "default"; |
---|
144 | else |
---|
145 | cp = inet_ntoa_r (r->dst, buf); |
---|
146 | printf ("%-16s", cp); |
---|
147 | if (!(r->flags & RTF_HOST)) |
---|
148 | cp = inet_ntoa_r (r->gw_mask, buf); |
---|
149 | else |
---|
150 | cp = ""; |
---|
151 | printf ("%-16s", cp); |
---|
152 | fc = "UGHRDM XLS"; |
---|
153 | for (flagbit = 0x1, cp = flagbuf ; *fc ; flagbit <<= 1, fc++) { |
---|
154 | if ((r->flags & flagbit) && (*fc != ' ')) |
---|
155 | *cp++ = *fc; |
---|
156 | } |
---|
157 | *cp = '\0'; |
---|
158 | printf ("%-10s%3d%9ld%7ld %.*s%d\n", flagbuf, |
---|
159 | r->refcnt, r->pksent, |
---|
160 | r->expire, |
---|
161 | (int)sizeof r->ifname, r->ifname, |
---|
162 | r->ifunit); |
---|
163 | } |
---|
164 | free (d.routes); |
---|
165 | } |
---|