Xinqi Bao's Git
32e78d6409092961487c808bbbf21c13669d202d
1 /* See LICENSE file for copyright and license details. */
11 netspeed_rx(const char *interface
)
14 static uint64_t rxbytes
= 0;
15 extern const unsigned int interval
;
20 if (esnprintf(path
, sizeof(path
),
21 "/sys/class/net/%s/statistics/rx_bytes",
25 if (pscanf(path
, "%llu", &rxbytes
) != 1) {
29 return oldrxbytes
? fmt_scaled((rxbytes
- oldrxbytes
) *
30 1000 / interval
) : NULL
;
34 netspeed_tx(const char *interface
)
37 static uint64_t txbytes
= 0;
38 extern const unsigned int interval
;
43 if (esnprintf(path
, sizeof(path
),
44 "/sys/class/net/%s/statistics/tx_bytes",
48 if (pscanf(path
, "%llu", &txbytes
) != 1) {
52 return oldtxbytes
? fmt_scaled((txbytes
- oldtxbytes
) *
53 1000 / interval
) : NULL
;
55 #elif defined(__OpenBSD__)
58 #include <sys/types.h>
59 #include <sys/socket.h>
63 netspeed_rx(const char *interface
)
65 struct ifaddrs
*ifal
, *ifa
;
68 static uint64_t rxbytes
= 0;
69 extern const unsigned int interval
;
74 if (getifaddrs(&ifal
) == -1) {
75 warn("getifaddrs failed");
79 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
80 if (!strcmp(ifa
->ifa_name
, interface
) &&
81 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
82 rxbytes
+= ifd
->ifi_ibytes
, if_ok
= 1;
87 warn("reading 'if_data' failed");
91 return oldrxbytes
? fmt_scaled((rxbytes
- oldrxbytes
) *
92 1000 / interval
) : NULL
;
96 netspeed_tx(const char *interface
)
98 struct ifaddrs
*ifal
, *ifa
;
101 static uint64_t txbytes
= 0;
102 extern const unsigned int interval
;
105 oldtxbytes
= txbytes
;
107 if (getifaddrs(&ifal
) == -1) {
108 warn("getifaddrs failed");
112 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
113 if (!strcmp(ifa
->ifa_name
, interface
) &&
114 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
115 txbytes
+= ifd
->ifi_obytes
, if_ok
= 1;
120 warn("reading 'if_data' failed");
124 return oldtxbytes
? fmt_scaled((txbytes
- oldtxbytes
) *
125 1000 / interval
) : NULL
;