netcmds.c revision 36916
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1980, 1992, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3519234Swollman/*
361590Srgrimesstatic char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 6/6/93";
3719234Swollman*/
3819234Swollmanstatic const char rcsid[] =
3936916Speter	"$Id: netcmds.c,v 1.7 1997/12/04 03:44:44 steve Exp $";
401590Srgrimes#endif /* not lint */
411590Srgrimes
421590Srgrimes/*
431590Srgrimes * Common network command support routines.
441590Srgrimes */
451590Srgrimes#include <sys/param.h>
4614543Sdg#include <sys/queue.h>
471590Srgrimes#include <sys/socket.h>
481590Srgrimes#include <sys/socketvar.h>
491590Srgrimes#include <sys/protosw.h>
501590Srgrimes
511590Srgrimes#include <net/route.h>
521590Srgrimes#include <netinet/in.h>
531590Srgrimes#include <netinet/in_systm.h>
541590Srgrimes#include <netinet/ip.h>
551590Srgrimes#include <netinet/in_pcb.h>
5636916Speter#include <arpa/inet.h>
571590Srgrimes
581590Srgrimes#include <netdb.h>
591590Srgrimes#include <stdlib.h>
601590Srgrimes#include <string.h>
611590Srgrimes#include <ctype.h>
621590Srgrimes#include "systat.h"
631590Srgrimes#include "extern.h"
641590Srgrimes
651590Srgrimes#define	streq(a,b)	(strcmp(a,b)==0)
661590Srgrimes
671590Srgrimesstatic	struct hitem {
681590Srgrimes	struct	in_addr addr;
691590Srgrimes	int	onoff;
701590Srgrimes} *hosts;
711590Srgrimes
721590Srgrimesint nports, nhosts, protos;
731590Srgrimes
741590Srgrimesstatic void changeitems __P((char *, int));
751590Srgrimesstatic int selectproto __P((char *));
761590Srgrimesstatic void showprotos __P((void));
771590Srgrimesstatic int selectport __P((long, int));
781590Srgrimesstatic void showports __P((void));
791590Srgrimesstatic int selecthost __P((struct in_addr *, int));
801590Srgrimesstatic void showhosts __P((void));
811590Srgrimes
821590Srgrimesint
831590Srgrimesnetcmd(cmd, args)
841590Srgrimes	char *cmd, *args;
851590Srgrimes{
861590Srgrimes
8731522Ssteve	if (prefix(cmd, "proto")) {
8831522Ssteve		if (*args == '\0') {
8931522Ssteve			move(CMDLINE, 0);
9031522Ssteve			clrtoeol();
9131522Ssteve			addstr("which proto?");
9231522Ssteve		} else if (!selectproto(args)) {
9331522Ssteve			error("%s: Unknown protocol.", args);
9431522Ssteve		}
951590Srgrimes		return (1);
961590Srgrimes	}
971590Srgrimes	if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
981590Srgrimes		changeitems(args, prefix(cmd, "display"));
991590Srgrimes		return (1);
1001590Srgrimes	}
1011590Srgrimes	if (prefix(cmd, "reset")) {
1021590Srgrimes		selectproto(0);
1031590Srgrimes		selecthost(0, 0);
1041590Srgrimes		selectport(-1, 0);
1051590Srgrimes		return (1);
1061590Srgrimes	}
1071590Srgrimes	if (prefix(cmd, "show")) {
1081590Srgrimes		move(CMDLINE, 0); clrtoeol();
1091590Srgrimes		if (*args == '\0') {
1101590Srgrimes			showprotos();
1111590Srgrimes			showhosts();
1121590Srgrimes			showports();
1131590Srgrimes			return (1);
1141590Srgrimes		}
1151590Srgrimes		if (prefix(args, "protos"))
1161590Srgrimes			showprotos();
1171590Srgrimes		else if (prefix(args, "hosts"))
1181590Srgrimes			showhosts();
1191590Srgrimes		else if (prefix(args, "ports"))
1201590Srgrimes			showports();
1211590Srgrimes		else
1221590Srgrimes			addstr("show what?");
1231590Srgrimes		return (1);
1241590Srgrimes	}
1251590Srgrimes	return (0);
1261590Srgrimes}
1271590Srgrimes
1281590Srgrimes
1291590Srgrimesstatic void
1301590Srgrimeschangeitems(args, onoff)
1311590Srgrimes	char *args;
1321590Srgrimes	int onoff;
1331590Srgrimes{
1341590Srgrimes	register char *cp;
1351590Srgrimes	struct servent *sp;
1361590Srgrimes	struct hostent *hp;
1371590Srgrimes	struct in_addr in;
1381590Srgrimes	char *index();
1391590Srgrimes
1401590Srgrimes	cp = index(args, '\n');
1411590Srgrimes	if (cp)
1421590Srgrimes		*cp = '\0';
1431590Srgrimes	for (;;args = cp) {
1441590Srgrimes		for (cp = args; *cp && isspace(*cp); cp++)
1451590Srgrimes			;
1461590Srgrimes		args = cp;
1471590Srgrimes		for (; *cp && !isspace(*cp); cp++)
1481590Srgrimes			;
1491590Srgrimes		if (*cp)
1501590Srgrimes			*cp++ = '\0';
1511590Srgrimes		if (cp - args == 0)
1521590Srgrimes			break;
1531590Srgrimes		sp = getservbyname(args,
1541590Srgrimes		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
1551590Srgrimes		if (sp) {
1561590Srgrimes			selectport(sp->s_port, onoff);
1571590Srgrimes			continue;
1581590Srgrimes		}
1591590Srgrimes		hp = gethostbyname(args);
1601590Srgrimes		if (hp == 0) {
1611590Srgrimes			in.s_addr = inet_addr(args);
1621590Srgrimes			if (in.s_addr == -1) {
1631590Srgrimes				error("%s: unknown host or port", args);
1641590Srgrimes				continue;
1651590Srgrimes			}
1661590Srgrimes		} else
1671590Srgrimes			in = *(struct in_addr *)hp->h_addr;
1681590Srgrimes		selecthost(&in, onoff);
1691590Srgrimes	}
1701590Srgrimes}
1711590Srgrimes
1721590Srgrimesstatic int
1731590Srgrimesselectproto(proto)
1741590Srgrimes	char *proto;
1751590Srgrimes{
1761590Srgrimes
1771590Srgrimes	if (proto == 0 || streq(proto, "all"))
17831522Ssteve		protos = TCP | UDP;
1791590Srgrimes	else if (streq(proto, "tcp"))
18031522Ssteve		protos = TCP;
1811590Srgrimes	else if (streq(proto, "udp"))
18231522Ssteve		protos = UDP;
18331522Ssteve	else
18431522Ssteve		return (0);
18531522Ssteve
18631522Ssteve	return (protos);
1871590Srgrimes}
1881590Srgrimes
1891590Srgrimesstatic void
1901590Srgrimesshowprotos()
1911590Srgrimes{
1921590Srgrimes
1931590Srgrimes	if ((protos&TCP) == 0)
1941590Srgrimes		addch('!');
1951590Srgrimes	addstr("tcp ");
1961590Srgrimes	if ((protos&UDP) == 0)
1971590Srgrimes		addch('!');
1981590Srgrimes	addstr("udp ");
1991590Srgrimes}
2001590Srgrimes
2011590Srgrimesstatic	struct pitem {
2021590Srgrimes	long	port;
2031590Srgrimes	int	onoff;
2041590Srgrimes} *ports;
2051590Srgrimes
2061590Srgrimesstatic int
2071590Srgrimesselectport(port, onoff)
2081590Srgrimes	long port;
2091590Srgrimes	int onoff;
2101590Srgrimes{
2111590Srgrimes	register struct pitem *p;
2121590Srgrimes
2131590Srgrimes	if (port == -1) {
2141590Srgrimes		if (ports == 0)
2151590Srgrimes			return (0);
2161590Srgrimes		free((char *)ports), ports = 0;
2171590Srgrimes		nports = 0;
2181590Srgrimes		return (1);
2191590Srgrimes	}
2201590Srgrimes	for (p = ports; p < ports+nports; p++)
2211590Srgrimes		if (p->port == port) {
2221590Srgrimes			p->onoff = onoff;
2231590Srgrimes			return (0);
2241590Srgrimes		}
2251590Srgrimes	if (nports == 0)
2261590Srgrimes		ports = (struct pitem *)malloc(sizeof (*p));
2271590Srgrimes	else
2281590Srgrimes		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
2291590Srgrimes	p = &ports[nports++];
2301590Srgrimes	p->port = port;
2311590Srgrimes	p->onoff = onoff;
2321590Srgrimes	return (1);
2331590Srgrimes}
2341590Srgrimes
2351590Srgrimesint
2361590Srgrimescheckport(inp)
2371590Srgrimes	register struct inpcb *inp;
2381590Srgrimes{
2391590Srgrimes	register struct pitem *p;
2401590Srgrimes
2411590Srgrimes	if (ports)
2421590Srgrimes	for (p = ports; p < ports+nports; p++)
2431590Srgrimes		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
2441590Srgrimes			return (p->onoff);
2451590Srgrimes	return (1);
2461590Srgrimes}
2471590Srgrimes
2481590Srgrimesstatic void
2491590Srgrimesshowports()
2501590Srgrimes{
2511590Srgrimes	register struct pitem *p;
2521590Srgrimes	struct servent *sp;
2531590Srgrimes
2541590Srgrimes	for (p = ports; p < ports+nports; p++) {
2551590Srgrimes		sp = getservbyport(p->port,
2561590Srgrimes		    protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
2571590Srgrimes		if (!p->onoff)
2581590Srgrimes			addch('!');
2591590Srgrimes		if (sp)
2601590Srgrimes			printw("%s ", sp->s_name);
2611590Srgrimes		else
2621590Srgrimes			printw("%d ", p->port);
2631590Srgrimes	}
2641590Srgrimes}
2651590Srgrimes
2661590Srgrimesstatic int
2671590Srgrimesselecthost(in, onoff)
2681590Srgrimes	struct in_addr *in;
2691590Srgrimes	int onoff;
2701590Srgrimes{
2711590Srgrimes	register struct hitem *p;
2721590Srgrimes
2731590Srgrimes	if (in == 0) {
2741590Srgrimes		if (hosts == 0)
2751590Srgrimes			return (0);
2761590Srgrimes		free((char *)hosts), hosts = 0;
2771590Srgrimes		nhosts = 0;
2781590Srgrimes		return (1);
2791590Srgrimes	}
2801590Srgrimes	for (p = hosts; p < hosts+nhosts; p++)
2811590Srgrimes		if (p->addr.s_addr == in->s_addr) {
2821590Srgrimes			p->onoff = onoff;
2831590Srgrimes			return (0);
2841590Srgrimes		}
2851590Srgrimes	if (nhosts == 0)
2861590Srgrimes		hosts = (struct hitem *)malloc(sizeof (*p));
2871590Srgrimes	else
2881590Srgrimes		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
2891590Srgrimes	p = &hosts[nhosts++];
2901590Srgrimes	p->addr = *in;
2911590Srgrimes	p->onoff = onoff;
2921590Srgrimes	return (1);
2931590Srgrimes}
2941590Srgrimes
2951590Srgrimesint
2961590Srgrimescheckhost(inp)
2971590Srgrimes	register struct inpcb *inp;
2981590Srgrimes{
2991590Srgrimes	register struct hitem *p;
3001590Srgrimes
3011590Srgrimes	if (hosts)
3021590Srgrimes	for (p = hosts; p < hosts+nhosts; p++)
3031590Srgrimes		if (p->addr.s_addr == inp->inp_laddr.s_addr ||
3041590Srgrimes		    p->addr.s_addr == inp->inp_faddr.s_addr)
3051590Srgrimes			return (p->onoff);
3061590Srgrimes	return (1);
3071590Srgrimes}
3081590Srgrimes
3091590Srgrimesstatic void
3101590Srgrimesshowhosts()
3111590Srgrimes{
3121590Srgrimes	register struct hitem *p;
3131590Srgrimes	struct hostent *hp;
3141590Srgrimes
3151590Srgrimes	for (p = hosts; p < hosts+nhosts; p++) {
3161590Srgrimes		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
3171590Srgrimes		if (!p->onoff)
3181590Srgrimes			addch('!');
3191590Srgrimes		printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
3201590Srgrimes	}
3211590Srgrimes}
322