xref: /netbsd-src/usr.bin/systat/netcmds.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: netcmds.c,v 1.22 2021/10/30 11:31:51 nia Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: netcmds.c,v 1.22 2021/10/30 11:31:51 nia Exp $");
38 #endif /* not lint */
39 
40 /*
41  * Common network command support routines.
42  */
43 #include <sys/param.h>
44 #include <sys/mbuf.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 #ifdef INET6
53 #include <netinet/ip6.h>
54 #include <netinet6/in6_pcb.h>
55 #endif
56 
57 #include <arpa/inet.h>
58 
59 #include <ctype.h>
60 #include <netdb.h>
61 #include <stdlib.h>
62 #include <string.h>
63 
64 #include "systat.h"
65 #include "extern.h"
66 
67 #define	streq(a,b)	(strcmp(a,b)==0)
68 
69 static	struct hitem {
70 	struct	sockaddr_storage addr;
71 	int	onoff;
72 } *hosts = NULL;
73 
74 int nports, nhosts, protos;
75 
76 static void changeitems(char *, int);
77 static void selectproto(const char *);
78 static void showprotos(void);
79 static int selectport(long, int);
80 static void showports(void);
81 static int addrcmp(struct sockaddr *, struct sockaddr *);
82 static int selecthost(struct sockaddr *, int);
83 static void showhosts(void);
84 
85 /* please note: there are also some netstat commands in netstat.c */
86 
87 void
88 netstat_display(char *args)
89 {
90 	changeitems(args, 1);
91 }
92 
93 void
94 netstat_ignore(char *args)
95 {
96 	changeitems(args, 0);
97 }
98 
99 void
100 netstat_reset(char *args)
101 {
102 	selectproto(0);
103 	selecthost(0, 0);
104 	selectport(-1, 0);
105 }
106 
107 void
108 netstat_show(char *args)
109 {
110 	move(CMDLINE, 0); clrtoeol();
111 	if (!args || *args == '\0') {
112 		showprotos();
113 		showhosts();
114 		showports();
115 		return;
116 	}
117 	if (strstr(args, "protos") == args)
118 		showprotos();
119 	else if (strstr(args, "hosts") == args)
120 		showhosts();
121 	else if (strstr(args, "ports") == args)
122 		showports();
123 	else
124 		addstr("show what?");
125 }
126 
127 void
128 netstat_tcp(char *args)
129 {
130 	selectproto("tcp");
131 }
132 
133 void
134 netstat_udp(char *args)
135 {
136 	selectproto("udp");
137 }
138 
139 static void
140 changeitems(char *args, int onoff)
141 {
142 	char *cp;
143 	struct servent *sp;
144 	struct addrinfo hints, *res, *res0;
145 
146 	cp = strchr(args, '\n');
147 	if (cp)
148 		*cp = '\0';
149 	for (;;args = cp) {
150 		for (cp = args; *cp && isspace((unsigned char)*cp); cp++)
151 			;
152 		args = cp;
153 		for (; *cp && !isspace((unsigned char)*cp); cp++)
154 			;
155 		if (*cp)
156 			*cp++ = '\0';
157 		if (cp - args == 0)
158 			break;
159 		sp = getservbyname(args,
160 		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
161 		if (sp) {
162 			selectport(sp->s_port, onoff);
163 			continue;
164 		}
165 
166 		memset(&hints, 0, sizeof(hints));
167 		hints.ai_family = PF_UNSPEC;
168 		hints.ai_socktype = SOCK_DGRAM;
169 		if (getaddrinfo(args, "0", &hints, &res0) != 0) {
170 			error("%s: unknown host or port", args);
171 			continue;
172 		}
173 		for (res = res0; res; res = res->ai_next)
174 			selecthost(res->ai_addr, onoff);
175 		freeaddrinfo(res0);
176 	}
177 }
178 
179 static void
180 selectproto(const char *proto)
181 {
182 
183 	if (proto == 0 || streq(proto, "all"))
184 		protos = TCP|UDP;
185 	else if (streq(proto, "tcp"))
186 		protos = TCP;
187 	else if (streq(proto, "udp"))
188 		protos = UDP;
189 }
190 
191 static void
192 showprotos(void)
193 {
194 
195 	if ((protos & TCP) == 0)
196 		addch('!');
197 	addstr("tcp ");
198 	if ((protos & UDP) == 0)
199 		addch('!');
200 	addstr("udp ");
201 }
202 
203 static	struct pitem {
204 	long	port;
205 	int	onoff;
206 } *ports = NULL;
207 
208 static int
209 selectport(long port, int onoff)
210 {
211 	struct pitem *p;
212 
213 	if (port == -1) {
214 		if (ports == NULL)
215 			return (0);
216 		free(ports);
217 		ports = NULL;
218 		nports = 0;
219 		return (1);
220 	}
221 	for (p = ports; p < ports+nports; p++)
222 		if (p->port == port) {
223 			p->onoff = onoff;
224 			return (0);
225 		}
226 	if (reallocarr(&ports, nports + 1, sizeof(*p)) != 0) {
227 		error("malloc failed");
228 		die(0);
229 	}
230 	p = &ports[nports++];
231 	p->port = port;
232 	p->onoff = onoff;
233 	return (1);
234 }
235 
236 int
237 checkport(struct inpcb *inp)
238 {
239 	struct pitem *p;
240 
241 	if (ports)
242 	for (p = ports; p < ports+nports; p++)
243 		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
244 			return (p->onoff);
245 	return (1);
246 }
247 
248 #ifdef INET6
249 int
250 checkport6(struct in6pcb *in6p)
251 {
252 	struct pitem *p;
253 
254 	if (ports)
255 	for (p = ports; p < ports+nports; p++)
256 		if (p->port == in6p->in6p_lport || p->port == in6p->in6p_fport)
257 			return (p->onoff);
258 	return (1);
259 }
260 #endif
261 
262 static void
263 showports(void)
264 {
265 	struct pitem *p;
266 	struct servent *sp;
267 
268 	for (p = ports; p < ports+nports; p++) {
269 		sp = getservbyport(p->port,
270 		    protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
271 		if (!p->onoff)
272 			addch('!');
273 		if (sp)
274 			printw("%s ", sp->s_name);
275 		else
276 			printw("%ld ", p->port);
277 	}
278 }
279 
280 static int
281 addrcmp(struct sockaddr *sa1, struct sockaddr *sa2)
282 {
283 	if (sa1->sa_family != sa2->sa_family)
284 		return 0;
285 	if (sa1->sa_len != sa2->sa_len)
286 		return 0;
287 	switch (sa1->sa_family) {
288 	case AF_INET:
289 		if (((struct sockaddr_in *)sa1)->sin_addr.s_addr ==
290 				((struct sockaddr_in *)sa2)->sin_addr.s_addr)
291 			return 1;
292 		break;
293 #ifdef INET6
294 	case AF_INET6:
295 		if (IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)sa1)->sin6_addr,
296 				&((struct sockaddr_in6 *)sa2)->sin6_addr))
297 			return 1;
298 		break;
299 #endif
300 	default:
301 		if (memcmp(sa1, sa2, sa1->sa_len) == 0)
302 			return 1;
303 		break;
304 	}
305 	return 0;
306 }
307 
308 static int
309 selecthost(struct sockaddr *sa, int onoff)
310 {
311 	struct hitem *p;
312 
313 	if (sa == 0) {
314 		if (hosts == 0)
315 			return (0);
316 		free((char *)hosts), hosts = 0;
317 		nhosts = 0;
318 		return (1);
319 	}
320 	for (p = hosts; p < hosts+nhosts; p++)
321 		if (addrcmp((struct sockaddr *)&p->addr, sa)) {
322 			p->onoff = onoff;
323 			return (0);
324 		}
325 	if (sa->sa_len > sizeof(struct sockaddr_storage))
326 		return (-1);	/*XXX*/
327 	if (reallocarr(&hosts, nhosts + 1, sizeof(*p)) != 0) {
328 		error("malloc failed");
329 		die(0);
330 	}
331 	p = &hosts[nhosts++];
332 	memcpy(&p->addr, sa, sa->sa_len);
333 	p->onoff = onoff;
334 	return (1);
335 }
336 
337 int
338 checkhost(struct inpcb *inp)
339 {
340 	struct hitem *p;
341 	struct sockaddr_in *s_in;
342 
343 	if (hosts)
344 		for (p = hosts; p < hosts+nhosts; p++) {
345 			if (((struct sockaddr *)&p->addr)->sa_family != AF_INET)
346 				continue;
347 			s_in = (struct sockaddr_in *)&p->addr;
348 			if (s_in->sin_addr.s_addr == inp->inp_laddr.s_addr ||
349 			    s_in->sin_addr.s_addr == inp->inp_faddr.s_addr)
350 				return (p->onoff);
351 		}
352 	return (1);
353 }
354 
355 #ifdef INET6
356 int
357 checkhost6(struct in6pcb *in6p)
358 {
359 	struct hitem *p;
360 	struct sockaddr_in6 *sin6;
361 
362 	if (hosts)
363 		for (p = hosts; p < hosts+nhosts; p++) {
364 			if (((struct sockaddr *)&p->addr)->sa_family != AF_INET6)
365 				continue;
366 			sin6 = (struct sockaddr_in6 *)&p->addr;
367 			if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &in6p->in6p_laddr) ||
368 			    IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &in6p->in6p_faddr))
369 				return (p->onoff);
370 		}
371 	return (1);
372 }
373 #endif
374 
375 static void
376 showhosts(void)
377 {
378 	struct hitem *p;
379 	char hbuf[NI_MAXHOST];
380 	struct sockaddr *sa;
381 	int flags;
382 
383 #if 0
384 	flags = nflag ? NI_NUMERICHOST : 0;
385 #else
386 	flags = 0;
387 #endif
388 	for (p = hosts; p < hosts+nhosts; p++) {
389 		sa = (struct sockaddr *)&p->addr;
390 		if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
391 				flags) != 0)
392 			strlcpy(hbuf, "(invalid)", sizeof(hbuf));
393 		if (!p->onoff)
394 			addch('!');
395 		printw("%s ", hbuf);
396 	}
397 }
398