diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2015-09-11 17:20:03 +0000 | 
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2015-09-11 17:20:03 +0000 | 
| commit | 0e1e5c22c20e636264ff1284083c6af7a1b282cb (patch) | |
| tree | 09add9017de3c98451b1eaf85a24b608cf228e4b /usr.bin/netstat/inet.c | |
| parent | 6160f3cfcac58513eef46e925b0bf15681e88512 (diff) | |
| parent | 92392e798859a0d87094d6c0b22afee48fde7881 (diff) | |
Notes
Diffstat (limited to 'usr.bin/netstat/inet.c')
| -rw-r--r-- | usr.bin/netstat/inet.c | 141 | 
1 files changed, 33 insertions, 108 deletions
| diff --git a/usr.bin/netstat/inet.c b/usr.bin/netstat/inet.c index 4c3675a78c23..5644af33b366 100644 --- a/usr.bin/netstat/inet.c +++ b/usr.bin/netstat/inet.c @@ -627,8 +627,7 @@ protopr(u_long off, const char *name, int af1, int proto)  void  tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  { -	struct tcpstat tcpstat, zerostat; -	size_t len = sizeof tcpstat; +	struct tcpstat tcpstat;  #ifdef INET6  	if (tcp_done != 0) @@ -637,16 +636,9 @@ tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  		tcp_done = 1;  #endif -	if (live) { -		if (zflag) -			memset(&zerostat, 0, len); -		if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len, -		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { -			xo_warn("sysctl: net.inet.tcp.stats"); -			return; -		} -	} else -		kread_counters(off, &tcpstat, len); +	if (fetch_stats("net.inet.tcp.stats", off, &tcpstat, +	    sizeof(tcpstat), kread_counters) != 0) +		return;  	xo_open_container("tcp");  	xo_emit("{T:/%s}:\n", name); @@ -860,8 +852,7 @@ tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  void  udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  { -	struct udpstat udpstat, zerostat; -	size_t len = sizeof udpstat; +	struct udpstat udpstat;  	uint64_t delivered;  #ifdef INET6 @@ -871,16 +862,9 @@ udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  		udp_done = 1;  #endif -	if (live) { -		if (zflag) -			memset(&zerostat, 0, len); -		if (sysctlbyname("net.inet.udp.stats", &udpstat, &len, -		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { -			xo_warn("sysctl: net.inet.udp.stats"); -			return; -		} -	} else -		kread_counters(off, &udpstat, len); +	if (fetch_stats("net.inet.udp.stats", off, &udpstat, +	    sizeof(udpstat), kread_counters) != 0) +		return;  	xo_open_container("udp");  	xo_emit("{T:/%s}:\n", name); @@ -933,23 +917,11 @@ udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  void  carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  { -	struct carpstats carpstat, zerostat; -	size_t len = sizeof(struct carpstats); +	struct carpstats carpstat; -	if (live) { -		if (zflag) -			memset(&zerostat, 0, len); -		if (sysctlbyname("net.inet.carp.stats", &carpstat, &len, -		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { -			if (errno != ENOENT) -				xo_warn("sysctl: net.inet.carp.stats"); -			return; -		} -	} else { -		if (off == 0) -			return; -		kread_counters(off, &carpstat, len); -	} +	if (fetch_stats("net.inet.carp.stats", off, &carpstat, +	    sizeof(carpstat), kread_counters) != 0) +		return;  	xo_open_container(name);  	xo_emit("{T:/%s}:\n", name); @@ -1000,19 +972,11 @@ carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  void  ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  { -	struct ipstat ipstat, zerostat; -	size_t len = sizeof ipstat; +	struct ipstat ipstat; -	if (live) { -		if (zflag) -			memset(&zerostat, 0, len); -		if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, -		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { -			xo_warn("sysctl: net.inet.ip.stats"); -			return; -		} -	} else -		kread_counters(off, &ipstat, len); +	if (fetch_stats("net.inet.ip.stats", off, &ipstat, +	    sizeof(ipstat), kread_counters) != 0) +		return;  	xo_open_container(name);  	xo_emit("{T:/%s}:\n", name); @@ -1093,19 +1057,11 @@ ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  void  arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  { -	struct arpstat arpstat, zerostat; -	size_t len = sizeof(arpstat); +	struct arpstat arpstat; -	if (live) { -		if (zflag) -			memset(&zerostat, 0, len); -		if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, -		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { -			xo_warn("sysctl: net.link.ether.arp.stats"); -			return; -		} -	} else -		kread_counters(off, &arpstat, len); +	if (fetch_stats("net.link.ether.arp.stats", off, &arpstat, +	    sizeof(arpstat), kread_counters) != 0) +		return;  	xo_open_container(name);  	xo_emit("{T:/%s}:\n", name); @@ -1186,21 +1142,13 @@ static	const char *icmpnames[ICMP_MAXTYPE + 1] = {  void  icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  { -	struct icmpstat icmpstat, zerostat; -	int i, first; +	struct icmpstat icmpstat;  	size_t len; +	int i, first; -	len = sizeof icmpstat; -	if (live) { -		if (zflag) -			memset(&zerostat, 0, len); -		if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len, -		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { -			xo_warn("sysctl: net.inet.icmp.stats"); -			return; -		} -	} else -		kread_counters(off, &icmpstat, len); +	if (fetch_stats("net.inet.icmp.stats", off, &icmpstat, +	    sizeof(icmpstat), kread_counters) != 0) +		return;  	xo_open_container(name);  	xo_emit("{T:/%s}:\n", name); @@ -1300,22 +1248,11 @@ icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  void  igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)  { -	struct igmpstat igmpstat, zerostat; -	size_t len; +	struct igmpstat igmpstat; -	len = sizeof(igmpstat); -	if (live) { -		if (zflag) -			memset(&zerostat, 0, len); -		if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len, -		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { -			xo_warn("sysctl: net.inet.igmp.stats"); -			return; -		} -	} else { -		len = sizeof(igmpstat); -		kread(off, &igmpstat, len); -	} +	if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat, +	    sizeof(igmpstat), kread) != 0) +		return;  	if (igmpstat.igps_version != IGPS_VERSION_3) {  		xo_warnx("%s: version mismatch (%d != %d)", __func__, @@ -1380,23 +1317,11 @@ void  pim_stats(u_long off __unused, const char *name, int af1 __unused,      int proto __unused)  { -	struct pimstat pimstat, zerostat; -	size_t len = sizeof pimstat; +	struct pimstat pimstat; -	if (live) { -		if (zflag) -			memset(&zerostat, 0, len); -		if (sysctlbyname("net.inet.pim.stats", &pimstat, &len, -		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { -			if (errno != ENOENT) -				xo_warn("sysctl: net.inet.pim.stats"); -			return; -		} -	} else { -		if (off == 0) -			return; -		kread_counters(off, &pimstat, len); -	} +	if (fetch_stats("net.inet.pim.stats", off, &pimstat, +	    sizeof(pimstat), kread_counters) != 0) +		return;  	xo_open_container(name);  	xo_emit("{T:/%s}:\n", name); | 
