Remove some unnecessary casts.
[dragonfly.git] / usr.bin / systat / netcmds.c
... / ...
CommitLineData
1/*-
2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
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.
20 *
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.
32 *
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.7 2008/11/10 04:59:45 swildner Exp $
36 */
37
38/*
39 * Common network command support routines.
40 */
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>
46
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>
53
54#include <netdb.h>
55#include <stdlib.h>
56#include <string.h>
57#include <ctype.h>
58#include "systat.h"
59#include "extern.h"
60
61#define streq(a,b) (strcmp(a,b)==0)
62
63static struct hitem {
64 struct in_addr addr;
65 int onoff;
66} *hosts;
67
68int nports, nhosts, protos;
69
70static void changeitems(char *, int);
71static int selectproto(char *);
72static void showprotos(void);
73static int selectport(long, int);
74static void showports(void);
75static int selecthost(struct in_addr *, int);
76static void showhosts(void);
77
78int
79netcmd(const char *cmd, char *args)
80{
81
82 if (prefix(cmd, "proto")) {
83 if (*args == '\0') {
84 move(CMDLINE, 0);
85 clrtoeol();
86 addstr("which proto?");
87 } else if (!selectproto(args)) {
88 error("%s: Unknown protocol.", args);
89 }
90 return (1);
91 }
92 if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
93 changeitems(args, prefix(cmd, "display"));
94 return (1);
95 }
96 if (prefix(cmd, "reset")) {
97 selectproto(0);
98 selecthost(0, 0);
99 selectport(-1, 0);
100 return (1);
101 }
102 if (prefix(cmd, "show")) {
103 move(CMDLINE, 0); clrtoeol();
104 if (*args == '\0') {
105 showprotos();
106 showhosts();
107 showports();
108 return (1);
109 }
110 if (prefix(args, "protos"))
111 showprotos();
112 else if (prefix(args, "hosts"))
113 showhosts();
114 else if (prefix(args, "ports"))
115 showports();
116 else
117 addstr("show what?");
118 return (1);
119 }
120 return (0);
121}
122
123
124static void
125changeitems(char *args, int onoff)
126{
127 char *cp;
128 struct servent *sp;
129 struct hostent *hp;
130 struct in_addr in;
131
132 cp = strchr(args, '\n');
133 if (cp)
134 *cp = '\0';
135 for (;;args = cp) {
136 for (cp = args; *cp && isspace(*cp); cp++)
137 ;
138 args = cp;
139 for (; *cp && !isspace(*cp); cp++)
140 ;
141 if (*cp)
142 *cp++ = '\0';
143 if (cp - args == 0)
144 break;
145 sp = getservbyname(args,
146 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
147 if (sp) {
148 selectport(sp->s_port, onoff);
149 continue;
150 }
151 hp = gethostbyname(args);
152 if (hp == 0) {
153 in.s_addr = inet_addr(args);
154 if ((int)in.s_addr == -1) {
155 error("%s: unknown host or port", args);
156 continue;
157 }
158 } else
159 in = *(struct in_addr *)hp->h_addr;
160 selecthost(&in, onoff);
161 }
162}
163
164static int
165selectproto(char *proto)
166{
167
168 if (proto == 0 || streq(proto, "all"))
169 protos = TCP | UDP;
170 else if (streq(proto, "tcp"))
171 protos = TCP;
172 else if (streq(proto, "udp"))
173 protos = UDP;
174 else
175 return (0);
176
177 return (protos);
178}
179
180static void
181showprotos(void)
182{
183
184 if ((protos&TCP) == 0)
185 addch('!');
186 addstr("tcp ");
187 if ((protos&UDP) == 0)
188 addch('!');
189 addstr("udp ");
190}
191
192static struct pitem {
193 long port;
194 int onoff;
195} *ports;
196
197static int
198selectport(long port, int onoff)
199{
200 struct pitem *p;
201
202 if (port == -1) {
203 if (ports == 0)
204 return (0);
205 free((char *)ports), ports = 0;
206 nports = 0;
207 return (1);
208 }
209 for (p = ports; p < ports+nports; p++)
210 if (p->port == port) {
211 p->onoff = onoff;
212 return (0);
213 }
214 if (nports == 0)
215 ports = (struct pitem *)malloc(sizeof (*p));
216 else
217 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
218 p = &ports[nports++];
219 p->port = port;
220 p->onoff = onoff;
221 return (1);
222}
223
224int
225checkport(struct inpcb *inp)
226{
227 struct pitem *p;
228
229 if (ports)
230 for (p = ports; p < ports+nports; p++)
231 if (p->port == inp->inp_lport || p->port == inp->inp_fport)
232 return (p->onoff);
233 return (1);
234}
235
236static void
237showports(void)
238{
239 struct pitem *p;
240 struct servent *sp;
241
242 for (p = ports; p < ports+nports; p++) {
243 sp = getservbyport(p->port,
244 protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
245 if (!p->onoff)
246 addch('!');
247 if (sp)
248 printw("%s ", sp->s_name);
249 else
250 printw("%d ", p->port);
251 }
252}
253
254static int
255selecthost(struct in_addr *in, int onoff)
256{
257 struct hitem *p;
258
259 if (in == 0) {
260 if (hosts == 0)
261 return (0);
262 free((char *)hosts), hosts = 0;
263 nhosts = 0;
264 return (1);
265 }
266 for (p = hosts; p < hosts+nhosts; p++)
267 if (p->addr.s_addr == in->s_addr) {
268 p->onoff = onoff;
269 return (0);
270 }
271 if (nhosts == 0)
272 hosts = (struct hitem *)malloc(sizeof (*p));
273 else
274 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
275 p = &hosts[nhosts++];
276 p->addr = *in;
277 p->onoff = onoff;
278 return (1);
279}
280
281int
282checkhost(struct inpcb *inp)
283{
284 struct hitem *p;
285
286 if (hosts)
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)
290 return (p->onoff);
291 return (1);
292}
293
294static void
295showhosts(void)
296{
297 struct hitem *p;
298 struct hostent *hp;
299
300 for (p = hosts; p < hosts+nhosts; p++) {
301 hp = gethostbyaddr(&p->addr, sizeof (p->addr), AF_INET);
302 if (!p->onoff)
303 addch('!');
304 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
305 }
306}