My slstatus configuration
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ip.c 1.1 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. static const char *
  13. ip(const char *iface, unsigned short sa_family)
  14. {
  15. struct ifaddrs *ifaddr, *ifa;
  16. int s;
  17. char host[NI_MAXHOST];
  18. if (getifaddrs(&ifaddr) < 0) {
  19. warn("getifaddrs:");
  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_in6),
  27. host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  28. if (!strcmp(ifa->ifa_name, iface) &&
  29. (ifa->ifa_addr->sa_family == sa_family)) {
  30. freeifaddrs(ifaddr);
  31. if (s != 0) {
  32. warn("getnameinfo: %s", gai_strerror(s));
  33. return NULL;
  34. }
  35. return bprintf("%s", host);
  36. }
  37. }
  38. freeifaddrs(ifaddr);
  39. return NULL;
  40. }
  41. const char *
  42. ipv4(const char *iface)
  43. {
  44. return ip(iface, AF_INET);
  45. }
  46. const char *
  47. ipv6(const char *iface)
  48. {
  49. return ip(iface, AF_INET6);
  50. }