2000-07-05 15:03:20 +04:00
|
|
|
/* $NetBSD: netcmds.c,v 1.16 2000/07/05 11:03:22 ad Exp $ */
|
1995-01-20 11:51:48 +03:00
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-21 11:04:56 +04:00
|
|
|
#include <sys/cdefs.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
#ifndef lint
|
1995-01-20 11:51:48 +03:00
|
|
|
#if 0
|
1995-01-20 11:30:50 +03:00
|
|
|
static char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93";
|
1995-01-20 11:51:48 +03:00
|
|
|
#endif
|
2000-07-05 15:03:20 +04:00
|
|
|
__RCSID("$NetBSD: netcmds.c,v 1.16 2000/07/05 11:03:22 ad Exp $");
|
1995-01-20 11:30:50 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common network command support routines.
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/in_pcb.h>
|
2000-01-05 14:59:12 +03:00
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet6/in6_pcb.h>
|
|
|
|
#endif
|
1995-01-20 11:30:50 +03:00
|
|
|
|
1995-04-29 09:54:42 +04:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "systat.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
#define streq(a,b) (strcmp(a,b)==0)
|
|
|
|
|
|
|
|
static struct hitem {
|
2000-01-05 14:59:12 +03:00
|
|
|
struct sockaddr_storage addr;
|
1995-01-20 11:30:50 +03:00
|
|
|
int onoff;
|
2000-01-04 18:12:42 +03:00
|
|
|
} *hosts = NULL;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
int nports, nhosts, protos;
|
|
|
|
|
2000-07-05 15:03:20 +04:00
|
|
|
static void changeitems(char *, int);
|
|
|
|
static void selectproto(char *);
|
|
|
|
static void showprotos(void);
|
|
|
|
static int selectport(long, int);
|
|
|
|
static void showports(void);
|
|
|
|
static int addrcmp(struct sockaddr *, struct sockaddr *);
|
|
|
|
static int selecthost(struct sockaddr *, int);
|
|
|
|
static void showhosts(void);
|
1995-01-20 11:30:50 +03:00
|
|
|
|
1999-12-20 06:45:01 +03:00
|
|
|
/* please note: there are also some netstat commands in netstat.c */
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
netstat_display(char *args)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1999-12-20 06:45:01 +03:00
|
|
|
changeitems(args, 1);
|
|
|
|
}
|
1995-01-20 11:30:50 +03:00
|
|
|
|
1999-12-20 06:45:01 +03:00
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
netstat_ignore(char *args)
|
1999-12-20 06:45:01 +03:00
|
|
|
{
|
|
|
|
changeitems(args, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
netstat_reset(char *args)
|
1999-12-20 06:45:01 +03:00
|
|
|
{
|
|
|
|
selectproto(0);
|
|
|
|
selecthost(0, 0);
|
|
|
|
selectport(-1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
netstat_show(char *args)
|
1999-12-20 06:45:01 +03:00
|
|
|
{
|
|
|
|
move(CMDLINE, 0); clrtoeol();
|
2000-01-05 14:48:21 +03:00
|
|
|
if (!args || *args == '\0') {
|
1999-12-20 06:45:01 +03:00
|
|
|
showprotos();
|
|
|
|
showhosts();
|
|
|
|
showports();
|
|
|
|
return;
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
1999-12-20 07:06:25 +03:00
|
|
|
if (strstr(args, "protos") == args)
|
1999-12-20 06:45:01 +03:00
|
|
|
showprotos();
|
1999-12-20 07:06:25 +03:00
|
|
|
else if (strstr(args, "hosts") == args)
|
1999-12-20 06:45:01 +03:00
|
|
|
showhosts();
|
1999-12-20 07:06:25 +03:00
|
|
|
else if (strstr(args, "ports") == args)
|
1999-12-20 06:45:01 +03:00
|
|
|
showports();
|
|
|
|
else
|
|
|
|
addstr("show what?");
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
|
1999-12-20 06:45:01 +03:00
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
netstat_tcp(char *args)
|
1999-12-20 06:45:01 +03:00
|
|
|
{
|
|
|
|
selectproto("tcp");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
netstat_udp(char *args)
|
1999-12-20 06:45:01 +03:00
|
|
|
{
|
|
|
|
selectproto("udp");
|
|
|
|
}
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
static void
|
2000-07-05 15:03:20 +04:00
|
|
|
changeitems(char *args, int onoff)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-10-20 03:36:20 +04:00
|
|
|
char *cp;
|
1995-01-20 11:30:50 +03:00
|
|
|
struct servent *sp;
|
2000-01-05 14:59:12 +03:00
|
|
|
struct addrinfo hints, *res, *res0;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
1997-10-20 03:36:20 +04:00
|
|
|
cp = strchr(args, '\n');
|
1995-01-20 11:30:50 +03:00
|
|
|
if (cp)
|
|
|
|
*cp = '\0';
|
|
|
|
for (;;args = cp) {
|
|
|
|
for (cp = args; *cp && isspace(*cp); cp++)
|
|
|
|
;
|
|
|
|
args = cp;
|
|
|
|
for (; *cp && !isspace(*cp); cp++)
|
|
|
|
;
|
|
|
|
if (*cp)
|
|
|
|
*cp++ = '\0';
|
|
|
|
if (cp - args == 0)
|
|
|
|
break;
|
|
|
|
sp = getservbyname(args,
|
|
|
|
protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
|
|
|
|
if (sp) {
|
|
|
|
selectport(sp->s_port, onoff);
|
|
|
|
continue;
|
|
|
|
}
|
2000-01-05 14:59:12 +03:00
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
if (getaddrinfo(args, "0", &hints, &res0) != 0) {
|
|
|
|
error("%s: unknown host or port", args);
|
|
|
|
continue;
|
1995-05-21 21:14:38 +04:00
|
|
|
}
|
2000-01-05 14:59:12 +03:00
|
|
|
for (res = res0; res; res = res->ai_next)
|
|
|
|
selecthost(res->ai_addr, onoff);
|
|
|
|
freeaddrinfo(res0);
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-07-21 11:04:56 +04:00
|
|
|
static void
|
2000-07-05 15:03:20 +04:00
|
|
|
selectproto(char *proto)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
if (proto == 0 || streq(proto, "all"))
|
1997-07-21 11:04:56 +04:00
|
|
|
protos = TCP|UDP;
|
1995-01-20 11:30:50 +03:00
|
|
|
else if (streq(proto, "tcp"))
|
1997-07-21 11:04:56 +04:00
|
|
|
protos = TCP;
|
1995-01-20 11:30:50 +03:00
|
|
|
else if (streq(proto, "udp"))
|
1997-07-21 11:04:56 +04:00
|
|
|
protos = UDP;
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-05 15:03:20 +04:00
|
|
|
showprotos(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
|
|
|
|
1998-07-12 09:59:00 +04:00
|
|
|
if ((protos & TCP) == 0)
|
1995-01-20 11:30:50 +03:00
|
|
|
addch('!');
|
|
|
|
addstr("tcp ");
|
1998-07-12 09:59:00 +04:00
|
|
|
if ((protos & UDP) == 0)
|
1995-01-20 11:30:50 +03:00
|
|
|
addch('!');
|
|
|
|
addstr("udp ");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pitem {
|
|
|
|
long port;
|
|
|
|
int onoff;
|
1999-12-31 15:58:12 +03:00
|
|
|
} *ports = NULL;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
static int
|
2000-07-05 15:03:20 +04:00
|
|
|
selectport(long port, int onoff)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-10-20 03:36:20 +04:00
|
|
|
struct pitem *p;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
if (port == -1) {
|
1999-12-31 15:58:12 +03:00
|
|
|
if (ports == NULL)
|
1995-01-20 11:30:50 +03:00
|
|
|
return (0);
|
1999-12-31 15:58:12 +03:00
|
|
|
free(ports);
|
|
|
|
ports = NULL;
|
1995-01-20 11:30:50 +03:00
|
|
|
nports = 0;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
for (p = ports; p < ports+nports; p++)
|
|
|
|
if (p->port == port) {
|
|
|
|
p->onoff = onoff;
|
|
|
|
return (0);
|
|
|
|
}
|
1999-12-31 15:58:12 +03:00
|
|
|
p = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
|
|
|
|
if (p == NULL) {
|
|
|
|
error("malloc failed");
|
|
|
|
die(0);
|
|
|
|
}
|
|
|
|
ports = p;
|
1995-01-20 11:30:50 +03:00
|
|
|
p = &ports[nports++];
|
|
|
|
p->port = port;
|
|
|
|
p->onoff = onoff;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-05 15:03:20 +04:00
|
|
|
checkport(struct inpcb *inp)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-10-20 03:36:20 +04:00
|
|
|
struct pitem *p;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
if (ports)
|
|
|
|
for (p = ports; p < ports+nports; p++)
|
|
|
|
if (p->port == inp->inp_lport || p->port == inp->inp_fport)
|
|
|
|
return (p->onoff);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2000-01-05 14:59:12 +03:00
|
|
|
#ifdef INET6
|
|
|
|
int
|
2000-07-05 15:03:20 +04:00
|
|
|
checkport6(struct in6pcb *in6p)
|
2000-01-05 14:59:12 +03:00
|
|
|
{
|
|
|
|
struct pitem *p;
|
|
|
|
|
|
|
|
if (ports)
|
|
|
|
for (p = ports; p < ports+nports; p++)
|
|
|
|
if (p->port == in6p->in6p_lport || p->port == in6p->in6p_fport)
|
|
|
|
return (p->onoff);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
static void
|
2000-07-05 15:03:20 +04:00
|
|
|
showports(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-10-20 03:36:20 +04:00
|
|
|
struct pitem *p;
|
1995-01-20 11:30:50 +03:00
|
|
|
struct servent *sp;
|
|
|
|
|
|
|
|
for (p = ports; p < ports+nports; p++) {
|
|
|
|
sp = getservbyport(p->port,
|
1997-07-21 11:04:56 +04:00
|
|
|
protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
|
1995-01-20 11:30:50 +03:00
|
|
|
if (!p->onoff)
|
|
|
|
addch('!');
|
|
|
|
if (sp)
|
|
|
|
printw("%s ", sp->s_name);
|
|
|
|
else
|
2000-04-27 04:30:51 +04:00
|
|
|
printw("%ld ", p->port);
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-05 15:03:20 +04:00
|
|
|
addrcmp(struct sockaddr *sa1, struct sockaddr *sa2)
|
2000-01-05 14:59:12 +03:00
|
|
|
{
|
|
|
|
if (sa1->sa_family != sa2->sa_family)
|
|
|
|
return 0;
|
|
|
|
if (sa1->sa_len != sa2->sa_len)
|
|
|
|
return 0;
|
|
|
|
switch (sa1->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (((struct sockaddr_in *)sa1)->sin_addr.s_addr ==
|
|
|
|
((struct sockaddr_in *)sa2)->sin_addr.s_addr)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)sa1)->sin6_addr,
|
|
|
|
&((struct sockaddr_in6 *)sa2)->sin6_addr))
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
if (memcmp(sa1, sa2, sa1->sa_len) == 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-05 15:03:20 +04:00
|
|
|
selecthost(struct sockaddr *sa, int onoff)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-10-20 03:36:20 +04:00
|
|
|
struct hitem *p;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
2000-01-05 14:59:12 +03:00
|
|
|
if (sa == 0) {
|
1995-01-20 11:30:50 +03:00
|
|
|
if (hosts == 0)
|
|
|
|
return (0);
|
|
|
|
free((char *)hosts), hosts = 0;
|
|
|
|
nhosts = 0;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
for (p = hosts; p < hosts+nhosts; p++)
|
2000-01-05 14:59:12 +03:00
|
|
|
if (addrcmp((struct sockaddr *)&p->addr, sa)) {
|
1995-01-20 11:30:50 +03:00
|
|
|
p->onoff = onoff;
|
|
|
|
return (0);
|
|
|
|
}
|
2000-01-05 14:59:12 +03:00
|
|
|
if (sa->sa_len > sizeof(struct sockaddr_storage))
|
|
|
|
return (-1); /*XXX*/
|
2000-01-04 18:12:42 +03:00
|
|
|
p = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
|
|
|
|
if (p == NULL) {
|
|
|
|
error("malloc failed");
|
|
|
|
die(0);
|
|
|
|
}
|
|
|
|
hosts = p;
|
1995-01-20 11:30:50 +03:00
|
|
|
p = &hosts[nhosts++];
|
2000-01-05 14:59:12 +03:00
|
|
|
memcpy(&p->addr, sa, sa->sa_len);
|
1995-01-20 11:30:50 +03:00
|
|
|
p->onoff = onoff;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-05 15:03:20 +04:00
|
|
|
checkhost(struct inpcb *inp)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-10-20 03:36:20 +04:00
|
|
|
struct hitem *p;
|
2000-01-05 14:59:12 +03:00
|
|
|
struct sockaddr_in *sin;
|
|
|
|
|
|
|
|
if (hosts)
|
|
|
|
for (p = hosts; p < hosts+nhosts; p++) {
|
|
|
|
if (((struct sockaddr *)&p->addr)->sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
sin = (struct sockaddr_in *)&p->addr;
|
|
|
|
if (sin->sin_addr.s_addr == inp->inp_laddr.s_addr ||
|
|
|
|
sin->sin_addr.s_addr == inp->inp_faddr.s_addr)
|
|
|
|
return (p->onoff);
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
int
|
2000-07-05 15:03:20 +04:00
|
|
|
checkhost6(struct in6pcb *in6p)
|
2000-01-05 14:59:12 +03:00
|
|
|
{
|
|
|
|
struct hitem *p;
|
|
|
|
struct sockaddr_in6 *sin6;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
if (hosts)
|
2000-01-05 14:59:12 +03:00
|
|
|
for (p = hosts; p < hosts+nhosts; p++) {
|
|
|
|
if (((struct sockaddr *)&p->addr)->sa_family != AF_INET6)
|
|
|
|
continue;
|
|
|
|
sin6 = (struct sockaddr_in6 *)&p->addr;
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &in6p->in6p_laddr) ||
|
|
|
|
IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &in6p->in6p_faddr))
|
1998-07-12 09:59:00 +04:00
|
|
|
return (p->onoff);
|
2000-01-05 14:59:12 +03:00
|
|
|
}
|
1995-01-20 11:30:50 +03:00
|
|
|
return (1);
|
|
|
|
}
|
2000-01-05 14:59:12 +03:00
|
|
|
#endif
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
static void
|
2000-07-05 15:03:20 +04:00
|
|
|
showhosts(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-10-20 03:36:20 +04:00
|
|
|
struct hitem *p;
|
2000-01-05 14:59:12 +03:00
|
|
|
char hbuf[NI_MAXHOST];
|
|
|
|
struct sockaddr *sa;
|
|
|
|
int flags;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
2000-01-05 14:59:12 +03:00
|
|
|
#if 0
|
|
|
|
flags = nflag ? NI_NUMERICHOST : 0;
|
|
|
|
#else
|
|
|
|
flags = 0;
|
|
|
|
#endif
|
1995-01-20 11:30:50 +03:00
|
|
|
for (p = hosts; p < hosts+nhosts; p++) {
|
2000-01-05 14:59:12 +03:00
|
|
|
sa = (struct sockaddr *)&p->addr;
|
|
|
|
if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
|
|
|
|
flags) != 0)
|
|
|
|
strcpy(hbuf, "(invalid)");
|
1995-01-20 11:30:50 +03:00
|
|
|
if (!p->onoff)
|
|
|
|
addch('!');
|
2000-01-05 14:59:12 +03:00
|
|
|
printw("%s ", hbuf);
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
}
|