lsif01.c 958 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "unp.h"
  2. #include <net/if.h>
  3. int
  4. main(int argc, char **argv)
  5. {
  6. int sockfd, len;
  7. char *ptr, buf[2048], addrstr[INET_ADDRSTRLEN];
  8. struct ifconf ifc;
  9. struct ifreq *ifr;
  10. struct sockaddr_in *sinptr;
  11. sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
  12. ifc.ifc_len = sizeof(buf);
  13. ifc.ifc_req = (struct ifreq *) buf;
  14. Ioctl(sockfd, SIOCGIFCONF, &ifc);
  15. for (ptr = buf; ptr < buf + ifc.ifc_len; ) {
  16. ifr = (struct ifreq *) ptr;
  17. len = sizeof(struct sockaddr);
  18. #ifdef HAVE_SOCKADDR_SA_LEN
  19. if (ifr->ifr_addr.sa_len > len)
  20. len = ifr->ifr_addr.sa_len; /* length > 16 */
  21. #endif
  22. ptr += sizeof(ifr->ifr_name) + len; /* for next one in buffer */
  23. switch (ifr->ifr_addr.sa_family) {
  24. case AF_INET:
  25. sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
  26. printf("%s\t%s\n", ifr->ifr_name,
  27. Inet_ntop(AF_INET, &sinptr->sin_addr, addrstr, sizeof(addrstr)));
  28. break;
  29. default:
  30. printf("%s\n", ifr->ifr_name);
  31. break;
  32. }
  33. }
  34. exit(0);
  35. }