1/*
2*
3* Copyright (c) 2003 Endace Technology Ltd, Hamilton, New Zealand.
4* All rights reserved.
5*
6* This software and documentation has been developed by Endace Technology Ltd.
7* along with the DAG PCI network capture cards. For further information please
8* visit http://www.endace.com/.
9*
10* Redistribution and use in source and binary forms, with or without
11* modification, are permitted provided that the following conditions are met:
12*
13*  1. Redistributions of source code must retain the above copyright notice,
14*  this list of conditions and the following disclaimer.
15*
16*  2. Redistributions in binary form must reproduce the above copyright
17*  notice, this list of conditions and the following disclaimer in the
18*  documentation and/or other materials provided with the distribution.
19*
20*  3. The name of Endace Technology Ltd may not be used to endorse or promote
21*  products derived from this software without specific prior written
22*  permission.
23*
24* THIS SOFTWARE IS PROVIDED BY ENDACE TECHNOLOGY LTD ``AS IS'' AND ANY EXPRESS
25* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
27* EVENT SHALL ENDACE TECHNOLOGY LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33* POSSIBILITY OF SUCH DAMAGE.
34*
35* $Id: erf.h 15544 2005-08-26 19:40:46Z guy $
36*/
37/*****
38 * NAME
39 *
40 *
41 * AUTHOR
42 *   taken from SCE
43 *
44 * COPYRIGHT
45 *   Copyright (c) 2004-2011 by cisco Systems, Inc.
46 *   All rights reserved.
47 *
48 * DESCRIPTION
49 *
50 ****/
51
52
53#ifndef __W_ERF_H__
54#define __W_ERF_H__
55#include "captureFile.h"
56/* Record type defines */
57#define TYPE_LEGACY	0
58#define TYPE_HDLC_POS	1
59#define TYPE_ETH	2
60#define TYPE_ATM	3
61#define TYPE_AAL5	4
62#include <stdio.h>
63
64typedef uint64_t guint64 ;
65typedef uint32_t guint32 ;
66typedef int32_t gint32;
67typedef uint16_t guint16 ;
68typedef uint8_t guint8 ;
69typedef uint8_t gchar;
70
71
72
73
74#define g_htonl PAL_NTOHL
75#define g_htons PAL_NTOHS
76#define g_ntohs PAL_NTOHS
77#define g_ntohl PAL_NTOHL
78
79
80#ifndef pntohs
81#define pntohs(p)  ((guint16)                       \
82                    ((guint16)*((const guint8 *)(p)+0)<<8|  \
83                     (guint16)*((const guint8 *)(p)+1)<<0))
84#endif
85
86#ifndef pntoh24
87#define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16| \
88                     (guint32)*((const guint8 *)(p)+1)<<8|  \
89                     (guint32)*((const guint8 *)(p)+2)<<0)
90#endif
91
92#ifndef pntohl
93#define pntohl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
94                    (guint32)*((const guint8 *)(p)+1)<<16|  \
95                    (guint32)*((const guint8 *)(p)+2)<<8|   \
96                    (guint32)*((const guint8 *)(p)+3)<<0)
97#endif
98
99#ifndef pntohll
100#define pntohll(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
101                     (guint64)*((const guint8 *)(p)+1)<<48|  \
102                     (guint64)*((const guint8 *)(p)+2)<<40|  \
103                     (guint64)*((const guint8 *)(p)+3)<<32|  \
104                     (guint64)*((const guint8 *)(p)+4)<<24|  \
105                     (guint64)*((const guint8 *)(p)+5)<<16|  \
106                     (guint64)*((const guint8 *)(p)+6)<<8|   \
107                     (guint64)*((const guint8 *)(p)+7)<<0)
108#endif
109
110
111#ifndef phtons
112#define phtons(p)  ((guint16)                       \
113                    ((guint16)*((const guint8 *)(p)+0)<<8|  \
114                     (guint16)*((const guint8 *)(p)+1)<<0))
115#endif
116
117#ifndef phtonl
118#define phtonl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
119                    (guint32)*((const guint8 *)(p)+1)<<16|  \
120                    (guint32)*((const guint8 *)(p)+2)<<8|   \
121                    (guint32)*((const guint8 *)(p)+3)<<0)
122#endif
123
124#ifndef pletohs
125#define pletohs(p) ((guint16)                       \
126                    ((guint16)*((const guint8 *)(p)+1)<<8|  \
127                     (guint16)*((const guint8 *)(p)+0)<<0))
128#endif
129
130#ifndef pletoh24
131#define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
132                     (guint32)*((const guint8 *)(p)+1)<<8|  \
133                     (guint32)*((const guint8 *)(p)+0)<<0)
134#endif
135
136
137#ifndef pletohl
138#define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
139                    (guint32)*((const guint8 *)(p)+2)<<16|  \
140                    (guint32)*((const guint8 *)(p)+1)<<8|   \
141                    (guint32)*((const guint8 *)(p)+0)<<0)
142#endif
143
144
145#ifndef pletohll
146#define pletohll(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
147                     (guint64)*((const guint8 *)(p)+6)<<48|  \
148                     (guint64)*((const guint8 *)(p)+5)<<40|  \
149                     (guint64)*((const guint8 *)(p)+4)<<32|  \
150                     (guint64)*((const guint8 *)(p)+3)<<24|  \
151                     (guint64)*((const guint8 *)(p)+2)<<16|  \
152                     (guint64)*((const guint8 *)(p)+1)<<8|   \
153                     (guint64)*((const guint8 *)(p)+0)<<0)
154#endif
155
156
157 /*
158  * The timestamp is 64bit unsigned fixed point little-endian value with
159  * 32 bits for second and 32 bits for fraction.
160  */
161typedef guint64 erf_timestamp_t;
162
163typedef struct erf_record {
164	erf_timestamp_t	ts;
165	guint8		type;
166	guint8		flags;
167	guint16		rlen;
168	guint16		lctr;
169	guint16		wlen;
170} erf_header_t;
171
172#define MAX_RECORD_LEN	0x10000 /* 64k */
173#define RECORDS_FOR_ERF_CHECK	3
174#define FCS_BITS	32
175
176#ifndef min
177#define min(a, b) ((a) > (b) ? (b) : (a))
178#endif
179
180/*
181 * ATM snaplength
182 */
183#define ATM_SNAPLEN		48
184
185/*
186 * Size of ATM payload
187 */
188#define ATM_SLEN(h, e)		ATM_SNAPLEN
189#define ATM_WLEN(h, e)		ATM_SNAPLEN
190
191/*
192 * Size of Ethernet payload
193 */
194#define ETHERNET_WLEN(h, e)	(g_htons((h)->wlen))
195#define ETHERNET_SLEN(h, e)	min(ETHERNET_WLEN(h, e), g_htons((h)->rlen) - sizeof(*(h)) - 2)
196
197/*
198 * Size of HDLC payload
199 */
200#define HDLC_WLEN(h, e)		(g_htons((h)->wlen))
201#define HDLC_SLEN(h, e)		min(HDLC_WLEN(h, e), g_htons((h)->rlen) - sizeof(*(h)))
202
203//int erf_open(wtap *wth, int *err, gchar **err_info);
204
205
206
207struct wtap {
208	FILE *			fh;
209	int			    file_type;
210	long			data_offset;
211};
212
213int erf_open(wtap *wth, int *err);
214
215int erf_read(wtap *wth,char *p,uint32_t *sec,uint32_t *nsec);
216
217
218
219class CErfFileWriter :  public CFileWriterBase {
220public:
221	virtual ~CErfFileWriter(){
222		Delete();
223	}
224    virtual bool Create(char *file_name);
225    void Delete();
226    virtual bool write_packet(CCapPktRaw * lpPacket);
227
228    /**
229     * flush all packets to disk
230     *
231     */
232    void flush_to_disk();
233
234private:
235    FILE *m_fd;
236    int m_cnt;
237
238};
239
240
241class CPcapFileWriter : CFileWriterBase{
242public:
243    bool Create(char *file_name);
244    void Delete();
245
246    bool write_packet(CCapPktRaw * lpPacket);
247private:
248    FILE *m_fd;
249    int m_cnt;
250};
251
252class CErfFileReader : public CCapReaderBase  {
253public:
254	virtual ~CErfFileReader() { Delete();}
255    bool Create(char *filename, int loops = 0);
256    void Delete();
257
258    virtual bool ReadPacket(CCapPktRaw * lpPacket);
259    virtual void Rewind();
260
261    virtual capture_type_e get_type() {
262        return ERF;
263    }
264
265private:
266    FILE * m_handle;
267};
268
269
270
271#endif /* __W_ERF_H__ */
272