Ticket #465: utility to read PCAP file and sample application to read RTP files from PCAP file, decode the payload, and write the PCM output to WAV file

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@1765 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjlib-util/build/Makefile b/pjlib-util/build/Makefile
index dd8a564..f3e1a5e 100644
--- a/pjlib-util/build/Makefile
+++ b/pjlib-util/build/Makefile
@@ -28,7 +28,7 @@
 export PJLIB_UTIL_SRCDIR = ../src/pjlib-util
 export PJLIB_UTIL_OBJS += $(OS_OBJS) $(M_OBJS) $(CC_OBJS) $(HOST_OBJS) \
 		base64.o crc32.o errno.o dns.o dns_dump.o getopt.o \
-		hmac_md5.o hmac_sha1.o md5.o resolver.o \
+		hmac_md5.o hmac_sha1.o md5.o pcap.o resolver.o \
 		scanner.o sha1.o srv_resolver.o string.o stun_simple.o \
 		stun_simple_client.o xml.o
 export PJLIB_UTIL_CFLAGS += $(_CFLAGS)
diff --git a/pjlib-util/build/pjlib_util.dsp b/pjlib-util/build/pjlib_util.dsp
index 825ede8..6578ab5 100644
--- a/pjlib-util/build/pjlib_util.dsp
+++ b/pjlib-util/build/pjlib_util.dsp
@@ -125,6 +125,10 @@
 # End Source File

 # Begin Source File

 

+SOURCE="..\src\pjlib-util\pcap.c"

+# End Source File

+# Begin Source File

+

 SOURCE="..\src\pjlib-util\resolver.c"

 # End Source File

 # Begin Source File

@@ -211,6 +215,10 @@
 # End Source File

 # Begin Source File

 

+SOURCE="..\include\pjlib-util\pcap.h"

+# End Source File

+# Begin Source File

+

 SOURCE="..\include\pjlib-util.h"

 # End Source File

 # Begin Source File

diff --git a/pjlib-util/build/pjlib_util.vcproj b/pjlib-util/build/pjlib_util.vcproj
index 2edaf65..41ed2c2 100644
--- a/pjlib-util/build/pjlib_util.vcproj
+++ b/pjlib-util/build/pjlib_util.vcproj
@@ -356,6 +356,10 @@
 				</FileConfiguration>

 			</File>

 			<File

+				RelativePath="..\src\pjlib-util\pcap.c"

+				>

+			</File>

+			<File

 				RelativePath="..\src\pjlib-util\resolver.c"

 				>

 				<FileConfiguration

@@ -625,6 +629,10 @@
 				>

 			</File>

 			<File

+				RelativePath="..\include\pjlib-util\pcap.h"

+				>

+			</File>

+			<File

 				RelativePath="..\include\pjlib-util.h"

 				>

 			</File>

diff --git a/pjlib-util/include/pjlib-util.h b/pjlib-util/include/pjlib-util.h
index fcafcf1..728d8fe 100644
--- a/pjlib-util/include/pjlib-util.h
+++ b/pjlib-util/include/pjlib-util.h
@@ -53,5 +53,7 @@
 /* Old STUN */
 #include <pjlib-util/stun_simple.h>
 
+/* PCAP */
+#include <pjlib-util/pcap.h>
 
 #endif	/* __PJLIB_UTIL_H__ */
diff --git a/pjlib-util/include/pjlib-util/pcap.h b/pjlib-util/include/pjlib-util/pcap.h
new file mode 100644
index 0000000..e27f570
--- /dev/null
+++ b/pjlib-util/include/pjlib-util/pcap.h
@@ -0,0 +1,178 @@
+/* $Id$ */
+/* 
+ * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
+ */
+#ifndef __PJLIB_UTIL_PCAP_H__
+#define __PJLIB_UTIL_PCAP_H__
+
+/**
+ * @file pcap.h
+ * @brief Simple PCAP file reader
+ */
+
+#include <pj/types.h>
+
+PJ_BEGIN_DECL
+
+/**
+ * @defgroup PJ_PCAP Simple PCAP file reader
+ * @ingroup PJLIB_UTIL
+ * @{
+ * This module describes simple utility to read PCAP file. It is not intended
+ * to support all PCAP features (that's what libpcap is for!), but it can
+ * be useful for example to playback or stream PCAP contents.
+ */
+
+/**
+ * Enumeration to describe supported data link types.
+ */
+typedef enum pj_pcap_link_type
+{
+    /** Ethernet data link */
+    PJ_PCAP_LINK_TYPE_ETH   = 1
+
+} pj_pcap_link_type;
+
+
+/**
+ * Enumeration to describe supported protocol types.
+ */
+typedef enum pj_pcap_proto_type
+{
+    /** UDP protocol */
+    PJ_PCAP_PROTO_TYPE_UDP  = 17
+
+} pj_pcap_proto_type;
+
+
+/**
+ * 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.
+ */
+typedef struct pj_pcap_filter
+{
+    /**
+     * Select data link type, or zero to include any supported data links.
+     */
+    pj_pcap_link_type	link;
+
+    /**
+     * Select protocol, or zero to include all supported protocols.
+     */
+    pj_pcap_proto_type	proto;
+
+    /**
+     * Specify source IP address of the packets, or zero to include packets
+     * from any IP addresses. Note that IP address here must be in
+     * network byte order.
+     */
+    pj_uint32_t		ip_src;
+
+    /**
+     * Specify destination IP address of the packets, or zero to include packets
+     * destined to any IP addresses. Note that IP address here must be in
+     * network byte order.
+     */
+    pj_uint32_t		ip_dst;
+
+    /**
+     * Specify source port of the packets, or zero to include packets with
+     * any source port number. Note that the port number must be in network
+     * byte order.
+     */
+    pj_uint16_t		src_port;
+
+    /**
+     * Specify destination port of the packets, or zero to include packets with
+     * any destination port number. Note that the port number must be in network
+     * byte order.
+     */
+    pj_uint16_t		dst_port;
+
+} pj_pcap_filter;
+
+
+/** Opaque declaration for PCAP file */
+typedef struct pj_pcap_file pj_pcap_file;
+
+
+/**
+ * Initialize filter with default values. The default value is to allow
+ * any packets.
+ *
+ * @param filter    Filter to be initialized.
+ */
+PJ_DECL(void) pj_pcap_filter_default(pj_pcap_filter *filter);
+
+/**
+ * Open PCAP file.
+ *
+ * @param pool	    Pool to allocate memory.
+ * @param path	    File/path name.
+ * @param p_file    Pointer to receive PCAP file handle.
+ *
+ * @return	    PJ_SUCCESS if file can be opened successfully.
+ */
+PJ_DECL(pj_status_t) pj_pcap_open(pj_pool_t *pool,
+				  const char *path,
+				  pj_pcap_file **p_file);
+
+/**
+ * Close PCAP file.
+ *
+ * @param file	    PCAP file handle.
+ *
+ * @return	    PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pj_pcap_close(pj_pcap_file *file);
+
+/**
+ * Configure filter for reading the file.
+ *
+ * @param file	    PCAP file handle.
+ * @param filter    The filter.
+ *
+ * @return	    PJ_SUCCESS on success, or the appropriate error code.
+ */
+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.
+ *
+ * @param file		    PCAP file handle.
+ * @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
+ *			    of the payload as read from the packet.
+ *
+ * @return	    PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pj_pcap_read_udp(pj_pcap_file *file,
+				      pj_uint8_t *udp_payload,
+				      pj_size_t *udp_payload_size);
+
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+#endif	/* __PJLIB_UTIL_PCAP_H__ */
+
diff --git a/pjlib-util/src/pjlib-util/pcap.c b/pjlib-util/src/pjlib-util/pcap.c
new file mode 100644
index 0000000..6be6af3
--- /dev/null
+++ b/pjlib-util/src/pjlib-util/pcap.c
@@ -0,0 +1,307 @@
+#include <pjlib-util/pcap.h>
+#include <pj/assert.h>
+#include <pj/errno.h>
+#include <pj/file_io.h>
+#include <pj/pool.h>
+#include <pj/sock.h>
+#include <pj/string.h>
+
+
+typedef struct pj_pcap_hdr 
+{
+    pj_uint32_t magic_number;   /* magic number */
+    pj_uint16_t version_major;  /* major version number */
+    pj_uint16_t version_minor;  /* minor version number */
+    pj_int32_t  thiszone;       /* GMT to local correction */
+    pj_uint32_t sigfigs;        /* accuracy of timestamps */
+    pj_uint32_t snaplen;        /* max length of captured packets, in octets */
+    pj_uint32_t network;        /* data link type */
+} pj_pcap_hdr;
+
+typedef struct pj_pcap_rec_hdr 
+{
+    pj_uint32_t ts_sec;         /* timestamp seconds */
+    pj_uint32_t ts_usec;        /* timestamp microseconds */
+    pj_uint32_t incl_len;       /* number of octets of packet saved in file */
+    pj_uint32_t orig_len;       /* actual length of packet */
+} pj_pcap_rec_hdr;
+
+typedef struct pj_pcap_eth_hdr 
+{
+    pj_uint8_t  dest[6];
+    pj_uint8_t  src[6];
+    pj_uint16_t len;
+} pj_pcap_eth_hdr;
+
+typedef struct pj_pcap_ip_hdr 
+{
+    pj_uint8_t	v_ihl;
+    pj_uint8_t	tos;
+    pj_uint16_t	len;
+    pj_uint16_t	id;
+    pj_uint16_t	flags_fragment;
+    pj_uint8_t	ttl;
+    pj_uint8_t	proto;
+    pj_uint16_t	csum;
+    pj_uint32_t	ip_src;
+    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;
+
+
+/* Implementation of pcap file */
+struct pj_pcap_file
+{
+    pj_oshandle_t   fd;
+    pj_bool_t	    swap;
+    pj_pcap_hdr	    hdr;
+    pj_pcap_filter  filter;
+};
+
+/* Init default filter */
+PJ_DEF(void) pj_pcap_filter_default(pj_pcap_filter *filter)
+{
+    pj_bzero(filter, sizeof(*filter));
+}
+
+/* Open pcap file */
+PJ_DEF(pj_status_t) pj_pcap_open(pj_pool_t *pool,
+				 const char *path,
+				 pj_pcap_file **p_file)
+{
+    pj_pcap_file *file;
+    pj_ssize_t sz;
+    pj_status_t status;
+
+    PJ_ASSERT_RETURN(pool && path && p_file, PJ_EINVAL);
+
+    file = PJ_POOL_ZALLOC_T(pool, pj_pcap_file);
+
+    status = pj_file_open(pool, path, PJ_O_RDONLY, &file->fd);
+    if (status != PJ_SUCCESS)
+	return status;
+
+    /* Read file pcap header */
+    sz = sizeof(file->hdr);
+    status = pj_file_read(file->fd, &file->hdr, &sz);
+    if (status != PJ_SUCCESS) {
+	pj_file_close(file->fd);
+	return status;
+    }
+
+    /* Check magic number */
+    if (file->hdr.magic_number == 0xa1b2c3d4) {
+	file->swap = PJ_FALSE;
+    } else if (file->hdr.magic_number == 0xd4c3b2a1) {
+	file->swap = PJ_TRUE;
+	file->hdr.network = pj_ntohl(file->hdr.network);
+    } else {
+	/* Not PCAP file */
+	pj_file_close(file->fd);
+	return PJ_EINVALIDOP;
+    }
+
+    *p_file = file;
+    return PJ_SUCCESS;
+}
+
+/* Close pcap file */
+PJ_DEF(pj_status_t) pj_pcap_close(pj_pcap_file *file)
+{
+    PJ_ASSERT_RETURN(file, PJ_EINVAL);
+    return pj_file_close(file->fd);
+}
+
+/* Setup filter */
+PJ_DEF(pj_status_t) pj_pcap_set_filter(pj_pcap_file *file,
+				       const pj_pcap_filter *fil)
+{
+    PJ_ASSERT_RETURN(file && fil, PJ_EINVAL);
+    pj_memcpy(&file->filter, fil, sizeof(pj_pcap_filter));
+    return PJ_SUCCESS;
+}
+
+/* Read file */
+static pj_status_t read_file(pj_pcap_file *file,
+			     void *buf,
+			     pj_ssize_t *sz)
+{
+    pj_status_t status;
+    status = pj_file_read(file->fd, buf, sz);
+    if (status != PJ_SUCCESS)
+	return status;
+    if (*sz == 0)
+	return PJ_EEOF;
+    return PJ_SUCCESS;
+}
+
+static pj_status_t skip(pj_oshandle_t fd, pj_off_t bytes)
+{
+    pj_status_t status;
+    status = pj_file_setpos(fd, bytes, PJ_SEEK_CUR);
+    if (status != PJ_SUCCESS)
+	return status; 
+    return PJ_SUCCESS;
+}
+
+
+#define SKIP_PKT()  \
+	if (rec_incl > sz_read) { \
+	    status = skip(file->fd, rec_incl-sz_read);\
+	    if (status != PJ_SUCCESS) \
+		return status; \
+	}
+
+/* Read UDP packet */
+PJ_DEF(pj_status_t) pj_pcap_read_udp(pj_pcap_file *file,
+				     pj_uint8_t *udp_payload,
+				     pj_size_t *udp_payload_size)
+{
+    PJ_ASSERT_RETURN(file && udp_payload && udp_payload_size, PJ_EINVAL);
+    PJ_ASSERT_RETURN(*udp_payload_size, PJ_EINVAL);
+
+    /* Check data link type in PCAP file header */
+    if ((file->filter.link && 
+	    file->hdr.network != (pj_uint32_t)file->filter.link) ||
+	file->hdr.network != PJ_PCAP_LINK_TYPE_ETH)
+    {
+	/* Link header other than Ethernet is not supported for now */
+	return PJ_ENOTSUP;
+    }
+
+    /* Loop until we have the packet */
+    for (;;) {
+	union {
+	    pj_pcap_rec_hdr rec;
+	    pj_pcap_eth_hdr eth;
+	    pj_pcap_ip_hdr ip;
+	    pj_pcap_udp_hdr udp;
+	} tmp;
+	unsigned rec_incl;
+	pj_ssize_t sz;
+	unsigned sz_read = 0;
+	pj_status_t status;
+
+	/* Read PCAP packet header */
+	sz = sizeof(tmp.rec);
+	status = read_file(file, &tmp.rec, &sz);
+	if (status != PJ_SUCCESS) 
+	    return status;
+
+	rec_incl = tmp.rec.incl_len;
+
+	/* Swap byte ordering */
+	if (file->swap) {
+	    tmp.rec.incl_len = pj_ntohl(tmp.rec.incl_len);
+	    tmp.rec.orig_len = pj_ntohl(tmp.rec.orig_len);
+	    tmp.rec.ts_sec = pj_ntohl(tmp.rec.ts_sec);
+	    tmp.rec.ts_usec = pj_ntohl(tmp.rec.ts_usec);
+	}
+
+	/* Read link layer header */
+	switch (file->hdr.network) {
+	case PJ_PCAP_LINK_TYPE_ETH:
+	    sz = sizeof(tmp.eth);
+	    status = read_file(file, &tmp.eth, &sz);
+	    break;
+	default:
+	    return PJ_ENOTSUP;
+	}
+
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	sz_read += sz;
+	    
+	/* Read IP header */
+	sz = sizeof(tmp.ip);
+	status = read_file(file, &tmp.ip, &sz);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	sz_read += sz;
+
+	/* Skip if IP source mismatch */
+	if (file->filter.ip_src && tmp.ip.ip_src != file->filter.ip_src) {
+	    SKIP_PKT();
+	    continue;
+	}
+
+	/* Skip if IP destination mismatch */
+	if (file->filter.ip_dst && tmp.ip.ip_dst != file->filter.ip_dst) {
+	    SKIP_PKT();
+	    continue;
+	}
+
+	/* Skip if proto mismatch */
+	if (file->filter.proto && tmp.ip.proto != file->filter.proto) {
+	    SKIP_PKT();
+	    continue;
+	}
+
+	/* Read transport layer header */
+	switch (tmp.ip.proto) {
+	case PJ_PCAP_PROTO_TYPE_UDP:
+	    sz = sizeof(tmp.udp);
+	    status = read_file(file, &tmp.udp, &sz);
+	    if (status != PJ_SUCCESS)
+		return status;
+
+	    sz_read += sz;
+
+	    /* Skip if source port mismatch */
+	    if (file->filter.src_port && 
+	        tmp.udp.src_port != file->filter.src_port) 
+	    {
+		SKIP_PKT();
+		continue;
+	    }
+
+	    /* Skip if destination port mismatch */
+	    if (file->filter.dst_port && 
+		tmp.udp.dst_port != file->filter.dst_port) 
+	    {
+		SKIP_PKT();
+		continue;
+	    }
+
+	    /* Calculate payload size */
+	    sz = pj_ntohs(tmp.udp.len) - sizeof(tmp.udp);
+	    break;
+	default:
+	    SKIP_PKT();
+	    continue;
+	}
+
+	/* Check if payload fits the buffer */
+	if (sz > (pj_ssize_t)*udp_payload_size) {
+	    SKIP_PKT();
+	    return PJ_ETOOSMALL;
+	}
+
+	/* Read the payload */
+	status = read_file(file, udp_payload, &sz);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	sz_read += sz;
+
+	*udp_payload_size = sz;
+
+	/* Check that we've read all the packets */
+	PJ_ASSERT_RETURN(sz_read == rec_incl, PJ_EBUG);
+
+	return PJ_SUCCESS;
+    }
+
+    /* Does not reach here */
+}
+
+