Home | History | Annotate | Line # | Download | only in systat
netstat.c revision 1.14
      1  1.14  itojun /*	$NetBSD: netstat.c,v 1.14 2000/01/05 11:50:21 itojun 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.14  itojun __RCSID("$NetBSD: netstat.c,v 1.14 2000/01/05 11:50:21 itojun 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.1     jtc #include <netinet/tcp.h>
     65   1.1     jtc #include <netinet/tcpip.h>
     66   1.1     jtc #include <netinet/tcp_seq.h>
     67   1.1     jtc #define TCPSTATES
     68   1.1     jtc #include <netinet/tcp_fsm.h>
     69   1.1     jtc #include <netinet/tcp_timer.h>
     70   1.1     jtc #include <netinet/tcp_var.h>
     71   1.1     jtc #include <netinet/tcp_debug.h>
     72   1.1     jtc #include <netinet/udp.h>
     73   1.1     jtc #include <netinet/udp_var.h>
     74   1.1     jtc 
     75   1.1     jtc #include <netdb.h>
     76   1.1     jtc #include <stdlib.h>
     77   1.1     jtc #include <string.h>
     78   1.1     jtc #include <nlist.h>
     79   1.1     jtc #include <paths.h>
     80   1.1     jtc #include "systat.h"
     81   1.1     jtc #include "extern.h"
     82   1.1     jtc 
     83   1.1     jtc static void enter __P((struct inpcb *, struct socket *, int, char *));
     84   1.1     jtc static char *inetname __P((struct in_addr));
     85   1.1     jtc static void inetprint __P((struct in_addr *, int, char *));
     86   1.1     jtc 
     87   1.1     jtc #define	streq(a,b)	(strcmp(a,b)==0)
     88   1.1     jtc 
     89   1.1     jtc struct netinfo {
     90   1.1     jtc 	struct	netinfo *ni_forw, *ni_prev;
     91   1.1     jtc 	short	ni_line;		/* line on screen */
     92   1.1     jtc 	short	ni_seen;		/* 0 when not present in list */
     93   1.1     jtc 	short	ni_flags;
     94   1.1     jtc #define	NIF_LACHG	0x1		/* local address changed */
     95   1.1     jtc #define	NIF_FACHG	0x2		/* foreign address changed */
     96   1.1     jtc 	short	ni_state;		/* tcp state */
     97   1.1     jtc 	char	*ni_proto;		/* protocol */
     98   1.1     jtc 	struct	in_addr ni_laddr;	/* local address */
     99   1.1     jtc 	long	ni_lport;		/* local port */
    100   1.1     jtc 	struct	in_addr	ni_faddr;	/* foreign address */
    101   1.1     jtc 	long	ni_fport;		/* foreign port */
    102   1.1     jtc 	long	ni_rcvcc;		/* rcv buffer character count */
    103   1.1     jtc 	long	ni_sndcc;		/* snd buffer character count */
    104   1.1     jtc };
    105   1.1     jtc 
    106   1.1     jtc static struct {
    107   1.1     jtc 	struct	netinfo *ni_forw, *ni_prev;
    108   1.1     jtc } netcb;
    109   1.1     jtc 
    110   1.1     jtc static	int aflag = 0;
    111   1.1     jtc static	int nflag = 0;
    112   1.1     jtc static	int lastrow = 1;
    113   1.1     jtc 
    114   1.9     mrg WINDOW *
    115   1.9     mrg opennetstat()
    116   1.9     mrg {
    117   1.9     mrg 
    118   1.9     mrg 	sethostent(1);
    119   1.9     mrg 	setnetent(1);
    120   1.9     mrg 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
    121   1.9     mrg }
    122   1.9     mrg 
    123   1.1     jtc void
    124   1.1     jtc closenetstat(w)
    125   1.5  scottr 	WINDOW *w;
    126   1.1     jtc {
    127   1.8   lukem 	struct netinfo *p;
    128   1.1     jtc 
    129   1.1     jtc 	endhostent();
    130   1.1     jtc 	endnetent();
    131   1.1     jtc 	p = (struct netinfo *)netcb.ni_forw;
    132   1.1     jtc 	while (p != (struct netinfo *)&netcb) {
    133   1.1     jtc 		if (p->ni_line != -1)
    134   1.1     jtc 			lastrow--;
    135   1.1     jtc 		p->ni_line = -1;
    136   1.1     jtc 		p = p->ni_forw;
    137   1.1     jtc 	}
    138   1.5  scottr 	if (w != NULL) {
    139   1.1     jtc 		wclear(w);
    140   1.1     jtc 		wrefresh(w);
    141   1.1     jtc 		delwin(w);
    142   1.1     jtc 	}
    143   1.1     jtc }
    144   1.1     jtc 
    145   1.1     jtc static struct nlist namelist[] = {
    146   1.3     cgd #define	X_TCBTABLE	0
    147   1.3     cgd 	{ "_tcbtable" },
    148   1.3     cgd #define	X_UDBTABLE	1
    149   1.3     cgd 	{ "_udbtable" },
    150   1.1     jtc 	{ "" },
    151   1.1     jtc };
    152   1.1     jtc 
    153   1.1     jtc int
    154   1.1     jtc initnetstat()
    155   1.1     jtc {
    156   1.1     jtc 	if (kvm_nlist(kd, namelist)) {
    157   1.1     jtc 		nlisterr(namelist);
    158   1.1     jtc 		return(0);
    159   1.1     jtc 	}
    160   1.3     cgd 	if (namelist[X_TCBTABLE].n_value == 0) {
    161   1.1     jtc 		error("No symbols in namelist");
    162   1.1     jtc 		return(0);
    163   1.1     jtc 	}
    164   1.1     jtc 	netcb.ni_forw = netcb.ni_prev = (struct netinfo *)&netcb;
    165   1.1     jtc 	protos = TCP|UDP;
    166   1.1     jtc 	return(1);
    167   1.1     jtc }
    168   1.1     jtc 
    169   1.1     jtc void
    170   1.1     jtc fetchnetstat()
    171   1.1     jtc {
    172   1.3     cgd 	struct inpcbtable pcbtable;
    173   1.8   lukem 	struct inpcb *head, *prev, *next;
    174   1.8   lukem 	struct netinfo *p;
    175   1.1     jtc 	struct inpcb inpcb;
    176   1.1     jtc 	struct socket sockb;
    177   1.1     jtc 	struct tcpcb tcpcb;
    178   1.1     jtc 	void *off;
    179   1.1     jtc 	int istcp;
    180   1.1     jtc 
    181   1.3     cgd 	if (namelist[X_TCBTABLE].n_value == 0)
    182   1.1     jtc 		return;
    183   1.1     jtc 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw)
    184   1.1     jtc 		p->ni_seen = 0;
    185   1.1     jtc 	if (protos&TCP) {
    186   1.3     cgd 		off = NPTR(X_TCBTABLE);
    187   1.1     jtc 		istcp = 1;
    188   1.1     jtc 	}
    189   1.1     jtc 	else if (protos&UDP) {
    190   1.3     cgd 		off = NPTR(X_UDBTABLE);
    191   1.1     jtc 		istcp = 0;
    192   1.1     jtc 	}
    193   1.1     jtc 	else {
    194   1.1     jtc 		error("No protocols to display");
    195   1.1     jtc 		return;
    196   1.1     jtc 	}
    197   1.1     jtc again:
    198  1.11    ross 	KREAD(off, &pcbtable, sizeof pcbtable);
    199   1.3     cgd 	prev = head = (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue;
    200   1.3     cgd 	next = pcbtable.inpt_queue.cqh_first;
    201   1.3     cgd 	while (next != head) {
    202   1.1     jtc 		KREAD(next, &inpcb, sizeof (inpcb));
    203   1.3     cgd 		if (inpcb.inp_queue.cqe_prev != prev) {
    204   1.7     mrg printf("prev = %p, head = %p, next = %p, inpcb...prev = %p\n", prev, head, next, inpcb.inp_queue.cqe_prev);
    205   1.1     jtc 			p = netcb.ni_forw;
    206   1.1     jtc 			for (; p != (struct netinfo *)&netcb; p = p->ni_forw)
    207   1.1     jtc 				p->ni_seen = 1;
    208   1.1     jtc 			error("Kernel state in transition");
    209   1.1     jtc 			return;
    210   1.1     jtc 		}
    211   1.3     cgd 		prev = next;
    212   1.3     cgd 		next = inpcb.inp_queue.cqe_next;
    213   1.3     cgd 
    214   1.1     jtc 		if (!aflag && inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
    215   1.1     jtc 			continue;
    216   1.1     jtc 		if (nhosts && !checkhost(&inpcb))
    217   1.1     jtc 			continue;
    218   1.1     jtc 		if (nports && !checkport(&inpcb))
    219   1.1     jtc 			continue;
    220   1.1     jtc 		KREAD(inpcb.inp_socket, &sockb, sizeof (sockb));
    221   1.1     jtc 		if (istcp) {
    222   1.1     jtc 			KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
    223   1.1     jtc 			enter(&inpcb, &sockb, tcpcb.t_state, "tcp");
    224   1.1     jtc 		} else
    225   1.1     jtc 			enter(&inpcb, &sockb, 0, "udp");
    226   1.1     jtc 	}
    227   1.1     jtc 	if (istcp && (protos&UDP)) {
    228   1.1     jtc 		istcp = 0;
    229   1.3     cgd 		off = NPTR(X_UDBTABLE);
    230   1.1     jtc 		goto again;
    231   1.1     jtc 	}
    232   1.1     jtc }
    233   1.1     jtc 
    234   1.1     jtc static void
    235   1.1     jtc enter(inp, so, state, proto)
    236   1.7     mrg 	struct inpcb *inp;
    237   1.7     mrg 	struct socket *so;
    238   1.1     jtc 	int state;
    239   1.1     jtc 	char *proto;
    240   1.1     jtc {
    241   1.8   lukem 	struct netinfo *p;
    242   1.1     jtc 
    243   1.1     jtc 	/*
    244   1.1     jtc 	 * Only take exact matches, any sockets with
    245   1.1     jtc 	 * previously unbound addresses will be deleted
    246   1.1     jtc 	 * below in the display routine because they
    247   1.1     jtc 	 * will appear as ``not seen'' in the kernel
    248   1.1     jtc 	 * data structures.
    249   1.1     jtc 	 */
    250   1.1     jtc 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
    251   1.1     jtc 		if (!streq(proto, p->ni_proto))
    252   1.1     jtc 			continue;
    253   1.1     jtc 		if (p->ni_lport != inp->inp_lport ||
    254   1.1     jtc 		    p->ni_laddr.s_addr != inp->inp_laddr.s_addr)
    255   1.1     jtc 			continue;
    256   1.1     jtc 		if (p->ni_faddr.s_addr == inp->inp_faddr.s_addr &&
    257   1.1     jtc 		    p->ni_fport == inp->inp_fport)
    258   1.1     jtc 			break;
    259   1.1     jtc 	}
    260   1.1     jtc 	if (p == (struct netinfo *)&netcb) {
    261   1.1     jtc 		if ((p = malloc(sizeof(*p))) == NULL) {
    262   1.1     jtc 			error("Out of memory");
    263   1.1     jtc 			return;
    264   1.1     jtc 		}
    265   1.1     jtc 		p->ni_prev = (struct netinfo *)&netcb;
    266   1.1     jtc 		p->ni_forw = netcb.ni_forw;
    267   1.1     jtc 		netcb.ni_forw->ni_prev = p;
    268   1.1     jtc 		netcb.ni_forw = p;
    269   1.1     jtc 		p->ni_line = -1;
    270   1.1     jtc 		p->ni_laddr = inp->inp_laddr;
    271   1.1     jtc 		p->ni_lport = inp->inp_lport;
    272   1.1     jtc 		p->ni_faddr = inp->inp_faddr;
    273   1.1     jtc 		p->ni_fport = inp->inp_fport;
    274   1.1     jtc 		p->ni_proto = proto;
    275   1.1     jtc 		p->ni_flags = NIF_LACHG|NIF_FACHG;
    276   1.1     jtc 	}
    277   1.1     jtc 	p->ni_rcvcc = so->so_rcv.sb_cc;
    278   1.1     jtc 	p->ni_sndcc = so->so_snd.sb_cc;
    279   1.1     jtc 	p->ni_state = state;
    280   1.1     jtc 	p->ni_seen = 1;
    281   1.1     jtc }
    282   1.1     jtc 
    283   1.1     jtc /* column locations */
    284   1.1     jtc #define	LADDR	0
    285   1.1     jtc #define	FADDR	LADDR+23
    286   1.1     jtc #define	PROTO	FADDR+23
    287   1.1     jtc #define	RCVCC	PROTO+6
    288   1.1     jtc #define	SNDCC	RCVCC+7
    289   1.1     jtc #define	STATE	SNDCC+7
    290   1.1     jtc 
    291   1.1     jtc void
    292   1.1     jtc labelnetstat()
    293   1.1     jtc {
    294   1.9     mrg 
    295   1.3     cgd 	if (namelist[X_TCBTABLE].n_type == 0)
    296   1.1     jtc 		return;
    297   1.1     jtc 	wmove(wnd, 0, 0); wclrtobot(wnd);
    298   1.1     jtc 	mvwaddstr(wnd, 0, LADDR, "Local Address");
    299   1.1     jtc 	mvwaddstr(wnd, 0, FADDR, "Foreign Address");
    300   1.1     jtc 	mvwaddstr(wnd, 0, PROTO, "Proto");
    301   1.1     jtc 	mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
    302   1.1     jtc 	mvwaddstr(wnd, 0, SNDCC, "Send-Q");
    303   1.1     jtc 	mvwaddstr(wnd, 0, STATE, "(state)");
    304   1.1     jtc }
    305   1.1     jtc 
    306   1.1     jtc void
    307   1.1     jtc shownetstat()
    308   1.1     jtc {
    309   1.8   lukem 	struct netinfo *p, *q;
    310   1.1     jtc 
    311   1.1     jtc 	/*
    312   1.1     jtc 	 * First, delete any connections that have gone
    313   1.1     jtc 	 * away and adjust the position of connections
    314   1.1     jtc 	 * below to reflect the deleted line.
    315   1.1     jtc 	 */
    316   1.1     jtc 	p = netcb.ni_forw;
    317   1.1     jtc 	while (p != (struct netinfo *)&netcb) {
    318   1.1     jtc 		if (p->ni_line == -1 || p->ni_seen) {
    319   1.1     jtc 			p = p->ni_forw;
    320   1.1     jtc 			continue;
    321   1.1     jtc 		}
    322   1.1     jtc 		wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
    323   1.1     jtc 		q = netcb.ni_forw;
    324   1.1     jtc 		for (; q != (struct netinfo *)&netcb; q = q->ni_forw)
    325   1.1     jtc 			if (q != p && q->ni_line > p->ni_line) {
    326   1.1     jtc 				q->ni_line--;
    327   1.1     jtc 				/* this shouldn't be necessary */
    328   1.1     jtc 				q->ni_flags |= NIF_LACHG|NIF_FACHG;
    329   1.1     jtc 			}
    330   1.1     jtc 		lastrow--;
    331   1.1     jtc 		q = p->ni_forw;
    332   1.1     jtc 		p->ni_prev->ni_forw = p->ni_forw;
    333   1.1     jtc 		p->ni_forw->ni_prev = p->ni_prev;
    334   1.1     jtc 		free(p);
    335   1.1     jtc 		p = q;
    336   1.1     jtc 	}
    337   1.1     jtc 	/*
    338   1.1     jtc 	 * Update existing connections and add new ones.
    339   1.1     jtc 	 */
    340   1.1     jtc 	for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
    341   1.1     jtc 		if (p->ni_line == -1) {
    342   1.1     jtc 			/*
    343   1.1     jtc 			 * Add a new entry if possible.
    344   1.1     jtc 			 */
    345   1.6     jtc 			if (lastrow > getmaxy(wnd))
    346   1.1     jtc 				continue;
    347   1.1     jtc 			p->ni_line = lastrow++;
    348   1.1     jtc 			p->ni_flags |= NIF_LACHG|NIF_FACHG;
    349   1.1     jtc 		}
    350   1.1     jtc 		if (p->ni_flags & NIF_LACHG) {
    351   1.1     jtc 			wmove(wnd, p->ni_line, LADDR);
    352   1.1     jtc 			inetprint(&p->ni_laddr, p->ni_lport, p->ni_proto);
    353   1.1     jtc 			p->ni_flags &= ~NIF_LACHG;
    354   1.1     jtc 		}
    355   1.1     jtc 		if (p->ni_flags & NIF_FACHG) {
    356   1.1     jtc 			wmove(wnd, p->ni_line, FADDR);
    357   1.1     jtc 			inetprint(&p->ni_faddr, p->ni_fport, p->ni_proto);
    358   1.1     jtc 			p->ni_flags &= ~NIF_FACHG;
    359   1.1     jtc 		}
    360   1.1     jtc 		mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto);
    361   1.1     jtc 		mvwprintw(wnd, p->ni_line, RCVCC, "%6d", p->ni_rcvcc);
    362   1.1     jtc 		mvwprintw(wnd, p->ni_line, SNDCC, "%6d", p->ni_sndcc);
    363  1.10    ross 		if (streq(p->ni_proto, "tcp")) {
    364   1.1     jtc 			if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
    365   1.1     jtc 				mvwprintw(wnd, p->ni_line, STATE, "%d",
    366   1.1     jtc 				    p->ni_state);
    367   1.1     jtc 			else
    368   1.1     jtc 				mvwaddstr(wnd, p->ni_line, STATE,
    369   1.1     jtc 				    tcpstates[p->ni_state]);
    370  1.10    ross 		}
    371   1.1     jtc 		wclrtoeol(wnd);
    372   1.1     jtc 	}
    373   1.6     jtc 	if (lastrow < getmaxy(wnd)) {
    374   1.1     jtc 		wmove(wnd, lastrow, 0); wclrtobot(wnd);
    375   1.6     jtc 		wmove(wnd, getmaxy(wnd), 0); wdeleteln(wnd);	/* XXX */
    376   1.1     jtc 	}
    377   1.1     jtc }
    378   1.1     jtc 
    379   1.1     jtc /*
    380   1.1     jtc  * Pretty print an Internet address (net address + port).
    381   1.1     jtc  * If the nflag was specified, use numbers instead of names.
    382   1.1     jtc  */
    383   1.1     jtc static void
    384   1.1     jtc inetprint(in, port, proto)
    385   1.7     mrg 	struct in_addr *in;
    386   1.1     jtc 	int port;
    387   1.1     jtc 	char *proto;
    388   1.1     jtc {
    389   1.1     jtc 	struct servent *sp = 0;
    390   1.7     mrg 	char line[80], *cp;
    391   1.1     jtc 
    392   1.9     mrg 	(void)snprintf(line, sizeof line, "%.*s.", 16, inetname(*in));
    393   1.8   lukem 	cp = strchr(line, '\0');
    394   1.1     jtc 	if (!nflag && port)
    395   1.1     jtc 		sp = getservbyport(port, proto);
    396   1.1     jtc 	if (sp || port == 0)
    397   1.9     mrg 		(void)snprintf(cp, line + sizeof line - cp, "%.8s",
    398   1.9     mrg 		     sp ? sp->s_name : "*");
    399   1.1     jtc 	else
    400   1.9     mrg 		(void)snprintf(cp, line + sizeof line - cp, "%d",
    401   1.9     mrg 		     ntohs((u_short)port));
    402   1.1     jtc 	/* pad to full column to clear any garbage */
    403   1.8   lukem 	cp = strchr(line, '\0');
    404   1.1     jtc 	while (cp - line < 22)
    405   1.1     jtc 		*cp++ = ' ';
    406   1.1     jtc 	*cp = '\0';
    407   1.1     jtc 	waddstr(wnd, line);
    408   1.1     jtc }
    409   1.1     jtc 
    410   1.1     jtc /*
    411   1.1     jtc  * Construct an Internet address representation.
    412   1.1     jtc  * If the nflag has been supplied, give
    413   1.1     jtc  * numeric value, otherwise try for symbolic name.
    414   1.1     jtc  */
    415   1.1     jtc static char *
    416   1.1     jtc inetname(in)
    417   1.1     jtc 	struct in_addr in;
    418   1.1     jtc {
    419   1.1     jtc 	char *cp = 0;
    420   1.1     jtc 	static char line[50];
    421   1.1     jtc 	struct hostent *hp;
    422   1.1     jtc 	struct netent *np;
    423   1.1     jtc 
    424   1.1     jtc 	if (!nflag && in.s_addr != INADDR_ANY) {
    425   1.1     jtc 		int net = inet_netof(in);
    426   1.1     jtc 		int lna = inet_lnaof(in);
    427   1.1     jtc 
    428   1.1     jtc 		if (lna == INADDR_ANY) {
    429   1.1     jtc 			np = getnetbyaddr(net, AF_INET);
    430   1.1     jtc 			if (np)
    431   1.1     jtc 				cp = np->n_name;
    432   1.1     jtc 		}
    433   1.1     jtc 		if (cp == 0) {
    434   1.1     jtc 			hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
    435   1.1     jtc 			if (hp)
    436   1.1     jtc 				cp = hp->h_name;
    437   1.1     jtc 		}
    438   1.1     jtc 	}
    439   1.1     jtc 	if (in.s_addr == INADDR_ANY)
    440   1.9     mrg 		strncpy(line, "*", sizeof(line) - 1);
    441   1.1     jtc 	else if (cp)
    442   1.9     mrg 		strncpy(line, cp, sizeof(line) - 1);
    443   1.1     jtc 	else {
    444   1.1     jtc 		in.s_addr = ntohl(in.s_addr);
    445   1.1     jtc #define C(x)	((x) & 0xff)
    446   1.9     mrg 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u",
    447   1.9     mrg 		    C(in.s_addr >> 24), C(in.s_addr >> 16),
    448   1.9     mrg 		    C(in.s_addr >> 8), C(in.s_addr));
    449   1.9     mrg #undef C
    450   1.1     jtc 	}
    451   1.9     mrg 	line[sizeof(line) - 1] = '\0';
    452   1.1     jtc 	return (line);
    453   1.1     jtc }
    454   1.1     jtc 
    455  1.12   jwise /* please note: there are also some netstat commands in netcmds.c */
    456  1.12   jwise 
    457  1.12   jwise void
    458  1.12   jwise netstat_all (args)
    459  1.12   jwise 	char *args;
    460  1.12   jwise {
    461  1.12   jwise 	aflag = !aflag;
    462  1.12   jwise 	fetchnetstat();
    463  1.12   jwise 	shownetstat();
    464  1.12   jwise 	refresh();
    465  1.12   jwise }
    466  1.12   jwise 
    467  1.12   jwise void
    468  1.12   jwise netstat_names (args)
    469  1.12   jwise 	char *args;
    470  1.12   jwise {
    471  1.12   jwise 	struct netinfo *p;
    472  1.12   jwise 
    473  1.12   jwise 	if (nflag == 0)
    474  1.12   jwise 		return;
    475  1.12   jwise 
    476  1.12   jwise 	p = netcb.ni_forw;
    477  1.12   jwise 	for (; p != (struct netinfo *)&netcb; p = p->ni_forw) {
    478  1.12   jwise 		if (p->ni_line == -1)
    479  1.12   jwise 			continue;
    480  1.12   jwise 		p->ni_flags |= NIF_LACHG|NIF_FACHG;
    481  1.12   jwise 	}
    482  1.12   jwise 	nflag = 0;
    483  1.12   jwise 	wclear(wnd);
    484  1.12   jwise 	labelnetstat();
    485  1.12   jwise 	shownetstat();
    486  1.12   jwise 	refresh();
    487  1.12   jwise }
    488  1.12   jwise 
    489  1.12   jwise void
    490  1.12   jwise netstat_numbers (args)
    491  1.12   jwise 	char *args;
    492   1.1     jtc {
    493   1.8   lukem 	struct netinfo *p;
    494   1.1     jtc 
    495  1.13  itojun 	if (nflag != 0)
    496  1.12   jwise 		return;
    497  1.12   jwise 
    498  1.12   jwise 	p = netcb.ni_forw;
    499  1.12   jwise 	for (; p != (struct netinfo *)&netcb; p = p->ni_forw) {
    500  1.12   jwise 		if (p->ni_line == -1)
    501  1.12   jwise 			continue;
    502  1.12   jwise 		p->ni_flags |= NIF_LACHG|NIF_FACHG;
    503  1.12   jwise 	}
    504  1.13  itojun 	nflag = 1;
    505  1.12   jwise 	wclear(wnd);
    506  1.12   jwise 	labelnetstat();
    507   1.1     jtc 	shownetstat();
    508   1.1     jtc 	refresh();
    509   1.1     jtc }
    510