Home | History | Annotate | Line # | Download | only in systat
netcmds.c revision 1.12
      1 /*	$NetBSD: netcmds.c,v 1.12 2000/01/04 15:12:42 itojun 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)netcmds.c	8.1 (Berkeley) 6/6/93";
     40 #endif
     41 __RCSID("$NetBSD: netcmds.c,v 1.12 2000/01/04 15:12:42 itojun Exp $");
     42 #endif /* not lint */
     43 
     44 /*
     45  * Common network command support routines.
     46  */
     47 #include <sys/param.h>
     48 #include <sys/socket.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/protosw.h>
     51 
     52 #include <net/route.h>
     53 #include <netinet/in.h>
     54 #include <netinet/in_systm.h>
     55 #include <netinet/ip.h>
     56 #include <netinet/in_pcb.h>
     57 
     58 #include <arpa/inet.h>
     59 
     60 #include <netdb.h>
     61 #include <stdlib.h>
     62 #include <string.h>
     63 #include <ctype.h>
     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	in_addr addr;
     71 	int	onoff;
     72 } *hosts = NULL;
     73 
     74 int nports, nhosts, protos;
     75 
     76 static void changeitems __P((char *, int));
     77 static void selectproto __P((char *));
     78 static void showprotos __P((void));
     79 static int selectport __P((long, int));
     80 static void showports __P((void));
     81 static int selecthost __P((struct in_addr *, int));
     82 static void showhosts __P((void));
     83 
     84 /* please note: there are also some netstat commands in netstat.c */
     85 
     86 void
     87 netstat_display (args)
     88 	char *args;
     89 {
     90 	changeitems(args, 1);
     91 }
     92 
     93 void
     94 netstat_ignore (args)
     95 	char *args;
     96 {
     97 	changeitems(args, 0);
     98 }
     99 
    100 void
    101 netstat_reset (args)
    102 	char *args;
    103 {
    104 	selectproto(0);
    105 	selecthost(0, 0);
    106 	selectport(-1, 0);
    107 }
    108 
    109 void
    110 netstat_show (args)
    111 	char *args;
    112 {
    113 	move(CMDLINE, 0); clrtoeol();
    114 	if (*args == '\0') {
    115 		showprotos();
    116 		showhosts();
    117 		showports();
    118 		return;
    119 	}
    120 	if (strstr(args, "protos") == args)
    121 		showprotos();
    122 	else if (strstr(args, "hosts") == args)
    123 		showhosts();
    124 	else if (strstr(args, "ports") == args)
    125 		showports();
    126 	else
    127 		addstr("show what?");
    128 }
    129 
    130 void
    131 netstat_tcp (args)
    132 	char *args;
    133 {
    134 	selectproto("tcp");
    135 }
    136 
    137 void
    138 netstat_udp (args)
    139 	char *args;
    140 {
    141 	selectproto("udp");
    142 }
    143 
    144 static void
    145 changeitems(args, onoff)
    146 	char *args;
    147 	int onoff;
    148 {
    149 	char *cp;
    150 	struct servent *sp;
    151 	struct hostent *hp;
    152 	struct in_addr in;
    153 
    154 	cp = strchr(args, '\n');
    155 	if (cp)
    156 		*cp = '\0';
    157 	for (;;args = cp) {
    158 		for (cp = args; *cp && isspace(*cp); cp++)
    159 			;
    160 		args = cp;
    161 		for (; *cp && !isspace(*cp); cp++)
    162 			;
    163 		if (*cp)
    164 			*cp++ = '\0';
    165 		if (cp - args == 0)
    166 			break;
    167 		sp = getservbyname(args,
    168 		    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
    169 		if (sp) {
    170 			selectport(sp->s_port, onoff);
    171 			continue;
    172 		}
    173 		if (inet_aton(args, &in) == 0) {
    174 			hp = gethostbyname(args);
    175 			if (hp == 0) {
    176 				error("%s: unknown host or port", args);
    177 				continue;
    178 			}
    179 			memcpy(&in, hp->h_addr, hp->h_length);
    180 		}
    181 		selecthost(&in, onoff);
    182 	}
    183 }
    184 
    185 static void
    186 selectproto(proto)
    187 	char *proto;
    188 {
    189 
    190 	if (proto == 0 || streq(proto, "all"))
    191 		protos = TCP|UDP;
    192 	else if (streq(proto, "tcp"))
    193 		protos = TCP;
    194 	else if (streq(proto, "udp"))
    195 		protos = UDP;
    196 }
    197 
    198 static void
    199 showprotos()
    200 {
    201 
    202 	if ((protos & TCP) == 0)
    203 		addch('!');
    204 	addstr("tcp ");
    205 	if ((protos & UDP) == 0)
    206 		addch('!');
    207 	addstr("udp ");
    208 }
    209 
    210 static	struct pitem {
    211 	long	port;
    212 	int	onoff;
    213 } *ports = NULL;
    214 
    215 static int
    216 selectport(port, onoff)
    217 	long port;
    218 	int onoff;
    219 {
    220 	struct pitem *p;
    221 
    222 	if (port == -1) {
    223 		if (ports == NULL)
    224 			return (0);
    225 		free(ports);
    226 		ports = NULL;
    227 		nports = 0;
    228 		return (1);
    229 	}
    230 	for (p = ports; p < ports+nports; p++)
    231 		if (p->port == port) {
    232 			p->onoff = onoff;
    233 			return (0);
    234 		}
    235 	p = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
    236 	if (p == NULL) {
    237 		error("malloc failed");
    238 		die(0);
    239 	}
    240 	ports = p;
    241 	p = &ports[nports++];
    242 	p->port = port;
    243 	p->onoff = onoff;
    244 	return (1);
    245 }
    246 
    247 int
    248 checkport(inp)
    249 	struct inpcb *inp;
    250 {
    251 	struct pitem *p;
    252 
    253 	if (ports)
    254 	for (p = ports; p < ports+nports; p++)
    255 		if (p->port == inp->inp_lport || p->port == inp->inp_fport)
    256 			return (p->onoff);
    257 	return (1);
    258 }
    259 
    260 static void
    261 showports()
    262 {
    263 	struct pitem *p;
    264 	struct servent *sp;
    265 
    266 	for (p = ports; p < ports+nports; p++) {
    267 		sp = getservbyport(p->port,
    268 		    protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
    269 		if (!p->onoff)
    270 			addch('!');
    271 		if (sp)
    272 			printw("%s ", sp->s_name);
    273 		else
    274 			printw("%d ", p->port);
    275 	}
    276 }
    277 
    278 static int
    279 selecthost(in, onoff)
    280 	struct in_addr *in;
    281 	int onoff;
    282 {
    283 	struct hitem *p;
    284 
    285 	if (in == 0) {
    286 		if (hosts == 0)
    287 			return (0);
    288 		free((char *)hosts), hosts = 0;
    289 		nhosts = 0;
    290 		return (1);
    291 	}
    292 	for (p = hosts; p < hosts+nhosts; p++)
    293 		if (p->addr.s_addr == in->s_addr) {
    294 			p->onoff = onoff;
    295 			return (0);
    296 		}
    297 	p = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
    298 	if (p == NULL) {
    299 		error("malloc failed");
    300 		die(0);
    301 	}
    302 	hosts = p;
    303 	p = &hosts[nhosts++];
    304 	p->addr = *in;
    305 	p->onoff = onoff;
    306 	return (1);
    307 }
    308 
    309 int
    310 checkhost(inp)
    311 	struct inpcb *inp;
    312 {
    313 	struct hitem *p;
    314 
    315 	if (hosts)
    316 		for (p = hosts; p < hosts+nhosts; p++)
    317 			if (p->addr.s_addr == inp->inp_laddr.s_addr ||
    318 			    p->addr.s_addr == inp->inp_faddr.s_addr)
    319 				return (p->onoff);
    320 	return (1);
    321 }
    322 
    323 static void
    324 showhosts()
    325 {
    326 	struct hitem *p;
    327 	struct hostent *hp;
    328 
    329 	for (p = hosts; p < hosts+nhosts; p++) {
    330 		hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
    331 		if (!p->onoff)
    332 			addch('!');
    333 		printw("%s ", hp ? hp->h_name : inet_ntoa(p->addr));
    334 	}
    335 }
    336