xref: /netbsd-src/usr.bin/systat/netcmds.c (revision f5d3fbbc6ff4a77159fb268d247bd94cb7d7e332)
1 /*	$NetBSD: netcmds.c,v 1.6 1997/10/19 23:36:28 lukem 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.6 1997/10/19 23:36:28 lukem 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/socketvar.h>
50 #include <sys/mbuf.h>
51 #include <sys/protosw.h>
52 
53 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_pcb.h>
58 
59 #include <arpa/inet.h>
60 
61 #include <netdb.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <ctype.h>
65 #include "systat.h"
66 #include "extern.h"
67 
68 #define	streq(a,b)	(strcmp(a,b)==0)
69 
70 static	struct hitem {
71 	struct	in_addr addr;
72 	int	onoff;
73 } *hosts;
74 
75 int nports, nhosts, protos;
76 
77 static void changeitems __P((char *, int));
78 static void selectproto __P((char *));
79 static void showprotos __P((void));
80 static int selectport __P((long, int));
81 static void showports __P((void));
82 static int selecthost __P((struct in_addr *, int));
83 static void showhosts __P((void));
84 
85 int
86 netcmd(cmd, args)
87 	char *cmd, *args;
88 {
89 
90 	if (prefix(cmd, "tcp") || prefix(cmd, "udp")) {
91 		selectproto(cmd);
92 		return (1);
93 	}
94 	if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
95 		changeitems(args, prefix(cmd, "display"));
96 		return (1);
97 	}
98 	if (prefix(cmd, "reset")) {
99 		selectproto(0);
100 		selecthost(0, 0);
101 		selectport(-1, 0);
102 		return (1);
103 	}
104 	if (prefix(cmd, "show")) {
105 		move(CMDLINE, 0); clrtoeol();
106 		if (*args == '\0') {
107 			showprotos();
108 			showhosts();
109 			showports();
110 			return (1);
111 		}
112 		if (prefix(args, "protos"))
113 			showprotos();
114 		else if (prefix(args, "hosts"))
115 			showhosts();
116 		else if (prefix(args, "ports"))
117 			showports();
118 		else
119 			addstr("show what?");
120 		return (1);
121 	}
122 	return (0);
123 }
124 
125 
126 static void
127 changeitems(args, onoff)
128 	char *args;
129 	int onoff;
130 {
131 	char *cp;
132 	struct servent *sp;
133 	struct hostent *hp;
134 	struct in_addr in;
135 
136 	cp = strchr(args, '\n');
137 	if (cp)
138 		*cp = '\0';
139 	for (;;args = cp) {
140 		for (cp = args; *cp && isspace(*cp); cp++)
141 			;
142 		args = cp;
143 		for (; *cp && !isspace(*cp); cp++)
144 			;
145 		if (*cp)
146 			*cp++ = '\0';
147 		if (cp - args == 0)
148 			break;
149 		sp = getservbyname(args,
150 		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
151 		if (sp) {
152 			selectport(sp->s_port, onoff);
153 			continue;
154 		}
155 		if (inet_aton(args, &in) == 0) {
156 			hp = gethostbyname(args);
157 			if (hp == 0) {
158 				error("%s: unknown host or port", args);
159 				continue;
160 			}
161 			memcpy(&in, hp->h_addr, hp->h_length);
162 		}
163 		selecthost(&in, onoff);
164 	}
165 }
166 
167 static void
168 selectproto(proto)
169 	char *proto;
170 {
171 
172 	if (proto == 0 || streq(proto, "all"))
173 		protos = TCP|UDP;
174 	else if (streq(proto, "tcp"))
175 		protos = TCP;
176 	else if (streq(proto, "udp"))
177 		protos = UDP;
178 }
179 
180 static void
181 showprotos()
182 {
183 
184 	if ((protos&TCP) == 0)
185 		addch('!');
186 	addstr("tcp ");
187 	if ((protos&UDP) == 0)
188 		addch('!');
189 	addstr("udp ");
190 }
191 
192 static	struct pitem {
193 	long	port;
194 	int	onoff;
195 } *ports;
196 
197 static int
198 selectport(port, onoff)
199 	long port;
200 	int onoff;
201 {
202 	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 
226 int
227 checkport(inp)
228 	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 
239 static void
240 showports()
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 
257 static int
258 selecthost(in, onoff)
259 	struct in_addr *in;
260 	int onoff;
261 {
262 	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 
286 int
287 checkhost(inp)
288 	struct inpcb *inp;
289 {
290 	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 
300 static void
301 showhosts()
302 {
303 	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 : inet_ntoa(p->addr));
311 	}
312 }
313