18b52a31eSHanoh Haim/*
28b52a31eSHanoh Haim*
38b52a31eSHanoh Haim* Copyright (c) 2003 Endace Technology Ltd, Hamilton, New Zealand.
48b52a31eSHanoh Haim* All rights reserved.
58b52a31eSHanoh Haim*
68b52a31eSHanoh Haim* This software and documentation has been developed by Endace Technology Ltd.
78b52a31eSHanoh Haim* along with the DAG PCI network capture cards. For further information please
88b52a31eSHanoh Haim* visit http://www.endace.com/.
98b52a31eSHanoh Haim*
108b52a31eSHanoh Haim* Redistribution and use in source and binary forms, with or without
118b52a31eSHanoh Haim* modification, are permitted provided that the following conditions are met:
128b52a31eSHanoh Haim*
138b52a31eSHanoh Haim*  1. Redistributions of source code must retain the above copyright notice,
148b52a31eSHanoh Haim*  this list of conditions and the following disclaimer.
158b52a31eSHanoh Haim*
168b52a31eSHanoh Haim*  2. Redistributions in binary form must reproduce the above copyright
178b52a31eSHanoh Haim*  notice, this list of conditions and the following disclaimer in the
188b52a31eSHanoh Haim*  documentation and/or other materials provided with the distribution.
198b52a31eSHanoh Haim*
208b52a31eSHanoh Haim*  3. The name of Endace Technology Ltd may not be used to endorse or promote
218b52a31eSHanoh Haim*  products derived from this software without specific prior written
228b52a31eSHanoh Haim*  permission.
238b52a31eSHanoh Haim*
248b52a31eSHanoh Haim* THIS SOFTWARE IS PROVIDED BY ENDACE TECHNOLOGY LTD ``AS IS'' AND ANY EXPRESS
258b52a31eSHanoh Haim* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
268b52a31eSHanoh Haim* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
278b52a31eSHanoh Haim* EVENT SHALL ENDACE TECHNOLOGY LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
288b52a31eSHanoh Haim* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
298b52a31eSHanoh Haim* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
308b52a31eSHanoh Haim* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
318b52a31eSHanoh Haim* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
328b52a31eSHanoh Haim* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
338b52a31eSHanoh Haim* POSSIBILITY OF SUCH DAMAGE.
348b52a31eSHanoh Haim*
358b52a31eSHanoh Haim* $Id: erf.c 15544 2005-08-26 19:40:46Z guy $
368b52a31eSHanoh Haim*/
378b52a31eSHanoh Haim
388b52a31eSHanoh Haim/*****
398b52a31eSHanoh Haim * NAME
408b52a31eSHanoh Haim *
418b52a31eSHanoh Haim *
428b52a31eSHanoh Haim * AUTHOR
438b52a31eSHanoh Haim *   taken from SCE
448b52a31eSHanoh Haim *
458b52a31eSHanoh Haim * COPYRIGHT
468b52a31eSHanoh Haim *   Copyright (c) 2004-2011 by cisco Systems, Inc.
478b52a31eSHanoh Haim *   All rights reserved.
488b52a31eSHanoh Haim *
498b52a31eSHanoh Haim * DESCRIPTION
508b52a31eSHanoh Haim *
518b52a31eSHanoh Haim ****/
528b52a31eSHanoh Haim
538b52a31eSHanoh Haim/*
548b52a31eSHanoh Haim * erf - Endace ERF (Extensible Record Format)
558b52a31eSHanoh Haim *
568b52a31eSHanoh Haim * See
578b52a31eSHanoh Haim *
588b52a31eSHanoh Haim *	http://www.endace.com/support/EndaceRecordFormat.pdf
598b52a31eSHanoh Haim */
608b52a31eSHanoh Haim
618b52a31eSHanoh Haim
628b52a31eSHanoh Haim
638b52a31eSHanoh Haim#include <stdlib.h>
648b52a31eSHanoh Haim#include <string.h>
658b52a31eSHanoh Haim#include "erf.h"
668b52a31eSHanoh Haim#include "basic_utils.h"
678b52a31eSHanoh Haim#include "pal_utl.h"
688b52a31eSHanoh Haim
698b52a31eSHanoh Haim
708b52a31eSHanoh Haim#define MAX_ERF_PACKET   READER_MAX_PACKET_SIZE
718b52a31eSHanoh Haim
728b52a31eSHanoh Haim
738b52a31eSHanoh Haimextern long file_seek(void *stream, long offset, int whence, int *err);
748b52a31eSHanoh Haim#define file_read fread
758b52a31eSHanoh Haim#define file_write fwrite
768b52a31eSHanoh Haim#define file_close fclose
778b52a31eSHanoh Haimextern int file_error(FILE *fh);
788b52a31eSHanoh Haim#define file_getc fgetc
798b52a31eSHanoh Haim#define file_gets fgets
808b52a31eSHanoh Haim#define file_eof feof
818b52a31eSHanoh Haim
828b52a31eSHanoh Haim
838b52a31eSHanoh Haimlong file_seek(FILE *stream, long offset, int whence, int *err)
848b52a31eSHanoh Haim{
858b52a31eSHanoh Haim	long ret;
868b52a31eSHanoh Haim
878b52a31eSHanoh Haim	ret = CAP_FSEEK_64(stream, offset, whence);
888b52a31eSHanoh Haim	if (ret == -1)
898b52a31eSHanoh Haim		*err = file_error(stream);
908b52a31eSHanoh Haim	return ret;
918b52a31eSHanoh Haim}
928b52a31eSHanoh Haim
938b52a31eSHanoh Haim
948b52a31eSHanoh Haimint file_error(FILE *fh)
958b52a31eSHanoh Haim{
968b52a31eSHanoh Haim	if (ferror(fh))
978b52a31eSHanoh Haim		return -1;
988b52a31eSHanoh Haim	else
998b52a31eSHanoh Haim		return 0;
1008b52a31eSHanoh Haim}
1018b52a31eSHanoh Haim
1028b52a31eSHanoh Haim
1038b52a31eSHanoh Haimint erf_open(wtap *wth, int *err)
1048b52a31eSHanoh Haim{
1058b52a31eSHanoh Haim	guint32 i;
1068b52a31eSHanoh Haim	int common_type = 0;
1078b52a31eSHanoh Haim	erf_timestamp_t prevts;
1088b52a31eSHanoh Haim
1098b52a31eSHanoh Haim	memset(&prevts, 0, sizeof(prevts));
1108b52a31eSHanoh Haim
11104eae221Simarom    long records_for_erf_check = 10;
1128b52a31eSHanoh Haim
1138b52a31eSHanoh Haim	/* ERF is a little hard because there's no magic number */
1148b52a31eSHanoh Haim
1158b52a31eSHanoh Haim	for (i = 0; i < (guint32)records_for_erf_check; i++) {
1168b52a31eSHanoh Haim
1178b52a31eSHanoh Haim		erf_header_t header;
1188b52a31eSHanoh Haim		guint32 packet_size;
1198b52a31eSHanoh Haim		erf_timestamp_t ts;
1208b52a31eSHanoh Haim
1218b52a31eSHanoh Haim		if (file_read(&header,1,sizeof(header),wth->fh) != sizeof(header)) {
1228b52a31eSHanoh Haim			if ((*err = file_error(wth->fh)) != 0)
1238b52a31eSHanoh Haim				return -1;
1248b52a31eSHanoh Haim			else
1258b52a31eSHanoh Haim				break; /* eof */
1268b52a31eSHanoh Haim		}
1278b52a31eSHanoh Haim
1288b52a31eSHanoh Haim		packet_size = g_ntohs(header.rlen) - sizeof(header);
1298b52a31eSHanoh Haim
1308b52a31eSHanoh Haim		/* fail on invalid record type, decreasing timestamps or non-zero pad-bits */
1318b52a31eSHanoh Haim		if (header.type == 0 || header.type != TYPE_ETH ||
1328b52a31eSHanoh Haim		    (header.flags & 0xc0) != 0) {
1338b52a31eSHanoh Haim			return 0;
1348b52a31eSHanoh Haim		}
1358b52a31eSHanoh Haim
1368b52a31eSHanoh Haim		if ((ts = pletohll(&header.ts)) < prevts) {
1378b52a31eSHanoh Haim			/* reassembled AAL5 records may not be in time order, so allow 1 sec fudge */
1388b52a31eSHanoh Haim			if (header.type != TYPE_AAL5 || ((prevts-ts)>>32) > 1) {
1398b52a31eSHanoh Haim				return 0;
1408b52a31eSHanoh Haim			}
1418b52a31eSHanoh Haim		}
1428b52a31eSHanoh Haim		memcpy(&prevts, &ts, sizeof(prevts));
1438b52a31eSHanoh Haim
1448b52a31eSHanoh Haim		if (common_type == 0) {
1458b52a31eSHanoh Haim			common_type = header.type;
1468b52a31eSHanoh Haim		} else
1478b52a31eSHanoh Haim		if (common_type > 0 && common_type != header.type) {
1488b52a31eSHanoh Haim			common_type = -1;
1498b52a31eSHanoh Haim		}
1508b52a31eSHanoh Haim
1518b52a31eSHanoh Haim		if (header.type == TYPE_HDLC_POS ) {
1528b52a31eSHanoh Haim            // do not support HDLS
1538b52a31eSHanoh Haim            return (-1);
1548b52a31eSHanoh Haim		}
1558b52a31eSHanoh Haim        if (file_seek(wth->fh, packet_size, SEEK_CUR, err) == -1) {
1568b52a31eSHanoh Haim            return -1;
1578b52a31eSHanoh Haim        }
1588b52a31eSHanoh Haim	}
1598b52a31eSHanoh Haim
1608b52a31eSHanoh Haim	if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) {	/* rewind */
1618b52a31eSHanoh Haim		return -1;
1628b52a31eSHanoh Haim	}
1638b52a31eSHanoh Haim	wth->data_offset = 0;
1648b52a31eSHanoh Haim    // VALID ERF file
1658b52a31eSHanoh Haim	return 1;
1668b52a31eSHanoh Haim}
1678b52a31eSHanoh Haim
1688b52a31eSHanoh Haim
16904eae221Simaromint erf_read(wtap *wth,char *p,uint32_t *sec,uint32_t *nsec, uint8_t *interface)
1708b52a31eSHanoh Haim{
1718b52a31eSHanoh Haim    erf_header_t header;
1728b52a31eSHanoh Haim    int common_type = 0;
1738b52a31eSHanoh Haim    if (file_read(&header,1,sizeof(header),wth->fh) != sizeof(header)) {
1748b52a31eSHanoh Haim        if (( file_error(wth->fh)) != 0)
1758b52a31eSHanoh Haim            return -1;
1768b52a31eSHanoh Haim        else
1778b52a31eSHanoh Haim            return (0); // end
1788b52a31eSHanoh Haim    }
1798b52a31eSHanoh Haim
1808b52a31eSHanoh Haim    guint32 packet_size = g_ntohs(header.rlen) - sizeof(header);
1818b52a31eSHanoh Haim
1828b52a31eSHanoh Haim    /* fail on invalid record type, decreasing timestamps or non-zero pad-bits */
1838b52a31eSHanoh Haim    if ( header.type != TYPE_ETH ||
1848b52a31eSHanoh Haim        (header.flags & 0xc0) != 0) {
1858b52a31eSHanoh Haim        //printf(" ERF header not supported \n");
1868b52a31eSHanoh Haim        // no valid
1878b52a31eSHanoh Haim        return -1;
1888b52a31eSHanoh Haim    }
1898b52a31eSHanoh Haim
1908b52a31eSHanoh Haim    if (common_type == 0) {
1918b52a31eSHanoh Haim        common_type = header.type;
1928b52a31eSHanoh Haim    } else
1938b52a31eSHanoh Haim    if (common_type > 0 && common_type != header.type) {
1948b52a31eSHanoh Haim        common_type = -1;
1958b52a31eSHanoh Haim    }
1968b52a31eSHanoh Haim
1978b52a31eSHanoh Haim
1988b52a31eSHanoh Haim    if ( (  packet_size >= MAX_ERF_PACKET ) ||
1998b52a31eSHanoh Haim        (g_ntohs(header.wlen)>MAX_ERF_PACKET) ) {
2008b52a31eSHanoh Haim        printf(" ERF packet size too big  \n");
2018b52a31eSHanoh Haim		assert(0);
2028b52a31eSHanoh Haim        return (-1);
2038b52a31eSHanoh Haim    }
2048b52a31eSHanoh Haim
2058b52a31eSHanoh Haim    int err;
2068b52a31eSHanoh Haim    if (file_seek(wth->fh, 2, SEEK_CUR, &err) == -1) {
2078b52a31eSHanoh Haim        return -1;
2088b52a31eSHanoh Haim    }
2098b52a31eSHanoh Haim    int realpkt_size = packet_size-2;
2108b52a31eSHanoh Haim
2118b52a31eSHanoh Haim    if (file_read(p,1,realpkt_size ,wth->fh) == realpkt_size) {
2128b52a31eSHanoh Haim        guint64 ts = pletohll(&header.ts);
2138b52a31eSHanoh Haim        *sec = (uint32_t) (ts >> 32);
2148b52a31eSHanoh Haim        uint32_t frac =(ts &0xffffffff);
2158b52a31eSHanoh Haim        double usec_frac =(double)frac*(1000000000.0/(4294967296.0));
2168b52a31eSHanoh Haim        *nsec = (uint32_t) (usec_frac);
21704eae221Simarom        *interface = header.flags & 0x3;
2188b52a31eSHanoh Haim        return (g_ntohs(header.wlen));
2198b52a31eSHanoh Haim    }else{
2208b52a31eSHanoh Haim        return (-1);
2218b52a31eSHanoh Haim    }
2228b52a31eSHanoh Haim}
2238b52a31eSHanoh Haim
2248b52a31eSHanoh Haim
2258b52a31eSHanoh Haimbool CErfFileWriter::Create(char *file_name){
2268b52a31eSHanoh Haim    m_fd=CAP_FOPEN_64(file_name,"wb");
2278b52a31eSHanoh Haim    if (m_fd==0) {
2288b52a31eSHanoh Haim        printf(" ERROR create file \n");
2298b52a31eSHanoh Haim        return(false);
2308b52a31eSHanoh Haim    }
2318b52a31eSHanoh Haim    m_cnt=0;
2328b52a31eSHanoh Haim    return(true);
2338b52a31eSHanoh Haim}
2348b52a31eSHanoh Haim
2358b52a31eSHanoh Haimvoid CErfFileWriter::Delete(){
2368b52a31eSHanoh Haim    if (m_fd) {
2378b52a31eSHanoh Haim       fclose(m_fd);
2388b52a31eSHanoh Haim       m_fd=0;
2398b52a31eSHanoh Haim    }
2408b52a31eSHanoh Haim}
2418b52a31eSHanoh Haim
2428b52a31eSHanoh Haim
2438b52a31eSHanoh Haimtypedef struct erf_dummy_header_ {
2448b52a31eSHanoh Haim    uint16_t dummy;
2458b52a31eSHanoh Haim}erf_dummy_header_t ;
2468b52a31eSHanoh Haim
2478b52a31eSHanoh Haimstatic uint32_t frame_check[20];
2488b52a31eSHanoh Haim
2498b52a31eSHanoh Haimbool CErfFileWriter::write_packet(CCapPktRaw * lpPacket){
2508b52a31eSHanoh Haim    erf_header_t header;
2518b52a31eSHanoh Haim    erf_dummy_header_t dummy;
2528b52a31eSHanoh Haim
2538b52a31eSHanoh Haim    dummy.dummy =0;
2548b52a31eSHanoh Haim    memset(&header,0,sizeof(erf_header_t));
2558b52a31eSHanoh Haim    double nsec_frac = 4294967295.9 *(lpPacket->time_nsec /1000000000.0);
2568b52a31eSHanoh Haim    uint64_t ts= (((uint64_t)lpPacket->time_sec) <<32) +((uint32_t)nsec_frac);
2578b52a31eSHanoh Haim    header.ts =ts;
2588b52a31eSHanoh Haim
2598b52a31eSHanoh Haim    uint16_t size=lpPacket->pkt_len;
2608b52a31eSHanoh Haim
2618b52a31eSHanoh Haim    uint16_t total_size=(uint16_t)size+sizeof(erf_header_t)+2+4;
2628b52a31eSHanoh Haim    uint16_t align = (total_size & 0x7);
2638b52a31eSHanoh Haim    if (align >0 ) {
2648b52a31eSHanoh Haim        align = 8-align;
2658b52a31eSHanoh Haim    }
2668b52a31eSHanoh Haim
2678b52a31eSHanoh Haim    header.flags =4+lpPacket->getInterface();
2688b52a31eSHanoh Haim    header.type =TYPE_ETH;
2698b52a31eSHanoh Haim    header.wlen = g_ntohs((uint16_t)size+4);
2708b52a31eSHanoh Haim    header.rlen = g_ntohs(total_size+align);
2718b52a31eSHanoh Haim
2728b52a31eSHanoh Haim    int n = fwrite(&header,1,sizeof(header),m_fd);
2738b52a31eSHanoh Haim    n+= fwrite(&dummy,1,sizeof(dummy),m_fd);
2748b52a31eSHanoh Haim    n+= fwrite(lpPacket->raw,1,size,m_fd);
2758b52a31eSHanoh Haim    n+= fwrite(frame_check,1,4+align,m_fd);
2768b52a31eSHanoh Haim
2778b52a31eSHanoh Haim	if (n < (int)(total_size+align)) {
2788b52a31eSHanoh Haim		return false;
2798b52a31eSHanoh Haim	}
2808b52a31eSHanoh Haim	return true;
2818b52a31eSHanoh Haim}
2828b52a31eSHanoh Haim
283537f5831Simaromvoid CErfFileWriter::flush_to_disk() {
284537f5831Simarom    if (m_fd) {
285537f5831Simarom        fflush(m_fd);
286537f5831Simarom    }
287537f5831Simarom}
2888b52a31eSHanoh Haim
2898b52a31eSHanoh Haimbool CPcapFileWriter::Create(char *file_name){
2908b52a31eSHanoh Haim    m_fd=CAP_FOPEN_64(file_name,"wb");
2918b52a31eSHanoh Haim    if (m_fd==0) {
2928b52a31eSHanoh Haim        printf(" ERROR create file \n");
2938b52a31eSHanoh Haim        return(false);
2948b52a31eSHanoh Haim    }
2958b52a31eSHanoh Haim    m_cnt=0;
2968b52a31eSHanoh Haim    return(true);
2978b52a31eSHanoh Haim}
2988b52a31eSHanoh Haim
2998b52a31eSHanoh Haimvoid CPcapFileWriter::Delete(){
3008b52a31eSHanoh Haim    if (m_fd) {
3018b52a31eSHanoh Haim       fclose(m_fd);
3028b52a31eSHanoh Haim       m_fd=0;
3038b52a31eSHanoh Haim    }
3048b52a31eSHanoh Haim}
3058b52a31eSHanoh Haim
3068b52a31eSHanoh Haim
3078b52a31eSHanoh Haim
3088b52a31eSHanoh Haim
3098b52a31eSHanoh Haim#define	PCAP_MAGIC			0xa1b2c3d4
3108b52a31eSHanoh Haim#define	PCAP_SWAPPED_MAGIC		0xd4c3b2a1
3118b52a31eSHanoh Haim#define	PCAP_MODIFIED_MAGIC		0xa1b2cd34
3128b52a31eSHanoh Haim#define	PCAP_SWAPPED_MODIFIED_MAGIC	0x34cdb2a1
3138b52a31eSHanoh Haim#define	PCAP_NSEC_MAGIC			0xa1b23c4d
3148b52a31eSHanoh Haim#define	PCAP_SWAPPED_NSEC_MAGIC		0x4d3cb2a1
3158b52a31eSHanoh Haim
3168b52a31eSHanoh Haim/* "libpcap" file header (minus magic number). */
3178b52a31eSHanoh Haimstruct pcap_hdr {
3188b52a31eSHanoh Haim    guint32 magic_number;
3198b52a31eSHanoh Haim	guint16	version_major;	/* major version number */
3208b52a31eSHanoh Haim	guint16	version_minor;	/* minor version number */
3218b52a31eSHanoh Haim	gint32	thiszone;	/* GMT to local correction */
3228b52a31eSHanoh Haim	guint32	sigfigs;	/* accuracy of timestamps */
3238b52a31eSHanoh Haim	guint32	snaplen;	/* max length of captured packets, in octets */
3248b52a31eSHanoh Haim	guint32	network;	/* data link type */
3258b52a31eSHanoh Haim};
3268b52a31eSHanoh Haim
3278b52a31eSHanoh Haim/* "libpcap" record header. */
3288b52a31eSHanoh Haimstruct pcaprec_hdr {
3298b52a31eSHanoh Haim	guint32	ts_sec;		/* timestamp seconds */
3308b52a31eSHanoh Haim	guint32	ts_usec;	/* timestamp microseconds (nsecs for PCAP_NSEC_MAGIC) */
3318b52a31eSHanoh Haim	guint32	incl_len;	/* number of octets of packet saved in file */
3328b52a31eSHanoh Haim	guint32	orig_len;	/* actual length of packet */
3338b52a31eSHanoh Haim};
3348b52a31eSHanoh Haim
3358b52a31eSHanoh Haim
3368b52a31eSHanoh Haimbool CPcapFileWriter::write_packet(CCapPktRaw * lpPacket){
3378b52a31eSHanoh Haim    if (m_cnt == 0) {
3388b52a31eSHanoh Haim        pcap_hdr header;
3398b52a31eSHanoh Haim        header.magic_number   = PCAP_NSEC_MAGIC;
3408b52a31eSHanoh Haim        header.version_major  = 0x0002;
3418b52a31eSHanoh Haim        header.version_minor  = 0x0004;
3428b52a31eSHanoh Haim        header.thiszone       = 0;
3438b52a31eSHanoh Haim        header.sigfigs        = 0;
3448b52a31eSHanoh Haim        header.snaplen        = 2000;
3458b52a31eSHanoh Haim        header.network        = 1;
3468b52a31eSHanoh Haim        fwrite(&header,1,sizeof(header),m_fd);
3478b52a31eSHanoh Haim    }
3488b52a31eSHanoh Haim    pcaprec_hdr pkt_header;
3498b52a31eSHanoh Haim    pkt_header.ts_sec   = lpPacket->time_sec ;
3508b52a31eSHanoh Haim    pkt_header.ts_usec  = lpPacket->time_nsec;
3518b52a31eSHanoh Haim    pkt_header.incl_len = lpPacket->pkt_len;
3528b52a31eSHanoh Haim    pkt_header.orig_len = lpPacket->pkt_len;
3538b52a31eSHanoh Haim    fwrite(&pkt_header,1,sizeof(pkt_header),m_fd);
3548b52a31eSHanoh Haim    fwrite(lpPacket->raw,1,lpPacket->pkt_len,m_fd);
3558b52a31eSHanoh Haim    m_cnt++;
3568b52a31eSHanoh Haim	return true;
3578b52a31eSHanoh Haim}
3588b52a31eSHanoh Haim
3598b52a31eSHanoh Haim
3608b52a31eSHanoh Haim
3618b52a31eSHanoh Haim#if 0
3628b52a31eSHanoh Haim //erf_create(wtap *wth,char *p,uint32_t *sec)
3638b52a31eSHanoh Haim
3648b52a31eSHanoh Haimstatic uint8_t DataPacket0[]={
3658b52a31eSHanoh Haim
3668b52a31eSHanoh Haim0x00, 0x50, 0x04, 0xB9 ,0xC8, 0xA0,
3678b52a31eSHanoh Haim0x00, 0x50, 0x04, 0xB9, 0xC5, 0x83,
3688b52a31eSHanoh Haim0x08, 0x00,
3698b52a31eSHanoh Haim
3708b52a31eSHanoh Haim0x45, 0x10, 0x00, 0x40,
3718b52a31eSHanoh Haim0x00, 0x00, 0x40, 0x00,
3728b52a31eSHanoh Haim0x80, 0x06, 0xDD, 0x99,
3738b52a31eSHanoh Haim
3748b52a31eSHanoh Haim0x0A, 0x01, 0x04, 0x91,
3758b52a31eSHanoh Haim0x0A, 0x01, 0x04, 0x90,
3768b52a31eSHanoh Haim
3778b52a31eSHanoh Haim0x05, 0x11,
3788b52a31eSHanoh Haim0x00, 0x50,
3798b52a31eSHanoh Haim
3808b52a31eSHanoh Haim0x00, 0x00, 0xF9, 0x00,
3818b52a31eSHanoh Haim0x00, 0x00, 0x00, 0x00,
3828b52a31eSHanoh Haim
3838b52a31eSHanoh Haim0x60, 0x00, 0x20, 0x00,
3848b52a31eSHanoh Haim0x5C, 0xA2, 0x00, 0x00,
3858b52a31eSHanoh Haim0x02, 0x04, 0x05, 0xB4,
3868b52a31eSHanoh Haim0x00, 0x00, 0x76, 0x4A,
3878b52a31eSHanoh Haim
3888b52a31eSHanoh Haim0x60, 0x02, 0x20, 0x00,
3898b52a31eSHanoh Haim0x5C, 0xA2, 0x00, 0x00,
3908b52a31eSHanoh Haim0x02, 0x04, 0x05, 0xB4,
3918b52a31eSHanoh Haim0x00, 0x00, 0x76, 0x4A,
3928b52a31eSHanoh Haim0x60, 0x02, 0x20, 0x00,
3938b52a31eSHanoh Haim0x5C, 0xA2, 0x00, 0x00,
3948b52a31eSHanoh Haim0x02, 0x04, 0x05, 0xB4,
3958b52a31eSHanoh Haim0x00, 0x00, 0x76, 0x4A,
3968b52a31eSHanoh Haim
3978b52a31eSHanoh Haim};
3988b52a31eSHanoh Haim
3998b52a31eSHanoh Haim
4008b52a31eSHanoh Haimvoid test_erf_create(void){
4018b52a31eSHanoh Haim    CPcapFileWriter erf ;
4028b52a31eSHanoh Haim    erf.Create("my_test.erf");
4038b52a31eSHanoh Haim    int i;
4048b52a31eSHanoh Haim    for (i=0; i<10; i++) {
4058b52a31eSHanoh Haim        erf.write_packet((char *)&DataPacket0[0],sizeof(DataPacket0));
4068b52a31eSHanoh Haim    }
4078b52a31eSHanoh Haim    erf.Delete();
4088b52a31eSHanoh Haim}
4098b52a31eSHanoh Haim#endif
4108b52a31eSHanoh Haim
4118b52a31eSHanoh Haim
4128b52a31eSHanoh Haimbool CErfFileReader::Create(char *filename, int loops){
4138b52a31eSHanoh Haim
4148b52a31eSHanoh Haim    this->m_loops = loops;
4158b52a31eSHanoh Haim	m_handle = CAP_FOPEN_64(filename, "rb");
4168b52a31eSHanoh Haim    if (m_handle == NULL) {
4178b52a31eSHanoh Haim        fprintf(stderr, "Failed to open file `%s'.\n", filename);
4188b52a31eSHanoh Haim        return false;
4198b52a31eSHanoh Haim    }
4208b52a31eSHanoh Haim
4218b52a31eSHanoh Haim
4228b52a31eSHanoh Haim    CAP_FSEEK_64 (m_handle, 0, SEEK_END);
4238b52a31eSHanoh Haim    m_file_size = CAP_FTELL_64(m_handle);
4248b52a31eSHanoh Haim    rewind (m_handle);
4258b52a31eSHanoh Haim
4268b52a31eSHanoh Haim    wtap wth;
4278b52a31eSHanoh Haim    memset(&wth,0,sizeof(wtap));
4288b52a31eSHanoh Haim    int err=0;
4298b52a31eSHanoh Haim    wth.fh =m_handle;
4308b52a31eSHanoh Haim    if ( erf_open(&wth, &err)== 1){
4318b52a31eSHanoh Haim        return (true);
4328b52a31eSHanoh Haim    }else{
4338b52a31eSHanoh Haim        return (false);
4348b52a31eSHanoh Haim    }
4358b52a31eSHanoh Haim}
4368b52a31eSHanoh Haim
4378b52a31eSHanoh Haimvoid CErfFileReader::Delete(){
4388b52a31eSHanoh Haim    if (m_handle) {
4398b52a31eSHanoh Haim        fclose(m_handle);
4408b52a31eSHanoh Haim        m_handle=0;
4418b52a31eSHanoh Haim    }
4428b52a31eSHanoh Haim}
4438b52a31eSHanoh Haim
44432323865Simaromvoid CErfFileReader::Rewind() {
44532323865Simarom   if (m_handle) {
44632323865Simarom       rewind(m_handle);
44732323865Simarom   }
44832323865Simarom}
44932323865Simarom
4508b52a31eSHanoh Haim
4518b52a31eSHanoh Haimbool CErfFileReader::ReadPacket(CCapPktRaw * lpPacket){
45204eae221Simarom    uint8_t interface;
4538b52a31eSHanoh Haim    wtap wth;
45404eae221Simarom
4558b52a31eSHanoh Haim    wth.fh = m_handle;
45604eae221Simarom    int length = erf_read(&wth,
45704eae221Simarom                          lpPacket->raw,
45804eae221Simarom                          &lpPacket->time_sec,
45904eae221Simarom                          &lpPacket->time_nsec,
46004eae221Simarom                          &interface);
4618b52a31eSHanoh Haim    if ( length >0   ) {
4628b52a31eSHanoh Haim        lpPacket->pkt_len =(uint16_t)length;
4630d4c9932Simarom        lpPacket->pkt_cnt++;
46404eae221Simarom        lpPacket->setInterface(interface);
4658b52a31eSHanoh Haim        return (true);
4668b52a31eSHanoh Haim    }
4678b52a31eSHanoh Haim    return (false);
4688b52a31eSHanoh Haim}
4698b52a31eSHanoh Haim
470