xref: /dragonfly/usr.bin/systat/netcmds.c (revision 1de703da)
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  * @(#)netcmds.c	8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.bin/systat/netcmds.c,v 1.9 1999/08/28 01:06:04 peter Exp $
35  * $DragonFly: src/usr.bin/systat/netcmds.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
36  */
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 <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 
63 static	struct hitem {
64 	struct	in_addr addr;
65 	int	onoff;
66 } *hosts;
67 
68 int nports, nhosts, protos;
69 
70 static void changeitems __P((char *, int));
71 static int selectproto __P((char *));
72 static void showprotos __P((void));
73 static int selectport __P((long, int));
74 static void showports __P((void));
75 static int selecthost __P((struct in_addr *, int));
76 static void showhosts __P((void));
77 
78 int
79 netcmd(cmd, args)
80 	char *cmd, *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 
125 static void
126 changeitems(args, onoff)
127 	char *args;
128 	int onoff;
129 {
130 	register char *cp;
131 	struct servent *sp;
132 	struct hostent *hp;
133 	struct in_addr in;
134 	char *index();
135 
136 	cp = index(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 		hp = gethostbyname(args);
156 		if (hp == 0) {
157 			in.s_addr = inet_addr(args);
158 			if (in.s_addr == -1) {
159 				error("%s: unknown host or port", args);
160 				continue;
161 			}
162 		} else
163 			in = *(struct in_addr *)hp->h_addr;
164 		selecthost(&in, onoff);
165 	}
166 }
167 
168 static int
169 selectproto(proto)
170 	char *proto;
171 {
172 
173 	if (proto == 0 || streq(proto, "all"))
174 		protos = TCP | UDP;
175 	else if (streq(proto, "tcp"))
176 		protos = TCP;
177 	else if (streq(proto, "udp"))
178 		protos = UDP;
179 	else
180 		return (0);
181 
182 	return (protos);
183 }
184 
185 static void
186 showprotos()
187 {
188 
189 	if ((protos&TCP) == 0)
190 		addch('!');
191 	addstr("tcp ");
192 	if ((protos&UDP) == 0)
193 		addch('!');
194 	addstr("udp ");
195 }
196 
197 static	struct pitem {
198 	long	port;
199 	int	onoff;
200 } *ports;
201 
202 static int
203 selectport(port, onoff)
204 	long port;
205 	int onoff;
206 {
207 	register struct pitem *p;
208 
209 	if (port == -1) {
210 		if (ports == 0)
211 			return (0);
212 		free((char *)ports), ports = 0;
213 		nports = 0;
214 		return (1);
215 	}
216 	for (p = ports; p < ports+nports; p++)
217 		if (p->port == port) {
218 			p->onoff = onoff;
219 			return (0);
220 		}
221 	if (nports == 0)
222 		ports = (struct pitem *)malloc(sizeof (*p));
223 	else
224 		ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
225 	p = &ports[nports++];
226 	p->port = port;
227 	p->onoff = onoff;
228 	return (1);
229 }
230 
231 int
232 checkport(inp)
233 	register struct inpcb *inp;
234 {
235 	register struct pitem *p;
236 
237 	if (ports)
238 	for (p = ports; p < ports+nports; p++)
239 		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
240 			return (p->onoff);
241 	return (1);
242 }
243 
244 static void
245 showports()
246 {
247 	register struct pitem *p;
248 	struct servent *sp;
249 
250 	for (p = ports; p < ports+nports; p++) {
251 		sp = getservbyport(p->port,
252 		    protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
253 		if (!p->onoff)
254 			addch('!');
255 		if (sp)
256 			printw("%s ", sp->s_name);
257 		else
258 			printw("%d ", p->port);
259 	}
260 }
261 
262 static int
263 selecthost(in, onoff)
264 	struct in_addr *in;
265 	int onoff;
266 {
267 	register struct hitem *p;
268 
269 	if (in == 0) {
270 		if (hosts == 0)
271 			return (0);
272 		free((char *)hosts), hosts = 0;
273 		nhosts = 0;
274 		return (1);
275 	}
276 	for (p = hosts; p < hosts+nhosts; p++)
277 		if (p->addr.s_addr == in->s_addr) {
278 			p->onoff = onoff;
279 			return (0);
280 		}
281 	if (nhosts == 0)
282 		hosts = (struct hitem *)malloc(sizeof (*p));
283 	else
284 		hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
285 	p = &hosts[nhosts++];
286 	p->addr = *in;
287 	p->onoff = onoff;
288 	return (1);
289 }
290 
291 int
292 checkhost(inp)
293 	register struct inpcb *inp;
294 {
295 	register struct hitem *p;
296 
297 	if (hosts)
298 	for (p = hosts; p < hosts+nhosts; p++)
299 		if (p->addr.s_addr == inp->inp_laddr.s_addr ||
300 		    p->addr.s_addr == inp->inp_faddr.s_addr)
301 			return (p->onoff);
302 	return (1);
303 }
304 
305 static void
306 showhosts()
307 {
308 	register struct hitem *p;
309 	struct hostent *hp;
310 
311 	for (p = hosts; p < hosts+nhosts; p++) {
312 		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
313 		if (!p->onoff)
314 			addch('!');
315 		printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
316 	}
317 }
318