netcmds.c revision 31522
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[] =
3931522Ssteve	"$Id: netcmds.c,v 1.6 1997/02/22 19:57:16 peter 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>
561590Srgrimes
571590Srgrimes#include <netdb.h>
581590Srgrimes#include <stdlib.h>
591590Srgrimes#include <string.h>
601590Srgrimes#include <ctype.h>
611590Srgrimes#include "systat.h"
621590Srgrimes#include "extern.h"
631590Srgrimes
641590Srgrimes#define	streq(a,b)	(strcmp(a,b)==0)
651590Srgrimes
661590Srgrimesstatic	struct hitem {
671590Srgrimes	struct	in_addr addr;
681590Srgrimes	int	onoff;
691590Srgrimes} *hosts;
701590Srgrimes
711590Srgrimesint nports, nhosts, protos;
721590Srgrimes
731590Srgrimesstatic void changeitems __P((char *, int));
741590Srgrimesstatic int selectproto __P((char *));
751590Srgrimesstatic void showprotos __P((void));
761590Srgrimesstatic int selectport __P((long, int));
771590Srgrimesstatic void showports __P((void));
781590Srgrimesstatic int selecthost __P((struct in_addr *, int));
791590Srgrimesstatic void showhosts __P((void));
801590Srgrimes
811590Srgrimesint
821590Srgrimesnetcmd(cmd, args)
831590Srgrimes	char *cmd, *args;
841590Srgrimes{
851590Srgrimes
8631522Ssteve	if (prefix(cmd, "proto")) {
8731522Ssteve		if (*args == '\0') {
8831522Ssteve			move(CMDLINE, 0);
8931522Ssteve			clrtoeol();
9031522Ssteve			addstr("which proto?");
9131522Ssteve		} else if (!selectproto(args)) {
9231522Ssteve			error("%s: Unknown protocol.", args);
9331522Ssteve		}
941590Srgrimes		return (1);
951590Srgrimes	}
961590Srgrimes	if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
971590Srgrimes		changeitems(args, prefix(cmd, "display"));
981590Srgrimes		return (1);
991590Srgrimes	}
1001590Srgrimes	if (prefix(cmd, "reset")) {
1011590Srgrimes		selectproto(0);
1021590Srgrimes		selecthost(0, 0);
1031590Srgrimes		selectport(-1, 0);
1041590Srgrimes		return (1);
1051590Srgrimes	}
1061590Srgrimes	if (prefix(cmd, "show")) {
1071590Srgrimes		move(CMDLINE, 0); clrtoeol();
1081590Srgrimes		if (*args == '\0') {
1091590Srgrimes			showprotos();
1101590Srgrimes			showhosts();
1111590Srgrimes			showports();
1121590Srgrimes			return (1);
1131590Srgrimes		}
1141590Srgrimes		if (prefix(args, "protos"))
1151590Srgrimes			showprotos();
1161590Srgrimes		else if (prefix(args, "hosts"))
1171590Srgrimes			showhosts();
1181590Srgrimes		else if (prefix(args, "ports"))
1191590Srgrimes			showports();
1201590Srgrimes		else
1211590Srgrimes			addstr("show what?");
1221590Srgrimes		return (1);
1231590Srgrimes	}
1241590Srgrimes	return (0);
1251590Srgrimes}
1261590Srgrimes
1271590Srgrimes
1281590Srgrimesstatic void
1291590Srgrimeschangeitems(args, onoff)
1301590Srgrimes	char *args;
1311590Srgrimes	int onoff;
1321590Srgrimes{
1331590Srgrimes	register char *cp;
1341590Srgrimes	struct servent *sp;
1351590Srgrimes	struct hostent *hp;
1361590Srgrimes	struct in_addr in;
1371590Srgrimes	char *index();
1381590Srgrimes
1391590Srgrimes	cp = index(args, '\n');
1401590Srgrimes	if (cp)
1411590Srgrimes		*cp = '\0';
1421590Srgrimes	for (;;args = cp) {
1431590Srgrimes		for (cp = args; *cp && isspace(*cp); cp++)
1441590Srgrimes			;
1451590Srgrimes		args = cp;
1461590Srgrimes		for (; *cp && !isspace(*cp); cp++)
1471590Srgrimes			;
1481590Srgrimes		if (*cp)
1491590Srgrimes			*cp++ = '\0';
1501590Srgrimes		if (cp - args == 0)
1511590Srgrimes			break;
1521590Srgrimes		sp = getservbyname(args,
1531590Srgrimes		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
1541590Srgrimes		if (sp) {
1551590Srgrimes			selectport(sp->s_port, onoff);
1561590Srgrimes			continue;
1571590Srgrimes		}
1581590Srgrimes		hp = gethostbyname(args);
1591590Srgrimes		if (hp == 0) {
1601590Srgrimes			in.s_addr = inet_addr(args);
1611590Srgrimes			if (in.s_addr == -1) {
1621590Srgrimes				error("%s: unknown host or port", args);
1631590Srgrimes				continue;
1641590Srgrimes			}
1651590Srgrimes		} else
1661590Srgrimes			in = *(struct in_addr *)hp->h_addr;
1671590Srgrimes		selecthost(&in, onoff);
1681590Srgrimes	}
1691590Srgrimes}
1701590Srgrimes
1711590Srgrimesstatic int
1721590Srgrimesselectproto(proto)
1731590Srgrimes	char *proto;
1741590Srgrimes{
1751590Srgrimes
1761590Srgrimes	if (proto == 0 || streq(proto, "all"))
17731522Ssteve		protos = TCP | UDP;
1781590Srgrimes	else if (streq(proto, "tcp"))
17931522Ssteve		protos = TCP;
1801590Srgrimes	else if (streq(proto, "udp"))
18131522Ssteve		protos = UDP;
18231522Ssteve	else
18331522Ssteve		return (0);
18431522Ssteve
18531522Ssteve	return (protos);
1861590Srgrimes}
1871590Srgrimes
1881590Srgrimesstatic void
1891590Srgrimesshowprotos()
1901590Srgrimes{
1911590Srgrimes
1921590Srgrimes	if ((protos&TCP) == 0)
1931590Srgrimes		addch('!');
1941590Srgrimes	addstr("tcp ");
1951590Srgrimes	if ((protos&UDP) == 0)
1961590Srgrimes		addch('!');
1971590Srgrimes	addstr("udp ");
1981590Srgrimes}
1991590Srgrimes
2001590Srgrimesstatic	struct pitem {
2011590Srgrimes	long	port;
2021590Srgrimes	int	onoff;
2031590Srgrimes} *ports;
2041590Srgrimes
2051590Srgrimesstatic int
2061590Srgrimesselectport(port, onoff)
2071590Srgrimes	long port;
2081590Srgrimes	int onoff;
2091590Srgrimes{
2101590Srgrimes	register struct pitem *p;
2111590Srgrimes
2121590Srgrimes	if (port == -1) {
2131590Srgrimes		if (ports == 0)
2141590Srgrimes			return (0);
2151590Srgrimes		free((char *)ports), ports = 0;
2161590Srgrimes		nports = 0;
2171590Srgrimes		return (1);
2181590Srgrimes	}
2191590Srgrimes	for (p = ports; p < ports+nports; p++)
2201590Srgrimes		if (p->port == port) {
2211590Srgrimes			p->onoff = onoff;
2221590Srgrimes			return (0);
2231590Srgrimes		}
2241590Srgrimes	if (nports == 0)
2251590Srgrimes		ports = (struct pitem *)malloc(sizeof (*p));
2261590Srgrimes	else
2271590Srgrimes		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
2281590Srgrimes	p = &ports[nports++];
2291590Srgrimes	p->port = port;
2301590Srgrimes	p->onoff = onoff;
2311590Srgrimes	return (1);
2321590Srgrimes}
2331590Srgrimes
2341590Srgrimesint
2351590Srgrimescheckport(inp)
2361590Srgrimes	register struct inpcb *inp;
2371590Srgrimes{
2381590Srgrimes	register struct pitem *p;
2391590Srgrimes
2401590Srgrimes	if (ports)
2411590Srgrimes	for (p = ports; p < ports+nports; p++)
2421590Srgrimes		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
2431590Srgrimes			return (p->onoff);
2441590Srgrimes	return (1);
2451590Srgrimes}
2461590Srgrimes
2471590Srgrimesstatic void
2481590Srgrimesshowports()
2491590Srgrimes{
2501590Srgrimes	register struct pitem *p;
2511590Srgrimes	struct servent *sp;
2521590Srgrimes
2531590Srgrimes	for (p = ports; p < ports+nports; p++) {
2541590Srgrimes		sp = getservbyport(p->port,
2551590Srgrimes		    protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
2561590Srgrimes		if (!p->onoff)
2571590Srgrimes			addch('!');
2581590Srgrimes		if (sp)
2591590Srgrimes			printw("%s ", sp->s_name);
2601590Srgrimes		else
2611590Srgrimes			printw("%d ", p->port);
2621590Srgrimes	}
2631590Srgrimes}
2641590Srgrimes
2651590Srgrimesstatic int
2661590Srgrimesselecthost(in, onoff)
2671590Srgrimes	struct in_addr *in;
2681590Srgrimes	int onoff;
2691590Srgrimes{
2701590Srgrimes	register struct hitem *p;
2711590Srgrimes
2721590Srgrimes	if (in == 0) {
2731590Srgrimes		if (hosts == 0)
2741590Srgrimes			return (0);
2751590Srgrimes		free((char *)hosts), hosts = 0;
2761590Srgrimes		nhosts = 0;
2771590Srgrimes		return (1);
2781590Srgrimes	}
2791590Srgrimes	for (p = hosts; p < hosts+nhosts; p++)
2801590Srgrimes		if (p->addr.s_addr == in->s_addr) {
2811590Srgrimes			p->onoff = onoff;
2821590Srgrimes			return (0);
2831590Srgrimes		}
2841590Srgrimes	if (nhosts == 0)
2851590Srgrimes		hosts = (struct hitem *)malloc(sizeof (*p));
2861590Srgrimes	else
2871590Srgrimes		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
2881590Srgrimes	p = &hosts[nhosts++];
2891590Srgrimes	p->addr = *in;
2901590Srgrimes	p->onoff = onoff;
2911590Srgrimes	return (1);
2921590Srgrimes}
2931590Srgrimes
2941590Srgrimesint
2951590Srgrimescheckhost(inp)
2961590Srgrimes	register struct inpcb *inp;
2971590Srgrimes{
2981590Srgrimes	register struct hitem *p;
2991590Srgrimes
3001590Srgrimes	if (hosts)
3011590Srgrimes	for (p = hosts; p < hosts+nhosts; p++)
3021590Srgrimes		if (p->addr.s_addr == inp->inp_laddr.s_addr ||
3031590Srgrimes		    p->addr.s_addr == inp->inp_faddr.s_addr)
3041590Srgrimes			return (p->onoff);
3051590Srgrimes	return (1);
3061590Srgrimes}
3071590Srgrimes
3081590Srgrimesstatic void
3091590Srgrimesshowhosts()
3101590Srgrimes{
3111590Srgrimes	register struct hitem *p;
3121590Srgrimes	struct hostent *hp;
3131590Srgrimes
3141590Srgrimes	for (p = hosts; p < hosts+nhosts; p++) {
3151590Srgrimes		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
3161590Srgrimes		if (!p->onoff)
3171590Srgrimes			addch('!');
3181590Srgrimes		printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
3191590Srgrimes	}
3201590Srgrimes}
321