2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93";
41 * Common network command support routines.
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/protosw.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/in_pcb.h>
54 #include <arpa/inet.h>
64 #define streq(a,b) (strcmp(a,b)==0)
71 int nports, nhosts, protos;
73 static void changeitems(const char *, int);
74 static int selectproto(const char *);
75 static void showprotos(void);
76 static int selectport(long, int);
77 static void showports(void);
78 static int selecthost(struct in_addr *, int);
79 static void showhosts(void);
82 netcmd(const char *cmd, const char *args)
85 if (prefix(cmd, "proto")) {
89 addstr("which proto?");
90 } else if (!selectproto(args)) {
91 error("%s: Unknown protocol.", args);
95 if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
96 changeitems(args, prefix(cmd, "display"));
99 if (prefix(cmd, "reset")) {
105 if (prefix(cmd, "show")) {
106 move(CMDLINE, 0); clrtoeol();
113 if (prefix(args, "protos"))
115 else if (prefix(args, "hosts"))
117 else if (prefix(args, "ports"))
120 addstr("show what?");
127 changeitems(const char *args, int onoff)
129 char *cp, *tmpstr, *tmpstr1;
134 tmpstr = tmpstr1 = strdup(args);
135 cp = strchr(tmpstr1, '\n');
138 for (;;tmpstr1 = cp) {
139 for (cp = tmpstr1; *cp && isspace(*cp); cp++)
142 for (; *cp && !isspace(*cp); cp++)
146 if (cp - tmpstr1 == 0)
148 sp = getservbyname(tmpstr1,
149 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
151 selectport(sp->s_port, onoff);
154 hp = gethostbyname(tmpstr1);
156 in.s_addr = inet_addr(tmpstr1);
157 if (in.s_addr == INADDR_NONE) {
158 error("%s: unknown host or port", tmpstr1);
162 in = *(struct in_addr *)hp->h_addr;
163 selecthost(&in, onoff);
169 selectproto(const char *proto)
172 if (proto == NULL || streq(proto, "all"))
174 else if (streq(proto, "tcp"))
176 else if (streq(proto, "udp"))
188 if ((protos&TCP) == 0)
191 if ((protos&UDP) == 0)
196 static struct pitem {
202 selectport(long port, int onoff)
209 free((char *)ports), ports = 0;
213 for (p = ports; p < ports + nports; p++)
214 if (p->port == port) {
219 ports = (struct pitem *)malloc(sizeof (*p));
221 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
222 p = &ports[nports++];
229 checkport(struct in_conninfo *inc)
234 for (p = ports; p < ports+nports; p++)
235 if (p->port == inc->inc_lport || p->port == inc->inc_fport)
246 for (p = ports; p < ports+nports; p++) {
247 sp = getservbyport(p->port,
248 protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
252 printw("%s ", sp->s_name);
254 printw("%d ", p->port);
259 selecthost(struct in_addr *in, int onoff)
266 free((char *)hosts), hosts = 0;
270 for (p = hosts; p < hosts+nhosts; p++)
271 if (p->addr.s_addr == in->s_addr) {
276 hosts = (struct hitem *)malloc(sizeof (*p));
278 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
279 p = &hosts[nhosts++];
286 checkhost(struct in_conninfo *inc)
291 for (p = hosts; p < hosts+nhosts; p++)
292 if (p->addr.s_addr == inc->inc_laddr.s_addr ||
293 p->addr.s_addr == inc->inc_faddr.s_addr)
304 for (p = hosts; p < hosts+nhosts; p++) {
305 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
308 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));