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