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