netspeeds.c 2.7 KB

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