xref: /csrg-svn/usr.bin/systat/netcmds.c (revision 62283)
155624Sbostic /*-
2*62283Sbostic  * Copyright (c) 1980, 1992, 1993
3*62283Sbostic  *	The Regents of the University of California.  All rights reserved.
455624Sbostic  *
560197Smckusick  * %sccs.include.redist.c%
621460Smckusick  */
721460Smckusick 
821459Smckusick #ifndef lint
9*62283Sbostic static char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 06/06/93";
1055624Sbostic #endif /* not lint */
1121459Smckusick 
1221459Smckusick /*
1321459Smckusick  * Common network command support routines.
1421459Smckusick  */
1555624Sbostic #include <sys/param.h>
1621459Smckusick #include <sys/socket.h>
1721459Smckusick #include <sys/socketvar.h>
1821459Smckusick #include <sys/mbuf.h>
1921459Smckusick #include <sys/protosw.h>
2021459Smckusick 
2121459Smckusick #include <net/route.h>
2255624Sbostic #include <netinet/in.h>
2321459Smckusick #include <netinet/in_systm.h>
2440891Ssklower #include <netinet/ip.h>
2521459Smckusick #include <netinet/in_pcb.h>
2621459Smckusick 
2755624Sbostic #include <netdb.h>
2855624Sbostic #include <stdlib.h>
2955624Sbostic #include <string.h>
3055624Sbostic #include <ctype.h>
3155624Sbostic #include "systat.h"
3255624Sbostic #include "extern.h"
3355624Sbostic 
3421459Smckusick #define	streq(a,b)	(strcmp(a,b)==0)
3521459Smckusick 
3655624Sbostic static	struct hitem {
3755624Sbostic 	struct	in_addr addr;
3855624Sbostic 	int	onoff;
3955624Sbostic } *hosts;
4046265Storek 
4155624Sbostic int nports, nhosts, protos;
4255624Sbostic 
4355624Sbostic static void changeitems __P((char *, int));
4455624Sbostic static int selectproto __P((char *));
4555624Sbostic static void showprotos __P((void));
4655624Sbostic static int selectport __P((long, int));
4755624Sbostic static void showports __P((void));
4855624Sbostic static int selecthost __P((struct in_addr *, int));
4955624Sbostic static void showhosts __P((void));
5055624Sbostic 
5155624Sbostic int
netcmd(cmd,args)5221459Smckusick netcmd(cmd, args)
5321459Smckusick 	char *cmd, *args;
5421459Smckusick {
5521459Smckusick 
5621459Smckusick 	if (prefix(cmd, "tcp") || prefix(cmd, "udp")) {
5721459Smckusick 		selectproto(cmd);
5821459Smckusick 		return (1);
5921459Smckusick 	}
6021459Smckusick 	if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
6121459Smckusick 		changeitems(args, prefix(cmd, "display"));
6221459Smckusick 		return (1);
6321459Smckusick 	}
6421459Smckusick 	if (prefix(cmd, "reset")) {
6521459Smckusick 		selectproto(0);
6655624Sbostic 		selecthost(0, 0);
6755624Sbostic 		selectport(-1, 0);
6821459Smckusick 		return (1);
6921459Smckusick 	}
7021459Smckusick 	if (prefix(cmd, "show")) {
7121459Smckusick 		move(CMDLINE, 0); clrtoeol();
7221459Smckusick 		if (*args == '\0') {
7321459Smckusick 			showprotos();
7421459Smckusick 			showhosts();
7521459Smckusick 			showports();
7621459Smckusick 			return (1);
7721459Smckusick 		}
7821459Smckusick 		if (prefix(args, "protos"))
7921459Smckusick 			showprotos();
8021459Smckusick 		else if (prefix(args, "hosts"))
8121459Smckusick 			showhosts();
8221459Smckusick 		else if (prefix(args, "ports"))
8321459Smckusick 			showports();
8421459Smckusick 		else
8521459Smckusick 			addstr("show what?");
8621459Smckusick 		return (1);
8721459Smckusick 	}
8821459Smckusick 	return (0);
8921459Smckusick }
9021459Smckusick 
9155624Sbostic 
9246265Storek static void
changeitems(args,onoff)9321459Smckusick changeitems(args, onoff)
9421459Smckusick 	char *args;
9521459Smckusick 	int onoff;
9621459Smckusick {
9721459Smckusick 	register char *cp;
9821459Smckusick 	struct servent *sp;
9921459Smckusick 	struct hostent *hp;
10021459Smckusick 	struct in_addr in;
10121459Smckusick 	char *index();
10221459Smckusick 
10321459Smckusick 	cp = index(args, '\n');
10421459Smckusick 	if (cp)
10521459Smckusick 		*cp = '\0';
10621459Smckusick 	for (;;args = cp) {
10721459Smckusick 		for (cp = args; *cp && isspace(*cp); cp++)
10821459Smckusick 			;
10921459Smckusick 		args = cp;
11021459Smckusick 		for (; *cp && !isspace(*cp); cp++)
11121459Smckusick 			;
11221459Smckusick 		if (*cp)
11321459Smckusick 			*cp++ = '\0';
11421459Smckusick 		if (cp - args == 0)
11521459Smckusick 			break;
11621459Smckusick 		sp = getservbyname(args,
11721459Smckusick 		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
11821459Smckusick 		if (sp) {
11932188Sbostic 			selectport(sp->s_port, onoff);
12021459Smckusick 			continue;
12121459Smckusick 		}
12221459Smckusick 		hp = gethostbyname(args);
12321459Smckusick 		if (hp == 0) {
12421459Smckusick 			in.s_addr = inet_addr(args);
12521459Smckusick 			if (in.s_addr == -1) {
12621459Smckusick 				error("%s: unknown host or port", args);
12721459Smckusick 				continue;
12821459Smckusick 			}
12921459Smckusick 		} else
13021459Smckusick 			in = *(struct in_addr *)hp->h_addr;
13121459Smckusick 		selecthost(&in, onoff);
13221459Smckusick 	}
13321459Smckusick }
13421459Smckusick 
13546265Storek static int
selectproto(proto)13621459Smckusick selectproto(proto)
13721459Smckusick 	char *proto;
13821459Smckusick {
13921459Smckusick 	int new = protos;
14021459Smckusick 
14121459Smckusick 	if (proto == 0 || streq(proto, "all"))
14221459Smckusick 		new = TCP|UDP;
14321459Smckusick 	else if (streq(proto, "tcp"))
14421459Smckusick 		new = TCP;
14521459Smckusick 	else if (streq(proto, "udp"))
14621459Smckusick 		new = UDP;
14721459Smckusick 	return (new != protos, protos = new);
14821459Smckusick }
14921459Smckusick 
15046265Storek static void
showprotos()15121459Smckusick showprotos()
15221459Smckusick {
15321459Smckusick 
15421459Smckusick 	if ((protos&TCP) == 0)
15521459Smckusick 		addch('!');
15621459Smckusick 	addstr("tcp ");
15721459Smckusick 	if ((protos&UDP) == 0)
15821459Smckusick 		addch('!');
15921459Smckusick 	addstr("udp ");
16021459Smckusick }
16121459Smckusick 
16221459Smckusick static	struct pitem {
16321459Smckusick 	long	port;
16421459Smckusick 	int	onoff;
16521459Smckusick } *ports;
16621459Smckusick 
16746265Storek static int
selectport(port,onoff)16821459Smckusick selectport(port, onoff)
16921459Smckusick 	long port;
17021459Smckusick 	int onoff;
17121459Smckusick {
17225587Sbloom 	register struct pitem *p;
17321459Smckusick 
17421459Smckusick 	if (port == -1) {
17521459Smckusick 		if (ports == 0)
17621459Smckusick 			return (0);
17721459Smckusick 		free((char *)ports), ports = 0;
17821459Smckusick 		nports = 0;
17921459Smckusick 		return (1);
18021459Smckusick 	}
18121459Smckusick 	for (p = ports; p < ports+nports; p++)
18221459Smckusick 		if (p->port == port) {
18321459Smckusick 			p->onoff = onoff;
18421459Smckusick 			return (0);
18521459Smckusick 		}
18621459Smckusick 	if (nports == 0)
18721459Smckusick 		ports = (struct pitem *)malloc(sizeof (*p));
18821459Smckusick 	else
18921459Smckusick 		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
19021459Smckusick 	p = &ports[nports++];
19121459Smckusick 	p->port = port;
19221459Smckusick 	p->onoff = onoff;
19321459Smckusick 	return (1);
19421459Smckusick }
19521459Smckusick 
19655624Sbostic int
checkport(inp)19721459Smckusick checkport(inp)
19821459Smckusick 	register struct inpcb *inp;
19921459Smckusick {
20021459Smckusick 	register struct pitem *p;
20121459Smckusick 
20221459Smckusick 	if (ports)
20321459Smckusick 	for (p = ports; p < ports+nports; p++)
20421459Smckusick 		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
20521459Smckusick 			return (p->onoff);
20621459Smckusick 	return (1);
20721459Smckusick }
20821459Smckusick 
20946265Storek static void
showports()21021459Smckusick showports()
21121459Smckusick {
21221459Smckusick 	register struct pitem *p;
21321459Smckusick 	struct servent *sp;
21421459Smckusick 
21521459Smckusick 	for (p = ports; p < ports+nports; p++) {
21621459Smckusick 		sp = getservbyport(p->port,
21721459Smckusick 		    protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
21821459Smckusick 		if (!p->onoff)
21921459Smckusick 			addch('!');
22021459Smckusick 		if (sp)
22121459Smckusick 			printw("%s ", sp->s_name);
22221459Smckusick 		else
22321459Smckusick 			printw("%d ", p->port);
22421459Smckusick 	}
22521459Smckusick }
22621459Smckusick 
22746265Storek static int
selecthost(in,onoff)22821459Smckusick selecthost(in, onoff)
22921459Smckusick 	struct in_addr *in;
23046265Storek 	int onoff;
23121459Smckusick {
23221459Smckusick 	register struct hitem *p;
23321459Smckusick 
23421459Smckusick 	if (in == 0) {
23521459Smckusick 		if (hosts == 0)
23621459Smckusick 			return (0);
23721459Smckusick 		free((char *)hosts), hosts = 0;
23821459Smckusick 		nhosts = 0;
23921459Smckusick 		return (1);
24021459Smckusick 	}
24121459Smckusick 	for (p = hosts; p < hosts+nhosts; p++)
24221459Smckusick 		if (p->addr.s_addr == in->s_addr) {
24321459Smckusick 			p->onoff = onoff;
24421459Smckusick 			return (0);
24521459Smckusick 		}
24621459Smckusick 	if (nhosts == 0)
24721459Smckusick 		hosts = (struct hitem *)malloc(sizeof (*p));
24821459Smckusick 	else
24921459Smckusick 		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
25021459Smckusick 	p = &hosts[nhosts++];
25121459Smckusick 	p->addr = *in;
25221459Smckusick 	p->onoff = onoff;
25321459Smckusick 	return (1);
25421459Smckusick }
25521459Smckusick 
25655624Sbostic int
checkhost(inp)25721459Smckusick checkhost(inp)
25821459Smckusick 	register struct inpcb *inp;
25921459Smckusick {
26021459Smckusick 	register struct hitem *p;
26121459Smckusick 
26221459Smckusick 	if (hosts)
26321459Smckusick 	for (p = hosts; p < hosts+nhosts; p++)
26421459Smckusick 		if (p->addr.s_addr == inp->inp_laddr.s_addr ||
26521459Smckusick 		    p->addr.s_addr == inp->inp_faddr.s_addr)
26621459Smckusick 			return (p->onoff);
26721459Smckusick 	return (1);
26821459Smckusick }
26921459Smckusick 
27046265Storek static void
showhosts()27121459Smckusick showhosts()
27221459Smckusick {
27321459Smckusick 	register struct hitem *p;
27421459Smckusick 	struct hostent *hp;
27521459Smckusick 
27621459Smckusick 	for (p = hosts; p < hosts+nhosts; p++) {
27755624Sbostic 		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
27821459Smckusick 		if (!p->onoff)
27921459Smckusick 			addch('!');
28055624Sbostic 		printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
28121459Smckusick 	}
28221459Smckusick }
283