2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93";
43 * Common network command support routines.
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/in_pcb.h>
56 #include <arpa/inet.h>
66 #define streq(a,b) (strcmp(a,b)==0)
73 int nports, nhosts, protos;
75 static void changeitems(const char *, int);
76 static int selectproto(const char *);
77 static void showprotos(void);
78 static int selectport(long, int);
79 static void showports(void);
80 static int selecthost(struct in_addr *, int);
81 static void showhosts(void);
84 netcmd(const char *cmd, const char *args)
87 if (prefix(cmd, "proto")) {
91 addstr("which proto?");
92 } else if (!selectproto(args)) {
93 error("%s: Unknown protocol.", args);
97 if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
98 changeitems(args, prefix(cmd, "display"));
101 if (prefix(cmd, "reset")) {
107 if (prefix(cmd, "show")) {
108 move(CMDLINE, 0); clrtoeol();
115 if (prefix(args, "protos"))
117 else if (prefix(args, "hosts"))
119 else if (prefix(args, "ports"))
122 addstr("show what?");
130 changeitems(const char *args, int onoff)
132 char *cp, *tmpstr, *tmpstr1;
137 tmpstr = tmpstr1 = strdup(args);
138 cp = index(tmpstr1, '\n');
141 for (;;tmpstr1 = cp) {
142 for (cp = tmpstr1; *cp && isspace(*cp); cp++)
145 for (; *cp && !isspace(*cp); cp++)
149 if (cp - tmpstr1 == 0)
151 sp = getservbyname(tmpstr1,
152 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
154 selectport(sp->s_port, onoff);
157 hp = gethostbyname(tmpstr1);
159 in.s_addr = inet_addr(tmpstr1);
160 if ((int)in.s_addr == -1) {
161 error("%s: unknown host or port", tmpstr1);
165 in = *(struct in_addr *)hp->h_addr;
166 selecthost(&in, onoff);
172 selectproto(const char *proto)
175 if (proto == 0 || streq(proto, "all"))
177 else if (streq(proto, "tcp"))
179 else if (streq(proto, "udp"))
191 if ((protos&TCP) == 0)
194 if ((protos&UDP) == 0)
199 static struct pitem {
205 selectport(long port, int onoff)
212 free((char *)ports), ports = 0;
216 for (p = ports; p < ports+nports; p++)
217 if (p->port == port) {
222 ports = (struct pitem *)malloc(sizeof (*p));
224 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
225 p = &ports[nports++];
232 checkport(struct inpcb *inp)
237 for (p = ports; p < ports+nports; p++)
238 if (p->port == inp->inp_lport || p->port == inp->inp_fport)
249 for (p = ports; p < ports+nports; p++) {
250 sp = getservbyport(p->port,
251 protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
255 printw("%s ", sp->s_name);
257 printw("%d ", p->port);
262 selecthost(struct in_addr *in, int onoff)
269 free((char *)hosts), hosts = 0;
273 for (p = hosts; p < hosts+nhosts; p++)
274 if (p->addr.s_addr == in->s_addr) {
279 hosts = (struct hitem *)malloc(sizeof (*p));
281 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
282 p = &hosts[nhosts++];
289 checkhost(struct inpcb *inp)
294 for (p = hosts; p < hosts+nhosts; p++)
295 if (p->addr.s_addr == inp->inp_laddr.s_addr ||
296 p->addr.s_addr == inp->inp_faddr.s_addr)
307 for (p = hosts; p < hosts+nhosts; p++) {
308 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
311 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));