netcmds.c revision 1590
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
351590Srgrimesstatic char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 6/6/93";
361590Srgrimes#endif /* not lint */
371590Srgrimes
381590Srgrimes/*
391590Srgrimes * Common network command support routines.
401590Srgrimes */
411590Srgrimes#include <sys/param.h>
421590Srgrimes#include <sys/socket.h>
431590Srgrimes#include <sys/socketvar.h>
441590Srgrimes#include <sys/mbuf.h>
451590Srgrimes#include <sys/protosw.h>
461590Srgrimes
471590Srgrimes#include <net/route.h>
481590Srgrimes#include <netinet/in.h>
491590Srgrimes#include <netinet/in_systm.h>
501590Srgrimes#include <netinet/ip.h>
511590Srgrimes#include <netinet/in_pcb.h>
521590Srgrimes
531590Srgrimes#include <netdb.h>
541590Srgrimes#include <stdlib.h>
551590Srgrimes#include <string.h>
561590Srgrimes#include <ctype.h>
571590Srgrimes#include "systat.h"
581590Srgrimes#include "extern.h"
591590Srgrimes
601590Srgrimes#define	streq(a,b)	(strcmp(a,b)==0)
611590Srgrimes
621590Srgrimesstatic	struct hitem {
631590Srgrimes	struct	in_addr addr;
641590Srgrimes	int	onoff;
651590Srgrimes} *hosts;
661590Srgrimes
671590Srgrimesint nports, nhosts, protos;
681590Srgrimes
691590Srgrimesstatic void changeitems __P((char *, int));
701590Srgrimesstatic int selectproto __P((char *));
711590Srgrimesstatic void showprotos __P((void));
721590Srgrimesstatic int selectport __P((long, int));
731590Srgrimesstatic void showports __P((void));
741590Srgrimesstatic int selecthost __P((struct in_addr *, int));
751590Srgrimesstatic void showhosts __P((void));
761590Srgrimes
771590Srgrimesint
781590Srgrimesnetcmd(cmd, args)
791590Srgrimes	char *cmd, *args;
801590Srgrimes{
811590Srgrimes
821590Srgrimes	if (prefix(cmd, "tcp") || prefix(cmd, "udp")) {
831590Srgrimes		selectproto(cmd);
841590Srgrimes		return (1);
851590Srgrimes	}
861590Srgrimes	if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
871590Srgrimes		changeitems(args, prefix(cmd, "display"));
881590Srgrimes		return (1);
891590Srgrimes	}
901590Srgrimes	if (prefix(cmd, "reset")) {
911590Srgrimes		selectproto(0);
921590Srgrimes		selecthost(0, 0);
931590Srgrimes		selectport(-1, 0);
941590Srgrimes		return (1);
951590Srgrimes	}
961590Srgrimes	if (prefix(cmd, "show")) {
971590Srgrimes		move(CMDLINE, 0); clrtoeol();
981590Srgrimes		if (*args == '\0') {
991590Srgrimes			showprotos();
1001590Srgrimes			showhosts();
1011590Srgrimes			showports();
1021590Srgrimes			return (1);
1031590Srgrimes		}
1041590Srgrimes		if (prefix(args, "protos"))
1051590Srgrimes			showprotos();
1061590Srgrimes		else if (prefix(args, "hosts"))
1071590Srgrimes			showhosts();
1081590Srgrimes		else if (prefix(args, "ports"))
1091590Srgrimes			showports();
1101590Srgrimes		else
1111590Srgrimes			addstr("show what?");
1121590Srgrimes		return (1);
1131590Srgrimes	}
1141590Srgrimes	return (0);
1151590Srgrimes}
1161590Srgrimes
1171590Srgrimes
1181590Srgrimesstatic void
1191590Srgrimeschangeitems(args, onoff)
1201590Srgrimes	char *args;
1211590Srgrimes	int onoff;
1221590Srgrimes{
1231590Srgrimes	register char *cp;
1241590Srgrimes	struct servent *sp;
1251590Srgrimes	struct hostent *hp;
1261590Srgrimes	struct in_addr in;
1271590Srgrimes	char *index();
1281590Srgrimes
1291590Srgrimes	cp = index(args, '\n');
1301590Srgrimes	if (cp)
1311590Srgrimes		*cp = '\0';
1321590Srgrimes	for (;;args = cp) {
1331590Srgrimes		for (cp = args; *cp && isspace(*cp); cp++)
1341590Srgrimes			;
1351590Srgrimes		args = cp;
1361590Srgrimes		for (; *cp && !isspace(*cp); cp++)
1371590Srgrimes			;
1381590Srgrimes		if (*cp)
1391590Srgrimes			*cp++ = '\0';
1401590Srgrimes		if (cp - args == 0)
1411590Srgrimes			break;
1421590Srgrimes		sp = getservbyname(args,
1431590Srgrimes		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
1441590Srgrimes		if (sp) {
1451590Srgrimes			selectport(sp->s_port, onoff);
1461590Srgrimes			continue;
1471590Srgrimes		}
1481590Srgrimes		hp = gethostbyname(args);
1491590Srgrimes		if (hp == 0) {
1501590Srgrimes			in.s_addr = inet_addr(args);
1511590Srgrimes			if (in.s_addr == -1) {
1521590Srgrimes				error("%s: unknown host or port", args);
1531590Srgrimes				continue;
1541590Srgrimes			}
1551590Srgrimes		} else
1561590Srgrimes			in = *(struct in_addr *)hp->h_addr;
1571590Srgrimes		selecthost(&in, onoff);
1581590Srgrimes	}
1591590Srgrimes}
1601590Srgrimes
1611590Srgrimesstatic int
1621590Srgrimesselectproto(proto)
1631590Srgrimes	char *proto;
1641590Srgrimes{
1651590Srgrimes	int new = protos;
1661590Srgrimes
1671590Srgrimes	if (proto == 0 || streq(proto, "all"))
1681590Srgrimes		new = TCP|UDP;
1691590Srgrimes	else if (streq(proto, "tcp"))
1701590Srgrimes		new = TCP;
1711590Srgrimes	else if (streq(proto, "udp"))
1721590Srgrimes		new = UDP;
1731590Srgrimes	return (new != protos, protos = new);
1741590Srgrimes}
1751590Srgrimes
1761590Srgrimesstatic void
1771590Srgrimesshowprotos()
1781590Srgrimes{
1791590Srgrimes
1801590Srgrimes	if ((protos&TCP) == 0)
1811590Srgrimes		addch('!');
1821590Srgrimes	addstr("tcp ");
1831590Srgrimes	if ((protos&UDP) == 0)
1841590Srgrimes		addch('!');
1851590Srgrimes	addstr("udp ");
1861590Srgrimes}
1871590Srgrimes
1881590Srgrimesstatic	struct pitem {
1891590Srgrimes	long	port;
1901590Srgrimes	int	onoff;
1911590Srgrimes} *ports;
1921590Srgrimes
1931590Srgrimesstatic int
1941590Srgrimesselectport(port, onoff)
1951590Srgrimes	long port;
1961590Srgrimes	int onoff;
1971590Srgrimes{
1981590Srgrimes	register struct pitem *p;
1991590Srgrimes
2001590Srgrimes	if (port == -1) {
2011590Srgrimes		if (ports == 0)
2021590Srgrimes			return (0);
2031590Srgrimes		free((char *)ports), ports = 0;
2041590Srgrimes		nports = 0;
2051590Srgrimes		return (1);
2061590Srgrimes	}
2071590Srgrimes	for (p = ports; p < ports+nports; p++)
2081590Srgrimes		if (p->port == port) {
2091590Srgrimes			p->onoff = onoff;
2101590Srgrimes			return (0);
2111590Srgrimes		}
2121590Srgrimes	if (nports == 0)
2131590Srgrimes		ports = (struct pitem *)malloc(sizeof (*p));
2141590Srgrimes	else
2151590Srgrimes		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
2161590Srgrimes	p = &ports[nports++];
2171590Srgrimes	p->port = port;
2181590Srgrimes	p->onoff = onoff;
2191590Srgrimes	return (1);
2201590Srgrimes}
2211590Srgrimes
2221590Srgrimesint
2231590Srgrimescheckport(inp)
2241590Srgrimes	register struct inpcb *inp;
2251590Srgrimes{
2261590Srgrimes	register struct pitem *p;
2271590Srgrimes
2281590Srgrimes	if (ports)
2291590Srgrimes	for (p = ports; p < ports+nports; p++)
2301590Srgrimes		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
2311590Srgrimes			return (p->onoff);
2321590Srgrimes	return (1);
2331590Srgrimes}
2341590Srgrimes
2351590Srgrimesstatic void
2361590Srgrimesshowports()
2371590Srgrimes{
2381590Srgrimes	register struct pitem *p;
2391590Srgrimes	struct servent *sp;
2401590Srgrimes
2411590Srgrimes	for (p = ports; p < ports+nports; p++) {
2421590Srgrimes		sp = getservbyport(p->port,
2431590Srgrimes		    protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
2441590Srgrimes		if (!p->onoff)
2451590Srgrimes			addch('!');
2461590Srgrimes		if (sp)
2471590Srgrimes			printw("%s ", sp->s_name);
2481590Srgrimes		else
2491590Srgrimes			printw("%d ", p->port);
2501590Srgrimes	}
2511590Srgrimes}
2521590Srgrimes
2531590Srgrimesstatic int
2541590Srgrimesselecthost(in, onoff)
2551590Srgrimes	struct in_addr *in;
2561590Srgrimes	int onoff;
2571590Srgrimes{
2581590Srgrimes	register struct hitem *p;
2591590Srgrimes
2601590Srgrimes	if (in == 0) {
2611590Srgrimes		if (hosts == 0)
2621590Srgrimes			return (0);
2631590Srgrimes		free((char *)hosts), hosts = 0;
2641590Srgrimes		nhosts = 0;
2651590Srgrimes		return (1);
2661590Srgrimes	}
2671590Srgrimes	for (p = hosts; p < hosts+nhosts; p++)
2681590Srgrimes		if (p->addr.s_addr == in->s_addr) {
2691590Srgrimes			p->onoff = onoff;
2701590Srgrimes			return (0);
2711590Srgrimes		}
2721590Srgrimes	if (nhosts == 0)
2731590Srgrimes		hosts = (struct hitem *)malloc(sizeof (*p));
2741590Srgrimes	else
2751590Srgrimes		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
2761590Srgrimes	p = &hosts[nhosts++];
2771590Srgrimes	p->addr = *in;
2781590Srgrimes	p->onoff = onoff;
2791590Srgrimes	return (1);
2801590Srgrimes}
2811590Srgrimes
2821590Srgrimesint
2831590Srgrimescheckhost(inp)
2841590Srgrimes	register struct inpcb *inp;
2851590Srgrimes{
2861590Srgrimes	register struct hitem *p;
2871590Srgrimes
2881590Srgrimes	if (hosts)
2891590Srgrimes	for (p = hosts; p < hosts+nhosts; p++)
2901590Srgrimes		if (p->addr.s_addr == inp->inp_laddr.s_addr ||
2911590Srgrimes		    p->addr.s_addr == inp->inp_faddr.s_addr)
2921590Srgrimes			return (p->onoff);
2931590Srgrimes	return (1);
2941590Srgrimes}
2951590Srgrimes
2961590Srgrimesstatic void
2971590Srgrimesshowhosts()
2981590Srgrimes{
2991590Srgrimes	register struct hitem *p;
3001590Srgrimes	struct hostent *hp;
3011590Srgrimes
3021590Srgrimes	for (p = hosts; p < hosts+nhosts; p++) {
3031590Srgrimes		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
3041590Srgrimes		if (!p->onoff)
3051590Srgrimes			addch('!');
3061590Srgrimes		printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
3071590Srgrimes	}
3081590Srgrimes}
309