pcap_indextoname.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * char * pcap_indextoname (unsigned ifindex, char * ifname);
  11. *
  12. * ether.h
  13. *
  14. * a WinPcap version of POSIX if_indextoname function; return error
  15. * in non-pcap environments; set errno to ENXIO on error on MacOSX;
  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_INDEXTONAME_SOURCE
  22. #define PCAP_INDEXTONAME_SOURCE
  23. #include <pcap.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include "../ether/ether.h"
  27. char * pcap_indextoname (unsigned ifindex, char * ifname)
  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 ((index--) && (pcap_findalldevs (& devices, buffer) != - 1))
  34. {
  35. for (device = devices; device; device = device->next)
  36. {
  37. if (! index--)
  38. {
  39. memcpy (ifname, device->name, strlen (device->name));
  40. pcap_freealldevs (devices);
  41. return (ifname);
  42. }
  43. }
  44. pcap_freealldevs (devices);
  45. }
  46. #endif
  47. #if defined (__APPLE__)
  48. errno = ENXIO;
  49. #endif
  50. return ((char *) (0));
  51. }
  52. #endif