2 vmd/cmd/simple/pr_routes.c
15 #include <net/netlib.h>
17 #include <net/gen/in.h>
18 #include <net/gen/ip_io.h>
19 #include <net/gen/route.h>
21 #include <net/gen/inet.h>
22 #include <arpa/inet.h>
24 #define N_IF 64 /* More than enough? */
31 static void print_header(void);
32 static void print_route(nwio_route_t
*route
);
33 static void fill_iftab(void);
34 static char *get_ifname(ipaddr_t addr
);
35 static void fatal(char *fmt
, ...);
36 static void usage(void);
38 int main(int argc
, char *argv
[])
42 nwio_ipconf_t ip_conf
;
43 unsigned long ioctl_cmd
;
48 int a_flag
, i_flag
, o_flag
;
57 while ((c
=getopt(argc
, argv
, "?aI:io")) != -1)
84 fprintf(stderr
, "%s: getopt failed: '%c'\n",
96 ioctl_cmd
= NWIOGIPIROUTE
;
98 ioctl_cmd
= NWIOGIPOROUTE
;
100 if (ip_device
== NULL
)
101 ip_device
= getenv("IP_DEVICE");
103 if (ip_device
== NULL
)
104 ip_device
= IP_DEVICE
;
106 ip_fd
= open(ip_device
, O_RDONLY
);
109 fprintf(stderr
, "%s: unable to open %s: %s\n", prog_name
,
110 ip_device
, strerror(errno
));
114 if (!all_devices
&& ifname
)
116 cp
= strrchr(ip_device
, '/');
126 result
= ioctl(ip_fd
, NWIOGIPCONF
, &ip_conf
);
129 fprintf(stderr
, "%s: unable to NWIOIPGCONF: %s\n",
130 prog_name
, strerror(errno
));
135 result
= ioctl(ip_fd
, ioctl_cmd
, &route
);
138 fprintf(stderr
, "%s: unable to NWIOGIPxROUTE: %s\n",
139 prog_name
, strerror(errno
));
143 nr_routes
= route
.nwr_ent_count
;
144 for (i
= 0; i
<nr_routes
; i
++)
147 result
= ioctl(ip_fd
, ioctl_cmd
, &route
);
150 fprintf(stderr
, "%s: unable to NWIOGIPxROUTE: %s\n",
151 prog_name
, strerror(errno
));
154 if (all_devices
|| route
.nwr_ifaddr
== ip_conf
.nwic_ipaddr
)
163 int gateway_width
= 15;
168 static void print_header(void)
170 printf("%*s ", ent_width
, "ent #");
171 printf("%*s ", if_width
, "if");
172 printf("%*s ", dest_width
, "dest");
173 printf("%*s ", gateway_width
, "gateway");
174 printf("%*s ", dist_width
, "dist");
175 printf("%*s ", pref_width
, "pref");
176 printf("%*s ", mtu_width
, "mtu");
177 printf("%s", "flags");
181 static char *cidr2a(ipaddr_t addr
, ipaddr_t mask
)
183 ipaddr_t testmask
= 0xFFFFFFFF;
185 static char result
[sizeof("255.255.255.255/255.255.255.255")];
187 for (n
= 32; n
>= 0; n
--)
189 if (mask
== htonl(testmask
))
191 testmask
= (testmask
<< 1) & 0xFFFFFFFF;
194 sprintf(result
, "%s/%-2d", inet_ntoa(*(struct in_addr
*)&addr
), n
);
196 strcpy(strchr(result
, '/')+1,
197 inet_ntoa(*(struct in_addr
*)&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
,
211 inet_ntoa(*(struct in_addr
*)&route
->nwr_gateway
));
212 printf("%*lu ", dist_width
, (unsigned long) route
->nwr_dist
);
213 printf("%*ld ", pref_width
, (long) route
->nwr_pref
);
214 printf("%*lu", mtu_width
, (long) route
->nwr_mtu
);
215 if (route
->nwr_flags
& NWRF_STATIC
)
217 if (route
->nwr_flags
& NWRF_UNREACHABLE
)
222 static void fill_iftab(void)
225 nwio_ipconf_t ip_conf
;
226 char dev_name
[12]; /* /dev/ipXXXX */
228 for (i
= 0; i
<N_IF
; i
++)
232 sprintf(dev_name
, "/dev/ip%d", i
);
233 fd
= open(dev_name
, O_RDWR
);
236 if (errno
== EACCES
|| errno
== ENOENT
|| errno
== ENXIO
)
238 fatal("unable to open '%s': %s",
239 dev_name
, strerror(errno
));
241 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
242 r
= ioctl(fd
, NWIOGIPCONF
, &ip_conf
);
243 if (r
== -1 && errno
== EAGAIN
)
245 /* interface is down */
251 fatal("NWIOGIPCONF failed on %s: %s",
252 dev_name
, strerror(errno
));
255 iftab
[i
]= ip_conf
.nwic_ipaddr
;
260 if (iftab
[j
] == iftab
[i
])
262 fatal("duplicate address in ip%d and ip%d: %s",
264 inet_ntoa(*(struct in_addr
*)&iftab
[i
]));
271 static char *get_ifname(ipaddr_t addr
)
273 static char name
[7]; /* ipXXXX */
277 for (i
= 0; i
<N_IF
; i
++)
279 if (iftab
[i
] != addr
)
281 sprintf(name
, "ip%d", i
);
285 return inet_ntoa(*(struct in_addr
*)&addr
);
288 static void fatal(char *fmt
, ...)
293 fprintf(stderr
, "%s: ", prog_name
);
294 vfprintf(stderr
, fmt
, ap
);
295 fprintf(stderr
, "\n");
300 static void usage(void)
302 fprintf(stderr
, "Usage: %s [-i|-o] [ -a ] [ -I <ip-device> ]\n",
308 * $PchId: pr_routes.c,v 1.8 2002/04/11 10:58:58 philip Exp $