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. #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. oldrxbytes = rxbytes;
  55. if (getifaddrs(&ifal) == -1) {
  56. warn("getifaddrs failed");
  57. return NULL;
  58. }
  59. rxbytes = 0;
  60. for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
  61. if (!strcmp(ifa->ifa_name, interface) &&
  62. (ifd = (struct if_data *)ifa->ifa_data)) {
  63. rxbytes += ifd->ifi_ibytes, if_ok = 1;
  64. }
  65. }
  66. freeifaddrs(ifal);
  67. if (!if_ok) {
  68. warn("reading 'if_data' failed");
  69. return NULL;
  70. }
  71. return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
  72. interval * 1000) : NULL;
  73. }
  74. const char *
  75. netspeed_tx(const char *interface)
  76. {
  77. struct ifaddrs *ifal, *ifa;
  78. struct if_data *ifd;
  79. uint64_t oldtxbytes;
  80. static uint64_t txbytes = 0;
  81. extern const unsigned int interval;
  82. char if_ok = 0;
  83. oldtxbytes = txbytes;
  84. if (getifaddrs(&ifal) == -1) {
  85. warn("getifaddrs failed");
  86. return NULL;
  87. }
  88. txbytes = 0;
  89. for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
  90. if (!strcmp(ifa->ifa_name, interface) &&
  91. (ifd = (struct if_data *)ifa->ifa_data)) {
  92. txbytes += ifd->ifi_obytes, if_ok = 1;
  93. }
  94. }
  95. freeifaddrs(ifal);
  96. if (!if_ok) {
  97. warn("reading 'if_data' failed");
  98. return NULL;
  99. }
  100. return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
  101. interval * 1000) : NULL;
  102. }
  103. #endif