3 * Copyright (c) 2003 Endace Technology Ltd, Hamilton, New Zealand.
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/.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
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.
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
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.
39 * erf - Endace ERF (Extensible Record Format)
43 * http://www.endace.com/support/EndaceRecordFormat.pdf
54 #include "file_wrappers.h"
60 #define min(a, b) ((a) > (b) ? (b) : (a))
63 static int erf_read_header(FILE_T fh,
64 struct wtap_pkthdr *phdr,
65 union wtap_pseudo_header *pseudo_header,
66 erf_header_t *erf_header,
70 guint32 *packet_size);
71 static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
73 static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
74 union wtap_pseudo_header *pseudo_header, guchar *pd,
75 int length, int *err, gchar **err_info);
77 extern int erf_open(wtap *wth, int *err, gchar **err_info _U_)
79 int i, n, records_for_erf_check = RECORDS_FOR_ERF_CHECK;
81 erf_timestamp_t prevts,ts;
87 guint64 erf_ext_header;
92 memset(&prevts, 0, sizeof(prevts));
94 /* number of records to scan before deciding if this really is ERF */
95 if ((s = getenv("ERF_RECORDS_TO_CHECK")) != NULL) {
96 if ((n = atoi(s)) > 0 && n < 101) {
97 records_for_erf_check = n;
102 * ERF is a little hard because there's no magic number; we look at
103 * the first few records and see if they look enough like ERF
107 for (i = 0; i < records_for_erf_check; i++) { /* records_for_erf_check */
109 r = file_read(&header,1,sizeof(header),wth->fh);
112 if (r != sizeof(header)) {
113 if ((*err = file_error(wth->fh)) != 0) {
116 /* ERF header too short accept the file,
117 only if the very first records have been successfully checked */
118 if (i < MIN_RECORDS_FOR_ERF_CHECK) {
121 /* BREAK, the last record is too short, and will be ignored */
127 rlen=g_ntohs(header.rlen);
128 wlen=g_ntohs(header.wlen);
129 packet_size = rlen - (guint32)sizeof(header);
131 /* fail on invalid record type, invalid rlen, timestamps decreasing, or incrementing too far */
133 /* Test valid rlen >= 16 */
138 if (packet_size > WTAP_MAX_PACKET_SIZE) {
140 * Probably a corrupt capture file; don't blow up trying
141 * to allocate space for an immensely-large packet.
146 /* Skip PAD records, timestamps may not be set */
147 if ((header.type & 0x7F) == ERF_TYPE_PAD) {
148 if (file_seek(wth->fh, packet_size, SEEK_CUR, err) == -1) {
154 /* fail on invalid record type, decreasing timestamps or non-zero pad-bits */
155 /* Not all types within this range are decoded, but it is a first filter */
156 if ((header.type & 0x7F) == 0 || (header.type & 0x7F) > ERF_TYPE_MAX ) {
160 /* The ERF_TYPE_MAX is the PAD record, but the last used type is ERF_TYPE_RAW_LINK */
161 if ((header.type & 0x7F) > ERF_TYPE_RAW_LINK) {
165 if ((ts = pletohll(&header.ts)) < prevts) {
166 /* reassembled AALx records may not be in time order, also records are not in strict time order between physical interfaces, so allow 1 sec fudge */
167 if ( ((prevts-ts)>>32) > 1 ) {
172 /* Check to see if timestamp increment is > 1 week */
173 if ( (i) && (ts > prevts) && (((ts-prevts)>>32) > 3600*24*7) ) {
177 memcpy(&prevts, &ts, sizeof(prevts));
179 /* Read over the extension headers */
182 if (file_read(&erf_ext_header, 1, sizeof(erf_ext_header),wth->fh) != sizeof(erf_ext_header)) {
183 *err = file_error(wth->fh);
186 packet_size -= (guint32)sizeof(erf_ext_header);
187 memcpy(&type, &erf_ext_header, sizeof(type));
191 /* Read over MC or ETH subheader */
192 switch(header.type & 0x7F) {
193 case ERF_TYPE_MC_HDLC:
194 case ERF_TYPE_MC_RAW:
195 case ERF_TYPE_MC_ATM:
196 case ERF_TYPE_MC_RAW_CHANNEL:
197 case ERF_TYPE_MC_AAL5:
198 case ERF_TYPE_MC_AAL2:
199 case ERF_TYPE_COLOR_MC_HDLC_POS:
200 if (file_read(&mc_hdr,1,sizeof(mc_hdr),wth->fh) != sizeof(mc_hdr)) {
201 *err = file_error(wth->fh);
204 packet_size -= (guint32)sizeof(mc_hdr);
207 case ERF_TYPE_COLOR_ETH:
208 case ERF_TYPE_DSM_COLOR_ETH:
209 if (file_read(ð_hdr,1,sizeof(eth_hdr),wth->fh) != sizeof(eth_hdr)) {
210 *err = file_error(wth->fh);
213 packet_size -= (guint32)sizeof(eth_hdr);
219 /* The file_seek function do not return an error if the end of file
220 is reached whereas the record is truncated */
221 buffer=g_malloc(packet_size);
222 r = file_read(buffer, 1, packet_size, wth->fh);
225 if (r != packet_size) {
226 /* ERF record too short, accept the file,
227 only if the very first records have been successfully checked */
228 if (i < MIN_RECORDS_FOR_ERF_CHECK) {
233 } /* records_for_erf_check */
235 if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) { /* rewind */
239 wth->data_offset = 0;
241 /* This is an ERF file */
242 wth->file_type = WTAP_FILE_ERF;
243 wth->snapshot_length = 0; /* not available in header, only in frame */
246 * Use the encapsulation for ERF records.
248 wth->file_encap = WTAP_ENCAP_ERF;
250 wth->subtype_read = erf_read;
251 wth->subtype_seek_read = erf_seek_read;
252 wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
257 /* Read the next packet */
258 static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
261 erf_header_t erf_header;
262 guint32 packet_size, bytes_read;
264 *data_offset = wth->data_offset;
267 if (!erf_read_header(wth->fh,
268 &wth->phdr, &wth->pseudo_header, &erf_header,
269 err, err_info, &bytes_read, &packet_size)) {
272 wth->data_offset += bytes_read;
273 } while ( erf_header.type == ERF_TYPE_PAD );
275 buffer_assure_space(wth->frame_buffer, packet_size);
277 wtap_file_read_expected_bytes(buffer_start_ptr(wth->frame_buffer),
278 (gint32)(packet_size), wth->fh, err );
279 wth->data_offset += packet_size;
284 static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
285 union wtap_pseudo_header *pseudo_header, guchar *pd,
286 int length _U_, int *err, gchar **err_info)
288 erf_header_t erf_header;
291 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
295 if (!erf_read_header(wth->random_fh, NULL, pseudo_header, &erf_header,
296 err, err_info, NULL, &packet_size))
298 } while ( erf_header.type == ERF_TYPE_PAD );
300 wtap_file_read_expected_bytes(pd, (int)packet_size, wth->random_fh, err);
305 static int erf_read_header(FILE_T fh,
306 struct wtap_pkthdr *phdr,
307 union wtap_pseudo_header *pseudo_header,
308 erf_header_t *erf_header,
312 guint32 *packet_size)
316 guint64 erf_exhdr_sw;
320 int i = 0 , max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
322 wtap_file_read_expected_bytes(erf_header, sizeof(*erf_header), fh, err);
323 if (bytes_read != NULL) {
324 *bytes_read = sizeof(*erf_header);
327 *packet_size = g_ntohs(erf_header->rlen) - (guint32)sizeof(*erf_header);
329 if (*packet_size > WTAP_MAX_PACKET_SIZE) {
331 * Probably a corrupt capture file; don't blow up trying
332 * to allocate space for an immensely-large packet.
334 *err = WTAP_ERR_BAD_RECORD;
335 *err_info = g_strdup_printf("erf: File has %u-byte packet, bigger than maximum of %u",
336 *packet_size, WTAP_MAX_PACKET_SIZE);
341 guint64 ts = pletohll(&erf_header->ts);
343 phdr->ts.secs = (long) (ts >> 32);
344 ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
345 ts += (ts & 0x80000000) << 1; /* rounding */
346 phdr->ts.nsecs = ((int) (ts >> 32));
347 if (phdr->ts.nsecs >= 1000000000) {
348 phdr->ts.nsecs -= 1000000000;
353 /* Copy the ERF pseudo header */
354 pseudo_header->erf.phdr.ts = pletohll(&erf_header->ts);
355 pseudo_header->erf.phdr.type = erf_header->type;
356 pseudo_header->erf.phdr.flags = erf_header->flags;
357 pseudo_header->erf.phdr.rlen = g_ntohs(erf_header->rlen);
358 pseudo_header->erf.phdr.lctr = g_ntohs(erf_header->lctr);
359 pseudo_header->erf.phdr.wlen = g_ntohs(erf_header->wlen);
361 /* Copy the ERF extension header into the pseudo header */
362 type = erf_header->type;
364 wtap_file_read_expected_bytes(&erf_exhdr, sizeof(erf_exhdr), fh, err);
365 if (bytes_read != NULL)
366 *bytes_read += (guint32)sizeof(erf_exhdr);
367 *packet_size -= (guint32)sizeof(erf_exhdr);
368 skiplen += (guint32)sizeof(erf_exhdr);
369 erf_exhdr_sw = pntohll((guint64*) &(erf_exhdr[0]));
371 memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
376 switch (erf_header->type & 0x7F) {
379 case ERF_TYPE_RAW_LINK:
380 case ERF_TYPE_INFINIBAND:
383 phdr->len = g_htons(erf_header->wlen);
384 phdr->caplen = g_htons(erf_header->wlen);
390 case ERF_TYPE_HDLC_POS:
391 case ERF_TYPE_COLOR_HDLC_POS:
392 case ERF_TYPE_DSM_COLOR_HDLC_POS:
399 case ERF_TYPE_COLOR_ETH:
400 case ERF_TYPE_DSM_COLOR_ETH:
401 wtap_file_read_expected_bytes(ð_hdr, sizeof(eth_hdr), fh, err);
402 if (bytes_read != NULL)
403 *bytes_read += (guint32)sizeof(eth_hdr);
404 *packet_size -= (guint32)sizeof(eth_hdr);
405 skiplen += (guint32)sizeof(eth_hdr);
406 pseudo_header->erf.subhdr.eth_hdr = g_htons(eth_hdr);
409 case ERF_TYPE_MC_HDLC:
410 case ERF_TYPE_MC_RAW:
411 case ERF_TYPE_MC_ATM:
412 case ERF_TYPE_MC_RAW_CHANNEL:
413 case ERF_TYPE_MC_AAL5:
414 case ERF_TYPE_MC_AAL2:
415 case ERF_TYPE_COLOR_MC_HDLC_POS:
416 wtap_file_read_expected_bytes(&mc_hdr, sizeof(mc_hdr), fh, err);
417 if (bytes_read != NULL)
418 *bytes_read += (guint32)sizeof(mc_hdr);
419 *packet_size -= (guint32)sizeof(mc_hdr);
420 skiplen += (guint32)sizeof(mc_hdr);
421 pseudo_header->erf.subhdr.mc_hdr = g_htonl(mc_hdr);
424 case ERF_TYPE_IP_COUNTER:
425 case ERF_TYPE_TCP_FLOW_COUNTER:
426 /* unsupported, continue with default: */
428 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
429 *err_info = g_strdup_printf("erf: unknown record encapsulation %u",
435 phdr->len = g_htons(erf_header->wlen);
436 phdr->caplen = min( g_htons(erf_header->wlen),
437 g_htons(erf_header->rlen) - (guint32)sizeof(*erf_header) - skiplen );