netcmds.c revision 216370
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31
32__FBSDID("$FreeBSD: head/usr.bin/systat/netcmds.c 216370 2010-12-11 08:32:16Z joel $");
33
34#ifdef lint
35static const char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 6/6/93";
36#endif
37
38/*
39 * Common network command support routines.
40 */
41#include <sys/param.h>
42#include <sys/queue.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/protosw.h>
46
47#include <net/route.h>
48#include <netinet/in.h>
49#include <netinet/in_systm.h>
50#include <netinet/ip.h>
51#include <netinet/in_pcb.h>
52#include <arpa/inet.h>
53
54#include <ctype.h>
55#include <netdb.h>
56#include <stdlib.h>
57#include <string.h>
58
59#include "systat.h"
60#include "extern.h"
61
62#define	streq(a,b)	(strcmp(a,b)==0)
63
64static	struct hitem {
65	struct	in_addr addr;
66	int	onoff;
67} *hosts;
68
69int nports, nhosts, protos;
70
71static void changeitems(const char *, int);
72static int selectproto(const char *);
73static void showprotos(void);
74static int selectport(long, int);
75static void showports(void);
76static int selecthost(struct in_addr *, int);
77static void showhosts(void);
78
79int
80netcmd(const char *cmd, const char *args)
81{
82
83	if (prefix(cmd, "proto")) {
84		if (*args == '\0') {
85			move(CMDLINE, 0);
86			clrtoeol();
87			addstr("which proto?");
88		} else if (!selectproto(args)) {
89			error("%s: Unknown protocol.", args);
90		}
91		return (1);
92	}
93	if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
94		changeitems(args, prefix(cmd, "display"));
95		return (1);
96	}
97	if (prefix(cmd, "reset")) {
98		selectproto(0);
99		selecthost(0, 0);
100		selectport(-1, 0);
101		return (1);
102	}
103	if (prefix(cmd, "show")) {
104		move(CMDLINE, 0); clrtoeol();
105		if (*args == '\0') {
106			showprotos();
107			showhosts();
108			showports();
109			return (1);
110		}
111		if (prefix(args, "protos"))
112			showprotos();
113		else if (prefix(args, "hosts"))
114			showhosts();
115		else if (prefix(args, "ports"))
116			showports();
117		else
118			addstr("show what?");
119		return (1);
120	}
121	return (0);
122}
123
124
125static void
126changeitems(const char *args, int onoff)
127{
128	char *cp, *tmpstr, *tmpstr1;
129	struct servent *sp;
130	struct hostent *hp;
131	struct in_addr in;
132
133	tmpstr = tmpstr1 = strdup(args);
134	cp = index(tmpstr1, '\n');
135	if (cp)
136		*cp = '\0';
137	for (;;tmpstr1 = cp) {
138		for (cp = tmpstr1; *cp && isspace(*cp); cp++)
139			;
140		tmpstr1 = cp;
141		for (; *cp && !isspace(*cp); cp++)
142			;
143		if (*cp)
144			*cp++ = '\0';
145		if (cp - tmpstr1 == 0)
146			break;
147		sp = getservbyname(tmpstr1,
148		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
149		if (sp) {
150			selectport(sp->s_port, onoff);
151			continue;
152		}
153		hp = gethostbyname(tmpstr1);
154		if (hp == 0) {
155			in.s_addr = inet_addr(tmpstr1);
156			if ((int)in.s_addr == -1) {
157				error("%s: unknown host or port", tmpstr1);
158				continue;
159			}
160		} else
161			in = *(struct in_addr *)hp->h_addr;
162		selecthost(&in, onoff);
163	}
164	free(tmpstr);
165}
166
167static int
168selectproto(const char *proto)
169{
170
171	if (proto == 0 || streq(proto, "all"))
172		protos = TCP | UDP;
173	else if (streq(proto, "tcp"))
174		protos = TCP;
175	else if (streq(proto, "udp"))
176		protos = UDP;
177	else
178		return (0);
179
180	return (protos);
181}
182
183static void
184showprotos(void)
185{
186
187	if ((protos&TCP) == 0)
188		addch('!');
189	addstr("tcp ");
190	if ((protos&UDP) == 0)
191		addch('!');
192	addstr("udp ");
193}
194
195static	struct pitem {
196	long	port;
197	int	onoff;
198} *ports;
199
200static int
201selectport(long port, int onoff)
202{
203	struct pitem *p;
204
205	if (port == -1) {
206		if (ports == 0)
207			return (0);
208		free((char *)ports), ports = 0;
209		nports = 0;
210		return (1);
211	}
212	for (p = ports; p < ports+nports; p++)
213		if (p->port == port) {
214			p->onoff = onoff;
215			return (0);
216		}
217	if (nports == 0)
218		ports = (struct pitem *)malloc(sizeof (*p));
219	else
220		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
221	p = &ports[nports++];
222	p->port = port;
223	p->onoff = onoff;
224	return (1);
225}
226
227int
228checkport(struct inpcb *inp)
229{
230	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(void)
241{
242	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(struct in_addr *in, int onoff)
259{
260	struct hitem *p;
261
262	if (in == 0) {
263		if (hosts == 0)
264			return (0);
265		free((char *)hosts), hosts = 0;
266		nhosts = 0;
267		return (1);
268	}
269	for (p = hosts; p < hosts+nhosts; p++)
270		if (p->addr.s_addr == in->s_addr) {
271			p->onoff = onoff;
272			return (0);
273		}
274	if (nhosts == 0)
275		hosts = (struct hitem *)malloc(sizeof (*p));
276	else
277		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
278	p = &hosts[nhosts++];
279	p->addr = *in;
280	p->onoff = onoff;
281	return (1);
282}
283
284int
285checkhost(struct inpcb *inp)
286{
287	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(void)
299{
300	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