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