netspeeds.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include "../util.h"
  5. #if defined(__linux__)
  6. #include <stdint.h>
  7. const char *
  8. netspeed_rx(const char *interface)
  9. {
  10. uint64_t oldrxbytes;
  11. static uint64_t rxbytes = 0;
  12. extern const unsigned int interval;
  13. char path[PATH_MAX];
  14. oldrxbytes = rxbytes;
  15. snprintf(path, sizeof(path),
  16. "/sys/class/net/%s/statistics/rx_bytes", interface);
  17. if (pscanf(path, "%llu", &rxbytes) != 1) {
  18. return NULL;
  19. }
  20. return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
  21. interval * 1000) : NULL;
  22. }
  23. const char *
  24. netspeed_tx(const char *interface)
  25. {
  26. uint64_t oldtxbytes;
  27. static uint64_t txbytes = 0;
  28. extern const unsigned int interval;
  29. char path[PATH_MAX];
  30. oldtxbytes = txbytes;
  31. snprintf(path, sizeof(path),
  32. "/sys/class/net/%s/statistics/tx_bytes", interface);
  33. if (pscanf(path, "%llu", &txbytes) != 1) {
  34. return NULL;
  35. }
  36. return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
  37. interval * 1000) : NULL;
  38. }
  39. #elif defined(__OpenBSD__)
  40. #include <string.h>
  41. #include <ifaddrs.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <net/if.h>
  45. const char *
  46. netspeed_rx(const char *interface)
  47. {
  48. struct ifaddrs *ifal, *ifa;
  49. struct if_data *ifd;
  50. uint64_t oldrxbytes;
  51. static uint64_t rxbytes = 0;
  52. extern const unsigned int interval;
  53. char if_ok = 0;
  54. if (getifaddrs(&ifal) == -1) {
  55. warn("getifaddrs failed");
  56. return NULL;
  57. }
  58. oldrxbytes = rxbytes;
  59. for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
  60. if (!strcmp(ifa->ifa_name, interface) &&
  61. (ifd = (struct if_data *)ifa->ifa_data)) {
  62. rxbytes += ifd->ifi_ibytes, if_ok = 1;
  63. }
  64. }
  65. freeifaddrs(ifal);
  66. if (!if_ok) {
  67. warn("reading 'if_data' failed");
  68. return NULL;
  69. }
  70. return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
  71. interval * 1000) : NULL;
  72. }
  73. const char *
  74. netspeed_tx(const char *interface)
  75. {
  76. struct ifaddrs *ifal, *ifa;
  77. struct if_data *ifd;
  78. uint64_t oldtxbytes;
  79. static uint64_t txbytes = 0;
  80. extern const unsigned int interval;
  81. char if_ok = 0;
  82. if (getifaddrs(&ifal) == -1) {
  83. warn("getifaddrs failed");
  84. return NULL;
  85. }
  86. oldtxbytes = txbytes;
  87. for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
  88. if (!strcmp(ifa->ifa_name, interface) &&
  89. (ifd = (struct if_data *)ifa->ifa_data)) {
  90. txbytes += ifd->ifi_obytes, if_ok = 1;
  91. }
  92. }
  93. freeifaddrs(ifal);
  94. if (!if_ok) {
  95. warn("reading 'if_data' failed");
  96. return NULL;
  97. }
  98. return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
  99. interval * 1000) : NULL;
  100. }
  101. #endif