source: rtems/c/src/exec/libnetworking/rtems/rtems_showroute.c @ 39e6e65a

4.104.114.84.95
Last change on this file since 39e6e65a was 39e6e65a, checked in by Joel Sherrill <joel.sherrill@…>, on 08/19/98 at 21:32:28

Base files

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