npcap-api.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>The Npcap API</title><meta name="generator" content="DocBook XSL Stylesheets V1.79.2"><meta name="description" content="The Npcap Application Programming Interface (API) consists of the libpcap API and a few non-portable extensions: pcap_setbuff, pcap_setuserbuffer, pcap_setmintocopy, pcap_getevent, pcap_setmode, pcap_oid_get_request and pcap_oid_set_request, functions for batch-sending packets with pcap_send_queue, and pcap_stats_ex."><link rel="home" href="index.html" title="Npcap Reference Guide"><link rel="up" href="index.html" title="Npcap Reference Guide"><link rel="prev" href="npcap-devguide.html" title="Developing software with Npcap"><link rel="next" href="npcap-tutorial.html" title="Npcap Development Tutorial"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">The Npcap API</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="npcap-devguide.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="npcap-tutorial.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="npcap-api"></a>The Npcap API</h2></div><div><div class="abstract"><p class="title"><b>Abstract</b></p>
  2. <p>The Npcap Application Programming Interface (<acronym class="acronym">API</acronym>) consists of the
  3. libpcap API and a few non-portable extensions: <code class="code">pcap_setbuff</code>,
  4. <code class="code">pcap_setuserbuffer</code>,
  5. <code class="code">pcap_setmintocopy</code>,
  6. <code class="code">pcap_getevent</code>,
  7. <code class="code">pcap_setmode</code>,
  8. <code class="code">pcap_oid_get_request</code> and <code class="code">pcap_oid_set_request</code>,
  9. functions for batch-sending packets with <code class="code">pcap_send_queue</code>,
  10. and <code class="code">pcap_stats_ex</code>.
  11. </p>
  12. </div></div></div></div>
  13. <p>The Npcap API is exported by <code class="filename">wpcap.dll</code> and is the
  14. Windows port of <a class="ulink" href="https://www.tcpdump.org/" target="_top">libpcap</a>.
  15. The API and functions are described in
  16. <a class="ulink" href="wpcap/pcap.html" target="_top">the pcap(1) man page</a>.
  17. </p>
  18. <div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="npcap-api-extensions"></a>Extensions to libpcap for Windows</h3></div></div></div>
  19. <p>
  20. There are a few extensions to libpcap that exist only on Windows.
  21. Software that uses these extensions will not be portable to non-Windows
  22. systems. The following is a brief list of these extensions and their purpose.
  23. </p>
  24. <div class="variablelist"><dl class="variablelist"><dt><span class="term">
  25. <code class="code">pcap_setbuff</code>
  26. </span></dt><dd>
  27. <p>
  28. Sets the size of the kernel buffer associated with an adapter.
  29. </p>
  30. <code class="code">int pcap_setbuff(pcap_t *p, int dim);</code>
  31. <p><code class="literal">dim</code> specifies the size of the buffer in
  32. bytes. The return value is 0 when the call succeeds, -1 otherwise.
  33. If an old buffer was already created with a previous call to
  34. <code class="literal">pcap_setbuff()</code>, it is deleted and its content is
  35. discarded. <a class="ulink" href="wpcap/pcap_open_live.html" target="_top">pcap_open_live()</a> creates
  36. a 1 MByte buffer by default.
  37. </p>
  38. <p>
  39. <span class="emphasis"><em>Portability note:</em></span> libpcap provides the <a class="ulink" href="wpcap/pcap_set_buffer_size.html" target="_top">pcap_set_buffer_size()</a>
  40. function for setting the kernel buffer size. This removes the need
  41. to use the non-portable <code class="literal">pcap_setbuff()</code> for this
  42. purpose.
  43. </p>
  44. </dd><dt><span class="term">
  45. <code class="code">pcap_setmode</code>
  46. </span></dt><dd>
  47. <p>Sets the working mode of the interface.</p>
  48. <code class="code">int pcap_setmode(pcap_t *p, int mode);</code>
  49. <p>
  50. Valid values for mode are <code class="literal">MODE_CAPT</code> (default
  51. capture mode) and <code class="literal">MODE_STAT</code> (statistical mode).
  52. See <a class="xref" href="npcap-tutorial.html#npcap-tutorial-statistics" title="Gathering Statistics on the network traffic">the section called &#8220;Gathering Statistics on the network traffic&#8221;</a> for details about
  53. statistical mode.
  54. </p>
  55. </dd><dt><span class="term">
  56. <code class="code">pcap_setmintocopy</code>
  57. </span></dt><dd>
  58. <p>
  59. Sets the minumum amount of data received by the kernel in a single call.
  60. </p>
  61. <code class="code">int pcap_setmintocopy(pcap_t *p, int size);</code>
  62. <p>
  63. This function changes the minimum amount of data in the
  64. kernel buffer that causes a read from the application to return
  65. (unless the timeout expires). If the value of
  66. <code class="literal">size</code> is large, the kernel is forced to wait the
  67. arrival of several packets before
  68. copying the data to the user. This guarantees a low number of
  69. system calls, i.e. low processor usage, and is a good setting for
  70. applications like packet-sniffers and protocol analyzers. Vice
  71. versa, in presence of a small value for this variable, the kernel
  72. will copy the packets as soon as the application is ready to
  73. receive them. This is useful for real time applications that need
  74. the best responsiveness from the kernel. <a class="ulink" href="wpcap/pcap_open_live.html" target="_top">pcap_open_live()</a> sets a
  75. default <code class="literal">size</code> value of 16000 bytes.
  76. </p>
  77. <p>
  78. <span class="emphasis"><em>Portability note:</em></span> libpcap provides the <a class="ulink" href="wpcap/pcap_set_immediate_mode.html" target="_top">pcap_set_immediate_mode()</a>
  79. function for applications that need to receive packets as soon as
  80. they arrive. This removes the need to use the non-portable
  81. <code class="literal">pcap_setmintocopy()</code> for this purpose.
  82. </p>
  83. </dd><dt><span class="term">
  84. <code class="code">pcap_getevent</code>
  85. </span></dt><dd>
  86. <p>Returns the handle of the event associated with the interface.</p>
  87. <code class="code">HANDLE pcap_getevent(pcap_t *p);</code>
  88. <p> This event can be passed to functions like
  89. <code class="literal">WaitForSingleObject()</code> or
  90. <code class="literal">WaitForMultipleObjects()</code> to wait until the
  91. driver's buffer contains some data without performing a read.
  92. </p>
  93. <p>
  94. <span class="emphasis"><em>Portability note:</em></span> This function is the Windows
  95. alternative to <a class="ulink" href="wpcap/pcap_get_selectable_fd.html" target="_top">pcap_get_selectable_fd()</a>,
  96. which is only available on UNIX-like systems.
  97. </p>
  98. </dd><dt><span class="term">
  99. <code class="code">pcap_oid_get_request</code> and <code class="code">pcap_oid_set_request</code>
  100. </span></dt><dd>
  101. <p>Send an OID request to the underlying NDIS drivers</p>
  102. <code class="code">int pcap_oid_get_request(pcap_t *, bpf_u_int32, void *, size_t *);</code>
  103. <code class="code">int pcap_oid_set_request(pcap_t *, bpf_u_int32, const void *, size_t *);</code>
  104. </dd><dt><span class="term">
  105. Queuing sent packets with <code class="code">pcap_send_queue</code>
  106. </span></dt><dd>
  107. <p>
  108. Npcap has the ability to queue multiple raw packets for
  109. transmission on the network in a single call. This is more
  110. efficient than issuing a series of
  111. <code class="literal">pcap_sendpacket()</code>, because the packets are
  112. buffered in the kernel driver, so the number of context switches is
  113. reduced.
  114. </p>
  115. <code class="code">pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);</code>
  116. <code class="code">void pcap_sendqueue_destroy(pcap_send_queue* queue);</code>
  117. <p>Allocate a send queue as a buffer of <code class="literal">memsize</code>
  118. bytes. The <code class="literal">pcap_send_queue</code> allocated can be
  119. freed with <code class="literal">pcap_sendqueue_destroy()</code>.</p>
  120. <code class="code">int pcap_sendqueue_queue(pcap_send_queue* queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);</code>
  121. <p>
  122. <code class="literal">pcap_sendqueue_queue()</code> adds a packet at the end
  123. of the send queue pointed by the <code class="literal">queue</code>
  124. parameter. <code class="literal">pkt_header</code> points to a
  125. <code class="literal">pcap_pkthdr</code> structure with the timestamp and the
  126. length of the packet, <code class="literal">pkt_data</code> points to a
  127. buffer with the data of the packet.
  128. </p>
  129. <p>
  130. The <code class="literal">pcap_pkthdr</code> structure is the same used by
  131. Npcap and libpcap to store the packets in a file, therefore sending
  132. a capture file is straightforward. 'Raw packet' means that the
  133. sending application will have to include the protocol headers,
  134. since every packet is sent to the network 'as is'. The CRC of the
  135. packets needs not to be calculated, because it will be
  136. transparently added by the network interface.
  137. </p>
  138. <code class="code">u_int pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue* queue, int sync);</code>
  139. <p>
  140. This function transmits the content of a queue to the wire.
  141. <code class="literal">p</code> is a pointer to the adapter on which the
  142. packets will be sent, <code class="literal">queue</code> points to a
  143. <code class="literal">pcap_send_queue</code> structure containing the packets
  144. to send), <code class="literal">sync</code> determines if the send operation
  145. must be synchronized: if it is non-zero, the packets are sent
  146. respecting the timestamps, otherwise they are sent as fast as
  147. possible.
  148. </p>
  149. <p>
  150. The return value is the amount of bytes actually sent. If it is
  151. smaller than the <code class="literal">size</code> parameter, an error
  152. occurred during the send. The error can be caused by a
  153. driver/adapter problem or by an inconsistent/bogus send queue.
  154. </p>
  155. <p>
  156. <span class="emphasis"><em>Performance note:</em></span> When <code class="literal">sync</code>
  157. is set to <code class="literal">TRUE</code>, the packets are synchronized in
  158. the kernel with a high precision timestamp. This requires a
  159. non-negligible amount of CPU, but allows normally to send the
  160. packets with a precision of some microseconds (depending on the
  161. accuracy of the performance counter of the machine). Such a
  162. precision cannot be reached sending the packets with
  163. <code class="literal">pcap_sendpacket()</code>.
  164. </p>
  165. </dd><dt><span class="term">
  166. <code class="code">pcap_stats_ex</code>
  167. </span></dt><dd>
  168. <code class="code">struct pcap_stat *pcap_stats_ex(pcap_t *p, int *pcap_stat_size);</code>
  169. <p>
  170. <code class="literal">pcap_stats_ex()</code> extends the
  171. <code class="literal">pcap_stats()</code> allowing to return more statistical
  172. parameters than the old call. One of the advantages of
  173. this new call is that the <code class="literal">pcap_stat</code> structure is
  174. not allocated by the user; instead, it is returned back by the
  175. system. This allow to extend the <code class="literal">pcap_stat</code>
  176. structure without affecting backward compatibility on older
  177. applications. These will simply check at the values of the members
  178. at the beginning of the structure, while only newest applications
  179. are able to read new statistical values, which are appended in
  180. tail.
  181. </p>
  182. <p>
  183. To be sure not to read a piece of memory which has not been allocated
  184. by the system, the variable <code class="literal">pcap_stat_size</code> will
  185. return back the size of the structure <code class="literal">pcap_stat</code>
  186. allocated by the system.
  187. </p>
  188. <p>
  189. <code class="literal">p</code>: pointer to the <code class="literal">pcap_t</code>
  190. currently in use. <code class="literal">pcap_stat_size</code>: pointer to an
  191. integer that will contain (when the function returns back) the size
  192. of the structure <code class="literal">pcap_stat</code> as it has been
  193. allocated by the system.
  194. </p>
  195. <p>
  196. The function returns a pointer to a pcap_stat structure, that will
  197. contain the statistics related to the current device. The return
  198. value is <code class="literal">NULL</code> in case of errors, and the error
  199. text can be obtained with <code class="literal">pcap_perror()</code> or
  200. <code class="literal">pcap_geterr()</code>.
  201. </p>
  202. </dd><dt><span class="term">
  203. <code class="code">pcap_setuserbuffer</code>
  204. </span></dt><dd>
  205. <p>Sets the size of the buffer that accepts packets from the kernel driver.</p>
  206. <code class="code">int pcap_setuserbuffer(pcap_t *p, int size);</code>
  207. <p>
  208. The size of the packet buffer is a parameter that can sensibly
  209. influence the performance of the capture process, since this buffer
  210. will contain the packets received from the the Npcap driver. The
  211. driver is able to return several packets using a single read call,
  212. and the number of packets transferable to the application in a call
  213. is limited only by the size of this buffer. Therefore setting a
  214. larger buffer siz can noticeably decrease the number of system
  215. calls, reducing the impact of the capture process on the processor.
  216. </p>
  217. </dd></dl></div>
  218. </div>
  219. </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="npcap-devguide.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="npcap-tutorial.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Developing software with Npcap </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Npcap Development Tutorial</td></tr></table></div></body></html>