Xinqi Bao's Git
c846a2caf325316060da02183b7af2a19aa7e8d6
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
;
69 if (getifaddrs(&ifal
) == -1) {
70 warn("getifaddrs failed");
73 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
74 if (!strcmp(ifa
->ifa_name
, interface
) &&
75 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
76 rxbytes
+= ifd
->ifi_ibytes
, if_ok
= 1;
81 warn("reading 'if_data' failed");
85 rxs
= oldrxbytes
? fmt_scaled((rxbytes
- oldrxbytes
) /
86 interval
* 1000) : NULL
;
87 return (oldrxbytes
= rxbytes
, rxs
);
91 netspeed_tx(const char *interface
)
93 struct ifaddrs
*ifal
, *ifa
;
95 static uint64_t oldtxbytes
;
98 extern const unsigned int interval
;
101 if (getifaddrs(&ifal
) == -1) {
102 warn("getifaddrs failed");
105 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
106 if (!strcmp(ifa
->ifa_name
, interface
) &&
107 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
108 txbytes
+= ifd
->ifi_obytes
, if_ok
= 1;
113 warn("reading 'if_data' failed");
117 txs
= oldtxbytes
? fmt_scaled((txbytes
- oldtxbytes
) /
118 interval
* 1000) : NULL
;
119 return (oldtxbytes
= txbytes
, txs
);