From Stephen Donnelly via bug 2235:
[metze/wireshark/wip.git] / wiretap / erf.c
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$
36  */
37
38 /*
39  * erf - Endace ERF (Extensible Record Format)
40  *
41  * See
42  *
43  *      http://www.endace.com/support/EndaceRecordFormat.pdf
44  */
45
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "wtap-int.h"
54 #include "file_wrappers.h"
55 #include "buffer.h"
56 #include "atm.h"
57 #include "erf.h"
58
59 static int erf_read_header(
60                            FILE_T fh,
61                            struct wtap_pkthdr *phdr,
62                            union wtap_pseudo_header *pseudo_header,
63                            erf_header_t *erf_header,
64                            int *err,
65                            gchar **err_info,
66                            guint32 *bytes_read,
67                            guint32 *packet_size);
68 static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
69                          gint64 *data_offset);
70 static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
71                               union wtap_pseudo_header *pseudo_header, guchar *pd,
72                               int length, int *err, gchar **err_info);
73
74 int erf_open(wtap *wth, int *err, gchar **err_info _U_)
75 {
76   int i, n, records_for_erf_check = RECORDS_FOR_ERF_CHECK;
77   char *s;
78   erf_timestamp_t prevts,ts; 
79   erf_header_t header;
80   guint32 mc_hdr;
81   guint16 eth_hdr;
82   guint32 packet_size;
83   guint16 rlen,wlen;
84   size_t r;
85   gchar * buffer;
86
87   memset(&prevts, 0, sizeof(prevts));
88
89   /* number of records to scan before deciding if this really is ERF */
90   if ((s = getenv("ERF_RECORDS_TO_CHECK")) != NULL) {
91     if ((n = atoi(s)) > 0 && n < 101) {
92       records_for_erf_check = n;
93     }
94   }
95
96   /*
97    * ERF is a little hard because there's no magic number; we look at
98    * the first few records and see if they look enough like ERF
99    * records.
100    */
101
102   for (i = 0; i < records_for_erf_check; i++) {  /* records_for_erf_check */
103
104     r = file_read(&header,1,sizeof(header),wth->fh);
105
106     if (r == 0 ) break;
107     if (r != sizeof(header)) {
108       if ((*err = file_error(wth->fh)) != 0) {
109         return -1;
110       } else {
111         /* ERF header too short accept the file,
112            only if the very first records have been successfully checked */
113         if (i < MIN_RECORDS_FOR_ERF_CHECK) {
114           return 0;
115         } else {
116           /* BREAK, the last record is too short, and will be ignored */
117           break;
118         }
119       }
120     }
121
122     rlen=g_ntohs(header.rlen);
123     wlen=g_ntohs(header.wlen);
124     packet_size = rlen - sizeof(header);
125
126     if (packet_size > WTAP_MAX_PACKET_SIZE) {
127       /*
128        * Probably a corrupt capture file; don't blow up trying
129        * to allocate space for an immensely-large packet.
130        */
131       return 0;
132     }
133
134     /* Skip PAD records, timestamps may not be set */
135     if (header.type == ERF_TYPE_PAD) {
136       if (file_seek(wth->fh, packet_size, SEEK_CUR, err) == -1) {
137         return -1;
138       }
139       continue;
140     }
141
142     if (rlen < wlen) {
143       /* record length must be greater than wire length */
144       switch(header.type) {
145       case ERF_TYPE_ETH:
146       case ERF_TYPE_COLOR_ETH:
147       case ERF_TYPE_DSM_COLOR_ETH:
148         /* skip the test, we have a file with truncated snaplen */
149         break;
150       default:
151         return 0;
152       }
153     }
154
155     /* fail on invalid record type, decreasing timestamps or non-zero pad-bits */
156     /* Not all types within this range are decoded, but it is a first filter */
157     if (header.type == 0 || header.type > ERF_TYPE_MAX ) {
158       return 0;
159     }
160
161     /* The ERF_TYPE_MAX is the PAD record, but the last used type is ERF_TYPE_AAL2 */
162         if (header.type > ERF_TYPE_INFINIBAND) {
163       return 0;
164     }
165
166     if ((ts = pletohll(&header.ts)) < prevts) {
167       /* reassembled AAL5 records may not be in time order, so allow 1 sec fudge */
168       if (header.type == ERF_TYPE_AAL5) {
169         if ( ((prevts-ts)>>32) > 1 ) {
170           return 0;
171         }
172       } else {
173         /* For other records, allow 1/256 sec fudge */
174         if ( (prevts-ts)>>24 > 1) {
175           return 0;
176         }
177       }
178     }
179     memcpy(&prevts, &ts, sizeof(prevts));
180
181     /* Read over MC or ETH subheader */
182     switch(header.type) {
183     case ERF_TYPE_MC_HDLC:
184     case ERF_TYPE_MC_RAW:
185     case ERF_TYPE_MC_ATM:
186     case ERF_TYPE_MC_RAW_CHANNEL:
187     case ERF_TYPE_MC_AAL5:
188     case ERF_TYPE_MC_AAL2:
189     case ERF_TYPE_COLOR_MC_HDLC_POS:
190       if (file_read(&mc_hdr,1,sizeof(mc_hdr),wth->fh) != sizeof(mc_hdr)) {
191         *err = file_error(wth->fh);
192         return -1;
193       }
194       packet_size -= sizeof(mc_hdr);
195       break;
196     case ERF_TYPE_ETH:
197     case ERF_TYPE_COLOR_ETH:
198     case ERF_TYPE_DSM_COLOR_ETH:
199       if (file_read(&eth_hdr,1,sizeof(eth_hdr),wth->fh) != sizeof(eth_hdr)) {
200         *err = file_error(wth->fh);
201         return -1;
202       }
203       packet_size -= sizeof(eth_hdr);
204       break;
205     default:
206       break;
207     }
208
209     /* The file_seek function do not return an error if the end of file
210        is reached whereas the record is truncated */
211     buffer=g_malloc(packet_size);
212     r = file_read(buffer, 1, packet_size, wth->fh);
213     g_free(buffer);
214
215     if (r != packet_size) { 
216       /* ERF record too short, accept the file,
217          only if the very first records have been successfully checked */
218       if (i < MIN_RECORDS_FOR_ERF_CHECK) {
219         return 0;
220       }
221
222     }
223   } /* records_for_erf_check */
224
225   if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) {    /* rewind */
226     return -1;
227   }
228
229   wth->data_offset = 0;
230
231   /* This is an ERF file */
232   wth->file_type = WTAP_FILE_ERF;
233   wth->snapshot_length = 0;     /* not available in header, only in frame */
234
235   /*
236    * Use the encapsulation for ERF records.
237    */
238   wth->file_encap = WTAP_ENCAP_ERF;
239
240   wth->subtype_read = erf_read;
241   wth->subtype_seek_read = erf_seek_read;
242   wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
243
244   return 1;
245 }
246
247 /* Read the next packet */
248 static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
249                          gint64 *data_offset)
250 {
251   erf_header_t erf_header;
252   guint32 packet_size, bytes_read;
253
254   *data_offset = wth->data_offset;
255
256   do {
257     if (!erf_read_header(wth->fh,
258                          &wth->phdr, &wth->pseudo_header, &erf_header,
259                          err, err_info, &bytes_read, &packet_size)) {
260       return FALSE;
261     }
262     wth->data_offset += bytes_read;
263   } while ( erf_header.type == ERF_TYPE_PAD );
264
265   buffer_assure_space(wth->frame_buffer, packet_size);
266
267   wtap_file_read_expected_bytes(buffer_start_ptr(wth->frame_buffer),
268                                 (gint32)(packet_size), wth->fh, err );
269   wth->data_offset += packet_size;
270
271   return TRUE;
272 }
273
274 static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
275                               union wtap_pseudo_header *pseudo_header, guchar *pd,
276                               int length, int *err, gchar **err_info)
277 {
278   erf_header_t erf_header;
279   guint32 packet_size;
280
281   if (length) {};
282
283   if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
284     return FALSE;
285
286   if (!erf_read_header(wth->random_fh, NULL, pseudo_header, &erf_header,
287                        err, err_info, NULL, &packet_size))
288     return FALSE;
289
290   wtap_file_read_expected_bytes(pd, (int)packet_size, wth->random_fh, err);
291
292   return TRUE;
293 }
294
295 static int erf_read_header(
296                            FILE_T fh,
297                            struct wtap_pkthdr *phdr,
298                            union wtap_pseudo_header *pseudo_header,
299                            erf_header_t *erf_header,
300                            int *err,
301                            gchar **err_info,
302                            guint32 *bytes_read,
303                            guint32 *packet_size)
304 {
305   guint32 rec_size;
306   guint32 mc_hdr;
307   guint16 eth_hdr;
308
309   wtap_file_read_expected_bytes(erf_header, sizeof(*erf_header), fh, err);
310   if (bytes_read != NULL) {
311     *bytes_read = sizeof(*erf_header);
312   }
313
314   rec_size = g_ntohs(erf_header->rlen);
315   *packet_size = rec_size - sizeof(*erf_header);
316
317   if (*packet_size > WTAP_MAX_PACKET_SIZE) {
318     /*
319      * Probably a corrupt capture file; don't blow up trying
320      * to allocate space for an immensely-large packet.
321      */
322     *err = WTAP_ERR_BAD_RECORD;
323     *err_info = g_strdup_printf("erf: File has %u-byte packet, bigger than maximum of %u",
324                                 *packet_size, WTAP_MAX_PACKET_SIZE);
325     return FALSE;
326   }
327
328   if (phdr != NULL) {
329     guint64 ts = pletohll(&erf_header->ts);
330
331     phdr->ts.secs = (long) (ts >> 32);
332     ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
333     ts += (ts & 0x80000000) << 1; /* rounding */
334     phdr->ts.nsecs = ((long) (ts >> 32));
335     if (phdr->ts.nsecs >= 1000000000) {
336       phdr->ts.nsecs -= 1000000000;
337       phdr->ts.secs += 1;
338     }
339   }
340
341   /* Copy the ERF pseudo header */
342   pseudo_header->erf.phdr.ts = pletohll(&erf_header->ts);
343   pseudo_header->erf.phdr.type = erf_header->type;
344   pseudo_header->erf.phdr.flags = erf_header->flags;
345   pseudo_header->erf.phdr.rlen = g_ntohs(erf_header->rlen);
346   pseudo_header->erf.phdr.lctr = g_ntohs(erf_header->lctr);
347   pseudo_header->erf.phdr.wlen = g_ntohs(erf_header->wlen);
348
349   switch (erf_header->type) {
350   
351   case ERF_TYPE_INFINIBAND:
352         if (phdr != NULL) 
353         {
354       phdr->len =  g_htons(erf_header->wlen);
355       phdr->caplen = g_htons(erf_header->wlen); 
356     }  
357     break;
358   case ERF_TYPE_HDLC_POS:
359   case ERF_TYPE_COLOR_HDLC_POS:
360   case ERF_TYPE_DSM_COLOR_HDLC_POS:
361   case ERF_TYPE_ATM:
362   case ERF_TYPE_AAL5:
363   case ERF_TYPE_AAL2:
364     break;
365
366   case ERF_TYPE_ETH:
367   case ERF_TYPE_COLOR_ETH:
368   case ERF_TYPE_DSM_COLOR_ETH:
369     wtap_file_read_expected_bytes(&eth_hdr, sizeof(eth_hdr), fh, err);
370     if (bytes_read != NULL)
371       *bytes_read += sizeof(eth_hdr);
372     *packet_size -=  sizeof(eth_hdr);
373     pseudo_header->erf.subhdr.eth_hdr = g_htons(eth_hdr);
374     break;
375
376   case ERF_TYPE_MC_HDLC:
377   case ERF_TYPE_MC_RAW:
378   case ERF_TYPE_MC_ATM:
379   case ERF_TYPE_MC_RAW_CHANNEL:
380   case ERF_TYPE_MC_AAL5:
381   case ERF_TYPE_MC_AAL2:
382   case ERF_TYPE_COLOR_MC_HDLC_POS:
383     wtap_file_read_expected_bytes(&mc_hdr, sizeof(mc_hdr), fh, err);
384     if (bytes_read != NULL)
385       *bytes_read += sizeof(mc_hdr);
386     *packet_size -=  sizeof(mc_hdr);
387     pseudo_header->erf.subhdr.mc_hdr = g_htonl(mc_hdr);
388     break;
389
390   case ERF_TYPE_IP_COUNTER:
391   case ERF_TYPE_TCP_FLOW_COUNTER:
392     /* unsupported, continue with default: */
393   default:
394     *err = WTAP_ERR_UNSUPPORTED_ENCAP;
395     *err_info = g_strdup_printf("erf: unknown record encapsulation %u",
396                                 erf_header->type);
397     return FALSE;
398   }
399
400   if (phdr != NULL) {
401     phdr->len =  g_htons(erf_header->wlen);
402     phdr->caplen = g_htons(erf_header->rlen);
403   }
404   return TRUE;
405 }