xref: /freebsd/usr.bin/systat/netcmds.c (revision b3608ae18f1e5598bed81d0a10dd585a5080c40d)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1992, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309ff712b0SMark Murray #include <sys/cdefs.h>
319ff712b0SMark Murray 
329ff712b0SMark Murray __FBSDID("$FreeBSD$");
339ff712b0SMark Murray 
349ff712b0SMark Murray #ifdef lint
359ff712b0SMark Murray static const char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 6/6/93";
369ff712b0SMark Murray #endif
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes /*
399b50d902SRodney W. Grimes  * Common network command support routines.
409b50d902SRodney W. Grimes  */
419b50d902SRodney W. Grimes #include <sys/param.h>
42d8d89152SDavid Greenman #include <sys/queue.h>
439b50d902SRodney W. Grimes #include <sys/socket.h>
449b50d902SRodney W. Grimes #include <sys/socketvar.h>
459b50d902SRodney W. Grimes #include <sys/protosw.h>
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <net/route.h>
489b50d902SRodney W. Grimes #include <netinet/in.h>
499b50d902SRodney W. Grimes #include <netinet/in_systm.h>
509b50d902SRodney W. Grimes #include <netinet/ip.h>
519b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
529ec49abdSPeter Wemm #include <arpa/inet.h>
539b50d902SRodney W. Grimes 
549ff712b0SMark Murray #include <ctype.h>
559b50d902SRodney W. Grimes #include <netdb.h>
569b50d902SRodney W. Grimes #include <stdlib.h>
579b50d902SRodney W. Grimes #include <string.h>
589ff712b0SMark Murray 
599b50d902SRodney W. Grimes #include "systat.h"
609b50d902SRodney W. Grimes #include "extern.h"
619b50d902SRodney W. Grimes 
629b50d902SRodney W. Grimes #define	streq(a,b)	(strcmp(a,b)==0)
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes static	struct hitem {
659b50d902SRodney W. Grimes 	struct	in_addr addr;
669b50d902SRodney W. Grimes 	int	onoff;
679b50d902SRodney W. Grimes } *hosts;
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes int nports, nhosts, protos;
709b50d902SRodney W. Grimes 
713f330d7dSWarner Losh static void changeitems(const char *, int);
723f330d7dSWarner Losh static int selectproto(const char *);
733f330d7dSWarner Losh static void showprotos(void);
743f330d7dSWarner Losh static int selectport(long, int);
753f330d7dSWarner Losh static void showports(void);
763f330d7dSWarner Losh static int selecthost(struct in_addr *, int);
773f330d7dSWarner Losh static void showhosts(void);
789b50d902SRodney W. Grimes 
799b50d902SRodney W. Grimes int
8093b9f504SXin LI netcmd(const char *cmd, const char *args)
819b50d902SRodney W. Grimes {
829b50d902SRodney W. Grimes 
8379431394SSteve Price 	if (prefix(cmd, "proto")) {
8479431394SSteve Price 		if (*args == '\0') {
8579431394SSteve Price 			move(CMDLINE, 0);
8679431394SSteve Price 			clrtoeol();
8779431394SSteve Price 			addstr("which proto?");
8879431394SSteve Price 		} else if (!selectproto(args)) {
8979431394SSteve Price 			error("%s: Unknown protocol.", args);
9079431394SSteve Price 		}
919b50d902SRodney W. Grimes 		return (1);
929b50d902SRodney W. Grimes 	}
939b50d902SRodney W. Grimes 	if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
949b50d902SRodney W. Grimes 		changeitems(args, prefix(cmd, "display"));
959b50d902SRodney W. Grimes 		return (1);
969b50d902SRodney W. Grimes 	}
979b50d902SRodney W. Grimes 	if (prefix(cmd, "reset")) {
989b50d902SRodney W. Grimes 		selectproto(0);
999b50d902SRodney W. Grimes 		selecthost(0, 0);
1009b50d902SRodney W. Grimes 		selectport(-1, 0);
1019b50d902SRodney W. Grimes 		return (1);
1029b50d902SRodney W. Grimes 	}
1039b50d902SRodney W. Grimes 	if (prefix(cmd, "show")) {
1049b50d902SRodney W. Grimes 		move(CMDLINE, 0); clrtoeol();
1059b50d902SRodney W. Grimes 		if (*args == '\0') {
1069b50d902SRodney W. Grimes 			showprotos();
1079b50d902SRodney W. Grimes 			showhosts();
1089b50d902SRodney W. Grimes 			showports();
1099b50d902SRodney W. Grimes 			return (1);
1109b50d902SRodney W. Grimes 		}
1119b50d902SRodney W. Grimes 		if (prefix(args, "protos"))
1129b50d902SRodney W. Grimes 			showprotos();
1139b50d902SRodney W. Grimes 		else if (prefix(args, "hosts"))
1149b50d902SRodney W. Grimes 			showhosts();
1159b50d902SRodney W. Grimes 		else if (prefix(args, "ports"))
1169b50d902SRodney W. Grimes 			showports();
1179b50d902SRodney W. Grimes 		else
1189b50d902SRodney W. Grimes 			addstr("show what?");
1199b50d902SRodney W. Grimes 		return (1);
1209b50d902SRodney W. Grimes 	}
1219b50d902SRodney W. Grimes 	return (0);
1229b50d902SRodney W. Grimes }
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes 
1259b50d902SRodney W. Grimes static void
12693b9f504SXin LI changeitems(const char *args, int onoff)
1279b50d902SRodney W. Grimes {
1289ff712b0SMark Murray 	char *cp, *tmpstr, *tmpstr1;
1299b50d902SRodney W. Grimes 	struct servent *sp;
1309b50d902SRodney W. Grimes 	struct hostent *hp;
1319b50d902SRodney W. Grimes 	struct in_addr in;
1329b50d902SRodney W. Grimes 
1339ff712b0SMark Murray 	tmpstr = tmpstr1 = strdup(args);
134*b3608ae1SEd Schouten 	cp = strchr(tmpstr1, '\n');
1359b50d902SRodney W. Grimes 	if (cp)
1369b50d902SRodney W. Grimes 		*cp = '\0';
1379ff712b0SMark Murray 	for (;;tmpstr1 = cp) {
1389ff712b0SMark Murray 		for (cp = tmpstr1; *cp && isspace(*cp); cp++)
1399b50d902SRodney W. Grimes 			;
1409ff712b0SMark Murray 		tmpstr1 = cp;
1419b50d902SRodney W. Grimes 		for (; *cp && !isspace(*cp); cp++)
1429b50d902SRodney W. Grimes 			;
1439b50d902SRodney W. Grimes 		if (*cp)
1449b50d902SRodney W. Grimes 			*cp++ = '\0';
1459ff712b0SMark Murray 		if (cp - tmpstr1 == 0)
1469b50d902SRodney W. Grimes 			break;
1479ff712b0SMark Murray 		sp = getservbyname(tmpstr1,
1489b50d902SRodney W. Grimes 		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
1499b50d902SRodney W. Grimes 		if (sp) {
1509b50d902SRodney W. Grimes 			selectport(sp->s_port, onoff);
1519b50d902SRodney W. Grimes 			continue;
1529b50d902SRodney W. Grimes 		}
1539ff712b0SMark Murray 		hp = gethostbyname(tmpstr1);
1549b50d902SRodney W. Grimes 		if (hp == 0) {
1559ff712b0SMark Murray 			in.s_addr = inet_addr(tmpstr1);
1569ff712b0SMark Murray 			if ((int)in.s_addr == -1) {
1579ff712b0SMark Murray 				error("%s: unknown host or port", tmpstr1);
1589b50d902SRodney W. Grimes 				continue;
1599b50d902SRodney W. Grimes 			}
1609b50d902SRodney W. Grimes 		} else
1619b50d902SRodney W. Grimes 			in = *(struct in_addr *)hp->h_addr;
1629b50d902SRodney W. Grimes 		selecthost(&in, onoff);
1639b50d902SRodney W. Grimes 	}
1649ff712b0SMark Murray 	free(tmpstr);
1659b50d902SRodney W. Grimes }
1669b50d902SRodney W. Grimes 
1679b50d902SRodney W. Grimes static int
16893b9f504SXin LI selectproto(const char *proto)
1699b50d902SRodney W. Grimes {
1709b50d902SRodney W. Grimes 
1719b50d902SRodney W. Grimes 	if (proto == 0 || streq(proto, "all"))
17279431394SSteve Price 		protos = TCP | UDP;
1739b50d902SRodney W. Grimes 	else if (streq(proto, "tcp"))
17479431394SSteve Price 		protos = TCP;
1759b50d902SRodney W. Grimes 	else if (streq(proto, "udp"))
17679431394SSteve Price 		protos = UDP;
17779431394SSteve Price 	else
17879431394SSteve Price 		return (0);
17979431394SSteve Price 
18079431394SSteve Price 	return (protos);
1819b50d902SRodney W. Grimes }
1829b50d902SRodney W. Grimes 
1839b50d902SRodney W. Grimes static void
18493b9f504SXin LI showprotos(void)
1859b50d902SRodney W. Grimes {
1869b50d902SRodney W. Grimes 
1879b50d902SRodney W. Grimes 	if ((protos&TCP) == 0)
1889b50d902SRodney W. Grimes 		addch('!');
1899b50d902SRodney W. Grimes 	addstr("tcp ");
1909b50d902SRodney W. Grimes 	if ((protos&UDP) == 0)
1919b50d902SRodney W. Grimes 		addch('!');
1929b50d902SRodney W. Grimes 	addstr("udp ");
1939b50d902SRodney W. Grimes }
1949b50d902SRodney W. Grimes 
1959b50d902SRodney W. Grimes static	struct pitem {
1969b50d902SRodney W. Grimes 	long	port;
1979b50d902SRodney W. Grimes 	int	onoff;
1989b50d902SRodney W. Grimes } *ports;
1999b50d902SRodney W. Grimes 
2009b50d902SRodney W. Grimes static int
20193b9f504SXin LI selectport(long port, int onoff)
2029b50d902SRodney W. Grimes {
20393b9f504SXin LI 	struct pitem *p;
2049b50d902SRodney W. Grimes 
2059b50d902SRodney W. Grimes 	if (port == -1) {
2069b50d902SRodney W. Grimes 		if (ports == 0)
2079b50d902SRodney W. Grimes 			return (0);
2089b50d902SRodney W. Grimes 		free((char *)ports), ports = 0;
2099b50d902SRodney W. Grimes 		nports = 0;
2109b50d902SRodney W. Grimes 		return (1);
2119b50d902SRodney W. Grimes 	}
2129b50d902SRodney W. Grimes 	for (p = ports; p < ports+nports; p++)
2139b50d902SRodney W. Grimes 		if (p->port == port) {
2149b50d902SRodney W. Grimes 			p->onoff = onoff;
2159b50d902SRodney W. Grimes 			return (0);
2169b50d902SRodney W. Grimes 		}
2179b50d902SRodney W. Grimes 	if (nports == 0)
2189b50d902SRodney W. Grimes 		ports = (struct pitem *)malloc(sizeof (*p));
2199b50d902SRodney W. Grimes 	else
2209b50d902SRodney W. Grimes 		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
2219b50d902SRodney W. Grimes 	p = &ports[nports++];
2229b50d902SRodney W. Grimes 	p->port = port;
2239b50d902SRodney W. Grimes 	p->onoff = onoff;
2249b50d902SRodney W. Grimes 	return (1);
2259b50d902SRodney W. Grimes }
2269b50d902SRodney W. Grimes 
2279b50d902SRodney W. Grimes int
22893b9f504SXin LI checkport(struct inpcb *inp)
2299b50d902SRodney W. Grimes {
23093b9f504SXin LI 	struct pitem *p;
2319b50d902SRodney W. Grimes 
2329b50d902SRodney W. Grimes 	if (ports)
2339b50d902SRodney W. Grimes 	for (p = ports; p < ports+nports; p++)
2349b50d902SRodney W. Grimes 		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
2359b50d902SRodney W. Grimes 			return (p->onoff);
2369b50d902SRodney W. Grimes 	return (1);
2379b50d902SRodney W. Grimes }
2389b50d902SRodney W. Grimes 
2399b50d902SRodney W. Grimes static void
24093b9f504SXin LI showports(void)
2419b50d902SRodney W. Grimes {
24293b9f504SXin LI 	struct pitem *p;
2439b50d902SRodney W. Grimes 	struct servent *sp;
2449b50d902SRodney W. Grimes 
2459b50d902SRodney W. Grimes 	for (p = ports; p < ports+nports; p++) {
2469b50d902SRodney W. Grimes 		sp = getservbyport(p->port,
247342e2faaSThomas Moestl 		    protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
2489b50d902SRodney W. Grimes 		if (!p->onoff)
2499b50d902SRodney W. Grimes 			addch('!');
2509b50d902SRodney W. Grimes 		if (sp)
2519b50d902SRodney W. Grimes 			printw("%s ", sp->s_name);
2529b50d902SRodney W. Grimes 		else
2539b50d902SRodney W. Grimes 			printw("%d ", p->port);
2549b50d902SRodney W. Grimes 	}
2559b50d902SRodney W. Grimes }
2569b50d902SRodney W. Grimes 
2579b50d902SRodney W. Grimes static int
25893b9f504SXin LI selecthost(struct in_addr *in, int onoff)
2599b50d902SRodney W. Grimes {
26093b9f504SXin LI 	struct hitem *p;
2619b50d902SRodney W. Grimes 
2629b50d902SRodney W. Grimes 	if (in == 0) {
2639b50d902SRodney W. Grimes 		if (hosts == 0)
2649b50d902SRodney W. Grimes 			return (0);
2659b50d902SRodney W. Grimes 		free((char *)hosts), hosts = 0;
2669b50d902SRodney W. Grimes 		nhosts = 0;
2679b50d902SRodney W. Grimes 		return (1);
2689b50d902SRodney W. Grimes 	}
2699b50d902SRodney W. Grimes 	for (p = hosts; p < hosts+nhosts; p++)
2709b50d902SRodney W. Grimes 		if (p->addr.s_addr == in->s_addr) {
2719b50d902SRodney W. Grimes 			p->onoff = onoff;
2729b50d902SRodney W. Grimes 			return (0);
2739b50d902SRodney W. Grimes 		}
2749b50d902SRodney W. Grimes 	if (nhosts == 0)
2759b50d902SRodney W. Grimes 		hosts = (struct hitem *)malloc(sizeof (*p));
2769b50d902SRodney W. Grimes 	else
2779b50d902SRodney W. Grimes 		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
2789b50d902SRodney W. Grimes 	p = &hosts[nhosts++];
2799b50d902SRodney W. Grimes 	p->addr = *in;
2809b50d902SRodney W. Grimes 	p->onoff = onoff;
2819b50d902SRodney W. Grimes 	return (1);
2829b50d902SRodney W. Grimes }
2839b50d902SRodney W. Grimes 
2849b50d902SRodney W. Grimes int
28593b9f504SXin LI checkhost(struct inpcb *inp)
2869b50d902SRodney W. Grimes {
28793b9f504SXin LI 	struct hitem *p;
2889b50d902SRodney W. Grimes 
2899b50d902SRodney W. Grimes 	if (hosts)
2909b50d902SRodney W. Grimes 	for (p = hosts; p < hosts+nhosts; p++)
2919b50d902SRodney W. Grimes 		if (p->addr.s_addr == inp->inp_laddr.s_addr ||
2929b50d902SRodney W. Grimes 		    p->addr.s_addr == inp->inp_faddr.s_addr)
2939b50d902SRodney W. Grimes 			return (p->onoff);
2949b50d902SRodney W. Grimes 	return (1);
2959b50d902SRodney W. Grimes }
2969b50d902SRodney W. Grimes 
2979b50d902SRodney W. Grimes static void
29893b9f504SXin LI showhosts(void)
2999b50d902SRodney W. Grimes {
30093b9f504SXin LI 	struct hitem *p;
3019b50d902SRodney W. Grimes 	struct hostent *hp;
3029b50d902SRodney W. Grimes 
3039b50d902SRodney W. Grimes 	for (p = hosts; p < hosts+nhosts; p++) {
3049b50d902SRodney W. Grimes 		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
3059b50d902SRodney W. Grimes 		if (!p->onoff)
3069b50d902SRodney W. Grimes 			addch('!');
3079b50d902SRodney W. Grimes 		printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
3089b50d902SRodney W. Grimes 	}
3099b50d902SRodney W. Grimes }
310