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