Home | History | Annotate | Line # | Download | only in systat
netstat.c revision 1.14
      1 /*	$NetBSD: netstat.c,v 1.14 2000/01/05 11:50:21 itojun Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)netstat.c	8.1 (Berkeley) 6/6/93";
     40 #endif
     41 __RCSID("$NetBSD: netstat.c,v 1.14 2000/01/05 11:50:21 itojun Exp $");
     42 #endif /* not lint */
     43 
     44 /*
     45  * netstat
     46  */
     47 #include <sys/param.h>
     48 #include <sys/socket.h>
     49 #include <sys/socketvar.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/protosw.h>
     52 
     53 #include <netinet/in.h>
     54 
     55 #include <arpa/inet.h>
     56 #include <net/route.h>
     57 
     58 #include <netinet/in_systm.h>
     59 #include <netinet/ip.h>
     60 #include <netinet/in_pcb.h>
     61 #include <netinet/ip_icmp.h>
     62 #include <netinet/icmp_var.h>
     63 #include <netinet/ip_var.h>
     64 #include <netinet/tcp.h>
     65 #include <netinet/tcpip.h>
     66 #include <netinet/tcp_seq.h>
     67 #define TCPSTATES
     68 #include <netinet/tcp_fsm.h>
     69 #include <netinet/tcp_timer.h>
     70 #include <netinet/tcp_var.h>
     71 #include <netinet/tcp_debug.h>
     72 #include <netinet/udp.h>
     73 #include <netinet/udp_var.h>
     74 
     75 #include <netdb.h>
     76 #include <stdlib.h>
     77 #include <string.h>
     78 #include <nlist.h>
     79 #include <paths.h>
     80 #include "systat.h"
     81 #include "extern.h"
     82 
     83 static void enter __P((struct inpcb *, struct socket *, int, char *));
     84 static char *inetname __P((struct in_addr));
     85 static void inetprint __P((struct in_addr *, int, char *));
     86 
     87 #define	streq(a,b)	(strcmp(a,b)==0)
     88 
     89 struct netinfo {
     90 	struct	netinfo *ni_forw, *ni_prev;
     91 	short	ni_line;		/* line on screen */
     92 	short	ni_seen;		/* 0 when not present in list */
     93 	short	ni_flags;
     94 #define	NIF_LACHG	0x1		/* local address changed */
     95 #define	NIF_FACHG	0x2		/* foreign address changed */
     96 	short	ni_state;		/* tcp state */
     97 	char	*ni_proto;		/* protocol */
     98 	struct	in_addr ni_laddr;	/* local address */
     99 	long	ni_lport;		/* local port */
    100 	struct	in_addr	ni_faddr;	/* foreign address */
    101 	long	ni_fport;		/* foreign port */
    102 	long	ni_rcvcc;		/* rcv buffer character count */
    103 	long	ni_sndcc;		/* snd buffer character count */
    104 };
    105 
    106 static struct {
    107 	struct	netinfo *ni_forw, *ni_prev;
    108 } netcb;
    109 
    110 static	int aflag = 0;
    111 static	int nflag = 0;
    112 static	int lastrow = 1;
    113 
    114 WINDOW *
    115 opennetstat()
    116 {
    117 
    118 	sethostent(1);
    119 	setnetent(1);
    120 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
    121 }
    122 
    123 void
    124 closenetstat(w)
    125 	WINDOW *w;
    126 {
    127 	struct netinfo *p;
    128 
    129 	endhostent();
    130 	endnetent();
    131 	p = (struct netinfo *)netcb.ni_forw;
    132 	while (p != (struct netinfo *)&netcb) {
    133 		if (p->ni_line != -1)
    134 			lastrow--;
    135 		p->ni_line = -1;
    136 		p = p->ni_forw;
    137 	}
    138 	if (w != NULL) {
    139 		wclear(w);
    140 		wrefresh(w);
    141 		delwin(w);
    142 	}
    143 }
    144 
    145 static struct nlist namelist[] = {
    146 #define	X_TCBTABLE	0
    147 	{ "_tcbtable" },
    148 #define	X_UDBTABLE	1
    149 	{ "_udbtable" },
    150 	{ "" },
    151 };
    152 
    153 int
    154 initnetstat()
    155 {
    156 	if (kvm_nlist(kd, namelist)) {
    157 		nlisterr(namelist);
    158 		return(0);
    159 	}
    160 	if (namelist[X_TCBTABLE].n_value == 0) {
    161 		error("No symbols in namelist");
    162 		return(0);
    163 	}
    164 	netcb.ni_forw = netcb.ni_prev = (struct netinfo *)&netcb;
    165 	protos = TCP|UDP;
    166 	return(1);
    167 }
    168 
    169 void
    170 fetchnetstat()
    171 {
    172 	struct inpcbtable pcbtable;
    173 	struct inpcb *head, *prev, *next;
    174 	struct netinfo *p;
    175 	struct inpcb inpcb;
    176 	struct socket sockb;
    177 	struct tcpcb tcpcb;
    178 	void *off;
    179 	int istcp;
    180 
    181 	if (namelist[X_TCBTABLE].n_value == 0)
    182 		return;
    183 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw)
    184 		p->ni_seen = 0;
    185 	if (protos&TCP) {
    186 		off = NPTR(X_TCBTABLE);
    187 		istcp = 1;
    188 	}
    189 	else if (protos&UDP) {
    190 		off = NPTR(X_UDBTABLE);
    191 		istcp = 0;
    192 	}
    193 	else {
    194 		error("No protocols to display");
    195 		return;
    196 	}
    197 again:
    198 	KREAD(off, &pcbtable, sizeof pcbtable);
    199 	prev = head = (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue;
    200 	next = pcbtable.inpt_queue.cqh_first;
    201 	while (next != head) {
    202 		KREAD(next, &inpcb, sizeof (inpcb));
    203 		if (inpcb.inp_queue.cqe_prev != prev) {
    204 printf("prev = %p, head = %p, next = %p, inpcb...prev = %p\n", prev, head, next, inpcb.inp_queue.cqe_prev);
    205 			p = netcb.ni_forw;
    206 			for (; p != (struct netinfo *)&netcb; p = p->ni_forw)
    207 				p->ni_seen = 1;
    208 			error("Kernel state in transition");
    209 			return;
    210 		}
    211 		prev = next;
    212 		next = inpcb.inp_queue.cqe_next;
    213 
    214 		if (!aflag && inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
    215 			continue;
    216 		if (nhosts && !checkhost(&inpcb))
    217 			continue;
    218 		if (nports && !checkport(&inpcb))
    219 			continue;
    220 		KREAD(inpcb.inp_socket, &sockb, sizeof (sockb));
    221 		if (istcp) {
    222 			KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
    223 			enter(&inpcb, &sockb, tcpcb.t_state, "tcp");
    224 		} else
    225 			enter(&inpcb, &sockb, 0, "udp");
    226 	}
    227 	if (istcp && (protos&UDP)) {
    228 		istcp = 0;
    229 		off = NPTR(X_UDBTABLE);
    230 		goto again;
    231 	}
    232 }
    233 
    234 static void
    235 enter(inp, so, state, proto)
    236 	struct inpcb *inp;
    237 	struct socket *so;
    238 	int state;
    239 	char *proto;
    240 {
    241 	struct netinfo *p;
    242 
    243 	/*
    244 	 * Only take exact matches, any sockets with
    245 	 * previously unbound addresses will be deleted
    246 	 * below in the display routine because they
    247 	 * will appear as ``not seen'' in the kernel
    248 	 * data structures.
    249 	 */
    250 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
    251 		if (!streq(proto, p->ni_proto))
    252 			continue;
    253 		if (p->ni_lport != inp->inp_lport ||
    254 		    p->ni_laddr.s_addr != inp->inp_laddr.s_addr)
    255 			continue;
    256 		if (p->ni_faddr.s_addr == inp->inp_faddr.s_addr &&
    257 		    p->ni_fport == inp->inp_fport)
    258 			break;
    259 	}
    260 	if (p == (struct netinfo *)&netcb) {
    261 		if ((p = malloc(sizeof(*p))) == NULL) {
    262 			error("Out of memory");
    263 			return;
    264 		}
    265 		p->ni_prev = (struct netinfo *)&netcb;
    266 		p->ni_forw = netcb.ni_forw;
    267 		netcb.ni_forw->ni_prev = p;
    268 		netcb.ni_forw = p;
    269 		p->ni_line = -1;
    270 		p->ni_laddr = inp->inp_laddr;
    271 		p->ni_lport = inp->inp_lport;
    272 		p->ni_faddr = inp->inp_faddr;
    273 		p->ni_fport = inp->inp_fport;
    274 		p->ni_proto = proto;
    275 		p->ni_flags = NIF_LACHG|NIF_FACHG;
    276 	}
    277 	p->ni_rcvcc = so->so_rcv.sb_cc;
    278 	p->ni_sndcc = so->so_snd.sb_cc;
    279 	p->ni_state = state;
    280 	p->ni_seen = 1;
    281 }
    282 
    283 /* column locations */
    284 #define	LADDR	0
    285 #define	FADDR	LADDR+23
    286 #define	PROTO	FADDR+23
    287 #define	RCVCC	PROTO+6
    288 #define	SNDCC	RCVCC+7
    289 #define	STATE	SNDCC+7
    290 
    291 void
    292 labelnetstat()
    293 {
    294 
    295 	if (namelist[X_TCBTABLE].n_type == 0)
    296 		return;
    297 	wmove(wnd, 0, 0); wclrtobot(wnd);
    298 	mvwaddstr(wnd, 0, LADDR, "Local Address");
    299 	mvwaddstr(wnd, 0, FADDR, "Foreign Address");
    300 	mvwaddstr(wnd, 0, PROTO, "Proto");
    301 	mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
    302 	mvwaddstr(wnd, 0, SNDCC, "Send-Q");
    303 	mvwaddstr(wnd, 0, STATE, "(state)");
    304 }
    305 
    306 void
    307 shownetstat()
    308 {
    309 	struct netinfo *p, *q;
    310 
    311 	/*
    312 	 * First, delete any connections that have gone
    313 	 * away and adjust the position of connections
    314 	 * below to reflect the deleted line.
    315 	 */
    316 	p = netcb.ni_forw;
    317 	while (p != (struct netinfo *)&netcb) {
    318 		if (p->ni_line == -1 || p->ni_seen) {
    319 			p = p->ni_forw;
    320 			continue;
    321 		}
    322 		wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
    323 		q = netcb.ni_forw;
    324 		for (; q != (struct netinfo *)&netcb; q = q->ni_forw)
    325 			if (q != p && q->ni_line > p->ni_line) {
    326 				q->ni_line--;
    327 				/* this shouldn't be necessary */
    328 				q->ni_flags |= NIF_LACHG|NIF_FACHG;
    329 			}
    330 		lastrow--;
    331 		q = p->ni_forw;
    332 		p->ni_prev->ni_forw = p->ni_forw;
    333 		p->ni_forw->ni_prev = p->ni_prev;
    334 		free(p);
    335 		p = q;
    336 	}
    337 	/*
    338 	 * Update existing connections and add new ones.
    339 	 */
    340 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
    341 		if (p->ni_line == -1) {
    342 			/*
    343 			 * Add a new entry if possible.
    344 			 */
    345 			if (lastrow > getmaxy(wnd))
    346 				continue;
    347 			p->ni_line = lastrow++;
    348 			p->ni_flags |= NIF_LACHG|NIF_FACHG;
    349 		}
    350 		if (p->ni_flags & NIF_LACHG) {
    351 			wmove(wnd, p->ni_line, LADDR);
    352 			inetprint(&p->ni_laddr, p->ni_lport, p->ni_proto);
    353 			p->ni_flags &= ~NIF_LACHG;
    354 		}
    355 		if (p->ni_flags & NIF_FACHG) {
    356 			wmove(wnd, p->ni_line, FADDR);
    357 			inetprint(&p->ni_faddr, p->ni_fport, p->ni_proto);
    358 			p->ni_flags &= ~NIF_FACHG;
    359 		}
    360 		mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto);
    361 		mvwprintw(wnd, p->ni_line, RCVCC, "%6d", p->ni_rcvcc);
    362 		mvwprintw(wnd, p->ni_line, SNDCC, "%6d", p->ni_sndcc);
    363 		if (streq(p->ni_proto, "tcp")) {
    364 			if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
    365 				mvwprintw(wnd, p->ni_line, STATE, "%d",
    366 				    p->ni_state);
    367 			else
    368 				mvwaddstr(wnd, p->ni_line, STATE,
    369 				    tcpstates[p->ni_state]);
    370 		}
    371 		wclrtoeol(wnd);
    372 	}
    373 	if (lastrow < getmaxy(wnd)) {
    374 		wmove(wnd, lastrow, 0); wclrtobot(wnd);
    375 		wmove(wnd, getmaxy(wnd), 0); wdeleteln(wnd);	/* XXX */
    376 	}
    377 }
    378 
    379 /*
    380  * Pretty print an Internet address (net address + port).
    381  * If the nflag was specified, use numbers instead of names.
    382  */
    383 static void
    384 inetprint(in, port, proto)
    385 	struct in_addr *in;
    386 	int port;
    387 	char *proto;
    388 {
    389 	struct servent *sp = 0;
    390 	char line[80], *cp;
    391 
    392 	(void)snprintf(line, sizeof line, "%.*s.", 16, inetname(*in));
    393 	cp = strchr(line, '\0');
    394 	if (!nflag && port)
    395 		sp = getservbyport(port, proto);
    396 	if (sp || port == 0)
    397 		(void)snprintf(cp, line + sizeof line - cp, "%.8s",
    398 		     sp ? sp->s_name : "*");
    399 	else
    400 		(void)snprintf(cp, line + sizeof line - cp, "%d",
    401 		     ntohs((u_short)port));
    402 	/* pad to full column to clear any garbage */
    403 	cp = strchr(line, '\0');
    404 	while (cp - line < 22)
    405 		*cp++ = ' ';
    406 	*cp = '\0';
    407 	waddstr(wnd, line);
    408 }
    409 
    410 /*
    411  * Construct an Internet address representation.
    412  * If the nflag has been supplied, give
    413  * numeric value, otherwise try for symbolic name.
    414  */
    415 static char *
    416 inetname(in)
    417 	struct in_addr in;
    418 {
    419 	char *cp = 0;
    420 	static char line[50];
    421 	struct hostent *hp;
    422 	struct netent *np;
    423 
    424 	if (!nflag && in.s_addr != INADDR_ANY) {
    425 		int net = inet_netof(in);
    426 		int lna = inet_lnaof(in);
    427 
    428 		if (lna == INADDR_ANY) {
    429 			np = getnetbyaddr(net, AF_INET);
    430 			if (np)
    431 				cp = np->n_name;
    432 		}
    433 		if (cp == 0) {
    434 			hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
    435 			if (hp)
    436 				cp = hp->h_name;
    437 		}
    438 	}
    439 	if (in.s_addr == INADDR_ANY)
    440 		strncpy(line, "*", sizeof(line) - 1);
    441 	else if (cp)
    442 		strncpy(line, cp, sizeof(line) - 1);
    443 	else {
    444 		in.s_addr = ntohl(in.s_addr);
    445 #define C(x)	((x) & 0xff)
    446 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u",
    447 		    C(in.s_addr >> 24), C(in.s_addr >> 16),
    448 		    C(in.s_addr >> 8), C(in.s_addr));
    449 #undef C
    450 	}
    451 	line[sizeof(line) - 1] = '\0';
    452 	return (line);
    453 }
    454 
    455 /* please note: there are also some netstat commands in netcmds.c */
    456 
    457 void
    458 netstat_all (args)
    459 	char *args;
    460 {
    461 	aflag = !aflag;
    462 	fetchnetstat();
    463 	shownetstat();
    464 	refresh();
    465 }
    466 
    467 void
    468 netstat_names (args)
    469 	char *args;
    470 {
    471 	struct netinfo *p;
    472 
    473 	if (nflag == 0)
    474 		return;
    475 
    476 	p = netcb.ni_forw;
    477 	for (; p != (struct netinfo *)&netcb; p = p->ni_forw) {
    478 		if (p->ni_line == -1)
    479 			continue;
    480 		p->ni_flags |= NIF_LACHG|NIF_FACHG;
    481 	}
    482 	nflag = 0;
    483 	wclear(wnd);
    484 	labelnetstat();
    485 	shownetstat();
    486 	refresh();
    487 }
    488 
    489 void
    490 netstat_numbers (args)
    491 	char *args;
    492 {
    493 	struct netinfo *p;
    494 
    495 	if (nflag != 0)
    496 		return;
    497 
    498 	p = netcb.ni_forw;
    499 	for (; p != (struct netinfo *)&netcb; p = p->ni_forw) {
    500 		if (p->ni_line == -1)
    501 			continue;
    502 		p->ni_flags |= NIF_LACHG|NIF_FACHG;
    503 	}
    504 	nflag = 1;
    505 	wclear(wnd);
    506 	labelnetstat();
    507 	shownetstat();
    508 	refresh();
    509 }
    510