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