Guest User

Untitled

a guest
Jan 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <sys/ioctl.h>
  2. #include <net/if.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <arpa/inet.h>
  6.  
  7. int main()
  8. {
  9. char buf[1024];
  10. struct ifconf ifc;
  11. struct ifreq *ifr;
  12. int sck;
  13. int count;
  14. int i;
  15.  
  16. sck = socket(PF_INET, SOCK_DGRAM, 0);
  17. if (sck < 0)
  18. {
  19. perror("socket");
  20. return 1;
  21. }
  22.  
  23. ifc.ifc_len = sizeof(buf);
  24. ifc.ifc_buf = buf;
  25. if (ioctl(sck, SIOCGIFCONF, &ifc) < 0)
  26. {
  27. perror("ioerror");
  28. return 1;
  29. }
  30.  
  31. ifr = ifc.ifc_req;
  32. count = ifc.ifc_len / sizeof(struct ifreq);
  33.  
  34. for (i = 0; i < count; i++)
  35. {
  36. struct ifreq *item = &ifr[i];
  37. fprintf(stdout, "name: %s\n", item->ifr_ifrn.ifrn_name);
  38. fprintf(stdout, "hwaddr: %02x:%02x:%02x:%02x:%02x:%02x\n",
  39. (unsigned char)item->ifr_hwaddr.sa_data[0],
  40. (unsigned char)item->ifr_hwaddr.sa_data[1],
  41. (unsigned char)item->ifr_hwaddr.sa_data[2],
  42. (unsigned char)item->ifr_hwaddr.sa_data[3],
  43. (unsigned char)item->ifr_hwaddr.sa_data[4],
  44. (unsigned char)item->ifr_hwaddr.sa_data[5]);
  45. }
  46. }
Add Comment
Please, Sign In to add comment