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
29 * @(#)netcmds.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/systat/netcmds.c,v 1.9 1999/08/28 01:06:04 peter Exp $
34 * Common network command support routines.
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/protosw.h>
42 #include <net/route.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46 #include <netinet/in_pcb.h>
47 #include <arpa/inet.h>
56 #define streq(a,b) (strcmp(a,b)==0)
63 int nports, nhosts, protos;
65 static void changeitems(char *, int);
66 static int selectproto(char *);
67 static void showprotos(void);
68 static int selectport(long, int);
69 static void showports(void);
70 static int selecthost(struct in_addr *, int);
71 static void showhosts(void);
74 netcmd(const char *cmd, char *args)
77 if (prefix(cmd, "proto")) {
81 addstr("which proto?");
82 } else if (!selectproto(args)) {
83 error("%s: Unknown protocol.", args);
87 if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
88 changeitems(args, prefix(cmd, "display"));
91 if (prefix(cmd, "reset")) {
97 if (prefix(cmd, "show")) {
98 move(CMDLINE, 0); clrtoeol();
105 if (prefix(args, "protos"))
107 else if (prefix(args, "hosts"))
109 else if (prefix(args, "ports"))
112 addstr("show what?");
120 changeitems(char *args, int onoff)
127 cp = strchr(args, '\n');
131 for (cp = args; *cp && isspace(*cp); cp++)
134 for (; *cp && !isspace(*cp); cp++)
140 sp = getservbyname(args,
141 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
143 selectport(sp->s_port, onoff);
146 hp = gethostbyname(args);
148 in.s_addr = inet_addr(args);
149 if ((int)in.s_addr == -1) {
150 error("%s: unknown host or port", args);
154 in = *(struct in_addr *)hp->h_addr;
155 selecthost(&in, onoff);
160 selectproto(char *proto)
163 if (proto == NULL || streq(proto, "all"))
165 else if (streq(proto, "tcp"))
167 else if (streq(proto, "udp"))
179 if ((protos&TCP) == 0)
182 if ((protos&UDP) == 0)
187 static struct pitem {
193 selectport(long port, int onoff)
200 free((char *)ports), ports = NULL;
204 for (p = ports; p < ports+nports; p++)
205 if (p->port == port) {
210 ports = (struct pitem *)malloc(sizeof (*p));
212 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
213 p = &ports[nports++];
220 checkport(struct inpcb *inp)
225 for (p = ports; p < ports+nports; p++) {
226 if (p->port == inp->inp_lport ||
227 p->port == inp->inp_fport)
239 for (p = ports; p < ports+nports; p++) {
240 sp = getservbyport(p->port,
241 protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
245 printw("%s ", sp->s_name);
247 printw("%d ", p->port);
252 selecthost(struct in_addr *in, int onoff)
259 free((char *)hosts), hosts = NULL;
263 for (p = hosts; p < hosts+nhosts; p++)
264 if (p->addr.s_addr == in->s_addr) {
269 hosts = (struct hitem *)malloc(sizeof (*p));
271 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
272 p = &hosts[nhosts++];
279 checkhost(struct inpcb *inp)
284 for (p = hosts; p < hosts+nhosts; p++) {
285 if (p->addr.s_addr == inp->inp_laddr.s_addr ||
286 p->addr.s_addr == inp->inp_faddr.s_addr)
298 for (p = hosts; p < hosts+nhosts; p++) {
299 hp = gethostbyaddr(&p->addr, sizeof (p->addr), AF_INET);
302 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));