pcap_nameindex.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * struct if_nameindex * pcap_nameindex (void)
  11. *
  12. * ether.h
  13. *
  14. * a WinPcap version of POSIX if_nameindex 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_NAMEINDEX_SOURCE
  22. #define PCAP_NAMEINDEX_SOURCE
  23. #include <pcap.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "../ether/ether.h"
  27. struct if_nameindex * pcap_nameindex (void)
  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. struct if_nameindex * ifs;
  36. struct if_nameindex * ifp;
  37. unsigned count = 1;
  38. for (device = devices; device; device = device->next)
  39. {
  40. count++;
  41. }
  42. ifp = ifs = (struct if_nameindex *) (malloc (count * sizeof (struct if_nameindex)));
  43. if (ifs) for (device = devices; device; device = device->next)
  44. {
  45. ifp->if_index = device->index;
  46. ifp->if_name = strdup (device->name);
  47. ifp++;
  48. }
  49. memset (ifp, 0, sizeof (* ifp));
  50. pcap_freealldevs (devices);
  51. return (ifs);
  52. }
  53. #endif
  54. return ((struct if_nameindex *) (0));
  55. }
  56. #endif