pcapdevs.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * pcapdevs.c - pcap device enumerator;
  44. *
  45. *
  46. * Contributor(s):
  47. * Nathaniel Houghton
  48. * Charles Maier
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*
  52. * system header files;
  53. *--------------------------------------------------------------------*/
  54. #include <unistd.h>
  55. #include <errno.h>
  56. #include <string.h>
  57. #include <pcap.h>
  58. #if defined (__linux__)
  59. #elif defined (__APPLE__)
  60. #elif defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  61. # include <sys/types.h>
  62. # include <sys/socket.h>
  63. # include <net/if.h>
  64. # include <netinet/in.h>
  65. # include <netinet/if_ether.h>
  66. #elif defined (WINPCAP) || defined (LIBPCAP)
  67. #else
  68. #error "Unknown environment"
  69. #endif
  70. /*====================================================================*
  71. * custom header files;
  72. *--------------------------------------------------------------------*/
  73. #include "../tools/getoptv.h"
  74. #include "../tools/putoptv.h"
  75. #include "../tools/version.h"
  76. #include "../tools/memory.h"
  77. #include "../tools/flags.h"
  78. #include "../tools/types.h"
  79. #include "../tools/error.h"
  80. #include "../ether/ether.h"
  81. /*====================================================================*
  82. * custom source files;
  83. *--------------------------------------------------------------------*/
  84. #ifndef MAKEFILE
  85. #include "../tools/getoptv.c"
  86. #include "../tools/putoptv.c"
  87. #include "../tools/version.c"
  88. #include "../tools/error.c"
  89. #include "../tools/hexdecode.c"
  90. #endif
  91. /*====================================================================*
  92. * program constants;
  93. *--------------------------------------------------------------------*/
  94. #define PCAP_VERBOSE (1 << 0)
  95. #define PCAP_SILENCE (1 << 1)
  96. #define PCAP_DEVICES (1 << 2)
  97. #define PCAP_NICS (1 << 3)
  98. #define PCAP_MACS (1 << 4)
  99. /*====================================================================*
  100. *
  101. * void pcap_enum (flag_t flags);
  102. *
  103. * pcap_enum available pcap devices on stdout;
  104. *
  105. *
  106. * Contributor(s):
  107. * Nathaniel Houghton
  108. *
  109. *--------------------------------------------------------------------*/
  110. void pcap_enum (flag_t flags)
  111. {
  112. char report [PCAP_ERRBUF_SIZE];
  113. char string [ETHER_ADDR_LEN * 3];
  114. byte number [ETHER_ADDR_LEN];
  115. pcap_if_t * device;
  116. pcap_if_t * devices = (pcap_if_t *)(0);
  117. unsigned index;
  118. if (pcap_findalldevs (&devices, report) == -1)
  119. {
  120. error (1, 0, "Can't enumerate interfaces");
  121. }
  122. if (!devices)
  123. {
  124. error (1, 0, "No interfaces available");
  125. }
  126. if (_anyset (flags, PCAP_DEVICES))
  127. {
  128. for (device = devices, index = 1; device; device = device->next, index++)
  129. {
  130. gethwaddr (number, device->name);
  131. hexdecode (number, sizeof (number), string, sizeof (string));
  132. printf ("%2d %s %s", index, string, device->name);
  133. if (device->description)
  134. {
  135. printf ("\t(%s)", device->description);
  136. }
  137. printf ("\n");
  138. }
  139. }
  140. if (_anyset (flags, PCAP_NICS))
  141. {
  142. for (device = devices, index = 1; device; device = device->next, index++)
  143. {
  144. #if defined (WIN32)
  145. printf ("ETH%d=%d", index, index);
  146. #else
  147. printf ("ETH%d=%s", index, device->name);
  148. #endif
  149. if (device->description)
  150. {
  151. printf ("\t# %s", device->description);
  152. }
  153. printf ("\n");
  154. }
  155. printf ("\n");
  156. }
  157. if (_anyset (flags, PCAP_MACS))
  158. {
  159. for (device = devices, index = 1; device; device = device->next, index++)
  160. {
  161. gethwaddr (number, device->name);
  162. hexdecode (number, sizeof (number), string, sizeof (string));
  163. printf ("NIC%d=%s", index, string);
  164. if (device->description)
  165. {
  166. printf ("\t# %s", device->description);
  167. }
  168. printf ("\n");
  169. }
  170. printf ("\n");
  171. }
  172. pcap_freealldevs (devices);
  173. return;
  174. }
  175. /*====================================================================*
  176. *
  177. * int main (int argc, char const * argv [])
  178. *
  179. *
  180. *--------------------------------------------------------------------*/
  181. int main (int argc, char const * argv [])
  182. {
  183. static char const * optv [] =
  184. {
  185. "hqv",
  186. "",
  187. "enumerate available pcap devices on stdout",
  188. "h\tprint host definitions for scripting",
  189. "q\tquiet",
  190. "v\tverbose messages",
  191. (char const *) (0)
  192. };
  193. flag_t flags = PCAP_DEVICES;
  194. signed c;
  195. optind = 1;
  196. while ((c = getoptv (argc, argv, optv)) != -1)
  197. {
  198. switch ((char) (c))
  199. {
  200. case 'h':
  201. _clrbits (flags, (PCAP_DEVICES));
  202. _setbits (flags, (PCAP_NICS | PCAP_MACS));
  203. break;
  204. case 'q':
  205. _setbits (flags, PCAP_SILENCE);
  206. break;
  207. case 'v':
  208. _setbits (flags, PCAP_VERBOSE);
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214. argc -= optind;
  215. argv += optind;
  216. if (argc)
  217. {
  218. error (1, ECANCELED, ERROR_TOOMANY);
  219. }
  220. pcap_enum (flags);
  221. return (0);
  222. }