xref: /netbsd-src/usr.bin/systat/netstat.c (revision be0f86d642f3e7603459b502bf711150feb47410)
1 /*	$NetBSD: netstat.c,v 1.32 2022/10/28 05:27:17 ozaki-r 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.32 2022/10/28 05:27:17 ozaki-r 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/tcp_seq.h>
65 #define TCPSTATES
66 #include <netinet/tcp_fsm.h>
67 #include <netinet/tcp_timer.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/tcp_debug.h>
70 #include <netinet/udp.h>
71 #include <netinet/udp_var.h>
72 
73 #include <netdb.h>
74 #include <stdlib.h>
75 #include <string.h>
76 
77 #include "systat.h"
78 #include "extern.h"
79 
80 static void fetchnetstat4(void *, int);
81 static void enter(struct inpcb *, struct socket *, int, const char *);
82 static const char *inetname(struct in_addr);
83 static void inetprint(struct in_addr *, int, const char *);
84 #ifdef INET6
85 static void fetchnetstat6(void *, int);
86 static void enter6(struct inpcb *, struct socket *, int, const char *);
87 static const char *inet6name(struct in6_addr *);
88 static void inet6print(struct in6_addr *, int, const char *);
89 #endif
90 
91 #define	streq(a,b)	(strcmp(a,b)==0)
92 
93 struct netinfo {
94 	struct	netinfo *ni_forw, *ni_prev;
95 	int	ni_family;
96 	short	ni_line;		/* line on screen */
97 	short	ni_seen;		/* 0 when not present in list */
98 	short	ni_flags;
99 #define	NIF_LACHG	0x1		/* local address changed */
100 #define	NIF_FACHG	0x2		/* foreign address changed */
101 	short	ni_state;		/* tcp state */
102 	const char	*ni_proto;		/* protocol */
103 	struct	in_addr ni_laddr;	/* local address */
104 #ifdef INET6
105 	struct	in6_addr ni_laddr6;	/* local address */
106 #endif
107 	long	ni_lport;		/* local port */
108 	struct	in_addr	ni_faddr;	/* foreign address */
109 #ifdef INET6
110 	struct	in6_addr ni_faddr6;	/* foreign address */
111 #endif
112 	long	ni_fport;		/* foreign port */
113 	long	ni_rcvcc;		/* rcv buffer character count */
114 	long	ni_sndcc;		/* snd buffer character count */
115 };
116 
117 static struct {
118 	struct	netinfo *ni_forw, *ni_prev;
119 } netcb;
120 
121 struct netinfo *nhead;
122 
123 static	int aflag = 0;
124 int nflag = 0;
125 static	int lastrow = 1;
126 
127 WINDOW *
opennetstat(void)128 opennetstat(void)
129 {
130 
131 	sethostent(1);
132 	setnetent(1);
133 	return (subwin(stdscr, -1, 0, 5, 0));
134 }
135 
136 void
closenetstat(WINDOW * w)137 closenetstat(WINDOW *w)
138 {
139 	struct netinfo *p;
140 
141 	endhostent();
142 	endnetent();
143 	p = netcb.ni_forw;
144 	while (p != nhead) {
145 		if (p->ni_line != -1)
146 			lastrow--;
147 		p->ni_line = -1;
148 		p = p->ni_forw;
149 	}
150 	if (w != NULL) {
151 		wclear(w);
152 		wrefresh(w);
153 		delwin(w);
154 	}
155 }
156 
157 static struct nlist namelist[] = {
158 #define	X_TCBTABLE	0
159 	{ .n_name = "_tcbtable" },
160 #define	X_UDBTABLE	1
161 	{ .n_name = "_udbtable" },
162 	{ .n_name = NULL },
163 };
164 
165 int
initnetstat(void)166 initnetstat(void)
167 {
168 	int n;
169 
170 	n = kvm_nlist(kd, namelist);
171 	if (n < 0) {
172 		nlisterr(namelist);
173 		return(0);
174 	} else if (n == sizeof(namelist) / sizeof(namelist[0]) - 1) {
175 		error("No symbols in namelist");
176 		return(0);
177 	}
178 
179 	nhead = (struct netinfo *)(void *)&netcb;
180 
181 	netcb.ni_forw = netcb.ni_prev = nhead;
182 	protos = TCP|UDP;
183 	return(1);
184 }
185 
186 void
fetchnetstat(void)187 fetchnetstat(void)
188 {
189 	struct netinfo *p;
190 
191 	if (namelist[X_TCBTABLE].n_value == 0)
192 		return;
193 	for (p = netcb.ni_forw; p != nhead; p = p->ni_forw)
194 		p->ni_seen = 0;
195 
196 	if ((protos & (TCP | UDP)) == 0) {
197 		error("No protocols to display");
198 		return;
199 	}
200 	if ((protos & TCP) && namelist[X_TCBTABLE].n_type)
201 		fetchnetstat4(NPTR(X_TCBTABLE), 1);
202 	if ((protos & UDP) && namelist[X_UDBTABLE].n_type)
203 		fetchnetstat4(NPTR(X_UDBTABLE), 0);
204 #ifdef INET6
205 	if ((protos & TCP) && namelist[X_TCBTABLE].n_type)
206 		fetchnetstat6(NPTR(X_TCBTABLE), 1);
207 	if ((protos & UDP) && namelist[X_UDBTABLE].n_type)
208 		fetchnetstat6(NPTR(X_UDBTABLE), 0);
209 #endif
210 }
211 
212 static void
fetchnetstat4(void * off,int istcp)213 fetchnetstat4(void *off, int istcp)
214 {
215 	struct inpcbtable pcbtable;
216 	struct inpcb **pprev, *next;
217 	struct netinfo *p;
218 	struct inpcb *inpcbp, *inp;
219 	struct in4pcb in4pcb;
220 	struct socket sockb;
221 	struct tcpcb tcpcb;
222 
223 	KREAD(off, &pcbtable, sizeof pcbtable);
224 	pprev = &((struct inpcbtable *)off)->inpt_queue.tqh_first;
225 	next = TAILQ_FIRST(&pcbtable.inpt_queue);
226 	while (next != TAILQ_END(&pcbtable.inpt_queue)) {
227 		inpcbp = (struct inpcb *)next;
228 		KREAD(inpcbp, &in4pcb, sizeof (in4pcb));
229 		inp = (struct inpcb *)&in4pcb;
230 		if (inp->inp_queue.tqe_prev != pprev) {
231 			for (p = netcb.ni_forw; p != nhead; p = p->ni_forw)
232 				p->ni_seen = 1;
233 			error("Kernel state in transition");
234 			return;
235 		}
236 		pprev = &next->inp_queue.tqe_next;
237 		next = inp->inp_queue.tqe_next;
238 
239 		if (inp->inp_af != AF_INET)
240 			continue;
241 		if (!aflag && inet_lnaof(in4p_laddr(inp)) == INADDR_ANY)
242 			continue;
243 		if (nhosts && !checkhost(inp))
244 			continue;
245 		if (nports && !checkport(inp))
246 			continue;
247 		KREAD(inp->inp_socket, &sockb, sizeof (sockb));
248 		if (istcp) {
249 			KREAD(inp->inp_ppcb, &tcpcb, sizeof (tcpcb));
250 			enter(inp, &sockb, tcpcb.t_state, "tcp");
251 		} else
252 			enter(inp, &sockb, 0, "udp");
253 	}
254 }
255 
256 #ifdef INET6
257 static void
fetchnetstat6(void * off,int istcp)258 fetchnetstat6(void *off, int istcp)
259 {
260 	struct inpcbtable pcbtable;
261 	struct inpcb **pprev, *next;
262 	struct netinfo *p;
263 	struct socket sockb;
264 	struct tcpcb tcpcb;
265 	struct inpcb *inp, *inpcbp;
266 	struct in6pcb in6pcb;
267 
268 	KREAD(off, &pcbtable, sizeof pcbtable);
269 	pprev = &((struct inpcbtable *)off)->inpt_queue.tqh_first;
270 	next = TAILQ_FIRST(&pcbtable.inpt_queue);
271 	while (next != TAILQ_END(&pcbtable.inpt_queue)) {
272 		inpcbp = (struct inpcb *)next;
273 		KREAD(inpcbp, &in6pcb, sizeof (in6pcb));
274 		inp = (struct inpcb *)&in6pcb;
275 		if (inp->inp_queue.tqe_prev != pprev) {
276 			for (p = netcb.ni_forw; p != nhead; p = p->ni_forw)
277 				p->ni_seen = 1;
278 			error("Kernel state in transition");
279 			return;
280 		}
281 		pprev = &next->inp_queue.tqe_next;
282 		next = inp->inp_queue.tqe_next;
283 
284 		if (inp->inp_af != AF_INET6)
285 			continue;
286 		if (!aflag && IN6_IS_ADDR_UNSPECIFIED(&in6p_laddr(inp)))
287 			continue;
288 		if (nhosts && !checkhost(inp))
289 			continue;
290 		if (nports && !checkport(inp))
291 			continue;
292 		KREAD(inp->inp_socket, &sockb, sizeof (sockb));
293 		if (istcp) {
294 			KREAD(inp->inp_ppcb, &tcpcb, sizeof (tcpcb));
295 			enter6(inp, &sockb, tcpcb.t_state, "tcp");
296 		} else
297 			enter6(inp, &sockb, 0, "udp");
298 	}
299 }
300 #endif /*INET6*/
301 
302 static void
enter(struct inpcb * inp,struct socket * so,int state,const char * proto)303 enter(struct inpcb *inp, struct socket *so, int state, const 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 != nhead; 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 != in4p_laddr(inp).s_addr)
321 			continue;
322 		if (p->ni_faddr.s_addr == in4p_faddr(inp).s_addr &&
323 		    p->ni_fport == inp->inp_fport)
324 			break;
325 	}
326 	if (p == nhead) {
327 		if ((p = malloc(sizeof(*p))) == NULL) {
328 			error("Out of memory");
329 			return;
330 		}
331 		p->ni_prev = nhead;
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 = in4p_laddr(inp);
337 		p->ni_lport = inp->inp_lport;
338 		p->ni_faddr = in4p_faddr(inp);
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
enter6(struct inpcb * inp,struct socket * so,int state,const char * proto)352 enter6(struct inpcb *inp, struct socket *so, int state, const 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 != nhead; 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 != inp->inp_lport ||
369 		    !IN6_ARE_ADDR_EQUAL(&p->ni_laddr6, &in6p_laddr(inp)))
370 			continue;
371 		if (IN6_ARE_ADDR_EQUAL(&p->ni_faddr6, &in6p_faddr(inp)) &&
372 		    p->ni_fport == inp->inp_fport)
373 			break;
374 	}
375 	if (p == nhead) {
376 		if ((p = malloc(sizeof(*p))) == NULL) {
377 			error("Out of memory");
378 			return;
379 		}
380 		p->ni_prev = nhead;
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_laddr(inp);
386 		p->ni_lport = inp->inp_lport;
387 		p->ni_faddr6 = in6p_faddr(inp);
388 		p->ni_fport = inp->inp_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
labelnetstat(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 	for (p = netcb.ni_forw; p != nhead; p = p->ni_forw) {
424 		if (p->ni_line == -1)
425 			continue;
426 		p->ni_flags |= NIF_LACHG | NIF_FACHG;
427 	}
428 }
429 
430 void
shownetstat(void)431 shownetstat(void)
432 {
433 	struct netinfo *p, *q;
434 
435 	/*
436 	 * First, delete any connections that have gone
437 	 * away and adjust the position of connections
438 	 * below to reflect the deleted line.
439 	 */
440 	p = netcb.ni_forw;
441 	while (p != nhead) {
442 		if (p->ni_line == -1 || p->ni_seen) {
443 			p = p->ni_forw;
444 			continue;
445 		}
446 		wmove(wnd, p->ni_line, 0);
447 		wdeleteln(wnd);
448 		for (q = netcb.ni_forw; q != nhead; q = q->ni_forw)
449 			if (q != p && q->ni_line > p->ni_line) {
450 				q->ni_line--;
451 				/* this shouldn't be necessary */
452 				q->ni_flags |= NIF_LACHG | NIF_FACHG;
453 			}
454 		lastrow--;
455 		q = p->ni_forw;
456 		p->ni_prev->ni_forw = p->ni_forw;
457 		p->ni_forw->ni_prev = p->ni_prev;
458 		free(p);
459 		p = q;
460 	}
461 	/*
462 	 * Update existing connections and add new ones.
463 	 */
464 	for (p = netcb.ni_forw; p != nhead; p = p->ni_forw) {
465 		if (p->ni_line == -1) {
466 			/*
467 			 * Add a new entry if possible.
468 			 */
469 			if (lastrow > getmaxy(wnd))
470 				continue;
471 			p->ni_line = lastrow++;
472 			p->ni_flags |= NIF_LACHG | NIF_FACHG;
473 		}
474 		if (p->ni_flags & NIF_LACHG) {
475 			wmove(wnd, p->ni_line, LADDR);
476 			switch (p->ni_family) {
477 			case AF_INET:
478 				inetprint(&p->ni_laddr, p->ni_lport,
479 					p->ni_proto);
480 				break;
481 #ifdef INET6
482 			case AF_INET6:
483 				inet6print(&p->ni_laddr6, p->ni_lport,
484 					p->ni_proto);
485 				break;
486 #endif
487 			}
488 			p->ni_flags &= ~NIF_LACHG;
489 		}
490 		if (p->ni_flags & NIF_FACHG) {
491 			wmove(wnd, p->ni_line, FADDR);
492 			switch (p->ni_family) {
493 			case AF_INET:
494 				inetprint(&p->ni_faddr, p->ni_fport,
495 					p->ni_proto);
496 				break;
497 #ifdef INET6
498 			case AF_INET6:
499 				inet6print(&p->ni_faddr6, p->ni_fport,
500 					p->ni_proto);
501 				break;
502 #endif
503 			}
504 			p->ni_flags &= ~NIF_FACHG;
505 		}
506 		mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto);
507 #ifdef INET6
508 		if (p->ni_family == AF_INET6)
509 			waddstr(wnd, "6");
510 #endif
511 		mvwprintw(wnd, p->ni_line, RCVCC, "%6ld", p->ni_rcvcc);
512 		mvwprintw(wnd, p->ni_line, SNDCC, "%6ld", p->ni_sndcc);
513 		if (streq(p->ni_proto, "tcp")) {
514 			if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
515 				mvwprintw(wnd, p->ni_line, STATE, "%d",
516 				    p->ni_state);
517 			else
518 				mvwaddstr(wnd, p->ni_line, STATE,
519 				    tcpstates[p->ni_state]);
520 		}
521 		wclrtoeol(wnd);
522 	}
523 	if (lastrow < getmaxy(wnd)) {
524 		wmove(wnd, lastrow, 0); wclrtobot(wnd);
525 		wmove(wnd, getmaxy(wnd), 0); wdeleteln(wnd);	/* XXX */
526 	}
527 }
528 
529 /*
530  * Pretty print an Internet address (net address + port).
531  * If the nflag was specified, use numbers instead of names.
532  */
533 static void
inetprint(struct in_addr * in,int port,const char * proto)534 inetprint(struct in_addr *in, int port, const char *proto)
535 {
536 	struct servent *sp = 0;
537 	char line[80], *cp;
538 
539 	(void)snprintf(line, sizeof line, "%.*s.", 16, inetname(*in));
540 	cp = strchr(line, '\0');
541 	if (!nflag && port)
542 		sp = getservbyport(port, proto);
543 	if (sp || port == 0)
544 		(void)snprintf(cp, line + sizeof line - cp, "%.8s",
545 		     sp ? sp->s_name : "*");
546 	else
547 		(void)snprintf(cp, line + sizeof line - cp, "%d",
548 		     ntohs((u_short)port));
549 	/* pad to full column to clear any garbage */
550 	cp = strchr(line, '\0');
551 	while (cp - line < 22)
552 		*cp++ = ' ';
553 	*cp = '\0';
554 	waddstr(wnd, line);
555 }
556 
557 #ifdef INET6
558 static void
inet6print(struct in6_addr * in6,int port,const char * proto)559 inet6print(struct in6_addr *in6, int port, const char *proto)
560 {
561 	struct servent *sp = 0;
562 	char line[80], *cp;
563 
564 	(void)snprintf(line, sizeof line, "%.*s.", 16, inet6name(in6));
565 	cp = strchr(line, '\0');
566 	if (!nflag && port)
567 		sp = getservbyport(port, proto);
568 	if (sp || port == 0)
569 		(void)snprintf(cp, line + sizeof line - cp, "%.8s",
570 		     sp ? sp->s_name : "*");
571 	else
572 		(void)snprintf(cp, line + sizeof line - cp, "%d",
573 		     ntohs((u_short)port));
574 	/* pad to full column to clear any garbage */
575 	cp = strchr(line, '\0');
576 	while (cp - line < 22)
577 		*cp++ = ' ';
578 	*cp = '\0';
579 	waddstr(wnd, line);
580 }
581 #endif
582 
583 /*
584  * Construct an Internet address representation.
585  * If the nflag has been supplied, give
586  * numeric value, otherwise try for symbolic name.
587  */
588 static const char *
inetname(struct in_addr in)589 inetname(struct in_addr in)
590 {
591 	char *cp = 0;
592 	static char line[50];
593 	struct hostent *hp;
594 	struct netent *np;
595 
596 	if (!nflag && in.s_addr != INADDR_ANY) {
597 		int net = inet_netof(in);
598 		int lna = inet_lnaof(in);
599 
600 		if (lna == INADDR_ANY) {
601 			np = getnetbyaddr(net, AF_INET);
602 			if (np)
603 				cp = np->n_name;
604 		}
605 		if (cp == 0) {
606 			hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
607 			if (hp)
608 				cp = hp->h_name;
609 		}
610 	}
611 	if (in.s_addr == INADDR_ANY)
612 		strlcpy(line, "*", sizeof(line));
613 	else if (cp)
614 		strlcpy(line, cp, sizeof(line));
615 	else {
616 		in.s_addr = ntohl(in.s_addr);
617 #define C(x)	((x) & 0xff)
618 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u",
619 		    C(in.s_addr >> 24), C(in.s_addr >> 16),
620 		    C(in.s_addr >> 8), C(in.s_addr));
621 #undef C
622 	}
623 	return (line);
624 }
625 
626 #ifdef INET6
627 static const char *
inet6name(struct in6_addr * in6)628 inet6name(struct in6_addr *in6)
629 {
630 	static char line[NI_MAXHOST];
631 	struct sockaddr_in6 sin6;
632 	int flags;
633 
634 	if (nflag)
635 		flags = NI_NUMERICHOST;
636 	else
637 		flags = 0;
638 	if (IN6_IS_ADDR_UNSPECIFIED(in6))
639 		return "*";
640 	memset(&sin6, 0, sizeof(sin6));
641 	sin6.sin6_family = AF_INET6;
642 	sin6.sin6_len = sizeof(struct sockaddr_in6);
643 	sin6.sin6_addr = *in6;
644 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
645 			line, sizeof(line), NULL, 0, flags) == 0)
646 		return line;
647 	return "?";
648 }
649 #endif
650 
651 /* please note: there are also some netstat commands in netcmds.c */
652 
653 void
netstat_all(char * args)654 netstat_all(char *args)
655 {
656 	aflag = !aflag;
657 	fetchnetstat();
658 	shownetstat();
659 	refresh();
660 }
661 
662 void
netstat_names(char * args)663 netstat_names(char *args)
664 {
665 
666 	if (nflag == 0)
667 		return;
668 
669 	nflag = 0;
670 	wclear(wnd);
671 	labelnetstat();
672 	shownetstat();
673 	refresh();
674 }
675 
676 void
netstat_numbers(char * args)677 netstat_numbers(char *args)
678 {
679 
680 	if (nflag != 0)
681 		return;
682 
683 	nflag = 1;
684 	wclear(wnd);
685 	labelnetstat();
686 	shownetstat();
687 	refresh();
688 }
689