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