pcap_nametoindex.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * unsigned pcap_nametoindex (char const * name);
  11. *
  12. * ether.h
  13. *
  14. * a WinPcap version of POSIX if_nametoindex function; return error
  15. * in non-pcap environments;
  16. *
  17. * see The Open Group Base Specifications Issue 6 IEEE Std 1003.1,
  18. * 2004 Edition for a description of this function;
  19. *
  20. *--------------------------------------------------------------------*/
  21. #ifndef PCAP_NAMETOINDEX_SOURCE
  22. #define PCAP_NAMETOINDEX_SOURCE
  23. #include <pcap.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include "../ether/ether.h"
  27. unsigned pcap_nametoindex (char const * name)
  28. {
  29. #if defined (WINPCAP) || defined (LIBPCAP)
  30. char buffer [PCAP_ERRBUF_SIZE];
  31. pcap_if_t * devices = (pcap_if_t *) (0);
  32. pcap_if_t * device;
  33. if (pcap_findalldevs (& devices, buffer) != - 1)
  34. {
  35. unsigned index = 1;
  36. for (device = devices; device; device = device->next)
  37. {
  38. if (! strcmp (name, device->name))
  39. {
  40. index++;
  41. continue;
  42. }
  43. pcap_freealldevs (devices);
  44. return (index);
  45. }
  46. pcap_freealldevs (devices);
  47. }
  48. #endif
  49. #if defined (__APPLE__)
  50. errno = ENXIO;
  51. #endif
  52. return (0);
  53. }
  54. #endif