netcmds.c revision 7715
1/*-
2 * Copyright (c) 1980, 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 6/6/93";
36#endif /* not lint */
37
38/*
39 * Common network command support routines.
40 */
41#include <sys/param.h>
42#include <sys/socket.h>
43#include <sys/socketvar.h>
44#include <sys/mbuf.h>
45#include <sys/protosw.h>
46#include <sys/queue.h>
47
48#include <net/route.h>
49#include <netinet/in.h>
50#include <netinet/in_systm.h>
51#include <netinet/ip.h>
52#include <netinet/in_pcb.h>
53
54#include <netdb.h>
55#include <stdlib.h>
56#include <string.h>
57#include <ctype.h>
58#include "systat.h"
59#include "extern.h"
60
61#define	streq(a,b)	(strcmp(a,b)==0)
62
63static	struct hitem {
64	struct	in_addr addr;
65	int	onoff;
66} *hosts;
67
68int nports, nhosts, protos;
69
70static void changeitems __P((char *, int));
71static int selectproto __P((char *));
72static void showprotos __P((void));
73static int selectport __P((long, int));
74static void showports __P((void));
75static int selecthost __P((struct in_addr *, int));
76static void showhosts __P((void));
77
78int
79netcmd(cmd, args)
80	char *cmd, *args;
81{
82
83	if (prefix(cmd, "tcp") || prefix(cmd, "udp")) {
84		selectproto(cmd);
85		return (1);
86	}
87	if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
88		changeitems(args, prefix(cmd, "display"));
89		return (1);
90	}
91	if (prefix(cmd, "reset")) {
92		selectproto(0);
93		selecthost(0, 0);
94		selectport(-1, 0);
95		return (1);
96	}
97	if (prefix(cmd, "show")) {
98		move(CMDLINE, 0); clrtoeol();
99		if (*args == '\0') {
100			showprotos();
101			showhosts();
102			showports();
103			return (1);
104		}
105		if (prefix(args, "protos"))
106			showprotos();
107		else if (prefix(args, "hosts"))
108			showhosts();
109		else if (prefix(args, "ports"))
110			showports();
111		else
112			addstr("show what?");
113		return (1);
114	}
115	return (0);
116}
117
118
119static void
120changeitems(args, onoff)
121	char *args;
122	int onoff;
123{
124	register char *cp;
125	struct servent *sp;
126	struct hostent *hp;
127	struct in_addr in;
128	char *index();
129
130	cp = index(args, '\n');
131	if (cp)
132		*cp = '\0';
133	for (;;args = cp) {
134		for (cp = args; *cp && isspace(*cp); cp++)
135			;
136		args = cp;
137		for (; *cp && !isspace(*cp); cp++)
138			;
139		if (*cp)
140			*cp++ = '\0';
141		if (cp - args == 0)
142			break;
143		sp = getservbyname(args,
144		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
145		if (sp) {
146			selectport(sp->s_port, onoff);
147			continue;
148		}
149		hp = gethostbyname(args);
150		if (hp == 0) {
151			in.s_addr = inet_addr(args);
152			if (in.s_addr == -1) {
153				error("%s: unknown host or port", args);
154				continue;
155			}
156		} else
157			in = *(struct in_addr *)hp->h_addr;
158		selecthost(&in, onoff);
159	}
160}
161
162static int
163selectproto(proto)
164	char *proto;
165{
166	int new = protos;
167
168	if (proto == 0 || streq(proto, "all"))
169		new = TCP|UDP;
170	else if (streq(proto, "tcp"))
171		new = TCP;
172	else if (streq(proto, "udp"))
173		new = UDP;
174	return (new != protos, protos = new);
175}
176
177static void
178showprotos()
179{
180
181	if ((protos&TCP) == 0)
182		addch('!');
183	addstr("tcp ");
184	if ((protos&UDP) == 0)
185		addch('!');
186	addstr("udp ");
187}
188
189static	struct pitem {
190	long	port;
191	int	onoff;
192} *ports;
193
194static int
195selectport(port, onoff)
196	long port;
197	int onoff;
198{
199	register struct pitem *p;
200
201	if (port == -1) {
202		if (ports == 0)
203			return (0);
204		free((char *)ports), ports = 0;
205		nports = 0;
206		return (1);
207	}
208	for (p = ports; p < ports+nports; p++)
209		if (p->port == port) {
210			p->onoff = onoff;
211			return (0);
212		}
213	if (nports == 0)
214		ports = (struct pitem *)malloc(sizeof (*p));
215	else
216		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
217	p = &ports[nports++];
218	p->port = port;
219	p->onoff = onoff;
220	return (1);
221}
222
223int
224checkport(inp)
225	register struct inpcb *inp;
226{
227	register struct pitem *p;
228
229	if (ports)
230	for (p = ports; p < ports+nports; p++)
231		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
232			return (p->onoff);
233	return (1);
234}
235
236static void
237showports()
238{
239	register struct pitem *p;
240	struct servent *sp;
241
242	for (p = ports; p < ports+nports; p++) {
243		sp = getservbyport(p->port,
244		    protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
245		if (!p->onoff)
246			addch('!');
247		if (sp)
248			printw("%s ", sp->s_name);
249		else
250			printw("%d ", p->port);
251	}
252}
253
254static int
255selecthost(in, onoff)
256	struct in_addr *in;
257	int onoff;
258{
259	register struct hitem *p;
260
261	if (in == 0) {
262		if (hosts == 0)
263			return (0);
264		free((char *)hosts), hosts = 0;
265		nhosts = 0;
266		return (1);
267	}
268	for (p = hosts; p < hosts+nhosts; p++)
269		if (p->addr.s_addr == in->s_addr) {
270			p->onoff = onoff;
271			return (0);
272		}
273	if (nhosts == 0)
274		hosts = (struct hitem *)malloc(sizeof (*p));
275	else
276		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
277	p = &hosts[nhosts++];
278	p->addr = *in;
279	p->onoff = onoff;
280	return (1);
281}
282
283int
284checkhost(inp)
285	register struct inpcb *inp;
286{
287	register struct hitem *p;
288
289	if (hosts)
290	for (p = hosts; p < hosts+nhosts; p++)
291		if (p->addr.s_addr == inp->inp_laddr.s_addr ||
292		    p->addr.s_addr == inp->inp_faddr.s_addr)
293			return (p->onoff);
294	return (1);
295}
296
297static void
298showhosts()
299{
300	register struct hitem *p;
301	struct hostent *hp;
302
303	for (p = hosts; p < hosts+nhosts; p++) {
304		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
305		if (!p->onoff)
306			addch('!');
307		printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
308	}
309}
310