netstat.c revision 9304e224e74af981127c0d8c72015e36a2c06743
194d84dbjmcneill/*-
2001152cjmcneill * Copyright (c) 1980, 1992, 1993
3001152cjmcneill *	The Regents of the University of California.  All rights reserved.
4001152cjmcneill *
5001152cjmcneill * Redistribution and use in source and binary forms, with or without
6001152cjmcneill * modification, are permitted provided that the following conditions
7001152cjmcneill * are met:
8001152cjmcneill * 1. Redistributions of source code must retain the above copyright
9001152cjmcneill *    notice, this list of conditions and the following disclaimer.
10001152cjmcneill * 2. Redistributions in binary form must reproduce the above copyright
11001152cjmcneill *    notice, this list of conditions and the following disclaimer in the
12001152cjmcneill *    documentation and/or other materials provided with the distribution.
13001152cjmcneill * 4. Neither the name of the University nor the names of its contributors
14001152cjmcneill *    may be used to endorse or promote products derived from this software
15001152cjmcneill *    without specific prior written permission.
16001152cjmcneill *
17001152cjmcneill * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18001152cjmcneill * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19001152cjmcneill * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20001152cjmcneill * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21001152cjmcneill * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22001152cjmcneill * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23001152cjmcneill * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24001152cjmcneill * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25001152cjmcneill * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26001152cjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27001152cjmcneill * SUCH DAMAGE.
28001152cjmcneill */
29001152cjmcneill
30001152cjmcneill#include <sys/cdefs.h>
31001152cjmcneill
32001152cjmcneill__FBSDID("$FreeBSD$");
33001152cjmcneill
34001152cjmcneill#ifdef lint
3594d84dbjmcneillstatic const char sccsid[] = "@(#)netstat.c	8.1 (Berkeley) 6/6/93";
36001152cjmcneill#endif
37001152cjmcneill
38001152cjmcneill/*
39001152cjmcneill * netstat
40001152cjmcneill */
41001152cjmcneill#include <sys/param.h>
42001152cjmcneill#include <sys/queue.h>
43001152cjmcneill#include <sys/socket.h>
44001152cjmcneill#include <sys/socketvar.h>
45001152cjmcneill#include <sys/protosw.h>
46001152cjmcneill
47001152cjmcneill#include <netinet/in.h>
48001152cjmcneill#include <arpa/inet.h>
49001152cjmcneill#include <net/route.h>
50001152cjmcneill#include <netinet/in_systm.h>
5194d84dbjmcneill#include <netinet/ip.h>
52001152cjmcneill#ifdef INET6
53001152cjmcneill#include <netinet/ip6.h>
54001152cjmcneill#endif
55001152cjmcneill#include <netinet/in_pcb.h>
56001152cjmcneill#include <netinet/ip_icmp.h>
57001152cjmcneill#include <netinet/icmp_var.h>
58001152cjmcneill#include <netinet/ip_var.h>
59001152cjmcneill#include <netinet/tcp.h>
60001152cjmcneill#include <netinet/tcpip.h>
61001152cjmcneill#include <netinet/tcp_seq.h>
62001152cjmcneill#include <netinet/tcp_var.h>
63001152cjmcneill#define TCPSTATES
64001152cjmcneill#include <netinet/tcp_fsm.h>
65001152cjmcneill#include <netinet/tcp_timer.h>
66001152cjmcneill#include <netinet/tcp_debug.h>
67001152cjmcneill#include <netinet/udp.h>
68001152cjmcneill#include <netinet/udp_var.h>
69001152cjmcneill
70001152cjmcneill#include <netdb.h>
71001152cjmcneill#include <nlist.h>
72001152cjmcneill#include <paths.h>
73001152cjmcneill#include <stdlib.h>
74001152cjmcneill#include <string.h>
75001152cjmcneill
76001152cjmcneill#include "systat.h"
77001152cjmcneill#include "extern.h"
78001152cjmcneill
79001152cjmcneillstatic struct netinfo *enter(struct inpcb *, int, const char *);
80001152cjmcneillstatic void enter_kvm(struct inpcb *, struct socket *, int, const char *);
81001152cjmcneillstatic void enter_sysctl(struct inpcb *, struct xsocket *, int, const char *);
82001152cjmcneillstatic void fetchnetstat_kvm(void);
83001152cjmcneillstatic void fetchnetstat_sysctl(void);
84001152cjmcneillstatic char *inetname(struct sockaddr *);
85001152cjmcneillstatic void inetprint(struct sockaddr *, const char *);
86001152cjmcneill
87001152cjmcneill#define	streq(a,b)	(strcmp(a,b)==0)
88001152cjmcneill#define	YMAX(w)		((w)->_maxy-1)
89001152cjmcneill
90001152cjmcneillWINDOW *
91001152cjmcneillopennetstat(void)
92001152cjmcneill{
93001152cjmcneill	sethostent(1);
94001152cjmcneill	setnetent(1);
95001152cjmcneill	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
96001152cjmcneill}
97001152cjmcneill
98001152cjmcneillstruct netinfo {
99001152cjmcneill	TAILQ_ENTRY(netinfo) chain;
100001152cjmcneill	short	ni_line;		/* line on screen */
101001152cjmcneill	short	ni_seen;		/* 0 when not present in list */
102001152cjmcneill	short	ni_flags;
103001152cjmcneill#define	NIF_LACHG	0x1		/* local address changed */
104001152cjmcneill#define	NIF_FACHG	0x2		/* foreign address changed */
105001152cjmcneill	short	ni_state;		/* tcp state */
106001152cjmcneill	const char	*ni_proto;		/* protocol */
107001152cjmcneill	struct sockaddr_storage ni_lsa;	/* local address */
108001152cjmcneill	struct sockaddr_storage	ni_fsa;	/* foreign address */
109001152cjmcneill	u_int	ni_rcvcc;		/* rcv buffer character count */
110001152cjmcneill	u_int	ni_sndcc;		/* snd buffer character count */
111001152cjmcneill};
112001152cjmcneill
113001152cjmcneillTAILQ_HEAD(netinfohead, netinfo) netcb = TAILQ_HEAD_INITIALIZER(netcb);
114001152cjmcneill
115001152cjmcneillstatic	int aflag = 0;
116001152cjmcneillstatic	int nflag = 0;
117001152cjmcneillstatic	int lastrow = 1;
118001152cjmcneill
119001152cjmcneillvoid
120001152cjmcneillclosenetstat(WINDOW *w)
121001152cjmcneill{
122001152cjmcneill	struct netinfo *p;
123001152cjmcneill
124001152cjmcneill	endhostent();
125001152cjmcneill	endnetent();
126001152cjmcneill	TAILQ_FOREACH(p, &netcb, chain) {
127001152cjmcneill		if (p->ni_line != -1)
128001152cjmcneill			lastrow--;
129001152cjmcneill		p->ni_line = -1;
130001152cjmcneill	}
131001152cjmcneill        if (w != NULL) {
132001152cjmcneill		wclear(w);
133001152cjmcneill		wrefresh(w);
134001152cjmcneill		delwin(w);
135001152cjmcneill	}
136001152cjmcneill}
137001152cjmcneill
138001152cjmcneillstatic const char *miblist[] = {
139001152cjmcneill	"net.inet.tcp.pcblist",
140001152cjmcneill	"net.inet.udp.pcblist"
141001152cjmcneill};
142001152cjmcneill
143001152cjmcneillstatic char tcb[] = "tcb", udb[] = "udb";
144001152cjmcneill
145001152cjmcneillstruct nlist namelist[] = {
146001152cjmcneill#define	X_TCB	0
147001152cjmcneill	{ .n_name = tcb },
148001152cjmcneill#define	X_UDB	1
149001152cjmcneill	{ .n_name = udb },
150ea86363jmcneill	{ .n_name = NULL },
151001152cjmcneill};
152acdad7fjmcneill
153001152cjmcneillint
154acdad7fjmcneillinitnetstat(void)
155ea86363jmcneill{
156acdad7fjmcneill	protos = TCP|UDP;
157acdad7fjmcneill	return(1);
158acdad7fjmcneill}
159acdad7fjmcneill
160001152cjmcneillvoid
161acdad7fjmcneillfetchnetstat(void)
162001152cjmcneill{
163001152cjmcneill	if (use_kvm)
164001152cjmcneill		fetchnetstat_kvm();
165001152cjmcneill	else
166001152cjmcneill		fetchnetstat_sysctl();
167001152cjmcneill}
168001152cjmcneill
169001152cjmcneillstatic void
170001152cjmcneillfetchnetstat_kvm(void)
171001152cjmcneill{
172001152cjmcneill	struct inpcb *next;
173	struct netinfo *p;
174	struct inpcbhead head;
175	struct inpcb inpcb;
176	struct socket sockb;
177	struct tcpcb tcpcb;
178	void *off;
179	int istcp;
180
181	if (namelist[X_TCB].n_value == 0)
182		return;
183	TAILQ_FOREACH(p, &netcb, chain)
184		p->ni_seen = 0;
185	if (protos&TCP) {
186		off = NPTR(X_TCB);
187		istcp = 1;
188	}
189	else if (protos&UDP) {
190		off = NPTR(X_UDB);
191		istcp = 0;
192	}
193	else {
194		error("No protocols to display");
195		return;
196	}
197again:
198	KREAD(off, &head, sizeof (struct inpcbhead));
199	LIST_FOREACH(next, &head, inp_list) {
200		KREAD(next, &inpcb, sizeof (inpcb));
201		next = &inpcb;
202		if (!aflag) {
203			if (inpcb.inp_vflag & INP_IPV4) {
204				if (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
205					continue;
206			}
207#ifdef INET6
208			else if (inpcb.inp_vflag & INP_IPV6) {
209				if (memcmp(&inpcb.in6p_laddr,
210				    &in6addr_any, sizeof(in6addr_any)) == 0)
211					continue;
212			}
213#endif
214		}
215		if (nhosts && !checkhost(&inpcb))
216			continue;
217		if (nports && !checkport(&inpcb))
218			continue;
219		if (istcp) {
220			if (inpcb.inp_flags & INP_TIMEWAIT) {
221				bzero(&sockb, sizeof(sockb));
222				enter_kvm(&inpcb, &sockb, TCPS_TIME_WAIT,
223					 "tcp");
224			} else {
225				KREAD(inpcb.inp_socket, &sockb,
226					sizeof (sockb));
227				KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
228				enter_kvm(&inpcb, &sockb, tcpcb.t_state,
229					"tcp");
230			}
231		} else
232			enter_kvm(&inpcb, &sockb, 0, "udp");
233	}
234	if (istcp && (protos&UDP)) {
235		istcp = 0;
236		off = NPTR(X_UDB);
237		goto again;
238	}
239}
240
241static void
242fetchnetstat_sysctl(void)
243{
244	struct netinfo *p;
245	int idx;
246	struct xinpgen *inpg;
247	char *cur, *end;
248	struct inpcb *inpcb;
249	struct xinpcb *xip = NULL;
250	struct xtcpcb *xtp = NULL;
251	int plen;
252	size_t lsz;
253
254	TAILQ_FOREACH(p, &netcb, chain)
255		p->ni_seen = 0;
256	if (protos&TCP) {
257		idx = 0;
258	} else if (protos&UDP) {
259		idx = 1;
260	} else {
261		error("No protocols to display");
262		return;
263	}
264
265	for (;idx < 2; idx++) {
266		if (idx == 1 && !(protos&UDP))
267			break;
268		inpg = (struct xinpgen *)sysctl_dynread(miblist[idx], &lsz);
269		if (inpg == NULL) {
270			error("sysctl(%s...) failed", miblist[idx]);
271			continue;
272		}
273		/*
274		 * We currently do no require a consistent pcb list.
275		 * Try to be robust in case of struct size changes
276		 */
277		cur = ((char *)inpg) + inpg->xig_len;
278		/* There is also a trailing struct xinpgen */
279		end = ((char *)inpg) + lsz - inpg->xig_len;
280		if (end <= cur) {
281			free(inpg);
282			continue;
283		}
284		if (idx == 0) { /* TCP */
285			xtp = (struct xtcpcb *)cur;
286			plen = xtp->xt_len;
287		} else {
288			xip = (struct xinpcb *)cur;
289			plen = xip->xi_len;
290		}
291		while (cur + plen <= end) {
292			if (idx == 0) { /* TCP */
293				xtp = (struct xtcpcb *)cur;
294				inpcb = &xtp->xt_inp;
295			} else {
296				xip = (struct xinpcb *)cur;
297				inpcb = &xip->xi_inp;
298			}
299			cur += plen;
300
301			if (!aflag) {
302				if (inpcb->inp_vflag & INP_IPV4) {
303					if (inet_lnaof(inpcb->inp_laddr) ==
304					    INADDR_ANY)
305						continue;
306				}
307#ifdef INET6
308				else if (inpcb->inp_vflag & INP_IPV6) {
309					if (memcmp(&inpcb->in6p_laddr,
310					    &in6addr_any, sizeof(in6addr_any))
311					    == 0)
312						continue;
313				}
314#endif
315			}
316			if (nhosts && !checkhost(inpcb))
317				continue;
318			if (nports && !checkport(inpcb))
319				continue;
320			if (idx == 0)	/* TCP */
321				enter_sysctl(inpcb, &xtp->xt_socket,
322				    xtp->xt_tp.t_state, "tcp");
323			else		/* UDP */
324				enter_sysctl(inpcb, &xip->xi_socket, 0, "udp");
325		}
326		free(inpg);
327	}
328}
329
330static void
331enter_kvm(struct inpcb *inp, struct socket *so, int state, const char *proto)
332{
333	struct netinfo *p;
334
335	if ((p = enter(inp, state, proto)) != NULL) {
336		p->ni_rcvcc = so->so_rcv.sb_cc;
337		p->ni_sndcc = so->so_snd.sb_cc;
338	}
339}
340
341static void
342enter_sysctl(struct inpcb *inp, struct xsocket *so, int state, const char *proto)
343{
344	struct netinfo *p;
345
346	if ((p = enter(inp, state, proto)) != NULL) {
347		p->ni_rcvcc = so->so_rcv.sb_cc;
348		p->ni_sndcc = so->so_snd.sb_cc;
349	}
350}
351
352
353static struct netinfo *
354enter(struct inpcb *inp, int state, const char *proto)
355{
356	struct netinfo *p;
357	struct sockaddr_storage lsa, fsa;
358	struct sockaddr_in *sa4;
359#ifdef INET6
360	struct sockaddr_in6 *sa6;
361#endif
362
363	memset(&lsa, 0, sizeof(lsa));
364	memset(&fsa, 0, sizeof(fsa));
365	if (inp->inp_vflag & INP_IPV4) {
366		sa4 = (struct sockaddr_in *)&lsa;
367		sa4->sin_addr = inp->inp_laddr;
368		sa4->sin_port = inp->inp_lport;
369		sa4->sin_family = AF_INET;
370		sa4->sin_len = sizeof(struct sockaddr_in);
371
372		sa4 = (struct sockaddr_in *)&fsa;
373		sa4->sin_addr = inp->inp_faddr;
374		sa4->sin_port = inp->inp_fport;
375		sa4->sin_family = AF_INET;
376		sa4->sin_len = sizeof(struct sockaddr_in);
377	}
378#ifdef INET6
379	else if (inp->inp_vflag & INP_IPV6) {
380		sa6 = (struct sockaddr_in6 *)&lsa;
381		memcpy(&sa6->sin6_addr, &inp->in6p_laddr,
382		    sizeof(struct in6_addr));
383		sa6->sin6_port = inp->inp_lport;
384		sa6->sin6_family = AF_INET6;
385		sa6->sin6_len = sizeof(struct sockaddr_in6);
386
387		sa6 = (struct sockaddr_in6 *)&fsa;
388		memcpy(&sa6->sin6_addr, &inp->in6p_faddr,
389		    sizeof(struct in6_addr));
390		sa6->sin6_port = inp->inp_fport;
391		sa6->sin6_family = AF_INET6;
392		sa6->sin6_len = sizeof(struct sockaddr_in6);
393	}
394#endif
395	else
396		return NULL;
397
398	/*
399	 * Only take exact matches, any sockets with
400	 * previously unbound addresses will be deleted
401	 * below in the display routine because they
402	 * will appear as ``not seen'' in the kernel
403	 * data structures.
404	 */
405	TAILQ_FOREACH(p, &netcb, chain) {
406		if (!streq(proto, p->ni_proto))
407			continue;
408		if (p->ni_lsa.ss_family != lsa.ss_family ||
409		    memcmp(&p->ni_lsa, &lsa, lsa.ss_len) != 0)
410			continue;
411		if (p->ni_fsa.ss_family == fsa.ss_family &&
412		    memcmp(&p->ni_fsa, &fsa, fsa.ss_len) == 0)
413			break;
414	}
415	if (p == NULL) {
416		if ((p = malloc(sizeof(*p))) == NULL) {
417			error("Out of memory");
418			return NULL;
419		}
420		TAILQ_INSERT_HEAD(&netcb, p, chain);
421		p->ni_line = -1;
422		memcpy(&p->ni_lsa, &lsa, lsa.ss_len);
423		memcpy(&p->ni_fsa, &fsa, fsa.ss_len);
424		p->ni_proto = strdup(proto);
425		p->ni_flags = NIF_LACHG|NIF_FACHG;
426	}
427	p->ni_state = state;
428	p->ni_seen = 1;
429	return p;
430}
431
432/* column locations */
433#define	LADDR	0
434#define	FADDR	LADDR+23
435#define	PROTO	FADDR+23
436#define	RCVCC	PROTO+6
437#define	SNDCC	RCVCC+7
438#define	STATE	SNDCC+7
439
440
441void
442labelnetstat(void)
443{
444	if (use_kvm && namelist[X_TCB].n_type == 0)
445		return;
446	wmove(wnd, 0, 0); wclrtobot(wnd);
447	mvwaddstr(wnd, 0, LADDR, "Local Address");
448	mvwaddstr(wnd, 0, FADDR, "Foreign Address");
449	mvwaddstr(wnd, 0, PROTO, "Proto");
450	mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
451	mvwaddstr(wnd, 0, SNDCC, "Send-Q");
452	mvwaddstr(wnd, 0, STATE, "(state)");
453}
454
455void
456shownetstat(void)
457{
458	struct netinfo *p, *q;
459	char proto[6];
460	const char *family = "";
461
462	/*
463	 * First, delete any connections that have gone
464	 * away and adjust the position of connections
465	 * below to reflect the deleted line.
466	 */
467	p = TAILQ_FIRST(&netcb);
468	while (p != NULL) {
469		if (p->ni_line == -1 || p->ni_seen) {
470			p = TAILQ_NEXT(p, chain);
471			continue;
472		}
473		wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
474		TAILQ_FOREACH(q, &netcb, chain)
475			if (q != p && q->ni_line > p->ni_line) {
476				q->ni_line--;
477				/* this shouldn't be necessary */
478				q->ni_flags |= NIF_LACHG|NIF_FACHG;
479			}
480		lastrow--;
481		q = TAILQ_NEXT(p, chain);
482		TAILQ_REMOVE(&netcb, p, chain);
483		free(p);
484		p = q;
485	}
486	/*
487	 * Update existing connections and add new ones.
488	 */
489	TAILQ_FOREACH(p, &netcb, chain) {
490		if (p->ni_line == -1) {
491			/*
492			 * Add a new entry if possible.
493			 */
494			if (lastrow > YMAX(wnd))
495				continue;
496			p->ni_line = lastrow++;
497			p->ni_flags |= NIF_LACHG|NIF_FACHG;
498		}
499		if (p->ni_flags & NIF_LACHG) {
500			wmove(wnd, p->ni_line, LADDR);
501			inetprint((struct sockaddr *)&p->ni_lsa, p->ni_proto);
502			p->ni_flags &= ~NIF_LACHG;
503		}
504		if (p->ni_flags & NIF_FACHG) {
505			wmove(wnd, p->ni_line, FADDR);
506			inetprint((struct sockaddr *)&p->ni_fsa, p->ni_proto);
507			p->ni_flags &= ~NIF_FACHG;
508		}
509#ifdef INET6
510		family = (p->ni_lsa.ss_family == AF_INET) ? "4" : "6";
511#endif
512		snprintf(proto, sizeof(proto), "%s%s", p->ni_proto, family);
513		mvwaddstr(wnd, p->ni_line, PROTO, proto);
514		mvwprintw(wnd, p->ni_line, RCVCC, "%6u", p->ni_rcvcc);
515		mvwprintw(wnd, p->ni_line, SNDCC, "%6u", p->ni_sndcc);
516		if (streq(p->ni_proto, "tcp")) {
517			if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
518				mvwprintw(wnd, p->ni_line, STATE, "%d",
519				    p->ni_state);
520			else
521				mvwaddstr(wnd, p->ni_line, STATE,
522				    tcpstates[p->ni_state]);
523		}
524		wclrtoeol(wnd);
525	}
526	if (lastrow < YMAX(wnd)) {
527		wmove(wnd, lastrow, 0); wclrtobot(wnd);
528		wmove(wnd, YMAX(wnd), 0); wdeleteln(wnd);	/* XXX */
529	}
530}
531
532/*
533 * Pretty print an Internet address (net address + port).
534 * If the nflag was specified, use numbers instead of names.
535 */
536static void
537inetprint(struct sockaddr *sa, const char *proto)
538{
539	struct servent *sp = 0;
540	char line[80], *cp;
541	int port;
542
543	switch (sa->sa_family) {
544	case AF_INET:
545		port = ((struct sockaddr_in *)sa)->sin_port;
546		break;
547#ifdef INET6
548	case AF_INET6:
549		port = ((struct sockaddr_in6 *)sa)->sin6_port;
550		break;
551#endif
552	default:
553		port = 0;
554		break;
555	}
556	snprintf(line, sizeof(line), "%.*s.", 16, inetname(sa));
557	cp = index(line, '\0');
558	if (!nflag && port)
559		sp = getservbyport(port, proto);
560	if (sp || port == 0)
561		snprintf(cp, sizeof(line) - (cp - line), "%.8s",
562		    sp ? sp->s_name : "*");
563	else
564		snprintf(cp, sizeof(line) - (cp - line), "%d",
565		    ntohs((u_short)port));
566	/* pad to full column to clear any garbage */
567	cp = index(line, '\0');
568	while (cp - line < 22)
569		*cp++ = ' ';
570	line[22] = '\0';
571	waddstr(wnd, line);
572}
573
574/*
575 * Construct an Internet address representation.
576 * If the nflag has been supplied, give
577 * numeric value, otherwise try for symbolic name.
578 */
579static char *
580inetname(struct sockaddr *sa)
581{
582	char *cp = 0;
583	static char line[NI_MAXHOST];
584	struct hostent *hp;
585	struct netent *np;
586	struct in_addr in;
587
588#ifdef INET6
589	if (sa->sa_family == AF_INET6) {
590		if (memcmp(&((struct sockaddr_in6 *)sa)->sin6_addr,
591		    &in6addr_any, sizeof(in6addr_any)) == 0)
592			strcpy(line, "*");
593		else
594			getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
595			    nflag ? NI_NUMERICHOST : 0);
596		return (line);
597	}
598#endif
599
600	in = ((struct sockaddr_in *)sa)->sin_addr;
601	if (!nflag && in.s_addr != INADDR_ANY) {
602		int net = inet_netof(in);
603		int lna = inet_lnaof(in);
604
605		if (lna == INADDR_ANY) {
606			np = getnetbyaddr(net, AF_INET);
607			if (np)
608				cp = np->n_name;
609		}
610		if (cp == 0) {
611			hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
612			if (hp)
613				cp = hp->h_name;
614		}
615	}
616	if (in.s_addr == INADDR_ANY)
617		strcpy(line, "*");
618	else if (cp)
619		snprintf(line, sizeof(line), "%s", cp);
620	else {
621		in.s_addr = ntohl(in.s_addr);
622#define C(x)	((x) & 0xff)
623		snprintf(line, sizeof(line), "%u.%u.%u.%u", C(in.s_addr >> 24),
624			C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
625	}
626	return (line);
627}
628
629int
630cmdnetstat(const char *cmd, const char *args)
631{
632	if (prefix(cmd, "all")) {
633		aflag = !aflag;
634		goto fixup;
635	}
636	if  (prefix(cmd, "numbers") || prefix(cmd, "names")) {
637		struct netinfo *p;
638		int new;
639
640		new = prefix(cmd, "numbers");
641		if (new == nflag)
642			return (1);
643		TAILQ_FOREACH(p, &netcb, chain) {
644			if (p->ni_line == -1)
645				continue;
646			p->ni_flags |= NIF_LACHG|NIF_FACHG;
647		}
648		nflag = new;
649		goto redisplay;
650	}
651	if (!netcmd(cmd, args))
652		return (0);
653fixup:
654	fetchnetstat();
655redisplay:
656	shownetstat();
657	refresh();
658	return (1);
659}
660