readpacket.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. * ssize_t readpacket (struct channel const * channel, void * memory, ssize_t extent);
  44. *
  45. * channel.h
  46. *
  47. * read one packet from a raw packet channel;
  48. *
  49. * return the packet size on success, 0 on timeout or -1 on error;
  50. * dump packets on stdout when the channel VERBOSE flag is set;
  51. *
  52. * constant __MAGIC__ enables code that reads frames from stdin,
  53. * instead of the network; you may use it whenever a network or
  54. * transmitting device is not available;
  55. *
  56. *
  57. * Contributor(s):
  58. * Charles Maier
  59. * Nathaniel Houghton
  60. * Werner Henze
  61. *
  62. *--------------------------------------------------------------------*/
  63. #ifndef READPACKET_SOURCE
  64. #define READPACKET_SOURCE
  65. #include <stdlib.h>
  66. #include <unistd.h>
  67. #include <memory.h>
  68. #include <errno.h>
  69. #if defined (__linux__)
  70. #include <poll.h>
  71. #endif
  72. #include "../ether/channel.h"
  73. #include "../tools/memory.h"
  74. #include "../tools/error.h"
  75. #include "../tools/flags.h"
  76. #if defined (__MAGIC__)
  77. #include "../tools/hexload.c"
  78. #endif
  79. ssize_t readpacket (struct channel const * channel, void * memory, ssize_t extent)
  80. {
  81. #if defined (__MAGIC__)
  82. memset (memory, 0, extent);
  83. extent = hexload (memory, extent, stdin);
  84. if (_anyset (channel->flags, CHANNEL_VERBOSE))
  85. {
  86. hexdump (memory, 0, extent, stdout);
  87. }
  88. return (extent);
  89. #elif defined (__linux__)
  90. struct pollfd pollfd =
  91. {
  92. channel->fd,
  93. POLLIN,
  94. 0
  95. };
  96. signed status = poll (&pollfd, 1, channel->capture);
  97. memset (memory, 0, extent);
  98. if ((status < 0) && (errno != EINTR))
  99. {
  100. error (0, errno, "%s can't poll %s", __func__, channel->ifname);
  101. return (-1);
  102. }
  103. if (status > 0)
  104. {
  105. status = recvfrom (channel->fd, memory, extent, 0, (struct sockaddr *) (0), (socklen_t *)(0));
  106. if (status < 0)
  107. {
  108. error (0, errno, "%s can't read %s", __func__, channel->ifname);
  109. return (-1);
  110. }
  111. if (status > 0)
  112. {
  113. extent = status;
  114. if (_anyset (channel->flags, CHANNEL_VERBOSE))
  115. {
  116. hexdump (memory, 0, extent, stdout);
  117. }
  118. return (extent);
  119. }
  120. }
  121. #elif defined (__APPLE__) || defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  122. struct bpf_hdr * bpf_packet;
  123. struct bpf * bpf = channel->bpf;;
  124. memset (memory, 0, extent);
  125. if (bpf->bpf_bufused <= 0)
  126. {
  127. bpf->bpf_bufused = read (channel->fd, bpf->bpf_buffer, bpf->bpf_length);
  128. bpf->bpf_bp = bpf->bpf_buffer;
  129. }
  130. if (bpf->bpf_bufused < 0)
  131. {
  132. error (0, errno, "bpf");
  133. return (-1);
  134. }
  135. if (bpf->bpf_bufused > 0)
  136. {
  137. bpf_packet = (struct bpf_hdr *)(bpf->bpf_bp);
  138. if ((size_t) (extent) > bpf_packet->bh_caplen)
  139. {
  140. extent = bpf_packet->bh_caplen;
  141. }
  142. if ((size_t) (extent) < bpf_packet->bh_caplen)
  143. {
  144. if (_anyset (channel->flags, CHANNEL_VERBOSE))
  145. {
  146. error (0, 0, "Truncated incoming frame (%u -> %zd bytes)", bpf_packet->bh_caplen, extent);
  147. }
  148. }
  149. memcpy (memory, bpf->bpf_bp + bpf_packet->bh_hdrlen, extent);
  150. bpf->bpf_bufused -= BPF_WORDALIGN (bpf_packet->bh_hdrlen + bpf_packet->bh_caplen);
  151. bpf->bpf_bp += BPF_WORDALIGN (bpf_packet->bh_hdrlen + bpf_packet->bh_caplen);
  152. if (_anyset (channel->flags, CHANNEL_VERBOSE))
  153. {
  154. hexdump (memory, 0, extent, stdout);
  155. }
  156. return (extent);
  157. }
  158. #elif defined (WINPCAP) || defined (LIBPCAP)
  159. struct pcap_pkthdr * header;
  160. const uint8_t *data;
  161. signed status = pcap_next_ex (channel->socket, &header, &data);
  162. memset (memory, 0, extent);
  163. if (status < 0)
  164. {
  165. error (0, errno, "pcap_next_ex");
  166. return (-1);
  167. }
  168. if (status > 0)
  169. {
  170. extent = header->caplen;
  171. memcpy (memory, data, extent);
  172. if (_anyset (channel->flags, CHANNEL_VERBOSE))
  173. {
  174. hexdump (memory, 0, extent, stdout);
  175. }
  176. return (extent);
  177. }
  178. #else
  179. #error "Unknown Environment"
  180. #endif
  181. return (0);
  182. }
  183. #endif