netspeeds.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. uintmax_t oldrxbytes;
  11. static uintmax_t rxbytes;
  12. extern const unsigned int interval;
  13. char path[PATH_MAX];
  14. oldrxbytes = rxbytes;
  15. if (esnprintf(path, sizeof(path),
  16. "/sys/class/net/%s/statistics/rx_bytes",
  17. interface) < 0) {
  18. return NULL;
  19. }
  20. if (pscanf(path, "%ju", &rxbytes) != 1) {
  21. return NULL;
  22. }
  23. if (oldrxbytes == 0) {
  24. return NULL;
  25. }
  26. return fmt_human((rxbytes - oldrxbytes) * 1000 / interval,
  27. 1024);
  28. }
  29. const char *
  30. netspeed_tx(const char *interface)
  31. {
  32. uintmax_t oldtxbytes;
  33. static uintmax_t txbytes;
  34. extern const unsigned int interval;
  35. char path[PATH_MAX];
  36. oldtxbytes = txbytes;
  37. if (esnprintf(path, sizeof(path),
  38. "/sys/class/net/%s/statistics/tx_bytes",
  39. interface) < 0) {
  40. return NULL;
  41. }
  42. if (pscanf(path, "%ju", &txbytes) != 1) {
  43. return NULL;
  44. }
  45. if (oldtxbytes == 0) {
  46. return NULL;
  47. }
  48. return fmt_human((txbytes - oldtxbytes) * 1000 / interval,
  49. 1024);
  50. }
  51. #elif defined(__OpenBSD__) | defined(__FreeBSD__)
  52. #include <string.h>
  53. #include <ifaddrs.h>
  54. #include <sys/types.h>
  55. #include <sys/socket.h>
  56. #include <net/if.h>
  57. const char *
  58. netspeed_rx(const char *interface)
  59. {
  60. struct ifaddrs *ifal, *ifa;
  61. struct if_data *ifd;
  62. uintmax_t oldrxbytes;
  63. static uintmax_t rxbytes;
  64. extern const unsigned int interval;
  65. int if_ok = 0;
  66. oldrxbytes = rxbytes;
  67. if (getifaddrs(&ifal) == -1) {
  68. warn("getifaddrs failed");
  69. return NULL;
  70. }
  71. rxbytes = 0;
  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, if_ok = 1;
  76. }
  77. }
  78. freeifaddrs(ifal);
  79. if (!if_ok) {
  80. warn("reading 'if_data' failed");
  81. return NULL;
  82. }
  83. if (oldrxbytes == 0) {
  84. return NULL;
  85. }
  86. return fmt_human((rxbytes - oldrxbytes) * 1000 / interval,
  87. 1024);
  88. }
  89. const char *
  90. netspeed_tx(const char *interface)
  91. {
  92. struct ifaddrs *ifal, *ifa;
  93. struct if_data *ifd;
  94. uintmax_t oldtxbytes;
  95. static uintmax_t txbytes;
  96. extern const unsigned int interval;
  97. int if_ok = 0;
  98. oldtxbytes = txbytes;
  99. if (getifaddrs(&ifal) == -1) {
  100. warn("getifaddrs failed");
  101. return NULL;
  102. }
  103. txbytes = 0;
  104. for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
  105. if (!strcmp(ifa->ifa_name, interface) &&
  106. (ifd = (struct if_data *)ifa->ifa_data)) {
  107. txbytes += ifd->ifi_obytes, if_ok = 1;
  108. }
  109. }
  110. freeifaddrs(ifal);
  111. if (!if_ok) {
  112. warn("reading 'if_data' failed");
  113. return NULL;
  114. }
  115. if (oldtxbytes == 0) {
  116. return NULL;
  117. }
  118. return fmt_human((txbytes - oldtxbytes) * 1000 / interval,
  119. 1024);
  120. }
  121. #endif