diff options
Diffstat (limited to 'pjlib-util')
-rw-r--r-- | pjlib-util/include/pjlib-util/pcap.h | 25 | ||||
-rw-r--r-- | pjlib-util/src/pjlib-util/pcap.c | 19 |
2 files changed, 29 insertions, 15 deletions
diff --git a/pjlib-util/include/pjlib-util/pcap.h b/pjlib-util/include/pjlib-util/pcap.h index e27f5709..58d39a9f 100644 --- a/pjlib-util/include/pjlib-util/pcap.h +++ b/pjlib-util/include/pjlib-util/pcap.h @@ -60,9 +60,22 @@ typedef enum pj_pcap_proto_type /** + * This describes UDP header, which may optionally be returned in + * #pj_pcap_read_udp() function. All fields are in network byte order. + */ +typedef struct pj_pcap_udp_hdr +{ + pj_uint16_t src_port; /**< Source port. */ + pj_uint16_t dst_port; /**< Destination port */ + pj_uint16_t len; /**< Length. */ + pj_uint16_t csum; /**< Checksum. */ +} pj_pcap_udp_hdr; + + +/** * This structure describes the filter to be used when reading packets from - * a PCAP file. The filter is used to select what packets can be returned - * to a read operation. + * a PCAP file. When a filter is configured, only packets matching all the + * filter specifications will be read from PCAP file. */ typedef struct pj_pcap_filter { @@ -142,7 +155,8 @@ PJ_DECL(pj_status_t) pj_pcap_open(pj_pool_t *pool, PJ_DECL(pj_status_t) pj_pcap_close(pj_pcap_file *file); /** - * Configure filter for reading the file. + * Configure filter for reading the file. When filter is configured, + * only packets matching all the filter settings will be returned. * * @param file PCAP file handle. * @param filter The filter. @@ -153,9 +167,11 @@ PJ_DECL(pj_status_t) pj_pcap_set_filter(pj_pcap_file *file, const pj_pcap_filter *filter); /** - * Read UDP payload from the next packet in the PCAP file. + * Read UDP payload from the next packet in the PCAP file. Optionally it + * can return the UDP header, if caller supplies it. * * @param file PCAP file handle. + * @param udp_hdr Optional buffer to receive UDP header. * @param udp_payload Buffer to receive the UDP payload. * @param udp_payload_size On input, specify the size of the buffer. * On output, it will be filled with the actual size @@ -164,6 +180,7 @@ PJ_DECL(pj_status_t) pj_pcap_set_filter(pj_pcap_file *file, * @return PJ_SUCCESS on success, or the appropriate error code. */ PJ_DECL(pj_status_t) pj_pcap_read_udp(pj_pcap_file *file, + pj_pcap_udp_hdr *udp_hdr, pj_uint8_t *udp_payload, pj_size_t *udp_payload_size); diff --git a/pjlib-util/src/pjlib-util/pcap.c b/pjlib-util/src/pjlib-util/pcap.c index 22d602d5..40ee7b3f 100644 --- a/pjlib-util/src/pjlib-util/pcap.c +++ b/pjlib-util/src/pjlib-util/pcap.c @@ -54,11 +54,12 @@ typedef struct pj_pcap_rec_hdr } pj_pcap_rec_hdr; #if 0 +/* gcc insisted on aligning this struct to 32bit on ARM */ typedef struct pj_pcap_eth_hdr { pj_uint8_t dest[6]; pj_uint8_t src[6]; - pj_uint8_t len[2]; /* problem with struct size if pj_uint16_t */ + pj_uint8_t len[2]; } pj_pcap_eth_hdr; #else typedef pj_uint8_t pj_pcap_eth_hdr[14]; @@ -78,16 +79,6 @@ typedef struct pj_pcap_ip_hdr pj_uint32_t ip_dst; } pj_pcap_ip_hdr; -typedef struct pj_pcap_udp_hdr -{ - pj_uint16_t src_port; - pj_uint16_t dst_port; - pj_uint16_t len; - pj_uint16_t csum; -} pj_pcap_udp_hdr; - -#pragma pack() - /* Implementation of pcap file */ struct pj_pcap_file { @@ -210,6 +201,7 @@ static pj_status_t skip(pj_oshandle_t fd, pj_off_t bytes) /* Read UDP packet */ PJ_DEF(pj_status_t) pj_pcap_read_udp(pj_pcap_file *file, + pj_pcap_udp_hdr *udp_hdr, pj_uint8_t *udp_payload, pj_size_t *udp_payload_size) { @@ -342,6 +334,11 @@ PJ_DEF(pj_status_t) pj_pcap_read_udp(pj_pcap_file *file, continue; } + /* Copy UDP header if caller wants it */ + if (udp_hdr) { + pj_memcpy(udp_hdr, &tmp.udp, sizeof(*udp_hdr)); + } + /* Calculate payload size */ sz = pj_ntohs(tmp.udp.len) - sizeof(tmp.udp); break; |