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