readpacket.c.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. readpacket.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='ReadNVM.c.html' title=' ReadNVM.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='ReadParameterBlock.c.html' title=' ReadParameterBlock.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. * ssize_t readpacket (struct channel const * channel, void * memory, ssize_t extent);
  64. *
  65. * channel.h
  66. *
  67. * read one packet from a raw packet channel;
  68. *
  69. * return the packet size on success, 0 on timeout or -1 on error;
  70. * dump packets on stdout when the channel VERBOSE flag is set;
  71. *
  72. * constant __MAGIC__ enables code that reads frames from stdin,
  73. * instead of the network; you may use it whenever a network or
  74. * transmitting device is not available;
  75. *
  76. *
  77. * Contributor(s):
  78. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  79. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  80. * Werner Henze &lt;w.henze@avm.de&gt;
  81. *
  82. *--------------------------------------------------------------------*/
  83. #ifndef READPACKET_SOURCE
  84. #define READPACKET_SOURCE
  85. #include &lt;stdlib.h&gt;
  86. #include &lt;unistd.h&gt;
  87. #include &lt;memory.h&gt;
  88. #include &lt;errno.h&gt;
  89. #include &quot;../ether/channel.h&quot;
  90. #include &quot;../tools/memory.h&quot;
  91. #include &quot;../tools/error.h&quot;
  92. #include &quot;../tools/flags.h&quot;
  93. #if defined (__MAGIC__)
  94. #include &quot;../tools/hexload.c&quot;
  95. #endif
  96. ssize_t readpacket (struct channel const * channel, void * memory, ssize_t extent)
  97. {
  98. #if defined (__MAGIC__)
  99. memset (memory, 0, extent);
  100. extent = hexload (memory, extent, stdin);
  101. if (_anyset (channel-&gt;flags, CHANNEL_VERBOSE))
  102. {
  103. hexdump (memory, 0, extent, stdout);
  104. }
  105. return (extent);
  106. #elif defined (__linux__)
  107. #include &lt;sys/poll.h&gt;
  108. struct pollfd pollfd =
  109. {
  110. channel-&gt;fd,
  111. POLLIN,
  112. 0
  113. };
  114. signed status = poll (&amp;pollfd, 1, channel-&gt;capture);
  115. memset (memory, 0, extent);
  116. if ((status &lt; 0) &amp;&amp; (errno != EINTR))
  117. {
  118. error (0, errno, &quot;%s can't poll %s&quot;, __func__, channel-&gt;ifname);
  119. return (-1);
  120. }
  121. if (status &gt; 0)
  122. {
  123. status = recvfrom (channel-&gt;fd, memory, extent, 0, (struct sockaddr *) (0), (socklen_t *)(0));
  124. if (status &lt; 0)
  125. {
  126. error (0, errno, &quot;%s can't read %s&quot;, __func__, channel-&gt;ifname);
  127. return (-1);
  128. }
  129. if (status &gt; 0)
  130. {
  131. extent = status;
  132. if (_anyset (channel-&gt;flags, CHANNEL_VERBOSE))
  133. {
  134. hexdump (memory, 0, extent, stdout);
  135. }
  136. return (extent);
  137. }
  138. }
  139. #elif defined (__APPLE__) || defined (__OpenBSD__)
  140. struct bpf_hdr * bpf_packet;
  141. struct bpf * bpf = channel-&gt;bpf;;
  142. memset (memory, 0, extent);
  143. if (bpf-&gt;bpf_bufused &lt;= 0)
  144. {
  145. bpf-&gt;bpf_bufused = read (channel-&gt;fd, bpf-&gt;bpf_buffer, bpf-&gt;bpf_length);
  146. bpf-&gt;bpf_bp = bpf-&gt;bpf_buffer;
  147. }
  148. if (bpf-&gt;bpf_bufused &lt; 0)
  149. {
  150. error (0, errno, &quot;bpf&quot;);
  151. return (-1);
  152. }
  153. if (bpf-&gt;bpf_bufused &gt; 0)
  154. {
  155. bpf_packet = (struct bpf_hdr *)(bpf-&gt;bpf_bp);
  156. if ((size_t) (extent) &gt; bpf_packet-&gt;bh_caplen)
  157. {
  158. extent = bpf_packet-&gt;bh_caplen;
  159. }
  160. if ((size_t) (extent) &lt; bpf_packet-&gt;bh_caplen)
  161. {
  162. if (_anyset (channel-&gt;flags, CHANNEL_VERBOSE))
  163. {
  164. error (0, 0, &quot;Truncated incoming frame (%u -&gt; %zd bytes)&quot;, bpf_packet-&gt;bh_caplen, extent);
  165. }
  166. }
  167. memcpy (memory, bpf-&gt;bpf_bp + bpf_packet-&gt;bh_hdrlen, extent);
  168. bpf-&gt;bpf_bufused -= BPF_WORDALIGN (bpf_packet-&gt;bh_hdrlen + bpf_packet-&gt;bh_caplen);
  169. bpf-&gt;bpf_bp += BPF_WORDALIGN (bpf_packet-&gt;bh_hdrlen + bpf_packet-&gt;bh_caplen);
  170. if (_anyset (channel-&gt;flags, CHANNEL_VERBOSE))
  171. {
  172. hexdump (memory, 0, extent, stdout);
  173. }
  174. return (extent);
  175. }
  176. #elif defined (WINPCAP) || defined (LIBPCAP)
  177. struct pcap_pkthdr * header;
  178. const uint8_t *data;
  179. signed status = pcap_next_ex (channel-&gt;socket, &amp;header, &amp;data);
  180. memset (memory, 0, extent);
  181. if (status &lt; 0)
  182. {
  183. error (0, errno, &quot;pcap_next_ex&quot;);
  184. return (-1);
  185. }
  186. if (status &gt; 0)
  187. {
  188. extent = header-&gt;caplen;
  189. memcpy (memory, data, extent);
  190. if (_anyset (channel-&gt;flags, CHANNEL_VERBOSE))
  191. {
  192. hexdump (memory, 0, extent, stdout);
  193. }
  194. return (extent);
  195. }
  196. #else
  197. #error &quot;Unknown Environment&quot;
  198. #endif
  199. return (0);
  200. }
  201. #endif
  202. </pre>
  203. <div class='footerlink'>
  204. [<a href='ReadNVM.c.html' title=' ReadNVM.c '>PREV</a>]
  205. [<a href='toolkit.html' title=' Index '>HOME</a>]
  206. [<a href='ReadParameterBlock.c.html' title=' ReadParameterBlock.c '>NEXT</a>]
  207. </div>
  208. </body>
  209. </html>