netstat.c revision fa1f2b928010b69e6b1c78b220d8285e3145eeeb
17c478bdstevel@tonic-gate/*-
27c478bdstevel@tonic-gate * Copyright (c) 1980, 1992, 1993
37c478bdstevel@tonic-gate *	The Regents of the University of California.  All rights reserved.
47c478bdstevel@tonic-gate *
57c478bdstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
67c478bdstevel@tonic-gate * modification, are permitted provided that the following conditions
77c478bdstevel@tonic-gate * are met:
87c478bdstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
97c478bdstevel@tonic-gate *    notice, this list of conditions and the following disclaimer.
107c478bdstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
117c478bdstevel@tonic-gate *    notice, this list of conditions and the following disclaimer in the
127c478bdstevel@tonic-gate *    documentation and/or other materials provided with the distribution.
137c478bdstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
147c478bdstevel@tonic-gate *    must display the following acknowledgement:
157c478bdstevel@tonic-gate *	This product includes software developed by the University of
167c478bdstevel@tonic-gate *	California, Berkeley and its contributors.
177c478bdstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
187c478bdstevel@tonic-gate *    may be used to endorse or promote products derived from this software
197c478bdstevel@tonic-gate *    without specific prior written permission.
207c478bdstevel@tonic-gate *
217c478bdstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bdstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bdstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bdstevel@tonic-gate * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bdstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bdstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bdstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bdstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bdstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bdstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bdstevel@tonic-gate * SUCH DAMAGE.
327c478bdstevel@tonic-gate */
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gate#include <sys/cdefs.h>
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gate__FBSDID("$FreeBSD$");
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#ifdef lint
397c478bdstevel@tonic-gatestatic const char sccsid[] = "@(#)netstat.c	8.1 (Berkeley) 6/6/93";
407c478bdstevel@tonic-gate#endif
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate/*
437c478bdstevel@tonic-gate * netstat
447c478bdstevel@tonic-gate */
457c478bdstevel@tonic-gate#include <sys/param.h>
467c478bdstevel@tonic-gate#include <sys/queue.h>
477c478bdstevel@tonic-gate#include <sys/socket.h>
487c478bdstevel@tonic-gate#include <sys/socketvar.h>
497c478bdstevel@tonic-gate#include <sys/protosw.h>
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gate#include <netinet/in.h>
527c478bdstevel@tonic-gate#include <arpa/inet.h>
537c478bdstevel@tonic-gate#include <net/route.h>
547c478bdstevel@tonic-gate#include <netinet/in_systm.h>
557c478bdstevel@tonic-gate#include <netinet/ip.h>
567c478bdstevel@tonic-gate#include <netinet/in_pcb.h>
577c478bdstevel@tonic-gate#include <netinet/ip_icmp.h>
587c478bdstevel@tonic-gate#include <netinet/icmp_var.h>
597c478bdstevel@tonic-gate#include <netinet/ip_var.h>
607c478bdstevel@tonic-gate#include <netinet/tcp.h>
617c478bdstevel@tonic-gate#include <netinet/tcpip.h>
627c478bdstevel@tonic-gate#include <netinet/tcp_seq.h>
637c478bdstevel@tonic-gate#include <netinet/tcp_var.h>
647c478bdstevel@tonic-gate#define TCPSTATES
657c478bdstevel@tonic-gate#include <netinet/tcp_fsm.h>
667c478bdstevel@tonic-gate#include <netinet/tcp_timer.h>
677c478bdstevel@tonic-gate#include <netinet/tcp_var.h>
687c478bdstevel@tonic-gate#include <netinet/tcp_debug.h>
697c478bdstevel@tonic-gate#include <netinet/udp.h>
707c478bdstevel@tonic-gate#include <netinet/udp_var.h>
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate#include <netdb.h>
737c478bdstevel@tonic-gate#include <nlist.h>
747c478bdstevel@tonic-gate#include <paths.h>
757c478bdstevel@tonic-gate#include <stdlib.h>
767c478bdstevel@tonic-gate#include <string.h>
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate#include "systat.h"
797c478bdstevel@tonic-gate#include "extern.h"
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gatestatic struct netinfo *enter __P((struct inpcb *, int, const char *));
827c478bdstevel@tonic-gatestatic void enter_kvm __P((struct inpcb *, struct socket *, int, const char *));
837c478bdstevel@tonic-gatestatic void enter_sysctl __P((struct inpcb *, struct xsocket *, int, const char *));
847c478bdstevel@tonic-gatestatic void fetchnetstat_kvm __P((void));
857c478bdstevel@tonic-gatestatic void fetchnetstat_sysctl __P((void));
867c478bdstevel@tonic-gatestatic char *inetname __P((struct in_addr));
877c478bdstevel@tonic-gatestatic void inetprint __P((struct in_addr *, int, const char *));
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate#define	streq(a,b)	(strcmp(a,b)==0)
907c478bdstevel@tonic-gate#define	YMAX(w)		((w)->_maxy-1)
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gateWINDOW *
937c478bdstevel@tonic-gateopennetstat()
947c478bdstevel@tonic-gate{
957c478bdstevel@tonic-gate	sethostent(1);
967c478bdstevel@tonic-gate	setnetent(1);
977c478bdstevel@tonic-gate	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
987c478bdstevel@tonic-gate}
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gatestruct netinfo {
1017c478bdstevel@tonic-gate	TAILQ_ENTRY(netinfo) chain;
1027c478bdstevel@tonic-gate	short	ni_line;		/* line on screen */
1037c478bdstevel@tonic-gate	short	ni_seen;		/* 0 when not present in list */
1047c478bdstevel@tonic-gate	short	ni_flags;
1057c478bdstevel@tonic-gate#define	NIF_LACHG	0x1		/* local address changed */
1067c478bdstevel@tonic-gate#define	NIF_FACHG	0x2		/* foreign address changed */
1077c478bdstevel@tonic-gate	short	ni_state;		/* tcp state */
1087c478bdstevel@tonic-gate	const char	*ni_proto;		/* protocol */
1097c478bdstevel@tonic-gate	struct	in_addr ni_laddr;	/* local address */
1107c478bdstevel@tonic-gate	long	ni_lport;		/* local port */
1117c478bdstevel@tonic-gate	struct	in_addr	ni_faddr;	/* foreign address */
1127c478bdstevel@tonic-gate	long	ni_fport;		/* foreign port */
1137c478bdstevel@tonic-gate	long	ni_rcvcc;		/* rcv buffer character count */
1147c478bdstevel@tonic-gate	long	ni_sndcc;		/* snd buffer character count */
1157c478bdstevel@tonic-gate};
1167c478bdstevel@tonic-gate
1177c478bdstevel@tonic-gateTAILQ_HEAD(netinfohead, netinfo) netcb = TAILQ_HEAD_INITIALIZER(netcb);
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gatestatic	int aflag = 0;
1207c478bdstevel@tonic-gatestatic	int nflag = 0;
1217c478bdstevel@tonic-gatestatic	int lastrow = 1;
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gatevoid
1247c478bdstevel@tonic-gateclosenetstat(w)
1257c478bdstevel@tonic-gate        WINDOW *w;
1267c478bdstevel@tonic-gate{
1277c478bdstevel@tonic-gate	struct netinfo *p;
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate	endhostent();
1307c478bdstevel@tonic-gate	endnetent();
1317c478bdstevel@tonic-gate	TAILQ_FOREACH(p, &netcb, chain) {
1327c478bdstevel@tonic-gate		if (p->ni_line != -1)
1337c478bdstevel@tonic-gate			lastrow--;
1347c478bdstevel@tonic-gate		p->ni_line = -1;
1357c478bdstevel@tonic-gate	}
1367c478bdstevel@tonic-gate        if (w != NULL) {
1377c478bdstevel@tonic-gate		wclear(w);
1387c478bdstevel@tonic-gate		wrefresh(w);
1397c478bdstevel@tonic-gate		delwin(w);
1407c478bdstevel@tonic-gate	}
1417c478bdstevel@tonic-gate}
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gatestatic const char *miblist[] = {
1447c478bdstevel@tonic-gate	"net.inet.tcp.pcblist",
1457c478bdstevel@tonic-gate	"net.inet.udp.pcblist"
1467c478bdstevel@tonic-gate};
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gatestruct nlist namelist[] = {
1497c478bdstevel@tonic-gate#define	X_TCB	0
1507c478bdstevel@tonic-gate	{ "tcb" },
1517c478bdstevel@tonic-gate#define	X_UDB	1
1527c478bdstevel@tonic-gate	{ "udb" },
1537c478bdstevel@tonic-gate	{ "" },
1547c478bdstevel@tonic-gate};
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gateint
1577c478bdstevel@tonic-gateinitnetstat()
1587c478bdstevel@tonic-gate{
1597c478bdstevel@tonic-gate	protos = TCP|UDP;
1607c478bdstevel@tonic-gate	return(1);
1617c478bdstevel@tonic-gate}
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gatevoid
1647c478bdstevel@tonic-gatefetchnetstat()
1657c478bdstevel@tonic-gate{
1667c478bdstevel@tonic-gate	if (use_kvm)
1677c478bdstevel@tonic-gate		fetchnetstat_kvm();
1687c478bdstevel@tonic-gate	else
1697c478bdstevel@tonic-gate		fetchnetstat_sysctl();
1707c478bdstevel@tonic-gate}
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gatestatic void
1737c478bdstevel@tonic-gatefetchnetstat_kvm()
1747c478bdstevel@tonic-gate{
1757c478bdstevel@tonic-gate	struct inpcb *next;
1767c478bdstevel@tonic-gate	struct netinfo *p;
1777c478bdstevel@tonic-gate	struct inpcbhead head;
1787c478bdstevel@tonic-gate	struct inpcb inpcb;
1797c478bdstevel@tonic-gate	struct socket sockb;
1807c478bdstevel@tonic-gate	struct tcpcb tcpcb;
1817c478bdstevel@tonic-gate	void *off;
1827c478bdstevel@tonic-gate	int istcp;
1837c478bdstevel@tonic-gate
1847c478bdstevel@tonic-gate	if (namelist[X_TCB].n_value == 0)
1857c478bdstevel@tonic-gate		return;
1867c478bdstevel@tonic-gate	TAILQ_FOREACH(p, &netcb, chain)
1877c478bdstevel@tonic-gate		p->ni_seen = 0;
1887c478bdstevel@tonic-gate	if (protos&TCP) {
1897c478bdstevel@tonic-gate		off = NPTR(X_TCB);
1907c478bdstevel@tonic-gate		istcp = 1;
1917c478bdstevel@tonic-gate	}
1927c478bdstevel@tonic-gate	else if (protos&UDP) {
1937c478bdstevel@tonic-gate		off = NPTR(X_UDB);
1947c478bdstevel@tonic-gate		istcp = 0;
1957c478bdstevel@tonic-gate	}
1967c478bdstevel@tonic-gate	else {
1977c478bdstevel@tonic-gate		error("No protocols to display");
1987c478bdstevel@tonic-gate		return;
1997c478bdstevel@tonic-gate	}
2007c478bdstevel@tonic-gateagain:
2017c478bdstevel@tonic-gate	KREAD(off, &head, sizeof (struct inpcbhead));
2027c478bdstevel@tonic-gate	LIST_FOREACH(next, &head, inp_list) {
2037c478bdstevel@tonic-gate		KREAD(next, &inpcb, sizeof (inpcb));
2047c478bdstevel@tonic-gate		next = &inpcb;
2057c478bdstevel@tonic-gate		if (!aflag && inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
2067c478bdstevel@tonic-gate			continue;
2077c478bdstevel@tonic-gate		if (nhosts && !checkhost(&inpcb))
2087c478bdstevel@tonic-gate			continue;
2097c478bdstevel@tonic-gate		if (nports && !checkport(&inpcb))
2107c478bdstevel@tonic-gate			continue;
2117c478bdstevel@tonic-gate		KREAD(inpcb.inp_socket, &sockb, sizeof (sockb));
2127c478bdstevel@tonic-gate		if (istcp) {
2137c478bdstevel@tonic-gate			KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
2147c478bdstevel@tonic-gate			enter_kvm(&inpcb, &sockb, tcpcb.t_state, "tcp");
2157c478bdstevel@tonic-gate		} else
2167c478bdstevel@tonic-gate			enter_kvm(&inpcb, &sockb, 0, "udp");
2177c478bdstevel@tonic-gate	}
2187c478bdstevel@tonic-gate	if (istcp && (protos&UDP)) {
2197c478bdstevel@tonic-gate		istcp = 0;
2207c478bdstevel@tonic-gate		off = NPTR(X_UDB);
2217c478bdstevel@tonic-gate		goto again;
2227c478bdstevel@tonic-gate	}
2237c478bdstevel@tonic-gate}
2247c478bdstevel@tonic-gate
2257c478bdstevel@tonic-gatestatic void
2267c478bdstevel@tonic-gatefetchnetstat_sysctl()
2277c478bdstevel@tonic-gate{
2287c478bdstevel@tonic-gate	struct netinfo *p;
2297c478bdstevel@tonic-gate	int idx;
2307c478bdstevel@tonic-gate	struct xinpgen *inpg;
2317c478bdstevel@tonic-gate	char *cur, *end;
2327c478bdstevel@tonic-gate	struct inpcb *inpcb;
2337c478bdstevel@tonic-gate	struct xinpcb *xip;
2347c478bdstevel@tonic-gate	struct xtcpcb *xtp;
2357c478bdstevel@tonic-gate	int plen;
2367c478bdstevel@tonic-gate	size_t lsz;
2377c478bdstevel@tonic-gate
2387c478bdstevel@tonic-gate	TAILQ_FOREACH(p, &netcb, chain)
2397c478bdstevel@tonic-gate		p->ni_seen = 0;
2407c478bdstevel@tonic-gate	if (protos&TCP) {
2417c478bdstevel@tonic-gate		idx = 0;
2427c478bdstevel@tonic-gate	} else if (protos&UDP) {
2437c478bdstevel@tonic-gate		idx = 1;
2447c478bdstevel@tonic-gate	} else {
2457c478bdstevel@tonic-gate		error("No protocols to display");
2467c478bdstevel@tonic-gate		return;
2477c478bdstevel@tonic-gate	}
2487c478bdstevel@tonic-gate
2497c478bdstevel@tonic-gate	for (;idx < 2; idx++) {
2507c478bdstevel@tonic-gate		if (idx == 1 && !(protos&UDP))
2517c478bdstevel@tonic-gate			break;
2527c478bdstevel@tonic-gate		inpg = (struct xinpgen *)sysctl_dynread(miblist[idx], &lsz);
2537c478bdstevel@tonic-gate		if (inpg == NULL) {
2547c478bdstevel@tonic-gate			error("sysctl(%s...) failed", miblist[idx]);
2557c478bdstevel@tonic-gate			continue;
2567c478bdstevel@tonic-gate		}
2577c478bdstevel@tonic-gate		/*
2587c478bdstevel@tonic-gate		 * We currently do no require a consistent pcb list.
2597c478bdstevel@tonic-gate		 * Try to be robust in case of struct size changes
2607c478bdstevel@tonic-gate		 */
2617c478bdstevel@tonic-gate		cur = ((char *)inpg) + inpg->xig_len;
2627c478bdstevel@tonic-gate		/* There is also a trailing struct xinpgen */
2637c478bdstevel@tonic-gate		end = ((char *)inpg) + lsz - inpg->xig_len;
2647c478bdstevel@tonic-gate		if (end <= cur) {
2657c478bdstevel@tonic-gate			free(inpg);
2667c478bdstevel@tonic-gate			continue;
2677c478bdstevel@tonic-gate		}
2687c478bdstevel@tonic-gate		if (idx == 0) { /* TCP */
2697c478bdstevel@tonic-gate			xtp = (struct xtcpcb *)cur;
2707c478bdstevel@tonic-gate			plen = xtp->xt_len;
2717c478bdstevel@tonic-gate		} else {
2727c478bdstevel@tonic-gate			xip = (struct xinpcb *)cur;
2737c478bdstevel@tonic-gate			plen = xip->xi_len;
2747c478bdstevel@tonic-gate		}
2757c478bdstevel@tonic-gate		while (cur + plen <= end) {
2767c478bdstevel@tonic-gate			if (idx == 0) { /* TCP */
2777c478bdstevel@tonic-gate				xtp = (struct xtcpcb *)cur;
2787c478bdstevel@tonic-gate				inpcb = &xtp->xt_inp;
2797c478bdstevel@tonic-gate			} else {
2807c478bdstevel@tonic-gate				xip = (struct xinpcb *)cur;
2817c478bdstevel@tonic-gate				inpcb = &xip->xi_inp;
2827c478bdstevel@tonic-gate			}
2837c478bdstevel@tonic-gate			cur += plen;
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate			if (!aflag && inet_lnaof(inpcb->inp_laddr) ==
2867c478bdstevel@tonic-gate			    INADDR_ANY)
2877c478bdstevel@tonic-gate				continue;
2887c478bdstevel@tonic-gate			if (nhosts && !checkhost(inpcb))
2897c478bdstevel@tonic-gate				continue;
2907c478bdstevel@tonic-gate			if (nports && !checkport(inpcb))
2917c478bdstevel@tonic-gate				continue;
2927c478bdstevel@tonic-gate			if (idx == 0)	/* TCP */
2937c478bdstevel@tonic-gate				enter_sysctl(inpcb, &xtp->xt_socket,
2947c478bdstevel@tonic-gate				    xtp->xt_tp.t_state, "tcp");
2957c478bdstevel@tonic-gate			else		/* UDP */
2967c478bdstevel@tonic-gate				enter_sysctl(inpcb, &xip->xi_socket, 0, "udp");
2977c478bdstevel@tonic-gate		}
2987c478bdstevel@tonic-gate		free(inpg);
2997c478bdstevel@tonic-gate	}
3007c478bdstevel@tonic-gate}
3017c478bdstevel@tonic-gate
3027c478bdstevel@tonic-gatestatic void
3037c478bdstevel@tonic-gateenter_kvm(inp, so, state, proto)
3047c478bdstevel@tonic-gate	struct inpcb *inp;
3057c478bdstevel@tonic-gate	struct socket *so;
3067c478bdstevel@tonic-gate	int state;
3077c478bdstevel@tonic-gate	const char *proto;
3087c478bdstevel@tonic-gate{
3097c478bdstevel@tonic-gate	struct netinfo *p;
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate	if ((p = enter(inp, state, proto)) != NULL) {
3127c478bdstevel@tonic-gate		p->ni_rcvcc = so->so_rcv.sb_cc;
3137c478bdstevel@tonic-gate		p->ni_sndcc = so->so_snd.sb_cc;
3147c478bdstevel@tonic-gate	}
3157c478bdstevel@tonic-gate}
3167c478bdstevel@tonic-gate
3177c478bdstevel@tonic-gatestatic void
3187c478bdstevel@tonic-gateenter_sysctl(inp, so, state, proto)
3197c478bdstevel@tonic-gate	struct inpcb *inp;
3207c478bdstevel@tonic-gate	struct xsocket *so;
3217c478bdstevel@tonic-gate	int state;
3227c478bdstevel@tonic-gate	const char *proto;
3237c478bdstevel@tonic-gate{
3247c478bdstevel@tonic-gate	struct netinfo *p;
3257c478bdstevel@tonic-gate
3267c478bdstevel@tonic-gate	if ((p = enter(inp, state, proto)) != NULL) {
3277c478bdstevel@tonic-gate		p->ni_rcvcc = so->so_rcv.sb_cc;
328		p->ni_sndcc = so->so_snd.sb_cc;
329	}
330}
331
332
333static struct netinfo *
334enter(inp, state, proto)
335	struct inpcb *inp;
336	int state;
337	const char *proto;
338{
339	struct netinfo *p;
340
341	/*
342	 * Only take exact matches, any sockets with
343	 * previously unbound addresses will be deleted
344	 * below in the display routine because they
345	 * will appear as ``not seen'' in the kernel
346	 * data structures.
347	 */
348	TAILQ_FOREACH(p, &netcb, chain) {
349		if (!streq(proto, p->ni_proto))
350			continue;
351		if (p->ni_lport != inp->inp_lport ||
352		    p->ni_laddr.s_addr != inp->inp_laddr.s_addr)
353			continue;
354		if (p->ni_faddr.s_addr == inp->inp_faddr.s_addr &&
355		    p->ni_fport == inp->inp_fport)
356			break;
357	}
358	if (p == NULL) {
359		if ((p = malloc(sizeof(*p))) == NULL) {
360			error("Out of memory");
361			return NULL;
362		}
363		TAILQ_INSERT_HEAD(&netcb, p, chain);
364		p->ni_line = -1;
365		p->ni_laddr = inp->inp_laddr;
366		p->ni_lport = inp->inp_lport;
367		p->ni_faddr = inp->inp_faddr;
368		p->ni_fport = inp->inp_fport;
369		p->ni_proto = strdup(proto);
370		p->ni_flags = NIF_LACHG|NIF_FACHG;
371	}
372	p->ni_state = state;
373	p->ni_seen = 1;
374	return p;
375}
376
377/* column locations */
378#define	LADDR	0
379#define	FADDR	LADDR+23
380#define	PROTO	FADDR+23
381#define	RCVCC	PROTO+6
382#define	SNDCC	RCVCC+7
383#define	STATE	SNDCC+7
384
385
386void
387labelnetstat()
388{
389	if (use_kvm && namelist[X_TCB].n_type == 0)
390		return;
391	wmove(wnd, 0, 0); wclrtobot(wnd);
392	mvwaddstr(wnd, 0, LADDR, "Local Address");
393	mvwaddstr(wnd, 0, FADDR, "Foreign Address");
394	mvwaddstr(wnd, 0, PROTO, "Proto");
395	mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
396	mvwaddstr(wnd, 0, SNDCC, "Send-Q");
397	mvwaddstr(wnd, 0, STATE, "(state)");
398}
399
400void
401shownetstat()
402{
403	struct netinfo *p, *q;
404
405	/*
406	 * First, delete any connections that have gone
407	 * away and adjust the position of connections
408	 * below to reflect the deleted line.
409	 */
410	p = TAILQ_FIRST(&netcb);
411	while (p != NULL) {
412		if (p->ni_line == -1 || p->ni_seen) {
413			p = TAILQ_NEXT(p, chain);
414			continue;
415		}
416		wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
417		TAILQ_FOREACH(q, &netcb, chain)
418			if (q != p && q->ni_line > p->ni_line) {
419				q->ni_line--;
420				/* this shouldn't be necessary */
421				q->ni_flags |= NIF_LACHG|NIF_FACHG;
422			}
423		lastrow--;
424		q = TAILQ_NEXT(p, chain);
425		TAILQ_REMOVE(&netcb, p, chain);
426		free(p);
427		p = q;
428	}
429	/*
430	 * Update existing connections and add new ones.
431	 */
432	TAILQ_FOREACH(p, &netcb, chain) {
433		if (p->ni_line == -1) {
434			/*
435			 * Add a new entry if possible.
436			 */
437			if (lastrow > YMAX(wnd))
438				continue;
439			p->ni_line = lastrow++;
440			p->ni_flags |= NIF_LACHG|NIF_FACHG;
441		}
442		if (p->ni_flags & NIF_LACHG) {
443			wmove(wnd, p->ni_line, LADDR);
444			inetprint(&p->ni_laddr, p->ni_lport, p->ni_proto);
445			p->ni_flags &= ~NIF_LACHG;
446		}
447		if (p->ni_flags & NIF_FACHG) {
448			wmove(wnd, p->ni_line, FADDR);
449			inetprint(&p->ni_faddr, p->ni_fport, p->ni_proto);
450			p->ni_flags &= ~NIF_FACHG;
451		}
452		mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto);
453		mvwprintw(wnd, p->ni_line, RCVCC, "%6d", p->ni_rcvcc);
454		mvwprintw(wnd, p->ni_line, SNDCC, "%6d", p->ni_sndcc);
455		if (streq(p->ni_proto, "tcp")) {
456			if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
457				mvwprintw(wnd, p->ni_line, STATE, "%d",
458				    p->ni_state);
459			else
460				mvwaddstr(wnd, p->ni_line, STATE,
461				    tcpstates[p->ni_state]);
462		}
463		wclrtoeol(wnd);
464	}
465	if (lastrow < YMAX(wnd)) {
466		wmove(wnd, lastrow, 0); wclrtobot(wnd);
467		wmove(wnd, YMAX(wnd), 0); wdeleteln(wnd);	/* XXX */
468	}
469}
470
471/*
472 * Pretty print an Internet address (net address + port).
473 * If the nflag was specified, use numbers instead of names.
474 */
475static void
476inetprint(in, port, proto)
477	struct in_addr *in;
478	int port;
479	const char *proto;
480{
481	struct servent *sp = 0;
482	char line[80], *cp;
483
484	snprintf(line, sizeof(line), "%.*s.", 16, inetname(*in));
485	cp = index(line, '\0');
486	if (!nflag && port)
487		sp = getservbyport(port, proto);
488	if (sp || port == 0)
489		snprintf(cp, sizeof(line) - (cp - line), "%.8s",
490		    sp ? sp->s_name : "*");
491	else
492		snprintf(cp, sizeof(line) - (cp - line), "%d",
493		    ntohs((u_short)port));
494	/* pad to full column to clear any garbage */
495	cp = index(line, '\0');
496	while (cp - line < 22)
497		*cp++ = ' ';
498	line[22] = '\0';
499	waddstr(wnd, line);
500}
501
502/*
503 * Construct an Internet address representation.
504 * If the nflag has been supplied, give
505 * numeric value, otherwise try for symbolic name.
506 */
507static char *
508inetname(in)
509	struct in_addr in;
510{
511	char *cp = 0;
512	static char line[50];
513	struct hostent *hp;
514	struct netent *np;
515
516	if (!nflag && in.s_addr != INADDR_ANY) {
517		int net = inet_netof(in);
518		int lna = inet_lnaof(in);
519
520		if (lna == INADDR_ANY) {
521			np = getnetbyaddr(net, AF_INET);
522			if (np)
523				cp = np->n_name;
524		}
525		if (cp == 0) {
526			hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
527			if (hp)
528				cp = hp->h_name;
529		}
530	}
531	if (in.s_addr == INADDR_ANY)
532		strcpy(line, "*");
533	else if (cp)
534		snprintf(line, sizeof(line), "%s", cp);
535	else {
536		in.s_addr = ntohl(in.s_addr);
537#define C(x)	((x) & 0xff)
538		snprintf(line, sizeof(line), "%u.%u.%u.%u", C(in.s_addr >> 24),
539			C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
540	}
541	return (line);
542}
543
544int
545cmdnetstat(cmd, args)
546	const char *cmd, *args;
547{
548	if (prefix(cmd, "all")) {
549		aflag = !aflag;
550		goto fixup;
551	}
552	if  (prefix(cmd, "numbers") || prefix(cmd, "names")) {
553		struct netinfo *p;
554		int new;
555
556		new = prefix(cmd, "numbers");
557		if (new == nflag)
558			return (1);
559		TAILQ_FOREACH(p, &netcb, chain) {
560			if (p->ni_line == -1)
561				continue;
562			p->ni_flags |= NIF_LACHG|NIF_FACHG;
563		}
564		nflag = new;
565		goto redisplay;
566	}
567	if (!netcmd(cmd, args))
568		return (0);
569fixup:
570	fetchnetstat();
571redisplay:
572	shownetstat();
573	refresh();
574	return (1);
575}
576