netcmds.c revision 21673
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
35/*
36static char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 6/6/93";
37*/
38static const char rcsid[] =
39	"$FreeBSD: head/usr.bin/systat/netcmds.c 21673 1997-01-14 07:20:47Z jkh $";
40#endif /* not lint */
41
42/*
43 * Common network command support routines.
44 */
45#include <sys/param.h>
46#include <sys/queue.h>
47#include <sys/socket.h>
48#include <sys/socketvar.h>
49#include <sys/protosw.h>
50
51#include <net/route.h>
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/ip.h>
55#include <netinet/in_pcb.h>
56
57#include <netdb.h>
58#include <stdlib.h>
59#include <string.h>
60#include <ctype.h>
61#include "systat.h"
62#include "extern.h"
63
64#define	streq(a,b)	(strcmp(a,b)==0)
65
66static	struct hitem {
67	struct	in_addr addr;
68	int	onoff;
69} *hosts;
70
71int nports, nhosts, protos;
72
73static void changeitems __P((char *, int));
74static int selectproto __P((char *));
75static void showprotos __P((void));
76static int selectport __P((long, int));
77static void showports __P((void));
78static int selecthost __P((struct in_addr *, int));
79static void showhosts __P((void));
80
81int
82netcmd(cmd, args)
83	char *cmd, *args;
84{
85
86	if (prefix(cmd, "tcp") || prefix(cmd, "udp")) {
87		selectproto(cmd);
88		return (1);
89	}
90	if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
91		changeitems(args, prefix(cmd, "display"));
92		return (1);
93	}
94	if (prefix(cmd, "reset")) {
95		selectproto(0);
96		selecthost(0, 0);
97		selectport(-1, 0);
98		return (1);
99	}
100	if (prefix(cmd, "show")) {
101		move(CMDLINE, 0); clrtoeol();
102		if (*args == '\0') {
103			showprotos();
104			showhosts();
105			showports();
106			return (1);
107		}
108		if (prefix(args, "protos"))
109			showprotos();
110		else if (prefix(args, "hosts"))
111			showhosts();
112		else if (prefix(args, "ports"))
113			showports();
114		else
115			addstr("show what?");
116		return (1);
117	}
118	return (0);
119}
120
121
122static void
123changeitems(args, onoff)
124	char *args;
125	int onoff;
126{
127	register char *cp;
128	struct servent *sp;
129	struct hostent *hp;
130	struct in_addr in;
131	char *index();
132
133	cp = index(args, '\n');
134	if (cp)
135		*cp = '\0';
136	for (;;args = cp) {
137		for (cp = args; *cp && isspace(*cp); cp++)
138			;
139		args = cp;
140		for (; *cp && !isspace(*cp); cp++)
141			;
142		if (*cp)
143			*cp++ = '\0';
144		if (cp - args == 0)
145			break;
146		sp = getservbyname(args,
147		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
148		if (sp) {
149			selectport(sp->s_port, onoff);
150			continue;
151		}
152		hp = gethostbyname(args);
153		if (hp == 0) {
154			in.s_addr = inet_addr(args);
155			if (in.s_addr == -1) {
156				error("%s: unknown host or port", args);
157				continue;
158			}
159		} else
160			in = *(struct in_addr *)hp->h_addr;
161		selecthost(&in, onoff);
162	}
163}
164
165static int
166selectproto(proto)
167	char *proto;
168{
169	int new = protos;
170
171	if (proto == 0 || streq(proto, "all"))
172		new = TCP|UDP;
173	else if (streq(proto, "tcp"))
174		new = TCP;
175	else if (streq(proto, "udp"))
176		new = UDP;
177	return (new != protos, protos = new);
178}
179
180static void
181showprotos()
182{
183
184	if ((protos&TCP) == 0)
185		addch('!');
186	addstr("tcp ");
187	if ((protos&UDP) == 0)
188		addch('!');
189	addstr("udp ");
190}
191
192static	struct pitem {
193	long	port;
194	int	onoff;
195} *ports;
196
197static int
198selectport(port, onoff)
199	long port;
200	int onoff;
201{
202	register struct pitem *p;
203
204	if (port == -1) {
205		if (ports == 0)
206			return (0);
207		free((char *)ports), ports = 0;
208		nports = 0;
209		return (1);
210	}
211	for (p = ports; p < ports+nports; p++)
212		if (p->port == port) {
213			p->onoff = onoff;
214			return (0);
215		}
216	if (nports == 0)
217		ports = (struct pitem *)malloc(sizeof (*p));
218	else
219		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
220	p = &ports[nports++];
221	p->port = port;
222	p->onoff = onoff;
223	return (1);
224}
225
226int
227checkport(inp)
228	register struct inpcb *inp;
229{
230	register struct pitem *p;
231
232	if (ports)
233	for (p = ports; p < ports+nports; p++)
234		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
235			return (p->onoff);
236	return (1);
237}
238
239static void
240showports()
241{
242	register struct pitem *p;
243	struct servent *sp;
244
245	for (p = ports; p < ports+nports; p++) {
246		sp = getservbyport(p->port,
247		    protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
248		if (!p->onoff)
249			addch('!');
250		if (sp)
251			printw("%s ", sp->s_name);
252		else
253			printw("%d ", p->port);
254	}
255}
256
257static int
258selecthost(in, onoff)
259	struct in_addr *in;
260	int onoff;
261{
262	register struct hitem *p;
263
264	if (in == 0) {
265		if (hosts == 0)
266			return (0);
267		free((char *)hosts), hosts = 0;
268		nhosts = 0;
269		return (1);
270	}
271	for (p = hosts; p < hosts+nhosts; p++)
272		if (p->addr.s_addr == in->s_addr) {
273			p->onoff = onoff;
274			return (0);
275		}
276	if (nhosts == 0)
277		hosts = (struct hitem *)malloc(sizeof (*p));
278	else
279		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
280	p = &hosts[nhosts++];
281	p->addr = *in;
282	p->onoff = onoff;
283	return (1);
284}
285
286int
287checkhost(inp)
288	register struct inpcb *inp;
289{
290	register struct hitem *p;
291
292	if (hosts)
293	for (p = hosts; p < hosts+nhosts; p++)
294		if (p->addr.s_addr == inp->inp_laddr.s_addr ||
295		    p->addr.s_addr == inp->inp_faddr.s_addr)
296			return (p->onoff);
297	return (1);
298}
299
300static void
301showhosts()
302{
303	register struct hitem *p;
304	struct hostent *hp;
305
306	for (p = hosts; p < hosts+nhosts; p++) {
307		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
308		if (!p->onoff)
309			addch('!');
310		printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
311	}
312}
313