summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--usr.bin/netstat/main.c40
-rw-r--r--usr.bin/netstat/mbuf.c5
-rw-r--r--usr.bin/netstat/route.c50
3 files changed, 62 insertions, 33 deletions
diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c
index deed78b..ac2287d 100644
--- a/usr.bin/netstat/main.c
+++ b/usr.bin/netstat/main.c
@@ -59,6 +59,7 @@ static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <err.h>
#include "netstat.h"
struct nlist nl[] = {
@@ -195,6 +196,7 @@ main(argc, argv)
int ch;
char *nlistf = NULL, *memf = NULL;
char buf[_POSIX2_LINE_MAX];
+ char buf2[_POSIX2_LINE_MAX];
if (cp = rindex(argv[0], '/'))
prog = cp + 1;
@@ -226,10 +228,7 @@ main(argc, argv)
else if (strcmp(optarg, "iso") == 0)
af = AF_ISO;
else {
- (void)fprintf(stderr,
- "%s: %s: unknown address family\n",
- prog, optarg);
- exit(1);
+ errx(1, "%s: unknown address family", optarg);
}
break;
case 'g':
@@ -262,10 +261,9 @@ main(argc, argv)
break;
case 'p':
if ((tp = name2protox(optarg)) == NULL) {
- (void)fprintf(stderr,
- "%s: %s: unknown or uninstrumented protocol\n",
- prog, optarg);
- exit(1);
+ errx(1,
+ "%s: unknown or uninstrumented protocol",
+ optarg);
}
pflag = 1;
break;
@@ -317,16 +315,22 @@ main(argc, argv)
if (nlistf != NULL || memf != NULL)
setgid(getgid());
- if ((kvmd = kvm_open(nlistf, memf, NULL, O_RDONLY, prog)) == NULL) {
- fprintf(stderr, "%s: kvm_open: %s\n", prog, buf);
- exit(1);
+ kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
+ if (kvmd == NULL) {
+ errx(1, "kvm_open: %s", buf);
}
- if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
- if (nlistf)
- fprintf(stderr, "%s: %s: no namelist\n", prog, nlistf);
+ if (kvm_nlist(kvmd, nl) < 0) {
+ if(nlistf)
+ errx(1, "%s: kvm_nlist: %s", nlistf, kvm_geterr(kvmd));
else
- fprintf(stderr, "%s: no namelist\n", prog);
- exit(1);
+ errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
+ }
+
+ if (nl[0].n_type == 0) {
+ if(nlistf)
+ errx(1, "%s: no namelist", nlistf);
+ else
+ errx(1, "no namelist");
}
if (mflag) {
mbpr(nl[N_MBSTAT].n_value);
@@ -427,9 +431,7 @@ kread(addr, buf, size)
{
if (kvm_read(kvmd, addr, buf, size) != size) {
- /* XXX this duplicates kvm_read's error printout */
- (void)fprintf(stderr, "%s: kvm_read %s\n", prog,
- kvm_geterr(kvmd));
+ warnx("kvm_read: %s", kvm_geterr(kvmd));
return (-1);
}
return (0);
diff --git a/usr.bin/netstat/mbuf.c b/usr.bin/netstat/mbuf.c
index dd60a0e..157c4f6 100644
--- a/usr.bin/netstat/mbuf.c
+++ b/usr.bin/netstat/mbuf.c
@@ -84,12 +84,11 @@ mbpr(mbaddr)
register struct mbtypes *mp;
if (nmbtypes != 256) {
- fprintf(stderr,
- "%s: unexpected change to mbstat; check source\n", prog);
+ warnx("unexpected change to mbstat; check source");
return;
}
if (mbaddr == 0) {
- fprintf(stderr, "%s: mbstat: symbol not in namelist\n", prog);
+ warnx("mbstat: symbol not in namelist");
return;
}
if (kread(mbaddr, (char *)&mbstat, sizeof (mbstat)))
diff --git a/usr.bin/netstat/route.c b/usr.bin/netstat/route.c
index f220e5e..57e0c84 100644
--- a/usr.bin/netstat/route.c
+++ b/usr.bin/netstat/route.c
@@ -32,7 +32,11 @@
*/
#ifndef lint
-static char sccsid[] = "@(#)route.c 8.3 (Berkeley) 3/9/94";
+#if 0
+static char sccsid[] = "From: @(#)route.c 8.3 (Berkeley) 3/9/94";
+#endif
+static const char rcsid[] =
+ "$Id$";
#endif /* not lint */
#include <sys/param.h>
@@ -57,6 +61,8 @@ static char sccsid[] = "@(#)route.c 8.3 (Berkeley) 3/9/94";
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <err.h>
+#include <time.h>
#include "netstat.h"
#define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
@@ -194,10 +200,10 @@ pr_rthdr()
if (Aflag)
printf("%-8.8s ","Address");
- printf("%-*.*s %-*.*s %-6.6s %6.6s%8.8s %s\n",
+ printf("%-*.*s %-*.*s %-6.6s %6.6s%8.8s %8.8s %6s\n",
WID_DST, WID_DST, "Destination",
WID_GW, WID_GW, "Gateway",
- "Flags", "Refs", "Use", "Interface");
+ "Flags", "Refs", "Use", "Netif", "Expire");
}
static struct sockaddr *
@@ -293,12 +299,16 @@ ntreestuff()
mib[3] = 0;
mib[4] = NET_RT_DUMP;
mib[5] = 0;
- if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
- { perror("route-sysctl-estimate"); exit(1);}
- if ((buf = malloc(needed)) == 0)
- { printf("out of space\n"); exit(1);}
- if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
- { perror("sysctl of routing table"); exit(1);}
+ if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
+ err(1, "sysctl: net.route.0.0.dump estimate");
+ }
+
+ if ((buf = malloc(needed)) == 0) {
+ err(2, "malloc(%lu)", (unsigned long)needed);
+ }
+ if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
+ err(1, "sysctl: net.route.0.0.dump");
+ }
lim = buf + needed;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
@@ -447,6 +457,13 @@ p_rtentry(rt)
{
static struct ifnet ifnet, *lastif;
static char name[16];
+ static char prettyname[9];
+
+ /*
+ * Don't print protocol-cloned routes unless -a.
+ */
+ if(rt->rt_parent && !aflag)
+ return;
p_sockaddr(kgetsa(rt_key(rt)), rt->rt_flags, WID_DST);
p_sockaddr(kgetsa(rt->rt_gateway), RTF_HOST, WID_GW);
@@ -457,9 +474,20 @@ p_rtentry(rt)
kget(rt->rt_ifp, ifnet);
kread((u_long)ifnet.if_name, name, 16);
lastif = rt->rt_ifp;
+ snprintf(prettyname, sizeof prettyname,
+ "%.6s%d", name, ifnet.if_unit);
}
- printf(" %.15s%d%s", name, ifnet.if_unit,
- rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
+ if(rt->rt_rmx.rmx_expire) {
+ time_t now = time((time_t *)0);
+
+ printf(" %8.8s %6d%s", prettyname,
+ rt->rt_rmx.rmx_expire - now,
+ rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
+ } else {
+ printf(" %8.8s%s", prettyname,
+ rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
+ }
+
}
putchar('\n');
}
OpenPOWER on IntegriCloud