Make Content-Length and Max-Forwards fields uints
[obnox/wireshark/wip.git] / editcap.c
1 /* Edit capture files.  We can delete packets, adjust timestamps, or
2  * simply convert from one format to another format.
3  *
4  * $Id$
5  *
6  * Originally written by Richard Sharpe.
7  * Improved by Guy Harris.
8  * Further improved by Richard Sharpe.
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <glib.h>
19
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23
24
25 /*
26  * Just make sure we include the prototype for strptime as well
27  * (needed for glibc 2.2)
28  */
29 #define __USE_XOPEN
30
31 #include <time.h>
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
35
36 #include <string.h>
37 #include "wtap.h"
38
39 #ifdef NEED_GETOPT_H
40 #include "getopt.h"
41 #endif
42
43 #ifdef _WIN32
44 #include <process.h>    /* getpid */
45 #endif
46
47 #ifdef NEED_STRPTIME_H
48 # include "strptime.h"
49 #endif
50
51 #include "epan/crypt-md5.h"
52
53 #include "svnversion.h"
54
55 /*
56  * Some globals so we can pass things to various routines
57  */
58
59 struct select_item {
60
61   int inclusive;
62   int first, second;
63
64 };
65
66
67 /*
68  * Duplicate frame detection
69  */
70 typedef struct _fd_hash_t {
71   md5_byte_t digest[16];
72   guint32 len;
73 } fd_hash_t;
74
75 #define DUP_DEPTH 5
76 fd_hash_t fd_hash[DUP_DEPTH];
77 int cur_dup = 0;
78
79 #define ONE_MILLION 1000000
80
81 /* Weights of different errors we can introduce */
82 /* We should probably make these command-line arguments */
83 /* XXX - Should we add a bit-level error? */
84 #define ERR_WT_BIT   5  /* Flip a random bit */
85 #define ERR_WT_BYTE  5  /* Substitute a random byte */
86 #define ERR_WT_ALNUM 5  /* Substitute a random character in [A-Za-z0-9] */
87 #define ERR_WT_FMT   2  /* Substitute "%s" */
88 #define ERR_WT_AA    1  /* Fill the remainder of the buffer with 0xAA */
89 #define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
90
91 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
92 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
93
94
95 struct time_adjustment {
96   struct timeval tv;
97   int is_negative;
98 };
99
100 static struct select_item selectfrm[100];
101 static int max_selected = -1;
102 static int keep_em = 0;
103 static int out_file_type = WTAP_FILE_PCAP;   /* default to "libpcap"   */
104 static int out_frame_type = -2;              /* Leave frame type alone */
105 static int verbose = 0;                      /* Not so verbose         */
106 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
107 static double err_prob = 0.0;
108 static time_t starttime = 0;
109 static time_t stoptime = 0;
110 static gboolean check_startstop = FALSE;
111 static gboolean dup_detect = FALSE;
112
113 /* Add a selection item, a simple parser for now */
114
115 static void add_selection(char *sel)
116 {
117   char *locn;
118   char *next;
119
120   if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
121     return;
122
123   printf("Add_Selected: %s\n", sel);
124
125   if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
126
127     printf("Not inclusive ...");
128
129     max_selected++;
130     selectfrm[max_selected].inclusive = 0;
131     selectfrm[max_selected].first = atoi(sel);
132
133     printf(" %i\n", selectfrm[max_selected].first);
134
135   }
136   else {
137
138     printf("Inclusive ...");
139
140     next = locn + 1;
141     max_selected++;
142     selectfrm[max_selected].inclusive = 1;
143     selectfrm[max_selected].first = atoi(sel);
144     selectfrm[max_selected].second = atoi(next);
145
146     printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
147
148   }
149
150
151 }
152
153 /* Was the packet selected? */
154
155 static int selected(int recno)
156 {
157   int i = 0;
158
159   for (i = 0; i<= max_selected; i++) {
160
161     if (selectfrm[i].inclusive) {
162       if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
163         return 1;
164     }
165     else {
166       if (recno == selectfrm[i].first)
167         return 1;
168     }
169   }
170
171   return 0;
172
173 }
174
175 /* is the packet in the selected timeframe */
176 static gboolean check_timestamp(wtap *wth) {
177         struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
178         return ( (time_t) pkthdr->ts.secs >= starttime ) && ( (time_t) pkthdr->ts.secs <= stoptime );
179 }
180
181 static void
182 set_time_adjustment(char *optarg)
183 {
184   char *frac, *end;
185   long val;
186   int frac_digits;
187
188   if (!optarg)
189     return;
190
191   /* skip leading whitespace */
192   while (*optarg == ' ' || *optarg == '\t') {
193       optarg++;
194   }
195
196   /* check for a negative adjustment */
197   if (*optarg == '-') {
198       time_adj.is_negative = 1;
199       optarg++;
200   }
201
202   /* collect whole number of seconds, if any */
203   if (*optarg == '.') {         /* only fractional (i.e., .5 is ok) */
204       val  = 0;
205       frac = optarg;
206   } else {
207       val = strtol(optarg, &frac, 10);
208       if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
209           fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
210                   optarg);
211           exit(1);
212       }
213       if (val < 0) {            /* implies '--' since we caught '-' above  */
214           fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
215                   optarg);
216           exit(1);
217       }
218   }
219   time_adj.tv.tv_sec = val;
220
221   /* now collect the partial seconds, if any */
222   if (*frac != '\0') {             /* chars left, so get fractional part */
223     val = strtol(&(frac[1]), &end, 10);
224     if (*frac != '.' || end == NULL || end == frac
225         || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
226       fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
227               optarg);
228       exit(1);
229     }
230   }
231   else {
232     return;                     /* no fractional digits */
233   }
234
235   /* adjust fractional portion from fractional to numerator
236    * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
237   if (frac && end) {            /* both are valid */
238     frac_digits = end - frac - 1;   /* fractional digit count (remember '.') */
239     while(frac_digits < 6) {    /* this is frac of 10^6 */
240       val *= 10;
241       frac_digits++;
242     }
243   }
244   time_adj.tv.tv_usec = val;
245 }
246
247 static gboolean
248 is_duplicate(guint8* fd, guint32 len) {
249   int i;
250   md5_state_t ms;
251
252   cur_dup++;
253   if (cur_dup >= DUP_DEPTH)
254     cur_dup = 0;
255
256   /* Calculate our digest */
257   md5_init(&ms);
258   md5_append(&ms, fd, len);
259   md5_finish(&ms, fd_hash[cur_dup].digest);
260
261   fd_hash[cur_dup].len = len;
262
263   /* Look for duplicates */
264   for (i = 0; i < DUP_DEPTH; i++) {
265     if (i == cur_dup)
266       continue;
267
268     if (fd_hash[i].len == fd_hash[cur_dup].len &&
269         memcmp(fd_hash[i].digest, fd_hash[cur_dup].digest, 16) == 0) {
270       return TRUE;
271     }
272   }
273
274   return FALSE;
275 }
276
277 static void usage(void)
278 {
279   fprintf(stderr, "Editcap %s"
280 #ifdef SVNVERSION
281           " (" SVNVERSION ")"
282 #endif
283           "\n", VERSION);
284   fprintf(stderr, "Edit and/or translate the format of capture files.\n");
285   fprintf(stderr, "See http://www.wireshark.org for more information.\n");
286   fprintf(stderr, "\n");
287   fprintf(stderr, "Usage: editcap [options] ... <infile> <outfile> [ <packet#>[-<packet#>] ... ]\n");
288   fprintf(stderr, "\n");
289   fprintf(stderr, "A single packet or a range of packets can be selected.\n");
290   fprintf(stderr, "\n");
291   fprintf(stderr, "Packets:\n");
292   fprintf(stderr, "  -C <choplen>           chop each packet at the end by <choplen> bytes\n");
293   fprintf(stderr, "  -d                     remove duplicate packets\n");
294   fprintf(stderr, "  -E <error probability> set the probability (between 0.0 and 1.0 incl.)\n");
295   fprintf(stderr, "                         that a particular packet byte will be randomly changed\n");
296   fprintf(stderr, "  -r                     keep the selected packets, default is to delete them\n");
297   fprintf(stderr, "  -s <snaplen>           truncate packets to max. <snaplen> bytes of data\n");
298   fprintf(stderr, "  -t <time adjustment>   adjust the timestamp of selected packets,\n");
299   fprintf(stderr, "                         <time adjustment> is in relative seconds (e.g. -0.5)\n");
300   fprintf(stderr, "  -A <start time>        don't output packets whose timestamp is before the\n");
301   fprintf(stderr, "                         given time (format as YYYY-MM-DD hh:mm:ss)\n");
302   fprintf(stderr, "  -B <stop time>         don't output packets whose timestamp is after the\n");
303   fprintf(stderr, "                         given time (format as YYYY-MM-DD hh:mm:ss)\n");
304   fprintf(stderr, "\n");
305   fprintf(stderr, "Output File(s):\n");
306   fprintf(stderr, "  -c <packets per file>  split the packet output to different files,\n");
307   fprintf(stderr, "                         with a maximum of <packets per file> each\n");
308   fprintf(stderr, "  -F <capture type>      set the output file type, default is libpcap\n");
309   fprintf(stderr, "                         an empty \"-F\" option will list the file types\n");
310   fprintf(stderr, "  -T <encap type>        set the output file encapsulation type,\n");
311   fprintf(stderr, "                         default is the same as the input file\n");
312   fprintf(stderr, "                         an empty \"-T\" option will list the encapsulation types\n");
313   fprintf(stderr, "\n");
314   fprintf(stderr, "Miscellaneous:\n");
315   fprintf(stderr, "  -h                     display this help and exit\n");
316   fprintf(stderr, "  -v                     verbose output\n");
317   fprintf(stderr, "\n");
318 }
319
320 static void list_capture_types(void) {
321     int i;
322
323     fprintf(stderr, "editcap: The available capture file types for \"F\":\n");
324     for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
325       if (wtap_dump_can_open(i))
326         fprintf(stderr, "    %s - %s\n",
327           wtap_file_type_short_string(i), wtap_file_type_string(i));
328     }
329 }
330
331 static void list_encap_types(void) {
332     int i;
333     const char *string;
334
335     fprintf(stderr, "editcap: The available encapsulation types for \"T\":\n");
336     for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
337         string = wtap_encap_short_string(i);
338         if (string != NULL)
339           fprintf(stderr, "    %s - %s\n",
340             string, wtap_encap_string(i));
341     }
342 }
343
344 int main(int argc, char *argv[])
345
346 {
347   wtap *wth;
348   int i, j, err;
349   gchar *err_info;
350   extern char *optarg;
351   extern int optind;
352   int opt;
353   char *p;
354   unsigned int snaplen = 0;             /* No limit               */
355   unsigned int choplen = 0;             /* No chop                */
356   wtap_dumper *pdh;
357   int count = 1;
358   long data_offset;
359   struct wtap_pkthdr snap_phdr;
360   const struct wtap_pkthdr *phdr;
361   int err_type;
362   guint8 *buf;
363   int split_packet_count = 0;
364   int written_count = 0;
365   char *filename;
366
367   /* Process the options first */
368
369   while ((opt = getopt(argc, argv, "A:B:c:C:dE:F:hrs:t:T:v")) !=-1) {
370
371     switch (opt) {
372
373     case 'E':
374       err_prob = strtod(optarg, &p);
375       if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
376         fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
377             optarg);
378         exit(1);
379       }
380       srand(time(NULL) + getpid());
381       break;
382
383     case 'F':
384       out_file_type = wtap_short_string_to_file_type(optarg);
385       if (out_file_type < 0) {
386         fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
387             optarg);
388         list_capture_types();
389         exit(1);
390       }
391       break;
392
393     case 'c':
394       split_packet_count = strtol(optarg, &p, 10);
395       if (p == optarg || *p != '\0') {
396         fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
397             optarg);
398         exit(1);
399       }
400       if (split_packet_count <= 0) {
401         fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
402                 split_packet_count);
403         exit(1);
404       }
405       break;
406
407     case 'C':
408       choplen = strtol(optarg, &p, 10);
409       if (p == optarg || *p != '\0') {
410         fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
411             optarg);
412         exit(1);
413       }
414       break;
415
416     case 'd':
417       dup_detect = TRUE;
418       for (i = 0; i < DUP_DEPTH; i++) {
419         memset(&fd_hash[i].digest, 0, 16);
420         fd_hash[i].len = 0;
421       }
422       break;
423
424     case '?':              /* Bad options if GNU getopt */
425       switch(optopt) {
426       case'F':
427         list_capture_types();
428         break;
429       case'T':
430         list_encap_types();
431         break;
432       default:
433         usage();
434       }
435       exit(1);
436       break;
437
438     case 'h':
439       usage();
440       exit(1);
441       break;
442
443     case 'r':
444       keep_em = !keep_em;  /* Just invert */
445       break;
446
447     case 's':
448       snaplen = strtol(optarg, &p, 10);
449       if (p == optarg || *p != '\0') {
450         fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
451             optarg);
452         exit(1);
453       }
454       break;
455
456     case 't':
457       set_time_adjustment(optarg);
458       break;
459
460     case 'T':
461       out_frame_type = wtap_short_string_to_encap(optarg);
462       if (out_frame_type < 0) {
463         fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n",
464             optarg);
465         list_encap_types();
466         exit(1);
467       }
468       break;
469
470     case 'v':
471       verbose = !verbose;  /* Just invert */
472       break;
473
474         case 'A':
475         {
476                 struct tm starttm;
477
478                 memset(&starttm,0,sizeof(struct tm));
479
480                 if(!strptime(optarg,"%F %T",&starttm)) {
481                         fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
482                                         optarg);
483                         exit(1);
484                 }
485
486                 check_startstop = TRUE;
487                 starttime = mktime(&starttm);
488                 break;
489         }
490         case 'B':
491         {
492                 struct tm stoptm;
493
494                 memset(&stoptm,0,sizeof(struct tm));
495
496                 if(!strptime(optarg,"%F %T",&stoptm)) {
497                         fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
498                                         optarg);
499                         exit(1);
500                 }
501                 check_startstop = TRUE;
502                 stoptime = mktime(&stoptm);
503                 break;
504         }
505     }
506
507   }
508
509 #ifdef DEBUG
510   printf("Optind = %i, argc = %i\n", optind, argc);
511 #endif
512
513   if ((argc - optind) < 1) {
514
515     usage();
516     exit(1);
517
518   }
519
520   if (check_startstop && !stoptime) {
521           struct tm stoptm;
522           /* XXX: will work until 2035 */
523           memset(&stoptm,0,sizeof(struct tm));
524           stoptm.tm_year = 135;
525           stoptm.tm_mday = 31;
526           stoptm.tm_mon = 11;
527
528           stoptime = mktime(&stoptm);
529   }
530
531   if (starttime > stoptime) {
532           fprintf(stderr, "editcap: start time is after the stop time\n");
533           exit(1);
534   }
535
536   wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
537
538   if (!wth) {
539     fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
540         wtap_strerror(err));
541     switch (err) {
542
543     case WTAP_ERR_UNSUPPORTED:
544     case WTAP_ERR_UNSUPPORTED_ENCAP:
545     case WTAP_ERR_BAD_RECORD:
546       fprintf(stderr, "(%s)\n", err_info);
547       g_free(err_info);
548       break;
549     }
550     exit(1);
551
552   }
553
554   if (verbose) {
555
556     fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
557             wtap_file_type_string(wtap_file_type(wth)));
558
559   }
560
561   /*
562    * Now, process the rest, if any ... we only write if there is an extra
563    * argument or so ...
564    */
565
566   if ((argc - optind) >= 2) {
567
568     if (out_frame_type == -2)
569       out_frame_type = wtap_file_encap(wth);
570
571     if (split_packet_count > 0) {
572       filename = (char *) malloc(strlen(argv[optind+1]) + 20);
573       if (!filename) {
574         exit(5);
575       }
576       sprintf(filename, "%s-%05d", argv[optind+1], 0);
577     } else {
578       filename = argv[optind+1];
579     }
580
581     pdh = wtap_dump_open(filename, out_file_type,
582                          out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
583     if (pdh == NULL) {
584
585       fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
586               wtap_strerror(err));
587       exit(1);
588
589     }
590
591     for (i = optind + 2; i < argc; i++)
592       add_selection(argv[i]);
593
594     while (wtap_read(wth, &err, &err_info, &data_offset)) {
595
596       if (split_packet_count > 0 && (written_count % split_packet_count == 0)) {
597         if (!wtap_dump_close(pdh, &err)) {
598
599           fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
600                   wtap_strerror(err));
601           exit(1);
602         }
603
604         sprintf(filename, "%s-%05d",argv[optind+1], count / split_packet_count);
605
606         if (verbose) {
607           fprintf(stderr, "Continuing writing in file %s\n", filename);
608         }
609
610         pdh = wtap_dump_open(filename, out_file_type,
611                              out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
612         if (pdh == NULL) {
613
614           fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
615                   wtap_strerror(err));
616           exit(1);
617
618         }
619       }
620
621       if ( ((check_startstop && check_timestamp(wth)) || (!check_startstop && !check_timestamp(wth))) && ((!selected(count) && !keep_em) ||
622           (selected(count) && keep_em)) ) {
623
624         if (verbose)
625           printf("Packet: %u\n", count);
626
627         /* We simply write it, perhaps after truncating it; we could do other
628            things, like modify it. */
629
630         phdr = wtap_phdr(wth);
631
632         if (choplen != 0 && phdr->caplen > choplen) {
633           snap_phdr = *phdr;
634           snap_phdr.caplen -= choplen;
635           phdr = &snap_phdr;
636         }
637
638         if (snaplen != 0 && phdr->caplen > snaplen) {
639           snap_phdr = *phdr;
640           snap_phdr.caplen = snaplen;
641           phdr = &snap_phdr;
642         }
643
644         /* assume that if the frame's tv_sec is 0, then
645          * the timestamp isn't supported */
646         if (phdr->ts.secs > 0 && time_adj.tv.tv_sec != 0) {
647           snap_phdr = *phdr;
648           if (time_adj.is_negative)
649             snap_phdr.ts.secs -= time_adj.tv.tv_sec;
650           else
651             snap_phdr.ts.secs += time_adj.tv.tv_sec;
652           phdr = &snap_phdr;
653         }
654
655         /* assume that if the frame's tv_sec is 0, then
656          * the timestamp isn't supported */
657         if (phdr->ts.secs > 0 && time_adj.tv.tv_usec != 0) {
658           snap_phdr = *phdr;
659           if (time_adj.is_negative) { /* subtract */
660             if (snap_phdr.ts.nsecs/1000 < time_adj.tv.tv_usec) { /* borrow */
661               snap_phdr.ts.secs--;
662               snap_phdr.ts.nsecs += ONE_MILLION * 1000;
663             }
664             snap_phdr.ts.nsecs -= time_adj.tv.tv_usec * 1000;
665           } else {                  /* add */
666             if (snap_phdr.ts.nsecs + time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
667               /* carry */
668               snap_phdr.ts.secs++;
669               snap_phdr.ts.nsecs += (time_adj.tv.tv_usec - ONE_MILLION) * 1000;
670             } else {
671               snap_phdr.ts.nsecs += time_adj.tv.tv_usec * 1000;
672             }
673           }
674           phdr = &snap_phdr;
675         }
676
677         if (dup_detect) {
678           buf = wtap_buf_ptr(wth);
679           if (is_duplicate(buf, phdr->caplen)) {
680             if (verbose)
681               printf("Skipping duplicate: %u\n", count);
682             count++;
683             continue;
684           }
685         }
686
687         if (err_prob > 0.0) {
688           buf = wtap_buf_ptr(wth);
689           for (i = 0; i < (int) phdr->caplen; i++) {
690             if (rand() <= err_prob * RAND_MAX) {
691               err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
692
693               if (err_type < ERR_WT_BIT) {
694                 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
695                 err_type = ERR_WT_TOTAL;
696               } else {
697                 err_type -= ERR_WT_BYTE;
698               }
699
700               if (err_type < ERR_WT_BYTE) {
701                 buf[i] = rand() / (RAND_MAX / 255 + 1);
702                 err_type = ERR_WT_TOTAL;
703               } else {
704                 err_type -= ERR_WT_BYTE;
705               }
706
707               if (err_type < ERR_WT_ALNUM) {
708                 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
709                 err_type = ERR_WT_TOTAL;
710               } else {
711                 err_type -= ERR_WT_ALNUM;
712               }
713
714               if (err_type < ERR_WT_FMT) {
715                 if ((unsigned int)i < phdr->caplen - 2)
716                   strcpy((char*) &buf[i],  "%s");
717                 err_type = ERR_WT_TOTAL;
718               } else {
719                 err_type -= ERR_WT_FMT;
720               }
721
722               if (err_type < ERR_WT_AA) {
723                 for (j = i; j < (int) phdr->caplen; j++) {
724                   buf[j] = 0xAA;
725                 }
726                 i = phdr->caplen;
727               }
728             }
729           }
730         }
731
732         if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
733                        &err)) {
734
735           fprintf(stderr, "editcap: Error writing to %s: %s\n",
736                   filename, wtap_strerror(err));
737           exit(1);
738
739         }
740
741         written_count++;
742
743       }
744
745       count++;
746
747     }
748
749     if (err != 0) {
750       /* Print a message noting that the read failed somewhere along the line. */
751       fprintf(stderr,
752               "editcap: An error occurred while reading \"%s\": %s.\n",
753               argv[optind], wtap_strerror(err));
754       switch (err) {
755
756       case WTAP_ERR_UNSUPPORTED:
757       case WTAP_ERR_UNSUPPORTED_ENCAP:
758       case WTAP_ERR_BAD_RECORD:
759         fprintf(stderr, "(%s)\n", err_info);
760         break;
761       }
762     }
763
764     if (!wtap_dump_close(pdh, &err)) {
765
766       fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
767               wtap_strerror(err));
768       exit(1);
769
770     }
771   }
772
773   return 0;
774 }
775