Not ideal, but OK for now - alignbytes, at that point, is < 8, so we
[metze/wireshark/wip.git] / wiretap / erf.c
1 /*
2  * Copyright (c) 2003 Endace Technology Ltd, Hamilton, New Zealand.
3  * All rights reserved.
4  *
5  * This software and documentation has been developed by Endace Technology Ltd.
6  * along with the DAG PCI network capture cards. For further information please
7  * visit http://www.endace.com/.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  *  1. Redistributions of source code must retain the above copyright notice,
13  *  this list of conditions and the following disclaimer.
14  *
15  *  2. Redistributions in binary form must reproduce the above copyright
16  *  notice, this list of conditions and the following disclaimer in the
17  *  documentation and/or other materials provided with the distribution.
18  *
19  *  3. The name of Endace Technology Ltd may not be used to endorse or promote
20  *  products derived from this software without specific prior written
21  *  permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY ENDACE TECHNOLOGY LTD ``AS IS'' AND ANY EXPRESS
24  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26  * EVENT SHALL ENDACE TECHNOLOGY LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id$
35  */
36
37 /*
38  * erf - Endace ERF (Extensible Record Format)
39  *
40  * See
41  *
42  *      http://www.endace.com/support/EndaceRecordFormat.pdf
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include <wsutil/crc32.c>
53
54 #include "wtap-int.h"
55 #include "file_wrappers.h"
56 #include "buffer.h"
57 #include "atm.h"
58 #include "erf.h"
59
60 static int erf_read_header(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 static const struct {
75   int erf_encap_value;
76   int wtap_encap_value;
77 } erf_to_wtap_map[] = {
78   { ERF_TYPE_HDLC_POS,  WTAP_ENCAP_CHDLC },
79   { ERF_TYPE_HDLC_POS,  WTAP_ENCAP_HHDLC },
80   { ERF_TYPE_HDLC_POS,  WTAP_ENCAP_CHDLC_WITH_PHDR },
81   { ERF_TYPE_HDLC_POS,  WTAP_ENCAP_PPP },
82   { ERF_TYPE_HDLC_POS,  WTAP_ENCAP_FRELAY },
83   { ERF_TYPE_HDLC_POS,  WTAP_ENCAP_MTP2 },
84   { ERF_TYPE_ETH,       WTAP_ENCAP_ETHERNET },
85   { 99,       WTAP_ENCAP_ERF }, /*this type added so WTAP_ENCAP_ERF will work and then be treated at ERF->ERF*/
86 };
87
88 #define NUM_ERF_ENCAPS (sizeof erf_to_wtap_map / sizeof erf_to_wtap_map[0])
89
90 extern int erf_open(wtap *wth, int *err, gchar **err_info)
91 {
92   int i, n, records_for_erf_check = RECORDS_FOR_ERF_CHECK;
93   int valid_prev = 0;
94   char *s;
95   erf_timestamp_t prevts,ts; 
96   erf_header_t header;
97   guint32 mc_hdr;
98   guint16 eth_hdr;
99   guint32 packet_size;
100   guint16 rlen;
101   guint64 erf_ext_header;
102   guint8 type;
103   size_t r;
104   gchar * buffer;
105
106   memset(&prevts, 0, sizeof(prevts));
107
108   /* number of records to scan before deciding if this really is ERF */
109   if ((s = getenv("ERF_RECORDS_TO_CHECK")) != NULL) {
110     if ((n = atoi(s)) > 0 && n < 101) {
111       records_for_erf_check = n;
112     }
113   }
114
115   /*
116    * ERF is a little hard because there's no magic number; we look at
117    * the first few records and see if they look enough like ERF
118    * records.
119    */
120
121   for (i = 0; i < records_for_erf_check; i++) {  /* records_for_erf_check */
122
123     r = file_read(&header,sizeof(header),wth->fh);
124
125     if (r == 0 ) break;
126     if (r != sizeof(header)) {
127       if ((*err = file_error(wth->fh, err_info)) != 0) {
128         return -1;
129       } else {
130         /* ERF header too short accept the file,
131            only if the very first records have been successfully checked */
132         if (i < MIN_RECORDS_FOR_ERF_CHECK) {
133           return 0;
134         } else {
135           /* BREAK, the last record is too short, and will be ignored */
136           break;
137         }
138       }
139     }
140
141     rlen=g_ntohs(header.rlen);
142
143     /* fail on invalid record type, invalid rlen, timestamps decreasing, or incrementing too far */
144     
145     /* Test valid rlen >= 16 */
146     if (rlen < 16) {
147       return 0;
148     }
149     
150     packet_size = rlen - (guint32)sizeof(header);
151     if (packet_size > WTAP_MAX_PACKET_SIZE) {
152       /*
153        * Probably a corrupt capture file or a file that's not an ERF file
154        * but that passed earlier tests; don't blow up trying
155        * to allocate space for an immensely-large packet.
156        */
157       return 0;
158     }
159
160     /* Skip PAD records, timestamps may not be set */
161     if ((header.type & 0x7F) == ERF_TYPE_PAD) {
162       if (file_seek(wth->fh, packet_size, SEEK_CUR, err) == -1) {
163         return -1;
164       }
165       continue;
166     }
167
168     /* fail on invalid record type, decreasing timestamps or non-zero pad-bits */
169     /* Not all types within this range are decoded, but it is a first filter */
170     if ((header.type & 0x7F) == 0 || (header.type & 0x7F) > ERF_TYPE_MAX ) {
171       return 0;
172     }
173     
174     /* The ERF_TYPE_MAX is the PAD record, but the last used type is ERF_TYPE_INFINIBAND_LINK */
175     if ((header.type & 0x7F) > ERF_TYPE_INFINIBAND_LINK) {
176       return 0;
177     }
178     
179     if ((ts = pletohll(&header.ts)) < prevts) {
180       /* 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 */
181       if ( ((prevts-ts)>>32) > 1 ) {
182         return 0;
183       }
184     }
185     
186     /* Check to see if timestamp increment is > 1 week */
187     if ( (valid_prev) && (ts > prevts) && (((ts-prevts)>>32) > 3600*24*7) ) {
188       return 0;
189     }
190     
191     memcpy(&prevts, &ts, sizeof(prevts));
192
193     /* Read over the extension headers */
194     type = header.type;
195     while (type & 0x80){
196             if (file_read(&erf_ext_header, sizeof(erf_ext_header),wth->fh) != sizeof(erf_ext_header)) {
197                     *err = file_error(wth->fh, err_info);
198                     return -1;
199             }
200             packet_size -= (guint32)sizeof(erf_ext_header);
201             memcpy(&type, &erf_ext_header, sizeof(type));
202     }
203     
204
205     /* Read over MC or ETH subheader */
206     switch(header.type & 0x7F) {
207     case ERF_TYPE_MC_HDLC:
208     case ERF_TYPE_MC_RAW:
209     case ERF_TYPE_MC_ATM:
210     case ERF_TYPE_MC_RAW_CHANNEL:
211     case ERF_TYPE_MC_AAL5:
212     case ERF_TYPE_MC_AAL2:
213     case ERF_TYPE_COLOR_MC_HDLC_POS:
214     case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */
215       if (file_read(&mc_hdr,sizeof(mc_hdr),wth->fh) != sizeof(mc_hdr)) {
216         *err = file_error(wth->fh, err_info);
217         return -1;
218       }
219       packet_size -= (guint32)sizeof(mc_hdr);
220       break;
221     case ERF_TYPE_ETH:
222     case ERF_TYPE_COLOR_ETH:
223     case ERF_TYPE_DSM_COLOR_ETH:
224       if (file_read(&eth_hdr,sizeof(eth_hdr),wth->fh) != sizeof(eth_hdr)) {
225         *err = file_error(wth->fh, err_info);
226         return -1;
227       }
228       packet_size -= (guint32)sizeof(eth_hdr);
229       break;
230     default:
231       break;
232     }
233
234     /* The file_seek function do not return an error if the end of file
235        is reached whereas the record is truncated */
236     if (packet_size > WTAP_MAX_PACKET_SIZE) {
237       /*
238        * Probably a corrupt capture file; don't blow up trying
239        * to allocate space for an immensely-large packet.
240        */
241       return 0;
242     }
243     buffer=g_malloc(packet_size);
244     r = file_read(buffer, packet_size, wth->fh);
245     g_free(buffer);
246
247     if (r != packet_size) { 
248       /* ERF record too short, accept the file,
249          only if the very first records have been successfully checked */
250       if (i < MIN_RECORDS_FOR_ERF_CHECK) {
251         return 0;
252       }
253     }
254
255     valid_prev = 1;
256
257   } /* records_for_erf_check */
258
259   if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) {    /* rewind */
260     return -1;
261   }
262
263   wth->data_offset = 0;
264
265   /* This is an ERF file */
266   wth->file_type = WTAP_FILE_ERF;
267   wth->snapshot_length = 0;     /* not available in header, only in frame */
268
269   /*
270    * Use the encapsulation for ERF records.
271    */
272   wth->file_encap = WTAP_ENCAP_ERF;
273
274   wth->subtype_read = erf_read;
275   wth->subtype_seek_read = erf_seek_read;
276   wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
277
278   return 1;
279 }
280
281 /* Read the next packet */
282 static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
283                          gint64 *data_offset)
284 {
285   erf_header_t erf_header;
286   guint32 packet_size, bytes_read;
287
288   *data_offset = wth->data_offset;
289
290   do {
291     if (!erf_read_header(wth->fh,
292                          &wth->phdr, &wth->pseudo_header, &erf_header,
293                          err, err_info, &bytes_read, &packet_size)) {
294       return FALSE;
295     }
296     wth->data_offset += bytes_read;
297
298     buffer_assure_space(wth->frame_buffer, packet_size);
299     
300     wtap_file_read_expected_bytes(buffer_start_ptr(wth->frame_buffer),
301                                 (gint32)(packet_size), wth->fh, err, err_info);
302     wth->data_offset += packet_size;
303
304   } while ( erf_header.type == ERF_TYPE_PAD );
305
306   return TRUE;
307 }
308
309 static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
310                               union wtap_pseudo_header *pseudo_header, guchar *pd,
311                               int length _U_, int *err, gchar **err_info)
312 {
313   erf_header_t erf_header;
314   guint32 packet_size;
315
316   if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
317     return FALSE;
318
319   do {
320     if (!erf_read_header(wth->random_fh, NULL, pseudo_header, &erf_header,
321                          err, err_info, NULL, &packet_size))
322       return FALSE;
323   } while ( erf_header.type == ERF_TYPE_PAD );
324
325   wtap_file_read_expected_bytes(pd, (int)packet_size, wth->random_fh, err,
326                                 err_info);
327
328   return TRUE;
329 }
330
331 static int erf_read_header(FILE_T fh,
332                            struct wtap_pkthdr *phdr,
333                            union wtap_pseudo_header *pseudo_header,
334                            erf_header_t *erf_header,
335                            int *err,
336                            gchar **err_info,
337                            guint32 *bytes_read,
338                            guint32 *packet_size)
339 {
340   guint32 mc_hdr;
341   guint8 erf_exhdr[8];
342   guint64 erf_exhdr_sw;
343   guint8 type = 0;
344   guint16 eth_hdr;
345   guint32 skiplen=0;
346   int i = 0 , max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
347
348   wtap_file_read_expected_bytes(erf_header, sizeof(*erf_header), fh, err,
349                                 err_info);
350   if (bytes_read != NULL) {
351     *bytes_read = sizeof(*erf_header);
352   }
353
354   *packet_size =  g_ntohs(erf_header->rlen) - (guint32)sizeof(*erf_header);
355
356   if (*packet_size > WTAP_MAX_PACKET_SIZE) {
357     /*
358      * Probably a corrupt capture file; don't blow up trying
359      * to allocate space for an immensely-large packet.
360      */
361     *err = WTAP_ERR_BAD_RECORD;
362     *err_info = g_strdup_printf("erf: File has %u-byte packet, bigger than maximum of %u",
363                                 *packet_size, WTAP_MAX_PACKET_SIZE);
364     return FALSE;
365   }
366
367   if (phdr != NULL) {
368     guint64 ts = pletohll(&erf_header->ts);
369
370     phdr->ts.secs = (long) (ts >> 32);
371     ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
372     ts += (ts & 0x80000000) << 1; /* rounding */
373     phdr->ts.nsecs = ((int) (ts >> 32));
374     if (phdr->ts.nsecs >= 1000000000) {
375       phdr->ts.nsecs -= 1000000000;
376       phdr->ts.secs += 1;
377     }
378   }
379
380   /* Copy the ERF pseudo header */
381   memset(&pseudo_header->erf, 0, sizeof(pseudo_header->erf));
382   pseudo_header->erf.phdr.ts = pletohll(&erf_header->ts);
383   pseudo_header->erf.phdr.type = erf_header->type;
384   pseudo_header->erf.phdr.flags = erf_header->flags;
385   pseudo_header->erf.phdr.rlen = g_ntohs(erf_header->rlen);
386   pseudo_header->erf.phdr.lctr = g_ntohs(erf_header->lctr);
387   pseudo_header->erf.phdr.wlen = g_ntohs(erf_header->wlen);
388
389   /* Copy the ERF extension header into the pseudo header */
390   type = erf_header->type;
391   while (type & 0x80){
392           wtap_file_read_expected_bytes(&erf_exhdr, sizeof(erf_exhdr), fh, err,
393                                         err_info);
394           if (bytes_read != NULL)
395                   *bytes_read += (guint32)sizeof(erf_exhdr);
396           *packet_size -=  (guint32)sizeof(erf_exhdr);
397           skiplen += (guint32)sizeof(erf_exhdr);
398           erf_exhdr_sw = pntohll(erf_exhdr);
399           if (i < max)
400             memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
401           type = erf_exhdr[0];
402           i++;
403   }
404
405   switch (erf_header->type & 0x7F) {
406   case ERF_TYPE_IPV4:
407   case ERF_TYPE_IPV6:
408   case ERF_TYPE_RAW_LINK:
409   case ERF_TYPE_INFINIBAND:
410   case ERF_TYPE_INFINIBAND_LINK:
411     /***
412     if (phdr != NULL) {
413       phdr->len =  g_htons(erf_header->wlen);
414       phdr->caplen = g_htons(erf_header->wlen); 
415     }  
416     return TRUE;
417     ***/
418     break;
419   case ERF_TYPE_PAD:
420   case ERF_TYPE_HDLC_POS:
421   case ERF_TYPE_COLOR_HDLC_POS:
422   case ERF_TYPE_DSM_COLOR_HDLC_POS:
423   case ERF_TYPE_ATM:
424   case ERF_TYPE_AAL5:
425     break;
426
427   case ERF_TYPE_ETH:
428   case ERF_TYPE_COLOR_ETH:
429   case ERF_TYPE_DSM_COLOR_ETH:
430     wtap_file_read_expected_bytes(&eth_hdr, sizeof(eth_hdr), fh, err,
431                                   err_info);
432     if (bytes_read != NULL)
433       *bytes_read += (guint32)sizeof(eth_hdr);
434     *packet_size -=  (guint32)sizeof(eth_hdr);
435     skiplen += (guint32)sizeof(eth_hdr);
436     pseudo_header->erf.subhdr.eth_hdr = g_htons(eth_hdr);
437     break;
438
439   case ERF_TYPE_MC_HDLC:
440   case ERF_TYPE_MC_RAW:
441   case ERF_TYPE_MC_ATM:
442   case ERF_TYPE_MC_RAW_CHANNEL:
443   case ERF_TYPE_MC_AAL5:
444   case ERF_TYPE_MC_AAL2:
445   case ERF_TYPE_COLOR_MC_HDLC_POS:
446   case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */
447     wtap_file_read_expected_bytes(&mc_hdr, sizeof(mc_hdr), fh, err,
448                                   err_info);
449     if (bytes_read != NULL)
450       *bytes_read += (guint32)sizeof(mc_hdr);
451     *packet_size -=  (guint32)sizeof(mc_hdr);
452     skiplen += (guint32)sizeof(mc_hdr);
453     pseudo_header->erf.subhdr.mc_hdr = g_htonl(mc_hdr);
454     break;
455
456   case ERF_TYPE_IP_COUNTER:
457   case ERF_TYPE_TCP_FLOW_COUNTER:
458     /* unsupported, continue with default: */
459   default:
460     *err = WTAP_ERR_UNSUPPORTED_ENCAP;
461     *err_info = g_strdup_printf("erf: unknown record encapsulation %u",
462                                 erf_header->type);
463     return FALSE;
464   }
465
466   if (phdr != NULL) {
467     phdr->len = g_htons(erf_header->wlen);
468     phdr->caplen = MIN( g_htons(erf_header->wlen),
469                         g_htons(erf_header->rlen) - (guint32)sizeof(*erf_header) - skiplen );
470   }
471   return TRUE;
472 }
473
474 static int wtap_wtap_encap_to_erf_encap(int encap)
475 {
476   unsigned int i;
477   for(i = 0; i < NUM_ERF_ENCAPS; i++){
478     if(erf_to_wtap_map[i].wtap_encap_value == encap)
479       return erf_to_wtap_map[i].erf_encap_value;
480   }
481   return -1;
482 }
483
484 static gboolean erf_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header, int * err)
485 {
486   guint8 erf_hdr[sizeof(struct erf_mc_phdr)];
487   guint8 erf_subhdr[((sizeof(struct erf_mc_hdr) > sizeof(struct erf_eth_hdr))?
488     sizeof(struct erf_mc_hdr) : sizeof(struct erf_eth_hdr))];
489   guint8 ehdr[8*MAX_ERF_EHDR];
490   size_t size = 0;
491   size_t subhdr_size = 0;
492   int i = 0;
493
494   switch(encap){
495     case WTAP_ENCAP_ERF:
496       memset(&erf_hdr, 0, sizeof(erf_hdr));
497       pletonll(&erf_hdr[0], pseudo_header->erf.phdr.ts);
498       erf_hdr[8] = pseudo_header->erf.phdr.type;
499       erf_hdr[9] = pseudo_header->erf.phdr.flags;
500       phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
501       phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
502       phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
503       size = sizeof(struct erf_phdr);
504
505       switch(pseudo_header->erf.phdr.type & 0x7F) {
506         case ERF_TYPE_MC_HDLC:
507         case ERF_TYPE_MC_RAW:
508         case ERF_TYPE_MC_ATM:
509         case ERF_TYPE_MC_RAW_CHANNEL:
510         case ERF_TYPE_MC_AAL5:
511         case ERF_TYPE_MC_AAL2:
512         case ERF_TYPE_COLOR_MC_HDLC_POS:
513           phtonl(&erf_subhdr[0], pseudo_header->erf.subhdr.mc_hdr);
514           subhdr_size += (int)sizeof(struct erf_mc_hdr);
515           break;
516         case ERF_TYPE_ETH:
517         case ERF_TYPE_COLOR_ETH:
518         case ERF_TYPE_DSM_COLOR_ETH:
519           phtons(&erf_subhdr[0], pseudo_header->erf.subhdr.eth_hdr);
520           subhdr_size += (int)sizeof(struct erf_eth_hdr);
521           break;
522         default:
523           break;
524       }
525       break;
526     default:
527       return FALSE;
528
529   }
530   if (!wtap_dump_file_write(wdh, erf_hdr, size, err))
531     return FALSE;
532   wdh->bytes_dumped += size;
533
534   /*write out up to MAX_ERF_EHDR extension headers*/
535   if((pseudo_header->erf.phdr.type & 0x80) != 0){  /*we have extension headers*/
536     do{
537       phtonll(ehdr+(i*8), pseudo_header->erf.ehdr_list[i].ehdr);
538       if(i == MAX_ERF_EHDR-1) ehdr[i*8] = ehdr[i*8] & 0x7F;
539       i++;
540     }while((ehdr[0] & 0x80) != 0 && i < MAX_ERF_EHDR);
541     wtap_dump_file_write(wdh, ehdr, MAX_ERF_EHDR*i, err);
542     wdh->bytes_dumped += MAX_ERF_EHDR*i;
543   }
544
545   if(!wtap_dump_file_write(wdh, erf_subhdr, subhdr_size, err))
546     return FALSE;
547   wdh->bytes_dumped += subhdr_size;
548
549   return TRUE;
550 }
551
552 static gboolean erf_dump(
553     wtap_dumper *wdh,
554     const struct wtap_pkthdr *phdr,
555     const union wtap_pseudo_header *pseudo_header,
556     const guchar *pd,
557     int *err)
558 {
559   union wtap_pseudo_header other_phdr;
560   int newencap = -1;
561   int encap;
562   gint64 alignbytes = 0;
563   int i;
564   guint32 crc32 = 0x00000000;
565
566   if(wdh->encap == WTAP_ENCAP_PER_PACKET){
567     encap = phdr->pkt_encap;
568   }else{
569     encap = wdh->encap;
570   }
571
572   switch(encap){
573     case WTAP_ENCAP_ERF:
574       alignbytes = wdh->bytes_dumped + pseudo_header->erf.phdr.rlen;
575
576       if(!erf_write_phdr(wdh, encap, pseudo_header, err)) return FALSE;
577
578       if(!wtap_dump_file_write(wdh, pd, phdr->caplen, err)) return FALSE;
579       wdh->bytes_dumped += phdr->caplen;
580
581       while(wdh->bytes_dumped < alignbytes){
582         if(!wtap_dump_file_write(wdh, "", 1, err)) return FALSE;
583         wdh->bytes_dumped++;
584       }
585       break;
586     default:  /*deal with generic wtap format*/
587       /*generate a fake header in other_phdr using data that we know*/
588       /*covert time erf timestamp format*/
589       other_phdr.erf.phdr.ts = ((guint64) phdr->ts.secs << 32) + (((guint64) phdr->ts.nsecs <<32) / 1000 / 1000 / 1000);
590       newencap = other_phdr.erf.phdr.type = wtap_wtap_encap_to_erf_encap(encap);
591       other_phdr.erf.phdr.flags = 0x4;  /*vlen flag set because we're creating variable length records*/
592       other_phdr.erf.phdr.lctr = 0;
593       /*now we work out rlen, accounting for all the different headers and missing fcs(eth)*/
594       other_phdr.erf.phdr.rlen = phdr->caplen+16;
595       other_phdr.erf.phdr.wlen = phdr->caplen;
596       switch(other_phdr.erf.phdr.type){
597         case ERF_TYPE_ETH:
598           crc32 = crc32_ccitt_seed(pd, phdr->caplen, 0xFFFFFFFF);
599           other_phdr.erf.phdr.rlen += 2;  /*2 bytes for erf eth_type*/
600           other_phdr.erf.phdr.rlen += 4;  /*4 bytes for added checksum*/
601           other_phdr.erf.phdr.wlen += 4;
602           break;
603         case ERF_TYPE_HDLC_POS:
604           /*we assume that it's missing a FCS checksum, make one up*/
605           crc32 = crc32_ccitt_seed(pd, phdr->caplen, 0xFFFFFFFF);
606           other_phdr.erf.phdr.rlen += 4;  /*4 bytes for added checksum*/
607           other_phdr.erf.phdr.wlen += 4;
608           break;
609         default:
610           break;
611       }
612
613       alignbytes = (8 - (other_phdr.erf.phdr.rlen % 8)) % 8;  /*calculate how much padding will be required */
614       other_phdr.erf.phdr.rlen += (gint16)alignbytes;
615
616       if(!erf_write_phdr(wdh, WTAP_ENCAP_ERF, &other_phdr, err)) return FALSE;
617       if(!wtap_dump_file_write(wdh, pd, phdr->caplen, err)) return FALSE;
618       wdh->bytes_dumped += phdr->caplen;
619
620       /*add the 4 byte checksum if type eth*/
621       if(newencap == ERF_TYPE_ETH || newencap == ERF_TYPE_HDLC_POS){
622         if(!wtap_dump_file_write(wdh, &crc32, 4, err)) return FALSE;
623         wdh->bytes_dumped += 4;
624       }
625       /*records should be 8byte aligned, so we add padding*/
626       for(i = (gint16)alignbytes; i > 0; i--){
627         if(!wtap_dump_file_write(wdh, "", 1, err)) return FALSE;
628         wdh->bytes_dumped++;
629       }
630
631       break;
632   }
633
634   return TRUE;
635 }
636
637 int erf_dump_can_write_encap(int encap)
638 {
639
640   if(encap == WTAP_ENCAP_PER_PACKET)
641     return 0;
642
643   if (wtap_wtap_encap_to_erf_encap(encap) == -1)
644     return WTAP_ERR_UNSUPPORTED_ENCAP;
645
646   return 0;
647 }
648
649 int erf_dump_open(wtap_dumper *wdh, int *err)
650 {
651   wdh->subtype_write = erf_dump;
652   wdh->subtype_close = NULL;
653
654   switch(wdh->file_type){
655     case WTAP_FILE_ERF:
656       wdh->tsprecision = WTAP_FILE_TSPREC_NSEC;
657       break;
658     default:
659       *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
660       return FALSE;
661       break;
662   }
663
664   return TRUE;
665 }