Home | History | Annotate | Line # | Download | only in systat
netcmds.c revision 1.9
      1  1.9    jwise /*	$NetBSD: netcmds.c,v 1.9 1999/12/20 03:45:02 jwise 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.5      mrg #include <sys/cdefs.h>
     37  1.1      jtc #ifndef lint
     38  1.2      jtc #if 0
     39  1.1      jtc static char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 6/6/93";
     40  1.2      jtc #endif
     41  1.9    jwise __RCSID("$NetBSD: netcmds.c,v 1.9 1999/12/20 03:45:02 jwise Exp $");
     42  1.1      jtc #endif /* not lint */
     43  1.1      jtc 
     44  1.1      jtc /*
     45  1.1      jtc  * Common network command support routines.
     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/mbuf.h>
     50  1.1      jtc #include <sys/protosw.h>
     51  1.1      jtc 
     52  1.1      jtc #include <net/route.h>
     53  1.1      jtc #include <netinet/in.h>
     54  1.1      jtc #include <netinet/in_systm.h>
     55  1.1      jtc #include <netinet/ip.h>
     56  1.1      jtc #include <netinet/in_pcb.h>
     57  1.1      jtc 
     58  1.3      cgd #include <arpa/inet.h>
     59  1.3      cgd 
     60  1.1      jtc #include <netdb.h>
     61  1.1      jtc #include <stdlib.h>
     62  1.1      jtc #include <string.h>
     63  1.1      jtc #include <ctype.h>
     64  1.1      jtc #include "systat.h"
     65  1.1      jtc #include "extern.h"
     66  1.1      jtc 
     67  1.1      jtc #define	streq(a,b)	(strcmp(a,b)==0)
     68  1.1      jtc 
     69  1.1      jtc static	struct hitem {
     70  1.1      jtc 	struct	in_addr addr;
     71  1.1      jtc 	int	onoff;
     72  1.1      jtc } *hosts;
     73  1.1      jtc 
     74  1.1      jtc int nports, nhosts, protos;
     75  1.1      jtc 
     76  1.1      jtc static void changeitems __P((char *, int));
     77  1.5      mrg static void selectproto __P((char *));
     78  1.1      jtc static void showprotos __P((void));
     79  1.1      jtc static int selectport __P((long, int));
     80  1.1      jtc static void showports __P((void));
     81  1.1      jtc static int selecthost __P((struct in_addr *, int));
     82  1.1      jtc static void showhosts __P((void));
     83  1.1      jtc 
     84  1.9    jwise /* please note: there are also some netstat commands in netstat.c */
     85  1.9    jwise 
     86  1.9    jwise void
     87  1.9    jwise netstat_display (args)
     88  1.9    jwise 	char *args;
     89  1.1      jtc {
     90  1.9    jwise 	changeitems(args, 1);
     91  1.9    jwise }
     92  1.9    jwise 
     93  1.9    jwise void
     94  1.9    jwise netstat_ignore (args)
     95  1.9    jwise 	char *args;
     96  1.9    jwise {
     97  1.9    jwise 	changeitems(args, 0);
     98  1.9    jwise }
     99  1.1      jtc 
    100  1.9    jwise void
    101  1.9    jwise netstat_reset (args)
    102  1.9    jwise 	char *args;
    103  1.9    jwise {
    104  1.9    jwise 	selectproto(0);
    105  1.9    jwise 	selecthost(0, 0);
    106  1.9    jwise 	selectport(-1, 0);
    107  1.9    jwise }
    108  1.9    jwise 
    109  1.9    jwise void
    110  1.9    jwise netstat_show (args)
    111  1.9    jwise 	char *args;
    112  1.9    jwise {
    113  1.9    jwise 	move(CMDLINE, 0); clrtoeol();
    114  1.9    jwise 	if (*args == '\0') {
    115  1.9    jwise 		showprotos();
    116  1.9    jwise 		showhosts();
    117  1.9    jwise 		showports();
    118  1.9    jwise 		return;
    119  1.1      jtc 	}
    120  1.9    jwise 	if (prefix(args, "protos"))
    121  1.9    jwise 		showprotos();
    122  1.9    jwise 	else if (prefix(args, "hosts"))
    123  1.9    jwise 		showhosts();
    124  1.9    jwise 	else if (prefix(args, "ports"))
    125  1.9    jwise 		showports();
    126  1.9    jwise 	else
    127  1.9    jwise 		addstr("show what?");
    128  1.9    jwise }
    129  1.9    jwise 
    130  1.9    jwise void
    131  1.9    jwise netstat_tcp (args)
    132  1.9    jwise 	char *args;
    133  1.9    jwise {
    134  1.9    jwise 	selectproto("tcp");
    135  1.1      jtc }
    136  1.1      jtc 
    137  1.9    jwise void
    138  1.9    jwise netstat_udp (args)
    139  1.9    jwise 	char *args;
    140  1.9    jwise {
    141  1.9    jwise 	selectproto("udp");
    142  1.9    jwise }
    143  1.1      jtc 
    144  1.1      jtc static void
    145  1.1      jtc changeitems(args, onoff)
    146  1.1      jtc 	char *args;
    147  1.1      jtc 	int onoff;
    148  1.1      jtc {
    149  1.6    lukem 	char *cp;
    150  1.1      jtc 	struct servent *sp;
    151  1.1      jtc 	struct hostent *hp;
    152  1.1      jtc 	struct in_addr in;
    153  1.1      jtc 
    154  1.6    lukem 	cp = strchr(args, '\n');
    155  1.1      jtc 	if (cp)
    156  1.1      jtc 		*cp = '\0';
    157  1.1      jtc 	for (;;args = cp) {
    158  1.1      jtc 		for (cp = args; *cp && isspace(*cp); cp++)
    159  1.1      jtc 			;
    160  1.1      jtc 		args = cp;
    161  1.1      jtc 		for (; *cp && !isspace(*cp); cp++)
    162  1.1      jtc 			;
    163  1.1      jtc 		if (*cp)
    164  1.1      jtc 			*cp++ = '\0';
    165  1.1      jtc 		if (cp - args == 0)
    166  1.1      jtc 			break;
    167  1.1      jtc 		sp = getservbyname(args,
    168  1.1      jtc 		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
    169  1.1      jtc 		if (sp) {
    170  1.1      jtc 			selectport(sp->s_port, onoff);
    171  1.1      jtc 			continue;
    172  1.1      jtc 		}
    173  1.4  mycroft 		if (inet_aton(args, &in) == 0) {
    174  1.4  mycroft 			hp = gethostbyname(args);
    175  1.4  mycroft 			if (hp == 0) {
    176  1.1      jtc 				error("%s: unknown host or port", args);
    177  1.1      jtc 				continue;
    178  1.1      jtc 			}
    179  1.4  mycroft 			memcpy(&in, hp->h_addr, hp->h_length);
    180  1.4  mycroft 		}
    181  1.1      jtc 		selecthost(&in, onoff);
    182  1.1      jtc 	}
    183  1.1      jtc }
    184  1.1      jtc 
    185  1.5      mrg static void
    186  1.1      jtc selectproto(proto)
    187  1.1      jtc 	char *proto;
    188  1.1      jtc {
    189  1.1      jtc 
    190  1.1      jtc 	if (proto == 0 || streq(proto, "all"))
    191  1.5      mrg 		protos = TCP|UDP;
    192  1.1      jtc 	else if (streq(proto, "tcp"))
    193  1.5      mrg 		protos = TCP;
    194  1.1      jtc 	else if (streq(proto, "udp"))
    195  1.5      mrg 		protos = UDP;
    196  1.1      jtc }
    197  1.1      jtc 
    198  1.1      jtc static void
    199  1.1      jtc showprotos()
    200  1.1      jtc {
    201  1.1      jtc 
    202  1.8      mrg 	if ((protos & TCP) == 0)
    203  1.1      jtc 		addch('!');
    204  1.1      jtc 	addstr("tcp ");
    205  1.8      mrg 	if ((protos & UDP) == 0)
    206  1.1      jtc 		addch('!');
    207  1.1      jtc 	addstr("udp ");
    208  1.1      jtc }
    209  1.1      jtc 
    210  1.1      jtc static	struct pitem {
    211  1.1      jtc 	long	port;
    212  1.1      jtc 	int	onoff;
    213  1.1      jtc } *ports;
    214  1.1      jtc 
    215  1.1      jtc static int
    216  1.1      jtc selectport(port, onoff)
    217  1.1      jtc 	long port;
    218  1.1      jtc 	int onoff;
    219  1.1      jtc {
    220  1.6    lukem 	struct pitem *p;
    221  1.1      jtc 
    222  1.1      jtc 	if (port == -1) {
    223  1.1      jtc 		if (ports == 0)
    224  1.1      jtc 			return (0);
    225  1.1      jtc 		free((char *)ports), ports = 0;
    226  1.1      jtc 		nports = 0;
    227  1.1      jtc 		return (1);
    228  1.1      jtc 	}
    229  1.1      jtc 	for (p = ports; p < ports+nports; p++)
    230  1.1      jtc 		if (p->port == port) {
    231  1.1      jtc 			p->onoff = onoff;
    232  1.1      jtc 			return (0);
    233  1.1      jtc 		}
    234  1.1      jtc 	if (nports == 0)
    235  1.1      jtc 		ports = (struct pitem *)malloc(sizeof (*p));
    236  1.1      jtc 	else
    237  1.1      jtc 		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
    238  1.1      jtc 	p = &ports[nports++];
    239  1.1      jtc 	p->port = port;
    240  1.1      jtc 	p->onoff = onoff;
    241  1.1      jtc 	return (1);
    242  1.1      jtc }
    243  1.1      jtc 
    244  1.1      jtc int
    245  1.1      jtc checkport(inp)
    246  1.6    lukem 	struct inpcb *inp;
    247  1.1      jtc {
    248  1.6    lukem 	struct pitem *p;
    249  1.1      jtc 
    250  1.1      jtc 	if (ports)
    251  1.1      jtc 	for (p = ports; p < ports+nports; p++)
    252  1.1      jtc 		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
    253  1.1      jtc 			return (p->onoff);
    254  1.1      jtc 	return (1);
    255  1.1      jtc }
    256  1.1      jtc 
    257  1.1      jtc static void
    258  1.1      jtc showports()
    259  1.1      jtc {
    260  1.6    lukem 	struct pitem *p;
    261  1.1      jtc 	struct servent *sp;
    262  1.1      jtc 
    263  1.1      jtc 	for (p = ports; p < ports+nports; p++) {
    264  1.1      jtc 		sp = getservbyport(p->port,
    265  1.5      mrg 		    protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
    266  1.1      jtc 		if (!p->onoff)
    267  1.1      jtc 			addch('!');
    268  1.1      jtc 		if (sp)
    269  1.1      jtc 			printw("%s ", sp->s_name);
    270  1.1      jtc 		else
    271  1.1      jtc 			printw("%d ", p->port);
    272  1.1      jtc 	}
    273  1.1      jtc }
    274  1.1      jtc 
    275  1.1      jtc static int
    276  1.1      jtc selecthost(in, onoff)
    277  1.1      jtc 	struct in_addr *in;
    278  1.1      jtc 	int onoff;
    279  1.1      jtc {
    280  1.6    lukem 	struct hitem *p;
    281  1.1      jtc 
    282  1.1      jtc 	if (in == 0) {
    283  1.1      jtc 		if (hosts == 0)
    284  1.1      jtc 			return (0);
    285  1.1      jtc 		free((char *)hosts), hosts = 0;
    286  1.1      jtc 		nhosts = 0;
    287  1.1      jtc 		return (1);
    288  1.1      jtc 	}
    289  1.1      jtc 	for (p = hosts; p < hosts+nhosts; p++)
    290  1.1      jtc 		if (p->addr.s_addr == in->s_addr) {
    291  1.1      jtc 			p->onoff = onoff;
    292  1.1      jtc 			return (0);
    293  1.1      jtc 		}
    294  1.1      jtc 	if (nhosts == 0)
    295  1.1      jtc 		hosts = (struct hitem *)malloc(sizeof (*p));
    296  1.1      jtc 	else
    297  1.1      jtc 		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
    298  1.1      jtc 	p = &hosts[nhosts++];
    299  1.1      jtc 	p->addr = *in;
    300  1.1      jtc 	p->onoff = onoff;
    301  1.1      jtc 	return (1);
    302  1.1      jtc }
    303  1.1      jtc 
    304  1.1      jtc int
    305  1.1      jtc checkhost(inp)
    306  1.6    lukem 	struct inpcb *inp;
    307  1.1      jtc {
    308  1.6    lukem 	struct hitem *p;
    309  1.1      jtc 
    310  1.1      jtc 	if (hosts)
    311  1.8      mrg 		for (p = hosts; p < hosts+nhosts; p++)
    312  1.8      mrg 			if (p->addr.s_addr == inp->inp_laddr.s_addr ||
    313  1.8      mrg 			    p->addr.s_addr == inp->inp_faddr.s_addr)
    314  1.8      mrg 				return (p->onoff);
    315  1.1      jtc 	return (1);
    316  1.1      jtc }
    317  1.1      jtc 
    318  1.1      jtc static void
    319  1.1      jtc showhosts()
    320  1.1      jtc {
    321  1.6    lukem 	struct hitem *p;
    322  1.1      jtc 	struct hostent *hp;
    323  1.1      jtc 
    324  1.1      jtc 	for (p = hosts; p < hosts+nhosts; p++) {
    325  1.1      jtc 		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
    326  1.1      jtc 		if (!p->onoff)
    327  1.1      jtc 			addch('!');
    328  1.3      cgd 		printw("%s ", hp ? hp->h_name : inet_ntoa(p->addr));
    329  1.1      jtc 	}
    330  1.1      jtc }
    331