Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
9 netspeed_rx(const char *interface
)
12 static unsigned long long rxbytes
;
13 unsigned long oldrxbytes
;
14 extern const unsigned int interval
;
18 snprintf(path
, sizeof(path
), "/sys/class/net/%s/statistics/rx_bytes", interface
);
19 if (pscanf(path
, "%llu", &rxbytes
) != 1) {
27 return fmt_scaled((rxbytes
- oldrxbytes
) / interval
* 1000);
31 netspeed_tx(const char *interface
)
34 static unsigned long long txbytes
;
35 unsigned long oldtxbytes
;
36 extern const unsigned int interval
;
40 snprintf(path
, sizeof(path
), "/sys/class/net/%s/statistics/tx_bytes", interface
);
41 if (pscanf(path
, "%llu", &txbytes
) != 1) {
49 return fmt_scaled((txbytes
- oldtxbytes
) / interval
* 1000);
51 #elif defined(__OpenBSD__)
54 #include <sys/types.h>
55 #include <sys/socket.h>
59 netspeed_rx(const char *interface
)
61 struct ifaddrs
*ifal
, *ifa
;
63 static uint64_t oldrxbytes
;
66 extern const unsigned int interval
;
68 if (getifaddrs(&ifal
) == -1) {
69 warn("getifaddrs failed");
72 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
73 if (!strcmp(ifa
->ifa_name
, interface
) &&
74 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
75 rxbytes
+= ifd
->ifi_ibytes
;
80 rxs
= oldrxbytes
? fmt_scaled((rxbytes
- oldrxbytes
) /
81 interval
* 1000) : NULL
;
82 return (oldrxbytes
= rxbytes
, rxs
);
86 netspeed_tx(const char *interface
)
88 struct ifaddrs
*ifal
, *ifa
;
90 static uint64_t oldtxbytes
;
93 extern const unsigned int interval
;
95 if (getifaddrs(&ifal
) == -1) {
96 warn("getifaddrs failed");
99 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
100 if (!strcmp(ifa
->ifa_name
, interface
) &&
101 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
102 txbytes
+= ifd
->ifi_obytes
;
107 txs
= oldtxbytes
? fmt_scaled((txbytes
- oldtxbytes
) /
108 interval
* 1000) : NULL
;
109 return (oldtxbytes
= txbytes
, txs
);