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