pcap.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJLIB_UTIL_PCAP_H__
  20. #define __PJLIB_UTIL_PCAP_H__
  21. /**
  22. * @file pcap.h
  23. * @brief Simple PCAP file reader
  24. */
  25. #include <pj/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_PCAP Simple PCAP file reader
  29. * @ingroup PJ_FILE_FMT
  30. * @{
  31. * This module describes simple utility to read PCAP file. It is not intended
  32. * to support all PCAP features (that's what libpcap is for!), but it can
  33. * be useful for example to playback or stream PCAP contents.
  34. */
  35. /**
  36. * Enumeration to describe supported data link types.
  37. */
  38. typedef enum pj_pcap_link_type
  39. {
  40. /** Ethernet data link */
  41. PJ_PCAP_LINK_TYPE_ETH = 1
  42. } pj_pcap_link_type;
  43. /**
  44. * Enumeration to describe supported protocol types.
  45. */
  46. typedef enum pj_pcap_proto_type
  47. {
  48. /** UDP protocol */
  49. PJ_PCAP_PROTO_TYPE_UDP = 17
  50. } pj_pcap_proto_type;
  51. /**
  52. * This describes UDP header, which may optionally be returned in
  53. * #pj_pcap_read_udp() function. All fields are in network byte order.
  54. */
  55. typedef struct pj_pcap_udp_hdr
  56. {
  57. pj_uint16_t src_port; /**< Source port. */
  58. pj_uint16_t dst_port; /**< Destination port */
  59. pj_uint16_t len; /**< Length. */
  60. pj_uint16_t csum; /**< Checksum. */
  61. } pj_pcap_udp_hdr;
  62. /**
  63. * This structure describes the filter to be used when reading packets from
  64. * a PCAP file. When a filter is configured, only packets matching all the
  65. * filter specifications will be read from PCAP file.
  66. */
  67. typedef struct pj_pcap_filter
  68. {
  69. /**
  70. * Select data link type, or zero to include any supported data links.
  71. */
  72. pj_pcap_link_type link;
  73. /**
  74. * Select protocol, or zero to include all supported protocols.
  75. */
  76. pj_pcap_proto_type proto;
  77. /**
  78. * Specify source IP address of the packets, or zero to include packets
  79. * from any IP addresses. Note that IP address here must be in
  80. * network byte order.
  81. */
  82. pj_uint32_t ip_src;
  83. /**
  84. * Specify destination IP address of the packets, or zero to include packets
  85. * destined to any IP addresses. Note that IP address here must be in
  86. * network byte order.
  87. */
  88. pj_uint32_t ip_dst;
  89. /**
  90. * Specify source port of the packets, or zero to include packets with
  91. * any source port number. Note that the port number must be in network
  92. * byte order.
  93. */
  94. pj_uint16_t src_port;
  95. /**
  96. * Specify destination port of the packets, or zero to include packets with
  97. * any destination port number. Note that the port number must be in network
  98. * byte order.
  99. */
  100. pj_uint16_t dst_port;
  101. } pj_pcap_filter;
  102. /** Opaque declaration for PCAP file */
  103. typedef struct pj_pcap_file pj_pcap_file;
  104. /**
  105. * Initialize filter with default values. The default value is to allow
  106. * any packets.
  107. *
  108. * @param filter Filter to be initialized.
  109. */
  110. PJ_DECL(void) pj_pcap_filter_default(pj_pcap_filter *filter);
  111. /**
  112. * Open PCAP file.
  113. *
  114. * @param pool Pool to allocate memory.
  115. * @param path File/path name.
  116. * @param p_file Pointer to receive PCAP file handle.
  117. *
  118. * @return PJ_SUCCESS if file can be opened successfully.
  119. */
  120. PJ_DECL(pj_status_t) pj_pcap_open(pj_pool_t *pool,
  121. const char *path,
  122. pj_pcap_file **p_file);
  123. /**
  124. * Close PCAP file.
  125. *
  126. * @param file PCAP file handle.
  127. *
  128. * @return PJ_SUCCESS on success, or the appropriate error code.
  129. */
  130. PJ_DECL(pj_status_t) pj_pcap_close(pj_pcap_file *file);
  131. /**
  132. * Configure filter for reading the file. When filter is configured,
  133. * only packets matching all the filter settings will be returned.
  134. *
  135. * @param file PCAP file handle.
  136. * @param filter The filter.
  137. *
  138. * @return PJ_SUCCESS on success, or the appropriate error code.
  139. */
  140. PJ_DECL(pj_status_t) pj_pcap_set_filter(pj_pcap_file *file,
  141. const pj_pcap_filter *filter);
  142. /**
  143. * Read UDP payload from the next packet in the PCAP file. Optionally it
  144. * can return the UDP header, if caller supplies it.
  145. *
  146. * @param file PCAP file handle.
  147. * @param udp_hdr Optional buffer to receive UDP header.
  148. * @param udp_payload Buffer to receive the UDP payload.
  149. * @param udp_payload_size On input, specify the size of the buffer.
  150. * On output, it will be filled with the actual size
  151. * of the payload as read from the packet.
  152. *
  153. * @return PJ_SUCCESS on success, or the appropriate error code.
  154. */
  155. PJ_DECL(pj_status_t) pj_pcap_read_udp(pj_pcap_file *file,
  156. pj_pcap_udp_hdr *udp_hdr,
  157. pj_uint8_t *udp_payload,
  158. pj_size_t *udp_payload_size);
  159. /**
  160. * @}
  161. */
  162. PJ_END_DECL
  163. #endif /* __PJLIB_UTIL_PCAP_H__ */