version 1.102, 2007/12/13 20:00:53 |
version 1.103, 2007/12/14 18:35:46 |
|
|
#include <net/pfvar.h> |
#include <net/pfvar.h> |
#include <net/if_pfsync.h> |
#include <net/if_pfsync.h> |
|
|
|
#include <rpc/rpc.h> |
|
#include <rpc/pmap_prot.h> |
|
#include <rpc/pmap_clnt.h> |
|
|
#include <arpa/inet.h> |
#include <arpa/inet.h> |
#include <limits.h> |
#include <limits.h> |
#include <netdb.h> |
#include <netdb.h> |
|
|
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <errno.h> |
#include "netstat.h" |
#include "netstat.h" |
|
|
#include <rpc/rpc.h> |
|
#include <rpc/pmap_prot.h> |
|
#include <rpc/pmap_clnt.h> |
|
|
|
struct inpcb inpcb; |
struct inpcb inpcb; |
struct tcpcb tcpcb; |
struct tcpcb tcpcb; |
struct socket sockb; |
struct socket sockb; |
|
|
|
|
char *inetname(struct in_addr *); |
char *inetname(struct in_addr *); |
void inetprint(struct in_addr *, in_port_t, char *, int); |
void inetprint(struct in_addr *, in_port_t, char *, int); |
#ifdef INET6 |
|
char *inet6name(struct in6_addr *); |
char *inet6name(struct in6_addr *); |
void inet6print(struct in6_addr *, int, char *, int); |
void inet6print(struct in6_addr *, int, char *, int); |
#endif |
|
|
|
/* |
/* |
* Print a summary of connections related to an Internet |
* Print a summary of connections related to an Internet |
|
|
protopr0(off, name, AF_INET); |
protopr0(off, name, AF_INET); |
} |
} |
|
|
#ifdef INET6 |
|
void |
void |
ip6protopr(u_long off, char *name) |
ip6protopr(u_long off, char *name) |
{ |
{ |
protopr0(off, name, AF_INET6); |
protopr0(off, name, AF_INET6); |
} |
} |
#endif |
|
|
|
static void |
static void |
protopr0(u_long off, char *name, int af) |
protopr0(u_long off, char *name, int af) |
|
|
else |
else |
printf("%*p ", PLEN, prev); |
printf("%*p ", PLEN, prev); |
} |
} |
#ifdef INET6 |
|
if (inpcb.inp_flags & INP_IPV6 && !israw) { |
if (inpcb.inp_flags & INP_IPV6 && !israw) { |
strlcpy(namebuf, name0, sizeof namebuf); |
strlcpy(namebuf, name0, sizeof namebuf); |
strlcat(namebuf, "6", sizeof namebuf); |
strlcat(namebuf, "6", sizeof namebuf); |
name = namebuf; |
name = namebuf; |
} else |
} else |
name = name0; |
name = name0; |
#endif |
|
printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc, |
printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc, |
sockb.so_snd.sb_cc); |
sockb.so_snd.sb_cc); |
#ifdef INET6 |
|
if (inpcb.inp_flags & INP_IPV6) { |
if (inpcb.inp_flags & INP_IPV6) { |
inet6print(&inpcb.inp_laddr6, (int)inpcb.inp_lport, |
inet6print(&inpcb.inp_laddr6, (int)inpcb.inp_lport, |
name, 1); |
name, 1); |
inet6print(&inpcb.inp_faddr6, (int)inpcb.inp_fport, |
inet6print(&inpcb.inp_faddr6, (int)inpcb.inp_fport, |
name, 0); |
name, 0); |
} else |
} else { |
#endif |
|
{ |
|
inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport, |
inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport, |
name, 1); |
name, 1); |
inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport, |
inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport, |
|
|
printf(" %s", tcpstates[tcpcb.t_state]); |
printf(" %s", tcpstates[tcpcb.t_state]); |
} else if (israw) { |
} else if (israw) { |
u_int8_t proto; |
u_int8_t proto; |
#ifdef INET6 |
|
if (inpcb.inp_flags & INP_IPV6) |
if (inpcb.inp_flags & INP_IPV6) |
proto = inpcb.inp_ipv6.ip6_nxt; |
proto = inpcb.inp_ipv6.ip6_nxt; |
else |
else |
#endif |
|
proto = inpcb.inp_ip.ip_p; |
proto = inpcb.inp_ip.ip_p; |
printf(" %u", proto); |
printf(" %u", proto); |
} |
} |
|
|
* Dump TCP statistics structure. |
* Dump TCP statistics structure. |
*/ |
*/ |
void |
void |
tcp_stats(u_long off, char *name) |
tcp_stats(char *name) |
{ |
{ |
struct tcpstat tcpstat; |
struct tcpstat tcpstat; |
size_t len; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_TCP, TCPCTL_STATS }; |
int mib[] = { CTL_NET, AF_INET, IPPROTO_TCP, TCPCTL_STATS }; |
|
size_t len = sizeof(tcpstat); |
|
|
if (off == 0) |
|
return; |
|
|
|
len = sizeof(tcpstat); |
|
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
&tcpstat, &len, NULL, 0) == -1) { |
&tcpstat, &len, NULL, 0) == -1) { |
warn(name); |
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
} |
} |
|
|
|
|
* Dump UDP statistics structure. |
* Dump UDP statistics structure. |
*/ |
*/ |
void |
void |
udp_stats(u_long off, char *name) |
udp_stats(char *name) |
{ |
{ |
struct udpstat udpstat; |
struct udpstat udpstat; |
u_long delivered; |
u_long delivered; |
size_t len; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_UDP, UDPCTL_STATS }; |
int mib[] = { CTL_NET, AF_INET, IPPROTO_UDP, UDPCTL_STATS }; |
|
size_t len = sizeof(udpstat); |
|
|
if (off == 0) |
|
return; |
|
|
|
len = sizeof(udpstat); |
|
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
&udpstat, &len, NULL, 0) == -1) { |
&udpstat, &len, NULL, 0) == -1) { |
warn(name); |
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
} |
} |
|
|
|
|
* Dump IP statistics structure. |
* Dump IP statistics structure. |
*/ |
*/ |
void |
void |
ip_stats(u_long off, char *name) |
ip_stats(char *name) |
{ |
{ |
struct ipstat ipstat; |
struct ipstat ipstat; |
size_t len; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_IP, IPCTL_STATS }; |
int mib[] = { CTL_NET, AF_INET, IPPROTO_IP, IPCTL_STATS }; |
|
size_t len = sizeof(ipstat);; |
|
|
if (off == 0) |
|
return; |
|
|
|
len = sizeof(ipstat); |
|
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
&ipstat, &len, NULL, 0) == -1) { |
&ipstat, &len, NULL, 0) == -1) { |
warn(name); |
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
} |
} |
|
|
|
|
* Dump ICMP statistics. |
* Dump ICMP statistics. |
*/ |
*/ |
void |
void |
icmp_stats(u_long off, char *name) |
icmp_stats(char *name) |
{ |
{ |
struct icmpstat icmpstat; |
struct icmpstat icmpstat; |
int i, first; |
int i, first; |
int mib[] = { CTL_NET, AF_INET, IPPROTO_ICMP, ICMPCTL_STATS }; |
int mib[] = { CTL_NET, AF_INET, IPPROTO_ICMP, ICMPCTL_STATS }; |
size_t len; |
size_t len = sizeof(icmpstat); |
|
|
if (off == 0) |
|
return; |
|
|
|
len = sizeof(icmpstat); |
|
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
&icmpstat, &len, NULL, 0) == -1) { |
&icmpstat, &len, NULL, 0) == -1) { |
warn(name); |
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
} |
} |
|
|
|
|
* Dump IGMP statistics structure. |
* Dump IGMP statistics structure. |
*/ |
*/ |
void |
void |
igmp_stats(u_long off, char *name) |
igmp_stats(char *name) |
{ |
{ |
struct igmpstat igmpstat; |
struct igmpstat igmpstat; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_IGMP, IGMPCTL_STATS }; |
|
size_t len = sizeof(igmpstat); |
|
|
if (off == 0) |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
|
&igmpstat, &len, NULL, 0) == -1) { |
|
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
kread(off, &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) \ |
printf(m, igmpstat.f, plural(igmpstat.f)) |
printf(m, igmpstat.f, plural(igmpstat.f)) |
#define py(f, m) if (igmpstat.f || sflag <= 1) \ |
#define py(f, m) if (igmpstat.f || sflag <= 1) \ |
|
|
* Dump PIM statistics structure. |
* Dump PIM statistics structure. |
*/ |
*/ |
void |
void |
pim_stats(u_long off, char *name) |
pim_stats(char *name) |
{ |
{ |
struct pimstat pimstat; |
struct pimstat pimstat; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_PIM, PIMCTL_STATS }; |
|
size_t len = sizeof(pimstat); |
|
|
if (off == 0) |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
|
&pimstat, &len, NULL, 0) == -1) { |
|
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
if (kread(off, &pimstat, sizeof (pimstat)) != 0) { |
|
/* XXX: PIM is probably not enabled in the kernel */ |
|
return; |
|
} |
} |
|
|
printf("%s:\n", name); |
printf("%s:\n", name); |
|
|
#define p(f, m) if (pimstat.f || sflag <= 1) \ |
#define p(f, m) if (pimstat.f || sflag <= 1) \ |
printf(m, pimstat.f, plural(pimstat.f)) |
printf(m, pimstat.f, plural(pimstat.f)) |
#define py(f, m) if (pimstat.f || sflag <= 1) \ |
#define py(f, m) if (pimstat.f || sflag <= 1) \ |
|
|
* Dump AH statistics structure. |
* Dump AH statistics structure. |
*/ |
*/ |
void |
void |
ah_stats(u_long off, char *name) |
ah_stats(char *name) |
{ |
{ |
struct ahstat ahstat; |
struct ahstat ahstat; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_AH, AHCTL_STATS }; |
|
size_t len = sizeof(ahstat); |
|
|
if (off == 0) |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
|
&ahstat, &len, NULL, 0) == -1) { |
|
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
kread(off, &ahstat, sizeof (ahstat)); |
} |
printf("%s:\n", name); |
|
|
|
|
printf("%s:\n", name); |
#define p(f, m) if (ahstat.f || sflag <= 1) \ |
#define p(f, m) if (ahstat.f || sflag <= 1) \ |
printf(m, ahstat.f, plural(ahstat.f)) |
printf(m, ahstat.f, plural(ahstat.f)) |
#define p1(f, m) if (ahstat.f || sflag <= 1) \ |
#define p1(f, m) if (ahstat.f || sflag <= 1) \ |
|
|
* Dump etherip statistics structure. |
* Dump etherip statistics structure. |
*/ |
*/ |
void |
void |
etherip_stats(u_long off, char *name) |
etherip_stats(char *name) |
{ |
{ |
struct etheripstat etheripstat; |
struct etheripstat etheripstat; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_ETHERIP, ETHERIPCTL_STATS }; |
|
size_t len = sizeof(etheripstat); |
|
|
if (off == 0) |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
|
ðeripstat, &len, NULL, 0) == -1) { |
|
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
kread(off, ðeripstat, sizeof (etheripstat)); |
} |
printf("%s:\n", name); |
|
|
|
|
printf("%s:\n", name); |
#define p(f, m) if (etheripstat.f || sflag <= 1) \ |
#define p(f, m) if (etheripstat.f || sflag <= 1) \ |
printf(m, etheripstat.f, plural(etheripstat.f)) |
printf(m, etheripstat.f, plural(etheripstat.f)) |
|
|
|
|
* Dump ESP statistics structure. |
* Dump ESP statistics structure. |
*/ |
*/ |
void |
void |
esp_stats(u_long off, char *name) |
esp_stats(char *name) |
{ |
{ |
struct espstat espstat; |
struct espstat espstat; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_ESP, ESPCTL_STATS }; |
|
size_t len = sizeof(espstat); |
|
|
if (off == 0) |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
|
&espstat, &len, NULL, 0) == -1) { |
|
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
kread(off, &espstat, sizeof (espstat)); |
} |
printf("%s:\n", name); |
|
|
|
|
printf("%s:\n", name); |
#define p(f, m) if (espstat.f || sflag <= 1) \ |
#define p(f, m) if (espstat.f || sflag <= 1) \ |
printf(m, espstat.f, plural(espstat.f)) |
printf(m, espstat.f, plural(espstat.f)) |
|
|
|
|
* Dump IP-in-IP statistics structure. |
* Dump IP-in-IP statistics structure. |
*/ |
*/ |
void |
void |
ipip_stats(u_long off, char *name) |
ipip_stats(char *name) |
{ |
{ |
struct ipipstat ipipstat; |
struct ipipstat ipipstat; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_IPIP, IPIPCTL_STATS }; |
|
size_t len = sizeof(ipipstat); |
|
|
if (off == 0) |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
|
&ipipstat, &len, NULL, 0) == -1) { |
|
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
kread(off, &ipipstat, sizeof (ipipstat)); |
} |
printf("%s:\n", name); |
|
|
|
|
printf("%s:\n", name); |
#define p(f, m) if (ipipstat.f || sflag <= 1) \ |
#define p(f, m) if (ipipstat.f || sflag <= 1) \ |
printf(m, ipipstat.f, plural(ipipstat.f)) |
printf(m, ipipstat.f, plural(ipipstat.f)) |
|
|
|
|
* Dump CARP statistics structure. |
* Dump CARP statistics structure. |
*/ |
*/ |
void |
void |
carp_stats(u_long off, char *name) |
carp_stats(char *name) |
{ |
{ |
struct carpstats carpstat; |
struct carpstats carpstat; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_CARP, CARPCTL_STATS }; |
|
size_t len = sizeof(carpstat); |
|
|
if (off == 0) |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
|
&carpstat, &len, NULL, 0) == -1) { |
|
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
kread(off, &carpstat, sizeof(carpstat)); |
} |
printf("%s:\n", name); |
|
|
|
|
printf("%s:\n", name); |
#define p(f, m) if (carpstat.f || sflag <= 1) \ |
#define p(f, m) if (carpstat.f || sflag <= 1) \ |
printf(m, carpstat.f, plural(carpstat.f)) |
printf(m, carpstat.f, plural(carpstat.f)) |
#define p2(f, m) if (carpstat.f || sflag <= 1) \ |
#define p2(f, m) if (carpstat.f || sflag <= 1) \ |
|
|
* Dump pfsync statistics structure. |
* Dump pfsync statistics structure. |
*/ |
*/ |
void |
void |
pfsync_stats(u_long off, char *name) |
pfsync_stats(char *name) |
{ |
{ |
struct pfsyncstats pfsyncstat; |
struct pfsyncstats pfsyncstat; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_PFSYNC, PFSYNCCTL_STATS }; |
|
size_t len = sizeof(pfsyncstat); |
|
|
if (off == 0) |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
|
&pfsyncstat, &len, NULL, 0) == -1) { |
|
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
kread(off, &pfsyncstat, sizeof(pfsyncstat)); |
} |
printf("%s:\n", name); |
|
|
|
|
printf("%s:\n", name); |
#define p(f, m) if (pfsyncstat.f || sflag <= 1) \ |
#define p(f, m) if (pfsyncstat.f || sflag <= 1) \ |
printf(m, pfsyncstat.f, plural(pfsyncstat.f)) |
printf(m, pfsyncstat.f, plural(pfsyncstat.f)) |
#define p2(f, m) if (pfsyncstat.f || sflag <= 1) \ |
#define p2(f, m) if (pfsyncstat.f || sflag <= 1) \ |
|
|
* Dump IPCOMP statistics structure. |
* Dump IPCOMP statistics structure. |
*/ |
*/ |
void |
void |
ipcomp_stats(u_long off, char *name) |
ipcomp_stats(char *name) |
{ |
{ |
struct ipcompstat ipcompstat; |
struct ipcompstat ipcompstat; |
|
int mib[] = { CTL_NET, AF_INET, IPPROTO_IPCOMP, IPCOMPCTL_STATS }; |
|
size_t len = sizeof(ipcompstat); |
|
|
if (off == 0) |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
|
&ipcompstat, &len, NULL, 0) == -1) { |
|
if (errno != ENOPROTOOPT) |
|
warn(name); |
return; |
return; |
kread(off, &ipcompstat, sizeof (ipcompstat)); |
} |
printf("%s:\n", name); |
|
|
|
|
printf("%s:\n", name); |
#define p(f, m) if (ipcompstat.f || sflag <= 1) \ |
#define p(f, m) if (ipcompstat.f || sflag <= 1) \ |
printf(m, ipcompstat.f, plural(ipcompstat.f)) |
printf(m, ipcompstat.f, plural(ipcompstat.f)) |
|
|