From 05e8b8d68df87946abadcabb719ee3084748ffcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Thu, 22 Mar 2001 13:48:44 +0000 Subject: MFC: most changes (-W option, trimdomain -> libutil, queue.h cleanup) --- usr.bin/netstat/Makefile | 4 +-- usr.bin/netstat/if.c | 31 +++++++++++------------ usr.bin/netstat/inet.c | 26 ++++++++++++------- usr.bin/netstat/inet6.c | 2 +- usr.bin/netstat/main.c | 62 ++++++++++++++++++++++------------------------ usr.bin/netstat/netgraph.c | 2 +- usr.bin/netstat/netstat.1 | 18 ++++++++++---- usr.bin/netstat/netstat.h | 44 ++++++++++++++++---------------- usr.bin/netstat/route.c | 17 +++++++------ usr.bin/netstat/unix.c | 2 +- 10 files changed, 110 insertions(+), 98 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/netstat/Makefile b/usr.bin/netstat/Makefile index 7f88c3bfef64..c0a19a7c1671 100644 --- a/usr.bin/netstat/Makefile +++ b/usr.bin/netstat/Makefile @@ -11,8 +11,8 @@ CFLAGS+=-DIPSEC #.PATH: ${.CURDIR}/../../sys/netiso BINGRP= kmem BINMODE=2555 -DPADD= ${LIBKVM} ${LIBIPX} ${LIBNETGRAPH} -LDADD= -lkvm -lipx -lnetgraph +DPADD= ${LIBKVM} ${LIBIPX} ${LIBNETGRAPH} ${LIBUTIL} +LDADD= -lkvm -lipx -lnetgraph -lutil CFLAGS+=-DINET6 -DIPSEC .include diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c index af395df855e5..b611726585e3 100644 --- a/usr.bin/netstat/if.c +++ b/usr.bin/netstat/if.c @@ -204,7 +204,7 @@ intpr(interval, ifnetaddr, pfunc) } if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead)) return; - ifnetaddr = (u_long)ifnethead.tqh_first; + ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead); if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet)) return; @@ -241,7 +241,7 @@ intpr(interval, ifnetaddr, pfunc) kread((u_long)ifnet.if_name, tname, 16)) return; tname[15] = '\0'; - ifnetaddr = (u_long)ifnet.if_link.tqe_next; + ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link); snprintf(name, 32, "%s%d", tname, ifnet.if_unit); if (interface != 0 && (strcmp(name, interface) != 0)) continue; @@ -255,7 +255,7 @@ intpr(interval, ifnetaddr, pfunc) if ((ifnet.if_flags&IFF_UP) == 0) *cp++ = '*'; *cp = '\0'; - ifaddraddr = (u_long)ifnet.if_addrhead.tqh_first; + ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead); } printf("%-5.5s %-5lu ", name, ifnet.if_mtu); ifaddrfound = ifaddraddr; @@ -402,7 +402,7 @@ intpr(interval, ifnetaddr, pfunc) ibytes = ifaddr.in.ia_ifa.if_ibytes; } - ifaddraddr = (u_long)ifaddr.ifa.ifa_link.tqe_next; + ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link); } show_stat("lu", 8, ipackets, link_layer|network_layer); @@ -435,7 +435,7 @@ intpr(interval, ifnetaddr, pfunc) /* * Print family's multicast addresses */ - u_long multiaddr; + struct ifmultiaddr *multiaddr; struct ifmultiaddr ifma; union { struct sockaddr sa; @@ -447,10 +447,8 @@ intpr(interval, ifnetaddr, pfunc) } msa; const char *fmt; - for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first; - multiaddr; - multiaddr = (u_long)ifma.ifma_link.le_next) { - if (kread(multiaddr, (char *)&ifma, + LIST_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) { + if (kread(*(u_long *)multiaddr, (char *)&ifma, sizeof ifma)) break; if (kread((u_long)ifma.ifma_addr, (char *)&msa, @@ -492,7 +490,7 @@ intpr(interval, ifnetaddr, pfunc) } struct iftot { - struct iftot *ift_next; /* next element list*/ + SLIST_ENTRY(iftot) chain; char ift_name[16]; /* interface name */ u_long ift_ip; /* input packets */ u_long ift_ie; /* input errors */ @@ -528,7 +526,7 @@ sidewaysintpr(interval, off) if (kread(off, (char *)&ifnethead, sizeof ifnethead)) return; - firstifnet = (u_long)ifnethead.tqh_first; + firstifnet = (u_long)TAILQ_FIRST(&ifnethead); if ((iftot = malloc(sizeof(struct iftot))) == NULL) { printf("malloc failed\n"); @@ -557,9 +555,9 @@ sidewaysintpr(interval, off) exit(1); } memset(ipn, 0, sizeof(struct iftot)); - ip->ift_next = ipn; + SLIST_NEXT(ip, chain) = ipn; ip = ipn; - off = (u_long) ifnet.if_link.tqe_next; + off = (u_long)TAILQ_NEXT(&ifnet, if_link); } if ((total = malloc(sizeof(struct iftot))) == NULL) { printf("malloc failed\n"); @@ -624,8 +622,9 @@ loop: sum->ift_ob = 0; sum->ift_co = 0; sum->ift_dr = 0; - for (off = firstifnet, ip = iftot; off && ip->ift_next != NULL; - ip = ip->ift_next) { + for (off = firstifnet, ip = iftot; + off && SLIST_NEXT(ip, chain) != NULL; + ip = SLIST_NEXT(ip, chain)) { if (kread(off, (char *)&ifnet, sizeof ifnet)) { off = 0; continue; @@ -638,7 +637,7 @@ loop: sum->ift_ob += ifnet.if_obytes; sum->ift_co += ifnet.if_collisions; sum->ift_dr += ifnet.if_snd.ifq_drops; - off = (u_long) ifnet.if_link.tqe_next; + off = (u_long)TAILQ_NEXT(&ifnet, if_link); } if (!first) { printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu", diff --git a/usr.bin/netstat/inet.c b/usr.bin/netstat/inet.c index c14c7a202ad6..185506c7181c 100644 --- a/usr.bin/netstat/inet.c +++ b/usr.bin/netstat/inet.c @@ -72,6 +72,7 @@ static const char rcsid[] = #include #include #include +#include #include #include #include @@ -219,7 +220,7 @@ protopr(proto, name, af) printf("%-14.14s %-22.22s\n", "Listen", "Local Address"); else - printf(Aflag ? + printf((Aflag && !Wflag) ? "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" : "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n", "Proto", "Recv-Q", "Send-Q", @@ -243,7 +244,7 @@ protopr(proto, name, af) } else continue; else { - const u_char *vchar; + const char *vchar; #ifdef INET6 if ((inp->inp_vflag & INP_IPV6) != 0) @@ -476,6 +477,7 @@ udp_stats(off, name) p1a(udps_hdrops, "\t%lu with incomplete header\n"); p1a(udps_badlen, "\t%lu with bad data length field\n"); p1a(udps_badsum, "\t%lu with bad checksum\n"); + p1a(udps_nosum, "\t%lu with no checksum\n"); p1a(udps_noport, "\t%lu dropped due to no socket\n"); p(udps_noportbcast, "\t%lu broadcast/multicast datagram%s dropped due to no socket\n"); @@ -693,16 +695,22 @@ inetprint(in, port, proto,numeric) char line[80], *cp; int width; - sprintf(line, "%.*s.", (Aflag && !numeric) ? 12 : 16, inetname(in)); + if (Wflag) + sprintf(line, "%s.", inetname(in)); + else + sprintf(line, "%.*s.", (Aflag && !numeric) ? 12 : 16, inetname(in)); cp = index(line, '\0'); if (!numeric && port) sp = getservbyport((int)port, proto); if (sp || port == 0) - sprintf(cp, "%.15s", sp ? sp->s_name : "*"); + sprintf(cp, "%.15s ", sp ? sp->s_name : "*"); + else + sprintf(cp, "%d ", ntohs((u_short)port)); + width = (Aflag && !Wflag) ? 18 : 22; + if (Wflag) + printf("%-*s ", width, line); else - sprintf(cp, "%d", ntohs((u_short)port)); - width = Aflag ? 18 : 22; - printf("%-*.*s ", width, width, line); + printf("%-*.*s ", width, width, line); } /* @@ -715,7 +723,7 @@ inetname(inp) struct in_addr *inp; { register char *cp; - static char line[MAXHOSTNAMELEN + 1]; + static char line[MAXHOSTNAMELEN]; struct hostent *hp; struct netent *np; @@ -733,7 +741,7 @@ inetname(inp) hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); if (hp) { cp = hp->h_name; - trimdomain(cp); + trimdomain(cp, strlen(cp)); } } } diff --git a/usr.bin/netstat/inet6.c b/usr.bin/netstat/inet6.c index acdbaf0e67e6..50d57e192c06 100644 --- a/usr.bin/netstat/inet6.c +++ b/usr.bin/netstat/inet6.c @@ -1016,7 +1016,7 @@ inet6name(in6p) register char *cp; static char line[50]; struct hostent *hp; - static char domain[MAXHOSTNAMELEN + 1]; + static char domain[MAXHOSTNAMELEN]; static int first = 1; if (first && !nflag) { diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index 9c128aad4fdf..7a11793a3eec 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -290,7 +290,30 @@ static struct protox *name2protox __P((char *)); static struct protox *knownname __P((char *)); static kvm_t *kvmd; -char *nlistf = NULL, *memf = NULL; +static char *nlistf = NULL, *memf = NULL; + +int Aflag; /* show addresses of protocol control block */ +int aflag; /* show all sockets (including servers) */ +int bflag; /* show i/f total bytes in/out */ +int dflag; /* show i/f dropped packets */ +int gflag; /* show group (multicast) routing or stats */ +int iflag; /* show interfaces */ +int lflag; /* show routing table with use and ref */ +int Lflag; /* show size of listen queues */ +int mflag; /* show memory stats */ +int nflag; /* show addresses numerically */ +int pflag; /* show given protocol */ +int rflag; /* show routing tables (or routing stats) */ +int sflag; /* show protocol statistics */ +int tflag; /* show i/f watchdog timers */ +int Wflag; /* wide display */ + +int interval; /* repeat interval for i/f stats */ + +char *interface; /* desired i/f for stats, or NULL for all i/fs */ +int unit; /* unit number for above */ + +int af; /* address family */ int main(argc, argv) @@ -302,7 +325,7 @@ main(argc, argv) af = AF_UNSPEC; - while ((ch = getopt(argc, argv, "Aabdf:ghI:lLiM:mN:np:rstuw:")) != -1) + while ((ch = getopt(argc, argv, "Aabdf:ghI:lLiM:mN:np:rstuWw:")) != -1) switch(ch) { case 'A': Aflag = 1; @@ -402,6 +425,9 @@ main(argc, argv) case 'u': af = AF_UNIX; break; + case 'W': + Wflag = 1; + break; case 'w': interval = atoi(optarg); iflag = 1; @@ -696,39 +722,9 @@ static void usage() { (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", -"usage: netstat [-AaLln] [-f address_family] [-M core] [-N system]", +"usage: netstat [-AaLlnW] [-f address_family] [-M core] [-N system]", " netstat [-abdghilmnrs] [-f address_family] [-M core] [-N system]", " netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]", " netstat [-M core] [-N system] [-p protocol]"); exit(1); } - -void -trimdomain(cp) - char *cp; -{ - static char domain[MAXHOSTNAMELEN + 1]; - static int first = 1; - char *s; - - if (first) { - first = 0; - if (gethostname(domain, MAXHOSTNAMELEN) == 0 && - (s = strchr(domain, '.'))) - (void) strcpy(domain, s + 1); - else - domain[0] = 0; - } - - if (domain[0]) { - while ((cp = strchr(cp, '.'))) { - if (!strcasecmp(cp + 1, domain)) { - *cp = 0; /* hit it */ - break; - } else { - cp++; - } - } - } -} - diff --git a/usr.bin/netstat/netgraph.c b/usr.bin/netstat/netgraph.c index 82a2d87c37e4..17cc9a06bc6c 100644 --- a/usr.bin/netstat/netgraph.c +++ b/usr.bin/netstat/netgraph.c @@ -134,7 +134,7 @@ netgraphprotopr(u_long off, char *name) /* Read in ngpcb structure */ kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb)); - next = ngpcb.socks.le_next; + next = LIST_NEXT(&ngpcb, socks); /* Read in socket structure */ kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb)); diff --git a/usr.bin/netstat/netstat.1 b/usr.bin/netstat/netstat.1 index 2f4a02dd79d3..aef1cc737d67 100644 --- a/usr.bin/netstat/netstat.1 +++ b/usr.bin/netstat/netstat.1 @@ -40,7 +40,7 @@ .Nd show network status .Sh SYNOPSIS .Nm -.Op Fl AaLln +.Op Fl AaLlnW .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system @@ -100,9 +100,14 @@ With the default display, show the state of all sockets; normally sockets used by server processes are not shown. With the routing table display (option -.Fl r -, as described below), show protocol-cloned routes (routes -generated by a RTF_PRCLONING parent route); normally these routes are +.Fl r , +as described below), show cloned routes (routes +generated by a +parent route with either +.Dv RTF_CLONING +or +.Dv RTF_PRCLONING +flag set); normally these routes are not shown. .It Fl b With the interface display (option @@ -242,7 +247,7 @@ If this option is repeated, counters with a value of zero are suppressed. Show the routing tables. Use with .Fl a -to show protocol-cloned routes. +to show cloned routes. When .Fl s is also present, show routing statistics instead. @@ -251,6 +256,9 @@ When is also present, .Nm assumes more columns are there. +.It Fl W +In certain displays, avoid truncating addresses even if this causes +some fields to overflow. .It Fl w Ar wait Show network interface statistics at intervals of .Ar wait diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index 70bfbf0c6471..ef297d583670 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -36,32 +36,32 @@ #include -int Aflag; /* show addresses of protocol control block */ -int aflag; /* show all sockets (including servers) */ -int bflag; /* show i/f total bytes in/out */ -int dflag; /* show i/f dropped packets */ -int gflag; /* show group (multicast) routing or stats */ -int iflag; /* show interfaces */ -int lflag; /* show routing table with use and ref */ -int Lflag; /* show size of listen queues */ -int mflag; /* show memory stats */ -int nflag; /* show addresses numerically */ -int pflag; /* show given protocol */ -int rflag; /* show routing tables (or routing stats) */ -int sflag; /* show protocol statistics */ -int tflag; /* show i/f watchdog timers */ - -int interval; /* repeat interval for i/f stats */ - -char *interface; /* desired i/f for stats, or NULL for all i/fs */ -int unit; /* unit number for above */ - -int af; /* address family */ +extern int Aflag; /* show addresses of protocol control block */ +extern int aflag; /* show all sockets (including servers) */ +extern int bflag; /* show i/f total bytes in/out */ +extern int dflag; /* show i/f dropped packets */ +extern int gflag; /* show group (multicast) routing or stats */ +extern int iflag; /* show interfaces */ +extern int lflag; /* show routing table with use and ref */ +extern int Lflag; /* show size of listen queues */ +extern int mflag; /* show memory stats */ +extern int nflag; /* show addresses numerically */ +extern int pflag; /* show given protocol */ +extern int rflag; /* show routing tables (or routing stats) */ +extern int sflag; /* show protocol statistics */ +extern int tflag; /* show i/f watchdog timers */ +extern int Wflag; /* wide display */ + +extern int interval; /* repeat interval for i/f stats */ + +extern char *interface; /* desired i/f for stats, or NULL for all i/fs */ +extern int unit; /* unit number for above */ + +extern int af; /* address family */ int kread __P((u_long addr, char *buf, int size)); char *plural __P((int)); char *plurales __P((int)); -void trimdomain __P((char *)); void protopr __P((u_long, char *, int)); void tcp_stats __P((u_long, char *)); diff --git a/usr.bin/netstat/route.c b/usr.bin/netstat/route.c index 0a52e56d4f1d..7e4b69d857d8 100644 --- a/usr.bin/netstat/route.c +++ b/usr.bin/netstat/route.c @@ -62,6 +62,7 @@ static const char rcsid[] = #include #include +#include #include #include #include @@ -587,9 +588,9 @@ p_rtentry(rt) sa_u addr, mask; /* - * Don't print protocol-cloned routes unless -a. + * Don't print cloned routes unless -a. */ - if (rt->rt_parent && !aflag) + if (rt->rt_flags & RTF_WASCLONED && !aflag) return; bzero(&addr, sizeof(addr)); @@ -637,7 +638,7 @@ routename(in) u_long in; { register char *cp; - static char line[MAXHOSTNAMELEN + 1]; + static char line[MAXHOSTNAMELEN]; struct hostent *hp; cp = 0; @@ -646,7 +647,7 @@ routename(in) AF_INET); if (hp) { cp = hp->h_name; - trimdomain(cp); + trimdomain(cp, strlen(cp)); } } if (cp) { @@ -715,7 +716,7 @@ netname(in, mask) u_long in, mask; { char *cp = 0; - static char line[MAXHOSTNAMELEN + 1]; + static char line[MAXHOSTNAMELEN]; struct netent *np = 0; u_long net, omask, dmask; register u_long i; @@ -729,7 +730,7 @@ netname(in, mask) np = getnetbyaddr(net, AF_INET); if (np) { cp = np->n_name; - trimdomain(cp); + trimdomain(cp, strlen(cp)); } } if (cp) @@ -753,7 +754,7 @@ netname6(sa6, mask) struct sockaddr_in6 *sa6; struct in6_addr *mask; { - static char line[MAXHOSTNAMELEN + 1]; + static char line[MAXHOSTNAMELEN]; u_char *p = (u_char *)mask; u_char *lim; int masklen, illegal = 0, flag = NI_WITHSCOPEID; @@ -816,7 +817,7 @@ char * routename6(sa6) struct sockaddr_in6 *sa6; { - static char line[MAXHOSTNAMELEN + 1]; + static char line[MAXHOSTNAMELEN]; int flag = NI_WITHSCOPEID; /* use local variable for safety */ struct sockaddr_in6 sa6_local = {AF_INET6, sizeof(sa6_local),}; diff --git a/usr.bin/netstat/unix.c b/usr.bin/netstat/unix.c index 345e28a4ee41..8941a2639370 100644 --- a/usr.bin/netstat/unix.c +++ b/usr.bin/netstat/unix.c @@ -152,7 +152,7 @@ unixdomainpr(xunp, so) (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc, (long)unp->unp_vnode, (long)unp->unp_conn, - (long)unp->unp_refs.lh_first, (long)unp->unp_reflink.le_next); + (long)LIST_FIRST(&unp->unp_refs), (long)LIST_NEXT(unp, unp_reflink)); if (sa) printf(" %.*s", (int)(sa->sun_len - offsetof(struct sockaddr_un, sun_path)), -- cgit v1.2.3