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.
 
 
 
 

79 lines
1.6 KiB

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