Manually revert the changes to CMakeLists.txt from commit 40602 - they should not...
[obnox/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, guint8 *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, guint8 *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_FILE;
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 (*packet_size == 0) {
368     /* Again a corrupt packet, bail out */
369    *err = WTAP_ERR_BAD_FILE;
370    *err_info = g_strdup_printf("erf: File has 0 byte packet");
371
372    return FALSE;
373   }
374
375   if (phdr != NULL) {
376     guint64 ts = pletohll(&erf_header->ts);
377
378     phdr->ts.secs = (long) (ts >> 32);
379     ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
380     ts += (ts & 0x80000000) << 1; /* rounding */
381     phdr->ts.nsecs = ((int) (ts >> 32));
382     if (phdr->ts.nsecs >= 1000000000) {
383       phdr->ts.nsecs -= 1000000000;
384       phdr->ts.secs += 1;
385     }
386   }
387
388   /* Copy the ERF pseudo header */
389   memset(&pseudo_header->erf, 0, sizeof(pseudo_header->erf));
390   pseudo_header->erf.phdr.ts = pletohll(&erf_header->ts);
391   pseudo_header->erf.phdr.type = erf_header->type;
392   pseudo_header->erf.phdr.flags = erf_header->flags;
393   pseudo_header->erf.phdr.rlen = g_ntohs(erf_header->rlen);
394   pseudo_header->erf.phdr.lctr = g_ntohs(erf_header->lctr);
395   pseudo_header->erf.phdr.wlen = g_ntohs(erf_header->wlen);
396
397   /* Copy the ERF extension header into the pseudo header */
398   type = erf_header->type;
399   while (type & 0x80){
400           wtap_file_read_expected_bytes(&erf_exhdr, sizeof(erf_exhdr), fh, err,
401                                         err_info);
402           if (bytes_read != NULL)
403                   *bytes_read += (guint32)sizeof(erf_exhdr);
404           *packet_size -=  (guint32)sizeof(erf_exhdr);
405           skiplen += (guint32)sizeof(erf_exhdr);
406           erf_exhdr_sw = pntohll(erf_exhdr);
407           if (i < max)
408             memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
409           type = erf_exhdr[0];
410           i++;
411   }
412
413   switch (erf_header->type & 0x7F) {
414   case ERF_TYPE_IPV4:
415   case ERF_TYPE_IPV6:
416   case ERF_TYPE_RAW_LINK:
417   case ERF_TYPE_INFINIBAND:
418   case ERF_TYPE_INFINIBAND_LINK:
419     /***
420     if (phdr != NULL) {
421       phdr->len =  g_htons(erf_header->wlen);
422       phdr->caplen = g_htons(erf_header->wlen); 
423     }  
424     return TRUE;
425     ***/
426     break;
427   case ERF_TYPE_PAD:
428   case ERF_TYPE_HDLC_POS:
429   case ERF_TYPE_COLOR_HDLC_POS:
430   case ERF_TYPE_DSM_COLOR_HDLC_POS:
431   case ERF_TYPE_ATM:
432   case ERF_TYPE_AAL5:
433     break;
434
435   case ERF_TYPE_ETH:
436   case ERF_TYPE_COLOR_ETH:
437   case ERF_TYPE_DSM_COLOR_ETH:
438     wtap_file_read_expected_bytes(&eth_hdr, sizeof(eth_hdr), fh, err,
439                                   err_info);
440     if (bytes_read != NULL)
441       *bytes_read += (guint32)sizeof(eth_hdr);
442     *packet_size -=  (guint32)sizeof(eth_hdr);
443     skiplen += (guint32)sizeof(eth_hdr);
444     pseudo_header->erf.subhdr.eth_hdr = g_htons(eth_hdr);
445     break;
446
447   case ERF_TYPE_MC_HDLC:
448   case ERF_TYPE_MC_RAW:
449   case ERF_TYPE_MC_ATM:
450   case ERF_TYPE_MC_RAW_CHANNEL:
451   case ERF_TYPE_MC_AAL5:
452   case ERF_TYPE_MC_AAL2:
453   case ERF_TYPE_COLOR_MC_HDLC_POS:
454   case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */
455     wtap_file_read_expected_bytes(&mc_hdr, sizeof(mc_hdr), fh, err,
456                                   err_info);
457     if (bytes_read != NULL)
458       *bytes_read += (guint32)sizeof(mc_hdr);
459     *packet_size -=  (guint32)sizeof(mc_hdr);
460     skiplen += (guint32)sizeof(mc_hdr);
461     pseudo_header->erf.subhdr.mc_hdr = g_htonl(mc_hdr);
462     break;
463
464   case ERF_TYPE_IP_COUNTER:
465   case ERF_TYPE_TCP_FLOW_COUNTER:
466     /* unsupported, continue with default: */
467   default:
468     *err = WTAP_ERR_UNSUPPORTED_ENCAP;
469     *err_info = g_strdup_printf("erf: unknown record encapsulation %u",
470                                 erf_header->type);
471     return FALSE;
472   }
473
474   if (phdr != NULL) {
475     phdr->len = g_htons(erf_header->wlen);
476     phdr->caplen = MIN( g_htons(erf_header->wlen),
477                         g_htons(erf_header->rlen) - (guint32)sizeof(*erf_header) - skiplen );
478   }
479
480   if (*packet_size > WTAP_MAX_PACKET_SIZE) {
481     /*
482      * Probably a corrupt capture file; don't blow up trying
483      * to allocate space for an immensely-large packet.
484      */
485     *err = WTAP_ERR_BAD_FILE;
486     *err_info = g_strdup_printf("erf: File has %u-byte packet, bigger than maximum of %u",
487                                 *packet_size, WTAP_MAX_PACKET_SIZE);
488     return FALSE;
489   }
490
491   return TRUE;
492 }
493
494 static int wtap_wtap_encap_to_erf_encap(int encap)
495 {
496   unsigned int i;
497   for(i = 0; i < NUM_ERF_ENCAPS; i++){
498     if(erf_to_wtap_map[i].wtap_encap_value == encap)
499       return erf_to_wtap_map[i].erf_encap_value;
500   }
501   return -1;
502 }
503
504 static gboolean erf_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header, int * err)
505 {
506   guint8 erf_hdr[sizeof(struct erf_mc_phdr)];
507   guint8 erf_subhdr[((sizeof(struct erf_mc_hdr) > sizeof(struct erf_eth_hdr))?
508     sizeof(struct erf_mc_hdr) : sizeof(struct erf_eth_hdr))];
509   guint8 ehdr[8*MAX_ERF_EHDR];
510   size_t size = 0;
511   size_t subhdr_size = 0;
512   int i = 0;
513
514   switch(encap){
515     case WTAP_ENCAP_ERF:
516       memset(&erf_hdr, 0, sizeof(erf_hdr));
517       phtolell(&erf_hdr[0], pseudo_header->erf.phdr.ts);
518       erf_hdr[8] = pseudo_header->erf.phdr.type;
519       erf_hdr[9] = pseudo_header->erf.phdr.flags;
520       phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
521       phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
522       phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
523       size = sizeof(struct erf_phdr);
524
525       switch(pseudo_header->erf.phdr.type & 0x7F) {
526         case ERF_TYPE_MC_HDLC:
527         case ERF_TYPE_MC_RAW:
528         case ERF_TYPE_MC_ATM:
529         case ERF_TYPE_MC_RAW_CHANNEL:
530         case ERF_TYPE_MC_AAL5:
531         case ERF_TYPE_MC_AAL2:
532         case ERF_TYPE_COLOR_MC_HDLC_POS:
533           phtonl(&erf_subhdr[0], pseudo_header->erf.subhdr.mc_hdr);
534           subhdr_size += (int)sizeof(struct erf_mc_hdr);
535           break;
536         case ERF_TYPE_ETH:
537         case ERF_TYPE_COLOR_ETH:
538         case ERF_TYPE_DSM_COLOR_ETH:
539           phtons(&erf_subhdr[0], pseudo_header->erf.subhdr.eth_hdr);
540           subhdr_size += (int)sizeof(struct erf_eth_hdr);
541           break;
542         default:
543           break;
544       }
545       break;
546     default:
547       return FALSE;
548
549   }
550   if (!wtap_dump_file_write(wdh, erf_hdr, size, err))
551     return FALSE;
552   wdh->bytes_dumped += size;
553
554   /*write out up to MAX_ERF_EHDR extension headers*/
555   if((pseudo_header->erf.phdr.type & 0x80) != 0){  /*we have extension headers*/
556     do{
557       phtonll(ehdr+(i*8), pseudo_header->erf.ehdr_list[i].ehdr);
558       if(i == MAX_ERF_EHDR-1) ehdr[i*8] = ehdr[i*8] & 0x7F;
559       i++;
560     }while((ehdr[0] & 0x80) != 0 && i < MAX_ERF_EHDR);
561     if (!wtap_dump_file_write(wdh, ehdr, MAX_ERF_EHDR*i, err))
562       return FALSE;
563     wdh->bytes_dumped += MAX_ERF_EHDR*i;
564   }
565
566   if(!wtap_dump_file_write(wdh, erf_subhdr, subhdr_size, err))
567     return FALSE;
568   wdh->bytes_dumped += subhdr_size;
569
570   return TRUE;
571 }
572
573 static gboolean erf_dump(
574     wtap_dumper *wdh,
575     const struct wtap_pkthdr *phdr,
576     const union wtap_pseudo_header *pseudo_header,
577     const guint8 *pd,
578     int *err)
579 {
580   union wtap_pseudo_header other_phdr;
581   int encap;
582   gint64 alignbytes = 0;
583   int i;
584   int round_down = 0;
585   gboolean must_add_crc = FALSE;
586   guint32 crc32 = 0x00000000;
587
588   if(wdh->encap == WTAP_ENCAP_PER_PACKET){
589     encap = phdr->pkt_encap;
590   }else{
591     encap = wdh->encap;
592   }
593
594   switch(encap){
595     case WTAP_ENCAP_ERF:
596       alignbytes = wdh->bytes_dumped + pseudo_header->erf.phdr.rlen;
597
598       if(!erf_write_phdr(wdh, encap, pseudo_header, err)) return FALSE;
599
600       if(!wtap_dump_file_write(wdh, pd, phdr->caplen, err)) return FALSE;
601       wdh->bytes_dumped += phdr->caplen;
602
603       while(wdh->bytes_dumped < alignbytes){
604         if(!wtap_dump_file_write(wdh, "", 1, err)) return FALSE;
605         wdh->bytes_dumped++;
606       }
607       must_add_crc = TRUE; /* XXX - not if this came from an ERF file with an FCS! */
608       break;
609     default:  /*deal with generic wtap format*/
610       /*generate a fake header in other_phdr using data that we know*/
611       /*covert time erf timestamp format*/
612       other_phdr.erf.phdr.ts = ((guint64) phdr->ts.secs << 32) + (((guint64) phdr->ts.nsecs <<32) / 1000 / 1000 / 1000);
613       other_phdr.erf.phdr.type = wtap_wtap_encap_to_erf_encap(encap);
614       other_phdr.erf.phdr.flags = 0x4;  /*vlen flag set because we're creating variable length records*/
615       other_phdr.erf.phdr.lctr = 0;
616       /*now we work out rlen, accounting for all the different headers and missing fcs(eth)*/
617       other_phdr.erf.phdr.rlen = phdr->caplen+16;
618       other_phdr.erf.phdr.wlen = phdr->len;
619       switch(other_phdr.erf.phdr.type){
620         case ERF_TYPE_ETH:
621           other_phdr.erf.phdr.rlen += 2;  /*2 bytes for erf eth_type*/
622           if (pseudo_header->eth.fcs_len != 4) {
623             /* Either this packet doesn't include the FCS
624                (pseudo_header->eth.fcs_len = 0), or we don't
625                know whether it has an FCS (= -1).  We have to
626                synthesize an FCS.*/
627
628             if(!(phdr->caplen < phdr->len)){ /*don't add FCS if packet has been snapped off*/
629               crc32 = crc32_ccitt_seed(pd, phdr->caplen, 0xFFFFFFFF);
630               other_phdr.erf.phdr.rlen += 4;  /*4 bytes for added checksum*/
631               other_phdr.erf.phdr.wlen += 4;
632               must_add_crc = TRUE;
633             }
634           }
635           break;
636         case ERF_TYPE_HDLC_POS:
637           /*we assume that it's missing a FCS checksum, make one up*/
638           if(!(phdr->caplen < phdr->len)){  /*unless of course, the packet has been snapped off*/
639             crc32 = crc32_ccitt_seed(pd, phdr->caplen, 0xFFFFFFFF);
640             other_phdr.erf.phdr.rlen += 4;  /*4 bytes for added checksum*/
641             other_phdr.erf.phdr.wlen += 4;
642             must_add_crc = TRUE; /* XXX - these never have an FCS? */
643           }
644           break;
645         default:
646           break;
647       }
648
649       alignbytes = (8 - (other_phdr.erf.phdr.rlen % 8)) % 8;  /*calculate how much padding will be required */
650       if(phdr->caplen < phdr->len){ /*if packet has been snapped, we need to round down what we output*/
651         round_down = (8 - alignbytes) % 8;
652         other_phdr.erf.phdr.rlen -= round_down;
653       }else{
654         other_phdr.erf.phdr.rlen += (gint16)alignbytes;
655       }
656
657       if(!erf_write_phdr(wdh, WTAP_ENCAP_ERF, &other_phdr, err)) return FALSE;
658       if(!wtap_dump_file_write(wdh, pd, phdr->caplen - round_down, err)) return FALSE;
659       wdh->bytes_dumped += phdr->caplen - round_down;
660
661       /*add the 4 byte CRC if necessary*/
662       if(must_add_crc){
663         if(!wtap_dump_file_write(wdh, &crc32, 4, err)) return FALSE;
664         wdh->bytes_dumped += 4;
665       }
666       /*records should be 8byte aligned, so we add padding*/
667       if(round_down == 0){
668         for(i = (gint16)alignbytes; i > 0; i--){
669           if(!wtap_dump_file_write(wdh, "", 1, err)) return FALSE;
670           wdh->bytes_dumped++;
671         }
672       }
673
674       break;
675   }
676
677   return TRUE;
678 }
679
680 int erf_dump_can_write_encap(int encap)
681 {
682
683   if(encap == WTAP_ENCAP_PER_PACKET)
684     return 0;
685
686   if (wtap_wtap_encap_to_erf_encap(encap) == -1)
687     return WTAP_ERR_UNSUPPORTED_ENCAP;
688
689   return 0;
690 }
691
692 int erf_dump_open(wtap_dumper *wdh, int *err)
693 {
694   wdh->subtype_write = erf_dump;
695   wdh->subtype_close = NULL;
696
697   switch(wdh->file_type){
698     case WTAP_FILE_ERF:
699       wdh->tsprecision = WTAP_FILE_TSPREC_NSEC;
700       break;
701     default:
702       *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
703       return FALSE;
704       break;
705   }
706
707   return TRUE;
708 }