2 vmd/cmd/simple/pr_routes.c
5 #define _POSIX_C_SOURCE 2
17 #include <net/netlib.h>
19 #include <net/gen/in.h>
20 #include <net/gen/ip_io.h>
21 #include <net/gen/route.h>
22 #include <net/gen/netdb.h>
23 #include <net/gen/inet.h>
25 #define N_IF 64 /* More than enough? */
32 static void print_header(void);
33 static void print_route(nwio_route_t
*route
);
34 static void fill_iftab(void);
35 static char *get_ifname(ipaddr_t addr
);
36 static void fatal(char *fmt
, ...);
37 static void usage(void);
39 int main(int argc
, char *argv
[])
43 nwio_ipconf_t ip_conf
;
44 unsigned long ioctl_cmd
;
49 int a_flag
, i_flag
, o_flag
;
58 while ((c
=getopt(argc
, argv
, "?aI:io")) != -1)
85 fprintf(stderr
, "%s: getopt failed: '%c'\n",
97 ioctl_cmd
= NWIOGIPIROUTE
;
99 ioctl_cmd
= NWIOGIPOROUTE
;
101 if (ip_device
== NULL
)
102 ip_device
= getenv("IP_DEVICE");
104 if (ip_device
== NULL
)
105 ip_device
= IP_DEVICE
;
107 ip_fd
= open(ip_device
, O_RDONLY
);
110 fprintf(stderr
, "%s: unable to open %s: %s\n", prog_name
,
111 ip_device
, strerror(errno
));
115 if (!all_devices
&& ifname
)
117 cp
= strrchr(ip_device
, '/');
127 result
= ioctl(ip_fd
, NWIOGIPCONF
, &ip_conf
);
130 fprintf(stderr
, "%s: unable to NWIOIPGCONF: %s\n",
131 prog_name
, strerror(errno
));
136 result
= ioctl(ip_fd
, ioctl_cmd
, &route
);
139 fprintf(stderr
, "%s: unable to NWIOGIPxROUTE: %s\n",
140 prog_name
, strerror(errno
));
144 nr_routes
= route
.nwr_ent_count
;
145 for (i
= 0; i
<nr_routes
; i
++)
148 result
= ioctl(ip_fd
, ioctl_cmd
, &route
);
151 fprintf(stderr
, "%s: unable to NWIOGIPxROUTE: %s\n",
152 prog_name
, strerror(errno
));
155 if (all_devices
|| route
.nwr_ifaddr
== ip_conf
.nwic_ipaddr
)
164 int gateway_width
= 15;
169 static void print_header(void)
171 printf("%*s ", ent_width
, "ent #");
172 printf("%*s ", if_width
, "if");
173 printf("%*s ", dest_width
, "dest");
174 printf("%*s ", gateway_width
, "gateway");
175 printf("%*s ", dist_width
, "dist");
176 printf("%*s ", pref_width
, "pref");
177 printf("%*s ", mtu_width
, "mtu");
178 printf("%s", "flags");
182 static char *cidr2a(ipaddr_t addr
, ipaddr_t mask
)
184 ipaddr_t testmask
= 0xFFFFFFFF;
186 static char result
[sizeof("255.255.255.255/255.255.255.255")];
188 for (n
= 32; n
>= 0; n
--)
190 if (mask
== htonl(testmask
))
192 testmask
= (testmask
<< 1) & 0xFFFFFFFF;
195 sprintf(result
, "%s/%-2d", inet_ntoa(addr
), n
);
197 strcpy(strchr(result
, '/')+1, inet_ntoa(mask
));
201 static void print_route(nwio_route_t
*route
)
203 if (!(route
->nwr_flags
& NWRF_INUSE
))
206 printf("%*lu ", ent_width
, (unsigned long) route
->nwr_ent_no
);
207 printf("%*s ", if_width
,
208 ifname
? ifname
: get_ifname(route
->nwr_ifaddr
));
209 printf("%*s ", dest_width
, cidr2a(route
->nwr_dest
, route
->nwr_netmask
));
210 printf("%*s ", gateway_width
, inet_ntoa(route
->nwr_gateway
));
211 printf("%*lu ", dist_width
, (unsigned long) route
->nwr_dist
);
212 printf("%*ld ", pref_width
, (long) route
->nwr_pref
);
213 printf("%*lu", mtu_width
, (long) route
->nwr_mtu
);
214 if (route
->nwr_flags
& NWRF_STATIC
)
216 if (route
->nwr_flags
& NWRF_UNREACHABLE
)
221 static void fill_iftab(void)
224 nwio_ipconf_t ip_conf
;
225 char dev_name
[12]; /* /dev/ipXXXX */
227 for (i
= 0; i
<N_IF
; i
++)
231 sprintf(dev_name
, "/dev/ip%d", i
);
232 fd
= open(dev_name
, O_RDWR
);
235 if (errno
== EACCES
|| errno
== ENOENT
|| errno
== ENXIO
)
237 fatal("unable to open '%s': %s",
238 dev_name
, strerror(errno
));
240 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
241 r
= ioctl(fd
, NWIOGIPCONF
, &ip_conf
);
242 if (r
== -1 && errno
== EAGAIN
)
244 /* interface is down */
250 fatal("NWIOGIPCONF failed on %s: %s",
251 dev_name
, strerror(errno
));
254 iftab
[i
]= ip_conf
.nwic_ipaddr
;
259 if (iftab
[j
] == iftab
[i
])
261 fatal("duplicate address in ip%d and ip%d: %s",
262 i
, j
, inet_ntoa(iftab
[i
]));
269 static char *get_ifname(ipaddr_t addr
)
271 static char name
[7]; /* ipXXXX */
275 for (i
= 0; i
<N_IF
; i
++)
277 if (iftab
[i
] != addr
)
279 sprintf(name
, "ip%d", i
);
283 return inet_ntoa(addr
);
286 static void fatal(char *fmt
, ...)
291 fprintf(stderr
, "%s: ", prog_name
);
292 vfprintf(stderr
, fmt
, ap
);
293 fprintf(stderr
, "\n");
298 static void usage(void)
300 fprintf(stderr
, "Usage: %s [-i|-o] [ -a ] [ -I <ip-device> ]\n",
306 * $PchId: pr_routes.c,v 1.8 2002/04/11 10:58:58 philip Exp $