1994-05-27 12:33:43 +00: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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-12-12 00:13:37 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#ifdef lint
|
|
|
|
static const char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Common network command support routines.
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
1996-03-11 13:01:12 +00:00
|
|
|
#include <sys/queue.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.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>
|
1998-06-12 14:18:33 +00:00
|
|
|
#include <arpa/inet.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-12-12 00:13:37 +00:00
|
|
|
#include <ctype.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-12-12 00:13:37 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "systat.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
#define streq(a,b) (strcmp(a,b)==0)
|
|
|
|
|
|
|
|
static struct hitem {
|
|
|
|
struct in_addr addr;
|
|
|
|
int onoff;
|
|
|
|
} *hosts;
|
|
|
|
|
|
|
|
int nports, nhosts, protos;
|
|
|
|
|
2002-03-22 01:42:45 +00:00
|
|
|
static void changeitems(const char *, int);
|
|
|
|
static int selectproto(const char *);
|
|
|
|
static void showprotos(void);
|
|
|
|
static int selectport(long, int);
|
|
|
|
static void showports(void);
|
|
|
|
static int selecthost(struct in_addr *, int);
|
|
|
|
static void showhosts(void);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2008-01-16 19:27:43 +00:00
|
|
|
netcmd(const char *cmd, const char *args)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
1997-12-04 03:44:46 +00:00
|
|
|
if (prefix(cmd, "proto")) {
|
|
|
|
if (*args == '\0') {
|
|
|
|
move(CMDLINE, 0);
|
|
|
|
clrtoeol();
|
|
|
|
addstr("which proto?");
|
|
|
|
} else if (!selectproto(args)) {
|
|
|
|
error("%s: Unknown protocol.", args);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
|
|
|
|
changeitems(args, prefix(cmd, "display"));
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (prefix(cmd, "reset")) {
|
|
|
|
selectproto(0);
|
|
|
|
selecthost(0, 0);
|
|
|
|
selectport(-1, 0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (prefix(cmd, "show")) {
|
|
|
|
move(CMDLINE, 0); clrtoeol();
|
|
|
|
if (*args == '\0') {
|
|
|
|
showprotos();
|
|
|
|
showhosts();
|
|
|
|
showports();
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (prefix(args, "protos"))
|
|
|
|
showprotos();
|
|
|
|
else if (prefix(args, "hosts"))
|
|
|
|
showhosts();
|
|
|
|
else if (prefix(args, "ports"))
|
|
|
|
showports();
|
|
|
|
else
|
|
|
|
addstr("show what?");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-16 19:27:43 +00:00
|
|
|
changeitems(const char *args, int onoff)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
char *cp, *tmpstr, *tmpstr1;
|
1994-05-27 12:33:43 +00:00
|
|
|
struct servent *sp;
|
|
|
|
struct hostent *hp;
|
|
|
|
struct in_addr in;
|
|
|
|
|
2001-12-12 00:13:37 +00:00
|
|
|
tmpstr = tmpstr1 = strdup(args);
|
2012-01-03 18:51:58 +00:00
|
|
|
cp = strchr(tmpstr1, '\n');
|
1994-05-27 12:33:43 +00:00
|
|
|
if (cp)
|
|
|
|
*cp = '\0';
|
2001-12-12 00:13:37 +00:00
|
|
|
for (;;tmpstr1 = cp) {
|
|
|
|
for (cp = tmpstr1; *cp && isspace(*cp); cp++)
|
1994-05-27 12:33:43 +00:00
|
|
|
;
|
2001-12-12 00:13:37 +00:00
|
|
|
tmpstr1 = cp;
|
1994-05-27 12:33:43 +00:00
|
|
|
for (; *cp && !isspace(*cp); cp++)
|
|
|
|
;
|
|
|
|
if (*cp)
|
|
|
|
*cp++ = '\0';
|
2001-12-12 00:13:37 +00:00
|
|
|
if (cp - tmpstr1 == 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2001-12-12 00:13:37 +00:00
|
|
|
sp = getservbyname(tmpstr1,
|
1994-05-27 12:33:43 +00:00
|
|
|
protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
|
|
|
|
if (sp) {
|
|
|
|
selectport(sp->s_port, onoff);
|
|
|
|
continue;
|
|
|
|
}
|
2001-12-12 00:13:37 +00:00
|
|
|
hp = gethostbyname(tmpstr1);
|
2016-04-20 01:26:03 +00:00
|
|
|
if (hp == NULL) {
|
2001-12-12 00:13:37 +00:00
|
|
|
in.s_addr = inet_addr(tmpstr1);
|
2013-10-15 07:37:30 +00:00
|
|
|
if (in.s_addr == INADDR_NONE) {
|
2001-12-12 00:13:37 +00:00
|
|
|
error("%s: unknown host or port", tmpstr1);
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
in = *(struct in_addr *)hp->h_addr;
|
|
|
|
selecthost(&in, onoff);
|
|
|
|
}
|
2001-12-12 00:13:37 +00:00
|
|
|
free(tmpstr);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-01-16 19:27:43 +00:00
|
|
|
selectproto(const char *proto)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
2016-04-20 01:26:03 +00:00
|
|
|
if (proto == NULL || streq(proto, "all"))
|
1997-12-04 03:44:46 +00:00
|
|
|
protos = TCP | UDP;
|
1994-05-27 12:33:43 +00:00
|
|
|
else if (streq(proto, "tcp"))
|
1997-12-04 03:44:46 +00:00
|
|
|
protos = TCP;
|
1994-05-27 12:33:43 +00:00
|
|
|
else if (streq(proto, "udp"))
|
1997-12-04 03:44:46 +00:00
|
|
|
protos = UDP;
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (protos);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-16 19:27:43 +00:00
|
|
|
showprotos(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if ((protos&TCP) == 0)
|
|
|
|
addch('!');
|
|
|
|
addstr("tcp ");
|
|
|
|
if ((protos&UDP) == 0)
|
|
|
|
addch('!');
|
|
|
|
addstr("udp ");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pitem {
|
|
|
|
long port;
|
|
|
|
int onoff;
|
|
|
|
} *ports;
|
|
|
|
|
|
|
|
static int
|
2008-01-16 19:27:43 +00:00
|
|
|
selectport(long port, int onoff)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2008-01-16 19:27:43 +00:00
|
|
|
struct pitem *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (port == -1) {
|
2016-04-20 01:26:03 +00:00
|
|
|
if (ports == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
return (0);
|
|
|
|
free((char *)ports), ports = 0;
|
|
|
|
nports = 0;
|
|
|
|
return (1);
|
|
|
|
}
|
2016-04-20 01:26:03 +00:00
|
|
|
for (p = ports; p < ports + nports; p++)
|
1994-05-27 12:33:43 +00:00
|
|
|
if (p->port == port) {
|
|
|
|
p->onoff = onoff;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (nports == 0)
|
|
|
|
ports = (struct pitem *)malloc(sizeof (*p));
|
|
|
|
else
|
|
|
|
ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
|
|
|
|
p = &ports[nports++];
|
|
|
|
p->port = port;
|
|
|
|
p->onoff = onoff;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
Hide struct inpcb, struct tcpcb from the userland.
This is a painful change, but it is needed. On the one hand, we avoid
modifying them, and this slows down some ideas, on the other hand we still
eventually modify them and tools like netstat(1) never work on next version of
FreeBSD. We maintain a ton of spares in them, and we already got some ifdef
hell at the end of tcpcb.
Details:
- Hide struct inpcb, struct tcpcb under _KERNEL || _WANT_FOO.
- Make struct xinpcb, struct xtcpcb pure API structures, not including
kernel structures inpcb and tcpcb inside. Export into these structures
the fields from inpcb and tcpcb that are known to be used, and put there
a ton of spare space.
- Make kernel and userland utilities compilable after these changes.
- Bump __FreeBSD_version.
Reviewed by: rrs, gnn
Differential Revision: D10018
2017-03-21 06:39:49 +00:00
|
|
|
checkport(struct in_conninfo *inc)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2008-01-16 19:27:43 +00:00
|
|
|
struct pitem *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (ports)
|
|
|
|
for (p = ports; p < ports+nports; p++)
|
Hide struct inpcb, struct tcpcb from the userland.
This is a painful change, but it is needed. On the one hand, we avoid
modifying them, and this slows down some ideas, on the other hand we still
eventually modify them and tools like netstat(1) never work on next version of
FreeBSD. We maintain a ton of spares in them, and we already got some ifdef
hell at the end of tcpcb.
Details:
- Hide struct inpcb, struct tcpcb under _KERNEL || _WANT_FOO.
- Make struct xinpcb, struct xtcpcb pure API structures, not including
kernel structures inpcb and tcpcb inside. Export into these structures
the fields from inpcb and tcpcb that are known to be used, and put there
a ton of spare space.
- Make kernel and userland utilities compilable after these changes.
- Bump __FreeBSD_version.
Reviewed by: rrs, gnn
Differential Revision: D10018
2017-03-21 06:39:49 +00:00
|
|
|
if (p->port == inc->inc_lport || p->port == inc->inc_fport)
|
1994-05-27 12:33:43 +00:00
|
|
|
return (p->onoff);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-16 19:27:43 +00:00
|
|
|
showports(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2008-01-16 19:27:43 +00:00
|
|
|
struct pitem *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
struct servent *sp;
|
|
|
|
|
|
|
|
for (p = ports; p < ports+nports; p++) {
|
|
|
|
sp = getservbyport(p->port,
|
2001-03-23 03:58:25 +00:00
|
|
|
protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!p->onoff)
|
|
|
|
addch('!');
|
|
|
|
if (sp)
|
|
|
|
printw("%s ", sp->s_name);
|
|
|
|
else
|
|
|
|
printw("%d ", p->port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-01-16 19:27:43 +00:00
|
|
|
selecthost(struct in_addr *in, int onoff)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2008-01-16 19:27:43 +00:00
|
|
|
struct hitem *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2016-04-20 01:26:03 +00:00
|
|
|
if (in == NULL) {
|
|
|
|
if (hosts == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
return (0);
|
|
|
|
free((char *)hosts), hosts = 0;
|
|
|
|
nhosts = 0;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
for (p = hosts; p < hosts+nhosts; p++)
|
|
|
|
if (p->addr.s_addr == in->s_addr) {
|
|
|
|
p->onoff = onoff;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (nhosts == 0)
|
|
|
|
hosts = (struct hitem *)malloc(sizeof (*p));
|
|
|
|
else
|
|
|
|
hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
|
|
|
|
p = &hosts[nhosts++];
|
|
|
|
p->addr = *in;
|
|
|
|
p->onoff = onoff;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
Hide struct inpcb, struct tcpcb from the userland.
This is a painful change, but it is needed. On the one hand, we avoid
modifying them, and this slows down some ideas, on the other hand we still
eventually modify them and tools like netstat(1) never work on next version of
FreeBSD. We maintain a ton of spares in them, and we already got some ifdef
hell at the end of tcpcb.
Details:
- Hide struct inpcb, struct tcpcb under _KERNEL || _WANT_FOO.
- Make struct xinpcb, struct xtcpcb pure API structures, not including
kernel structures inpcb and tcpcb inside. Export into these structures
the fields from inpcb and tcpcb that are known to be used, and put there
a ton of spare space.
- Make kernel and userland utilities compilable after these changes.
- Bump __FreeBSD_version.
Reviewed by: rrs, gnn
Differential Revision: D10018
2017-03-21 06:39:49 +00:00
|
|
|
checkhost(struct in_conninfo *inc)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2008-01-16 19:27:43 +00:00
|
|
|
struct hitem *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (hosts)
|
|
|
|
for (p = hosts; p < hosts+nhosts; p++)
|
Hide struct inpcb, struct tcpcb from the userland.
This is a painful change, but it is needed. On the one hand, we avoid
modifying them, and this slows down some ideas, on the other hand we still
eventually modify them and tools like netstat(1) never work on next version of
FreeBSD. We maintain a ton of spares in them, and we already got some ifdef
hell at the end of tcpcb.
Details:
- Hide struct inpcb, struct tcpcb under _KERNEL || _WANT_FOO.
- Make struct xinpcb, struct xtcpcb pure API structures, not including
kernel structures inpcb and tcpcb inside. Export into these structures
the fields from inpcb and tcpcb that are known to be used, and put there
a ton of spare space.
- Make kernel and userland utilities compilable after these changes.
- Bump __FreeBSD_version.
Reviewed by: rrs, gnn
Differential Revision: D10018
2017-03-21 06:39:49 +00:00
|
|
|
if (p->addr.s_addr == inc->inc_laddr.s_addr ||
|
|
|
|
p->addr.s_addr == inc->inc_faddr.s_addr)
|
1994-05-27 12:33:43 +00:00
|
|
|
return (p->onoff);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-16 19:27:43 +00:00
|
|
|
showhosts(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2008-01-16 19:27:43 +00:00
|
|
|
struct hitem *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
struct hostent *hp;
|
|
|
|
|
|
|
|
for (p = hosts; p < hosts+nhosts; p++) {
|
|
|
|
hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
|
|
|
|
if (!p->onoff)
|
|
|
|
addch('!');
|
|
|
|
printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr));
|
|
|
|
}
|
|
|
|
}
|