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
34 static const char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93";
38 * Common network command support routines.
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/protosw.h>
46 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <netinet/in_pcb.h>
51 #include <arpa/inet.h>
61 #define streq(a,b) (strcmp(a,b)==0)
68 int nports, nhosts, protos;
70 static void changeitems(const char *, int);
71 static int selectproto(const char *);
72 static void showprotos(void);
73 static int selectport(long, int);
74 static void showports(void);
75 static int selecthost(struct in_addr *, int);
76 static void showhosts(void);
79 netcmd(const char *cmd, const char *args)
82 if (prefix(cmd, "proto")) {
86 addstr("which proto?");
87 } else if (!selectproto(args)) {
88 error("%s: Unknown protocol.", args);
92 if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
93 changeitems(args, prefix(cmd, "display"));
96 if (prefix(cmd, "reset")) {
102 if (prefix(cmd, "show")) {
103 move(CMDLINE, 0); clrtoeol();
110 if (prefix(args, "protos"))
112 else if (prefix(args, "hosts"))
114 else if (prefix(args, "ports"))
117 addstr("show what?");
124 changeitems(const char *args, int onoff)
126 char *cp, *tmpstr, *tmpstr1;
131 tmpstr = tmpstr1 = strdup(args);
132 cp = strchr(tmpstr1, '\n');
135 for (;;tmpstr1 = cp) {
136 for (cp = tmpstr1; *cp && isspace(*cp); cp++)
139 for (; *cp && !isspace(*cp); cp++)
143 if (cp - tmpstr1 == 0)
145 sp = getservbyname(tmpstr1,
146 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
148 selectport(sp->s_port, onoff);
151 hp = gethostbyname(tmpstr1);
153 in.s_addr = inet_addr(tmpstr1);
154 if (in.s_addr == INADDR_NONE) {
155 error("%s: unknown host or port", tmpstr1);
159 in = *(struct in_addr *)hp->h_addr;
160 selecthost(&in, onoff);
166 selectproto(const char *proto)
169 if (proto == NULL || streq(proto, "all"))
171 else if (streq(proto, "tcp"))
173 else if (streq(proto, "udp"))
185 if ((protos&TCP) == 0)
188 if ((protos&UDP) == 0)
193 static struct pitem {
199 selectport(long port, int onoff)
206 free((char *)ports), ports = 0;
210 for (p = ports; p < ports + nports; p++)
211 if (p->port == port) {
216 ports = (struct pitem *)malloc(sizeof (*p));
218 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
219 p = &ports[nports++];
226 checkport(struct in_conninfo *inc)
231 for (p = ports; p < ports+nports; p++)
232 if (p->port == inc->inc_lport || p->port == inc->inc_fport)
243 for (p = ports; p < ports+nports; p++) {
244 sp = getservbyport(p->port,
245 protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
249 printw("%s ", sp->s_name);
251 printw("%d ", p->port);
256 selecthost(struct in_addr *in, int onoff)
263 free((char *)hosts), hosts = 0;
267 for (p = hosts; p < hosts+nhosts; p++)
268 if (p->addr.s_addr == in->s_addr) {
273 hosts = (struct hitem *)malloc(sizeof (*p));
275 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
276 p = &hosts[nhosts++];
283 checkhost(struct in_conninfo *inc)
288 for (p = hosts; p < hosts+nhosts; p++)
289 if (p->addr.s_addr == inc->inc_laddr.s_addr ||
290 p->addr.s_addr == inc->inc_faddr.s_addr)
301 for (p = hosts; p < hosts+nhosts; p++) {
302 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
305 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));