Xinqi Bao's Git
5af5a9476acd5dfa930947a8ece51c7ef3e83f65
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 snprintf(path
, sizeof(path
),
21 "/sys/class/net/%s/statistics/rx_bytes", interface
);
22 if (pscanf(path
, "%llu", &rxbytes
) != 1) {
26 return oldrxbytes
? fmt_scaled((rxbytes
- oldrxbytes
) /
27 interval
* 1000) : NULL
;
31 netspeed_tx(const char *interface
)
34 static uint64_t txbytes
= 0;
35 extern const unsigned int interval
;
40 snprintf(path
, sizeof(path
),
41 "/sys/class/net/%s/statistics/tx_bytes", interface
);
42 if (pscanf(path
, "%llu", &txbytes
) != 1) {
46 return oldtxbytes
? fmt_scaled((txbytes
- oldtxbytes
) /
47 interval
* 1000) : NULL
;
49 #elif defined(__OpenBSD__)
52 #include <sys/types.h>
53 #include <sys/socket.h>
57 netspeed_rx(const char *interface
)
59 struct ifaddrs
*ifal
, *ifa
;
62 static uint64_t rxbytes
= 0;
63 extern const unsigned int interval
;
66 if (getifaddrs(&ifal
) == -1) {
67 warn("getifaddrs failed");
71 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
72 if (!strcmp(ifa
->ifa_name
, interface
) &&
73 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
74 rxbytes
+= ifd
->ifi_ibytes
, if_ok
= 1;
79 warn("reading 'if_data' failed");
83 return oldrxbytes
? fmt_scaled((rxbytes
- oldrxbytes
) /
84 interval
* 1000) : NULL
;
88 netspeed_tx(const char *interface
)
90 struct ifaddrs
*ifal
, *ifa
;
93 static uint64_t txbytes
= 0;
94 extern const unsigned int interval
;
97 if (getifaddrs(&ifal
) == -1) {
98 warn("getifaddrs failed");
101 oldtxbytes
= txbytes
;
102 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
103 if (!strcmp(ifa
->ifa_name
, interface
) &&
104 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
105 txbytes
+= ifd
->ifi_obytes
, if_ok
= 1;
110 warn("reading 'if_data' failed");
114 return oldtxbytes
? fmt_scaled((txbytes
- oldtxbytes
) /
115 interval
* 1000) : NULL
;