Let us qualify 'route flush' and 'route show' commands with -llinfo

and -host flags, which is useful for displaying/flushing ARP entries:

# route -n show -inet -llinfo -host
Routing table

Internet:
Destination        Gateway            Flags
169.254.1.119      link#1             UH
169.254.230.110    00:02:6f:21:e6:6e  UH
# route -n flush -inet -llinfo -host
169.254.1.119                             done
169.254.230.110      0.2.6f.21.e6.6e      done
169.254.237.70                            done
# route -n show -inet -llinfo -host
Routing table
#

This will help me address bin/11079, "dhclient may require arp and sed".

Extract common code from flushroutes() and show(), creating
parse_show_opts().

While I'm here, make small cosmetic changes to flushroutes().
This commit is contained in:
dyoung 2006-09-23 22:41:25 +00:00
parent 9f4b947629
commit 6333e1495e
3 changed files with 87 additions and 73 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.8 2006/09/07 02:40:31 dogcow Exp $ */
/* $NetBSD: extern.h,v 1.9 2006/09/23 22:41:25 dyoung Exp $ */
/*
* Copyright (c) 1997 Christos Zoulas. All rights reserved.
@ -33,6 +33,7 @@ struct sockaddr;
struct sockaddr_x25;
struct sockaddr_ns;
void parse_show_opts(int, char **, int *, int *, const char **, int);
/* show.c */
void show(int, char **);

View File

@ -1,4 +1,4 @@
/* $NetBSD: route.c,v 1.103 2006/09/23 21:55:47 dyoung Exp $ */
/* $NetBSD: route.c,v 1.104 2006/09/23 22:41:25 dyoung Exp $ */
/*
* Copyright (c) 1983, 1989, 1991, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1991, 1993\n\
#if 0
static char sccsid[] = "@(#)route.c 8.6 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: route.c,v 1.103 2006/09/23 21:55:47 dyoung Exp $");
__RCSID("$NetBSD: route.c,v 1.104 2006/09/23 22:41:25 dyoung Exp $");
#endif
#endif /* not lint */
@ -243,43 +243,17 @@ main(int argc, char **argv)
static int
flushroutes(int argc, char *argv[], int doall)
{
struct sockaddr *sa;
size_t needed;
int mib[6], rlen, seqno;
int flags, mib[6], rlen, seqno;
char *buf, *next, *lim;
const char *afname;
struct rt_msghdr *rtm;
flags = 0;
af = AF_UNSPEC;
shutdown(sock, SHUT_RD); /* Don't want to read back our messages */
if (argc > 1) {
argv++;
if (argc == 2 && **argv == '-') {
switch (keyword(*argv + 1)) {
case K_INET:
af = AF_INET;
break;
#ifdef INET6
case K_INET6:
af = AF_INET6;
break;
#endif
#ifndef SMALL
case K_ATALK:
af = AF_APPLETALK;
break;
case K_ISO:
case K_OSI:
af = AF_ISO;
break;
#endif /* SMALL */
case K_LINK:
af = AF_LINK;
break;
default:
goto bad;
}
} else
bad: usage(*argv);
}
parse_show_opts(argc, argv, &af, &flags, &afname, 0);
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0; /* protocol */
@ -299,24 +273,23 @@ bad: usage(*argv);
if (verbose) {
(void)printf("Examining routing table from sysctl\n");
if (af != AF_UNSPEC)
printf("(address family %s)\n", (*argv + 1));
printf("(address family %s)\n", afname);
}
if (needed == 0)
return 0;
seqno = 0; /* ??? */
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
sa = (struct sockaddr *)(rtm + 1);
if (verbose)
print_rtmsg(rtm, rtm->rtm_msglen);
if ((rtm->rtm_flags & flags) != flags)
continue;
if (!(rtm->rtm_flags & (RTF_GATEWAY | RTF_STATIC |
RTF_LLINFO)) && !doall)
continue;
if (af != AF_UNSPEC) {
struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
if (sa->sa_family != af)
continue;
}
if (af != AF_UNSPEC && sa->sa_family != af)
continue;
if (debugonly)
continue;
rtm->rtm_type = RTM_DELETE;
@ -336,10 +309,10 @@ bad: usage(*argv);
if (verbose)
print_rtmsg(rtm, rlen);
else {
struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
(void)printf("%-20.20s ",
routename(sa, NULL, rtm->rtm_flags));
sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) +
(char *)sa);
(void)printf("%-20.20s ",
routename(sa, NULL, RTF_HOST));
(void)printf("done\n");

View File

@ -1,4 +1,4 @@
/* $NetBSD: show.c,v 1.32 2006/09/23 21:11:53 dyoung Exp $ */
/* $NetBSD: show.c,v 1.33 2006/09/23 22:41:25 dyoung Exp $ */
/*
* Copyright (c) 1983, 1988, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
#else
__RCSID("$NetBSD: show.c,v 1.32 2006/09/23 21:11:53 dyoung Exp $");
__RCSID("$NetBSD: show.c,v 1.33 2006/09/23 22:41:25 dyoung Exp $");
#endif
#endif /* not lint */
@ -98,6 +98,70 @@ static void pr_family(int);
static void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int );
static void p_flags(int);
void
parse_show_opts(int argc, char **argv, int *afp, int *flagsp,
const char **afnamep, int nolink)
{
const char *afname = "unspec";
int af, flags;
flags = 0;
af = AF_UNSPEC;
for (; argc >= 2; argc--) {
if (*argv[argc - 1] != '-')
goto bad;
switch (keyword(argv[argc - 1] + 1)) {
case K_HOST:
flags |= RTF_HOST;
break;
case K_LLINFO:
flags |= RTF_LLINFO;
break;
case K_INET:
af = AF_INET;
afname = argv[argc - 1] + 1;
break;
#ifdef INET6
case K_INET6:
af = AF_INET6;
afname = argv[argc - 1] + 1;
break;
#endif
#ifndef SMALL
case K_ATALK:
af = AF_APPLETALK;
afname = argv[argc - 1] + 1;
break;
case K_ISO:
case K_OSI:
af = AF_ISO;
afname = argv[argc - 1] + 1;
break;
#endif /* SMALL */
case K_LINK:
if (nolink)
goto bad;
af = AF_LINK;
afname = argv[argc - 1] + 1;
break;
default:
goto bad;
}
}
switch (argc) {
case 1:
case 0:
break;
default:
bad:
usage(argv[argc - 1]);
}
if (afnamep != NULL)
*afnamep = afname;
*afp = af;
*flagsp = flags;
}
/*
* Print routing tables.
*/
@ -105,38 +169,12 @@ void
show(int argc, char **argv)
{
size_t needed;
int af, mib[6];
int af, flags, mib[6];
char *buf, *next, *lim;
struct rt_msghdr *rtm;
struct sockaddr *sa;
af = AF_UNSPEC;
if (argc > 1) {
argv++;
if (argc == 2 && **argv == '-')
switch (keyword(*argv + 1)) {
case K_INET:
af = AF_INET;
break;
#ifdef INET6
case K_INET6:
af = AF_INET6;
break;
#endif
#ifndef SMALL
case K_ATALK:
af = AF_APPLETALK;
break;
case K_ISO:
case K_OSI:
af = AF_ISO;
break;
#endif /* SMALL */
default:
goto bad;
} else
bad: usage(*argv);
}
parse_show_opts(argc, argv, &af, &flags, NULL, 1);
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
@ -160,6 +198,8 @@ bad: usage(*argv);
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
sa = (struct sockaddr *)(rtm + 1);
if ((rtm->rtm_flags & flags) != flags)
continue;
if (af == AF_UNSPEC || af == sa->sa_family)
p_rtentry(rtm);
}