pcapdevs.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * pcapdevs.c - pcap device enumerator;
  11. *
  12. * Contributor(s):
  13. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  14. * Charles Maier <cmaier@qca.qualcomm.com>
  15. *
  16. *--------------------------------------------------------------------*/
  17. /*====================================================================*
  18. * system header files;
  19. *--------------------------------------------------------------------*/
  20. #include <unistd.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <pcap.h>
  24. #if defined (__linux__)
  25. #elif defined (__APPLE__)
  26. #elif defined (__OpenBSD__)
  27. # include <sys/types.h>
  28. # include <sys/socket.h>
  29. # include <net/if.h>
  30. # include <netinet/in.h>
  31. # include <netinet/if_ether.h>
  32. #elif defined (WINPCAP) || defined (LIBPCAP)
  33. #else
  34. #error "Unknown environment"
  35. #endif
  36. /*====================================================================*
  37. * custom header files;
  38. *--------------------------------------------------------------------*/
  39. #include "../tools/getoptv.h"
  40. #include "../tools/putoptv.h"
  41. #include "../tools/version.h"
  42. #include "../tools/memory.h"
  43. #include "../tools/flags.h"
  44. #include "../tools/types.h"
  45. #include "../tools/error.h"
  46. #include "../ether/ether.h"
  47. /*====================================================================*
  48. * custom source files;
  49. *--------------------------------------------------------------------*/
  50. #ifndef MAKEFILE
  51. #include "../tools/getoptv.c"
  52. #include "../tools/putoptv.c"
  53. #include "../tools/version.c"
  54. #include "../tools/error.c"
  55. #include "../tools/hexdecode.c"
  56. #endif
  57. /*====================================================================*
  58. * program constants;
  59. *--------------------------------------------------------------------*/
  60. #define PCAP_VERBOSE (1 << 0)
  61. #define PCAP_SILENCE (1 << 1)
  62. #define PCAP_DEVICES (1 << 2)
  63. #define PCAP_NICS (1 << 3)
  64. #define PCAP_MACS (1 << 4)
  65. /*====================================================================*
  66. *
  67. * void pcap_enum (flag_t flags);
  68. *
  69. * pcap_enum available pcap devices on stdout;
  70. *
  71. * Contributor(s):
  72. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  73. *
  74. *--------------------------------------------------------------------*/
  75. void pcap_enum (flag_t flags)
  76. {
  77. char report [PCAP_ERRBUF_SIZE];
  78. char string [ETHER_ADDR_LEN * 3];
  79. byte number [ETHER_ADDR_LEN];
  80. pcap_if_t * device;
  81. pcap_if_t * devices = (pcap_if_t *) (0);
  82. unsigned index;
  83. if (pcap_findalldevs (& devices, report) == - 1)
  84. {
  85. error (1, 0, "Can't enumerate interfaces");
  86. }
  87. if (! devices)
  88. {
  89. error (1, 0, "No interfaces available");
  90. }
  91. if (_anyset (flags, PCAP_DEVICES))
  92. {
  93. for (device = devices, index = 1; device; device = device->next, index++)
  94. {
  95. gethwaddr (number, device->name);
  96. hexdecode (number, sizeof (number), string, sizeof (string));
  97. printf ("%2d %s %s", index, string, device->name);
  98. if (device->description)
  99. {
  100. printf ("\t(%s)", device->description);
  101. }
  102. printf ("\n");
  103. }
  104. }
  105. if (_anyset (flags, PCAP_NICS))
  106. {
  107. for (device = devices, index = 1; device; device = device->next, index++)
  108. {
  109. #if defined (WIN32)
  110. printf ("ETH%d=%d", index, index);
  111. #else
  112. printf ("ETH%d=%s", index, device->name);
  113. #endif
  114. if (device->description)
  115. {
  116. printf ("\t# %s", device->description);
  117. }
  118. printf ("\n");
  119. }
  120. printf ("\n");
  121. }
  122. if (_anyset (flags, PCAP_MACS))
  123. {
  124. for (device = devices, index = 1; device; device = device->next, index++)
  125. {
  126. gethwaddr (number, device->name);
  127. hexdecode (number, sizeof (number), string, sizeof (string));
  128. printf ("NIC%d=%s", index, string);
  129. if (device->description)
  130. {
  131. printf ("\t# %s", device->description);
  132. }
  133. printf ("\n");
  134. }
  135. printf ("\n");
  136. }
  137. pcap_freealldevs (devices);
  138. return;
  139. }
  140. /*====================================================================*
  141. *
  142. * int main (int argc, char const * argv [])
  143. *
  144. *--------------------------------------------------------------------*/
  145. int main (int argc, char const * argv [])
  146. {
  147. static char const * optv [] =
  148. {
  149. "hqv",
  150. "",
  151. "enumerate available pcap devices on stdout",
  152. "h\tprint host definitions for scripting",
  153. "q\tquiet",
  154. "v\tverbose messages",
  155. (char const *) (0)
  156. };
  157. flag_t flags = PCAP_DEVICES;
  158. signed c;
  159. optind = 1;
  160. while (~ (c = getoptv (argc, argv, optv)))
  161. {
  162. switch ((char) (c))
  163. {
  164. case 'h':
  165. _clrbits (flags, (PCAP_DEVICES));
  166. _setbits (flags, (PCAP_NICS | PCAP_MACS));
  167. break;
  168. case 'q':
  169. _setbits (flags, PCAP_SILENCE);
  170. break;
  171. case 'v':
  172. _setbits (flags, PCAP_VERBOSE);
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. argc -= optind;
  179. argv += optind;
  180. if (argc)
  181. {
  182. error (1, ECANCELED, ERROR_TOOMANY);
  183. }
  184. pcap_enum (flags);
  185. return (0);
  186. }