kbdmap: Sync with FreeBSD.
[dragonfly.git] / usr.bin / systat / netcmds.c
blob6ad6c2575015d900691ab82df612856a79a53196
1 /*-
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
7 * are met:
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
31 * SUCH DAMAGE.
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 $
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>
53 #include <netdb.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <ctype.h>
57 #include "systat.h"
58 #include "extern.h"
60 #define streq(a,b) (strcmp(a,b)==0)
62 static struct hitem {
63 struct in_addr addr;
64 int onoff;
65 } *hosts;
67 int nports, nhosts, protos;
69 static void changeitems(char *, int);
70 static int selectproto(char *);
71 static void showprotos(void);
72 static int selectport(long, int);
73 static void showports(void);
74 static int selecthost(struct in_addr *, int);
75 static void showhosts(void);
77 int
78 netcmd(const char *cmd, char *args)
81 if (prefix(cmd, "proto")) {
82 if (*args == '\0') {
83 move(CMDLINE, 0);
84 clrtoeol();
85 addstr("which proto?");
86 } else if (!selectproto(args)) {
87 error("%s: Unknown protocol.", args);
89 return (1);
91 if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
92 changeitems(args, prefix(cmd, "display"));
93 return (1);
95 if (prefix(cmd, "reset")) {
96 selectproto(0);
97 selecthost(0, 0);
98 selectport(-1, 0);
99 return (1);
101 if (prefix(cmd, "show")) {
102 move(CMDLINE, 0); clrtoeol();
103 if (*args == '\0') {
104 showprotos();
105 showhosts();
106 showports();
107 return (1);
109 if (prefix(args, "protos"))
110 showprotos();
111 else if (prefix(args, "hosts"))
112 showhosts();
113 else if (prefix(args, "ports"))
114 showports();
115 else
116 addstr("show what?");
117 return (1);
119 return (0);
123 static void
124 changeitems(char *args, int onoff)
126 char *cp;
127 struct servent *sp;
128 struct hostent *hp;
129 struct in_addr in;
131 cp = strchr(args, '\n');
132 if (cp)
133 *cp = '\0';
134 for (;;args = cp) {
135 for (cp = args; *cp && isspace(*cp); cp++)
137 args = cp;
138 for (; *cp && !isspace(*cp); cp++)
140 if (*cp)
141 *cp++ = '\0';
142 if (cp - args == 0)
143 break;
144 sp = getservbyname(args,
145 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
146 if (sp) {
147 selectport(sp->s_port, onoff);
148 continue;
150 hp = gethostbyname(args);
151 if (hp == NULL) {
152 in.s_addr = inet_addr(args);
153 if ((int)in.s_addr == -1) {
154 error("%s: unknown host or port", args);
155 continue;
157 } else
158 in = *(struct in_addr *)hp->h_addr;
159 selecthost(&in, onoff);
163 static int
164 selectproto(char *proto)
167 if (proto == NULL || streq(proto, "all"))
168 protos = TCP | UDP;
169 else if (streq(proto, "tcp"))
170 protos = TCP;
171 else if (streq(proto, "udp"))
172 protos = UDP;
173 else
174 return (0);
176 return (protos);
179 static void
180 showprotos(void)
183 if ((protos&TCP) == 0)
184 addch('!');
185 addstr("tcp ");
186 if ((protos&UDP) == 0)
187 addch('!');
188 addstr("udp ");
191 static struct pitem {
192 long port;
193 int onoff;
194 } *ports;
196 static int
197 selectport(long port, int onoff)
199 struct pitem *p;
201 if (port == -1) {
202 if (ports == NULL)
203 return (0);
204 free((char *)ports), ports = NULL;
205 nports = 0;
206 return (1);
208 for (p = ports; p < ports+nports; p++)
209 if (p->port == port) {
210 p->onoff = onoff;
211 return (0);
213 if (nports == 0)
214 ports = (struct pitem *)malloc(sizeof (*p));
215 else
216 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
217 p = &ports[nports++];
218 p->port = port;
219 p->onoff = onoff;
220 return (1);
224 checkport(struct inpcb *inp)
226 struct pitem *p;
228 if (ports)
229 for (p = ports; p < ports+nports; p++) {
230 if (p->port == inp->inp_lport ||
231 p->port == inp->inp_fport)
232 return (p->onoff);
234 return (1);
237 static void
238 showports(void)
240 struct pitem *p;
241 struct servent *sp;
243 for (p = ports; p < ports+nports; p++) {
244 sp = getservbyport(p->port,
245 protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
246 if (!p->onoff)
247 addch('!');
248 if (sp)
249 printw("%s ", sp->s_name);
250 else
251 printw("%d ", p->port);
255 static int
256 selecthost(struct in_addr *in, int onoff)
258 struct hitem *p;
260 if (in == NULL) {
261 if (hosts == NULL)
262 return (0);
263 free((char *)hosts), hosts = NULL;
264 nhosts = 0;
265 return (1);
267 for (p = hosts; p < hosts+nhosts; p++)
268 if (p->addr.s_addr == in->s_addr) {
269 p->onoff = onoff;
270 return (0);
272 if (nhosts == 0)
273 hosts = (struct hitem *)malloc(sizeof (*p));
274 else
275 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
276 p = &hosts[nhosts++];
277 p->addr = *in;
278 p->onoff = onoff;
279 return (1);
283 checkhost(struct inpcb *inp)
285 struct hitem *p;
287 if (hosts)
288 for (p = hosts; p < hosts+nhosts; p++) {
289 if (p->addr.s_addr == inp->inp_laddr.s_addr ||
290 p->addr.s_addr == inp->inp_faddr.s_addr)
291 return (p->onoff);
293 return (1);
296 static void
297 showhosts(void)
299 struct hitem *p;
300 struct hostent *hp;
302 for (p = hosts; p < hosts+nhosts; p++) {
303 hp = gethostbyaddr(&p->addr, sizeof (p->addr), AF_INET);
304 if (!p->onoff)
305 addch('!');
306 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));