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[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* netstat
|
|
|
|
*/
|
|
|
|
#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 <netinet/in.h>
|
1998-06-12 14:18:33 +00:00
|
|
|
#include <arpa/inet.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
2003-08-01 20:28:20 +00:00
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#endif
|
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
|
|
|
#define _WANT_INPCB
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#include <netinet/icmp_var.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/tcpip.h>
|
|
|
|
#include <netinet/tcp_seq.h>
|
|
|
|
#define TCPSTATES
|
|
|
|
#include <netinet/tcp_fsm.h>
|
|
|
|
#include <netinet/tcp_timer.h>
|
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
|
|
|
#define _WANT_TCPCB
|
2016-01-27 00:45:46 +00:00
|
|
|
#include <netinet/tcp_var.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netinet/tcp_debug.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/udp_var.h>
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <nlist.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <paths.h>
|
2001-12-12 00:13:37 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "systat.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
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
|
|
|
static struct netinfo *enter(struct in_conninfo *, uint8_t, int, const char *);
|
2002-03-22 01:42:45 +00:00
|
|
|
static void enter_kvm(struct inpcb *, struct socket *, int, const char *);
|
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
|
|
|
static void enter_sysctl(struct xinpcb *, struct xsocket *, int, const char *);
|
2002-03-22 01:42:45 +00:00
|
|
|
static void fetchnetstat_kvm(void);
|
|
|
|
static void fetchnetstat_sysctl(void);
|
2005-02-19 15:10:19 +00:00
|
|
|
static char *inetname(struct sockaddr *);
|
|
|
|
static void inetprint(struct sockaddr *, const char *);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#define streq(a,b) (strcmp(a,b)==0)
|
2015-09-10 22:07:52 +00:00
|
|
|
#define YMAX(w) (getmaxy(w)-2)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
WINDOW *
|
2008-01-16 19:27:43 +00:00
|
|
|
opennetstat(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
sethostent(1);
|
|
|
|
setnetent(1);
|
2006-04-30 04:26:46 +00:00
|
|
|
return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct netinfo {
|
2000-12-30 21:17:03 +00:00
|
|
|
TAILQ_ENTRY(netinfo) chain;
|
1994-05-27 12:33:43 +00:00
|
|
|
short ni_line; /* line on screen */
|
|
|
|
short ni_seen; /* 0 when not present in list */
|
|
|
|
short ni_flags;
|
|
|
|
#define NIF_LACHG 0x1 /* local address changed */
|
|
|
|
#define NIF_FACHG 0x2 /* foreign address changed */
|
|
|
|
short ni_state; /* tcp state */
|
2001-12-12 00:13:37 +00:00
|
|
|
const char *ni_proto; /* protocol */
|
2005-02-19 15:10:19 +00:00
|
|
|
struct sockaddr_storage ni_lsa; /* local address */
|
|
|
|
struct sockaddr_storage ni_fsa; /* foreign address */
|
2002-07-24 03:02:43 +00:00
|
|
|
u_int ni_rcvcc; /* rcv buffer character count */
|
|
|
|
u_int ni_sndcc; /* snd buffer character count */
|
1994-05-27 12:33:43 +00:00
|
|
|
};
|
|
|
|
|
2000-12-30 21:17:03 +00:00
|
|
|
TAILQ_HEAD(netinfohead, netinfo) netcb = TAILQ_HEAD_INITIALIZER(netcb);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
static int aflag = 0;
|
|
|
|
static int nflag = 0;
|
|
|
|
static int lastrow = 1;
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
closenetstat(WINDOW *w)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
struct netinfo *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
endhostent();
|
|
|
|
endnetent();
|
2000-12-30 21:56:18 +00:00
|
|
|
TAILQ_FOREACH(p, &netcb, chain) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (p->ni_line != -1)
|
|
|
|
lastrow--;
|
|
|
|
p->ni_line = -1;
|
|
|
|
}
|
2011-10-15 13:20:36 +00:00
|
|
|
if (w != NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
wclear(w);
|
|
|
|
wrefresh(w);
|
|
|
|
delwin(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-12 00:13:37 +00:00
|
|
|
static const char *miblist[] = {
|
2001-03-23 03:58:25 +00:00
|
|
|
"net.inet.tcp.pcblist",
|
2006-04-30 04:47:23 +00:00
|
|
|
"net.inet.udp.pcblist"
|
2001-03-23 03:58:25 +00:00
|
|
|
};
|
|
|
|
|
2006-11-27 17:24:36 +00:00
|
|
|
static char tcb[] = "tcb", udb[] = "udb";
|
|
|
|
|
2001-03-23 03:58:25 +00:00
|
|
|
struct nlist namelist[] = {
|
1994-05-27 12:33:43 +00:00
|
|
|
#define X_TCB 0
|
2006-11-27 17:24:36 +00:00
|
|
|
{ .n_name = tcb },
|
1994-05-27 12:33:43 +00:00
|
|
|
#define X_UDB 1
|
2006-11-27 17:24:36 +00:00
|
|
|
{ .n_name = udb },
|
|
|
|
{ .n_name = NULL },
|
1994-05-27 12:33:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2008-01-16 19:27:43 +00:00
|
|
|
initnetstat(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
protos = TCP|UDP;
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
fetchnetstat(void)
|
2001-03-23 03:58:25 +00:00
|
|
|
{
|
|
|
|
if (use_kvm)
|
|
|
|
fetchnetstat_kvm();
|
|
|
|
else
|
|
|
|
fetchnetstat_sysctl();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-16 19:27:43 +00:00
|
|
|
fetchnetstat_kvm(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
struct inpcb *next;
|
|
|
|
struct netinfo *p;
|
1995-04-09 15:21:08 +00:00
|
|
|
struct inpcbhead head;
|
1994-05-27 12:33:43 +00:00
|
|
|
struct inpcb inpcb;
|
|
|
|
struct socket sockb;
|
|
|
|
struct tcpcb tcpcb;
|
|
|
|
void *off;
|
|
|
|
int istcp;
|
|
|
|
|
|
|
|
if (namelist[X_TCB].n_value == 0)
|
|
|
|
return;
|
2000-12-30 21:17:03 +00:00
|
|
|
TAILQ_FOREACH(p, &netcb, chain)
|
1994-05-27 12:33:43 +00:00
|
|
|
p->ni_seen = 0;
|
|
|
|
if (protos&TCP) {
|
1995-05-30 06:41:30 +00:00
|
|
|
off = NPTR(X_TCB);
|
1994-05-27 12:33:43 +00:00
|
|
|
istcp = 1;
|
|
|
|
}
|
|
|
|
else if (protos&UDP) {
|
1995-05-30 06:41:30 +00:00
|
|
|
off = NPTR(X_UDB);
|
1994-05-27 12:33:43 +00:00
|
|
|
istcp = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error("No protocols to display");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
again:
|
1995-04-09 15:21:08 +00:00
|
|
|
KREAD(off, &head, sizeof (struct inpcbhead));
|
2000-12-30 21:17:03 +00:00
|
|
|
LIST_FOREACH(next, &head, inp_list) {
|
1994-05-27 12:33:43 +00:00
|
|
|
KREAD(next, &inpcb, sizeof (inpcb));
|
2000-12-30 21:56:18 +00:00
|
|
|
next = &inpcb;
|
2005-02-19 15:10:19 +00:00
|
|
|
if (!aflag) {
|
|
|
|
if (inpcb.inp_vflag & INP_IPV4) {
|
|
|
|
if (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef INET6
|
|
|
|
else if (inpcb.inp_vflag & INP_IPV6) {
|
|
|
|
if (memcmp(&inpcb.in6p_laddr,
|
|
|
|
&in6addr_any, sizeof(in6addr_any)) == 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
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 (nhosts && !checkhost(&inpcb.inp_inc))
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
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 (nports && !checkport(&inpcb.inp_inc))
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
|
|
|
if (istcp) {
|
2009-03-15 09:58:31 +00:00
|
|
|
if (inpcb.inp_flags & INP_TIMEWAIT) {
|
2003-12-24 08:54:53 +00:00
|
|
|
bzero(&sockb, sizeof(sockb));
|
|
|
|
enter_kvm(&inpcb, &sockb, TCPS_TIME_WAIT,
|
|
|
|
"tcp");
|
|
|
|
} else {
|
|
|
|
KREAD(inpcb.inp_socket, &sockb,
|
|
|
|
sizeof (sockb));
|
|
|
|
KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
|
|
|
|
enter_kvm(&inpcb, &sockb, tcpcb.t_state,
|
|
|
|
"tcp");
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
} else
|
2001-03-23 03:58:25 +00:00
|
|
|
enter_kvm(&inpcb, &sockb, 0, "udp");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
if (istcp && (protos&UDP)) {
|
|
|
|
istcp = 0;
|
|
|
|
off = NPTR(X_UDB);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-16 19:27:43 +00:00
|
|
|
fetchnetstat_sysctl(void)
|
2001-03-23 03:58:25 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
struct netinfo *p;
|
2001-03-23 03:58:25 +00:00
|
|
|
int idx;
|
|
|
|
struct xinpgen *inpg;
|
|
|
|
char *cur, *end;
|
2006-11-27 17:34:40 +00:00
|
|
|
struct xinpcb *xip = NULL;
|
|
|
|
struct xtcpcb *xtp = NULL;
|
2001-03-23 03:58:25 +00:00
|
|
|
int plen;
|
|
|
|
size_t lsz;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(p, &netcb, chain)
|
|
|
|
p->ni_seen = 0;
|
|
|
|
if (protos&TCP) {
|
|
|
|
idx = 0;
|
|
|
|
} else if (protos&UDP) {
|
|
|
|
idx = 1;
|
|
|
|
} else {
|
|
|
|
error("No protocols to display");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;idx < 2; idx++) {
|
|
|
|
if (idx == 1 && !(protos&UDP))
|
|
|
|
break;
|
|
|
|
inpg = (struct xinpgen *)sysctl_dynread(miblist[idx], &lsz);
|
|
|
|
if (inpg == NULL) {
|
|
|
|
error("sysctl(%s...) failed", miblist[idx]);
|
|
|
|
continue;
|
|
|
|
}
|
2006-04-30 04:47:23 +00:00
|
|
|
/*
|
2001-03-23 03:58:25 +00:00
|
|
|
* We currently do no require a consistent pcb list.
|
2006-04-30 04:47:23 +00:00
|
|
|
* Try to be robust in case of struct size changes
|
2001-03-23 03:58:25 +00:00
|
|
|
*/
|
|
|
|
cur = ((char *)inpg) + inpg->xig_len;
|
|
|
|
/* There is also a trailing struct xinpgen */
|
|
|
|
end = ((char *)inpg) + lsz - inpg->xig_len;
|
|
|
|
if (end <= cur) {
|
|
|
|
free(inpg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (idx == 0) { /* TCP */
|
|
|
|
xtp = (struct xtcpcb *)cur;
|
|
|
|
plen = xtp->xt_len;
|
|
|
|
} else {
|
|
|
|
xip = (struct xinpcb *)cur;
|
|
|
|
plen = xip->xi_len;
|
|
|
|
}
|
|
|
|
while (cur + plen <= end) {
|
|
|
|
if (idx == 0) { /* TCP */
|
|
|
|
xtp = (struct xtcpcb *)cur;
|
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
|
|
|
xip = &xtp->xt_inp;
|
2001-03-23 03:58:25 +00:00
|
|
|
} else {
|
|
|
|
xip = (struct xinpcb *)cur;
|
|
|
|
}
|
|
|
|
cur += plen;
|
|
|
|
|
2005-02-19 15:10:19 +00:00
|
|
|
if (!aflag) {
|
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 (xip->inp_vflag & INP_IPV4) {
|
|
|
|
if (inet_lnaof(xip->inp_laddr) ==
|
2005-02-19 15:10:19 +00:00
|
|
|
INADDR_ANY)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef INET6
|
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
|
|
|
else if (xip->inp_vflag & INP_IPV6) {
|
|
|
|
if (memcmp(&xip->in6p_laddr,
|
2005-02-19 15:10:19 +00:00
|
|
|
&in6addr_any, sizeof(in6addr_any))
|
|
|
|
== 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
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 (nhosts && !checkhost(&xip->inp_inc))
|
2001-03-23 03:58:25 +00:00
|
|
|
continue;
|
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 (nports && !checkport(&xip->inp_inc))
|
2001-03-23 03:58:25 +00:00
|
|
|
continue;
|
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 (idx == 0)
|
|
|
|
enter_sysctl(xip, &xip->xi_socket,
|
|
|
|
xtp->t_state, "tcp");
|
|
|
|
else
|
|
|
|
enter_sysctl(xip, &xip->xi_socket, 0, "udp");
|
2001-03-23 03:58:25 +00:00
|
|
|
}
|
|
|
|
free(inpg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-16 19:27:43 +00:00
|
|
|
enter_kvm(struct inpcb *inp, struct socket *so, int state, const char *proto)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
struct netinfo *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
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 = enter(&inp->inp_inc, inp->inp_vflag, state, proto)) != NULL) {
|
Merge from projects/sendfile:
o Introduce a notion of "not ready" mbufs in socket buffers. These
mbufs are now being populated by some I/O in background and are
referenced outside. This forces following implications:
- An mbuf which is "not ready" can't be taken out of the buffer.
- An mbuf that is behind a "not ready" in the queue neither.
- If sockbet buffer is flushed, then "not ready" mbufs shouln't be
freed.
o In struct sockbuf the sb_cc field is split into sb_ccc and sb_acc.
The sb_ccc stands for ""claimed character count", or "committed
character count". And the sb_acc is "available character count".
Consumers of socket buffer API shouldn't already access them directly,
but use sbused() and sbavail() respectively.
o Not ready mbufs are marked with M_NOTREADY, and ready but blocked ones
with M_BLOCKED.
o New field sb_fnrdy points to the first not ready mbuf, to avoid linear
search.
o New function sbready() is provided to activate certain amount of mbufs
in a socket buffer.
A special note on SCTP:
SCTP has its own sockbufs. Unfortunately, FreeBSD stack doesn't yet
allow protocol specific sockbufs. Thus, SCTP does some hacks to make
itself compatible with FreeBSD: it manages sockbufs on its own, but keeps
sb_cc updated to inform the stack of amount of data in them. The new
notion of "not ready" data isn't supported by SCTP. Instead, only a
mechanical substitute is done: s/sb_cc/sb_ccc/.
A proper solution would be to take away struct sockbuf from struct
socket and allow protocols to implement their own socket buffers, like
SCTP already does. This was discussed with rrs@.
Sponsored by: Netflix
Sponsored by: Nginx, Inc.
2014-11-30 12:52:33 +00:00
|
|
|
p->ni_rcvcc = so->so_rcv.sb_ccc;
|
|
|
|
p->ni_sndcc = so->so_snd.sb_ccc;
|
2001-03-23 03:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
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
|
|
|
enter_sysctl(struct xinpcb *xip, struct xsocket *so, int state,
|
|
|
|
const char *proto)
|
2001-03-23 03:58:25 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
struct netinfo *p;
|
2001-03-23 03:58:25 +00:00
|
|
|
|
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 = enter(&xip->inp_inc, xip->inp_vflag, state, proto)) != NULL) {
|
2001-03-23 03:58:25 +00:00
|
|
|
p->ni_rcvcc = so->so_rcv.sb_cc;
|
|
|
|
p->ni_sndcc = so->so_snd.sb_cc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct netinfo *
|
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
|
|
|
enter(struct in_conninfo *inc, uint8_t vflag, int state, const char *proto)
|
2001-03-23 03:58:25 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
struct netinfo *p;
|
2005-02-19 15:10:19 +00:00
|
|
|
struct sockaddr_storage lsa, fsa;
|
|
|
|
struct sockaddr_in *sa4;
|
|
|
|
#ifdef INET6
|
|
|
|
struct sockaddr_in6 *sa6;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset(&lsa, 0, sizeof(lsa));
|
|
|
|
memset(&fsa, 0, sizeof(fsa));
|
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 (vflag & INP_IPV4) {
|
2005-02-19 15:10:19 +00:00
|
|
|
sa4 = (struct sockaddr_in *)&lsa;
|
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
|
|
|
sa4->sin_addr = inc->inc_laddr;
|
|
|
|
sa4->sin_port = inc->inc_lport;
|
2005-02-19 15:10:19 +00:00
|
|
|
sa4->sin_family = AF_INET;
|
|
|
|
sa4->sin_len = sizeof(struct sockaddr_in);
|
|
|
|
|
|
|
|
sa4 = (struct sockaddr_in *)&fsa;
|
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
|
|
|
sa4->sin_addr = inc->inc_faddr;
|
|
|
|
sa4->sin_port = inc->inc_fport;
|
2005-02-19 15:10:19 +00:00
|
|
|
sa4->sin_family = AF_INET;
|
|
|
|
sa4->sin_len = sizeof(struct sockaddr_in);
|
|
|
|
}
|
|
|
|
#ifdef INET6
|
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
|
|
|
else if (vflag & INP_IPV6) {
|
2005-02-19 15:10:19 +00:00
|
|
|
sa6 = (struct sockaddr_in6 *)&lsa;
|
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
|
|
|
memcpy(&sa6->sin6_addr, &inc->inc6_laddr,
|
2005-02-19 15:10:19 +00:00
|
|
|
sizeof(struct in6_addr));
|
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
|
|
|
sa6->sin6_port = inc->inc_lport;
|
2005-02-19 15:10:19 +00:00
|
|
|
sa6->sin6_family = AF_INET6;
|
|
|
|
sa6->sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
|
|
|
|
sa6 = (struct sockaddr_in6 *)&fsa;
|
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
|
|
|
memcpy(&sa6->sin6_addr, &inc->inc6_faddr,
|
2005-02-19 15:10:19 +00:00
|
|
|
sizeof(struct in6_addr));
|
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
|
|
|
sa6->sin6_port = inc->inc_fport;
|
2005-02-19 15:10:19 +00:00
|
|
|
sa6->sin6_family = AF_INET6;
|
|
|
|
sa6->sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
}
|
|
|
|
#endif
|
2005-02-19 16:54:26 +00:00
|
|
|
else
|
2005-02-19 15:10:19 +00:00
|
|
|
return NULL;
|
2001-03-23 03:58:25 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Only take exact matches, any sockets with
|
|
|
|
* previously unbound addresses will be deleted
|
|
|
|
* below in the display routine because they
|
|
|
|
* will appear as ``not seen'' in the kernel
|
|
|
|
* data structures.
|
|
|
|
*/
|
2000-12-30 21:17:03 +00:00
|
|
|
TAILQ_FOREACH(p, &netcb, chain) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!streq(proto, p->ni_proto))
|
|
|
|
continue;
|
2005-02-19 15:10:19 +00:00
|
|
|
if (p->ni_lsa.ss_family != lsa.ss_family ||
|
|
|
|
memcmp(&p->ni_lsa, &lsa, lsa.ss_len) != 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
2005-02-19 15:10:19 +00:00
|
|
|
if (p->ni_fsa.ss_family == fsa.ss_family &&
|
|
|
|
memcmp(&p->ni_fsa, &fsa, fsa.ss_len) == 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-12-30 21:17:03 +00:00
|
|
|
if (p == NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((p = malloc(sizeof(*p))) == NULL) {
|
|
|
|
error("Out of memory");
|
2001-03-23 03:58:25 +00:00
|
|
|
return NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2000-12-30 21:17:03 +00:00
|
|
|
TAILQ_INSERT_HEAD(&netcb, p, chain);
|
1994-05-27 12:33:43 +00:00
|
|
|
p->ni_line = -1;
|
2005-02-19 15:10:19 +00:00
|
|
|
memcpy(&p->ni_lsa, &lsa, lsa.ss_len);
|
|
|
|
memcpy(&p->ni_fsa, &fsa, fsa.ss_len);
|
2001-12-12 00:13:37 +00:00
|
|
|
p->ni_proto = strdup(proto);
|
1994-05-27 12:33:43 +00:00
|
|
|
p->ni_flags = NIF_LACHG|NIF_FACHG;
|
|
|
|
}
|
|
|
|
p->ni_state = state;
|
|
|
|
p->ni_seen = 1;
|
2001-03-23 03:58:25 +00:00
|
|
|
return p;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* column locations */
|
|
|
|
#define LADDR 0
|
|
|
|
#define FADDR LADDR+23
|
|
|
|
#define PROTO FADDR+23
|
|
|
|
#define RCVCC PROTO+6
|
|
|
|
#define SNDCC RCVCC+7
|
|
|
|
#define STATE SNDCC+7
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
labelnetstat(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-03-23 03:58:25 +00:00
|
|
|
if (use_kvm && namelist[X_TCB].n_type == 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
return;
|
|
|
|
wmove(wnd, 0, 0); wclrtobot(wnd);
|
|
|
|
mvwaddstr(wnd, 0, LADDR, "Local Address");
|
|
|
|
mvwaddstr(wnd, 0, FADDR, "Foreign Address");
|
|
|
|
mvwaddstr(wnd, 0, PROTO, "Proto");
|
|
|
|
mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
|
|
|
|
mvwaddstr(wnd, 0, SNDCC, "Send-Q");
|
1995-05-30 06:41:30 +00:00
|
|
|
mvwaddstr(wnd, 0, STATE, "(state)");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
shownetstat(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
struct netinfo *p, *q;
|
2006-11-27 17:01:31 +00:00
|
|
|
char proto[6];
|
|
|
|
const char *family = "";
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First, delete any connections that have gone
|
|
|
|
* away and adjust the position of connections
|
|
|
|
* below to reflect the deleted line.
|
|
|
|
*/
|
2001-03-23 03:58:25 +00:00
|
|
|
p = TAILQ_FIRST(&netcb);
|
|
|
|
while (p != NULL) {
|
|
|
|
if (p->ni_line == -1 || p->ni_seen) {
|
|
|
|
p = TAILQ_NEXT(p, chain);
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
2001-03-23 03:58:25 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
|
2000-12-30 21:17:03 +00:00
|
|
|
TAILQ_FOREACH(q, &netcb, chain)
|
1994-05-27 12:33:43 +00:00
|
|
|
if (q != p && q->ni_line > p->ni_line) {
|
|
|
|
q->ni_line--;
|
|
|
|
/* this shouldn't be necessary */
|
|
|
|
q->ni_flags |= NIF_LACHG|NIF_FACHG;
|
|
|
|
}
|
|
|
|
lastrow--;
|
2001-03-23 03:58:25 +00:00
|
|
|
q = TAILQ_NEXT(p, chain);
|
2000-12-30 21:17:03 +00:00
|
|
|
TAILQ_REMOVE(&netcb, p, chain);
|
1994-05-27 12:33:43 +00:00
|
|
|
free(p);
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Update existing connections and add new ones.
|
|
|
|
*/
|
2000-12-30 21:17:03 +00:00
|
|
|
TAILQ_FOREACH(p, &netcb, chain) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (p->ni_line == -1) {
|
|
|
|
/*
|
|
|
|
* Add a new entry if possible.
|
|
|
|
*/
|
|
|
|
if (lastrow > YMAX(wnd))
|
|
|
|
continue;
|
|
|
|
p->ni_line = lastrow++;
|
|
|
|
p->ni_flags |= NIF_LACHG|NIF_FACHG;
|
|
|
|
}
|
|
|
|
if (p->ni_flags & NIF_LACHG) {
|
|
|
|
wmove(wnd, p->ni_line, LADDR);
|
2005-02-19 15:10:19 +00:00
|
|
|
inetprint((struct sockaddr *)&p->ni_lsa, p->ni_proto);
|
1994-05-27 12:33:43 +00:00
|
|
|
p->ni_flags &= ~NIF_LACHG;
|
|
|
|
}
|
|
|
|
if (p->ni_flags & NIF_FACHG) {
|
|
|
|
wmove(wnd, p->ni_line, FADDR);
|
2005-02-19 15:10:19 +00:00
|
|
|
inetprint((struct sockaddr *)&p->ni_fsa, p->ni_proto);
|
1994-05-27 12:33:43 +00:00
|
|
|
p->ni_flags &= ~NIF_FACHG;
|
|
|
|
}
|
2005-02-19 15:10:19 +00:00
|
|
|
#ifdef INET6
|
|
|
|
family = (p->ni_lsa.ss_family == AF_INET) ? "4" : "6";
|
|
|
|
#endif
|
|
|
|
snprintf(proto, sizeof(proto), "%s%s", p->ni_proto, family);
|
|
|
|
mvwaddstr(wnd, p->ni_line, PROTO, proto);
|
2002-07-24 03:02:43 +00:00
|
|
|
mvwprintw(wnd, p->ni_line, RCVCC, "%6u", p->ni_rcvcc);
|
|
|
|
mvwprintw(wnd, p->ni_line, SNDCC, "%6u", p->ni_sndcc);
|
2001-12-12 00:13:37 +00:00
|
|
|
if (streq(p->ni_proto, "tcp")) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
|
|
|
|
mvwprintw(wnd, p->ni_line, STATE, "%d",
|
|
|
|
p->ni_state);
|
|
|
|
else
|
|
|
|
mvwaddstr(wnd, p->ni_line, STATE,
|
|
|
|
tcpstates[p->ni_state]);
|
2001-12-12 00:13:37 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
wclrtoeol(wnd);
|
|
|
|
}
|
|
|
|
if (lastrow < YMAX(wnd)) {
|
|
|
|
wmove(wnd, lastrow, 0); wclrtobot(wnd);
|
|
|
|
wmove(wnd, YMAX(wnd), 0); wdeleteln(wnd); /* XXX */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pretty print an Internet address (net address + port).
|
|
|
|
* If the nflag was specified, use numbers instead of names.
|
|
|
|
*/
|
|
|
|
static void
|
2008-01-16 19:27:43 +00:00
|
|
|
inetprint(struct sockaddr *sa, const char *proto)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct servent *sp = 0;
|
2001-12-12 00:13:37 +00:00
|
|
|
char line[80], *cp;
|
2005-02-19 15:10:19 +00:00
|
|
|
int port;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-02-19 15:10:19 +00:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
port = ((struct sockaddr_in *)sa)->sin_port;
|
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
port = ((struct sockaddr_in6 *)sa)->sin6_port;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
port = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
snprintf(line, sizeof(line), "%.*s.", 16, inetname(sa));
|
2012-01-03 18:51:58 +00:00
|
|
|
cp = strchr(line, '\0');
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!nflag && port)
|
|
|
|
sp = getservbyport(port, proto);
|
|
|
|
if (sp || port == 0)
|
2006-04-30 04:47:23 +00:00
|
|
|
snprintf(cp, sizeof(line) - (cp - line), "%.8s",
|
1998-06-09 04:17:29 +00:00
|
|
|
sp ? sp->s_name : "*");
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
2006-04-30 04:47:23 +00:00
|
|
|
snprintf(cp, sizeof(line) - (cp - line), "%d",
|
1998-06-09 04:17:29 +00:00
|
|
|
ntohs((u_short)port));
|
1994-05-27 12:33:43 +00:00
|
|
|
/* pad to full column to clear any garbage */
|
2012-01-03 18:51:58 +00:00
|
|
|
cp = strchr(line, '\0');
|
1994-05-27 12:33:43 +00:00
|
|
|
while (cp - line < 22)
|
|
|
|
*cp++ = ' ';
|
1996-11-02 15:15:40 +00:00
|
|
|
line[22] = '\0';
|
1994-05-27 12:33:43 +00:00
|
|
|
waddstr(wnd, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct an Internet address representation.
|
1995-05-30 06:41:30 +00:00
|
|
|
* If the nflag has been supplied, give
|
1994-05-27 12:33:43 +00:00
|
|
|
* numeric value, otherwise try for symbolic name.
|
|
|
|
*/
|
|
|
|
static char *
|
2008-01-16 19:27:43 +00:00
|
|
|
inetname(struct sockaddr *sa)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
char *cp = 0;
|
2005-02-19 15:10:19 +00:00
|
|
|
static char line[NI_MAXHOST];
|
1994-05-27 12:33:43 +00:00
|
|
|
struct hostent *hp;
|
|
|
|
struct netent *np;
|
2005-02-19 15:10:19 +00:00
|
|
|
struct in_addr in;
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
if (sa->sa_family == AF_INET6) {
|
|
|
|
if (memcmp(&((struct sockaddr_in6 *)sa)->sin6_addr,
|
|
|
|
&in6addr_any, sizeof(in6addr_any)) == 0)
|
|
|
|
strcpy(line, "*");
|
|
|
|
else
|
|
|
|
getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
|
|
|
|
nflag ? NI_NUMERICHOST : 0);
|
|
|
|
return (line);
|
|
|
|
}
|
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-02-19 15:10:19 +00:00
|
|
|
in = ((struct sockaddr_in *)sa)->sin_addr;
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!nflag && in.s_addr != INADDR_ANY) {
|
|
|
|
int net = inet_netof(in);
|
|
|
|
int lna = inet_lnaof(in);
|
|
|
|
|
|
|
|
if (lna == INADDR_ANY) {
|
|
|
|
np = getnetbyaddr(net, AF_INET);
|
|
|
|
if (np)
|
|
|
|
cp = np->n_name;
|
|
|
|
}
|
2016-04-20 01:26:03 +00:00
|
|
|
if (cp == NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
|
|
|
|
if (hp)
|
|
|
|
cp = hp->h_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in.s_addr == INADDR_ANY)
|
|
|
|
strcpy(line, "*");
|
|
|
|
else if (cp)
|
1998-06-09 04:17:29 +00:00
|
|
|
snprintf(line, sizeof(line), "%s", cp);
|
1994-05-27 12:33:43 +00:00
|
|
|
else {
|
|
|
|
in.s_addr = ntohl(in.s_addr);
|
|
|
|
#define C(x) ((x) & 0xff)
|
1998-06-09 04:17:29 +00:00
|
|
|
snprintf(line, sizeof(line), "%u.%u.%u.%u", C(in.s_addr >> 24),
|
1994-05-27 12:33:43 +00:00
|
|
|
C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
|
|
|
|
}
|
|
|
|
return (line);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-01-16 19:27:43 +00:00
|
|
|
cmdnetstat(const char *cmd, const char *args)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
if (prefix(cmd, "all")) {
|
|
|
|
aflag = !aflag;
|
|
|
|
goto fixup;
|
|
|
|
}
|
|
|
|
if (prefix(cmd, "numbers") || prefix(cmd, "names")) {
|
2000-12-30 21:17:03 +00:00
|
|
|
struct netinfo *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
int new;
|
|
|
|
|
|
|
|
new = prefix(cmd, "numbers");
|
|
|
|
if (new == nflag)
|
|
|
|
return (1);
|
2000-12-30 21:17:03 +00:00
|
|
|
TAILQ_FOREACH(p, &netcb, chain) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (p->ni_line == -1)
|
|
|
|
continue;
|
|
|
|
p->ni_flags |= NIF_LACHG|NIF_FACHG;
|
|
|
|
}
|
|
|
|
nflag = new;
|
|
|
|
goto redisplay;
|
|
|
|
}
|
|
|
|
if (!netcmd(cmd, args))
|
|
|
|
return (0);
|
|
|
|
fixup:
|
|
|
|
fetchnetstat();
|
|
|
|
redisplay:
|
|
|
|
shownetstat();
|
|
|
|
refresh();
|
|
|
|
return (1);
|
|
|
|
}
|