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"
59 typedef guint32 atm_hdr_t;
61 static int erf_read_header(
63 struct wtap_pkthdr *phdr,
64 union wtap_pseudo_header *pseudo_header,
65 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);
76 static void erf_close(wtap *wth);
77 static int erf_encap_to_wtap_encap(erf_t *erf, guint8 erf_encap);
78 static void erf_set_pseudo_header(
83 union wtap_pseudo_header *pseudo_header);
85 int erf_open(wtap *wth, int *err, gchar **err_info _U_)
89 guint32 records_for_erf_check = RECORDS_FOR_ERF_CHECK;
90 guint32 atm_encap = WTAP_ENCAP_ATM_PDUS;
91 gboolean is_rawatm = FALSE;
92 gboolean is_ppp = FALSE;
94 erf_timestamp_t prevts;
96 memset(&prevts, 0, sizeof(prevts));
98 if ((s = getenv("ERF_ATM_ENCAP")) != NULL) {
99 if (!strcmp(s, "sunatm")) {
100 atm_encap = WTAP_ENCAP_ATM_PDUS;
102 if (!strcmp(s, "sunraw")) {
103 atm_encap = WTAP_ENCAP_ATM_PDUS;
106 if (!strcmp(s, "rfc1483")) {
107 atm_encap = WTAP_ENCAP_ATM_RFC1483;
111 /* number of records to scan before deciding if this really is ERF (dflt=3) */
112 if ((s = getenv("ERF_RECORDS_TO_CHECK")) != NULL) {
113 if ((n = atoi(s)) > 0 && n < 101) {
114 records_for_erf_check = n;
118 /* ERF is a little hard because there's no magic number */
120 for (i = 0; i < records_for_erf_check; i++) {
126 int r = file_read(&header,1,sizeof(header),wth->fh);
129 if (r != sizeof(header)) {
130 if ((*err = file_error(wth->fh)) != 0)
136 packet_size = g_ntohs(header.rlen) - sizeof(header);
138 /* fail on invalid record type, decreasing timestamps or non-zero pad-bits */
139 /* Only header type up to Multi Channel HDLC are taken into account in this software version */
140 if (header.type == 0 || header.type > TYPE_MC_HDLC ) {
144 if ((ts = pletohll(&header.ts)) < prevts) {
145 /* reassembled AAL5 records may not be in time order, so allow 1 sec fudge */
146 if (header.type != TYPE_AAL5 || ((prevts-ts)>>32) > 1) {
150 memcpy(&prevts, &ts, sizeof(prevts));
152 if (common_type == 0) {
153 common_type = header.type;
155 if (common_type > 0 && common_type != header.type) {
159 if (header.type == TYPE_HDLC_POS && !is_ppp) {
161 if (file_read(&chdlc_hdr,1,sizeof(chdlc_hdr),wth->fh) != sizeof(chdlc_hdr)) {
162 *err = file_error(wth->fh);
164 packet_size -= sizeof(chdlc_hdr);
165 if (g_ntohs(chdlc_hdr) == 0xff03) {
170 if (file_seek(wth->fh, packet_size, SEEK_CUR, err) == -1) {
175 if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) { /* rewind */
179 wth->data_offset = 0;
181 /* This is an ERF file */
182 wth->file_type = WTAP_FILE_ERF;
183 wth->snapshot_length = 0; /* not available in header, only in frame */
184 wth->capture.erf = g_malloc(sizeof(erf_t));
185 wth->capture.erf->is_ppp = is_ppp;
186 if (common_type == TYPE_AAL5) {
187 wth->capture.erf->atm_encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
188 wth->capture.erf->is_rawatm = FALSE;
190 wth->capture.erf->atm_encap = atm_encap;
191 wth->capture.erf->is_rawatm = is_rawatm;
195 * Really want WTAP_ENCAP_PER_PACKET here but that severely limits
196 * the number of output formats we can write to. If all the records
197 * tested in the loop above were the same encap then use that one,
198 * otherwise use WTAP_ENCAP_PER_PACKET.
202 ? WTAP_ENCAP_PER_PACKET
203 : erf_encap_to_wtap_encap(wth->capture.erf, (guint8) common_type));
205 wth->subtype_read = erf_read;
206 wth->subtype_seek_read = erf_seek_read;
207 wth->subtype_close = erf_close;
208 wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
213 /* Read the next packet */
214 static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
217 erf_header_t erf_header;
218 guint32 packet_size, bytes_read;
221 *data_offset = wth->data_offset;
223 if (!erf_read_header(
225 &wth->phdr, &wth->pseudo_header, &erf_header, wth->capture.erf,
226 err, err_info, &bytes_read, &packet_size)) {
229 wth->data_offset += bytes_read;
231 buffer_assure_space(wth->frame_buffer, packet_size+(wth->capture.erf->is_rawatm?(sizeof(atm_hdr_t)+1):0));
233 if (wth->capture.erf->is_rawatm) {
234 wtap_file_read_expected_bytes(
235 buffer_start_ptr(wth->frame_buffer), (gint32)sizeof(atm_hdr_t), wth->fh, err
237 wth->data_offset += sizeof(atm_hdr_t);
238 packet_size -= sizeof(atm_hdr_t);
239 offset += sizeof(atm_hdr_t)+1;
242 wtap_file_read_expected_bytes(
243 buffer_start_ptr(wth->frame_buffer)+offset, (gint32)packet_size, wth->fh, err
245 wth->data_offset += packet_size;
247 erf_set_pseudo_header(
248 erf_header.type, wth->capture.erf,
249 buffer_start_ptr(wth->frame_buffer), packet_size, &wth->pseudo_header
255 static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
256 union wtap_pseudo_header *pseudo_header, guchar *pd,
257 int length, int *err, gchar **err_info)
259 erf_header_t erf_header;
263 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
266 if (!erf_read_header(wth->random_fh, NULL, pseudo_header, &erf_header,
267 wth->capture.erf, err, err_info, NULL, &packet_size))
270 if (wth->capture.erf->is_rawatm) {
271 wtap_file_read_expected_bytes(pd, (int)sizeof(atm_hdr_t), wth->random_fh, err);
272 packet_size -= sizeof(atm_hdr_t);
273 offset += sizeof(atm_hdr_t)+1;
276 wtap_file_read_expected_bytes(pd+offset, (int)packet_size, wth->random_fh, err);
278 erf_set_pseudo_header(erf_header.type, wth->capture.erf, pd, length, pseudo_header);
283 static void erf_close(wtap *wth)
285 g_free(wth->capture.erf);
288 static int erf_read_header(
290 struct wtap_pkthdr *phdr,
291 union wtap_pseudo_header *pseudo_header,
292 erf_header_t *erf_header,
297 guint32 *packet_size)
299 guint32 rec_size, skip;
301 wtap_file_read_expected_bytes(erf_header, sizeof(*erf_header), fh, err);
302 if (bytes_read != NULL) {
303 *bytes_read = sizeof(*erf_header);
306 rec_size = g_ntohs(erf_header->rlen);
307 *packet_size = rec_size - sizeof(*erf_header);
308 skip = 0; /* # bytes of payload to ignore */
310 if (*packet_size > WTAP_MAX_PACKET_SIZE) {
312 * Probably a corrupt capture file; don't blow up trying
313 * to allocate space for an immensely-large packet.
315 *err = WTAP_ERR_BAD_RECORD;
316 *err_info = g_strdup_printf("erf: File has %u-byte packet, bigger than maximum of %u",
317 *packet_size, WTAP_MAX_PACKET_SIZE);
322 guint64 ts = pletohll(&erf_header->ts);
324 phdr->ts.secs = (long) (ts >> 32);
325 ts = ((ts & 0xffffffff) * 1000 * 1000);
326 ts += (ts & 0x80000000) << 1; /* rounding */
327 phdr->ts.nsecs = ((long) (ts >> 32)) * 1000;
328 if (phdr->ts.nsecs >= 1000000000) {
329 phdr->ts.nsecs -= 1000000000;
334 switch (erf_header->type) {
339 if (erf_header->type == TYPE_AAL5) {
340 phdr->caplen = phdr->len = *packet_size - sizeof(atm_hdr_t);
342 phdr->caplen = ATM_SLEN(erf_header, NULL);
343 phdr->len = ATM_WLEN(erf_header, NULL);
347 if (erf->atm_encap == WTAP_ENCAP_ATM_PDUS || erf->atm_encap == WTAP_ENCAP_ATM_PDUS_UNTRUNCATED) {
348 memset(&pseudo_header->atm, 0, sizeof(pseudo_header->atm));
349 if (erf->is_rawatm) {
350 pseudo_header->atm.flags = ATM_RAW_CELL;
352 phdr->caplen += sizeof(atm_hdr_t)+1;
353 phdr->len += sizeof(atm_hdr_t)+1;
358 wtap_file_read_expected_bytes(&atm_hdr, sizeof(atm_hdr), fh, err);
359 if (bytes_read != NULL) {
360 *bytes_read += sizeof(atm_hdr);
362 *packet_size -= sizeof(atm_hdr);
364 atm_hdr = g_ntohl(atm_hdr);
366 pseudo_header->atm.vpi = ((atm_hdr & 0x0ff00000) >> 20);
367 pseudo_header->atm.vci = ((atm_hdr & 0x000ffff0) >> 4);
368 pseudo_header->atm.channel = (erf_header->flags & 0x03);
376 phdr->caplen = ETHERNET_SLEN(erf_header, erf);
377 phdr->len = ETHERNET_WLEN(erf_header, erf);
383 phdr->caplen = HDLC_SLEN(erf_header, erf);
384 phdr->len = HDLC_WLEN(erf_header, erf);
386 memset(&pseudo_header->p2p, 0, sizeof(pseudo_header->p2p));
387 pseudo_header->p2p.sent = ((erf_header->flags & 0x01) ? TRUE : FALSE);
391 phdr->caplen = MC_HDLC_SLEN(erf_header, erf);
392 phdr->len = MC_HDLC_WLEN(erf_header, erf);
394 /* Skip the MC header, so the first data to dissect will be the MTP2 header */
396 memset(&pseudo_header->mtp2, 0, sizeof(pseudo_header->mtp2));
399 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
400 *err_info = g_strdup_printf("erf: unknown record encapsulation %u",
406 phdr->pkt_encap = erf_encap_to_wtap_encap(erf, erf_header->type);
410 if (file_seek(fh, skip, SEEK_CUR, err) == -1) {
413 if (bytes_read != NULL) {
416 *packet_size -= skip;
422 static int erf_encap_to_wtap_encap(erf_t *erf, guint8 erf_encap)
424 int wtap_encap = WTAP_ENCAP_UNKNOWN;
429 wtap_encap = erf->atm_encap;
432 wtap_encap = WTAP_ENCAP_ETHERNET;
435 wtap_encap = (erf->is_ppp ? WTAP_ENCAP_PPP : WTAP_ENCAP_CHDLC);
438 wtap_encap = WTAP_ENCAP_MTP2;
447 static void erf_set_pseudo_header(
448 guint8 type, erf_t *erf, guchar *pd, int length, union wtap_pseudo_header *pseudo_header)
450 if (type == TYPE_ETH) {
452 * We don't know whether there's an FCS in this frame or not.
454 pseudo_header->eth.fcs_len = -1;
456 if (!erf->is_rawatm &&
457 (type == TYPE_ATM || type == TYPE_AAL5) &&
458 (erf->atm_encap == WTAP_ENCAP_ATM_PDUS ||
459 erf->atm_encap == WTAP_ENCAP_ATM_PDUS_UNTRUNCATED)) {
460 atm_guess_traffic_type(pd, length, pseudo_header);
462 if (type == TYPE_AAL5) {
463 pseudo_header->atm.aal = AAL_5;
464 pseudo_header->atm.type = TRAF_UNKNOWN;
465 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;