macaddr.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * macaddr
  3. *
  4. * Program to return the MAC address of an Ethernet
  5. * adapter. This was written to help configure the
  6. * adapter based on the MAC address rather than the
  7. * name.
  8. *
  9. * Version 1.0 Eric Dittman 2001-10-19
  10. *
  11. * This is released unther the GPL license.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include "iwlib.h"
  21. int main(int argc, char** argv)
  22. {
  23. int devsock;
  24. struct ifreq ifbuffer;
  25. char buf[20];
  26. if ((argc != 2) || (argv[1][0] == '-')) {
  27. printf("Usage: macaddr interface\n");
  28. exit(1);
  29. }
  30. devsock = socket(AF_INET, SOCK_STREAM, 0);
  31. if (devsock == -1) {
  32. perror("Failed opening socket");
  33. exit (1);
  34. }
  35. memset(&ifbuffer, 0, sizeof(ifbuffer));
  36. strncpy(ifbuffer.ifr_name, argv[1], sizeof(ifbuffer.ifr_name));
  37. if (ioctl(devsock, SIOCGIFHWADDR, &ifbuffer) == -1) {
  38. fprintf(stderr, "There is no MACADDR for %s\n", argv[1]);
  39. exit(1);
  40. }
  41. close(devsock);
  42. puts(iw_saether_ntop(&ifbuffer.ifr_ifru.ifru_hwaddr, buf));
  43. exit(0);
  44. }