From Jim Young via bug 5580: Only update the time elapsed between the previous displa...
[obnox/wireshark/wip.git] / file.c
1 /* file.c
2  * File I/O routines
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #include <time.h>
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <errno.h>
40 #include <signal.h>
41
42 #ifdef HAVE_FCNTL_H
43 #include <fcntl.h>
44 #endif
45
46 #include <epan/epan.h>
47 #include <epan/filesystem.h>
48
49 #include "color.h"
50 #include "color_filters.h"
51 #include "cfile.h"
52 #include <epan/column.h>
53 #include <epan/packet.h>
54 #include <epan/column-utils.h>
55 #include "packet-range.h"
56 #include "print.h"
57 #include "file.h"
58 #include "fileset.h"
59 #include "tempfile.h"
60 #include "merge.h"
61 #include "alert_box.h"
62 #include "simple_dialog.h"
63 #include "main_statusbar.h"
64 #include "progress_dlg.h"
65 #include "ui_util.h"
66 #include <epan/prefs.h>
67 #include <epan/dfilter/dfilter.h>
68 #include <epan/epan_dissect.h>
69 #include <epan/tap.h>
70 #include <epan/dissectors/packet-data.h>
71 #include <epan/dissectors/packet-ber.h>
72 #include <epan/timestamp.h>
73 #include <epan/dfilter/dfilter-macro.h>
74 #include <wsutil/file_util.h>
75 #include <epan/strutil.h>
76 #include <epan/addr_resolv.h>
77
78 #ifdef HAVE_LIBPCAP
79 gboolean auto_scroll_live;
80 #endif
81
82 static guint32 cum_bytes;
83 static nstime_t first_ts;
84 static nstime_t prev_dis_ts;
85 static nstime_t prev_cap_ts;
86
87 static gulong computed_elapsed;
88
89 static void cf_reset_state(capture_file *cf);
90
91 static int read_packet(capture_file *cf, dfilter_t *dfcode,
92     gboolean filtering_tap_listeners, guint tap_flags, gint64 offset);
93
94 static void rescan_packets(capture_file *cf, const char *action, const char *action_item,
95     gboolean refilter, gboolean redissect);
96
97 typedef enum {
98   MR_NOTMATCHED,
99   MR_MATCHED,
100   MR_ERROR
101 } match_result;
102 static match_result match_protocol_tree(capture_file *cf, frame_data *fdata,
103     void *criterion);
104 static void match_subtree_text(proto_node *node, gpointer data);
105 static match_result match_summary_line(capture_file *cf, frame_data *fdata,
106     void *criterion);
107 static match_result match_ascii_and_unicode(capture_file *cf, frame_data *fdata,
108     void *criterion);
109 static match_result match_ascii(capture_file *cf, frame_data *fdata,
110     void *criterion);
111 static match_result match_unicode(capture_file *cf, frame_data *fdata,
112     void *criterion);
113 static match_result match_binary(capture_file *cf, frame_data *fdata,
114     void *criterion);
115 static match_result match_dfilter(capture_file *cf, frame_data *fdata,
116     void *criterion);
117 static match_result match_marked(capture_file *cf, frame_data *fdata,
118     void *criterion);
119 static match_result match_time_reference(capture_file *cf, frame_data *fdata,
120     void *criterion);
121 static gboolean find_packet(capture_file *cf,
122     match_result (*match_function)(capture_file *, frame_data *, void *),
123     void *criterion, search_direction dir);
124
125 static void cf_open_failure_alert_box(const char *filename, int err,
126                       gchar *err_info, gboolean for_writing,
127                       int file_type);
128 static const char *file_rename_error_message(int err);
129 static void cf_close_failure_alert_box(const char *filename, int err);
130 static void ref_time_packets(capture_file *cf);
131 /* Update the progress bar this many times when reading a file. */
132 #define N_PROGBAR_UPDATES   100
133 /* We read around 200k/100ms don't update the progress bar more often than that */
134 #define MIN_QUANTUM         200000
135 #define MIN_NUMBER_OF_PACKET 1500
136
137 /* Number of "frame_data" structures per memory chunk.
138    XXX - is this the right number? */
139 #define FRAME_DATA_CHUNK_SIZE   1024
140
141
142 /*
143  * We could probably use g_signal_...() instead of the callbacks below but that
144  * would require linking our CLI programs to libgobject and creating an object
145  * instance for the signals.
146  */
147 typedef struct {
148   cf_callback_t cb_fct;
149   gpointer user_data;
150 } cf_callback_data_t;
151
152 static GList *cf_callbacks = NULL;
153
154 static void
155 cf_callback_invoke(int event, gpointer data)
156 {
157   cf_callback_data_t *cb;
158   GList *cb_item = cf_callbacks;
159
160   /* there should be at least one interested */
161   g_assert(cb_item != NULL);
162
163   while(cb_item != NULL) {
164     cb = cb_item->data;
165     cb->cb_fct(event, data, cb->user_data);
166     cb_item = g_list_next(cb_item);
167   }
168 }
169
170
171 void
172 cf_callback_add(cf_callback_t func, gpointer user_data)
173 {
174   cf_callback_data_t *cb;
175
176   cb = g_malloc(sizeof(cf_callback_data_t));
177   cb->cb_fct = func;
178   cb->user_data = user_data;
179
180   cf_callbacks = g_list_append(cf_callbacks, cb);
181 }
182
183 void
184 cf_callback_remove(cf_callback_t func)
185 {
186   cf_callback_data_t *cb;
187   GList *cb_item = cf_callbacks;
188
189   while(cb_item != NULL) {
190     cb = cb_item->data;
191     if(cb->cb_fct == func) {
192       cf_callbacks = g_list_remove(cf_callbacks, cb);
193       g_free(cb);
194       return;
195     }
196     cb_item = g_list_next(cb_item);
197   }
198
199   g_assert_not_reached();
200 }
201
202 void
203 cf_timestamp_auto_precision(capture_file *cf)
204 {
205   int i;
206   int prec = timestamp_get_precision();
207
208
209   /* don't try to get the file's precision if none is opened */
210   if(cf->state == FILE_CLOSED) {
211     return;
212   }
213
214   /* if we are in auto mode, set precision of current file */
215   if(prec == TS_PREC_AUTO ||
216      prec == TS_PREC_AUTO_SEC ||
217      prec == TS_PREC_AUTO_DSEC ||
218      prec == TS_PREC_AUTO_CSEC ||
219      prec == TS_PREC_AUTO_MSEC ||
220      prec == TS_PREC_AUTO_USEC ||
221      prec == TS_PREC_AUTO_NSEC)
222   {
223     switch(wtap_file_tsprecision(cf->wth)) {
224     case(WTAP_FILE_TSPREC_SEC):
225       timestamp_set_precision(TS_PREC_AUTO_SEC);
226       break;
227     case(WTAP_FILE_TSPREC_DSEC):
228       timestamp_set_precision(TS_PREC_AUTO_DSEC);
229       break;
230     case(WTAP_FILE_TSPREC_CSEC):
231       timestamp_set_precision(TS_PREC_AUTO_CSEC);
232       break;
233     case(WTAP_FILE_TSPREC_MSEC):
234       timestamp_set_precision(TS_PREC_AUTO_MSEC);
235       break;
236     case(WTAP_FILE_TSPREC_USEC):
237       timestamp_set_precision(TS_PREC_AUTO_USEC);
238       break;
239     case(WTAP_FILE_TSPREC_NSEC):
240       timestamp_set_precision(TS_PREC_AUTO_NSEC);
241       break;
242     default:
243       g_assert_not_reached();
244     }
245   }
246   /* Set the column widths of those columns that show the time in
247      "command-line-specified" format. */
248   for (i = 0; i < cf->cinfo.num_cols; i++) {
249     if (col_has_time_fmt(&cf->cinfo, i)) {
250       new_packet_list_resize_column(i);
251     }
252   }
253 }
254
255 gulong
256 cf_get_computed_elapsed(void)
257 {
258   return computed_elapsed;
259 }
260
261 static void reset_elapsed(void)
262 {
263   computed_elapsed = 0;
264 }
265
266 static void compute_elapsed(GTimeVal *start_time)
267 {
268   gdouble    delta_time;
269   GTimeVal   time_now;
270
271   g_get_current_time(&time_now);
272
273   delta_time = (time_now.tv_sec - start_time->tv_sec) * 1e6 +
274     time_now.tv_usec - start_time->tv_usec;
275
276   computed_elapsed = (gulong) (delta_time / 1000); /* ms*/
277 }
278
279 cf_status_t
280 cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
281 {
282   wtap       *wth;
283   gchar       *err_info;
284
285   wth = wtap_open_offline(fname, err, &err_info, TRUE);
286   if (wth == NULL)
287     goto fail;
288
289   /* The open succeeded.  Close whatever capture file we had open,
290      and fill in the information for this file. */
291   cf_close(cf);
292
293   /* Cleanup all data structures used for dissection. */
294   cleanup_dissection();
295   /* Initialize all data structures used for dissection. */
296   init_dissection();
297
298   /* We're about to start reading the file. */
299   cf->state = FILE_READ_IN_PROGRESS;
300
301   cf->wth = wth;
302   cf->f_datalen = 0;
303
304   /* Set the file name because we need it to set the follow stream filter.
305      XXX - is that still true?  We need it for other reasons, though,
306      in any case. */
307   cf->filename = g_strdup(fname);
308
309   /* Indicate whether it's a permanent or temporary file. */
310   cf->is_tempfile = is_tempfile;
311
312   /* If it's a temporary capture buffer file, mark it as not saved. */
313   cf->user_saved = !is_tempfile;
314
315   reset_elapsed();
316
317   cf->cd_t        = wtap_file_type(cf->wth);
318   cf->count     = 0;
319   cf->displayed_count = 0;
320   cf->marked_count = 0;
321   cf->ignored_count = 0;
322   cf->ref_time_count = 0;
323   cf->drops_known = FALSE;
324   cf->drops     = 0;
325   cf->snap      = wtap_snapshot_length(cf->wth);
326   if (cf->snap == 0) {
327     /* Snapshot length not known. */
328     cf->has_snap = FALSE;
329     cf->snap = WTAP_MAX_PACKET_SIZE;
330   } else
331     cf->has_snap = TRUE;
332
333   /* Allocate a frame_data_sequence for the frames in this file */
334   cf->frames = new_frame_data_sequence();
335
336   nstime_set_zero(&cf->elapsed_time);
337   nstime_set_unset(&first_ts);
338   nstime_set_unset(&prev_dis_ts);
339   nstime_set_unset(&prev_cap_ts);
340   cum_bytes = 0;
341
342   /* Adjust timestamp precision if auto is selected, col width will be adjusted */
343   cf_timestamp_auto_precision(cf);
344   /* XXX needed ? */
345   new_packet_list_queue_draw();
346   fileset_file_opened(fname);
347
348   if(cf->cd_t == WTAP_FILE_BER) {
349     /* tell the BER dissector the file name */
350     ber_set_filename(cf->filename);
351   }
352
353   wtap_set_cb_new_ipv4(cf->wth, add_ipv4_name);
354   wtap_set_cb_new_ipv6(cf->wth, (wtap_new_ipv6_callback_t) add_ipv6_name);
355
356   return CF_OK;
357
358 fail:
359   cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
360   return CF_ERROR;
361 }
362
363
364 /*
365  * Reset the state for the currently closed file, but don't do the
366  * UI callbacks; this is for use in "cf_open()", where we don't
367  * want the UI to go from "file open" to "file closed" back to
368  * "file open", we want it to go from "old file open" to "new file
369  * open and being read".
370  */
371 static void
372 cf_reset_state(capture_file *cf)
373 {
374   /* Die if we're in the middle of reading a file. */
375   g_assert(cf->state != FILE_READ_IN_PROGRESS);
376
377   if (cf->wth) {
378     wtap_close(cf->wth);
379     cf->wth = NULL;
380   }
381   /* We have no file open... */
382   if (cf->filename != NULL) {
383     /* If it's a temporary file, remove it. */
384     if (cf->is_tempfile)
385       ws_unlink(cf->filename);
386     g_free(cf->filename);
387     cf->filename = NULL;
388   }
389   /* ...which means we have nothing to save. */
390   cf->user_saved = FALSE;
391
392   dfilter_free(cf->rfcode);
393   cf->rfcode = NULL;
394   if (cf->frames != NULL) {
395     free_frame_data_sequence(cf->frames);
396     cf->frames = NULL;
397   }
398 #ifdef WANT_PACKET_EDITOR
399   if (cf->edited_frames) {
400     g_tree_destroy(cf->edited_frames);
401     cf->edited_frames = NULL;
402   }
403 #endif
404   cf_unselect_packet(cf);   /* nothing to select */
405   cf->first_displayed = 0;
406   cf->last_displayed = 0;
407
408   /* No frames, no frame selected, no field in that frame selected. */
409   cf->count = 0;
410   cf->current_frame = 0;
411   cf->current_row = 0;
412   cf->finfo_selected = NULL;
413
414   /* Clear the packet list. */
415   new_packet_list_freeze();
416   new_packet_list_clear();
417   new_packet_list_thaw();
418
419   cf->f_datalen = 0;
420   nstime_set_zero(&cf->elapsed_time);
421
422   reset_tap_listeners();
423
424   /* We have no file open. */
425   cf->state = FILE_CLOSED;
426
427   fileset_file_closed();
428 }
429
430 /* Reset everything to a pristine state */
431 void
432 cf_close(capture_file *cf)
433 {
434   /* do GUI things even if file is already closed,
435    * e.g. to cleanup things if a capture couldn't be started */
436   cf_callback_invoke(cf_cb_file_closing, cf);
437
438   /* close things, if not already closed before */
439   if(cf->state != FILE_CLOSED) {
440     color_filters_cleanup();
441     cf_reset_state(cf);
442     cleanup_dissection();
443   }
444
445   cf_callback_invoke(cf_cb_file_closed, cf);
446 }
447
448 /* an out of memory exception occured, wait for a user button press to exit */
449 static void outofmemory_cb(gpointer dialog _U_, gint btn _U_, gpointer data _U_)
450 {
451     main_window_exit();
452 }
453
454 static float
455 calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos, gchar *status_str, gulong status_size)
456 {
457   float   progbar_val;
458
459   progbar_val = (gfloat) file_pos / (gfloat) size;
460   if (progbar_val > 1.0) {
461
462     /*  The file probably grew while we were reading it.
463      *  Update file size, and try again.
464      */
465     size = wtap_file_size(cf->wth, NULL);
466
467     if (size >= 0)
468       progbar_val = (gfloat) file_pos / (gfloat) size;
469
470     /*  If it's still > 1, either "wtap_file_size()" failed (in which
471      *  case there's not much we can do about it), or the file
472      *  *shrank* (in which case there's not much we can do about
473      *  it); just clip the progress value at 1.0.
474      */
475     if (progbar_val > 1.0f)
476       progbar_val = 1.0f;
477   }
478
479   g_snprintf(status_str, status_size,
480              "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
481              file_pos / 1024, size / 1024);
482
483   return progbar_val;
484 }
485
486 cf_read_status_t
487 cf_read(capture_file *cf, gboolean from_save)
488 {
489   int         err;
490   gchar       *err_info;
491   const gchar *name_ptr;
492   const char  *errmsg;
493   char         errmsg_errno[1024+1];
494   gint64       data_offset;
495   gint64       file_pos;
496   progdlg_t *volatile progbar = NULL;
497   gboolean     stop_flag;
498   volatile gint64 size;
499   volatile float progbar_val;
500   GTimeVal     start_time;
501   gchar        status_str[100];
502   volatile gint64 progbar_nextstep;
503   volatile gint64 progbar_quantum;
504   dfilter_t   *dfcode;
505   gboolean    filtering_tap_listeners;
506   guint       tap_flags;
507   volatile int count = 0;
508 #ifdef HAVE_LIBPCAP
509   volatile int displayed_once = 0;
510 #endif
511   gboolean compiled;
512
513   /* Compile the current display filter.
514    * We assume this will not fail since cf->dfilter is only set in
515    * cf_filter IFF the filter was valid.
516    */
517   compiled = dfilter_compile(cf->dfilter, &dfcode);
518   g_assert(!cf->dfilter || (compiled && dfcode));
519
520   /* Do we have any tap listeners with filters? */
521   filtering_tap_listeners = have_filtering_tap_listeners();
522
523   /* Get the union of the flags for all tap listeners. */
524   tap_flags = union_of_tap_listener_flags();
525
526   reset_tap_listeners();
527
528   name_ptr = get_basename(cf->filename);
529
530   if (from_save == FALSE)
531     cf_callback_invoke(cf_cb_file_read_started, cf);
532   else
533     cf_callback_invoke(cf_cb_file_save_started, (gpointer)name_ptr);
534
535   /* Find the size of the file. */
536   size = wtap_file_size(cf->wth, NULL);
537
538   /* Update the progress bar when it gets to this value. */
539   progbar_nextstep = 0;
540   /* When we reach the value that triggers a progress bar update,
541      bump that value by this amount. */
542   if (size >= 0){
543     progbar_quantum = size/N_PROGBAR_UPDATES;
544     if (progbar_quantum < MIN_QUANTUM)
545       progbar_quantum = MIN_QUANTUM;
546   }else
547     progbar_quantum = 0;
548   /* Progress so far. */
549   progbar_val = 0.0f;
550
551   /* The packet list window will be empty untill the file is completly loaded */
552   new_packet_list_freeze();
553
554   stop_flag = FALSE;
555   g_get_current_time(&start_time);
556
557   while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
558     if (size >= 0) {
559       count++;
560       file_pos = wtap_read_so_far(cf->wth);
561
562       /* Create the progress bar if necessary.
563        * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
564        */
565       if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)){
566         progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
567         if (from_save == FALSE)
568           progbar = delayed_create_progress_dlg("Loading", name_ptr,
569                                                 TRUE, &stop_flag, &start_time, progbar_val);
570         else
571           progbar = delayed_create_progress_dlg("Saving", name_ptr,
572                                                 TRUE, &stop_flag, &start_time, progbar_val);
573       }
574
575       /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
576          when we update it, we have to run the GTK+ main loop to get it
577          to repaint what's pending, and doing so may involve an "ioctl()"
578          to see if there's any pending input from an X server, and doing
579          that for every packet can be costly, especially on a big file. */
580       if (file_pos >= progbar_nextstep) {
581         if (progbar != NULL) {
582           progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
583           /* update the packet bar content on the first run or frequently on very large files */
584 #ifdef HAVE_LIBPCAP
585           if (progbar_quantum > 500000 || displayed_once == 0) {
586             if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->count != 0) {
587               displayed_once = 1;
588               packets_bar_update();
589             }
590           }
591 #endif /* HAVE_LIBPCAP */
592           update_progress_dlg(progbar, progbar_val, status_str);
593         }
594         progbar_nextstep += progbar_quantum;
595       }
596     }
597
598     if (stop_flag) {
599       /* Well, the user decided to abort the read. He/She will be warned and
600          it might be enough for him/her to work with the already loaded
601          packets.
602          This is especially true for very large capture files, where you don't
603          want to wait loading the whole file (which may last minutes or even
604          hours even on fast machines) just to see that it was the wrong file. */
605       break;
606     }
607     TRY {
608       read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
609     }
610     CATCH(OutOfMemoryError) {
611       gpointer dialog;
612
613       dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
614                              "%sOut Of Memory!%s\n"
615                              "\n"
616                              "Sorry, but Wireshark has to terminate now!\n"
617                              "\n"
618                              "Some infos / workarounds can be found at:\n"
619                              "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
620                              simple_dialog_primary_start(), simple_dialog_primary_end());
621       /* we have to terminate, as we cannot recover from the memory error */
622       simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
623       while(1) {
624         main_window_update();
625         /* XXX - how to avoid a busy wait? */
626         /* Sleep(100); */
627       };
628       break;
629     }
630     ENDTRY;
631   }
632
633   /* Cleanup and release all dfilter resources */
634   if (dfcode != NULL){
635     dfilter_free(dfcode);
636   }
637
638   /* We're done reading the file; destroy the progress bar if it was created. */
639   if (progbar != NULL)
640     destroy_progress_dlg(progbar);
641
642   /* We're done reading sequentially through the file. */
643   cf->state = FILE_READ_DONE;
644
645   /* Close the sequential I/O side, to free up memory it requires. */
646   wtap_sequential_close(cf->wth);
647
648   /* Allow the protocol dissectors to free up memory that they
649    * don't need after the sequential run-through of the packets. */
650   postseq_cleanup_all_protocols();
651
652   /* compute the time it took to load the file */
653   compute_elapsed(&start_time);
654
655   /* Set the file encapsulation type now; we don't know what it is until
656      we've looked at all the packets, as we don't know until then whether
657      there's more than one type (and thus whether it's
658      WTAP_ENCAP_PER_PACKET). */
659   cf->lnk_t = wtap_file_encap(cf->wth);
660
661   cf->current_frame = frame_data_sequence_find(cf->frames, cf->first_displayed);
662   cf->current_row = 0;
663
664   new_packet_list_thaw();
665   if (from_save == FALSE)
666     cf_callback_invoke(cf_cb_file_read_finished, cf);
667   else
668     cf_callback_invoke(cf_cb_file_save_finished, cf);
669
670   /* If we have any displayed packets to select, select the first of those
671      packets by making the first row the selected row. */
672   if (cf->first_displayed != 0){
673     new_packet_list_select_first_row();
674   }
675
676   if(stop_flag) {
677     simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
678                   "%sFile loading was cancelled!%s\n"
679                   "\n"
680                   "The remaining packets in the file were discarded.\n"
681                   "\n"
682                   "As a lot of packets from the original file will be missing,\n"
683                   "remember to be careful when saving the current content to a file.\n",
684                   simple_dialog_primary_start(), simple_dialog_primary_end());
685     return CF_READ_ERROR;
686   }
687
688   if (err != 0) {
689     /* Put up a message box noting that the read failed somewhere along
690        the line.  Don't throw out the stuff we managed to read, though,
691        if any. */
692     switch (err) {
693
694     case WTAP_ERR_UNSUPPORTED_ENCAP:
695       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
696                  "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
697                  err_info);
698       g_free(err_info);
699       errmsg = errmsg_errno;
700       break;
701
702     case WTAP_ERR_CANT_READ:
703       errmsg = "An attempt to read from the capture file failed for"
704         " some unknown reason.";
705       break;
706
707     case WTAP_ERR_SHORT_READ:
708       errmsg = "The capture file appears to have been cut short"
709         " in the middle of a packet.";
710       break;
711
712     case WTAP_ERR_BAD_FILE:
713       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
714                  "The capture file appears to be damaged or corrupt.\n(%s)",
715                  err_info);
716       g_free(err_info);
717       errmsg = errmsg_errno;
718       break;
719
720     case WTAP_ERR_DECOMPRESS:
721       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
722                  "The compressed capture file appears to be damaged or corrupt.\n"
723                  "(%s)", err_info);
724       g_free(err_info);
725       errmsg = errmsg_errno;
726       break;
727
728     default:
729       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
730                  "An error occurred while reading the"
731                  " capture file: %s.", wtap_strerror(err));
732       errmsg = errmsg_errno;
733       break;
734     }
735     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", errmsg);
736     return CF_READ_ERROR;
737   } else
738     return CF_READ_OK;
739 }
740
741 #ifdef HAVE_LIBPCAP
742 cf_status_t
743 cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
744 {
745   cf_status_t cf_status;
746
747   cf_status = cf_open(cf, fname, is_tempfile, err);
748   return cf_status;
749 }
750
751 cf_read_status_t
752 cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
753 {
754   gint64 data_offset = 0;
755   gchar *err_info;
756   volatile int newly_displayed_packets = 0;
757   dfilter_t   *dfcode;
758   gboolean filtering_tap_listeners;
759   guint tap_flags;
760   gboolean compiled;
761
762   /* Compile the current display filter.
763    * We assume this will not fail since cf->dfilter is only set in
764    * cf_filter IFF the filter was valid.
765    */
766   compiled = dfilter_compile(cf->dfilter, &dfcode);
767   g_assert(!cf->dfilter || (compiled && dfcode));
768
769   /* Do we have any tap listeners with filters? */
770   filtering_tap_listeners = have_filtering_tap_listeners();
771
772   /* Get the union of the flags for all tap listeners. */
773   tap_flags = union_of_tap_listener_flags();
774
775   *err = 0;
776
777   new_packet_list_check_end();
778   /* Don't freeze/thaw the list when doing live capture */
779   /*new_packet_list_freeze();*/
780
781   /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
782
783   while (to_read != 0) {
784     wtap_cleareof(cf->wth);
785     if (!wtap_read(cf->wth, err, &err_info, &data_offset)) {
786       break;
787     }
788     if (cf->state == FILE_READ_ABORTED) {
789       /* Well, the user decided to exit Wireshark.  Break out of the
790          loop, and let the code below (which is called even if there
791          aren't any packets left to read) exit. */
792       break;
793     }
794     TRY{
795       if (read_packet(cf, dfcode, filtering_tap_listeners, tap_flags,
796                       data_offset) != -1) {
797         newly_displayed_packets++;
798       }
799     }
800     CATCH(OutOfMemoryError) {
801       gpointer dialog;
802
803       dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
804                              "%sOut Of Memory!%s\n"
805                              "\n"
806                              "Sorry, but Wireshark has to terminate now!\n"
807                              "\n"
808                              "The capture file is not lost, it can be found at:\n"
809                              "%s\n"
810                              "\n"
811                              "Some infos / workarounds can be found at:\n"
812                              "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
813                              simple_dialog_primary_start(), simple_dialog_primary_end(), cf->filename);
814       /* we have to terminate, as we cannot recover from the memory error */
815       simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
816       while(1) {
817         main_window_update();
818         /* XXX - how to avoid a busy wait? */
819         /* Sleep(100); */
820       };
821       /* Don't freeze/thaw the list when doing live capture */
822       /*new_packet_list_thaw();*/
823       return CF_READ_ABORTED;
824     }
825     ENDTRY;
826     to_read--;
827   }
828
829   /* Cleanup and release all dfilter resources */
830   if (dfcode != NULL){
831     dfilter_free(dfcode);
832   }
833
834   /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u",
835     cf->count, cf->state, *err);*/
836
837   /* Don't freeze/thaw the list when doing live capture */
838   /*new_packet_list_thaw();*/
839   /* With the new packet list the first packet
840    * isn't automatically selected.
841    */
842   if(!cf->current_frame)
843     new_packet_list_select_first_row();
844
845   /* moving to the end of the packet list - if the user requested so and
846      we have some new packets. */
847   if (newly_displayed_packets && auto_scroll_live && cf->count != 0)
848       new_packet_list_moveto_end();
849
850   if (cf->state == FILE_READ_ABORTED) {
851     /* Well, the user decided to exit Wireshark.  Return CF_READ_ABORTED
852        so that our caller can kill off the capture child process;
853        this will cause an EOF on the pipe from the child, so
854        "cf_finish_tail()" will be called, and it will clean up
855        and exit. */
856     return CF_READ_ABORTED;
857   } else if (*err != 0) {
858     /* We got an error reading the capture file.
859        XXX - pop up a dialog box instead? */
860     g_warning("Error \"%s\" while reading: \"%s\"\n",
861         wtap_strerror(*err), cf->filename);
862
863     return CF_READ_ERROR;
864   } else
865     return CF_READ_OK;
866 }
867
868 void
869 cf_fake_continue_tail(capture_file *cf) {
870   cf->state = FILE_READ_DONE;
871 }
872
873 cf_read_status_t
874 cf_finish_tail(capture_file *cf, int *err)
875 {
876   gchar *err_info;
877   gint64 data_offset;
878   dfilter_t   *dfcode;
879   gboolean filtering_tap_listeners;
880   guint tap_flags;
881   gboolean compiled;
882
883   /* Compile the current display filter.
884    * We assume this will not fail since cf->dfilter is only set in
885    * cf_filter IFF the filter was valid.
886    */
887   compiled = dfilter_compile(cf->dfilter, &dfcode);
888   g_assert(!cf->dfilter || (compiled && dfcode));
889
890   /* Do we have any tap listeners with filters? */
891   filtering_tap_listeners = have_filtering_tap_listeners();
892
893   /* Get the union of the flags for all tap listeners. */
894   tap_flags = union_of_tap_listener_flags();
895
896   if(cf->wth == NULL) {
897     cf_close(cf);
898     return CF_READ_ERROR;
899   }
900
901   new_packet_list_check_end();
902   /* Don't freeze/thaw the list when doing live capture */
903   /*new_packet_list_freeze();*/
904
905   while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
906     if (cf->state == FILE_READ_ABORTED) {
907       /* Well, the user decided to abort the read.  Break out of the
908          loop, and let the code below (which is called even if there
909      aren't any packets left to read) exit. */
910       break;
911     }
912     read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
913   }
914
915   /* Cleanup and release all dfilter resources */
916   if (dfcode != NULL){
917     dfilter_free(dfcode);
918   }
919
920   /* Don't freeze/thaw the list when doing live capture */
921   /*new_packet_list_thaw();*/
922
923   if (cf->state == FILE_READ_ABORTED) {
924     /* Well, the user decided to abort the read.  We're only called
925        when the child capture process closes the pipe to us (meaning
926        it's probably exited), so we can just close the capture
927        file; we return CF_READ_ABORTED so our caller can do whatever
928        is appropriate when that happens. */
929     cf_close(cf);
930     return CF_READ_ABORTED;
931   }
932
933   if (auto_scroll_live && cf->count != 0)
934     new_packet_list_moveto_end();
935
936   /* We're done reading sequentially through the file. */
937   cf->state = FILE_READ_DONE;
938
939   /* We're done reading sequentially through the file; close the
940      sequential I/O side, to free up memory it requires. */
941   wtap_sequential_close(cf->wth);
942
943   /* Allow the protocol dissectors to free up memory that they
944    * don't need after the sequential run-through of the packets. */
945   postseq_cleanup_all_protocols();
946
947   /* Set the file encapsulation type now; we don't know what it is until
948      we've looked at all the packets, as we don't know until then whether
949      there's more than one type (and thus whether it's
950      WTAP_ENCAP_PER_PACKET). */
951   cf->lnk_t = wtap_file_encap(cf->wth);
952
953   if (*err != 0) {
954     /* We got an error reading the capture file.
955        XXX - pop up a dialog box? */
956     return CF_READ_ERROR;
957   } else {
958     return CF_READ_OK;
959   }
960 }
961 #endif /* HAVE_LIBPCAP */
962
963 const gchar *
964 cf_get_display_name(capture_file *cf)
965 {
966   const gchar *displayname;
967
968   /* Return a name to use in displays */
969   if (!cf->is_tempfile) {
970     /* Get the last component of the file name, and use that. */
971     if (cf->filename){
972       displayname = get_basename(cf->filename);
973     } else {
974       displayname="(No file)";
975     }
976   } else {
977     /* The file we read is a temporary file from a live capture;
978        we don't mention its name. */
979     if (cf->source) {
980       displayname = cf->source;
981     } else {
982       displayname = "(Untitled)";
983     }
984   }
985   return displayname;
986 }
987
988 void cf_set_tempfile_source(capture_file *cf, gchar *source) {
989   if (cf->source) {
990     g_free(cf->source);
991   }
992
993   if (source) {
994     cf->source = g_strdup(source);
995   } else {
996     cf->source = g_strdup("");
997   }
998 }
999
1000 const gchar *cf_get_tempfile_source(capture_file *cf) {
1001   if (!cf->source) {
1002     return "";
1003   }
1004
1005   return cf->source;
1006 }
1007
1008 /* XXX - use a macro instead? */
1009 int
1010 cf_get_packet_count(capture_file *cf)
1011 {
1012   return cf->count;
1013 }
1014
1015 /* XXX - use a macro instead? */
1016 void
1017 cf_set_packet_count(capture_file *cf, int packet_count)
1018 {
1019   cf->count = packet_count;
1020 }
1021
1022 /* XXX - use a macro instead? */
1023 gboolean
1024 cf_is_tempfile(capture_file *cf)
1025 {
1026   return cf->is_tempfile;
1027 }
1028
1029 void cf_set_tempfile(capture_file *cf, gboolean is_tempfile)
1030 {
1031   cf->is_tempfile = is_tempfile;
1032 }
1033
1034
1035 /* XXX - use a macro instead? */
1036 void cf_set_drops_known(capture_file *cf, gboolean drops_known)
1037 {
1038   cf->drops_known = drops_known;
1039 }
1040
1041 /* XXX - use a macro instead? */
1042 void cf_set_drops(capture_file *cf, guint32 drops)
1043 {
1044   cf->drops = drops;
1045 }
1046
1047 /* XXX - use a macro instead? */
1048 gboolean cf_get_drops_known(capture_file *cf)
1049 {
1050   return cf->drops_known;
1051 }
1052
1053 /* XXX - use a macro instead? */
1054 guint32 cf_get_drops(capture_file *cf)
1055 {
1056   return cf->drops;
1057 }
1058
1059 void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
1060 {
1061   cf->rfcode = rfcode;
1062 }
1063
1064 static int
1065 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
1066     dfilter_t *dfcode, gboolean filtering_tap_listeners,
1067     guint tap_flags,
1068     union wtap_pseudo_header *pseudo_header, const guchar *buf,
1069     gboolean refilter,
1070     gboolean add_to_packet_list)
1071 {
1072   gboolean  create_proto_tree = FALSE;
1073   epan_dissect_t edt;
1074   column_info *cinfo;
1075   gint row = -1;
1076
1077   cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
1078
1079   frame_data_set_before_dissect(fdata, &cf->elapsed_time,
1080                                 &first_ts, &prev_dis_ts, &prev_cap_ts);
1081
1082   /* If either
1083     + we have a display filter and are re-applying it;
1084     + we have tap listeners with filters;
1085     + we have tap listeners that require a protocol tree;
1086
1087      allocate a protocol tree root node, so that we'll construct
1088      a protocol tree against which a filter expression can be
1089      evaluated. */
1090   if ((dfcode != NULL && refilter) ||
1091       filtering_tap_listeners || (tap_flags & TL_REQUIRES_PROTO_TREE))
1092       create_proto_tree = TRUE;
1093
1094   /* Dissect the frame. */
1095   epan_dissect_init(&edt, create_proto_tree, FALSE);
1096
1097   if (dfcode != NULL && refilter) {
1098       epan_dissect_prime_dfilter(&edt, dfcode);
1099   }
1100
1101   tap_queue_init(&edt);
1102   epan_dissect_run(&edt, pseudo_header, buf, fdata, cinfo);
1103   tap_push_tapped_queue(&edt);
1104
1105   /* If we have a display filter, apply it if we're refiltering, otherwise
1106      leave the "passed_dfilter" flag alone.
1107
1108      If we don't have a display filter, set "passed_dfilter" to 1. */
1109   if (dfcode != NULL) {
1110     if (refilter) {
1111       fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, &edt) ? 1 : 0;
1112     }
1113   } else
1114     fdata->flags.passed_dfilter = 1;
1115
1116   if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1117     cf->displayed_count++;
1118
1119   if (add_to_packet_list) {
1120     /* We fill the needed columns from new_packet_list */
1121       row = new_packet_list_append(cinfo, fdata, &edt.pi);
1122   }
1123
1124   if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1125   {
1126     frame_data_set_after_dissect(fdata, &cum_bytes, &prev_dis_ts);
1127
1128     /* If we haven't yet seen the first frame, this is it.
1129
1130        XXX - we must do this before we add the row to the display,
1131        as, if the display's GtkCList's selection mode is
1132        GTK_SELECTION_BROWSE, when the first entry is added to it,
1133        "cf_select_packet()" will be called, and it will fetch the row
1134        data for the 0th row, and will get a null pointer rather than
1135        "fdata", as "gtk_clist_append()" won't yet have returned and
1136        thus "gtk_clist_set_row_data()" won't yet have been called.
1137
1138        We thus need to leave behind bread crumbs so that
1139        "cf_select_packet()" can find this frame.  See the comment
1140        in "cf_select_packet()". */
1141     if (cf->first_displayed == 0)
1142       cf->first_displayed = fdata->num;
1143
1144     /* This is the last frame we've seen so far. */
1145     cf->last_displayed = fdata->num;
1146   }
1147
1148   epan_dissect_cleanup(&edt);
1149   return row;
1150 }
1151
1152 /* read in a new packet */
1153 /* returns the row of the new packet in the packet list or -1 if not displayed */
1154 static int
1155 read_packet(capture_file *cf, dfilter_t *dfcode,
1156             gboolean filtering_tap_listeners, guint tap_flags, gint64 offset)
1157 {
1158   const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
1159   union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
1160   const guchar *buf = wtap_buf_ptr(cf->wth);
1161   frame_data    fdlocal;
1162   guint32       framenum;
1163   frame_data   *fdata;
1164   int           passed;
1165   int           row = -1;
1166
1167   /* The frame number of this packet is one more than the count of
1168      frames in this packet. */
1169   framenum = cf->count + 1;
1170
1171   frame_data_init(&fdlocal, framenum, phdr, offset, cum_bytes);
1172
1173   passed = TRUE;
1174   if (cf->rfcode) {
1175     epan_dissect_t edt;
1176     epan_dissect_init(&edt, TRUE, FALSE);
1177     epan_dissect_prime_dfilter(&edt, cf->rfcode);
1178     epan_dissect_run(&edt, pseudo_header, buf, &fdlocal, NULL);
1179     passed = dfilter_apply_edt(cf->rfcode, &edt);
1180     epan_dissect_cleanup(&edt);
1181   }
1182
1183   if (passed) {
1184     /* This does a shallow copy of fdlocal, which is good enough. */
1185     fdata = frame_data_sequence_add(cf->frames, &fdlocal);
1186
1187     cf->count++;
1188     cf->f_datalen = offset + fdlocal.cap_len;
1189
1190     if (!cf->redissecting) {
1191       row = add_packet_to_packet_list(fdata, cf, dfcode,
1192                                       filtering_tap_listeners, tap_flags,
1193                                       pseudo_header, buf, TRUE, TRUE);
1194     }
1195   }
1196
1197   return row;
1198 }
1199
1200 cf_status_t
1201 cf_merge_files(char **out_filenamep, int in_file_count,
1202                char *const *in_filenames, int file_type, gboolean do_append)
1203 {
1204   merge_in_file_t  *in_files, *in_file;
1205   char             *out_filename;
1206   char             *tmpname;
1207   int               out_fd;
1208   wtap_dumper      *pdh;
1209   int               open_err, read_err, write_err, close_err;
1210   gchar            *err_info;
1211   int               err_fileno;
1212   int               i;
1213   char              errmsg_errno[1024+1];
1214   const char       *errmsg;
1215   gboolean          got_read_error = FALSE, got_write_error = FALSE;
1216   gint64            data_offset;
1217   progdlg_t        *progbar = NULL;
1218   gboolean          stop_flag;
1219   gint64            f_len, file_pos;
1220   float             progbar_val;
1221   GTimeVal          start_time;
1222   gchar             status_str[100];
1223   gint64            progbar_nextstep;
1224   gint64            progbar_quantum;
1225
1226   /* open the input files */
1227   if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
1228                            &open_err, &err_info, &err_fileno)) {
1229     g_free(in_files);
1230     cf_open_failure_alert_box(in_filenames[err_fileno], open_err, err_info,
1231                               FALSE, 0);
1232     return CF_ERROR;
1233   }
1234
1235   if (*out_filenamep != NULL) {
1236     out_filename = *out_filenamep;
1237     out_fd = ws_open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
1238     if (out_fd == -1)
1239       open_err = errno;
1240   } else {
1241     out_fd = create_tempfile(&tmpname, "wireshark");
1242     if (out_fd == -1)
1243       open_err = errno;
1244     out_filename = g_strdup(tmpname);
1245     *out_filenamep = out_filename;
1246   }
1247   if (out_fd == -1) {
1248     err_info = NULL;
1249     merge_close_in_files(in_file_count, in_files);
1250     g_free(in_files);
1251     cf_open_failure_alert_box(out_filename, open_err, NULL, TRUE, file_type);
1252     return CF_ERROR;
1253   }
1254
1255   pdh = wtap_dump_fdopen(out_fd, file_type,
1256       merge_select_frame_type(in_file_count, in_files),
1257       merge_max_snapshot_length(in_file_count, in_files),
1258       FALSE /* compressed */, &open_err);
1259   if (pdh == NULL) {
1260     ws_close(out_fd);
1261     merge_close_in_files(in_file_count, in_files);
1262     g_free(in_files);
1263     cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
1264                               file_type);
1265     return CF_ERROR;
1266   }
1267
1268   /* Get the sum of the sizes of all the files. */
1269   f_len = 0;
1270   for (i = 0; i < in_file_count; i++)
1271     f_len += in_files[i].size;
1272
1273   /* Update the progress bar when it gets to this value. */
1274   progbar_nextstep = 0;
1275   /* When we reach the value that triggers a progress bar update,
1276      bump that value by this amount. */
1277   progbar_quantum = f_len/N_PROGBAR_UPDATES;
1278   /* Progress so far. */
1279   progbar_val = 0.0f;
1280
1281   stop_flag = FALSE;
1282   g_get_current_time(&start_time);
1283
1284   /* do the merge (or append) */
1285   for (;;) {
1286     if (do_append)
1287       in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
1288                                          &err_info);
1289     else
1290       in_file = merge_read_packet(in_file_count, in_files, &read_err,
1291                                   &err_info);
1292     if (in_file == NULL) {
1293       /* EOF */
1294       break;
1295     }
1296
1297     if (read_err != 0) {
1298       /* I/O error reading from in_file */
1299       got_read_error = TRUE;
1300       break;
1301     }
1302
1303     /* Get the sum of the data offsets in all of the files. */
1304     data_offset = 0;
1305     for (i = 0; i < in_file_count; i++)
1306       data_offset += in_files[i].data_offset;
1307
1308     /* Create the progress bar if necessary.
1309        We check on every iteration of the loop, so that it takes no
1310        longer than the standard time to create it (otherwise, for a
1311        large file, we might take considerably longer than that standard
1312        time in order to get to the next progress bar step). */
1313     if (progbar == NULL) {
1314       progbar = delayed_create_progress_dlg("Merging", "files",
1315         FALSE, &stop_flag, &start_time, progbar_val);
1316     }
1317
1318     /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1319        when we update it, we have to run the GTK+ main loop to get it
1320        to repaint what's pending, and doing so may involve an "ioctl()"
1321        to see if there's any pending input from an X server, and doing
1322        that for every packet can be costly, especially on a big file. */
1323     if (data_offset >= progbar_nextstep) {
1324         /* Get the sum of the seek positions in all of the files. */
1325         file_pos = 0;
1326         for (i = 0; i < in_file_count; i++)
1327           file_pos += wtap_read_so_far(in_files[i].wth);
1328         progbar_val = (gfloat) file_pos / (gfloat) f_len;
1329         if (progbar_val > 1.0f) {
1330           /* Some file probably grew while we were reading it.
1331              That "shouldn't happen", so we'll just clip the progress
1332              value at 1.0. */
1333           progbar_val = 1.0f;
1334         }
1335         if (progbar != NULL) {
1336           g_snprintf(status_str, sizeof(status_str),
1337                      "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
1338                      file_pos / 1024, f_len / 1024);
1339           update_progress_dlg(progbar, progbar_val, status_str);
1340         }
1341         progbar_nextstep += progbar_quantum;
1342     }
1343
1344     if (stop_flag) {
1345       /* Well, the user decided to abort the merge. */
1346       break;
1347     }
1348
1349     if (!wtap_dump(pdh, wtap_phdr(in_file->wth), wtap_pseudoheader(in_file->wth),
1350          wtap_buf_ptr(in_file->wth), &write_err)) {
1351       got_write_error = TRUE;
1352       break;
1353     }
1354   }
1355
1356   /* We're done merging the files; destroy the progress bar if it was created. */
1357   if (progbar != NULL)
1358     destroy_progress_dlg(progbar);
1359
1360   merge_close_in_files(in_file_count, in_files);
1361   if (!got_read_error && !got_write_error) {
1362     if (!wtap_dump_close(pdh, &write_err))
1363       got_write_error = TRUE;
1364   } else
1365     wtap_dump_close(pdh, &close_err);
1366
1367   if (got_read_error) {
1368     /*
1369      * Find the file on which we got the error, and report the error.
1370      */
1371     for (i = 0; i < in_file_count; i++) {
1372       if (in_files[i].state == GOT_ERROR) {
1373         /* Put up a message box noting that a read failed somewhere along
1374            the line. */
1375         switch (read_err) {
1376
1377         case WTAP_ERR_UNSUPPORTED_ENCAP:
1378           g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1379                      "The capture file %%s has a packet with a network type that Wireshark doesn't support.\n(%s)",
1380                      err_info);
1381           g_free(err_info);
1382           errmsg = errmsg_errno;
1383           break;
1384
1385         case WTAP_ERR_CANT_READ:
1386           errmsg = "An attempt to read from the capture file %s failed for"
1387                    " some unknown reason.";
1388           break;
1389
1390         case WTAP_ERR_SHORT_READ:
1391           errmsg = "The capture file %s appears to have been cut short"
1392                    " in the middle of a packet.";
1393           break;
1394
1395         case WTAP_ERR_BAD_FILE:
1396           g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1397                      "The capture file %%s appears to be damaged or corrupt.\n(%s)",
1398                      err_info);
1399           g_free(err_info);
1400           errmsg = errmsg_errno;
1401           break;
1402
1403         case WTAP_ERR_DECOMPRESS:
1404           g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1405                      "The compressed capture file %%s appears to be damaged or corrupt.\n"
1406                      "(%s)", err_info);
1407           g_free(err_info);
1408           errmsg = errmsg_errno;
1409           break;
1410
1411         default:
1412           g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1413                      "An error occurred while reading the"
1414                      " capture file %%s: %s.", wtap_strerror(read_err));
1415           errmsg = errmsg_errno;
1416           break;
1417         }
1418         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg, in_files[i].filename);
1419       }
1420     }
1421   }
1422
1423   if (got_write_error) {
1424     /* Put up an alert box for the write error. */
1425     if (write_err < 0) {
1426       /* Wiretap error. */
1427       switch (write_err) {
1428
1429       case WTAP_ERR_UNSUPPORTED_ENCAP:
1430         /*
1431          * This is a problem with the particular frame we're writing;
1432          * note that, and give the frame number.
1433          */
1434         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1435                       "Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.",
1436                       in_file->packet_num, in_file->filename,
1437                       wtap_file_type_string(file_type));
1438         break;
1439
1440       default:
1441         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1442                       "An error occurred while writing to the file \"%s\": %s.",
1443                       out_filename, wtap_strerror(write_err));
1444         break;
1445       }
1446     } else {
1447       /* OS error. */
1448       write_failure_alert_box(out_filename, write_err);
1449     }
1450   }
1451
1452   if (got_read_error || got_write_error || stop_flag) {
1453     /* Callers aren't expected to treat an error or an explicit abort
1454        differently - we put up error dialogs ourselves, so they don't
1455        have to. */
1456     return CF_ERROR;
1457   } else
1458     return CF_OK;
1459 }
1460
1461 cf_status_t
1462 cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
1463 {
1464   const char *filter_new = dftext ? dftext : "";
1465   const char *filter_old = cf->dfilter ? cf->dfilter : "";
1466   dfilter_t   *dfcode;
1467   GTimeVal     start_time;
1468
1469   /* if new filter equals old one, do nothing unless told to do so */
1470   if (!force && strcmp(filter_new, filter_old) == 0) {
1471     return CF_OK;
1472   }
1473
1474   dfcode=NULL;
1475
1476   if (dftext == NULL) {
1477     /* The new filter is an empty filter (i.e., display all packets).
1478      * so leave dfcode==NULL
1479      */
1480   } else {
1481     /*
1482      * We have a filter; make a copy of it (as we'll be saving it),
1483      * and try to compile it.
1484      */
1485     dftext = g_strdup(dftext);
1486     if (!dfilter_compile(dftext, &dfcode)) {
1487       /* The attempt failed; report an error. */
1488       gchar *safe_dftext = simple_dialog_format_message(dftext);
1489       gchar *safe_dfilter_error_msg = simple_dialog_format_message(
1490       dfilter_error_msg);
1491       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1492           "%s%s%s\n"
1493           "\n"
1494           "The following display filter isn't a valid display filter:\n%s\n"
1495           "See the help for a description of the display filter syntax.",
1496           simple_dialog_primary_start(), safe_dfilter_error_msg,
1497           simple_dialog_primary_end(), safe_dftext);
1498       g_free(safe_dfilter_error_msg);
1499       g_free(safe_dftext);
1500       g_free(dftext);
1501       return CF_ERROR;
1502     }
1503
1504     /* Was it empty? */
1505     if (dfcode == NULL) {
1506       /* Yes - free the filter text, and set it to null. */
1507       g_free(dftext);
1508       dftext = NULL;
1509     }
1510   }
1511
1512   /* We have a valid filter.  Replace the current filter. */
1513   g_free(cf->dfilter);
1514   cf->dfilter = dftext;
1515   g_get_current_time(&start_time);
1516
1517
1518   /* Now rescan the packet list, applying the new filter, but not
1519      throwing away information constructed on a previous pass. */
1520   if (dftext == NULL) {
1521     rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
1522   } else {
1523     rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
1524   }
1525
1526   /* Cleanup and release all dfilter resources */
1527   dfilter_free(dfcode);
1528
1529   return CF_OK;
1530 }
1531
1532 void
1533 cf_reftime_packets(capture_file *cf)
1534 {
1535
1536   ref_time_packets(cf);
1537 }
1538
1539 void
1540 cf_redissect_packets(capture_file *cf)
1541 {
1542   rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
1543 }
1544
1545 gboolean
1546 cf_read_frame_r(capture_file *cf, frame_data *fdata,
1547                 union wtap_pseudo_header *pseudo_header, guint8 *pd)
1548 {
1549   int err;
1550   gchar *err_info;
1551   char errmsg_errno[1024+1];
1552
1553 #ifdef WANT_PACKET_EDITOR
1554   /* if fdata->file_off == -1 it means packet was edited, and we must find data inside edited_frames tree */
1555   if (G_UNLIKELY(fdata->file_off == -1)) {
1556     const modified_frame_data *frame = (const modified_frame_data *) g_tree_lookup(cf->edited_frames, GINT_TO_POINTER(fdata->num));
1557
1558     if (!frame) {
1559       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "fdata->file_off == -1, but can't find modified frame!");
1560       return FALSE;
1561     }
1562
1563     *pseudo_header = frame->ph;
1564     memcpy(pd, frame->pd, fdata->cap_len);
1565     return TRUE;
1566   }
1567 #endif
1568
1569   if (!wtap_seek_read(cf->wth, fdata->file_off, pseudo_header, pd,
1570                       fdata->cap_len, &err, &err_info)) {
1571     switch (err) {
1572
1573     case WTAP_ERR_UNSUPPORTED_ENCAP:
1574       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1575                  "The file \"%%s\" has a packet with a network type that Wireshark doesn't support.\n(%s)",
1576                  err_info);
1577       g_free(err_info);
1578       break;
1579
1580     case WTAP_ERR_BAD_FILE:
1581       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1582                  "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
1583                  wtap_strerror(err), err_info);
1584       g_free(err_info);
1585       break;
1586
1587     default:
1588       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1589                  "An error occurred while reading from the file \"%%s\": %s.",
1590                  wtap_strerror(err));
1591       break;
1592     }
1593     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg_errno, cf->filename);
1594     return FALSE;
1595   }
1596   return TRUE;
1597 }
1598
1599 gboolean
1600 cf_read_frame(capture_file *cf, frame_data *fdata)
1601 {
1602   return cf_read_frame_r(cf, fdata, &cf->pseudo_header, cf->pd);
1603 }
1604
1605 /* Rescan the list of packets, reconstructing the CList.
1606
1607    "action" describes why we're doing this; it's used in the progress
1608    dialog box.
1609
1610    "action_item" describes what we're doing; it's used in the progress
1611    dialog box.
1612
1613    "refilter" is TRUE if we need to re-evaluate the filter expression.
1614
1615    "redissect" is TRUE if we need to make the dissectors reconstruct
1616    any state information they have (because a preference that affects
1617    some dissector has changed, meaning some dissector might construct
1618    its state differently from the way it was constructed the last time). */
1619 static void
1620 rescan_packets(capture_file *cf, const char *action, const char *action_item,
1621         gboolean refilter, gboolean redissect)
1622 {
1623   /* Rescan packets new packet list */
1624   guint32     framenum;
1625   frame_data *fdata;
1626   progdlg_t  *progbar = NULL;
1627   gboolean    stop_flag;
1628   int         count;
1629   frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
1630   int         selected_frame_num, preceding_frame_num, following_frame_num, prev_frame_num;
1631   gboolean    selected_frame_seen;
1632   float       progbar_val;
1633   GTimeVal    start_time;
1634   gchar       status_str[100];
1635   int         progbar_nextstep;
1636   int         progbar_quantum;
1637   dfilter_t   *dfcode;
1638   gboolean    filtering_tap_listeners;
1639   guint       tap_flags;
1640   gboolean    add_to_packet_list = FALSE;
1641   gboolean compiled;
1642
1643   /* Compile the current display filter.
1644    * We assume this will not fail since cf->dfilter is only set in
1645    * cf_filter IFF the filter was valid.
1646    */
1647   compiled = dfilter_compile(cf->dfilter, &dfcode);
1648   g_assert(!cf->dfilter || (compiled && dfcode));
1649
1650   /* Do we have any tap listeners with filters? */
1651   filtering_tap_listeners = have_filtering_tap_listeners();
1652
1653   /* Get the union of the flags for all tap listeners. */
1654   tap_flags = union_of_tap_listener_flags();
1655
1656   reset_tap_listeners();
1657   /* Which frame, if any, is the currently selected frame?
1658      XXX - should the selected frame or the focus frame be the "current"
1659      frame, that frame being the one from which "Find Frame" searches
1660      start? */
1661   selected_frame = cf->current_frame;
1662
1663   /* Mark frame num as not found */
1664   selected_frame_num = -1;
1665
1666   /* Freeze the packet list while we redo it, so we don't get any
1667      screen updates while it happens. */
1668   new_packet_list_freeze();
1669
1670   if (redissect) {
1671     /* We need to re-initialize all the state information that protocols
1672        keep, because some preference that controls a dissector has changed,
1673        which might cause the state information to be constructed differently
1674        by that dissector. */
1675
1676     /* We might receive new packets while redissecting, and we don't
1677        want to dissect those before their time. */
1678     cf->redissecting = TRUE;
1679
1680     /* Cleanup all data structures used for dissection. */
1681     cleanup_dissection();
1682     /* Initialize all data structures used for dissection. */
1683     init_dissection();
1684
1685     /* We need to redissect the packets so we have to discard our old
1686      * packet list store. */
1687     new_packet_list_clear();
1688     add_to_packet_list = TRUE;
1689   }
1690
1691   /* We don't yet know which will be the first and last frames displayed. */
1692   cf->first_displayed = 0;
1693   cf->last_displayed = 0;
1694
1695   /* We currently don't display any packets */
1696   cf->displayed_count = 0;
1697
1698   /* Iterate through the list of frames.  Call a routine for each frame
1699      to check whether it should be displayed and, if so, add it to
1700      the display list. */
1701   nstime_set_unset(&first_ts);
1702   nstime_set_unset(&prev_dis_ts);
1703   nstime_set_unset(&prev_cap_ts);
1704   cum_bytes = 0;
1705
1706   /* Update the progress bar when it gets to this value. */
1707   progbar_nextstep = 0;
1708   /* When we reach the value that triggers a progress bar update,
1709      bump that value by this amount. */
1710   progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1711   /* Count of packets at which we've looked. */
1712   count = 0;
1713   /* Progress so far. */
1714   progbar_val = 0.0f;
1715
1716   stop_flag = FALSE;
1717   g_get_current_time(&start_time);
1718
1719   /* no previous row yet */
1720   prev_frame_num = -1;
1721   prev_frame = NULL;
1722
1723   preceding_frame_num = -1;
1724   preceding_frame = NULL;
1725   following_frame_num = -1;
1726   following_frame = NULL;
1727
1728   selected_frame_seen = FALSE;
1729
1730   for (framenum = 1; framenum <= cf->count; framenum++) {
1731     fdata = frame_data_sequence_find(cf->frames, framenum);
1732
1733     /* Create the progress bar if necessary.
1734        We check on every iteration of the loop, so that it takes no
1735        longer than the standard time to create it (otherwise, for a
1736        large file, we might take considerably longer than that standard
1737        time in order to get to the next progress bar step). */
1738     if (progbar == NULL)
1739       progbar = delayed_create_progress_dlg(action, action_item, TRUE,
1740                                             &stop_flag, &start_time,
1741                                             progbar_val);
1742
1743     /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1744        when we update it, we have to run the GTK+ main loop to get it
1745        to repaint what's pending, and doing so may involve an "ioctl()"
1746        to see if there's any pending input from an X server, and doing
1747        that for every packet can be costly, especially on a big file. */
1748     if (count >= progbar_nextstep) {
1749       /* let's not divide by zero. I should never be started
1750        * with count == 0, so let's assert that
1751        */
1752       g_assert(cf->count > 0);
1753       progbar_val = (gfloat) count / cf->count;
1754
1755       if (progbar != NULL) {
1756         g_snprintf(status_str, sizeof(status_str),
1757                   "%4u of %u frames", count, cf->count);
1758         update_progress_dlg(progbar, progbar_val, status_str);
1759       }
1760
1761       progbar_nextstep += progbar_quantum;
1762     }
1763
1764     if (stop_flag) {
1765       /* Well, the user decided to abort the filtering.  Just stop.
1766
1767          XXX - go back to the previous filter?  Users probably just
1768          want not to wait for a filtering operation to finish;
1769          unless we cancel by having no filter, reverting to the
1770          previous filter will probably be even more expensive than
1771          continuing the filtering, as it involves going back to the
1772          beginning and filtering, and even with no filter we currently
1773          have to re-generate the entire clist, which is also expensive.
1774
1775          I'm not sure what Network Monitor does, but it doesn't appear
1776          to give you an unfiltered display if you cancel. */
1777       break;
1778     }
1779
1780     count++;
1781
1782     if (redissect) {
1783       /* Since all state for the frame was destroyed, mark the frame
1784        * as not visited, free the GSList referring to the state
1785        * data (the per-frame data itself was freed by
1786        * "init_dissection()"), and null out the GSList pointer. */
1787       fdata->flags.visited = 0;
1788       frame_data_cleanup(fdata);
1789     }
1790
1791     if (!cf_read_frame(cf, fdata))
1792       break; /* error reading the frame */
1793
1794     /* If the previous frame is displayed, and we haven't yet seen the
1795        selected frame, remember that frame - it's the closest one we've
1796        yet seen before the selected frame. */
1797     if (prev_frame_num != -1 && !selected_frame_seen && prev_frame->flags.passed_dfilter) {
1798       preceding_frame_num = prev_frame_num;
1799       preceding_frame = prev_frame;
1800     }
1801     add_packet_to_packet_list(fdata, cf, dfcode, filtering_tap_listeners,
1802                                     tap_flags, &cf->pseudo_header, cf->pd,
1803                                     refilter,
1804                                     add_to_packet_list);
1805
1806     /* If this frame is displayed, and this is the first frame we've
1807        seen displayed after the selected frame, remember this frame -
1808        it's the closest one we've yet seen at or after the selected
1809        frame. */
1810     if (fdata->flags.passed_dfilter && selected_frame_seen && following_frame_num == -1) {
1811       following_frame_num = fdata->num;
1812       following_frame = fdata;
1813     }
1814     if (fdata == selected_frame) {
1815       selected_frame_seen = TRUE;
1816       if (fdata->flags.passed_dfilter)
1817           selected_frame_num = fdata->num;
1818     }
1819
1820     /* Remember this frame - it'll be the previous frame
1821        on the next pass through the loop. */
1822     prev_frame_num = fdata->num;
1823     prev_frame = fdata;
1824   }
1825
1826   /* We are done redissecting the packet list. */
1827   cf->redissecting = FALSE;
1828
1829   if (redissect) {
1830     /* Clear out what remains of the visited flags and per-frame data
1831        pointers.
1832
1833        XXX - that may cause various forms of bogosity when dissecting
1834        these frames, as they won't have been seen by this sequential
1835        pass, but the only alternative I see is to keep scanning them
1836        even though the user requested that the scan stop, and that
1837        would leave the user stuck with an Wireshark grinding on
1838        until it finishes.  Should we just stick them with that? */
1839     for (; framenum <= cf->count; framenum++) {
1840       fdata = frame_data_sequence_find(cf->frames, framenum);
1841       fdata->flags.visited = 0;
1842       frame_data_cleanup(fdata);
1843     }
1844   }
1845
1846   /* We're done filtering the packets; destroy the progress bar if it
1847      was created. */
1848   if (progbar != NULL)
1849     destroy_progress_dlg(progbar);
1850
1851   /* Unfreeze the packet list. */
1852   if (!add_to_packet_list)
1853     new_packet_list_recreate_visible_rows();
1854
1855   /* Compute the time it took to filter the file */
1856   compute_elapsed(&start_time);
1857
1858   new_packet_list_thaw();
1859
1860   if (selected_frame_num == -1) {
1861     /* The selected frame didn't pass the filter. */
1862     if (selected_frame == NULL) {
1863       /* That's because there *was* no selected frame.  Make the first
1864          displayed frame the current frame. */
1865       selected_frame_num = 0;
1866     } else {
1867       /* Find the nearest displayed frame to the selected frame (whether
1868          it's before or after that frame) and make that the current frame.
1869          If the next and previous displayed frames are equidistant from the
1870          selected frame, choose the next one. */
1871       g_assert(following_frame == NULL ||
1872                following_frame->num >= selected_frame->num);
1873       g_assert(preceding_frame == NULL ||
1874                preceding_frame->num <= selected_frame->num);
1875       if (following_frame == NULL) {
1876         /* No frame after the selected frame passed the filter, so we
1877            have to select the last displayed frame before the selected
1878            frame. */
1879         selected_frame_num = preceding_frame_num;
1880         selected_frame = preceding_frame;
1881       } else if (preceding_frame == NULL) {
1882         /* No frame before the selected frame passed the filter, so we
1883            have to select the first displayed frame after the selected
1884            frame. */
1885         selected_frame_num = following_frame_num;
1886         selected_frame = following_frame;
1887       } else {
1888         /* Frames before and after the selected frame passed the filter, so
1889            we'll select the previous frame */
1890         selected_frame_num = preceding_frame_num;
1891         selected_frame = preceding_frame;
1892       }
1893     }
1894   }
1895
1896   if (selected_frame_num == -1) {
1897     /* There are no frames displayed at all. */
1898     cf_unselect_packet(cf);
1899   } else {
1900     /* Either the frame that was selected passed the filter, or we've
1901        found the nearest displayed frame to that frame.  Select it, make
1902        it the focus row, and make it visible. */
1903     /* Set to invalid to force update of packet list and packet details */
1904     cf->current_row = -1;
1905     if (selected_frame_num == 0) {
1906       new_packet_list_select_first_row();
1907     }else{
1908       if (!new_packet_list_select_row_from_data(selected_frame)) {
1909         /* We didn't find a row corresponding to this frame.
1910            This means that the frame isn't being displayed currently,
1911            so we can't select it. */
1912         simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
1913                       "%sEnd of capture exceeded!%s\n\n"
1914                       "The capture file is probably not fully dissected.",
1915                       simple_dialog_primary_start(), simple_dialog_primary_end());
1916       }
1917     }
1918   }
1919
1920   /* Cleanup and release all dfilter resources */
1921   dfilter_free(dfcode);
1922 }
1923
1924
1925 /*
1926  * Scan trough all frame data and recalculate the ref time
1927  * without rereading the file.
1928  * XXX - do we need a progres bar or is this fast enough?
1929  */
1930 static void
1931 ref_time_packets(capture_file *cf)
1932 {
1933   guint32 framenum;
1934   frame_data *fdata;
1935
1936   nstime_set_unset(&first_ts);
1937   nstime_set_unset(&prev_dis_ts);
1938   cum_bytes = 0;
1939
1940   for (framenum = 1; framenum <= cf->count; framenum++) {
1941     fdata = frame_data_sequence_find(cf->frames, framenum);
1942
1943     /* just add some value here until we know if it is being displayed or not */
1944     fdata->cum_bytes = cum_bytes + fdata->pkt_len;
1945
1946     /*
1947      *Timestamps
1948      */
1949
1950     /* If we don't have the time stamp of the first packet in the
1951      capture, it's because this is the first packet.  Save the time
1952      stamp of this packet as the time stamp of the first packet. */
1953     if (nstime_is_unset(&first_ts)) {
1954         first_ts  = fdata->abs_ts;
1955     }
1956       /* if this frames is marked as a reference time frame, reset
1957         firstsec and firstusec to this frame */
1958     if(fdata->flags.ref_time){
1959         first_ts = fdata->abs_ts;
1960     }
1961
1962     /* If we don't have the time stamp of the previous displayed packet,
1963      it's because this is the first displayed packet.  Save the time
1964      stamp of this packet as the time stamp of the previous displayed
1965      packet. */
1966     if (nstime_is_unset(&prev_dis_ts)) {
1967         prev_dis_ts = fdata->abs_ts;
1968     }
1969
1970     /* Get the time elapsed between the first packet and this packet. */
1971     nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
1972
1973     /* If it's greater than the current elapsed time, set the elapsed time
1974      to it (we check for "greater than" so as not to be confused by
1975      time moving backwards). */
1976     if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
1977         || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
1978         cf->elapsed_time = fdata->rel_ts;
1979     }
1980
1981     /* If this frame is displayed, get the time elapsed between the 
1982      previous displayed packet and this packet. */ 
1983     if( fdata->flags.passed_dfilter ) {
1984         nstime_delta(&fdata->del_dis_ts, &fdata->abs_ts, &prev_dis_ts);
1985         prev_dis_ts = fdata->abs_ts;
1986     }
1987
1988     /*
1989      * Byte counts
1990      */
1991     if( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) ){
1992         /* This frame either passed the display filter list or is marked as
1993         a time reference frame.  All time reference frames are displayed
1994         even if they dont pass the display filter */
1995         if(fdata->flags.ref_time){
1996             /* if this was a TIME REF frame we should reset the cum_bytes field */
1997             cum_bytes = fdata->pkt_len;
1998             fdata->cum_bytes =  cum_bytes;
1999         } else {
2000             /* increase cum_bytes with this packets length */
2001             cum_bytes += fdata->pkt_len;
2002         }
2003     }
2004   }
2005 }
2006
2007 typedef enum {
2008   PSP_FINISHED,
2009   PSP_STOPPED,
2010   PSP_FAILED
2011 } psp_return_t;
2012
2013 static psp_return_t
2014 process_specified_packets(capture_file *cf, packet_range_t *range,
2015     const char *string1, const char *string2, gboolean terminate_is_stop,
2016     gboolean (*callback)(capture_file *, frame_data *,
2017                          union wtap_pseudo_header *, const guint8 *, void *),
2018     void *callback_args)
2019 {
2020   guint32 framenum;
2021   frame_data *fdata;
2022   union wtap_pseudo_header pseudo_header;
2023   guint8      pd[WTAP_MAX_PACKET_SIZE+1];
2024   psp_return_t ret = PSP_FINISHED;
2025
2026   progdlg_t  *progbar = NULL;
2027   int         progbar_count;
2028   float       progbar_val;
2029   gboolean    progbar_stop_flag;
2030   GTimeVal    progbar_start_time;
2031   gchar       progbar_status_str[100];
2032   int         progbar_nextstep;
2033   int         progbar_quantum;
2034   range_process_e process_this;
2035
2036   /* Update the progress bar when it gets to this value. */
2037   progbar_nextstep = 0;
2038   /* When we reach the value that triggers a progress bar update,
2039      bump that value by this amount. */
2040   progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2041   /* Count of packets at which we've looked. */
2042   progbar_count = 0;
2043   /* Progress so far. */
2044   progbar_val = 0.0f;
2045
2046   progbar_stop_flag = FALSE;
2047   g_get_current_time(&progbar_start_time);
2048
2049   packet_range_process_init(range);
2050
2051   /* Iterate through all the packets, printing the packets that
2052      were selected by the current display filter.  */
2053   for (framenum = 1; framenum <= cf->count; framenum++) {
2054     fdata = frame_data_sequence_find(cf->frames, framenum);
2055
2056     /* Create the progress bar if necessary.
2057        We check on every iteration of the loop, so that it takes no
2058        longer than the standard time to create it (otherwise, for a
2059        large file, we might take considerably longer than that standard
2060        time in order to get to the next progress bar step). */
2061     if (progbar == NULL)
2062       progbar = delayed_create_progress_dlg(string1, string2,
2063                                             terminate_is_stop,
2064                                             &progbar_stop_flag,
2065                                             &progbar_start_time,
2066                                             progbar_val);
2067
2068     /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2069        when we update it, we have to run the GTK+ main loop to get it
2070        to repaint what's pending, and doing so may involve an "ioctl()"
2071        to see if there's any pending input from an X server, and doing
2072        that for every packet can be costly, especially on a big file. */
2073     if (progbar_count >= progbar_nextstep) {
2074       /* let's not divide by zero. I should never be started
2075        * with count == 0, so let's assert that
2076        */
2077       g_assert(cf->count > 0);
2078       progbar_val = (gfloat) progbar_count / cf->count;
2079
2080       if (progbar != NULL) {
2081         g_snprintf(progbar_status_str, sizeof(progbar_status_str),
2082                    "%4u of %u packets", progbar_count, cf->count);
2083         update_progress_dlg(progbar, progbar_val, progbar_status_str);
2084       }
2085
2086       progbar_nextstep += progbar_quantum;
2087     }
2088
2089     if (progbar_stop_flag) {
2090       /* Well, the user decided to abort the operation.  Just stop,
2091          and arrange to return PSP_STOPPED to our caller, so they know
2092          it was stopped explicitly. */
2093       ret = PSP_STOPPED;
2094       break;
2095     }
2096
2097     progbar_count++;
2098
2099     /* do we have to process this packet? */
2100     process_this = packet_range_process_packet(range, fdata);
2101     if (process_this == range_process_next) {
2102         /* this packet uninteresting, continue with next one */
2103         continue;
2104     } else if (process_this == range_processing_finished) {
2105         /* all interesting packets processed, stop the loop */
2106         break;
2107     }
2108
2109     /* Get the packet */
2110     if (!cf_read_frame_r(cf, fdata, &pseudo_header, pd)) {
2111       /* Attempt to get the packet failed. */
2112       ret = PSP_FAILED;
2113       break;
2114     }
2115     /* Process the packet */
2116     if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
2117       /* Callback failed.  We assume it reported the error appropriately. */
2118       ret = PSP_FAILED;
2119       break;
2120     }
2121   }
2122
2123   /* We're done printing the packets; destroy the progress bar if
2124      it was created. */
2125   if (progbar != NULL)
2126     destroy_progress_dlg(progbar);
2127
2128   return ret;
2129 }
2130
2131 typedef struct {
2132   gboolean construct_protocol_tree;
2133   column_info *cinfo;
2134 } retap_callback_args_t;
2135
2136 static gboolean
2137 retap_packet(capture_file *cf _U_, frame_data *fdata,
2138              union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2139              void *argsp)
2140 {
2141   retap_callback_args_t *args = argsp;
2142   epan_dissect_t edt;
2143
2144   epan_dissect_init(&edt, args->construct_protocol_tree, FALSE);
2145   tap_queue_init(&edt);
2146   epan_dissect_run(&edt, pseudo_header, pd, fdata, args->cinfo);
2147   tap_push_tapped_queue(&edt);
2148   epan_dissect_cleanup(&edt);
2149
2150   return TRUE;
2151 }
2152
2153 cf_read_status_t
2154 cf_retap_packets(capture_file *cf)
2155 {
2156   packet_range_t range;
2157   retap_callback_args_t callback_args;
2158   gboolean filtering_tap_listeners;
2159   guint tap_flags;
2160
2161   /* Do we have any tap listeners with filters? */
2162   filtering_tap_listeners = have_filtering_tap_listeners();
2163
2164   tap_flags = union_of_tap_listener_flags();
2165
2166   /* If any tap listeners have filters, or require the protocol tree,
2167      construct the protocol tree. */
2168   callback_args.construct_protocol_tree = filtering_tap_listeners ||
2169                                           (tap_flags & TL_REQUIRES_PROTO_TREE);
2170
2171   /* If any tap listeners require the columns, construct them. */
2172   callback_args.cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
2173
2174   /* Reset the tap listeners. */
2175   reset_tap_listeners();
2176
2177   /* Iterate through the list of packets, dissecting all packets and
2178      re-running the taps. */
2179   packet_range_init(&range);
2180   packet_range_process_init(&range);
2181   switch (process_specified_packets(cf, &range, "Recalculating statistics on",
2182                                     "all packets", TRUE, retap_packet,
2183                                     &callback_args)) {
2184   case PSP_FINISHED:
2185     /* Completed successfully. */
2186     return CF_READ_OK;
2187
2188   case PSP_STOPPED:
2189     /* Well, the user decided to abort the refiltering.
2190        Return CF_READ_ABORTED so our caller knows they did that. */
2191     return CF_READ_ABORTED;
2192
2193   case PSP_FAILED:
2194     /* Error while retapping. */
2195     return CF_READ_ERROR;
2196   }
2197
2198   g_assert_not_reached();
2199   return CF_READ_OK;
2200 }
2201
2202 typedef struct {
2203   print_args_t *print_args;
2204   gboolean      print_header_line;
2205   char         *header_line_buf;
2206   int           header_line_buf_len;
2207   gboolean      print_formfeed;
2208   gboolean      print_separator;
2209   char         *line_buf;
2210   int           line_buf_len;
2211   gint         *col_widths;
2212 } print_callback_args_t;
2213
2214 static gboolean
2215 print_packet(capture_file *cf, frame_data *fdata,
2216              union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2217              void *argsp)
2218 {
2219   print_callback_args_t *args = argsp;
2220   epan_dissect_t edt;
2221   int             i;
2222   char           *cp;
2223   int             line_len;
2224   int             column_len;
2225   int             cp_off;
2226   gboolean        proto_tree_needed;
2227   char            bookmark_name[9+10+1];    /* "__frameNNNNNNNNNN__\0" */
2228   char            bookmark_title[6+10+1];   /* "Frame NNNNNNNNNN__\0" */
2229
2230   /* Create the protocol tree, and make it visible, if we're printing
2231      the dissection or the hex data.
2232      XXX - do we need it if we're just printing the hex data? */
2233   proto_tree_needed =
2234       args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex || have_custom_cols(&cf->cinfo);
2235   epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2236
2237   /* Fill in the column information if we're printing the summary
2238      information. */
2239   if (args->print_args->print_summary) {
2240     col_custom_prime_edt(&edt, &cf->cinfo);
2241     epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2242     epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2243   } else
2244     epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2245
2246   if (args->print_formfeed) {
2247     if (!new_page(args->print_args->stream))
2248       goto fail;
2249   } else {
2250       if (args->print_separator) {
2251         if (!print_line(args->print_args->stream, 0, ""))
2252           goto fail;
2253       }
2254   }
2255
2256   /*
2257    * We generate bookmarks, if the output format supports them.
2258    * The name is "__frameN__".
2259    */
2260   g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
2261
2262   if (args->print_args->print_summary) {
2263     if (args->print_header_line) {
2264       if (!print_line(args->print_args->stream, 0, args->header_line_buf))
2265         goto fail;
2266       args->print_header_line = FALSE;  /* we might not need to print any more */
2267     }
2268     cp = &args->line_buf[0];
2269     line_len = 0;
2270     for (i = 0; i < cf->cinfo.num_cols; i++) {
2271       /* Find the length of the string for this column. */
2272       column_len = (int) strlen(cf->cinfo.col_data[i]);
2273       if (args->col_widths[i] > column_len)
2274          column_len = args->col_widths[i];
2275
2276       /* Make sure there's room in the line buffer for the column; if not,
2277          double its length. */
2278       line_len += column_len + 1;   /* "+1" for space */
2279       if (line_len > args->line_buf_len) {
2280         cp_off = (int) (cp - args->line_buf);
2281         args->line_buf_len = 2 * line_len;
2282         args->line_buf = g_realloc(args->line_buf, args->line_buf_len + 1);
2283         cp = args->line_buf + cp_off;
2284       }
2285
2286       /* Right-justify the packet number column. */
2287       if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2288         g_snprintf(cp, column_len+1, "%*s", args->col_widths[i], cf->cinfo.col_data[i]);
2289       else
2290         g_snprintf(cp, column_len+1, "%-*s", args->col_widths[i], cf->cinfo.col_data[i]);
2291       cp += column_len;
2292       if (i != cf->cinfo.num_cols - 1)
2293         *cp++ = ' ';
2294     }
2295     *cp = '\0';
2296
2297     /*
2298      * Generate a bookmark, using the summary line as the title.
2299      */
2300     if (!print_bookmark(args->print_args->stream, bookmark_name,
2301                         args->line_buf))
2302       goto fail;
2303
2304     if (!print_line(args->print_args->stream, 0, args->line_buf))
2305       goto fail;
2306   } else {
2307     /*
2308      * Generate a bookmark, using "Frame N" as the title, as we're not
2309      * printing the summary line.
2310      */
2311     g_snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
2312     if (!print_bookmark(args->print_args->stream, bookmark_name,
2313                         bookmark_title))
2314       goto fail;
2315   } /* if (print_summary) */
2316
2317   if (args->print_args->print_dissections != print_dissections_none) {
2318     if (args->print_args->print_summary) {
2319       /* Separate the summary line from the tree with a blank line. */
2320       if (!print_line(args->print_args->stream, 0, ""))
2321         goto fail;
2322     }
2323
2324     /* Print the information in that tree. */
2325     if (!proto_tree_print(args->print_args, &edt, args->print_args->stream))
2326       goto fail;
2327
2328     /* Print a blank line if we print anything after this (aka more than one packet). */
2329     args->print_separator = TRUE;
2330
2331     /* Print a header line if we print any more packet summaries */
2332     args->print_header_line = TRUE;
2333   }
2334
2335   if (args->print_args->print_hex) {
2336     /* Print the full packet data as hex. */
2337     if (!print_hex_data(args->print_args->stream, &edt))
2338       goto fail;
2339
2340     /* Print a blank line if we print anything after this (aka more than one packet). */
2341     args->print_separator = TRUE;
2342
2343     /* Print a header line if we print any more packet summaries */
2344     args->print_header_line = TRUE;
2345   } /* if (args->print_args->print_dissections != print_dissections_none) */
2346
2347   epan_dissect_cleanup(&edt);
2348
2349   /* do we want to have a formfeed between each packet from now on? */
2350   if(args->print_args->print_formfeed) {
2351     args->print_formfeed = TRUE;
2352   }
2353
2354   return TRUE;
2355
2356 fail:
2357   epan_dissect_cleanup(&edt);
2358   return FALSE;
2359 }
2360
2361 cf_print_status_t
2362 cf_print_packets(capture_file *cf, print_args_t *print_args)
2363 {
2364   int         i;
2365   print_callback_args_t callback_args;
2366   gint        data_width;
2367   char        *cp;
2368   int         cp_off;
2369   int         column_len;
2370   int         line_len;
2371   psp_return_t ret;
2372
2373   callback_args.print_args = print_args;
2374   callback_args.print_header_line = TRUE;
2375   callback_args.header_line_buf = NULL;
2376   callback_args.header_line_buf_len = 256;
2377   callback_args.print_formfeed = FALSE;
2378   callback_args.print_separator = FALSE;
2379   callback_args.line_buf = NULL;
2380   callback_args.line_buf_len = 256;
2381   callback_args.col_widths = NULL;
2382
2383   if (!print_preamble(print_args->stream, cf->filename)) {
2384     destroy_print_stream(print_args->stream);
2385     return CF_PRINT_WRITE_ERROR;
2386   }
2387
2388   if (print_args->print_summary) {
2389     /* We're printing packet summaries.  Allocate the header line buffer
2390        and get the column widths. */
2391     callback_args.header_line_buf = g_malloc(callback_args.header_line_buf_len + 1);
2392
2393     /* Find the widths for each of the columns - maximum of the
2394        width of the title and the width of the data - and construct
2395        a buffer with a line containing the column titles. */
2396     callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * cf->cinfo.num_cols);
2397     cp = &callback_args.header_line_buf[0];
2398     line_len = 0;
2399     for (i = 0; i < cf->cinfo.num_cols; i++) {
2400       /* Don't pad the last column. */
2401       if (i == cf->cinfo.num_cols - 1)
2402         callback_args.col_widths[i] = 0;
2403       else {
2404         callback_args.col_widths[i] = (gint) strlen(cf->cinfo.col_title[i]);
2405         data_width = get_column_char_width(get_column_format(i));
2406         if (data_width > callback_args.col_widths[i])
2407           callback_args.col_widths[i] = data_width;
2408       }
2409
2410       /* Find the length of the string for this column. */
2411       column_len = (int) strlen(cf->cinfo.col_title[i]);
2412       if (callback_args.col_widths[i] > column_len)
2413         column_len = callback_args.col_widths[i];
2414
2415       /* Make sure there's room in the line buffer for the column; if not,
2416          double its length. */
2417       line_len += column_len + 1;   /* "+1" for space */
2418       if (line_len > callback_args.header_line_buf_len) {
2419         cp_off = (int) (cp - callback_args.header_line_buf);
2420         callback_args.header_line_buf_len = 2 * line_len;
2421         callback_args.header_line_buf = g_realloc(callback_args.header_line_buf,
2422                                                   callback_args.header_line_buf_len + 1);
2423         cp = callback_args.header_line_buf + cp_off;
2424       }
2425
2426       /* Right-justify the packet number column. */
2427 /*      if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2428         g_snprintf(cp, column_len+1, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2429       else*/
2430       g_snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2431       cp += column_len;
2432       if (i != cf->cinfo.num_cols - 1)
2433         *cp++ = ' ';
2434     }
2435     *cp = '\0';
2436
2437     /* Now start out the main line buffer with the same length as the
2438        header line buffer. */
2439     callback_args.line_buf_len = callback_args.header_line_buf_len;
2440     callback_args.line_buf = g_malloc(callback_args.line_buf_len + 1);
2441   } /* if (print_summary) */
2442
2443   /* Iterate through the list of packets, printing the packets we were
2444      told to print. */
2445   ret = process_specified_packets(cf, &print_args->range, "Printing",
2446                                   "selected packets", TRUE, print_packet,
2447                                   &callback_args);
2448
2449   g_free(callback_args.header_line_buf);
2450   g_free(callback_args.line_buf);
2451   g_free(callback_args.col_widths);
2452
2453   switch (ret) {
2454
2455   case PSP_FINISHED:
2456     /* Completed successfully. */
2457     break;
2458
2459   case PSP_STOPPED:
2460     /* Well, the user decided to abort the printing.
2461
2462        XXX - note that what got generated before they did that
2463        will get printed if we're piping to a print program; we'd
2464        have to write to a file and then hand that to the print
2465        program to make it actually not print anything. */
2466     break;
2467
2468   case PSP_FAILED:
2469     /* Error while printing.
2470
2471        XXX - note that what got generated before they did that
2472        will get printed if we're piping to a print program; we'd
2473        have to write to a file and then hand that to the print
2474        program to make it actually not print anything. */
2475     destroy_print_stream(print_args->stream);
2476     return CF_PRINT_WRITE_ERROR;
2477   }
2478
2479   if (!print_finale(print_args->stream)) {
2480     destroy_print_stream(print_args->stream);
2481     return CF_PRINT_WRITE_ERROR;
2482   }
2483
2484   if (!destroy_print_stream(print_args->stream))
2485     return CF_PRINT_WRITE_ERROR;
2486
2487   return CF_PRINT_OK;
2488 }
2489
2490 static gboolean
2491 write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
2492                   union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2493           void *argsp)
2494 {
2495   FILE *fh = argsp;
2496   epan_dissect_t edt;
2497
2498   /* Create the protocol tree, but don't fill in the column information. */
2499   epan_dissect_init(&edt, TRUE, TRUE);
2500   epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2501
2502   /* Write out the information in that tree. */
2503   proto_tree_write_pdml(&edt, fh);
2504
2505   epan_dissect_cleanup(&edt);
2506
2507   return !ferror(fh);
2508 }
2509
2510 cf_print_status_t
2511 cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
2512 {
2513   FILE        *fh;
2514   psp_return_t ret;
2515
2516   fh = ws_fopen(print_args->file, "w");
2517   if (fh == NULL)
2518     return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2519
2520   write_pdml_preamble(fh, cf->filename);
2521   if (ferror(fh)) {
2522     fclose(fh);
2523     return CF_PRINT_WRITE_ERROR;
2524   }
2525
2526   /* Iterate through the list of packets, printing the packets we were
2527      told to print. */
2528   ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
2529                                   "selected packets", TRUE,
2530                                   write_pdml_packet, fh);
2531
2532   switch (ret) {
2533
2534   case PSP_FINISHED:
2535     /* Completed successfully. */
2536     break;
2537
2538   case PSP_STOPPED:
2539     /* Well, the user decided to abort the printing. */
2540     break;
2541
2542   case PSP_FAILED:
2543     /* Error while printing. */
2544     fclose(fh);
2545     return CF_PRINT_WRITE_ERROR;
2546   }
2547
2548   write_pdml_finale(fh);
2549   if (ferror(fh)) {
2550     fclose(fh);
2551     return CF_PRINT_WRITE_ERROR;
2552   }
2553
2554   /* XXX - check for an error */
2555   fclose(fh);
2556
2557   return CF_PRINT_OK;
2558 }
2559
2560 static gboolean
2561 write_psml_packet(capture_file *cf, frame_data *fdata,
2562                   union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2563           void *argsp)
2564 {
2565   FILE *fh = argsp;
2566   epan_dissect_t edt;
2567   gboolean proto_tree_needed;
2568
2569   /* Fill in the column information, only create the protocol tree
2570      if having custom columns. */
2571   proto_tree_needed = have_custom_cols(&cf->cinfo);
2572   epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2573   col_custom_prime_edt(&edt, &cf->cinfo);
2574   epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2575   epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2576
2577   /* Write out the information in that tree. */
2578   proto_tree_write_psml(&edt, fh);
2579
2580   epan_dissect_cleanup(&edt);
2581
2582   return !ferror(fh);
2583 }
2584
2585 cf_print_status_t
2586 cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
2587 {
2588   FILE        *fh;
2589   psp_return_t ret;
2590
2591   fh = ws_fopen(print_args->file, "w");
2592   if (fh == NULL)
2593     return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2594
2595   write_psml_preamble(fh);
2596   if (ferror(fh)) {
2597     fclose(fh);
2598     return CF_PRINT_WRITE_ERROR;
2599   }
2600
2601   /* Iterate through the list of packets, printing the packets we were
2602      told to print. */
2603   ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
2604                                   "selected packets", TRUE,
2605                                   write_psml_packet, fh);
2606
2607   switch (ret) {
2608
2609   case PSP_FINISHED:
2610     /* Completed successfully. */
2611     break;
2612
2613   case PSP_STOPPED:
2614     /* Well, the user decided to abort the printing. */
2615     break;
2616
2617   case PSP_FAILED:
2618     /* Error while printing. */
2619     fclose(fh);
2620     return CF_PRINT_WRITE_ERROR;
2621   }
2622
2623   write_psml_finale(fh);
2624   if (ferror(fh)) {
2625     fclose(fh);
2626     return CF_PRINT_WRITE_ERROR;
2627   }
2628
2629   /* XXX - check for an error */
2630   fclose(fh);
2631
2632   return CF_PRINT_OK;
2633 }
2634
2635 static gboolean
2636 write_csv_packet(capture_file *cf, frame_data *fdata,
2637                  union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2638                  void *argsp)
2639 {
2640   FILE *fh = argsp;
2641   epan_dissect_t edt;
2642   gboolean proto_tree_needed;
2643
2644   /* Fill in the column information, only create the protocol tree
2645      if having custom columns. */
2646   proto_tree_needed = have_custom_cols(&cf->cinfo);
2647   epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2648   col_custom_prime_edt(&edt, &cf->cinfo);
2649   epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2650   epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2651
2652   /* Write out the information in that tree. */
2653   proto_tree_write_csv(&edt, fh);
2654
2655   epan_dissect_cleanup(&edt);
2656
2657   return !ferror(fh);
2658 }
2659
2660 cf_print_status_t
2661 cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
2662 {
2663   FILE        *fh;
2664   psp_return_t ret;
2665
2666   fh = ws_fopen(print_args->file, "w");
2667   if (fh == NULL)
2668     return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2669
2670   write_csv_preamble(fh);
2671   if (ferror(fh)) {
2672     fclose(fh);
2673     return CF_PRINT_WRITE_ERROR;
2674   }
2675
2676   /* Iterate through the list of packets, printing the packets we were
2677      told to print. */
2678   ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
2679                                   "selected packets", TRUE,
2680                                   write_csv_packet, fh);
2681
2682   switch (ret) {
2683
2684   case PSP_FINISHED:
2685     /* Completed successfully. */
2686     break;
2687
2688   case PSP_STOPPED:
2689     /* Well, the user decided to abort the printing. */
2690     break;
2691
2692   case PSP_FAILED:
2693     /* Error while printing. */
2694     fclose(fh);
2695     return CF_PRINT_WRITE_ERROR;
2696   }
2697
2698   write_csv_finale(fh);
2699   if (ferror(fh)) {
2700     fclose(fh);
2701     return CF_PRINT_WRITE_ERROR;
2702   }
2703
2704   /* XXX - check for an error */
2705   fclose(fh);
2706
2707   return CF_PRINT_OK;
2708 }
2709
2710 static gboolean
2711 write_carrays_packet(capture_file *cf _U_, frame_data *fdata,
2712              union wtap_pseudo_header *pseudo_header,
2713              const guint8 *pd, void *argsp)
2714 {
2715   FILE *fh = argsp;
2716   epan_dissect_t edt;
2717
2718   epan_dissect_init(&edt, TRUE, TRUE);
2719   epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2720   proto_tree_write_carrays(fdata->num, fh, &edt);
2721   epan_dissect_cleanup(&edt);
2722
2723   return !ferror(fh);
2724 }
2725
2726 cf_print_status_t
2727 cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
2728 {
2729   FILE        *fh;
2730   psp_return_t ret;
2731
2732   fh = ws_fopen(print_args->file, "w");
2733
2734   if (fh == NULL)
2735     return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2736
2737   write_carrays_preamble(fh);
2738
2739   if (ferror(fh)) {
2740     fclose(fh);
2741     return CF_PRINT_WRITE_ERROR;
2742   }
2743
2744   /* Iterate through the list of packets, printing the packets we were
2745      told to print. */
2746   ret = process_specified_packets(cf, &print_args->range,
2747                   "Writing C Arrays",
2748                   "selected packets", TRUE,
2749                                   write_carrays_packet, fh);
2750   switch (ret) {
2751   case PSP_FINISHED:
2752     /* Completed successfully. */
2753     break;
2754   case PSP_STOPPED:
2755     /* Well, the user decided to abort the printing. */
2756     break;
2757   case PSP_FAILED:
2758     /* Error while printing. */
2759     fclose(fh);
2760     return CF_PRINT_WRITE_ERROR;
2761   }
2762
2763   write_carrays_finale(fh);
2764
2765   if (ferror(fh)) {
2766     fclose(fh);
2767     return CF_PRINT_WRITE_ERROR;
2768   }
2769
2770   fclose(fh);
2771   return CF_PRINT_OK;
2772 }
2773
2774 gboolean
2775 cf_find_packet_protocol_tree(capture_file *cf, const char *string,
2776                              search_direction dir)
2777 {
2778   match_data        mdata;
2779
2780   mdata.string = string;
2781   mdata.string_len = strlen(string);
2782   return find_packet(cf, match_protocol_tree, &mdata, dir);
2783 }
2784
2785 gboolean
2786 cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree,  match_data *mdata)
2787 {
2788   mdata->frame_matched = FALSE;
2789   mdata->string = convert_string_case(cf->sfilter, cf->case_type);
2790   mdata->string_len = strlen(mdata->string);
2791   mdata->cf = cf;
2792   /* Iterate through all the nodes looking for matching text */
2793   proto_tree_children_foreach(tree, match_subtree_text, mdata);
2794   return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2795 }
2796
2797 static match_result
2798 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
2799 {
2800   match_data        *mdata = criterion;
2801   epan_dissect_t    edt;
2802
2803   /* Load the frame's data. */
2804   if (!cf_read_frame(cf, fdata)) {
2805     /* Attempt to get the packet failed. */
2806     return MR_ERROR;
2807   }
2808
2809   /* Construct the protocol tree, including the displayed text */
2810   epan_dissect_init(&edt, TRUE, TRUE);
2811   /* We don't need the column information */
2812   epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
2813
2814   /* Iterate through all the nodes, seeing if they have text that matches. */
2815   mdata->cf = cf;
2816   mdata->frame_matched = FALSE;
2817   proto_tree_children_foreach(edt.tree, match_subtree_text, mdata);
2818   epan_dissect_cleanup(&edt);
2819   return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2820 }
2821
2822 static void
2823 match_subtree_text(proto_node *node, gpointer data)
2824 {
2825   match_data    *mdata = (match_data*) data;
2826   const gchar   *string = mdata->string;
2827   size_t        string_len = mdata->string_len;
2828   capture_file  *cf = mdata->cf;
2829   field_info    *fi = PNODE_FINFO(node);
2830   gchar         label_str[ITEM_LABEL_LENGTH];
2831   gchar         *label_ptr;
2832   size_t        label_len;
2833   guint32       i;
2834   guint8        c_char;
2835   size_t        c_match = 0;
2836
2837   g_assert(fi && "dissection with an invisible proto tree?");
2838
2839   if (mdata->frame_matched) {
2840     /* We already had a match; don't bother doing any more work. */
2841     return;
2842   }
2843
2844   /* Don't match invisible entries. */
2845   if (PROTO_ITEM_IS_HIDDEN(node))
2846     return;
2847
2848   /* was a free format label produced? */
2849   if (fi->rep) {
2850     label_ptr = fi->rep->representation;
2851   } else {
2852     /* no, make a generic label */
2853     label_ptr = label_str;
2854     proto_item_fill_label(fi, label_str);
2855   }
2856
2857   /* Does that label match? */
2858   label_len = strlen(label_ptr);
2859   for (i = 0; i < label_len; i++) {
2860     c_char = label_ptr[i];
2861     if (cf->case_type)
2862       c_char = toupper(c_char);
2863     if (c_char == string[c_match]) {
2864       c_match++;
2865       if (c_match == string_len) {
2866         /* No need to look further; we have a match */
2867         mdata->frame_matched = TRUE;
2868         mdata->finfo = fi;
2869         return;
2870       }
2871     } else
2872       c_match = 0;
2873   }
2874
2875   /* Recurse into the subtree, if it exists */
2876   if (node->first_child != NULL)
2877     proto_tree_children_foreach(node, match_subtree_text, mdata);
2878 }
2879
2880 gboolean
2881 cf_find_packet_summary_line(capture_file *cf, const char *string,
2882                             search_direction dir)
2883 {
2884   match_data        mdata;
2885
2886   mdata.string = string;
2887   mdata.string_len = strlen(string);
2888   return find_packet(cf, match_summary_line, &mdata, dir);
2889 }
2890
2891 static match_result
2892 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
2893 {
2894   match_data        *mdata = criterion;
2895   const gchar       *string = mdata->string;
2896   size_t            string_len = mdata->string_len;
2897   epan_dissect_t    edt;
2898   const char        *info_column;
2899   size_t            info_column_len;
2900   match_result      result = MR_NOTMATCHED;
2901   gint              colx;
2902   guint32           i;
2903   guint8            c_char;
2904   size_t            c_match = 0;
2905
2906   /* Load the frame's data. */
2907   if (!cf_read_frame(cf, fdata)) {
2908     /* Attempt to get the packet failed. */
2909     return MR_ERROR;
2910   }
2911
2912   /* Don't bother constructing the protocol tree */
2913   epan_dissect_init(&edt, FALSE, FALSE);
2914   /* Get the column information */
2915   epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, &cf->cinfo);
2916
2917   /* Find the Info column */
2918   for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
2919     if (cf->cinfo.fmt_matx[colx][COL_INFO]) {
2920       /* Found it.  See if we match. */
2921       info_column = edt.pi.cinfo->col_data[colx];
2922       info_column_len = strlen(info_column);
2923       for (i = 0; i < info_column_len; i++) {
2924         c_char = info_column[i];
2925         if (cf->case_type)
2926           c_char = toupper(c_char);
2927         if (c_char == string[c_match]) {
2928           c_match++;
2929           if (c_match == string_len) {
2930             result = MR_MATCHED;
2931             break;
2932           }
2933         } else
2934           c_match = 0;
2935       }
2936       break;
2937     }
2938   }
2939   epan_dissect_cleanup(&edt);
2940   return result;
2941 }
2942
2943 typedef struct {
2944     const guint8 *data;
2945     size_t data_len;
2946 } cbs_t;    /* "Counted byte string" */
2947
2948 gboolean
2949 cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
2950                     search_direction dir)
2951 {
2952   cbs_t info;
2953
2954   info.data = string;
2955   info.data_len = string_size;
2956
2957   /* String or hex search? */
2958   if (cf->string) {
2959     /* String search - what type of string? */
2960     switch (cf->scs_type) {
2961
2962     case SCS_ASCII_AND_UNICODE:
2963       return find_packet(cf, match_ascii_and_unicode, &info, dir);
2964
2965     case SCS_ASCII:
2966       return find_packet(cf, match_ascii, &info, dir);
2967
2968     case SCS_UNICODE:
2969       return find_packet(cf, match_unicode, &info, dir);
2970
2971     default:
2972       g_assert_not_reached();
2973       return FALSE;
2974     }
2975   } else
2976     return find_packet(cf, match_binary, &info, dir);
2977 }
2978
2979 static match_result
2980 match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
2981 {
2982   cbs_t        *info = criterion;
2983   const guint8 *ascii_text = info->data;
2984   size_t       textlen = info->data_len;
2985   match_result result;
2986   guint32      buf_len;
2987   guint32      i;
2988   guint8       c_char;
2989   size_t       c_match = 0;
2990
2991   /* Load the frame's data. */
2992   if (!cf_read_frame(cf, fdata)) {
2993     /* Attempt to get the packet failed. */
2994     return MR_ERROR;
2995   }
2996
2997   result = MR_NOTMATCHED;
2998   buf_len = fdata->pkt_len;
2999   for (i = 0; i < buf_len; i++) {
3000     c_char = cf->pd[i];
3001     if (cf->case_type)
3002       c_char = toupper(c_char);
3003     if (c_char != 0) {
3004       if (c_char == ascii_text[c_match]) {
3005         c_match++;
3006         if (c_match == textlen) {
3007           result = MR_MATCHED;
3008           cf->search_pos = i; /* Save the position of the last character
3009                                  for highlighting the field. */
3010           break;
3011         }
3012       } else
3013         c_match = 0;
3014     }
3015   }
3016   return result;
3017 }
3018
3019 static match_result
3020 match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
3021 {
3022   cbs_t        *info = criterion;
3023   const guint8 *ascii_text = info->data;
3024   size_t       textlen = info->data_len;
3025   match_result result;
3026   guint32      buf_len;
3027   guint32      i;
3028   guint8       c_char;
3029   size_t       c_match = 0;
3030
3031   /* Load the frame's data. */
3032   if (!cf_read_frame(cf, fdata)) {
3033     /* Attempt to get the packet failed. */
3034     return MR_ERROR;
3035   }
3036
3037   result = MR_NOTMATCHED;
3038   buf_len = fdata->pkt_len;
3039   for (i = 0; i < buf_len; i++) {
3040     c_char = cf->pd[i];
3041     if (cf->case_type)
3042       c_char = toupper(c_char);
3043     if (c_char == ascii_text[c_match]) {
3044       c_match++;
3045       if (c_match == textlen) {
3046         result = MR_MATCHED;
3047         cf->search_pos = i; /* Save the position of the last character
3048                                for highlighting the field. */
3049         break;
3050       }
3051     } else
3052       c_match = 0;
3053   }
3054   return result;
3055 }
3056
3057 static match_result
3058 match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
3059 {
3060   cbs_t        *info = criterion;
3061   const guint8 *ascii_text = info->data;
3062   size_t       textlen = info->data_len;
3063   match_result result;
3064   guint32      buf_len;
3065   guint32      i;
3066   guint8       c_char;
3067   size_t       c_match = 0;
3068
3069   /* Load the frame's data. */
3070   if (!cf_read_frame(cf, fdata)) {
3071     /* Attempt to get the packet failed. */
3072     return MR_ERROR;
3073   }
3074
3075   result = MR_NOTMATCHED;
3076   buf_len = fdata->pkt_len;
3077   for (i = 0; i < buf_len; i++) {
3078     c_char = cf->pd[i];
3079     if (cf->case_type)
3080       c_char = toupper(c_char);
3081     if (c_char == ascii_text[c_match]) {
3082       c_match++;
3083       i++;
3084       if (c_match == textlen) {
3085         result = MR_MATCHED;
3086         cf->search_pos = i; /* Save the position of the last character
3087                                for highlighting the field. */
3088         break;
3089       }
3090     } else
3091       c_match = 0;
3092   }
3093   return result;
3094 }
3095
3096 static match_result
3097 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
3098 {
3099   cbs_t        *info = criterion;
3100   const guint8 *binary_data = info->data;
3101   size_t       datalen = info->data_len;
3102   match_result result;
3103   guint32      buf_len;
3104   guint32      i;
3105   size_t       c_match = 0;
3106
3107   /* Load the frame's data. */
3108   if (!cf_read_frame(cf, fdata)) {
3109     /* Attempt to get the packet failed. */
3110     return MR_ERROR;
3111   }
3112
3113   result = MR_NOTMATCHED;
3114   buf_len = fdata->pkt_len;
3115   for (i = 0; i < buf_len; i++) {
3116     if (cf->pd[i] == binary_data[c_match]) {
3117       c_match++;
3118       if (c_match == datalen) {
3119         result = MR_MATCHED;
3120         cf->search_pos = i; /* Save the position of the last character
3121                                for highlighting the field. */
3122         break;
3123       }
3124     } else
3125       c_match = 0;
3126   }
3127   return result;
3128 }
3129
3130 gboolean
3131 cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode,
3132                        search_direction dir)
3133 {
3134   return find_packet(cf, match_dfilter, sfcode, dir);
3135 }
3136
3137 gboolean
3138 cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
3139                               search_direction dir)
3140 {
3141   dfilter_t *sfcode;
3142   gboolean result;
3143
3144   if (!dfilter_compile(filter, &sfcode)) {
3145      /*
3146       * XXX - this shouldn't happen, as the filter string is machine
3147       * generated
3148       */
3149     return FALSE;
3150   }
3151   if (sfcode == NULL) {
3152     /*
3153      * XXX - this shouldn't happen, as the filter string is machine
3154      * generated.
3155      */
3156     return FALSE;
3157   }
3158   result = find_packet(cf, match_dfilter, sfcode, dir);
3159   dfilter_free(sfcode);
3160   return result;
3161 }
3162
3163 static match_result
3164 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
3165 {
3166   dfilter_t      *sfcode = criterion;
3167   epan_dissect_t edt;
3168   match_result   result;
3169
3170   /* Load the frame's data. */
3171   if (!cf_read_frame(cf, fdata)) {
3172     /* Attempt to get the packet failed. */
3173     return MR_ERROR;
3174   }
3175
3176   epan_dissect_init(&edt, TRUE, FALSE);
3177   epan_dissect_prime_dfilter(&edt, sfcode);
3178   epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
3179   result = dfilter_apply_edt(sfcode, &edt) ? MR_MATCHED : MR_NOTMATCHED;
3180   epan_dissect_cleanup(&edt);
3181   return result;
3182 }
3183
3184 gboolean
3185 cf_find_packet_marked(capture_file *cf, search_direction dir)
3186 {
3187   return find_packet(cf, match_marked, NULL, dir);
3188 }
3189
3190 static match_result
3191 match_marked(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3192 {
3193   return fdata->flags.marked ? MR_MATCHED : MR_NOTMATCHED;
3194 }
3195
3196 gboolean
3197 cf_find_packet_time_reference(capture_file *cf, search_direction dir)
3198 {
3199   return find_packet(cf, match_time_reference, NULL, dir);
3200 }
3201
3202 static match_result
3203 match_time_reference(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3204 {
3205   return fdata->flags.ref_time ? MR_MATCHED : MR_NOTMATCHED;
3206 }
3207
3208 static gboolean
3209 find_packet(capture_file *cf,
3210             match_result (*match_function)(capture_file *, frame_data *, void *),
3211             void *criterion, search_direction dir)
3212 {
3213   frame_data  *start_fd;
3214   guint32      framenum;
3215   frame_data  *fdata;
3216   frame_data  *new_fd = NULL;
3217   progdlg_t   *progbar = NULL;
3218   gboolean     stop_flag;
3219   int          count;
3220   gboolean     found;
3221   float        progbar_val;
3222   GTimeVal     start_time;
3223   gchar        status_str[100];
3224   int          progbar_nextstep;
3225   int          progbar_quantum;
3226   const char  *title;
3227   match_result result;
3228
3229   start_fd = cf->current_frame;
3230   if (start_fd != NULL)  {
3231     /* Iterate through the list of packets, starting at the packet we've
3232        picked, calling a routine to run the filter on the packet, see if
3233        it matches, and stop if so.  */
3234     count = 0;
3235     framenum = start_fd->num;
3236
3237     /* Update the progress bar when it gets to this value. */
3238     progbar_nextstep = 0;
3239     /* When we reach the value that triggers a progress bar update,
3240        bump that value by this amount. */
3241     progbar_quantum = cf->count/N_PROGBAR_UPDATES;
3242     /* Progress so far. */
3243     progbar_val = 0.0f;
3244
3245     stop_flag = FALSE;
3246     g_get_current_time(&start_time);
3247
3248     title = cf->sfilter?cf->sfilter:"";
3249     for (;;) {
3250       /* Create the progress bar if necessary.
3251          We check on every iteration of the loop, so that it takes no
3252          longer than the standard time to create it (otherwise, for a
3253          large file, we might take considerably longer than that standard
3254          time in order to get to the next progress bar step). */
3255       if (progbar == NULL)
3256          progbar = delayed_create_progress_dlg("Searching", title,
3257            FALSE, &stop_flag, &start_time, progbar_val);
3258
3259       /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
3260          when we update it, we have to run the GTK+ main loop to get it
3261          to repaint what's pending, and doing so may involve an "ioctl()"
3262          to see if there's any pending input from an X server, and doing
3263          that for every packet can be costly, especially on a big file. */
3264       if (count >= progbar_nextstep) {
3265         /* let's not divide by zero. I should never be started
3266          * with count == 0, so let's assert that
3267          */
3268         g_assert(cf->count > 0);
3269
3270         progbar_val = (gfloat) count / cf->count;
3271
3272         if (progbar != NULL) {
3273           g_snprintf(status_str, sizeof(status_str),
3274                      "%4u of %u packets", count, cf->count);
3275           update_progress_dlg(progbar, progbar_val, status_str);
3276         }
3277
3278         progbar_nextstep += progbar_quantum;
3279       }
3280
3281       if (stop_flag) {
3282         /* Well, the user decided to abort the search.  Go back to the
3283            frame where we started. */
3284         new_fd = start_fd;
3285         break;
3286       }
3287
3288       /* Go past the current frame. */
3289       if (dir == SD_BACKWARD) {
3290         /* Go on to the previous frame. */
3291         if (framenum == 1) {
3292           /*
3293            * XXX - other apps have a bit more of a detailed message
3294            * for this, and instead of offering "OK" and "Cancel",
3295            * they offer things such as "Continue" and "Cancel";
3296            * we need an API for popping up alert boxes with
3297            * {Verb} and "Cancel".
3298            */
3299
3300           if (prefs.gui_find_wrap)
3301           {
3302               statusbar_push_temporary_msg("Search reached the beginning. Continuing at end.");
3303               framenum = cf->count;     /* wrap around */
3304           }
3305           else
3306           {
3307               statusbar_push_temporary_msg("Search reached the beginning.");
3308               framenum = start_fd->num; /* stay on previous packet */
3309           }
3310         } else
3311           framenum--;
3312       } else {
3313         /* Go on to the next frame. */
3314         if (framenum == cf->count) {
3315           if (prefs.gui_find_wrap)
3316           {
3317               statusbar_push_temporary_msg("Search reached the end. Continuing at beginning.");
3318               framenum = 1;             /* wrap around */
3319           }
3320           else
3321           {
3322               statusbar_push_temporary_msg("Search reached the end.");
3323               framenum = start_fd->num; /* stay on previous packet */
3324           }
3325         } else
3326           framenum++;
3327       }
3328       fdata = frame_data_sequence_find(cf->frames, framenum);
3329
3330       count++;
3331
3332       /* Is this packet in the display? */
3333       if (fdata->flags.passed_dfilter) {
3334         /* Yes.  Does it match the search criterion? */
3335         result = (*match_function)(cf, fdata, criterion);
3336         if (result == MR_ERROR) {
3337           /* Error; our caller has reported the error.  Go back to the frame
3338              where we started. */
3339           new_fd = start_fd;
3340           break;
3341         } else if (result == MR_MATCHED) {
3342           /* Yes.  Go to the new frame. */
3343           new_fd = fdata;
3344           break;
3345         }
3346       }
3347
3348       if (fdata == start_fd) {
3349         /* We're back to the frame we were on originally, and that frame
3350            doesn't match the search filter.  The search failed. */
3351         break;
3352       }
3353     }
3354
3355     /* We're done scanning the packets; destroy the progress bar if it
3356        was created. */
3357     if (progbar != NULL)
3358       destroy_progress_dlg(progbar);
3359   }
3360
3361   if (new_fd != NULL) {
3362     /* Find and select */
3363     cf->search_in_progress = TRUE;
3364     found = new_packet_list_select_row_from_data(new_fd);
3365     cf->search_in_progress = FALSE;
3366     cf->search_pos = 0; /* Reset the position */
3367     if (!found) {
3368       /* We didn't find a row corresponding to this frame.
3369          This means that the frame isn't being displayed currently,
3370          so we can't select it. */
3371       simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
3372                     "%sEnd of capture exceeded!%s\n\n"
3373                     "The capture file is probably not fully dissected.",
3374                     simple_dialog_primary_start(), simple_dialog_primary_end());
3375       return FALSE;
3376     }
3377     return TRUE;    /* success */
3378   } else
3379     return FALSE;   /* failure */
3380 }
3381
3382 gboolean
3383 cf_goto_frame(capture_file *cf, guint fnumber)
3384 {
3385   frame_data *fdata;
3386
3387   fdata = frame_data_sequence_find(cf->frames, fnumber);
3388
3389   if (fdata == NULL) {
3390     /* we didn't find a packet with that packet number */
3391     statusbar_push_temporary_msg("There is no packet number %u.", fnumber);
3392     return FALSE;   /* we failed to go to that packet */
3393   }
3394   if (!fdata->flags.passed_dfilter) {
3395     /* that packet currently isn't displayed */
3396     /* XXX - add it to the set of displayed packets? */
3397     statusbar_push_temporary_msg("Packet number %u isn't displayed.", fnumber);
3398     return FALSE;   /* we failed to go to that packet */
3399   }
3400
3401   if (!new_packet_list_select_row_from_data(fdata)) {
3402     /* We didn't find a row corresponding to this frame.
3403        This means that the frame isn't being displayed currently,
3404        so we can't select it. */
3405     simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
3406                   "%sEnd of capture exceeded!%s\n\n"
3407                   "The capture file is probably not fully dissected.",
3408                   simple_dialog_primary_start(), simple_dialog_primary_end());
3409     return FALSE;
3410   }
3411   return TRUE;  /* we got to that packet */
3412 }
3413
3414 gboolean
3415 cf_goto_top_frame(void)
3416 {
3417   /* Find and select */
3418   new_packet_list_select_first_row();
3419   return TRUE;  /* we got to that packet */
3420 }
3421
3422 gboolean
3423 cf_goto_bottom_frame(void)
3424 {
3425   /* Find and select */
3426   new_packet_list_select_last_row();
3427   return TRUE;  /* we got to that packet */
3428 }
3429
3430 /*
3431  * Go to frame specified by currently selected protocol tree item.
3432  */
3433 gboolean
3434 cf_goto_framenum(capture_file *cf)
3435 {
3436   header_field_info       *hfinfo;
3437   guint32                 framenum;
3438
3439   if (cf->finfo_selected) {
3440     hfinfo = cf->finfo_selected->hfinfo;
3441     g_assert(hfinfo);
3442     if (hfinfo->type == FT_FRAMENUM) {
3443       framenum = fvalue_get_uinteger(&cf->finfo_selected->value);
3444       if (framenum != 0)
3445         return cf_goto_frame(cf, framenum);
3446       }
3447   }
3448
3449   return FALSE;
3450 }
3451
3452 /* Select the packet on a given row. */
3453 void
3454 cf_select_packet(capture_file *cf, int row)
3455 {
3456   frame_data *fdata;
3457
3458   /* Get the frame data struct pointer for this frame */
3459   fdata = new_packet_list_get_row_data(row);
3460
3461   if (fdata == NULL) {
3462     /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
3463        the first entry is added to it by "real_insert_row()", that row
3464        is selected (see "real_insert_row()", in "gtk/gtkclist.c", in both
3465        our version and the vanilla GTK+ version).
3466
3467        This means that a "select-row" signal is emitted; this causes
3468        "packet_list_select_cb()" to be called, which causes "cf_select_packet()"
3469        to be called.
3470
3471        "cf_select_packet()" fetches, above, the data associated with the
3472        row that was selected; however, as "gtk_clist_append()", which
3473        called "real_insert_row()", hasn't yet returned, we haven't yet
3474        associated any data with that row, so we get back a null pointer.
3475
3476        We can't assume that there's only one frame in the frame list,
3477        either, as we may be filtering the display.
3478
3479        We therefore assume that, if "row" is 0, i.e. the first row
3480        is being selected, and "cf->first_displayed" equals
3481        "cf->last_displayed", i.e. there's only one frame being
3482        displayed, that frame is the frame we want.
3483
3484        This means we have to set "cf->first_displayed" and
3485        "cf->last_displayed" before adding the row to the
3486        GtkCList; see the comment in "add_packet_to_packet_list()". */
3487
3488        if (row == 0 && cf->first_displayed == cf->last_displayed)
3489          fdata = frame_data_sequence_find(cf->frames, cf->first_displayed);
3490   }
3491
3492   /* If fdata _still_ isn't set simply give up. */
3493   if (fdata == NULL) {
3494     return;
3495   }
3496
3497   /* Get the data in that frame. */
3498   if (!cf_read_frame (cf, fdata)) {
3499     return;
3500   }
3501
3502   /* Record that this frame is the current frame. */
3503   cf->current_frame = fdata;
3504   cf->current_row = row;
3505
3506   /* Create the logical protocol tree. */
3507   if (cf->edt != NULL)
3508     epan_dissect_free(cf->edt);
3509
3510   /* We don't need the columns here. */
3511   cf->edt = epan_dissect_new(TRUE, TRUE);
3512
3513   tap_build_interesting(cf->edt);
3514   epan_dissect_run(cf->edt, &cf->pseudo_header, cf->pd, cf->current_frame,
3515           NULL);
3516
3517   dfilter_macro_build_ftv_cache(cf->edt->tree);
3518
3519   cf_callback_invoke(cf_cb_packet_selected, cf);
3520 }
3521
3522 /* Unselect the selected packet, if any. */
3523 void
3524 cf_unselect_packet(capture_file *cf)
3525 {
3526   /* Destroy the epan_dissect_t for the unselected packet. */
3527   if (cf->edt != NULL) {
3528     epan_dissect_free(cf->edt);
3529     cf->edt = NULL;
3530   }
3531
3532   /* No packet is selected. */
3533   cf->current_frame = NULL;
3534   cf->current_row = 0;
3535
3536   cf_callback_invoke(cf_cb_packet_unselected, cf);
3537
3538   /* No protocol tree means no selected field. */
3539   cf_unselect_field(cf);
3540 }
3541
3542 /* Unset the selected protocol tree field, if any. */
3543 void
3544 cf_unselect_field(capture_file *cf)
3545 {
3546   cf->finfo_selected = NULL;
3547
3548   cf_callback_invoke(cf_cb_field_unselected, cf);
3549 }
3550
3551 /*
3552  * Mark a particular frame.
3553  */
3554 void
3555 cf_mark_frame(capture_file *cf, frame_data *frame)
3556 {
3557   if (! frame->flags.marked) {
3558     frame->flags.marked = TRUE;
3559     if (cf->count > cf->marked_count)
3560       cf->marked_count++;
3561   }
3562 }
3563
3564 /*
3565  * Unmark a particular frame.
3566  */
3567 void
3568 cf_unmark_frame(capture_file *cf, frame_data *frame)
3569 {
3570   if (frame->flags.marked) {
3571     frame->flags.marked = FALSE;
3572     if (cf->marked_count > 0)
3573       cf->marked_count--;
3574   }
3575 }
3576
3577 /*
3578  * Ignore a particular frame.
3579  */
3580 void
3581 cf_ignore_frame(capture_file *cf, frame_data *frame)
3582 {
3583   if (! frame->flags.ignored) {
3584     frame->flags.ignored = TRUE;
3585     if (cf->count > cf->ignored_count)
3586       cf->ignored_count++;
3587   }
3588 }
3589
3590 /*
3591  * Un-ignore a particular frame.
3592  */
3593 void
3594 cf_unignore_frame(capture_file *cf, frame_data *frame)
3595 {
3596   if (frame->flags.ignored) {
3597     frame->flags.ignored = FALSE;
3598     if (cf->ignored_count > 0)
3599       cf->ignored_count--;
3600   }
3601 }
3602
3603 typedef struct {
3604   wtap_dumper *pdh;
3605   const char  *fname;
3606   int          file_type;
3607 } save_callback_args_t;
3608
3609 /*
3610  * Save a capture to a file, in a particular format, saving either
3611  * all packets, all currently-displayed packets, or all marked packets.
3612  *
3613  * Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
3614  * up a message box for the failure.
3615  */
3616 static gboolean
3617 save_packet(capture_file *cf _U_, frame_data *fdata,
3618             union wtap_pseudo_header *pseudo_header, const guint8 *pd,
3619             void *argsp)
3620 {
3621   save_callback_args_t *args = argsp;
3622   struct wtap_pkthdr hdr;
3623   int           err;
3624
3625   /* init the wtap header for saving */
3626   hdr.ts.secs    = fdata->abs_ts.secs;
3627   hdr.ts.nsecs   = fdata->abs_ts.nsecs;
3628   hdr.caplen     = fdata->cap_len;
3629   hdr.len        = fdata->pkt_len;
3630   hdr.pkt_encap  = fdata->lnk_t;
3631
3632   /* and save the packet */
3633   if (!wtap_dump(args->pdh, &hdr, pseudo_header, pd, &err)) {
3634     if (err < 0) {
3635       /* Wiretap error. */
3636       switch (err) {
3637
3638       case WTAP_ERR_UNSUPPORTED_ENCAP:
3639         /*
3640          * This is a problem with the particular frame we're writing;
3641          * note that, and give the frame number.
3642          */
3643         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3644                       "Frame %u has a network type that can't be saved in a \"%s\" file.",
3645                       fdata->num, wtap_file_type_string(args->file_type));
3646         break;
3647
3648       default:
3649         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3650                       "An error occurred while writing to the file \"%s\": %s.",
3651                       args->fname, wtap_strerror(err));
3652         break;
3653       }
3654     } else {
3655       /* OS error. */
3656       write_failure_alert_box(args->fname, err);
3657     }
3658     return FALSE;
3659   }
3660   return TRUE;
3661 }
3662
3663 /*
3664  * Can this capture file be saved in any format except by copying the raw data?
3665  */
3666 gboolean
3667 cf_can_save_as(capture_file *cf)
3668 {
3669   int ft;
3670
3671   for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) {
3672     /* To save a file with Wiretap, Wiretap has to handle that format,
3673        and its code to handle that format must be able to write a file
3674        with this file's encapsulation type. */
3675     if (wtap_dump_can_open(ft) && wtap_dump_can_write_encap(ft, cf->lnk_t)) {
3676       /* OK, we can write it out in this type. */
3677       return TRUE;
3678     }
3679   }
3680
3681   /* No, we couldn't save it in any format. */
3682   return FALSE;
3683 }
3684
3685 cf_status_t
3686 cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_format, gboolean compressed)
3687 {
3688   gchar        *from_filename;
3689   int           err;
3690   gboolean      do_copy;
3691   wtap_dumper  *pdh;
3692   save_callback_args_t callback_args;
3693
3694   cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
3695
3696   /* don't write over an existing file. */
3697   /* this should've been already checked by our caller, just to be sure... */
3698   if (file_exists(fname)) {
3699     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3700       "%sCapture file: \"%s\" already exists!%s\n\n"
3701       "Please choose a different filename.",
3702       simple_dialog_primary_start(), fname, simple_dialog_primary_end());
3703     goto fail;
3704   }
3705
3706   packet_range_process_init(range);
3707
3708   if (packet_range_process_all(range) && save_format == cf->cd_t) {
3709     /* We're not filtering packets, and we're saving it in the format
3710        it's already in, so we can just move or copy the raw data. */
3711
3712     if (cf->is_tempfile) {
3713       /* The file being saved is a temporary file from a live
3714          capture, so it doesn't need to stay around under that name;
3715          first, try renaming the capture buffer file to the new name. */
3716 #ifndef _WIN32
3717       if (ws_rename(cf->filename, fname) == 0) {
3718         /* That succeeded - there's no need to copy the source file. */
3719         from_filename = NULL;
3720     do_copy = FALSE;
3721       } else {
3722         if (errno == EXDEV) {
3723           /* They're on different file systems, so we have to copy the
3724              file. */
3725           do_copy = TRUE;
3726           from_filename = cf->filename;
3727         } else {
3728           /* The rename failed, but not because they're on different
3729              file systems - put up an error message.  (Or should we
3730              just punt and try to copy?  The only reason why I'd
3731              expect the rename to fail and the copy to succeed would
3732              be if we didn't have permission to remove the file from
3733              the temporary directory, and that might be fixable - but
3734              is it worth requiring the user to go off and fix it?) */
3735           simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3736                         file_rename_error_message(errno), fname);
3737           goto fail;
3738         }
3739       }
3740 #else
3741       do_copy = TRUE;
3742       from_filename = cf->filename;
3743 #endif
3744     } else {
3745       /* It's a permanent file, so we should copy it, and not remove the
3746          original. */
3747       do_copy = TRUE;
3748       from_filename = cf->filename;
3749     }
3750
3751     if (do_copy) {
3752       /* Copy the file, if we haven't moved it. */
3753       if (!copy_file_binary_mode(from_filename, fname))
3754         goto fail;
3755     }
3756   } else {
3757     /* Either we're filtering packets, or we're saving in a different
3758        format; we can't do that by copying or moving the capture file,
3759        we have to do it by writing the packets out in Wiretap. */
3760     pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap,
3761         compressed, &err);
3762     if (pdh == NULL) {
3763       cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
3764       goto fail;
3765     }
3766
3767     /* XXX - we let the user save a subset of the packets.
3768
3769        If we do that, should we make that file the current file?  If so,
3770        it means we can no longer get at the other packets.  What does
3771        NetMon do? */
3772
3773     /* Iterate through the list of packets, processing the packets we were
3774        told to process.
3775
3776        XXX - we've already called "packet_range_process_init(range)", but
3777        "process_specified_packets()" will do it again.  Fortunately,
3778        that's harmless in this case, as we haven't done anything to
3779        "range" since we initialized it. */
3780     callback_args.pdh = pdh;
3781     callback_args.fname = fname;
3782     callback_args.file_type = save_format;
3783     switch (process_specified_packets(cf, range, "Saving", "selected packets",
3784                                       TRUE, save_packet, &callback_args)) {
3785
3786     case PSP_FINISHED:
3787       /* Completed successfully. */
3788       break;
3789
3790     case PSP_STOPPED:
3791       /* The user decided to abort the saving.
3792          XXX - remove the output file? */
3793       break;
3794
3795     case PSP_FAILED:
3796       /* Error while saving. */
3797       wtap_dump_close(pdh, &err);
3798       goto fail;
3799     }
3800
3801     if (!wtap_dump_close(pdh, &err)) {
3802       cf_close_failure_alert_box(fname, err);
3803       goto fail;
3804     }
3805   }
3806
3807   cf_callback_invoke(cf_cb_file_save_finished, NULL);
3808
3809   if (packet_range_process_all(range)) {
3810     /* We saved the entire capture, not just some packets from it.
3811        Open and read the file we saved it to.
3812
3813        XXX - this is somewhat of a waste; we already have the
3814        packets, all this gets us is updated file type information
3815        (which we could just stuff into "cf"), and having the new
3816        file be the one we have opened and from which we're reading
3817        the data, and it means we have to spend time opening and
3818        reading the file, which could be a significant amount of
3819        time if the file is large. */
3820     cf->user_saved = TRUE;
3821
3822     if ((cf_open(cf, fname, FALSE, &err)) == CF_OK) {
3823       /* XXX - report errors if this fails?
3824          What should we return if it fails or is aborted? */
3825
3826       switch (cf_read(cf, TRUE)) {
3827
3828       case CF_READ_OK:
3829       case CF_READ_ERROR:
3830     /* Just because we got an error, that doesn't mean we were unable
3831        to read any of the file; we handle what we could get from the
3832        file. */
3833     break;
3834
3835       case CF_READ_ABORTED:
3836     /* The user bailed out of re-reading the capture file; the
3837        capture file has been closed - just return (without
3838        changing any menu settings; "cf_close()" set them
3839        correctly for the "no capture file open" state). */
3840     break;
3841       }
3842       cf_callback_invoke(cf_cb_file_save_reload_finished, cf);
3843     }
3844   }
3845   return CF_OK;
3846
3847 fail:
3848   cf_callback_invoke(cf_cb_file_save_failed, NULL);
3849   return CF_ERROR;
3850 }
3851
3852 static void
3853 cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
3854                           gboolean for_writing, int file_type)
3855 {
3856   if (err < 0) {
3857     /* Wiretap error. */
3858     switch (err) {
3859
3860     case WTAP_ERR_NOT_REGULAR_FILE:
3861       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3862             "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
3863             filename);
3864       break;
3865
3866     case WTAP_ERR_RANDOM_OPEN_PIPE:
3867       /* Seen only when opening a capture file for reading. */
3868       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3869             "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.",
3870             filename);
3871       break;
3872
3873     case WTAP_ERR_FILE_UNKNOWN_FORMAT:
3874       /* Seen only when opening a capture file for reading. */
3875       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3876             "The file \"%s\" isn't a capture file in a format Wireshark understands.",
3877             filename);
3878       break;
3879
3880     case WTAP_ERR_UNSUPPORTED:
3881       /* Seen only when opening a capture file for reading. */
3882       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3883             "The file \"%s\" isn't a capture file in a format Wireshark understands.\n"
3884             "(%s)",
3885             filename, err_info);
3886       g_free(err_info);
3887       break;
3888
3889     case WTAP_ERR_CANT_WRITE_TO_PIPE:
3890       /* Seen only when opening a capture file for writing. */
3891       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3892             "The file \"%s\" is a pipe, and %s capture files can't be "
3893             "written to a pipe.",
3894             filename, wtap_file_type_string(file_type));
3895       break;
3896
3897     case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
3898       /* Seen only when opening a capture file for writing. */
3899       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3900             "Wireshark doesn't support writing capture files in that format.");
3901       break;
3902
3903     case WTAP_ERR_UNSUPPORTED_ENCAP:
3904       if (for_writing) {
3905         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3906               "Wireshark can't save this capture in that format.");
3907       } else {
3908         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3909               "The file \"%s\" is a capture for a network type that Wireshark doesn't support.\n"
3910               "(%s)",
3911               filename, err_info);
3912         g_free(err_info);
3913       }
3914       break;
3915
3916     case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
3917       if (for_writing) {
3918         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3919               "Wireshark can't save this capture in that format.");
3920       } else {
3921         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3922               "The file \"%s\" is a capture for a network type that Wireshark doesn't support.",
3923               filename);
3924       }
3925       break;
3926
3927     case WTAP_ERR_BAD_FILE:
3928       /* Seen only when opening a capture file for reading. */
3929       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3930             "The file \"%s\" appears to be damaged or corrupt.\n"
3931             "(%s)",
3932             filename, err_info);
3933       g_free(err_info);
3934       break;
3935
3936     case WTAP_ERR_CANT_OPEN:
3937       if (for_writing) {
3938         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3939               "The file \"%s\" could not be created for some unknown reason.",
3940               filename);
3941       } else {
3942         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3943               "The file \"%s\" could not be opened for some unknown reason.",
3944               filename);
3945       }
3946       break;
3947
3948     case WTAP_ERR_SHORT_READ:
3949       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3950             "The file \"%s\" appears to have been cut short"
3951             " in the middle of a packet or other data.",
3952             filename);
3953       break;
3954
3955     case WTAP_ERR_SHORT_WRITE:
3956       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3957             "A full header couldn't be written to the file \"%s\".",
3958             filename);
3959       break;
3960
3961     case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
3962       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3963             "Gzip compression not supported by this file type.");
3964       break;
3965
3966     case WTAP_ERR_DECOMPRESS:
3967       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3968             "The compressed file \"%s\" appears to be damaged or corrupt.\n"
3969             "(%s)", filename, err_info);
3970       g_free(err_info);
3971       break;
3972
3973     default:
3974       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3975             "The file \"%s\" could not be %s: %s.",
3976             filename,
3977             for_writing ? "created" : "opened",
3978             wtap_strerror(err));
3979       break;
3980     }
3981   } else {
3982     /* OS error. */
3983     open_failure_alert_box(filename, err, for_writing);
3984   }
3985 }
3986
3987 static const char *
3988 file_rename_error_message(int err)
3989 {
3990   const char *errmsg;
3991   static char errmsg_errno[1024+1];
3992
3993   switch (err) {
3994
3995   case ENOENT:
3996     errmsg = "The path to the file \"%s\" doesn't exist.";
3997     break;
3998
3999   case EACCES:
4000     errmsg = "You don't have permission to move the capture file to \"%s\".";
4001     break;
4002
4003   default:
4004     g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4005             "The file \"%%s\" could not be moved: %s.",
4006                 wtap_strerror(err));
4007     errmsg = errmsg_errno;
4008     break;
4009   }
4010   return errmsg;
4011 }
4012
4013 /* Check for write errors - if the file is being written to an NFS server,
4014    a write error may not show up until the file is closed, as NFS clients
4015    might not send writes to the server until the "write()" call finishes,
4016    so that the write may fail on the server but the "write()" may succeed. */
4017 static void
4018 cf_close_failure_alert_box(const char *filename, int err)
4019 {
4020   if (err < 0) {
4021     /* Wiretap error. */
4022     switch (err) {
4023
4024     case WTAP_ERR_CANT_CLOSE:
4025       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4026             "The file \"%s\" couldn't be closed for some unknown reason.",
4027             filename);
4028       break;
4029
4030     case WTAP_ERR_SHORT_WRITE:
4031       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4032             "Not all the packets could be written to the file \"%s\".",
4033                     filename);
4034       break;
4035
4036     default:
4037       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4038             "An error occurred while closing the file \"%s\": %s.",
4039             filename, wtap_strerror(err));
4040       break;
4041     }
4042   } else {
4043     /* OS error.
4044        We assume that a close error from the OS is really a write error. */
4045     write_failure_alert_box(filename, err);
4046   }
4047 }
4048
4049 /* Reload the current capture file. */
4050 void
4051 cf_reload(capture_file *cf) {
4052   gchar *filename;
4053   gboolean is_tempfile;
4054   int err;
4055
4056   /* If the file could be opened, "cf_open()" calls "cf_close()"
4057      to get rid of state for the old capture file before filling in state
4058      for the new capture file.  "cf_close()" will remove the file if
4059      it's a temporary file; we don't want that to happen (for one thing,
4060      it'd prevent subsequent reopens from working).  Remember whether it's
4061      a temporary file, mark it as not being a temporary file, and then
4062      reopen it as the type of file it was.
4063
4064      Also, "cf_close()" will free "cf->filename", so we must make
4065      a copy of it first. */
4066   filename = g_strdup(cf->filename);
4067   is_tempfile = cf->is_tempfile;
4068   cf->is_tempfile = FALSE;
4069   if (cf_open(cf, filename, is_tempfile, &err) == CF_OK) {
4070     switch (cf_read(cf, FALSE)) {
4071
4072     case CF_READ_OK:
4073     case CF_READ_ERROR:
4074       /* Just because we got an error, that doesn't mean we were unable
4075          to read any of the file; we handle what we could get from the
4076          file. */
4077       break;
4078
4079     case CF_READ_ABORTED:
4080       /* The user bailed out of re-reading the capture file; the
4081          capture file has been closed - just free the capture file name
4082          string and return (without changing the last containing
4083          directory). */
4084       g_free(filename);
4085       return;
4086     }
4087   } else {
4088     /* The open failed, so "cf->is_tempfile" wasn't set to "is_tempfile".
4089        Instead, the file was left open, so we should restore "cf->is_tempfile"
4090        ourselves.
4091
4092        XXX - change the menu?  Presumably "cf_open()" will do that;
4093        make sure it does! */
4094     cf->is_tempfile = is_tempfile;
4095   }
4096   /* "cf_open()" made a copy of the file name we handed it, so
4097      we should free up our copy. */
4098   g_free(filename);
4099 }
4100
4101 /*
4102  * Editor modelines
4103  *
4104  * Local Variables:
4105  * c-basic-offset: 2
4106  * tab-width: 8
4107  * indent-tabs-mode: nil
4108  * End:
4109  *
4110  * ex: set shiftwidth=2 tabstop=8 expandtab:
4111  * :indentSize=2:tabSize=8:noTabs=true:
4112  */