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.2 2003/06/17 04:29:32 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 __P((char *, int));
71 static int selectproto __P((char *));
72 static void showprotos __P((void));
73 static int selectport __P((long, int));
74 static void showports __P((void));
75 static int selecthost __P((struct in_addr *, int));
76 static void showhosts __P((void));
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?");
126 changeitems(args, onoff)
136 cp = index(args, '\n');
140 for (cp = args; *cp && isspace(*cp); cp++)
143 for (; *cp && !isspace(*cp); cp++)
149 sp = getservbyname(args,
150 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
152 selectport(sp->s_port, onoff);
155 hp = gethostbyname(args);
157 in.s_addr = inet_addr(args);
158 if (in.s_addr == -1) {
159 error("%s: unknown host or port", args);
163 in = *(struct in_addr *)hp->h_addr;
164 selecthost(&in, onoff);
173 if (proto == 0 || streq(proto, "all"))
175 else if (streq(proto, "tcp"))
177 else if (streq(proto, "udp"))
189 if ((protos&TCP) == 0)
192 if ((protos&UDP) == 0)
197 static struct pitem {
203 selectport(port, onoff)
207 register struct pitem *p;
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++];
233 register struct inpcb *inp;
235 register struct pitem *p;
238 for (p = ports; p < ports+nports; p++)
239 if (p->port == inp->inp_lport || p->port == inp->inp_fport)
247 register struct pitem *p;
250 for (p = ports; p < ports+nports; p++) {
251 sp = getservbyport(p->port,
252 protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
256 printw("%s ", sp->s_name);
258 printw("%d ", p->port);
263 selecthost(in, onoff)
267 register struct hitem *p;
272 free((char *)hosts), hosts = 0;
276 for (p = hosts; p < hosts+nhosts; p++)
277 if (p->addr.s_addr == in->s_addr) {
282 hosts = (struct hitem *)malloc(sizeof (*p));
284 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
285 p = &hosts[nhosts++];
293 register struct inpcb *inp;
295 register struct hitem *p;
298 for (p = hosts; p < hosts+nhosts; p++)
299 if (p->addr.s_addr == inp->inp_laddr.s_addr ||
300 p->addr.s_addr == inp->inp_faddr.s_addr)
308 register struct hitem *p;
311 for (p = hosts; p < hosts+nhosts; p++) {
312 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
315 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));