getifname.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * char * getifname (signed number);
  11. *
  12. * ether.h
  13. *
  14. * return the PCAP interface name for a given interface number; this
  15. * function is only needed when using LIBPCAP or WINPCAP libraries;
  16. *
  17. * Contributor(s):
  18. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  19. * Charles Maier <cmaier@qca.qualcomm.com>
  20. *
  21. *--------------------------------------------------------------------*/
  22. #ifndef GETIFNAME_SOURCE
  23. #define GETIFNAME_SOURCE
  24. #include <string.h>
  25. #if defined (WINPCAP) || defined (LIBPCAP)
  26. #include <pcap.h>
  27. #endif
  28. #include "../ether/ether.h"
  29. #include "../tools/error.h"
  30. char * getifname (signed index)
  31. {
  32. char * name = (char *) (0);
  33. #if defined (__linux__)
  34. #elif defined (__APPLE__) || defined (__OpenBSD__)
  35. #elif defined (WINPCAP) || defined (LIBPCAP)
  36. char buffer [PCAP_ERRBUF_SIZE];
  37. pcap_if_t * devices = (pcap_if_t *) (0);
  38. pcap_if_t * device;
  39. signed count;
  40. if (pcap_findalldevs (& devices, buffer) == - 1)
  41. {
  42. error (1, errno, "can't enumerate pcap devices");
  43. }
  44. for (device = devices, count = 1; device; device = device->next, count++)
  45. {
  46. if (count == index)
  47. {
  48. name = strdup (device->name);
  49. break;
  50. }
  51. }
  52. if (! device)
  53. {
  54. error (1, EINVAL, "invalid interface: %d", index);
  55. }
  56. pcap_freealldevs (devices);
  57. #else
  58. #error "Unknown environment"
  59. #endif
  60. return (name);
  61. }
  62. #endif