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