netcmds.c revision 1.3 1 /* $NetBSD: netcmds.c,v 1.3 1995/04/29 05:54:48 cgd 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: netcmds.c,v 1.3 1995/04/29 05:54:48 cgd Exp $";
41 #endif /* not lint */
42
43 /*
44 * Common network command support routines.
45 */
46 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.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 int 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 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 int new = protos;
173
174 if (proto == 0 || streq(proto, "all"))
175 new = TCP|UDP;
176 else if (streq(proto, "tcp"))
177 new = TCP;
178 else if (streq(proto, "udp"))
179 new = UDP;
180 return (new != protos, protos = new);
181 }
182
183 static void
184 showprotos()
185 {
186
187 if ((protos&TCP) == 0)
188 addch('!');
189 addstr("tcp ");
190 if ((protos&UDP) == 0)
191 addch('!');
192 addstr("udp ");
193 }
194
195 static struct pitem {
196 long port;
197 int onoff;
198 } *ports;
199
200 static int
201 selectport(port, onoff)
202 long port;
203 int onoff;
204 {
205 register struct pitem *p;
206
207 if (port == -1) {
208 if (ports == 0)
209 return (0);
210 free((char *)ports), ports = 0;
211 nports = 0;
212 return (1);
213 }
214 for (p = ports; p < ports+nports; p++)
215 if (p->port == port) {
216 p->onoff = onoff;
217 return (0);
218 }
219 if (nports == 0)
220 ports = (struct pitem *)malloc(sizeof (*p));
221 else
222 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
223 p = &ports[nports++];
224 p->port = port;
225 p->onoff = onoff;
226 return (1);
227 }
228
229 int
230 checkport(inp)
231 register struct inpcb *inp;
232 {
233 register struct pitem *p;
234
235 if (ports)
236 for (p = ports; p < ports+nports; p++)
237 if (p->port == inp->inp_lport || p->port == inp->inp_fport)
238 return (p->onoff);
239 return (1);
240 }
241
242 static void
243 showports()
244 {
245 register struct pitem *p;
246 struct servent *sp;
247
248 for (p = ports; p < ports+nports; p++) {
249 sp = getservbyport(p->port,
250 protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
251 if (!p->onoff)
252 addch('!');
253 if (sp)
254 printw("%s ", sp->s_name);
255 else
256 printw("%d ", p->port);
257 }
258 }
259
260 static int
261 selecthost(in, onoff)
262 struct in_addr *in;
263 int onoff;
264 {
265 register struct hitem *p;
266
267 if (in == 0) {
268 if (hosts == 0)
269 return (0);
270 free((char *)hosts), hosts = 0;
271 nhosts = 0;
272 return (1);
273 }
274 for (p = hosts; p < hosts+nhosts; p++)
275 if (p->addr.s_addr == in->s_addr) {
276 p->onoff = onoff;
277 return (0);
278 }
279 if (nhosts == 0)
280 hosts = (struct hitem *)malloc(sizeof (*p));
281 else
282 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
283 p = &hosts[nhosts++];
284 p->addr = *in;
285 p->onoff = onoff;
286 return (1);
287 }
288
289 int
290 checkhost(inp)
291 register struct inpcb *inp;
292 {
293 register struct hitem *p;
294
295 if (hosts)
296 for (p = hosts; p < hosts+nhosts; p++)
297 if (p->addr.s_addr == inp->inp_laddr.s_addr ||
298 p->addr.s_addr == inp->inp_faddr.s_addr)
299 return (p->onoff);
300 return (1);
301 }
302
303 static void
304 showhosts()
305 {
306 register struct hitem *p;
307 struct hostent *hp;
308
309 for (p = hosts; p < hosts+nhosts; p++) {
310 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
311 if (!p->onoff)
312 addch('!');
313 printw("%s ", hp ? hp->h_name : inet_ntoa(p->addr));
314 }
315 }
316