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
33 * @(#)netcmds.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/systat/netcmds.c,v 1.9 1999/08/28 01:06:04 peter Exp $
35 * $DragonFly: src/usr.bin/systat/netcmds.c,v 1.5 2004/09/03 20:38:01 dillon Exp $
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>
61 #define streq(a,b) (strcmp(a,b)==0)
68 int nports, nhosts, protos;
70 static void changeitems(char *, int);
71 static int selectproto(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(char *cmd, 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?");
125 changeitems(char *args, int onoff)
132 cp = strchr(args, '\n');
136 for (cp = args; *cp && isspace(*cp); cp++)
139 for (; *cp && !isspace(*cp); cp++)
145 sp = getservbyname(args,
146 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
148 selectport(sp->s_port, onoff);
151 hp = gethostbyname(args);
153 in.s_addr = inet_addr(args);
154 if (in.s_addr == -1) {
155 error("%s: unknown host or port", args);
159 in = *(struct in_addr *)hp->h_addr;
160 selecthost(&in, onoff);
165 selectproto(char *proto)
168 if (proto == 0 || streq(proto, "all"))
170 else if (streq(proto, "tcp"))
172 else if (streq(proto, "udp"))
184 if ((protos&TCP) == 0)
187 if ((protos&UDP) == 0)
192 static struct pitem {
198 selectport(long port, int onoff)
200 register struct pitem *p;
205 free((char *)ports), ports = 0;
209 for (p = ports; p < ports+nports; p++)
210 if (p->port == port) {
215 ports = (struct pitem *)malloc(sizeof (*p));
217 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
218 p = &ports[nports++];
225 checkport(register struct inpcb *inp)
227 register struct pitem *p;
230 for (p = ports; p < ports+nports; p++)
231 if (p->port == inp->inp_lport || p->port == inp->inp_fport)
239 register struct pitem *p;
242 for (p = ports; p < ports+nports; p++) {
243 sp = getservbyport(p->port,
244 protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
248 printw("%s ", sp->s_name);
250 printw("%d ", p->port);
255 selecthost(struct in_addr *in, int onoff)
257 register struct hitem *p;
262 free((char *)hosts), hosts = 0;
266 for (p = hosts; p < hosts+nhosts; p++)
267 if (p->addr.s_addr == in->s_addr) {
272 hosts = (struct hitem *)malloc(sizeof (*p));
274 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
275 p = &hosts[nhosts++];
282 checkhost(register struct inpcb *inp)
284 register struct hitem *p;
287 for (p = hosts; p < hosts+nhosts; p++)
288 if (p->addr.s_addr == inp->inp_laddr.s_addr ||
289 p->addr.s_addr == inp->inp_faddr.s_addr)
297 register struct hitem *p;
300 for (p = hosts; p < hosts+nhosts; p++) {
301 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
304 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));