Skip to content

Commit

Permalink
Hide informative output of flush routes/arp behind a verbose flag
Browse files Browse the repository at this point in the history
Suggested by Tom
  • Loading branch information
stspdotname committed Nov 1, 2024
1 parent 6ee0197 commit 726548d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
30 changes: 26 additions & 4 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1046,9 +1046,17 @@ sysctlhelp(int unused1, char **unused2, ...)
* Data structures and routines for the "flush" command.
*/

struct ghs flushtab[] = {
{ "<cr>", "Type Enter to run command", CMPL0 NULL, 0 },
{ "verbose", "Show verbose diagnostics", CMPL0 NULL, 0 },
{ NULL, NULL, NULL, NULL, 0 }
};

Menu flushlist[] = {
{ "routes", "IP routes", CMPL0 0, 0, 0, 0, flush_ip_routes },
{ "arp", "ARP cache", CMPL0 0, 0, 0, 0, flush_arp_cache },
{ "routes", "IP routes", CMPL(h) (char **)flushtab,
sizeof(struct ghs), 0, 1, flush_ip_routes },
{ "arp", "ARP cache", CMPL(h) (char **)flushtab,
sizeof(struct ghs), 0, 1, flush_arp_cache },
{ "ndp", "NDP cache", CMPL0 0, 0, 0, 0, flush_ndp_cache },
{ "line", "Active user", CMPL0 0, 0, 1, 1, flush_line },
{ "bridge-dyn", "Dynamically learned bridge addresses", CMPL0 0, 0, 1, 1, flush_bridgedyn },
Expand Down Expand Up @@ -3457,15 +3465,29 @@ powerdown(int argc, char **argv, ...)
static int
flush_ip_routes(int argc, char **argv, ...)
{
flushroutes(AF_INET, AF_INET);
va_list ap;
char *verbose_arg;

va_start(ap, argv);
verbose_arg = va_arg(ap, char *);
va_end(ap);

flushroutes(AF_INET, AF_INET, verbose_arg != NULL);

return(0);
}

static int
flush_arp_cache(int argc, char **argv, ...)
{
flushroutes(AF_INET, AF_LINK);
va_list ap;
char *verbose_arg;

va_start(ap, argv);
verbose_arg = va_arg(ap, char *);
va_end(ap);

flushroutes(AF_INET, AF_INET, verbose_arg != NULL);

return(0);
}
Expand Down
2 changes: 1 addition & 1 deletion externs.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ struct rtdump *getrtdump(int, int, int);
void freertdump(struct rtdump *);
int monitor(int, char **, ...);
int rtmsg(int, int, int, int, int);
void flushroutes(int, int);
void flushroutes(int, int, int);
void bprintf(FILE *, int, u_char *);
#ifdef _NET_IF_DL_H_
char *mylink_ntoa(const struct sockaddr_dl *);
Expand Down
21 changes: 12 additions & 9 deletions kroute.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ u_long rtm_inits;

char *mylink_ntoa(const struct sockaddr_dl *);

void flushroutes(int, int);
void print_rtmsg(struct rt_msghdr *);
void print_getmsg(struct rt_msghdr *, int);
void pmsg_common(struct rt_msghdr *);
Expand Down Expand Up @@ -147,7 +146,7 @@ freertdump(struct rtdump *rtdump)
* sockaddrs match requested address families
*/
void
flushroutes(int af, int af2)
flushroutes(int af, int af2, int flush_verbose)
{
int rlen, seqno, s;
char *next;
Expand Down Expand Up @@ -203,12 +202,13 @@ flushroutes(int af, int af2)
rlen = write(s, next, rtm->rtm_msglen);
if (rlen < (int)rtm->rtm_msglen) {
if (errno == ESRCH) {
printf("%% No such route to delete:");
print_addrs((char *)rtm + rtm->rtm_hdrlen,
rtm->rtm_addrs);
putchar('\n');
fflush(stdout);

if (flush_verbose) {
printf("%% No such route to delete:");
print_addrs((char *)rtm +
rtm->rtm_hdrlen, rtm->rtm_addrs);
putchar('\n');
fflush(stdout);
}
seqno++;
continue;
}
Expand All @@ -220,7 +220,7 @@ flushroutes(int af, int af2)
if (verbose) {
printf("\n%% Wrote message:\n");
print_rtmsg(rtm);
} else {
} else if (flush_verbose) {
printf("%% %-20.20s ", routename(sa));
printf("%-20.20s flushed\n", routename(sa2));
}
Expand All @@ -229,6 +229,9 @@ flushroutes(int af, int af2)
printf("\n");
if (!seqno)
printf("%% No entires found to flush\n");
else
printf("%% %d routing table entr%s flushed\n", seqno,
seqno == 1 ? "y" : "ies");
freertdump(rtdump);
close(s);
return;
Expand Down
10 changes: 8 additions & 2 deletions nsh.8
Original file line number Diff line number Diff line change
Expand Up @@ -3695,14 +3695,20 @@ nsh(p)/flush ?
.Ed
.Pp
.Tg route
.Ic flush routes
.Ic flush routes Oo Ic verbose Oc
.Pp
Clear the system routing table.
If the
.Ic verbose
flag is used then show which routes were deleted.
.Pp
.Tg arp
.Ic flush arp
.Ic flush arp Oo Ic verbose Oc
.Pp
Clear the system arp cache and static arp table.
If the
.Ic verbose
flag is used then show which ARP entries were deleted.
.Pp
.Tg ndp
.Ic flush ndp
Expand Down

0 comments on commit 726548d

Please sign in to comment.