My slstatus configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
1.5 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #if defined(__linux__)
  3. #include <errno.h>
  4. #include <ifaddrs.h>
  5. #include <netdb.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "../util.h"
  9. const char *
  10. ipv4(const char *iface)
  11. {
  12. struct ifaddrs *ifaddr, *ifa;
  13. int s;
  14. char host[NI_MAXHOST];
  15. if (getifaddrs(&ifaddr) == -1) {
  16. fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
  17. return NULL;
  18. }
  19. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  20. if (ifa->ifa_addr == NULL) {
  21. continue;
  22. }
  23. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  24. if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
  25. if (s != 0) {
  26. fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
  27. return NULL;
  28. }
  29. return bprintf("%s", host);
  30. }
  31. }
  32. freeifaddrs(ifaddr);
  33. return NULL;
  34. }
  35. const char *
  36. ipv6(const char *iface)
  37. {
  38. struct ifaddrs *ifaddr, *ifa;
  39. int s;
  40. char host[NI_MAXHOST];
  41. if (getifaddrs(&ifaddr) == -1) {
  42. fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
  43. return NULL;
  44. }
  45. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  46. if (ifa->ifa_addr == NULL) {
  47. continue;
  48. }
  49. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  50. if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET6)) {
  51. if (s != 0) {
  52. fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
  53. return NULL;
  54. }
  55. return bprintf("%s", host);
  56. }
  57. }
  58. freeifaddrs(ifaddr);
  59. return NULL;
  60. }
  61. #endif