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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93";
39 * Common network command support routines.
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>
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>
62 #define streq(a,b) (strcmp(a,b)==0)
69 int nports, nhosts, protos;
71 static void changeitems(const char *, int);
72 static int selectproto(const char *);
73 static void showprotos(void);
74 static int selectport(long, int);
75 static void showports(void);
76 static int selecthost(struct in_addr *, int);
77 static void showhosts(void);
80 netcmd(const char *cmd, const char *args)
83 if (prefix(cmd, "proto")) {
87 addstr("which proto?");
88 } else if (!selectproto(args)) {
89 error("%s: Unknown protocol.", args);
93 if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
94 changeitems(args, prefix(cmd, "display"));
97 if (prefix(cmd, "reset")) {
103 if (prefix(cmd, "show")) {
104 move(CMDLINE, 0); clrtoeol();
111 if (prefix(args, "protos"))
113 else if (prefix(args, "hosts"))
115 else if (prefix(args, "ports"))
118 addstr("show what?");
125 changeitems(const char *args, int onoff)
127 char *cp, *tmpstr, *tmpstr1;
132 tmpstr = tmpstr1 = strdup(args);
133 cp = strchr(tmpstr1, '\n');
136 for (;;tmpstr1 = cp) {
137 for (cp = tmpstr1; *cp && isspace(*cp); cp++)
140 for (; *cp && !isspace(*cp); cp++)
144 if (cp - tmpstr1 == 0)
146 sp = getservbyname(tmpstr1,
147 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
149 selectport(sp->s_port, onoff);
152 hp = gethostbyname(tmpstr1);
154 in.s_addr = inet_addr(tmpstr1);
155 if (in.s_addr == INADDR_NONE) {
156 error("%s: unknown host or port", tmpstr1);
160 in = *(struct in_addr *)hp->h_addr;
161 selecthost(&in, onoff);
167 selectproto(const char *proto)
170 if (proto == NULL || streq(proto, "all"))
172 else if (streq(proto, "tcp"))
174 else if (streq(proto, "udp"))
186 if ((protos&TCP) == 0)
189 if ((protos&UDP) == 0)
194 static struct pitem {
200 selectport(long port, int onoff)
207 free((char *)ports), ports = 0;
211 for (p = ports; p < ports + nports; p++)
212 if (p->port == port) {
217 ports = (struct pitem *)malloc(sizeof (*p));
219 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
220 p = &ports[nports++];
227 checkport(struct inpcb *inp)
232 for (p = ports; p < ports+nports; p++)
233 if (p->port == inp->inp_lport || p->port == inp->inp_fport)
244 for (p = ports; p < ports+nports; p++) {
245 sp = getservbyport(p->port,
246 protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
250 printw("%s ", sp->s_name);
252 printw("%d ", p->port);
257 selecthost(struct in_addr *in, int onoff)
264 free((char *)hosts), hosts = 0;
268 for (p = hosts; p < hosts+nhosts; p++)
269 if (p->addr.s_addr == in->s_addr) {
274 hosts = (struct hitem *)malloc(sizeof (*p));
276 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
277 p = &hosts[nhosts++];
284 checkhost(struct inpcb *inp)
289 for (p = hosts; p < hosts+nhosts; p++)
290 if (p->addr.s_addr == inp->inp_laddr.s_addr ||
291 p->addr.s_addr == inp->inp_faddr.s_addr)
302 for (p = hosts; p < hosts+nhosts; p++) {
303 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
306 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));