pcapdevs.c.html 8.1 KB

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