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