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