xref: /netbsd-src/usr.bin/systat/netstat.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: netstat.c,v 1.23 2003/07/05 06:48:52 dsl Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)netstat.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: netstat.c,v 1.23 2003/07/05 06:48:52 dsl Exp $");
42 #endif /* not lint */
43 
44 /*
45  * netstat
46  */
47 #include <sys/param.h>
48 #include <sys/socketvar.h>
49 #include <sys/mbuf.h>
50 #include <sys/protosw.h>
51 
52 #include <netinet/in.h>
53 
54 #include <arpa/inet.h>
55 #include <net/route.h>
56 
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/icmp_var.h>
62 #include <netinet/ip_var.h>
63 #ifdef INET6
64 #include <netinet/ip6.h>
65 #include <netinet6/in6_pcb.h>
66 #endif
67 #include <netinet/tcp.h>
68 #include <netinet/tcpip.h>
69 #include <netinet/tcp_seq.h>
70 #define TCPSTATES
71 #include <netinet/tcp_fsm.h>
72 #include <netinet/tcp_timer.h>
73 #include <netinet/tcp_var.h>
74 #include <netinet/tcp_debug.h>
75 #include <netinet/udp.h>
76 #include <netinet/udp_var.h>
77 
78 #include <netdb.h>
79 #include <stdlib.h>
80 #include <string.h>
81 
82 #include "systat.h"
83 #include "extern.h"
84 
85 static void fetchnetstat4(void *, int);
86 static void enter(struct inpcb *, struct socket *, int, char *);
87 static const char *inetname(struct in_addr);
88 static void inetprint(struct in_addr *, int, char *);
89 #ifdef INET6
90 static void fetchnetstat6(void *, int);
91 static void enter6(struct in6pcb *, struct socket *, int, char *);
92 static const char *inet6name(struct in6_addr *);
93 static void inet6print(struct in6_addr *, int, char *);
94 #endif
95 
96 #define	streq(a,b)	(strcmp(a,b)==0)
97 
98 struct netinfo {
99 	struct	netinfo *ni_forw, *ni_prev;
100 	int	ni_family;
101 	short	ni_line;		/* line on screen */
102 	short	ni_seen;		/* 0 when not present in list */
103 	short	ni_flags;
104 #define	NIF_LACHG	0x1		/* local address changed */
105 #define	NIF_FACHG	0x2		/* foreign address changed */
106 	short	ni_state;		/* tcp state */
107 	char	*ni_proto;		/* protocol */
108 	struct	in_addr ni_laddr;	/* local address */
109 #ifdef INET6
110 	struct	in6_addr ni_laddr6;	/* local address */
111 #endif
112 	long	ni_lport;		/* local port */
113 	struct	in_addr	ni_faddr;	/* foreign address */
114 #ifdef INET6
115 	struct	in6_addr ni_faddr6;	/* foreign address */
116 #endif
117 	long	ni_fport;		/* foreign port */
118 	long	ni_rcvcc;		/* rcv buffer character count */
119 	long	ni_sndcc;		/* snd buffer character count */
120 };
121 
122 static struct {
123 	struct	netinfo *ni_forw, *ni_prev;
124 } netcb;
125 
126 static	int aflag = 0;
127 int nflag = 0;
128 static	int lastrow = 1;
129 
130 WINDOW *
131 opennetstat(void)
132 {
133 
134 	sethostent(1);
135 	setnetent(1);
136 	return (subwin(stdscr, -1, 0, 5, 0));
137 }
138 
139 void
140 closenetstat(WINDOW *w)
141 {
142 	struct netinfo *p;
143 
144 	endhostent();
145 	endnetent();
146 	p = (struct netinfo *)netcb.ni_forw;
147 	while (p != (struct netinfo *)&netcb) {
148 		if (p->ni_line != -1)
149 			lastrow--;
150 		p->ni_line = -1;
151 		p = p->ni_forw;
152 	}
153 	if (w != NULL) {
154 		wclear(w);
155 		wrefresh(w);
156 		delwin(w);
157 	}
158 }
159 
160 static struct nlist namelist[] = {
161 #define	X_TCBTABLE	0
162 	{ "_tcbtable" },
163 #define	X_UDBTABLE	1
164 	{ "_udbtable" },
165 #ifdef INET6
166 #define	X_TCB6		2
167 	{ "_tcb6" },
168 #define	X_UDB6		3
169 	{ "_udb6" },
170 #endif
171 	{ "" },
172 };
173 
174 int
175 initnetstat(void)
176 {
177 	int n;
178 
179 	n = kvm_nlist(kd, namelist);
180 	if (n < 0) {
181 		nlisterr(namelist);
182 		return(0);
183 	} else if (n == sizeof(namelist) / sizeof(namelist[0]) - 1) {
184 		error("No symbols in namelist");
185 		return(0);
186 	}
187 
188 	netcb.ni_forw = netcb.ni_prev = (struct netinfo *)&netcb;
189 	protos = TCP|UDP;
190 	return(1);
191 }
192 
193 void
194 fetchnetstat(void)
195 {
196 	struct netinfo *p;
197 
198 	if (namelist[X_TCBTABLE].n_value == 0)
199 		return;
200 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw)
201 		p->ni_seen = 0;
202 
203 	if ((protos & (TCP | UDP)) == 0) {
204 		error("No protocols to display");
205 		return;
206 	}
207 	if ((protos & TCP) && namelist[X_TCBTABLE].n_type)
208 		fetchnetstat4(NPTR(X_TCBTABLE), 1);
209 	if ((protos & UDP) && namelist[X_UDBTABLE].n_type)
210 		fetchnetstat4(NPTR(X_UDBTABLE), 0);
211 #ifdef INET6
212 	if ((protos & TCP) && namelist[X_TCB6].n_type)
213 		fetchnetstat6(NPTR(X_TCB6), 1);
214 	if ((protos & UDP) && namelist[X_UDB6].n_type)
215 		fetchnetstat6(NPTR(X_UDB6), 0);
216 #endif
217 }
218 
219 static void
220 fetchnetstat4(void *off, int istcp)
221 {
222 	struct inpcbtable pcbtable;
223 	struct inpcb *head, *prev, *next;
224 	struct netinfo *p;
225 	struct inpcb inpcb;
226 	struct socket sockb;
227 	struct tcpcb tcpcb;
228 
229 	KREAD(off, &pcbtable, sizeof pcbtable);
230 	prev = head = (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue;
231 	next = pcbtable.inpt_queue.cqh_first;
232 	while (next != head) {
233 		KREAD(next, &inpcb, sizeof (inpcb));
234 		if (inpcb.inp_queue.cqe_prev != prev) {
235 printf("prev = %p, head = %p, next = %p, inpcb...prev = %p\n", prev, head, next, inpcb.inp_queue.cqe_prev);
236 			p = netcb.ni_forw;
237 			for (; p != (struct netinfo *)&netcb; p = p->ni_forw)
238 				p->ni_seen = 1;
239 			error("Kernel state in transition");
240 			return;
241 		}
242 		prev = next;
243 		next = inpcb.inp_queue.cqe_next;
244 
245 		if (!aflag && inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
246 			continue;
247 		if (nhosts && !checkhost(&inpcb))
248 			continue;
249 		if (nports && !checkport(&inpcb))
250 			continue;
251 		KREAD(inpcb.inp_socket, &sockb, sizeof (sockb));
252 		if (istcp) {
253 			KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
254 			enter(&inpcb, &sockb, tcpcb.t_state, "tcp");
255 		} else
256 			enter(&inpcb, &sockb, 0, "udp");
257 	}
258 }
259 
260 #ifdef INET6
261 static void
262 fetchnetstat6(void *off, int istcp)
263 {
264 	struct netinfo *p;
265 	struct socket sockb;
266 	struct tcpcb tcpcb;
267 	struct in6pcb in6pcb;
268 	struct in6pcb *head6, *prev6, *next6;
269 
270 	KREAD(off, &in6pcb, sizeof (struct in6pcb));
271 	prev6 = head6 = (struct in6pcb *)off;
272 	next6 = in6pcb.in6p_next;
273 	while (next6 != head6) {
274 		KREAD(next6, &in6pcb, sizeof (in6pcb));
275 		if (in6pcb.in6p_prev != prev6) {
276 printf("prev = %p, head = %p, next = %p, in6pcb...prev = %p\n", prev6, head6, next6, in6pcb.in6p_prev);
277 			p = netcb.ni_forw;
278 			for (; p != (struct netinfo *)&netcb; p = p->ni_forw)
279 				p->ni_seen = 1;
280 			error("Kernel state in transition");
281 			return;
282 		}
283 		prev6 = next6;
284 		next6 = in6pcb.in6p_next;
285 
286 		if (!aflag && IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr))
287 			continue;
288 		if (nhosts && !checkhost6(&in6pcb))
289 			continue;
290 		if (nports && !checkport6(&in6pcb))
291 			continue;
292 		KREAD(in6pcb.in6p_socket, &sockb, sizeof (sockb));
293 		if (istcp) {
294 			KREAD(in6pcb.in6p_ppcb, &tcpcb, sizeof (tcpcb));
295 			enter6(&in6pcb, &sockb, tcpcb.t_state, "tcp");
296 		} else
297 			enter6(&in6pcb, &sockb, 0, "udp");
298 	}
299 }
300 #endif /*INET6*/
301 
302 static void
303 enter(struct inpcb *inp, struct socket *so, int state, char *proto)
304 {
305 	struct netinfo *p;
306 
307 	/*
308 	 * Only take exact matches, any sockets with
309 	 * previously unbound addresses will be deleted
310 	 * below in the display routine because they
311 	 * will appear as ``not seen'' in the kernel
312 	 * data structures.
313 	 */
314 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
315 		if (p->ni_family != AF_INET)
316 			continue;
317 		if (!streq(proto, p->ni_proto))
318 			continue;
319 		if (p->ni_lport != inp->inp_lport ||
320 		    p->ni_laddr.s_addr != inp->inp_laddr.s_addr)
321 			continue;
322 		if (p->ni_faddr.s_addr == inp->inp_faddr.s_addr &&
323 		    p->ni_fport == inp->inp_fport)
324 			break;
325 	}
326 	if (p == (struct netinfo *)&netcb) {
327 		if ((p = malloc(sizeof(*p))) == NULL) {
328 			error("Out of memory");
329 			return;
330 		}
331 		p->ni_prev = (struct netinfo *)&netcb;
332 		p->ni_forw = netcb.ni_forw;
333 		netcb.ni_forw->ni_prev = p;
334 		netcb.ni_forw = p;
335 		p->ni_line = -1;
336 		p->ni_laddr = inp->inp_laddr;
337 		p->ni_lport = inp->inp_lport;
338 		p->ni_faddr = inp->inp_faddr;
339 		p->ni_fport = inp->inp_fport;
340 		p->ni_proto = proto;
341 		p->ni_flags = NIF_LACHG | NIF_FACHG;
342 		p->ni_family = AF_INET;
343 	}
344 	p->ni_rcvcc = so->so_rcv.sb_cc;
345 	p->ni_sndcc = so->so_snd.sb_cc;
346 	p->ni_state = state;
347 	p->ni_seen = 1;
348 }
349 
350 #ifdef INET6
351 static void
352 enter6(struct in6pcb *in6p, struct socket *so, int state, char *proto)
353 {
354 	struct netinfo *p;
355 
356 	/*
357 	 * Only take exact matches, any sockets with
358 	 * previously unbound addresses will be deleted
359 	 * below in the display routine because they
360 	 * will appear as ``not seen'' in the kernel
361 	 * data structures.
362 	 */
363 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
364 		if (p->ni_family != AF_INET6)
365 			continue;
366 		if (!streq(proto, p->ni_proto))
367 			continue;
368 		if (p->ni_lport != in6p->in6p_lport ||
369 		    !IN6_ARE_ADDR_EQUAL(&p->ni_laddr6, &in6p->in6p_laddr))
370 			continue;
371 		if (IN6_ARE_ADDR_EQUAL(&p->ni_faddr6, &in6p->in6p_faddr) &&
372 		    p->ni_fport == in6p->in6p_fport)
373 			break;
374 	}
375 	if (p == (struct netinfo *)&netcb) {
376 		if ((p = malloc(sizeof(*p))) == NULL) {
377 			error("Out of memory");
378 			return;
379 		}
380 		p->ni_prev = (struct netinfo *)&netcb;
381 		p->ni_forw = netcb.ni_forw;
382 		netcb.ni_forw->ni_prev = p;
383 		netcb.ni_forw = p;
384 		p->ni_line = -1;
385 		p->ni_laddr6 = in6p->in6p_laddr;
386 		p->ni_lport = in6p->in6p_lport;
387 		p->ni_faddr6 = in6p->in6p_faddr;
388 		p->ni_fport = in6p->in6p_fport;
389 		p->ni_proto = proto;
390 		p->ni_flags = NIF_LACHG | NIF_FACHG;
391 		p->ni_family = AF_INET6;
392 	}
393 	p->ni_rcvcc = so->so_rcv.sb_cc;
394 	p->ni_sndcc = so->so_snd.sb_cc;
395 	p->ni_state = state;
396 	p->ni_seen = 1;
397 }
398 #endif
399 
400 /* column locations */
401 #define	LADDR	0
402 #define	FADDR	LADDR+23
403 #define	PROTO	FADDR+23
404 #define	RCVCC	PROTO+6
405 #define	SNDCC	RCVCC+7
406 #define	STATE	SNDCC+7
407 
408 void
409 labelnetstat(void)
410 {
411 	struct netinfo *p;
412 
413 	if (namelist[X_TCBTABLE].n_type == 0)
414 		return;
415 	wmove(wnd, 0, 0); wclrtobot(wnd);
416 	mvwaddstr(wnd, 0, LADDR, "Local Address");
417 	mvwaddstr(wnd, 0, FADDR, "Foreign Address");
418 	mvwaddstr(wnd, 0, PROTO, "Proto");
419 	mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
420 	mvwaddstr(wnd, 0, SNDCC, "Send-Q");
421 	mvwaddstr(wnd, 0, STATE, "(state)");
422 
423 	p = netcb.ni_forw;
424 	for (; p != (struct netinfo *)&netcb; p = p->ni_forw) {
425 		if (p->ni_line == -1)
426 			continue;
427 		p->ni_flags |= NIF_LACHG | NIF_FACHG;
428 	}
429 }
430 
431 void
432 shownetstat(void)
433 {
434 	struct netinfo *p, *q;
435 
436 	/*
437 	 * First, delete any connections that have gone
438 	 * away and adjust the position of connections
439 	 * below to reflect the deleted line.
440 	 */
441 	p = netcb.ni_forw;
442 	while (p != (struct netinfo *)&netcb) {
443 		if (p->ni_line == -1 || p->ni_seen) {
444 			p = p->ni_forw;
445 			continue;
446 		}
447 		wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
448 		q = netcb.ni_forw;
449 		for (; q != (struct netinfo *)&netcb; q = q->ni_forw)
450 			if (q != p && q->ni_line > p->ni_line) {
451 				q->ni_line--;
452 				/* this shouldn't be necessary */
453 				q->ni_flags |= NIF_LACHG | NIF_FACHG;
454 			}
455 		lastrow--;
456 		q = p->ni_forw;
457 		p->ni_prev->ni_forw = p->ni_forw;
458 		p->ni_forw->ni_prev = p->ni_prev;
459 		free(p);
460 		p = q;
461 	}
462 	/*
463 	 * Update existing connections and add new ones.
464 	 */
465 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
466 		if (p->ni_line == -1) {
467 			/*
468 			 * Add a new entry if possible.
469 			 */
470 			if (lastrow > getmaxy(wnd))
471 				continue;
472 			p->ni_line = lastrow++;
473 			p->ni_flags |= NIF_LACHG | NIF_FACHG;
474 		}
475 		if (p->ni_flags & NIF_LACHG) {
476 			wmove(wnd, p->ni_line, LADDR);
477 			switch (p->ni_family) {
478 			case AF_INET:
479 				inetprint(&p->ni_laddr, p->ni_lport,
480 					p->ni_proto);
481 				break;
482 #ifdef INET6
483 			case AF_INET6:
484 				inet6print(&p->ni_laddr6, p->ni_lport,
485 					p->ni_proto);
486 				break;
487 #endif
488 			}
489 			p->ni_flags &= ~NIF_LACHG;
490 		}
491 		if (p->ni_flags & NIF_FACHG) {
492 			wmove(wnd, p->ni_line, FADDR);
493 			switch (p->ni_family) {
494 			case AF_INET:
495 				inetprint(&p->ni_faddr, p->ni_fport,
496 					p->ni_proto);
497 				break;
498 #ifdef INET6
499 			case AF_INET6:
500 				inet6print(&p->ni_faddr6, p->ni_fport,
501 					p->ni_proto);
502 				break;
503 #endif
504 			}
505 			p->ni_flags &= ~NIF_FACHG;
506 		}
507 		mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto);
508 #ifdef INET6
509 		if (p->ni_family == AF_INET6)
510 			waddstr(wnd, "6");
511 #endif
512 		mvwprintw(wnd, p->ni_line, RCVCC, "%6ld", p->ni_rcvcc);
513 		mvwprintw(wnd, p->ni_line, SNDCC, "%6ld", p->ni_sndcc);
514 		if (streq(p->ni_proto, "tcp")) {
515 			if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
516 				mvwprintw(wnd, p->ni_line, STATE, "%d",
517 				    p->ni_state);
518 			else
519 				mvwaddstr(wnd, p->ni_line, STATE,
520 				    tcpstates[p->ni_state]);
521 		}
522 		wclrtoeol(wnd);
523 	}
524 	if (lastrow < getmaxy(wnd)) {
525 		wmove(wnd, lastrow, 0); wclrtobot(wnd);
526 		wmove(wnd, getmaxy(wnd), 0); wdeleteln(wnd);	/* XXX */
527 	}
528 }
529 
530 /*
531  * Pretty print an Internet address (net address + port).
532  * If the nflag was specified, use numbers instead of names.
533  */
534 static void
535 inetprint(struct in_addr *in, int port, char *proto)
536 {
537 	struct servent *sp = 0;
538 	char line[80], *cp;
539 
540 	(void)snprintf(line, sizeof line, "%.*s.", 16, inetname(*in));
541 	cp = strchr(line, '\0');
542 	if (!nflag && port)
543 		sp = getservbyport(port, proto);
544 	if (sp || port == 0)
545 		(void)snprintf(cp, line + sizeof line - cp, "%.8s",
546 		     sp ? sp->s_name : "*");
547 	else
548 		(void)snprintf(cp, line + sizeof line - cp, "%d",
549 		     ntohs((u_short)port));
550 	/* pad to full column to clear any garbage */
551 	cp = strchr(line, '\0');
552 	while (cp - line < 22)
553 		*cp++ = ' ';
554 	*cp = '\0';
555 	waddstr(wnd, line);
556 }
557 
558 #ifdef INET6
559 static void
560 inet6print(struct in6_addr *in6, int port, char *proto)
561 {
562 	struct servent *sp = 0;
563 	char line[80], *cp;
564 
565 	(void)snprintf(line, sizeof line, "%.*s.", 16, inet6name(in6));
566 	cp = strchr(line, '\0');
567 	if (!nflag && port)
568 		sp = getservbyport(port, proto);
569 	if (sp || port == 0)
570 		(void)snprintf(cp, line + sizeof line - cp, "%.8s",
571 		     sp ? sp->s_name : "*");
572 	else
573 		(void)snprintf(cp, line + sizeof line - cp, "%d",
574 		     ntohs((u_short)port));
575 	/* pad to full column to clear any garbage */
576 	cp = strchr(line, '\0');
577 	while (cp - line < 22)
578 		*cp++ = ' ';
579 	*cp = '\0';
580 	waddstr(wnd, line);
581 }
582 #endif
583 
584 /*
585  * Construct an Internet address representation.
586  * If the nflag has been supplied, give
587  * numeric value, otherwise try for symbolic name.
588  */
589 static const char *
590 inetname(struct in_addr in)
591 {
592 	char *cp = 0;
593 	static char line[50];
594 	struct hostent *hp;
595 	struct netent *np;
596 
597 	if (!nflag && in.s_addr != INADDR_ANY) {
598 		int net = inet_netof(in);
599 		int lna = inet_lnaof(in);
600 
601 		if (lna == INADDR_ANY) {
602 			np = getnetbyaddr(net, AF_INET);
603 			if (np)
604 				cp = np->n_name;
605 		}
606 		if (cp == 0) {
607 			hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
608 			if (hp)
609 				cp = hp->h_name;
610 		}
611 	}
612 	if (in.s_addr == INADDR_ANY)
613 		strlcpy(line, "*", sizeof(line));
614 	else if (cp)
615 		strlcpy(line, cp, sizeof(line));
616 	else {
617 		in.s_addr = ntohl(in.s_addr);
618 #define C(x)	((x) & 0xff)
619 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u",
620 		    C(in.s_addr >> 24), C(in.s_addr >> 16),
621 		    C(in.s_addr >> 8), C(in.s_addr));
622 #undef C
623 	}
624 	return (line);
625 }
626 
627 #ifdef INET6
628 static const char *
629 inet6name(struct in6_addr *in6)
630 {
631 	static char line[NI_MAXHOST];
632 	struct sockaddr_in6 sin6;
633 	int flags;
634 
635 	if (nflag)
636 		flags = NI_NUMERICHOST;
637 	else
638 		flags = 0;
639 	if (IN6_IS_ADDR_UNSPECIFIED(in6))
640 		return "*";
641 	memset(&sin6, 0, sizeof(sin6));
642 	sin6.sin6_family = AF_INET6;
643 	sin6.sin6_len = sizeof(struct sockaddr_in6);
644 	sin6.sin6_addr = *in6;
645 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
646 			line, sizeof(line), NULL, 0, flags) == 0)
647 		return line;
648 	return "?";
649 }
650 #endif
651 
652 /* please note: there are also some netstat commands in netcmds.c */
653 
654 void
655 netstat_all(char *args)
656 {
657 	aflag = !aflag;
658 	fetchnetstat();
659 	shownetstat();
660 	refresh();
661 }
662 
663 void
664 netstat_names(char *args)
665 {
666 
667 	if (nflag == 0)
668 		return;
669 
670 	nflag = 0;
671 	wclear(wnd);
672 	labelnetstat();
673 	shownetstat();
674 	refresh();
675 }
676 
677 void
678 netstat_numbers(char *args)
679 {
680 
681 	if (nflag != 0)
682 		return;
683 
684 	nflag = 1;
685 	wclear(wnd);
686 	labelnetstat();
687 	shownetstat();
688 	refresh();
689 }
690