Commit 93610c03 authored by Kevin Bracey's avatar Kevin Bracey
Browse files

New inetstat that uses sysctl instead of kvm_nread. Main visible difference is...

New inetstat that uses sysctl instead of kvm_nread. Main visible difference is that raw sockets are listed, and there is no danger of garbage being printed if a socket opens or closes while running.

New sysctl that handles Internet 5.31's new sysctl code. No longer has any
hard-coded knowledge of any sysctls.
parent 37a156ff
| Copyright 1999 Pace Micro Technology plc
|
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
|
Dir <Obey$Dir>
amu_machine install INSTDIR=<Obey$Dir>
...@@ -29,6 +29,7 @@ If "%1" = "" Then Obey ...@@ -29,6 +29,7 @@ If "%1" = "" Then Obey
dir %1 dir %1
amu_machine %0 amu_machine %0
if "%0" = "clean" Then stripdepnd
up up
| No new-line at the end of this command, please! | No new-line at the end of this command, please!
......
...@@ -40,47 +40,3 @@ clean:; ${RM} ${TARGET} ...@@ -40,47 +40,3 @@ clean:; ${RM} ${TARGET}
${WIPE} o.* ${WFLAGS} ${WIPE} o.* ${WFLAGS}
# Dynamic dependencies: # Dynamic dependencies:
o.ARP: c.ARP
o.ARP: TCPIPLibs:sys.h.param
o.ARP: TCPIPLibs:sys.h.types
o.ARP: TCPIPLibs:sys.h.cdefs
o.ARP: TCPIPLibs:machine.h.endian
o.ARP: TCPIPLibs:machine.h.ansi
o.ARP: TCPIPLibs:machine.h.types
o.ARP: TCPIPLibs:sys.h.syslimits
o.ARP: TCPIPLibs:sys.h.signal
o.ARP: TCPIPLibs:machine.h.signal
o.ARP: TCPIPLibs:sys.h.cdefs
o.ARP: TCPIPLibs:machine.h.param
o.ARP: TCPIPLibs:machine.h.limits
o.ARP: TCPIPLibs:sys.h.file
o.ARP: TCPIPLibs:sys.h.fcntl
o.ARP: TCPIPLibs:sys.h.types
o.ARP: TCPIPLibs:sys.h.cdefs
o.ARP: TCPIPLibs:sys.h.unistd
o.ARP: TCPIPLibs:sys.h.socket
o.ARP: TCPIPLibs:sys.h.cdefs
o.ARP: TCPIPLibs:sys.h.sysctl
o.ARP: TCPIPLibs:net.h.if
o.ARP: TCPIPLibs:sys.h.socket
o.ARP: TCPIPLibs:sys.h.time
o.ARP: TCPIPLibs:sys.h.cdefs
o.ARP: TCPIPLibs:net.h.if_arp
o.ARP: TCPIPLibs:sys.h.socket
o.ARP: TCPIPLibs:net.h.if_dl
o.ARP: TCPIPLibs:sys.h.cdefs
o.ARP: TCPIPLibs:net.h.if_types
o.ARP: TCPIPLibs:net.h.route
o.ARP: TCPIPLibs:net.h.radix
o.ARP: TCPIPLibs:netinet.h.in
o.ARP: TCPIPLibs:netinet.h.if_ether
o.ARP: TCPIPLibs:arpa.h.inet
o.ARP: TCPIPLibs:sys.h.cdefs
o.ARP: TCPIPLibs:h.netdb
o.ARP: TCPIPLibs:sys.h.cdefs
o.ARP: TCPIPLibs:sys.h.errno
o.ARP: C:h.kernel
o.ARP: TCPIPLibs:h.unixlib
o.ARP: TCPIPLibs:h.riscos
o.ARP: TCPIPLibs:h.nlist
o.ARP: TCPIPLibs:sys.h.cdefs
...@@ -74,6 +74,7 @@ static const char rcsid[] = ...@@ -74,6 +74,7 @@ static const char rcsid[] =
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef __riscos #ifdef __riscos
#include <sys/errno.h>
#include <unixlib.h> #include <unixlib.h>
#else #else
#include <unistd.h> #include <unistd.h>
...@@ -94,46 +95,76 @@ void inetprint(struct in_addr *, int, char *, int); ...@@ -94,46 +95,76 @@ void inetprint(struct in_addr *, int, char *, int);
* -a (all) flag is specified. * -a (all) flag is specified.
*/ */
void void
protopr(off, name) protopr(proto, name)
u_long off; u_long proto; /* for sysctl version we pass proto # */
char *name; char *name;
{ {
struct inpcbhead head;
register struct inpcb *prev, *next;
int istcp; int istcp;
static int first = 1; static int first = 1;
#ifdef __riscos char *buf;
struct inpcb *inp = &inpcb; /* Transitional hack */ const char *mibvar;
#endif struct tcpcb *tp;
struct inpcb *inp;
struct xinpgen *xig, *oxig;
struct xsocket *so;
size_t len;
if (off == 0) istcp = 0;
switch (proto) {
case IPPROTO_TCP:
istcp = 1;
mibvar = "net.inet.tcp.pcblist";
break;
case IPPROTO_UDP:
mibvar = "net.inet.udp.pcblist";
break;
case IPPROTO_DIVERT:
mibvar = "net.inet.divert.pcblist";
break;
default:
mibvar = "net.inet.raw.pcblist";
break;
}
len = 0;
if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
if (errno != ENOENT)
warn("sysctl: %s", mibvar);
return; return;
}
if ((buf = malloc(len)) == 0) {
warn("malloc %lu bytes", (u_long)len);
return;
}
if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
warn("sysctl: %s", mibvar);
free(buf);
return;
}
istcp = strcmp(name, "tcp") == 0; oxig = xig = (struct xinpgen *)buf;
kread(off, (char *)&head, sizeof (struct inpcbhead)); for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
prev = (struct inpcb *)off; xig->xig_len > sizeof(struct xinpgen);
xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
for (next = head.lh_first; next != NULL; next = inpcb.inp_list.le_next) {
if (kread((u_long)next, (char *)&inpcb, sizeof (inpcb))) {
printf("???\n");
break;
}
if (!aflag &&
inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) {
prev = next;
continue;
}
if (kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb))) {
printf("???\n");
break;
};
if (istcp) { if (istcp) {
if (kread((u_long)inpcb.inp_ppcb, tp = &((struct xtcpcb *)xig)->xt_tp;
(char *)&tcpcb, sizeof (tcpcb))) { inp = &((struct xtcpcb *)xig)->xt_inp;
printf("???\n"); so = &((struct xtcpcb *)xig)->xt_socket;
break; } else {
}; inp = &((struct xinpcb *)xig)->xi_inp;
so = &((struct xinpcb *)xig)->xi_socket;
} }
/* Ignore sockets for protocols other than the desired one. */
if (so->xso_protocol != proto)
continue;
/* Ignore PCBs which were freed during copyout. */
if (inp->inp_gencnt > oxig->xig_gen)
continue;
if (!aflag && inet_lnaof(inp->inp_laddr) == INADDR_ANY)
continue;
if (first) { if (first) {
printf("Active Internet connections"); printf("Active Internet connections");
if (aflag) if (aflag)
...@@ -150,12 +181,12 @@ protopr(off, name) ...@@ -150,12 +181,12 @@ protopr(off, name)
} }
if (Aflag) { if (Aflag) {
if (istcp) if (istcp)
printf("%8lx ", (u_long)inpcb.inp_ppcb); printf("%8lx ", (u_long)inp->inp_ppcb);
else else
printf("%8lx ", (u_long)next); printf("%8lx ", (u_long)so->so_pcb);
} }
printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc, printf("%-5.5s %6ld %6ld ", name, so->so_rcv.sb_cc,
sockb.so_snd.sb_cc); so->so_snd.sb_cc);
if (nflag) { if (nflag) {
inetprint(&inp->inp_laddr, (int)inp->inp_lport, inetprint(&inp->inp_laddr, (int)inp->inp_lport,
name, 1); name, 1);
...@@ -173,20 +204,32 @@ protopr(off, name) ...@@ -173,20 +204,32 @@ protopr(off, name)
name, inp->inp_lport != inp->inp_fport); name, inp->inp_lport != inp->inp_fport);
} }
if (istcp) { if (istcp) {
if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES) if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
printf(" %d", tcpcb.t_state); printf(" %d", tp->t_state);
else { else {
printf(" %s", tcpstates[tcpcb.t_state]); printf(" %s", tcpstates[tp->t_state]);
#if defined(TF_NEEDSYN) && defined(TF_NEEDFIN) #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
/* Show T/TCP `hidden state' */ /* Show T/TCP `hidden state' */
if (tcpcb.t_flags & (TF_NEEDSYN|TF_NEEDFIN)) if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
putchar('*'); putchar('*');
#endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */ #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
} }
} }
putchar('\n'); putchar('\n');
prev = next;
} }
if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
if (oxig->xig_count > xig->xig_count) {
printf("Some %s sockets may have been deleted.\n",
name);
} else if (oxig->xig_count < xig->xig_count) {
printf("Some %s sockets may have been created.\n",
name);
} else {
printf("Some %s sockets may have been created or deleted",
name);
}
}
free(buf);
} }
/* /*
...@@ -198,11 +241,14 @@ tcp_stats(off, name) ...@@ -198,11 +241,14 @@ tcp_stats(off, name)
char *name; char *name;
{ {
struct tcpstat tcpstat; struct tcpstat tcpstat;
size_t len = sizeof tcpstat;
if (off == 0) if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len, 0, 0) < 0) {
warn("sysctl: net.inet.tcp.stats");
return; return;
}
printf ("%s:\n", name); printf ("%s:\n", name);
kread(off, (char *)&tcpstat, sizeof (tcpstat));
#define p(f, m) if (tcpstat.f || sflag <= 1) \ #define p(f, m) if (tcpstat.f || sflag <= 1) \
printf(m, tcpstat.f, plural(tcpstat.f)) printf(m, tcpstat.f, plural(tcpstat.f))
...@@ -288,11 +334,14 @@ udp_stats(off, name) ...@@ -288,11 +334,14 @@ udp_stats(off, name)
char *name; char *name;
{ {
struct udpstat udpstat; struct udpstat udpstat;
size_t len = sizeof udpstat;
u_long delivered; u_long delivered;
if (off == 0) if (sysctlbyname("net.inet.udp.stats", &udpstat, &len, 0, 0) < 0) {
warn("sysctl: net.inet.udp.stats");
return; return;
kread(off, (char *)&udpstat, sizeof (udpstat)); }
printf("%s:\n", name); printf("%s:\n", name);
#define p(f, m) if (udpstat.f || sflag <= 1) \ #define p(f, m) if (udpstat.f || sflag <= 1) \
printf(m, udpstat.f, plural(udpstat.f)) printf(m, udpstat.f, plural(udpstat.f))
...@@ -332,10 +381,13 @@ ip_stats(off, name) ...@@ -332,10 +381,13 @@ ip_stats(off, name)
char *name; char *name;
{ {
struct ipstat ipstat; struct ipstat ipstat;
size_t len = sizeof ipstat;
if (off == 0) if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, 0, 0) < 0) {
warn("sysctl: net.inet.ip.stats");
return; return;
kread(off, (char *)&ipstat, sizeof (ipstat)); }
printf("%s:\n", name); printf("%s:\n", name);
#define p(f, m) if (ipstat.f || sflag <= 1) \ #define p(f, m) if (ipstat.f || sflag <= 1) \
...@@ -480,10 +532,13 @@ igmp_stats(off, name) ...@@ -480,10 +532,13 @@ igmp_stats(off, name)
char *name; char *name;
{ {
struct igmpstat igmpstat; struct igmpstat igmpstat;
size_t len = sizeof igmpstat;
if (off == 0) if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len, 0, 0) < 0) {
warn("sysctl: net.inet.igmp.stats");
return; return;
kread(off, (char *)&igmpstat, sizeof (igmpstat)); }
printf("%s:\n", name); printf("%s:\n", name);
#define p(f, m) if (igmpstat.f || sflag <= 1) \ #define p(f, m) if (igmpstat.f || sflag <= 1) \
......
...@@ -140,17 +140,18 @@ struct protox { ...@@ -140,17 +140,18 @@ struct protox {
void (*pr_cblocks)(); /* control blocks printing routine */ void (*pr_cblocks)(); /* control blocks printing routine */
void (*pr_stats)(); /* statistics printing routine */ void (*pr_stats)(); /* statistics printing routine */
char *pr_name; /* well-known name */ char *pr_name; /* well-known name */
int pr_usesysctl; /* true if we use sysctl, not kvm */
} protox[] = { } protox[] = {
{ N_TCB, N_TCPSTAT, 1, protopr, { N_TCB, N_TCPSTAT, 1, protopr,
tcp_stats, "tcp" }, tcp_stats, "tcp", IPPROTO_TCP },
{ N_UDB, N_UDPSTAT, 1, protopr, { N_UDB, N_UDPSTAT, 1, protopr,
udp_stats, "udp" }, udp_stats, "udp", IPPROTO_UDP },
{ -1, N_IPSTAT, 1, 0, { -1, N_IPSTAT, 1, protopr,
ip_stats, "ip" }, ip_stats, "ip", IPPROTO_RAW },
{ -1, N_ICMPSTAT, 1, 0, { -1, N_ICMPSTAT, 1, protopr,
icmp_stats, "icmp" }, icmp_stats, "icmp", IPPROTO_ICMP },
{ -1, N_IGMPSTAT, 1, 0, { -1, N_IGMPSTAT, 1, protopr,
igmp_stats, "igmp" }, igmp_stats, "igmp", IPPROTO_IGMP },
{ -1, -1, 0, 0, { -1, -1, 0, 0,
0, 0 } 0, 0 }
}; };
...@@ -351,11 +352,17 @@ main(argc, argv) ...@@ -351,11 +352,17 @@ main(argc, argv)
} }
#endif #endif
if (pflag) { if (pflag) {
if (tp->pr_stats) if (!tp->pr_stats) {
(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
tp->pr_name);
else
printf("%s: no stats routine\n", tp->pr_name); printf("%s: no stats routine\n", tp->pr_name);
exit(0);
}
if (tp->pr_usesysctl) {
(*tp->pr_stats)(tp->pr_usesysctl, tp->pr_name);
} else {
kread(0, 0, 0);
(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
tp->pr_name);
}
exit(0); exit(0);
} }
/* /*
...@@ -427,10 +434,12 @@ printproto(tp, name) ...@@ -427,10 +434,12 @@ printproto(tp, name)
if (sflag) { if (sflag) {
pr = tp->pr_stats; pr = tp->pr_stats;
off = nl[tp->pr_sindex].n_value; off = tp->pr_usesysctl ? tp->pr_usesysctl
: nl[tp->pr_sindex].n_value;
} else { } else {
pr = tp->pr_cblocks; pr = tp->pr_cblocks;
off = nl[tp->pr_index].n_value; off = tp->pr_usesysctl ? tp->pr_usesysctl
: nl[tp->pr_index].n_value;
} }
if (pr != NULL && (off || af != AF_UNSPEC)) if (pr != NULL && (off || af != AF_UNSPEC))
(*pr)(off, name); (*pr)(off, name);
......
...@@ -39,6 +39,3 @@ clean:; ${RM} ${TARGET} ...@@ -39,6 +39,3 @@ clean:; ${RM} ${TARGET}
${WIPE} o.* ${WFLAGS} ${WIPE} o.* ${WFLAGS}
# Dynamic dependencies: # Dynamic dependencies:
o.NewFiler: c.NewFiler
o.NewFiler: C:h.kernel
o.NewFiler: C:h.swis
...@@ -40,52 +40,3 @@ clean:; ${RM} ${TARGET} ...@@ -40,52 +40,3 @@ clean:; ${RM} ${TARGET}
${WIPE} o.* ${WFLAGS} ${WIPE} o.* ${WFLAGS}
# Dynamic dependencies: # Dynamic dependencies:
o.Ping: c.Ping
o.Ping: TCPIPLibs:sys.h.param
o.Ping: TCPIPLibs:sys.h.types
o.Ping: TCPIPLibs:sys.h.cdefs
o.Ping: TCPIPLibs:machine.h.endian
o.Ping: TCPIPLibs:machine.h.ansi
o.Ping: TCPIPLibs:machine.h.types
o.Ping: TCPIPLibs:sys.h.syslimits
o.Ping: TCPIPLibs:sys.h.signal
o.Ping: TCPIPLibs:machine.h.signal
o.Ping: TCPIPLibs:sys.h.cdefs
o.Ping: TCPIPLibs:machine.h.param
o.Ping: TCPIPLibs:machine.h.limits
o.Ping: TCPIPLibs:sys.h.socket
o.Ping: TCPIPLibs:sys.h.cdefs
o.Ping: TCPIPLibs:sys.h.file
o.Ping: TCPIPLibs:sys.h.fcntl
o.Ping: TCPIPLibs:sys.h.types
o.Ping: TCPIPLibs:sys.h.cdefs
o.Ping: TCPIPLibs:sys.h.unistd
o.Ping: TCPIPLibs:sys.h.time
o.Ping: TCPIPLibs:sys.h.cdefs
o.Ping: TCPIPLibs:sys.h.signal
o.Ping: TCPIPLibs:sys.h.ioctl
o.Ping: TCPIPLibs:sys.h.ttycom
o.Ping: TCPIPLibs:sys.h.ioccom
o.Ping: TCPIPLibs:sys.h.ioccom
o.Ping: TCPIPLibs:sys.h.filio
o.Ping: TCPIPLibs:sys.h.ioccom
o.Ping: TCPIPLibs:sys.h.sockio
o.Ping: TCPIPLibs:sys.h.ioccom
o.Ping: TCPIPLibs:sys.h.cdefs
o.Ping: TCPIPLibs:netinet.h.in_systm
o.Ping: TCPIPLibs:netinet.h.in
o.Ping: TCPIPLibs:netinet.h.ip
o.Ping: TCPIPLibs:netinet.h.ip_icmp
o.Ping: TCPIPLibs:netinet.h.ip_var
o.Ping: TCPIPLibs:arpa.h.inet
o.Ping: TCPIPLibs:sys.h.cdefs
o.Ping: TCPIPLibs:h.netdb
o.Ping: TCPIPLibs:sys.h.cdefs
o.Ping: TCPIPLibs:h.unistd
o.Ping: TCPIPLibs:sys.h.cdefs
o.Ping: TCPIPLibs:sys.h.types
o.Ping: TCPIPLibs:sys.h.unistd
o.Ping: TCPIPLibs:sys.h.errno
o.Ping: C:h.kernel
o.Ping: TCPIPLibs:h.riscos
o.Ping: TCPIPLibs:h.unixlib
...@@ -107,8 +107,9 @@ typedef union sockunion *sup; ...@@ -107,8 +107,9 @@ typedef union sockunion *sup;
int pid, rtm_addrs, uid; int pid, rtm_addrs, uid;
int s = -1; int s = -1;
int forcehost, forcenet, doflush, nflag, af, qflag, tflag, keyword(); int forcehost, forcenet, doflush, nflag, af, qflag, tflag, keyword();
int errvarflag;
int iflag, verbose, aflen = sizeof (struct sockaddr_in); int iflag, verbose, aflen = sizeof (struct sockaddr_in);
int locking, lockrest, debugonly, errvarflag; int locking, lockrest, debugonly;
struct rt_metrics rt_metrics; struct rt_metrics rt_metrics;
u_long rtm_inits; u_long rtm_inits;
struct in_addr inet_makeaddr(); struct in_addr inet_makeaddr();
...@@ -172,6 +173,7 @@ main(argc, argv) ...@@ -172,6 +173,7 @@ main(argc, argv)
switch(ch) { switch(ch) {
case 'e': case 'e':
errvarflag = 1; errvarflag = 1;
err_set_silent(1);
break; break;
case 'n': case 'n':
nflag = 1; nflag = 1;
...@@ -684,36 +686,6 @@ newroute(argc, argv) ...@@ -684,36 +686,6 @@ newroute(argc, argv)
gateway = *argv; gateway = *argv;
(void) getaddr(RTA_GATEWAY, *argv, &hp); (void) getaddr(RTA_GATEWAY, *argv, &hp);
} else { } else {
int ret = atoi(*argv);
if (ret == 0) {
if (strcmp(*argv, "0") == 0)
#ifdef __riscos
{
if (!errvarflag)
#endif
printf("%s,%s",
"old usage of trailing 0",
"assuming route to if\n");
#ifdef __riscos
}
#endif
else
printhelp();
iflag = 1;
continue;
} else if (ret > 0 && ret < 10) {
#ifdef __riscos
if (!errvarflag) {
#endif
printf("old usage of trailing digit, ");
printf("assuming route via gateway\n");
#ifdef __riscos
}
#endif
iflag = 0;
continue;
}
(void) getaddr(RTA_NETMASK, *argv, 0); (void) getaddr(RTA_NETMASK, *argv, 0);
} }
} }
......
...@@ -43,56 +43,3 @@ PathConf: PathConf.o ...@@ -43,56 +43,3 @@ PathConf: PathConf.o
Link -o $@ PathConf.o ${LIBS} Link -o $@ PathConf.o ${LIBS}
# Dynamic dependencies: # Dynamic dependencies:
o.PathConf: c.PathConf
o.PathConf: TCPIPLibs:sys.h.param
o.PathConf: TCPIPLibs:sys.h.types
o.PathConf: TCPIPLibs:sys.h.cdefs
o.PathConf: TCPIPLibs:machine.h.endian
o.PathConf: TCPIPLibs:machine.h.ansi
o.PathConf: TCPIPLibs:machine.h.types
o.PathConf: TCPIPLibs:sys.h.syslimits
o.PathConf: TCPIPLibs:sys.h.signal
o.PathConf: TCPIPLibs:machine.h.signal
o.PathConf: TCPIPLibs:sys.h.cdefs
o.PathConf: TCPIPLibs:machine.h.param
o.PathConf: TCPIPLibs:machine.h.limits
o.PathConf: TCPIPLibs:sys.h.sysctl
o.PathConf: TCPIPLibs:sys.h.unistd
o.PathConf: TCPIPLibs:sys.h.errno
o.PathConf: C:h.kernel
o.SysCtl: c.SysCtl
o.SysCtl: TCPIPLibs:sys.h.param
o.SysCtl: TCPIPLibs:sys.h.types
o.SysCtl: TCPIPLibs:sys.h.cdefs
o.SysCtl: TCPIPLibs:machine.h.endian
o.SysCtl: TCPIPLibs:machine.h.ansi
o.SysCtl