source: rtems/c/src/exec/libnetworking/rtems/rtems_showroute.c @ 96b39164

4.104.114.84.95
Last change on this file since 96b39164 was 96b39164, checked in by Joel Sherrill <joel.sherrill@…>, on 08/20/98 at 21:56:40

Added CVS Ids

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