6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
46 #include <epan/epan.h>
47 #include <epan/filesystem.h>
50 #include "color_filters.h"
52 #include <epan/column.h>
53 #include <epan/packet.h>
54 #include <epan/column-utils.h>
55 #include "packet-range.h"
61 #include "alert_box.h"
62 #include "simple_dialog.h"
63 #include "progress_dlg.h"
65 #include <epan/prefs.h>
66 #include <epan/dfilter/dfilter.h>
67 #include <epan/epan_dissect.h>
69 #include <epan/dissectors/packet-data.h>
70 #include <epan/dissectors/packet-ber.h>
71 #include <epan/timestamp.h>
72 #include <epan/dfilter/dfilter-macro.h>
73 #include <wsutil/file_util.h>
74 #include <epan/strutil.h>
77 gboolean auto_scroll_live;
80 static guint32 cum_bytes;
81 static nstime_t first_ts;
82 static nstime_t prev_dis_ts;
83 static nstime_t prev_cap_ts;
85 static gulong computed_elapsed;
87 static void cf_reset_state(capture_file *cf);
89 static int read_packet(capture_file *cf, dfilter_t *dfcode,
90 gboolean filtering_tap_listeners, guint tap_flags, gint64 offset);
92 static void rescan_packets(capture_file *cf, const char *action, const char *action_item,
93 gboolean refilter, gboolean redissect);
95 static gboolean match_protocol_tree(capture_file *cf, frame_data *fdata,
97 static void match_subtree_text(proto_node *node, gpointer data);
98 static gboolean match_summary_line(capture_file *cf, frame_data *fdata,
100 static gboolean match_ascii_and_unicode(capture_file *cf, frame_data *fdata,
102 static gboolean match_ascii(capture_file *cf, frame_data *fdata,
104 static gboolean match_unicode(capture_file *cf, frame_data *fdata,
106 static gboolean match_binary(capture_file *cf, frame_data *fdata,
108 static gboolean match_dfilter(capture_file *cf, frame_data *fdata,
110 static gboolean find_packet(capture_file *cf,
111 gboolean (*match_function)(capture_file *, frame_data *, void *),
114 static void cf_open_failure_alert_box(const char *filename, int err,
115 gchar *err_info, gboolean for_writing,
117 static const char *file_rename_error_message(int err);
118 static void cf_write_failure_alert_box(const char *filename, int err);
119 static void cf_close_failure_alert_box(const char *filename, int err);
120 #ifdef NEW_PACKET_LIST
121 static void ref_time_packets(capture_file *cf);
123 /* Update the progress bar this many times when reading a file. */
124 #define N_PROGBAR_UPDATES 100
125 /* We read around 200k/100ms don't update the progress bar more often than that */
126 #define MIN_QUANTUM 200000
127 #define MIN_NUMBER_OF_PACKET 1500
129 /* Number of "frame_data" structures per memory chunk.
130 XXX - is this the right number? */
131 #define FRAME_DATA_CHUNK_SIZE 1024
134 /* this callback mechanism should possibly be replaced by the g_signal_...() stuff (if I only would know how :-) */
136 cf_callback_t cb_fct;
138 } cf_callback_data_t;
140 static GList *cf_callbacks = NULL;
143 cf_callback_invoke(int event, gpointer data)
145 cf_callback_data_t *cb;
146 GList *cb_item = cf_callbacks;
148 /* there should be at least one interested */
149 g_assert(cb_item != NULL);
151 while(cb_item != NULL) {
153 cb->cb_fct(event, data, cb->user_data);
154 cb_item = g_list_next(cb_item);
160 cf_callback_add(cf_callback_t func, gpointer user_data)
162 cf_callback_data_t *cb;
164 cb = g_malloc(sizeof(cf_callback_data_t));
166 cb->user_data = user_data;
168 cf_callbacks = g_list_append(cf_callbacks, cb);
172 cf_callback_remove(cf_callback_t func)
174 cf_callback_data_t *cb;
175 GList *cb_item = cf_callbacks;
177 while(cb_item != NULL) {
179 if(cb->cb_fct == func) {
180 cf_callbacks = g_list_remove(cf_callbacks, cb);
184 cb_item = g_list_next(cb_item);
187 g_assert_not_reached();
191 cf_timestamp_auto_precision(capture_file *cf)
193 #ifdef NEW_PACKET_LIST
196 int prec = timestamp_get_precision();
199 /* don't try to get the file's precision if none is opened */
200 if(cf->state == FILE_CLOSED) {
204 /* if we are in auto mode, set precision of current file */
205 if(prec == TS_PREC_AUTO ||
206 prec == TS_PREC_AUTO_SEC ||
207 prec == TS_PREC_AUTO_DSEC ||
208 prec == TS_PREC_AUTO_CSEC ||
209 prec == TS_PREC_AUTO_MSEC ||
210 prec == TS_PREC_AUTO_USEC ||
211 prec == TS_PREC_AUTO_NSEC)
213 switch(wtap_file_tsprecision(cf->wth)) {
214 case(WTAP_FILE_TSPREC_SEC):
215 timestamp_set_precision(TS_PREC_AUTO_SEC);
217 case(WTAP_FILE_TSPREC_DSEC):
218 timestamp_set_precision(TS_PREC_AUTO_DSEC);
220 case(WTAP_FILE_TSPREC_CSEC):
221 timestamp_set_precision(TS_PREC_AUTO_CSEC);
223 case(WTAP_FILE_TSPREC_MSEC):
224 timestamp_set_precision(TS_PREC_AUTO_MSEC);
226 case(WTAP_FILE_TSPREC_USEC):
227 timestamp_set_precision(TS_PREC_AUTO_USEC);
229 case(WTAP_FILE_TSPREC_NSEC):
230 timestamp_set_precision(TS_PREC_AUTO_NSEC);
233 g_assert_not_reached();
236 #ifdef NEW_PACKET_LIST
237 /* Set the column widths of those columns that show the time in
238 "command-line-specified" format. */
239 for (i = 0; i < cf->cinfo.num_cols; i++) {
240 if (col_has_time_fmt(&cf->cinfo, i)) {
241 new_packet_list_resize_column(i);
248 cf_get_computed_elapsed(void)
250 return computed_elapsed;
253 static void reset_elapsed(void)
255 computed_elapsed = 0;
258 static void compute_elapsed(GTimeVal *start_time)
263 g_get_current_time(&time_now);
265 delta_time = (time_now.tv_sec - start_time->tv_sec) * 1e6 +
266 time_now.tv_usec - start_time->tv_usec;
268 computed_elapsed = (gulong) (delta_time / 1000); /* ms*/
272 cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
277 wth = wtap_open_offline(fname, err, &err_info, TRUE);
281 /* The open succeeded. Close whatever capture file we had open,
282 and fill in the information for this file. */
285 /* Cleanup all data structures used for dissection. */
286 cleanup_dissection();
287 /* Initialize all data structures used for dissection. */
290 /* We're about to start reading the file. */
291 cf->state = FILE_READ_IN_PROGRESS;
296 /* Set the file name because we need it to set the follow stream filter.
297 XXX - is that still true? We need it for other reasons, though,
299 cf->filename = g_strdup(fname);
301 /* Indicate whether it's a permanent or temporary file. */
302 cf->is_tempfile = is_tempfile;
304 /* If it's a temporary capture buffer file, mark it as not saved. */
305 cf->user_saved = !is_tempfile;
309 cf->cd_t = wtap_file_type(cf->wth);
311 cf->displayed_count = 0;
312 cf->marked_count = 0;
313 cf->ignored_count = 0;
314 cf->drops_known = FALSE;
316 cf->snap = wtap_snapshot_length(cf->wth);
318 /* Snapshot length not known. */
319 cf->has_snap = FALSE;
320 cf->snap = WTAP_MAX_PACKET_SIZE;
324 nstime_set_zero(&cf->elapsed_time);
325 nstime_set_unset(&first_ts);
326 nstime_set_unset(&prev_dis_ts);
327 nstime_set_unset(&prev_cap_ts);
330 #if GLIB_CHECK_VERSION(2,10,0)
332 /* memory chunks have been deprecated in favor of the slice allocator,
333 * which has been added in 2.10
335 cf->plist_chunk = g_mem_chunk_new("frame_data_chunk",
337 FRAME_DATA_CHUNK_SIZE * sizeof(frame_data),
339 g_assert(cf->plist_chunk);
342 #ifdef NEW_PACKET_LIST
343 /* Adjust timestamp precision if auto is selected, col width will be adjusted */
344 cf_timestamp_auto_precision(cf);
346 new_packet_list_queue_draw();
348 /* change the time formats now, as we might have a new precision */
349 cf_change_time_formats(cf);
351 fileset_file_opened(fname);
353 if(cf->cd_t == WTAP_FILE_BER) {
354 /* tell the BER dissector the file name */
355 ber_set_filename(cf->filename);
361 cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
367 * Reset the state for the currently closed file, but don't do the
368 * UI callbacks; this is for use in "cf_open()", where we don't
369 * want the UI to go from "file open" to "file closed" back to
370 * "file open", we want it to go from "old file open" to "new file
371 * open and being read".
374 cf_reset_state(capture_file *cf)
376 /* Die if we're in the middle of reading a file. */
377 g_assert(cf->state != FILE_READ_IN_PROGRESS);
383 /* We have no file open... */
384 if (cf->filename != NULL) {
385 /* If it's a temporary file, remove it. */
387 ws_unlink(cf->filename);
388 g_free(cf->filename);
391 /* ...which means we have nothing to save. */
392 cf->user_saved = FALSE;
394 #if GLIB_CHECK_VERSION(2,10,0)
395 if (cf->plist_start != NULL)
396 g_slice_free_chain(frame_data, cf->plist_start, next);
398 /* memory chunks have been deprecated in favor of the slice allocator,
399 * which has been added in 2.10
401 if (cf->plist_chunk != NULL) {
402 g_mem_chunk_destroy(cf->plist_chunk);
403 cf->plist_chunk = NULL;
406 dfilter_free(cf->rfcode);
408 cf->plist_start = NULL;
409 cf->plist_end = NULL;
410 cf_unselect_packet(cf); /* nothing to select */
411 cf->first_displayed = NULL;
412 cf->last_displayed = NULL;
414 /* No frame selected, no field in that frame selected. */
415 cf->current_frame = NULL;
417 cf->finfo_selected = NULL;
419 /* Clear the packet list. */
420 #ifdef NEW_PACKET_LIST
421 new_packet_list_freeze();
422 new_packet_list_clear();
423 new_packet_list_thaw();
425 packet_list_freeze();
432 nstime_set_zero(&cf->elapsed_time);
434 reset_tap_listeners();
436 /* We have no file open. */
437 cf->state = FILE_CLOSED;
439 fileset_file_closed();
442 /* Reset everything to a pristine state */
444 cf_close(capture_file *cf)
446 /* do GUI things even if file is already closed,
447 * e.g. to cleanup things if a capture couldn't be started */
448 cf_callback_invoke(cf_cb_file_closing, cf);
450 /* close things, if not already closed before */
451 if(cf->state != FILE_CLOSED) {
452 color_filters_cleanup();
454 cleanup_dissection();
457 cf_callback_invoke(cf_cb_file_closed, cf);
460 /* an out of memory exception occured, wait for a user button press to exit */
461 static void outofmemory_cb(gpointer dialog _U_, gint btn _U_, gpointer data _U_)
466 static float calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos){
470 progbar_val = (gfloat) file_pos / (gfloat) size;
471 if (progbar_val > 1.0) {
472 /* The file probably grew while we were reading it.
473 Update file size, and try again. */
474 size = wtap_file_size(cf->wth, NULL);
476 progbar_val = (gfloat) file_pos / (gfloat) size;
477 /* If it's still > 1, either "wtap_file_size()" failed (in which
478 case there's not much we can do about it), or the file
479 *shrank* (in which case there's not much we can do about
480 it); just clip the progress value at 1.0. */
481 if (progbar_val > 1.0f)
488 cf_read(capture_file *cf, gboolean from_save)
492 const gchar *name_ptr;
494 char errmsg_errno[1024+1];
496 progdlg_t *volatile progbar = NULL;
498 volatile gint64 size;
499 volatile float progbar_val;
501 gchar status_str[100];
502 volatile gint64 progbar_nextstep;
503 volatile gint64 progbar_quantum;
505 gboolean filtering_tap_listeners;
507 volatile int count = 0;
509 volatile int displayed_once = 0;
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.
517 compiled = dfilter_compile(cf->dfilter, &dfcode);
518 g_assert(!cf->dfilter || (compiled && dfcode));
520 /* Do we have any tap listeners with filters? */
521 filtering_tap_listeners = have_filtering_tap_listeners();
523 /* Get the union of the flags for all tap listeners. */
524 tap_flags = union_of_tap_listener_flags();
526 reset_tap_listeners();
528 name_ptr = get_basename(cf->filename);
530 if (from_save == FALSE)
531 cf_callback_invoke(cf_cb_file_read_started, cf);
533 cf_callback_invoke(cf_cb_file_save_started, (gpointer)name_ptr);
535 /* Find the size of the file. */
536 size = wtap_file_size(cf->wth, NULL);
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. */
543 progbar_quantum = size/N_PROGBAR_UPDATES;
544 if (progbar_quantum < MIN_QUANTUM)
545 progbar_quantum = MIN_QUANTUM;
548 /* Progress so far. */
551 #ifdef NEW_PACKET_LIST
552 new_packet_list_freeze();
554 packet_list_freeze();
558 g_get_current_time(&start_time);
560 while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
563 /* Create the progress bar if necessary.
564 * Check wether it should be created or not every MIN_NUMBER_OF_PACKET
566 if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)){
567 progbar_val = calc_progbar_val( cf, size, data_offset);
568 if (from_save == FALSE)
569 progbar = delayed_create_progress_dlg("Loading", name_ptr,
570 TRUE, &stop_flag, &start_time, progbar_val);
572 progbar = delayed_create_progress_dlg("Saving", name_ptr,
573 TRUE, &stop_flag, &start_time, progbar_val);
576 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
577 when we update it, we have to run the GTK+ main loop to get it
578 to repaint what's pending, and doing so may involve an "ioctl()"
579 to see if there's any pending input from an X server, and doing
580 that for every packet can be costly, especially on a big file. */
581 if (data_offset >= progbar_nextstep) {
582 if (progbar != NULL) {
583 progbar_val = calc_progbar_val( cf, size, data_offset);
584 /* update the packet lists content on the first run or frequently on very large files */
585 /* (on smaller files the display update takes longer than reading the file) */
587 if (progbar_quantum > 500000 || displayed_once == 0) {
588 if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->plist_end != NULL) {
590 #ifdef NEW_PACKET_LIST
591 new_packet_list_thaw();
592 if (auto_scroll_live)
593 new_packet_list_moveto_end();
594 new_packet_list_freeze();
597 if (auto_scroll_live)
598 packet_list_moveto_end();
599 packet_list_freeze();
600 #endif /* NEW_PACKET_LIST */
603 #endif /* HAVE_LIBPCAP */
604 g_snprintf(status_str, sizeof(status_str),
605 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
606 data_offset / 1024, size / 1024);
607 update_progress_dlg(progbar, progbar_val, status_str);
609 progbar_nextstep += progbar_quantum;
614 /* Well, the user decided to abort the read. He/She will be warned and
615 it might be enough for him/her to work with the already loaded
617 This is especially true for very large capture files, where you don't
618 want to wait loading the whole file (which may last minutes or even
619 hours even on fast machines) just to see that it was the wrong file. */
623 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
625 CATCH(OutOfMemoryError) {
628 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
629 "%sOut Of Memory!%s\n"
631 "Sorry, but Wireshark has to terminate now!\n"
633 "Some infos / workarounds can be found at:\n"
634 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
635 simple_dialog_primary_start(), simple_dialog_primary_end());
636 /* we have to terminate, as we cannot recover from the memory error */
637 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
639 main_window_update();
640 /* XXX - how to avoid a busy wait? */
648 /* Cleanup and release all dfilter resources */
650 dfilter_free(dfcode);
653 /* We're done reading the file; destroy the progress bar if it was created. */
655 destroy_progress_dlg(progbar);
657 /* We're done reading sequentially through the file. */
658 cf->state = FILE_READ_DONE;
660 /* Close the sequential I/O side, to free up memory it requires. */
661 wtap_sequential_close(cf->wth);
663 /* Allow the protocol dissectors to free up memory that they
664 * don't need after the sequential run-through of the packets. */
665 postseq_cleanup_all_protocols();
667 /* compute the time it took to load the file */
668 compute_elapsed(&start_time);
670 /* Set the file encapsulation type now; we don't know what it is until
671 we've looked at all the packets, as we don't know until then whether
672 there's more than one type (and thus whether it's
673 WTAP_ENCAP_PER_PACKET). */
674 cf->lnk_t = wtap_file_encap(cf->wth);
676 cf->current_frame = cf->first_displayed;
679 #ifdef NEW_PACKET_LIST
680 new_packet_list_thaw();
684 if (from_save == FALSE)
685 cf_callback_invoke(cf_cb_file_read_finished, cf);
687 cf_callback_invoke(cf_cb_file_save_finished, cf);
689 /* If we have any displayed packets to select, select the first of those
690 packets by making the first row the selected row. */
691 if (cf->first_displayed != NULL){
692 #ifdef NEW_PACKET_LIST
693 new_packet_list_select_first_row();
695 packet_list_select_row(0);
696 #endif /* NEW_PACKET_LIST */
700 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
701 "%sFile loading was cancelled!%s\n"
703 "The remaining packets in the file were discarded.\n"
705 "As a lot of packets from the original file will be missing,\n"
706 "remember to be careful when saving the current content to a file.\n",
707 simple_dialog_primary_start(), simple_dialog_primary_end());
708 return CF_READ_ERROR;
712 /* Put up a message box noting that the read failed somewhere along
713 the line. Don't throw out the stuff we managed to read, though,
717 case WTAP_ERR_UNSUPPORTED_ENCAP:
718 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
719 "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
722 errmsg = errmsg_errno;
725 case WTAP_ERR_CANT_READ:
726 errmsg = "An attempt to read from the capture file failed for"
727 " some unknown reason.";
730 case WTAP_ERR_SHORT_READ:
731 errmsg = "The capture file appears to have been cut short"
732 " in the middle of a packet.";
735 case WTAP_ERR_BAD_RECORD:
736 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
737 "The capture file appears to be damaged or corrupt.\n(%s)",
740 errmsg = errmsg_errno;
744 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
745 "An error occurred while reading the"
746 " capture file: %s.", wtap_strerror(err));
747 errmsg = errmsg_errno;
750 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", errmsg);
751 return CF_READ_ERROR;
758 cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
760 cf_status_t cf_status;
762 cf_status = cf_open(cf, fname, is_tempfile, err);
767 cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
769 gint64 data_offset = 0;
771 volatile int newly_displayed_packets = 0;
773 gboolean filtering_tap_listeners;
777 /* Compile the current display filter.
778 * We assume this will not fail since cf->dfilter is only set in
779 * cf_filter IFF the filter was valid.
781 compiled = dfilter_compile(cf->dfilter, &dfcode);
782 g_assert(!cf->dfilter || (compiled && dfcode));
784 /* Do we have any tap listeners with filters? */
785 filtering_tap_listeners = have_filtering_tap_listeners();
787 /* Get the union of the flags for all tap listeners. */
788 tap_flags = union_of_tap_listener_flags();
792 #ifdef NEW_PACKET_LIST
793 new_packet_list_check_end();
794 /* Don't freeze/thaw the list when doing live capture */
795 /*new_packet_list_freeze();*/
797 packet_list_check_end();
798 packet_list_freeze();
801 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
803 wtap_cleareof(cf->wth);
804 while (to_read != 0 && (wtap_read(cf->wth, err, &err_info, &data_offset))) {
805 if (cf->state == FILE_READ_ABORTED) {
806 /* Well, the user decided to exit Wireshark. Break out of the
807 loop, and let the code below (which is called even if there
808 aren't any packets left to read) exit. */
812 if (read_packet(cf, dfcode, filtering_tap_listeners, tap_flags,
813 data_offset) != -1) {
814 newly_displayed_packets++;
817 CATCH(OutOfMemoryError) {
820 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
821 "%sOut Of Memory!%s\n"
823 "Sorry, but Wireshark has to terminate now!\n"
825 "The capture file is not lost, it can be found at:\n"
828 "Some infos / workarounds can be found at:\n"
829 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
830 simple_dialog_primary_start(), simple_dialog_primary_end(), cf->filename);
831 /* we have to terminate, as we cannot recover from the memory error */
832 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
834 main_window_update();
835 /* XXX - how to avoid a busy wait? */
838 #ifdef NEW_PACKET_LIST
839 /* Don't freeze/thaw the list when doing live capture */
840 /*new_packet_list_thaw();*/
844 return CF_READ_ABORTED;
850 /* Cleanup and release all dfilter resources */
852 dfilter_free(dfcode);
855 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u",
856 cf->count, cf->state, *err);*/
858 #ifdef NEW_PACKET_LIST
859 /* Don't freeze/thaw the list when doing live capture */
860 /*new_packet_list_thaw();*/
861 /* With the new packet list the first packet
862 * isn't automatically selected.
864 if(!cf->current_frame)
865 new_packet_list_select_first_row();
867 /* XXX - this causes "flickering" of the list */
871 /* moving to the end of the packet list - if the user requested so and
872 we have some new packets. */
873 if (newly_displayed_packets && auto_scroll_live && cf->plist_end != NULL)
874 #ifdef NEW_PACKET_LIST
875 new_packet_list_moveto_end();
877 /* this doesn't seem to work well with a frozen GTK_Clist, so do this after
878 packet_list_thaw() is done, see bugzilla 1188 */
879 /* XXX - this cheats and looks inside the packet list to find the final
881 packet_list_moveto_end();
882 #endif /* NEW_PACKET_LIST */
884 if (cf->state == FILE_READ_ABORTED) {
885 /* Well, the user decided to exit Wireshark. Return CF_READ_ABORTED
886 so that our caller can kill off the capture child process;
887 this will cause an EOF on the pipe from the child, so
888 "cf_finish_tail()" will be called, and it will clean up
890 return CF_READ_ABORTED;
891 } else if (*err != 0) {
892 /* We got an error reading the capture file.
893 XXX - pop up a dialog box instead? */
894 g_warning("Error \"%s\" while reading: \"%s\"\n",
895 wtap_strerror(*err), cf->filename);
897 return CF_READ_ERROR;
903 cf_fake_continue_tail(capture_file *cf) {
904 cf->state = FILE_READ_DONE;
908 cf_finish_tail(capture_file *cf, int *err)
913 gboolean filtering_tap_listeners;
917 /* Compile the current display filter.
918 * We assume this will not fail since cf->dfilter is only set in
919 * cf_filter IFF the filter was valid.
921 compiled = dfilter_compile(cf->dfilter, &dfcode);
922 g_assert(!cf->dfilter || (compiled && dfcode));
924 /* Do we have any tap listeners with filters? */
925 filtering_tap_listeners = have_filtering_tap_listeners();
927 /* Get the union of the flags for all tap listeners. */
928 tap_flags = union_of_tap_listener_flags();
930 if(cf->wth == NULL) {
932 return CF_READ_ERROR;
935 #ifdef NEW_PACKET_LIST
936 new_packet_list_check_end();
937 /* Don't freeze/thaw the list when doing live capture */
938 /*new_packet_list_freeze();*/
940 packet_list_check_end();
941 packet_list_freeze();
944 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
945 if (cf->state == FILE_READ_ABORTED) {
946 /* Well, the user decided to abort the read. Break out of the
947 loop, and let the code below (which is called even if there
948 aren't any packets left to read) exit. */
951 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
954 /* Cleanup and release all dfilter resources */
956 dfilter_free(dfcode);
959 #ifdef NEW_PACKET_LIST
960 /* Don't freeze/thaw the list when doing live capture */
961 /*new_packet_list_thaw();*/
966 if (cf->state == FILE_READ_ABORTED) {
967 /* Well, the user decided to abort the read. We're only called
968 when the child capture process closes the pipe to us (meaning
969 it's probably exited), so we can just close the capture
970 file; we return CF_READ_ABORTED so our caller can do whatever
971 is appropriate when that happens. */
973 return CF_READ_ABORTED;
976 if (auto_scroll_live && cf->plist_end != NULL)
977 #ifdef NEW_PACKET_LIST
978 new_packet_list_moveto_end();
980 /* XXX - this cheats and looks inside the packet list to find the final
982 packet_list_moveto_end();
985 /* We're done reading sequentially through the file. */
986 cf->state = FILE_READ_DONE;
988 /* We're done reading sequentially through the file; close the
989 sequential I/O side, to free up memory it requires. */
990 wtap_sequential_close(cf->wth);
992 /* Allow the protocol dissectors to free up memory that they
993 * don't need after the sequential run-through of the packets. */
994 postseq_cleanup_all_protocols();
996 /* Set the file encapsulation type now; we don't know what it is until
997 we've looked at all the packets, as we don't know until then whether
998 there's more than one type (and thus whether it's
999 WTAP_ENCAP_PER_PACKET). */
1000 cf->lnk_t = wtap_file_encap(cf->wth);
1003 /* We got an error reading the capture file.
1004 XXX - pop up a dialog box? */
1005 return CF_READ_ERROR;
1010 #endif /* HAVE_LIBPCAP */
1013 cf_get_display_name(capture_file *cf)
1015 const gchar *displayname;
1017 /* Return a name to use in displays */
1018 if (!cf->is_tempfile) {
1019 /* Get the last component of the file name, and use that. */
1021 displayname = get_basename(cf->filename);
1023 displayname="(No file)";
1026 /* The file we read is a temporary file from a live capture;
1027 we don't mention its name. */
1029 displayname = cf->source;
1031 displayname = "(Untitled)";
1037 void cf_set_tempfile_source(capture_file *cf, gchar *source) {
1043 cf->source = g_strdup(source);
1045 cf->source = g_strdup("");
1049 const gchar *cf_get_tempfile_source(capture_file *cf) {
1057 /* XXX - use a macro instead? */
1059 cf_get_packet_count(capture_file *cf)
1064 /* XXX - use a macro instead? */
1066 cf_set_packet_count(capture_file *cf, int packet_count)
1068 cf->count = packet_count;
1071 /* XXX - use a macro instead? */
1073 cf_is_tempfile(capture_file *cf)
1075 return cf->is_tempfile;
1078 void cf_set_tempfile(capture_file *cf, gboolean is_tempfile)
1080 cf->is_tempfile = is_tempfile;
1084 /* XXX - use a macro instead? */
1085 void cf_set_drops_known(capture_file *cf, gboolean drops_known)
1087 cf->drops_known = drops_known;
1090 /* XXX - use a macro instead? */
1091 void cf_set_drops(capture_file *cf, guint32 drops)
1096 /* XXX - use a macro instead? */
1097 gboolean cf_get_drops_known(capture_file *cf)
1099 return cf->drops_known;
1102 /* XXX - use a macro instead? */
1103 guint32 cf_get_drops(capture_file *cf)
1108 void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
1110 cf->rfcode = rfcode;
1113 #ifdef NEW_PACKET_LIST
1115 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
1116 dfilter_t *dfcode, gboolean filtering_tap_listeners,
1118 union wtap_pseudo_header *pseudo_header, const guchar *buf,
1120 gboolean add_to_packet_list)
1122 gboolean create_proto_tree = FALSE;
1127 cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
1129 frame_data_set_before_dissect(fdata, &cf->elapsed_time,
1130 &first_ts, &prev_dis_ts, &prev_cap_ts);
1133 + we have a display filter and are re-applying it;
1134 + we have tap listeners with filters;
1135 + we have tap listeners that require a protocol tree;
1137 allocate a protocol tree root node, so that we'll construct
1138 a protocol tree against which a filter expression can be
1140 if ((dfcode != NULL && refilter) ||
1141 filtering_tap_listeners || (tap_flags & TL_REQUIRES_PROTO_TREE))
1142 create_proto_tree = TRUE;
1144 /* Dissect the frame. */
1145 epan_dissect_init(&edt, create_proto_tree, FALSE);
1147 if (dfcode != NULL && refilter) {
1148 epan_dissect_prime_dfilter(&edt, dfcode);
1151 tap_queue_init(&edt);
1152 epan_dissect_run(&edt, pseudo_header, buf, fdata, cinfo);
1153 tap_push_tapped_queue(&edt);
1155 /* If we have a display filter, apply it if we're refiltering, otherwise
1156 leave the "passed_dfilter" flag alone.
1158 If we don't have a display filter, set "passed_dfilter" to 1. */
1159 if (dfcode != NULL) {
1161 fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, &edt) ? 1 : 0;
1164 fdata->flags.passed_dfilter = 1;
1166 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1167 cf->displayed_count++;
1169 if (add_to_packet_list) {
1170 /* We fill the needed columns from new_packet_list */
1171 row = new_packet_list_append(cinfo, fdata, &edt.pi);
1174 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1176 frame_data_set_after_dissect(fdata, &cum_bytes, &prev_dis_ts);
1178 /* If we haven't yet seen the first frame, this is it.
1180 XXX - we must do this before we add the row to the display,
1181 as, if the display's GtkCList's selection mode is
1182 GTK_SELECTION_BROWSE, when the first entry is added to it,
1183 "cf_select_packet()" will be called, and it will fetch the row
1184 data for the 0th row, and will get a null pointer rather than
1185 "fdata", as "gtk_clist_append()" won't yet have returned and
1186 thus "gtk_clist_set_row_data()" won't yet have been called.
1188 We thus need to leave behind bread crumbs so that
1189 "cf_select_packet()" can find this frame. See the comment
1190 in "cf_select_packet()". */
1191 if (cf->first_displayed == NULL)
1192 cf->first_displayed = fdata;
1194 /* This is the last frame we've seen so far. */
1195 cf->last_displayed = fdata;
1198 epan_dissect_cleanup(&edt);
1205 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
1206 dfilter_t *dfcode, gboolean filtering_tap_listeners,
1208 union wtap_pseudo_header *pseudo_header, const guchar *buf,
1210 gboolean add_to_packet_list _U_)
1212 gboolean create_proto_tree = FALSE;
1219 /* just add some value here until we know if it is being displayed or not */
1220 fdata->cum_bytes = cum_bytes + fdata->pkt_len;
1222 frame_data_set_before_dissect(fdata, &cf->elapsed_time,
1223 &first_ts, &prev_dis_ts, &prev_cap_ts);
1227 we have a display filter and are re-applying it;
1229 we have a list of color filters;
1231 we have tap listeners with filters;
1233 we have tap listeners that require a protocol tree;
1235 we have custom columns;
1237 allocate a protocol tree root node, so that we'll construct
1238 a protocol tree against which a filter expression can be
1240 if ((dfcode != NULL && refilter) ||
1241 color_filters_used() ||
1242 have_custom_cols(cinfo) ||
1243 filtering_tap_listeners || (tap_flags & TL_REQUIRES_PROTO_TREE))
1244 create_proto_tree = TRUE;
1246 /* Dissect the frame. */
1247 epan_dissect_init(&edt, create_proto_tree, FALSE);
1249 if (dfcode != NULL && refilter) {
1250 epan_dissect_prime_dfilter(&edt, dfcode);
1253 /* prepare color filters */
1254 color_filters_prime_edt(&edt);
1255 col_custom_prime_edt(&edt, cinfo);
1257 tap_queue_init(&edt);
1258 epan_dissect_run(&edt, pseudo_header, buf, fdata, cinfo);
1259 tap_push_tapped_queue(&edt);
1261 /* If we have a display filter, apply it if we're refiltering, otherwise
1262 leave the "passed_dfilter" flag alone.
1264 If we don't have a display filter, set "passed_dfilter" to 1. */
1265 if (dfcode != NULL) {
1267 fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, &edt) ? 1 : 0;
1270 fdata->flags.passed_dfilter = 1;
1272 if( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) )
1274 frame_data_set_after_dissect(fdata, &cum_bytes, &prev_dis_ts);
1276 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
1278 /* If we haven't yet seen the first frame, this is it.
1280 XXX - we must do this before we add the row to the display,
1281 as, if the display's GtkCList's selection mode is
1282 GTK_SELECTION_BROWSE, when the first entry is added to it,
1283 "cf_select_packet()" will be called, and it will fetch the row
1284 data for the 0th row, and will get a null pointer rather than
1285 "fdata", as "gtk_clist_append()" won't yet have returned and
1286 thus "gtk_clist_set_row_data()" won't yet have been called.
1288 We thus need to leave behind bread crumbs so that
1289 "cf_select_packet()" can find this frame. See the comment
1290 in "cf_select_packet()". */
1291 if (cf->first_displayed == NULL)
1292 cf->first_displayed = fdata;
1294 /* This is the last frame we've seen so far. */
1295 cf->last_displayed = fdata;
1297 row = packet_list_append(cinfo->col_data, fdata);
1299 /* colorize packet: first apply color filters
1300 * then if packet is marked, use preferences to overwrite color
1301 * we do both to make sure that when a packet gets un-marked, the
1302 * color will be correctly set (fixes bug 2038)
1304 fdata->color_filter = color_filters_colorize_packet(row, &edt);
1305 if (fdata->flags.marked) {
1306 packet_list_set_colors(row, &prefs.gui_marked_fg, &prefs.gui_marked_bg);
1308 if (fdata->flags.ignored) {
1309 packet_list_set_colors(row, &prefs.gui_ignored_fg, &prefs.gui_ignored_bg);
1312 cf->displayed_count++;
1315 epan_dissect_cleanup(&edt);
1320 /* read in a new packet */
1321 /* returns the row of the new packet in the packet list or -1 if not displayed */
1323 read_packet(capture_file *cf, dfilter_t *dfcode,
1324 gboolean filtering_tap_listeners, guint tap_flags, gint64 offset)
1326 const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
1327 union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
1328 const guchar *buf = wtap_buf_ptr(cf->wth);
1335 /* Allocate the next list entry, and add it to the list.
1336 * memory chunks have been deprecated in favor of the slice allocator,
1337 * which has been added in 2.10
1339 #if GLIB_CHECK_VERSION(2,10,0)
1340 fdata = g_slice_new(frame_data);
1342 fdata = g_mem_chunk_alloc(cf->plist_chunk);
1345 frame_data_init(fdata, cf->count, phdr, offset, cum_bytes);
1347 #ifdef NEW_PACKET_LIST
1348 fdata->col_text_len = se_alloc0(sizeof(fdata->col_text_len) * (cf->cinfo.num_cols));
1349 fdata->col_text = se_alloc0(sizeof(fdata->col_text) * (cf->cinfo.num_cols));
1355 epan_dissect_init(&edt, TRUE, FALSE);
1356 epan_dissect_prime_dfilter(&edt, cf->rfcode);
1357 epan_dissect_run(&edt, pseudo_header, buf, fdata, NULL);
1358 passed = dfilter_apply_edt(cf->rfcode, &edt);
1359 epan_dissect_cleanup(&edt);
1363 cap_file_add_fdata(cf, fdata);
1365 cf->f_datalen = offset + fdata->cap_len;
1367 if (!cf->redissecting) {
1368 row = add_packet_to_packet_list(fdata, cf, dfcode,
1369 filtering_tap_listeners, tap_flags,
1370 pseudo_header, buf, TRUE, TRUE);
1373 /* We didn't pass read filter so roll back count */
1376 /* XXX - if we didn't have read filters, or if we could avoid
1377 allocating the "frame_data" structure until we knew whether
1378 the frame passed the read filter, we could use a G_ALLOC_ONLY
1381 ...but, at least in one test I did, where I just made the chunk
1382 a G_ALLOC_ONLY chunk and read in a huge capture file, it didn't
1383 seem to save a noticeable amount of time or space. */
1384 #if GLIB_CHECK_VERSION(2,10,0)
1385 /* memory chunks have been deprecated in favor of the slice allocator,
1386 * which has been added in 2.10
1388 g_slice_free(frame_data,fdata);
1390 g_mem_chunk_free(cf->plist_chunk, fdata);
1398 cf_merge_files(char **out_filenamep, int in_file_count,
1399 char *const *in_filenames, int file_type, gboolean do_append)
1401 merge_in_file_t *in_files;
1407 int open_err, read_err, write_err, close_err;
1411 char errmsg_errno[1024+1];
1413 gboolean got_read_error = FALSE, got_write_error = FALSE;
1415 progdlg_t *progbar = NULL;
1417 gint64 f_len, file_pos;
1419 GTimeVal start_time;
1420 gchar status_str[100];
1421 gint64 progbar_nextstep;
1422 gint64 progbar_quantum;
1424 /* open the input files */
1425 if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
1426 &open_err, &err_info, &err_fileno)) {
1428 cf_open_failure_alert_box(in_filenames[err_fileno], open_err, err_info,
1433 if (*out_filenamep != NULL) {
1434 out_filename = *out_filenamep;
1435 out_fd = ws_open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
1439 out_fd = create_tempfile(&tmpname, "wireshark");
1442 out_filename = g_strdup(tmpname);
1443 *out_filenamep = out_filename;
1447 merge_close_in_files(in_file_count, in_files);
1449 cf_open_failure_alert_box(out_filename, open_err, NULL, TRUE, file_type);
1453 pdh = wtap_dump_fdopen(out_fd, file_type,
1454 merge_select_frame_type(in_file_count, in_files),
1455 merge_max_snapshot_length(in_file_count, in_files),
1456 FALSE /* compressed */, &open_err);
1459 merge_close_in_files(in_file_count, in_files);
1461 cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
1466 /* Get the sum of the sizes of all the files. */
1468 for (i = 0; i < in_file_count; i++)
1469 f_len += in_files[i].size;
1471 /* Update the progress bar when it gets to this value. */
1472 progbar_nextstep = 0;
1473 /* When we reach the value that triggers a progress bar update,
1474 bump that value by this amount. */
1475 progbar_quantum = f_len/N_PROGBAR_UPDATES;
1476 /* Progress so far. */
1480 g_get_current_time(&start_time);
1482 /* do the merge (or append) */
1485 wth = merge_append_read_packet(in_file_count, in_files, &read_err,
1488 wth = merge_read_packet(in_file_count, in_files, &read_err,
1492 got_read_error = TRUE;
1496 /* Get the sum of the data offsets in all of the files. */
1498 for (i = 0; i < in_file_count; i++)
1499 data_offset += in_files[i].data_offset;
1501 /* Create the progress bar if necessary.
1502 We check on every iteration of the loop, so that it takes no
1503 longer than the standard time to create it (otherwise, for a
1504 large file, we might take considerably longer than that standard
1505 time in order to get to the next progress bar step). */
1506 if (progbar == NULL) {
1507 progbar = delayed_create_progress_dlg("Merging", "files",
1508 FALSE, &stop_flag, &start_time, progbar_val);
1511 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1512 when we update it, we have to run the GTK+ main loop to get it
1513 to repaint what's pending, and doing so may involve an "ioctl()"
1514 to see if there's any pending input from an X server, and doing
1515 that for every packet can be costly, especially on a big file. */
1516 if (data_offset >= progbar_nextstep) {
1517 /* Get the sum of the seek positions in all of the files. */
1519 for (i = 0; i < in_file_count; i++)
1520 file_pos += wtap_read_so_far(in_files[i].wth, NULL);
1521 progbar_val = (gfloat) file_pos / (gfloat) f_len;
1522 if (progbar_val > 1.0f) {
1523 /* Some file probably grew while we were reading it.
1524 That "shouldn't happen", so we'll just clip the progress
1528 if (progbar != NULL) {
1529 g_snprintf(status_str, sizeof(status_str),
1530 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
1531 file_pos / 1024, f_len / 1024);
1532 update_progress_dlg(progbar, progbar_val, status_str);
1534 progbar_nextstep += progbar_quantum;
1538 /* Well, the user decided to abort the merge. */
1542 if (!wtap_dump(pdh, wtap_phdr(wth), wtap_pseudoheader(wth),
1543 wtap_buf_ptr(wth), &write_err)) {
1544 got_write_error = TRUE;
1549 /* We're done merging the files; destroy the progress bar if it was created. */
1550 if (progbar != NULL)
1551 destroy_progress_dlg(progbar);
1553 merge_close_in_files(in_file_count, in_files);
1554 if (!got_read_error && !got_write_error) {
1555 if (!wtap_dump_close(pdh, &write_err))
1556 got_write_error = TRUE;
1558 wtap_dump_close(pdh, &close_err);
1560 if (got_read_error) {
1562 * Find the file on which we got the error, and report the error.
1564 for (i = 0; i < in_file_count; i++) {
1565 if (in_files[i].state == GOT_ERROR) {
1566 /* Put up a message box noting that a read failed somewhere along
1570 case WTAP_ERR_UNSUPPORTED_ENCAP:
1571 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1572 "The capture file %%s has a packet with a network type that Wireshark doesn't support.\n(%s)",
1575 errmsg = errmsg_errno;
1578 case WTAP_ERR_CANT_READ:
1579 errmsg = "An attempt to read from the capture file %s failed for"
1580 " some unknown reason.";
1583 case WTAP_ERR_SHORT_READ:
1584 errmsg = "The capture file %s appears to have been cut short"
1585 " in the middle of a packet.";
1588 case WTAP_ERR_BAD_RECORD:
1589 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1590 "The capture file %%s appears to be damaged or corrupt.\n(%s)",
1593 errmsg = errmsg_errno;
1597 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1598 "An error occurred while reading the"
1599 " capture file %%s: %s.", wtap_strerror(read_err));
1600 errmsg = errmsg_errno;
1603 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg, in_files[i].filename);
1608 if (got_write_error) {
1609 /* Put up an alert box for the write error. */
1610 cf_write_failure_alert_box(out_filename, write_err);
1613 if (got_read_error || got_write_error || stop_flag) {
1614 /* Callers aren't expected to treat an error or an explicit abort
1615 differently - we put up error dialogs ourselves, so they don't
1623 cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
1625 const char *filter_new = dftext ? dftext : "";
1626 const char *filter_old = cf->dfilter ? cf->dfilter : "";
1628 GTimeVal start_time;
1630 /* if new filter equals old one, do nothing unless told to do so */
1631 if (!force && strcmp(filter_new, filter_old) == 0) {
1637 if (dftext == NULL) {
1638 /* The new filter is an empty filter (i.e., display all packets).
1639 * so leave dfcode==NULL
1643 * We have a filter; make a copy of it (as we'll be saving it),
1644 * and try to compile it.
1646 dftext = g_strdup(dftext);
1647 if (!dfilter_compile(dftext, &dfcode)) {
1648 /* The attempt failed; report an error. */
1649 gchar *safe_dftext = simple_dialog_format_message(dftext);
1650 gchar *safe_dfilter_error_msg = simple_dialog_format_message(
1652 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1655 "The following display filter isn't a valid display filter:\n%s\n"
1656 "See the help for a description of the display filter syntax.",
1657 simple_dialog_primary_start(), safe_dfilter_error_msg,
1658 simple_dialog_primary_end(), safe_dftext);
1659 g_free(safe_dfilter_error_msg);
1660 g_free(safe_dftext);
1666 if (dfcode == NULL) {
1667 /* Yes - free the filter text, and set it to null. */
1673 /* We have a valid filter. Replace the current filter. */
1674 g_free(cf->dfilter);
1675 cf->dfilter = dftext;
1676 g_get_current_time(&start_time);
1679 /* Now rescan the packet list, applying the new filter, but not
1680 throwing away information constructed on a previous pass. */
1681 if (dftext == NULL) {
1682 rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
1684 rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
1687 /* Cleanup and release all dfilter resources */
1688 dfilter_free(dfcode);
1694 cf_colorize_packets(capture_file *cf)
1696 rescan_packets(cf, "Colorizing", "all packets", FALSE, FALSE);
1700 cf_reftime_packets(capture_file *cf)
1703 #ifdef NEW_PACKET_LIST
1704 ref_time_packets(cf);
1706 rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
1711 cf_redissect_packets(capture_file *cf)
1713 rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
1717 cf_read_frame_r(capture_file *cf, frame_data *fdata,
1718 union wtap_pseudo_header *pseudo_header, guint8 *pd,
1719 int *err, gchar **err_info)
1721 return wtap_seek_read(cf->wth, fdata->file_off, pseudo_header,
1722 pd, fdata->cap_len, err, err_info);
1726 cf_read_frame(capture_file *cf, frame_data *fdata, int *err, gchar **err_info)
1728 return cf_read_frame_r(cf, fdata, &cf->pseudo_header, cf->pd, err, err_info);
1731 /* Rescan the list of packets, reconstructing the CList.
1733 "action" describes why we're doing this; it's used in the progress
1736 "action_item" describes what we're doing; it's used in the progress
1739 "refilter" is TRUE if we need to re-evaluate the filter expression.
1741 "redissect" is TRUE if we need to make the dissectors reconstruct
1742 any state information they have (because a preference that affects
1743 some dissector has changed, meaning some dissector might construct
1744 its state differently from the way it was constructed the last time). */
1745 #ifdef NEW_PACKET_LIST
1747 rescan_packets(capture_file *cf, const char *action, const char *action_item,
1748 gboolean refilter, gboolean redissect)
1750 /* Rescan packets new packet list */
1752 progdlg_t *progbar = NULL;
1757 frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
1758 int selected_frame_num, preceding_frame_num, following_frame_num, prev_frame_num;
1759 gboolean selected_frame_seen;
1762 GTimeVal start_time;
1763 gchar status_str[100];
1764 int progbar_nextstep;
1765 int progbar_quantum;
1767 gboolean filtering_tap_listeners;
1769 gboolean add_to_packet_list = FALSE;
1772 /* Compile the current display filter.
1773 * We assume this will not fail since cf->dfilter is only set in
1774 * cf_filter IFF the filter was valid.
1776 compiled = dfilter_compile(cf->dfilter, &dfcode);
1777 g_assert(!cf->dfilter || (compiled && dfcode));
1779 /* Do we have any tap listeners with filters? */
1780 filtering_tap_listeners = have_filtering_tap_listeners();
1782 /* Get the union of the flags for all tap listeners. */
1783 tap_flags = union_of_tap_listener_flags();
1785 reset_tap_listeners();
1786 /* Which frame, if any, is the currently selected frame?
1787 XXX - should the selected frame or the focus frame be the "current"
1788 frame, that frame being the one from which "Find Frame" searches
1790 selected_frame = cf->current_frame;
1792 /* Mark frane num as not found */
1793 selected_frame_num = -1;
1795 /* Freeze the packet list while we redo it, so we don't get any
1796 screen updates while it happens. */
1797 new_packet_list_freeze();
1800 /* We need to re-initialize all the state information that protocols
1801 keep, because some preference that controls a dissector has changed,
1802 which might cause the state information to be constructed differently
1803 by that dissector. */
1805 /* We might receive new packets while redissecting, and we don't
1806 want to dissect those before their time. */
1807 cf->redissecting = TRUE;
1809 /* Cleanup all data structures used for dissection. */
1810 cleanup_dissection();
1811 /* Initialize all data structures used for dissection. */
1814 /* We need to redissect the packets so we have to discard our old
1815 * packet list store. */
1816 new_packet_list_clear();
1817 add_to_packet_list = TRUE;
1820 /* We don't yet know which will be the first and last frames displayed. */
1821 cf->first_displayed = NULL;
1822 cf->last_displayed = NULL;
1824 /* We currently don't display any packets */
1825 cf->displayed_count = 0;
1827 /* Iterate through the list of frames. Call a routine for each frame
1828 to check whether it should be displayed and, if so, add it to
1829 the display list. */
1830 nstime_set_unset(&first_ts);
1831 nstime_set_unset(&prev_dis_ts);
1832 nstime_set_unset(&prev_cap_ts);
1835 /* Update the progress bar when it gets to this value. */
1836 progbar_nextstep = 0;
1837 /* When we reach the value that triggers a progress bar update,
1838 bump that value by this amount. */
1839 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1840 /* Count of packets at which we've looked. */
1842 /* Progress so far. */
1846 g_get_current_time(&start_time);
1848 /* no previous row yet */
1850 prev_frame_num = -1;
1853 preceding_frame_num = -1;
1854 preceding_frame = NULL;
1855 following_frame_num = -1;
1856 following_frame = NULL;
1858 selected_frame_seen = FALSE;
1860 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
1861 /* Create the progress bar if necessary.
1862 We check on every iteration of the loop, so that it takes no
1863 longer than the standard time to create it (otherwise, for a
1864 large file, we might take considerably longer than that standard
1865 time in order to get to the next progress bar step). */
1866 if (progbar == NULL)
1867 progbar = delayed_create_progress_dlg(action, action_item, TRUE,
1868 &stop_flag, &start_time,
1871 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1872 when we update it, we have to run the GTK+ main loop to get it
1873 to repaint what's pending, and doing so may involve an "ioctl()"
1874 to see if there's any pending input from an X server, and doing
1875 that for every packet can be costly, especially on a big file. */
1876 if (count >= progbar_nextstep) {
1877 /* let's not divide by zero. I should never be started
1878 * with count == 0, so let's assert that
1880 g_assert(cf->count > 0);
1881 progbar_val = (gfloat) count / cf->count;
1883 if (progbar != NULL) {
1884 g_snprintf(status_str, sizeof(status_str),
1885 "%4u of %u frames", count, cf->count);
1886 update_progress_dlg(progbar, progbar_val, status_str);
1889 progbar_nextstep += progbar_quantum;
1893 /* Well, the user decided to abort the filtering. Just stop.
1895 XXX - go back to the previous filter? Users probably just
1896 want not to wait for a filtering operation to finish;
1897 unless we cancel by having no filter, reverting to the
1898 previous filter will probably be even more expensive than
1899 continuing the filtering, as it involves going back to the
1900 beginning and filtering, and even with no filter we currently
1901 have to re-generate the entire clist, which is also expensive.
1903 I'm not sure what Network Monitor does, but it doesn't appear
1904 to give you an unfiltered display if you cancel. */
1911 /* Since all state for the frame was destroyed, mark the frame
1912 * as not visited, free the GSList referring to the state
1913 * data (the per-frame data itself was freed by
1914 * "init_dissection()"), and null out the GSList pointer. */
1915 fdata->flags.visited = 0;
1916 frame_data_cleanup(fdata);
1918 /* cleanup_dissection() calls se_free_all();
1919 * And after that fdata->col_text (which is allocated using se_alloc0())
1920 * no longer points to valid memory.
1922 fdata->col_text_len = se_alloc0(sizeof(fdata->col_text_len) * (cf->cinfo.num_cols));
1923 fdata->col_text = se_alloc0(sizeof(fdata->col_text) * (cf->cinfo.num_cols));
1926 if (!cf_read_frame (cf, fdata, &err, &err_info)) {
1927 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1928 cf_read_error_message(err, err_info), cf->filename);
1932 /* If the previous frame is displayed, and we haven't yet seen the
1933 selected frame, remember that frame - it's the closest one we've
1934 yet seen before the selected frame. */
1935 if (prev_frame_num != -1 && !selected_frame_seen && prev_frame->flags.passed_dfilter) {
1936 preceding_frame_num = prev_frame_num;
1937 preceding_frame = prev_frame;
1939 add_packet_to_packet_list(fdata, cf, dfcode, filtering_tap_listeners,
1940 tap_flags, &cf->pseudo_header, cf->pd,
1942 add_to_packet_list);
1944 /* If this frame is displayed, and this is the first frame we've
1945 seen displayed after the selected frame, remember this frame -
1946 it's the closest one we've yet seen at or after the selected
1948 if (fdata->flags.passed_dfilter && selected_frame_seen && following_frame_num == -1) {
1949 following_frame_num = fdata->num;
1950 following_frame = fdata;
1952 if (fdata == selected_frame) {
1953 selected_frame_seen = TRUE;
1954 if (fdata->flags.passed_dfilter)
1955 selected_frame_num = fdata->num;
1958 /* Remember this frame - it'll be the previous frame
1959 on the next pass through the loop. */
1960 prev_frame_num = fdata->num;
1964 /* We are done redissecting the packet list. */
1965 cf->redissecting = FALSE;
1968 /* Clear out what remains of the visited flags and per-frame data
1971 XXX - that may cause various forms of bogosity when dissecting
1972 these frames, as they won't have been seen by this sequential
1973 pass, but the only alternative I see is to keep scanning them
1974 even though the user requested that the scan stop, and that
1975 would leave the user stuck with an Wireshark grinding on
1976 until it finishes. Should we just stick them with that? */
1977 for (; fdata != NULL; fdata = fdata->next) {
1978 fdata->flags.visited = 0;
1979 frame_data_cleanup(fdata);
1983 /* We're done filtering the packets; destroy the progress bar if it
1985 if (progbar != NULL)
1986 destroy_progress_dlg(progbar);
1988 /* Unfreeze the packet list. */
1989 if (!add_to_packet_list)
1990 new_packet_list_recreate_visible_rows();
1992 /* Compute the time it took to filter the file */
1993 compute_elapsed(&start_time);
1995 new_packet_list_thaw();
1997 if (selected_frame_num == -1) {
1998 /* The selected frame didn't pass the filter. */
1999 if (selected_frame == NULL) {
2000 /* That's because there *was* no selected frame. Make the first
2001 displayed frame the current frame. */
2002 selected_frame_num = 0;
2004 /* Find the nearest displayed frame to the selected frame (whether
2005 it's before or after that frame) and make that the current frame.
2006 If the next and previous displayed frames are equidistant from the
2007 selected frame, choose the next one. */
2008 g_assert(following_frame == NULL ||
2009 following_frame->num >= selected_frame->num);
2010 g_assert(preceding_frame == NULL ||
2011 preceding_frame->num <= selected_frame->num);
2012 if (following_frame == NULL) {
2013 /* No frame after the selected frame passed the filter, so we
2014 have to select the last displayed frame before the selected
2016 selected_frame_num = preceding_frame_num;
2017 selected_frame = preceding_frame;
2018 } else if (preceding_frame == NULL) {
2019 /* No frame before the selected frame passed the filter, so we
2020 have to select the first displayed frame after the selected
2022 selected_frame_num = following_frame_num;
2023 selected_frame = following_frame;
2025 /* Frames before and after the selected frame passed the filter, so
2026 we'll select the previous frame */
2027 selected_frame_num = preceding_frame_num;
2028 selected_frame = preceding_frame;
2033 if (selected_frame_num == -1) {
2034 /* There are no frames displayed at all. */
2035 cf_unselect_packet(cf);
2037 /* Either the frame that was selected passed the filter, or we've
2038 found the nearest displayed frame to that frame. Select it, make
2039 it the focus row, and make it visible. */
2040 /* Set to invalid to force update of packet list and packet details */
2041 cf->current_row = -1;
2042 if (selected_frame_num == 0) {
2043 new_packet_list_select_first_row();
2045 new_packet_list_find_row_from_data(selected_frame, TRUE);
2049 /* Cleanup and release all dfilter resources */
2050 dfilter_free(dfcode);
2056 rescan_packets(capture_file *cf, const char *action, const char *action_item,
2057 gboolean refilter, gboolean redissect)
2060 progdlg_t *progbar = NULL;
2065 frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
2066 int selected_row, prev_row, preceding_row, following_row;
2067 gboolean selected_frame_seen;
2070 GTimeVal start_time;
2071 gchar status_str[100];
2072 int progbar_nextstep;
2073 int progbar_quantum;
2075 gboolean filtering_tap_listeners;
2077 gboolean add_to_packet_list = TRUE;
2080 /* Compile the current display filter.
2081 * We assume this will not fail since cf->dfilter is only set in
2082 * cf_filter IFF the filter was valid.
2084 compiled = dfilter_compile(cf->dfilter, &dfcode);
2085 g_assert(!cf->dfilter || (compiled && dfcode));
2087 /* Do we have any tap listeners with filters? */
2088 filtering_tap_listeners = have_filtering_tap_listeners();
2090 /* Get the union of the flags for all tap listeners. */
2091 tap_flags = union_of_tap_listener_flags();
2093 reset_tap_listeners();
2094 /* Which frame, if any, is the currently selected frame?
2095 XXX - should the selected frame or the focus frame be the "current"
2096 frame, that frame being the one from which "Find Frame" searches
2098 selected_frame = cf->current_frame;
2100 /* We don't yet know what row that frame will be on, if any, after we
2101 rebuild the clist, however. */
2104 /* Freeze the packet list while we redo it, so we don't get any
2105 screen updates while it happens. */
2106 packet_list_freeze();
2109 packet_list_clear();
2112 /* We need to re-initialize all the state information that protocols
2113 keep, because some preference that controls a dissector has changed,
2114 which might cause the state information to be constructed differently
2115 by that dissector. */
2117 /* We might receive new packets while redissecting, and we don't
2118 want to dissect those before their time. */
2119 cf->redissecting = TRUE;
2121 /* Cleanup all data structures used for dissection. */
2122 cleanup_dissection();
2123 /* Initialize all data structures used for dissection. */
2128 /* We don't yet know which will be the first and last frames displayed. */
2129 cf->first_displayed = NULL;
2130 cf->last_displayed = NULL;
2134 /* We currently don't display any packets */
2135 cf->displayed_count = 0;
2137 /* Iterate through the list of frames. Call a routine for each frame
2138 to check whether it should be displayed and, if so, add it to
2139 the display list. */
2140 nstime_set_unset(&first_ts);
2141 nstime_set_unset(&prev_dis_ts);
2142 nstime_set_unset(&prev_cap_ts);
2145 /* Update the progress bar when it gets to this value. */
2146 progbar_nextstep = 0;
2147 /* When we reach the value that triggers a progress bar update,
2148 bump that value by this amount. */
2149 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2150 /* Count of packets at which we've looked. */
2152 /* Progress so far. */
2156 g_get_current_time(&start_time);
2158 row = -1; /* no previous row yet */
2163 preceding_frame = NULL;
2165 following_frame = NULL;
2167 selected_frame_seen = FALSE;
2169 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
2170 /* Create the progress bar if necessary.
2171 We check on every iteration of the loop, so that it takes no
2172 longer than the standard time to create it (otherwise, for a
2173 large file, we might take considerably longer than that standard
2174 time in order to get to the next progress bar step). */
2175 if (progbar == NULL)
2176 progbar = delayed_create_progress_dlg(action, action_item, TRUE,
2177 &stop_flag, &start_time,
2180 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2181 when we update it, we have to run the GTK+ main loop to get it
2182 to repaint what's pending, and doing so may involve an "ioctl()"
2183 to see if there's any pending input from an X server, and doing
2184 that for every packet can be costly, especially on a big file. */
2185 if (count >= progbar_nextstep) {
2186 /* let's not divide by zero. I should never be started
2187 * with count == 0, so let's assert that
2189 g_assert(cf->count > 0);
2190 progbar_val = (gfloat) count / cf->count;
2192 if (progbar != NULL) {
2193 g_snprintf(status_str, sizeof(status_str),
2194 "%4u of %u frames", count, cf->count);
2195 update_progress_dlg(progbar, progbar_val, status_str);
2198 progbar_nextstep += progbar_quantum;
2202 /* Well, the user decided to abort the filtering. Just stop.
2204 XXX - go back to the previous filter? Users probably just
2205 want not to wait for a filtering operation to finish;
2206 unless we cancel by having no filter, reverting to the
2207 previous filter will probably be even more expensive than
2208 continuing the filtering, as it involves going back to the
2209 beginning and filtering, and even with no filter we currently
2210 have to re-generate the entire clist, which is also expensive.
2212 I'm not sure what Network Monitor does, but it doesn't appear
2213 to give you an unfiltered display if you cancel. */
2220 /* Since all state for the frame was destroyed, mark the frame
2221 * as not visited, free the GSList referring to the state
2222 * data (the per-frame data itself was freed by
2223 * "init_dissection()"), and null out the GSList pointer.
2225 fdata->flags.visited = 0;
2226 frame_data_cleanup(fdata);
2229 if (!cf_read_frame (cf, fdata, &err, &err_info)) {
2230 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2231 cf_read_error_message(err, err_info), cf->filename);
2235 /* If the previous frame is displayed, and we haven't yet seen the
2236 selected frame, remember that frame - it's the closest one we've
2237 yet seen before the selected frame. */
2238 if (prev_row != -1 && !selected_frame_seen) {
2239 preceding_row = prev_row;
2240 preceding_frame = prev_frame;
2242 row = add_packet_to_packet_list(fdata, cf, dfcode, filtering_tap_listeners,
2243 tap_flags, &cf->pseudo_header, cf->pd,
2245 add_to_packet_list);
2247 /* If this frame is displayed, and this is the first frame we've
2248 seen displayed after the selected frame, remember this frame -
2249 it's the closest one we've yet seen at or after the selected
2251 if (row != -1 && selected_frame_seen && following_row == -1) {
2252 following_row = row;
2253 following_frame = fdata;
2255 if (fdata == selected_frame) {
2257 selected_frame_seen = TRUE;
2260 /* Remember this row/frame - it'll be the previous row/frame
2261 on the next pass through the loop. */
2266 /* We are done redissecting the packet list. */
2267 cf->redissecting = FALSE;
2270 /* Clear out what remains of the visited flags and per-frame data
2273 XXX - that may cause various forms of bogosity when dissecting
2274 these frames, as they won't have been seen by this sequential
2275 pass, but the only alternative I see is to keep scanning them
2276 even though the user requested that the scan stop, and that
2277 would leave the user stuck with an Wireshark grinding on
2278 until it finishes. Should we just stick them with that? */
2279 for (; fdata != NULL; fdata = fdata->next) {
2280 fdata->flags.visited = 0;
2281 frame_data_cleanup(fdata);
2285 /* We're done filtering the packets; destroy the progress bar if it
2287 if (progbar != NULL)
2288 destroy_progress_dlg(progbar);
2290 /* Unfreeze the packet list. */
2293 if (selected_row == -1) {
2294 /* The selected frame didn't pass the filter. */
2295 if (selected_frame == NULL) {
2296 /* That's because there *was* no selected frame. Make the first
2297 displayed frame the current frame. */
2300 /* Find the nearest displayed frame to the selected frame (whether
2301 it's before or after that frame) and make that the current frame.
2302 If the next and previous displayed frames are equidistant from the
2303 selected frame, choose the next one. */
2304 g_assert(following_frame == NULL ||
2305 following_frame->num >= selected_frame->num);
2306 g_assert(preceding_frame == NULL ||
2307 preceding_frame->num <= selected_frame->num);
2308 if (following_frame == NULL) {
2309 /* No frame after the selected frame passed the filter, so we
2310 have to select the last displayed frame before the selected
2312 selected_row = preceding_row;
2313 } else if (preceding_frame == NULL) {
2314 /* No frame before the selected frame passed the filter, so we
2315 have to select the first displayed frame after the selected
2317 selected_row = following_row;
2319 /* Frames before and after the selected frame passed the filter, so
2320 we'll select the previous frame */
2321 selected_row = preceding_row;
2326 if (selected_row == -1) {
2327 /* There are no frames displayed at all. */
2328 cf_unselect_packet(cf);
2330 /* Either the frame that was selected passed the filter, or we've
2331 found the nearest displayed frame to that frame. Select it, make
2332 it the focus row, and make it visible. */
2333 if (selected_row == 0) {
2334 /* Set to invalid to force update of packet list and packet details */
2335 cf->current_row = -1;
2337 packet_list_set_selected_row(selected_row);
2340 /* Cleanup and release all dfilter resources */
2341 dfilter_free(dfcode);
2343 #endif /* NEW_PACKET_LIST */
2346 * Scan trough all frame data and recalculate the ref time
2347 * without rereading the file.
2348 * XXX - do we need a progres bar or is this fast enough?
2350 #ifdef NEW_PACKET_LIST
2352 ref_time_packets(capture_file *cf)
2356 nstime_set_unset(&first_ts);
2357 nstime_set_unset(&prev_dis_ts);
2360 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
2361 /* just add some value here until we know if it is being displayed or not */
2362 fdata->cum_bytes = cum_bytes + fdata->pkt_len;
2368 /* If we don't have the time stamp of the first packet in the
2369 capture, it's because this is the first packet. Save the time
2370 stamp of this packet as the time stamp of the first packet. */
2371 if (nstime_is_unset(&first_ts)) {
2372 first_ts = fdata->abs_ts;
2374 /* if this frames is marked as a reference time frame, reset
2375 firstsec and firstusec to this frame */
2376 if(fdata->flags.ref_time){
2377 first_ts = fdata->abs_ts;
2380 /* If we don't have the time stamp of the previous displayed packet,
2381 it's because this is the first displayed packet. Save the time
2382 stamp of this packet as the time stamp of the previous displayed
2384 if (nstime_is_unset(&prev_dis_ts)) {
2385 prev_dis_ts = fdata->abs_ts;
2388 /* Get the time elapsed between the first packet and this packet. */
2389 nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
2391 /* If it's greater than the current elapsed time, set the elapsed time
2392 to it (we check for "greater than" so as not to be confused by
2393 time moving backwards). */
2394 if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
2395 || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
2396 cf->elapsed_time = fdata->rel_ts;
2399 /* Get the time elapsed between the previous displayed packet and
2401 nstime_delta(&fdata->del_dis_ts, &fdata->abs_ts, &prev_dis_ts);
2403 prev_dis_ts = fdata->abs_ts;
2408 if( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) ){
2409 /* This frame either passed the display filter list or is marked as
2410 a time reference frame. All time reference frames are displayed
2411 even if they dont pass the display filter */
2412 if(fdata->flags.ref_time){
2413 /* if this was a TIME REF frame we should reset the cum_bytes field */
2414 cum_bytes = fdata->pkt_len;
2415 fdata->cum_bytes = cum_bytes;
2417 /* increase cum_bytes with this packets length */
2418 cum_bytes += fdata->pkt_len;
2432 process_specified_packets(capture_file *cf, packet_range_t *range,
2433 const char *string1, const char *string2, gboolean terminate_is_stop,
2434 gboolean (*callback)(capture_file *, frame_data *,
2435 union wtap_pseudo_header *, const guint8 *, void *),
2436 void *callback_args)
2441 union wtap_pseudo_header pseudo_header;
2442 guint8 pd[WTAP_MAX_PACKET_SIZE+1];
2443 psp_return_t ret = PSP_FINISHED;
2445 progdlg_t *progbar = NULL;
2448 gboolean progbar_stop_flag;
2449 GTimeVal progbar_start_time;
2450 gchar progbar_status_str[100];
2451 int progbar_nextstep;
2452 int progbar_quantum;
2453 range_process_e process_this;
2455 /* Update the progress bar when it gets to this value. */
2456 progbar_nextstep = 0;
2457 /* When we reach the value that triggers a progress bar update,
2458 bump that value by this amount. */
2459 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2460 /* Count of packets at which we've looked. */
2462 /* Progress so far. */
2465 progbar_stop_flag = FALSE;
2466 g_get_current_time(&progbar_start_time);
2468 packet_range_process_init(range);
2470 /* Iterate through the list of packets, printing the packets that
2471 were selected by the current display filter. */
2472 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
2473 /* Create the progress bar if necessary.
2474 We check on every iteration of the loop, so that it takes no
2475 longer than the standard time to create it (otherwise, for a
2476 large file, we might take considerably longer than that standard
2477 time in order to get to the next progress bar step). */
2478 if (progbar == NULL)
2479 progbar = delayed_create_progress_dlg(string1, string2,
2482 &progbar_start_time,
2485 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2486 when we update it, we have to run the GTK+ main loop to get it
2487 to repaint what's pending, and doing so may involve an "ioctl()"
2488 to see if there's any pending input from an X server, and doing
2489 that for every packet can be costly, especially on a big file. */
2490 if (progbar_count >= progbar_nextstep) {
2491 /* let's not divide by zero. I should never be started
2492 * with count == 0, so let's assert that
2494 g_assert(cf->count > 0);
2495 progbar_val = (gfloat) progbar_count / cf->count;
2497 if (progbar != NULL) {
2498 g_snprintf(progbar_status_str, sizeof(progbar_status_str),
2499 "%4u of %u packets", progbar_count, cf->count);
2500 update_progress_dlg(progbar, progbar_val, progbar_status_str);
2503 progbar_nextstep += progbar_quantum;
2506 if (progbar_stop_flag) {
2507 /* Well, the user decided to abort the operation. Just stop,
2508 and arrange to return PSP_STOPPED to our caller, so they know
2509 it was stopped explicitly. */
2516 /* do we have to process this packet? */
2517 process_this = packet_range_process_packet(range, fdata);
2518 if (process_this == range_process_next) {
2519 /* this packet uninteresting, continue with next one */
2521 } else if (process_this == range_processing_finished) {
2522 /* all interesting packets processed, stop the loop */
2526 /* Get the packet */
2527 if (!cf_read_frame_r(cf, fdata, &pseudo_header, pd, &err, &err_info)) {
2528 /* Attempt to get the packet failed. */
2529 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2530 cf_read_error_message(err, err_info), cf->filename);
2534 /* Process the packet */
2535 if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
2536 /* Callback failed. We assume it reported the error appropriately. */
2542 /* We're done printing the packets; destroy the progress bar if
2544 if (progbar != NULL)
2545 destroy_progress_dlg(progbar);
2551 gboolean construct_protocol_tree;
2553 } retap_callback_args_t;
2556 retap_packet(capture_file *cf _U_, frame_data *fdata,
2557 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2560 retap_callback_args_t *args = argsp;
2563 epan_dissect_init(&edt, args->construct_protocol_tree, FALSE);
2564 tap_queue_init(&edt);
2565 epan_dissect_run(&edt, pseudo_header, pd, fdata, args->cinfo);
2566 tap_push_tapped_queue(&edt);
2567 epan_dissect_cleanup(&edt);
2573 cf_retap_packets(capture_file *cf)
2575 packet_range_t range;
2576 retap_callback_args_t callback_args;
2577 gboolean filtering_tap_listeners;
2580 /* Do we have any tap listeners with filters? */
2581 filtering_tap_listeners = have_filtering_tap_listeners();
2583 tap_flags = union_of_tap_listener_flags();
2585 /* If any tap listeners have filters, or require the protocol tree,
2586 construct the protocol tree. */
2587 callback_args.construct_protocol_tree = filtering_tap_listeners ||
2588 (tap_flags & TL_REQUIRES_PROTO_TREE);
2590 /* If any tap listeners require the columns, construct them. */
2591 callback_args.cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
2593 /* Reset the tap listeners. */
2594 reset_tap_listeners();
2596 /* Iterate through the list of packets, dissecting all packets and
2597 re-running the taps. */
2598 packet_range_init(&range);
2599 packet_range_process_init(&range);
2600 switch (process_specified_packets(cf, &range, "Recalculating statistics on",
2601 "all packets", TRUE, retap_packet,
2604 /* Completed successfully. */
2608 /* Well, the user decided to abort the refiltering.
2609 Return CF_READ_ABORTED so our caller knows they did that. */
2610 return CF_READ_ABORTED;
2613 /* Error while retapping. */
2614 return CF_READ_ERROR;
2617 g_assert_not_reached();
2622 print_args_t *print_args;
2623 gboolean print_header_line;
2624 char *header_line_buf;
2625 int header_line_buf_len;
2626 gboolean print_formfeed;
2627 gboolean print_separator;
2631 } print_callback_args_t;
2634 print_packet(capture_file *cf, frame_data *fdata,
2635 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2638 print_callback_args_t *args = argsp;
2645 gboolean proto_tree_needed;
2646 char bookmark_name[9+10+1]; /* "__frameNNNNNNNNNN__\0" */
2647 char bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0" */
2649 /* Create the protocol tree, and make it visible, if we're printing
2650 the dissection or the hex data.
2651 XXX - do we need it if we're just printing the hex data? */
2653 args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex || have_custom_cols(&cf->cinfo);
2654 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2656 /* Fill in the column information if we're printing the summary
2658 if (args->print_args->print_summary) {
2659 col_custom_prime_edt(&edt, &cf->cinfo);
2660 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2661 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2663 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2665 if (args->print_formfeed) {
2666 if (!new_page(args->print_args->stream))
2669 if (args->print_separator) {
2670 if (!print_line(args->print_args->stream, 0, ""))
2676 * We generate bookmarks, if the output format supports them.
2677 * The name is "__frameN__".
2679 g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
2681 if (args->print_args->print_summary) {
2682 if (args->print_header_line) {
2683 if (!print_line(args->print_args->stream, 0, args->header_line_buf))
2685 args->print_header_line = FALSE; /* we might not need to print any more */
2687 cp = &args->line_buf[0];
2689 for (i = 0; i < cf->cinfo.num_cols; i++) {
2690 /* Find the length of the string for this column. */
2691 column_len = (int) strlen(cf->cinfo.col_data[i]);
2692 if (args->col_widths[i] > column_len)
2693 column_len = args->col_widths[i];
2695 /* Make sure there's room in the line buffer for the column; if not,
2696 double its length. */
2697 line_len += column_len + 1; /* "+1" for space */
2698 if (line_len > args->line_buf_len) {
2699 cp_off = (int) (cp - args->line_buf);
2700 args->line_buf_len = 2 * line_len;
2701 args->line_buf = g_realloc(args->line_buf, args->line_buf_len + 1);
2702 cp = args->line_buf + cp_off;
2705 /* Right-justify the packet number column. */
2706 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2707 g_snprintf(cp, column_len+1, "%*s", args->col_widths[i], cf->cinfo.col_data[i]);
2709 g_snprintf(cp, column_len+1, "%-*s", args->col_widths[i], cf->cinfo.col_data[i]);
2711 if (i != cf->cinfo.num_cols - 1)
2717 * Generate a bookmark, using the summary line as the title.
2719 if (!print_bookmark(args->print_args->stream, bookmark_name,
2723 if (!print_line(args->print_args->stream, 0, args->line_buf))
2727 * Generate a bookmark, using "Frame N" as the title, as we're not
2728 * printing the summary line.
2730 g_snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
2731 if (!print_bookmark(args->print_args->stream, bookmark_name,
2734 } /* if (print_summary) */
2736 if (args->print_args->print_dissections != print_dissections_none) {
2737 if (args->print_args->print_summary) {
2738 /* Separate the summary line from the tree with a blank line. */
2739 if (!print_line(args->print_args->stream, 0, ""))
2743 /* Print the information in that tree. */
2744 if (!proto_tree_print(args->print_args, &edt, args->print_args->stream))
2747 /* Print a blank line if we print anything after this (aka more than one packet). */
2748 args->print_separator = TRUE;
2750 /* Print a header line if we print any more packet summaries */
2751 args->print_header_line = TRUE;
2754 if (args->print_args->print_hex) {
2755 /* Print the full packet data as hex. */
2756 if (!print_hex_data(args->print_args->stream, &edt))
2759 /* Print a blank line if we print anything after this (aka more than one packet). */
2760 args->print_separator = TRUE;
2762 /* Print a header line if we print any more packet summaries */
2763 args->print_header_line = TRUE;
2764 } /* if (args->print_args->print_dissections != print_dissections_none) */
2766 epan_dissect_cleanup(&edt);
2768 /* do we want to have a formfeed between each packet from now on? */
2769 if(args->print_args->print_formfeed) {
2770 args->print_formfeed = TRUE;
2776 epan_dissect_cleanup(&edt);
2781 cf_print_packets(capture_file *cf, print_args_t *print_args)
2784 print_callback_args_t callback_args;
2792 callback_args.print_args = print_args;
2793 callback_args.print_header_line = TRUE;
2794 callback_args.header_line_buf = NULL;
2795 callback_args.header_line_buf_len = 256;
2796 callback_args.print_formfeed = FALSE;
2797 callback_args.print_separator = FALSE;
2798 callback_args.line_buf = NULL;
2799 callback_args.line_buf_len = 256;
2800 callback_args.col_widths = NULL;
2802 if (!print_preamble(print_args->stream, cf->filename)) {
2803 destroy_print_stream(print_args->stream);
2804 return CF_PRINT_WRITE_ERROR;
2807 if (print_args->print_summary) {
2808 /* We're printing packet summaries. Allocate the header line buffer
2809 and get the column widths. */
2810 callback_args.header_line_buf = g_malloc(callback_args.header_line_buf_len + 1);
2812 /* Find the widths for each of the columns - maximum of the
2813 width of the title and the width of the data - and construct
2814 a buffer with a line containing the column titles. */
2815 callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * cf->cinfo.num_cols);
2816 cp = &callback_args.header_line_buf[0];
2818 for (i = 0; i < cf->cinfo.num_cols; i++) {
2819 /* Don't pad the last column. */
2820 if (i == cf->cinfo.num_cols - 1)
2821 callback_args.col_widths[i] = 0;
2823 callback_args.col_widths[i] = (gint) strlen(cf->cinfo.col_title[i]);
2824 data_width = get_column_char_width(get_column_format(i));
2825 if (data_width > callback_args.col_widths[i])
2826 callback_args.col_widths[i] = data_width;
2829 /* Find the length of the string for this column. */
2830 column_len = (int) strlen(cf->cinfo.col_title[i]);
2831 if (callback_args.col_widths[i] > column_len)
2832 column_len = callback_args.col_widths[i];
2834 /* Make sure there's room in the line buffer for the column; if not,
2835 double its length. */
2836 line_len += column_len + 1; /* "+1" for space */
2837 if (line_len > callback_args.header_line_buf_len) {
2838 cp_off = (int) (cp - callback_args.header_line_buf);
2839 callback_args.header_line_buf_len = 2 * line_len;
2840 callback_args.header_line_buf = g_realloc(callback_args.header_line_buf,
2841 callback_args.header_line_buf_len + 1);
2842 cp = callback_args.header_line_buf + cp_off;
2845 /* Right-justify the packet number column. */
2846 /* if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2847 g_snprintf(cp, column_len+1, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2849 g_snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2851 if (i != cf->cinfo.num_cols - 1)
2856 /* Now start out the main line buffer with the same length as the
2857 header line buffer. */
2858 callback_args.line_buf_len = callback_args.header_line_buf_len;
2859 callback_args.line_buf = g_malloc(callback_args.line_buf_len + 1);
2860 } /* if (print_summary) */
2862 /* Iterate through the list of packets, printing the packets we were
2864 ret = process_specified_packets(cf, &print_args->range, "Printing",
2865 "selected packets", TRUE, print_packet,
2868 g_free(callback_args.header_line_buf);
2869 g_free(callback_args.line_buf);
2870 g_free(callback_args.col_widths);
2875 /* Completed successfully. */
2879 /* Well, the user decided to abort the printing.
2881 XXX - note that what got generated before they did that
2882 will get printed if we're piping to a print program; we'd
2883 have to write to a file and then hand that to the print
2884 program to make it actually not print anything. */
2888 /* Error while printing.
2890 XXX - note that what got generated before they did that
2891 will get printed if we're piping to a print program; we'd
2892 have to write to a file and then hand that to the print
2893 program to make it actually not print anything. */
2894 destroy_print_stream(print_args->stream);
2895 return CF_PRINT_WRITE_ERROR;
2898 if (!print_finale(print_args->stream)) {
2899 destroy_print_stream(print_args->stream);
2900 return CF_PRINT_WRITE_ERROR;
2903 if (!destroy_print_stream(print_args->stream))
2904 return CF_PRINT_WRITE_ERROR;
2910 write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
2911 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2917 /* Create the protocol tree, but don't fill in the column information. */
2918 epan_dissect_init(&edt, TRUE, TRUE);
2919 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2921 /* Write out the information in that tree. */
2922 proto_tree_write_pdml(&edt, fh);
2924 epan_dissect_cleanup(&edt);
2930 cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
2935 fh = ws_fopen(print_args->file, "w");
2937 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2939 write_pdml_preamble(fh);
2942 return CF_PRINT_WRITE_ERROR;
2945 /* Iterate through the list of packets, printing the packets we were
2947 ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
2948 "selected packets", TRUE,
2949 write_pdml_packet, fh);
2954 /* Completed successfully. */
2958 /* Well, the user decided to abort the printing. */
2962 /* Error while printing. */
2964 return CF_PRINT_WRITE_ERROR;
2967 write_pdml_finale(fh);
2970 return CF_PRINT_WRITE_ERROR;
2973 /* XXX - check for an error */
2980 write_psml_packet(capture_file *cf, frame_data *fdata,
2981 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2986 gboolean proto_tree_needed;
2988 /* Fill in the column information, only create the protocol tree
2989 if having custom columns. */
2990 proto_tree_needed = have_custom_cols(&cf->cinfo);
2991 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2992 col_custom_prime_edt(&edt, &cf->cinfo);
2993 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2994 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2996 /* Write out the information in that tree. */
2997 proto_tree_write_psml(&edt, fh);
2999 epan_dissect_cleanup(&edt);
3005 cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
3010 fh = ws_fopen(print_args->file, "w");
3012 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
3014 write_psml_preamble(fh);
3017 return CF_PRINT_WRITE_ERROR;
3020 /* Iterate through the list of packets, printing the packets we were
3022 ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
3023 "selected packets", TRUE,
3024 write_psml_packet, fh);
3029 /* Completed successfully. */
3033 /* Well, the user decided to abort the printing. */
3037 /* Error while printing. */
3039 return CF_PRINT_WRITE_ERROR;
3042 write_psml_finale(fh);
3045 return CF_PRINT_WRITE_ERROR;
3048 /* XXX - check for an error */
3055 write_csv_packet(capture_file *cf, frame_data *fdata,
3056 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
3061 gboolean proto_tree_needed;
3063 /* Fill in the column information, only create the protocol tree
3064 if having custom columns. */
3065 proto_tree_needed = have_custom_cols(&cf->cinfo);
3066 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
3067 col_custom_prime_edt(&edt, &cf->cinfo);
3068 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
3069 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
3071 /* Write out the information in that tree. */
3072 proto_tree_write_csv(&edt, fh);
3074 epan_dissect_cleanup(&edt);
3080 cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
3085 fh = ws_fopen(print_args->file, "w");
3087 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
3089 write_csv_preamble(fh);
3092 return CF_PRINT_WRITE_ERROR;
3095 /* Iterate through the list of packets, printing the packets we were
3097 ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
3098 "selected packets", TRUE,
3099 write_csv_packet, fh);
3104 /* Completed successfully. */
3108 /* Well, the user decided to abort the printing. */
3112 /* Error while printing. */
3114 return CF_PRINT_WRITE_ERROR;
3117 write_csv_finale(fh);
3120 return CF_PRINT_WRITE_ERROR;
3123 /* XXX - check for an error */
3130 write_carrays_packet(capture_file *cf _U_, frame_data *fdata,
3131 union wtap_pseudo_header *pseudo_header _U_,
3132 const guint8 *pd, void *argsp)
3136 proto_tree_write_carrays(pd, fdata->cap_len, fdata->num, fh);
3141 cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
3146 fh = ws_fopen(print_args->file, "w");
3149 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
3151 write_carrays_preamble(fh);
3155 return CF_PRINT_WRITE_ERROR;
3158 /* Iterate through the list of packets, printing the packets we were
3160 ret = process_specified_packets(cf, &print_args->range,
3162 "selected packets", TRUE,
3163 write_carrays_packet, fh);
3166 /* Completed successfully. */
3169 /* Well, the user decided to abort the printing. */
3172 /* Error while printing. */
3174 return CF_PRINT_WRITE_ERROR;
3177 write_carrays_finale(fh);
3181 return CF_PRINT_WRITE_ERROR;
3188 #ifndef NEW_PACKET_LIST /* This function is not needed with the new packet list */
3190 /* Scan through the packet list and change all columns that use the
3191 "command-line-specified" time stamp format to use the current
3192 value of that format. */
3194 cf_change_time_formats(capture_file *cf)
3198 progdlg_t *progbar = NULL;
3203 GTimeVal start_time;
3204 gchar status_str[100];
3205 int progbar_nextstep;
3206 int progbar_quantum;
3207 gboolean sorted_by_frame_column;
3209 /* Adjust timestamp precision if auto is selected */
3210 cf_timestamp_auto_precision(cf);
3212 /* Are there any columns with time stamps in the "command-line-specified"
3215 XXX - we have to force the "column is writable" flag on, as it
3216 might be off from the last frame that was dissected. */
3217 col_set_writable(&cf->cinfo, TRUE);
3218 if (!check_col(&cf->cinfo, COL_CLS_TIME) &&
3219 !check_col(&cf->cinfo, COL_ABS_TIME) &&
3220 !check_col(&cf->cinfo, COL_ABS_DATE_TIME) &&
3221 !check_col(&cf->cinfo, COL_REL_TIME) &&
3222 !check_col(&cf->cinfo, COL_DELTA_TIME) &&
3223 !check_col(&cf->cinfo, COL_DELTA_TIME_DIS)) {
3224 /* No, there aren't any columns in that format, so we have no work
3229 /* Freeze the packet list while we redo it, so we don't get any
3230 screen updates while it happens. */
3231 packet_list_freeze();
3233 /* Update the progress bar when it gets to this value. */
3234 progbar_nextstep = 0;
3235 /* When we reach the value that triggers a progress bar update,
3236 bump that value by this amount. */
3237 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
3238 /* Count of packets at which we've looked. */
3240 /* Progress so far. */
3243 /* If the rows are currently sorted by the frame column then we know
3244 * the row number of each packet: it's the row number of the previously
3245 * displayed packet + 1.
3247 * Otherwise, if the display is sorted by a different column then we have
3248 * to use the O(N) packet_list_find_row_from_data() (thus making the job
3249 * of changing the time display format O(N**2)).
3251 * (XXX - In fact it's still O(N**2) because gtk_clist_set_text() takes
3252 * the row number and walks that many elements down the clist to find
3253 * the appropriate element.)
3255 sorted_by_frame_column = FALSE;
3256 for (i = 0; i < cf->cinfo.num_cols; i++) {
3257 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
3259 sorted_by_frame_column = (i == packet_list_get_sort_column());
3265 g_get_current_time(&start_time);
3267 /* Iterate through the list of packets, checking whether the packet
3268 is in a row of the summary list and, if so, whether there are
3269 any columns that show the time in the "command-line-specified"
3270 format and, if so, update that row. */
3271 for (fdata = cf->plist_start, row = -1; fdata != NULL; fdata = fdata->next) {
3272 /* Create the progress bar if necessary.
3273 We check on every iteration of the loop, so that it takes no
3274 longer than the standard time to create it (otherwise, for a
3275 large file, we might take considerably longer than that standard
3276 time in order to get to the next progress bar step). */
3277 if (progbar == NULL)
3278 progbar = delayed_create_progress_dlg("Changing", "time display",
3279 TRUE, &stop_flag, &start_time, progbar_val);
3281 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
3282 when we update it, we have to run the GTK+ main loop to get it
3283 to repaint what's pending, and doing so may involve an "ioctl()"
3284 to see if there's any pending input from an X server, and doing
3285 that for every packet can be costly, especially on a big file. */
3286 if (count >= progbar_nextstep) {
3287 /* let's not divide by zero. I should never be started
3288 * with count == 0, so let's assert that
3290 g_assert(cf->count > 0);
3292 progbar_val = (gfloat) count / cf->count;
3294 if (progbar != NULL) {
3295 g_snprintf(status_str, sizeof(status_str),
3296 "%4u of %u packets", count, cf->count);
3297 update_progress_dlg(progbar, progbar_val, status_str);
3300 progbar_nextstep += progbar_quantum;
3304 /* Well, the user decided to abort the redisplay. Just stop.
3306 XXX - this leaves the time field in the old format in
3307 frames we haven't yet processed. So it goes; should we
3308 simply not offer them the option of stopping? */
3314 /* Find what row this packet is in. */
3315 if (!sorted_by_frame_column) {
3316 /* This function is O(N), so we try to avoid using it... */
3317 row = packet_list_find_row_from_data(fdata);
3319 /* ...which we do by maintaining a count of packets that are
3320 being displayed (i.e., that have passed the display filter),
3321 and using the current value of that count as the row number
3322 (which is why we can only do it when the display is sorted
3323 by the frame number). */
3324 if (fdata->flags.passed_dfilter)
3331 /* This packet is in the summary list, on row "row". */
3333 for (i = 0; i < cf->cinfo.num_cols; i++) {
3334 if (col_has_time_fmt(&cf->cinfo, i)) {
3335 /* This is one of the columns that shows the time in
3336 "command-line-specified" format; update it. */
3337 cf->cinfo.col_buf[i][0] = '\0';
3338 col_set_fmt_time(fdata, &cf->cinfo, cf->cinfo.col_fmt[i], i);
3339 packet_list_set_text(row, i, cf->cinfo.col_data[i]);
3345 /* We're done redisplaying the packets; destroy the progress bar if it
3347 if (progbar != NULL)
3348 destroy_progress_dlg(progbar);
3350 /* Set the column widths of those columns that show the time in
3351 "command-line-specified" format. */
3352 for (i = 0; i < cf->cinfo.num_cols; i++) {
3353 if (col_has_time_fmt(&cf->cinfo, i)) {
3354 packet_list_set_time_width(cf->cinfo.col_fmt[i], i);
3358 /* Unfreeze the packet list. */
3361 #endif /* NEW_PACKET_LIST */
3368 gboolean frame_matched;
3372 cf_find_packet_protocol_tree(capture_file *cf, const char *string)
3376 mdata.string = string;
3377 mdata.string_len = strlen(string);
3378 return find_packet(cf, match_protocol_tree, &mdata);
3382 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
3384 match_data *mdata = criterion;
3387 /* Construct the protocol tree, including the displayed text */
3388 epan_dissect_init(&edt, TRUE, TRUE);
3389 /* We don't need the column information */
3390 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
3392 /* Iterate through all the nodes, seeing if they have text that matches. */
3394 mdata->frame_matched = FALSE;
3395 proto_tree_children_foreach(edt.tree, match_subtree_text, mdata);
3396 epan_dissect_cleanup(&edt);
3397 return mdata->frame_matched;
3401 match_subtree_text(proto_node *node, gpointer data)
3403 match_data *mdata = (match_data*) data;
3404 const gchar *string = mdata->string;
3405 size_t string_len = mdata->string_len;
3406 capture_file *cf = mdata->cf;
3407 field_info *fi = PNODE_FINFO(node);
3408 gchar label_str[ITEM_LABEL_LENGTH];
3415 g_assert(fi && "dissection with an invisible proto tree?");
3417 if (mdata->frame_matched) {
3418 /* We already had a match; don't bother doing any more work. */
3422 /* Don't match invisible entries. */
3423 if (PROTO_ITEM_IS_HIDDEN(node))
3426 /* was a free format label produced? */
3428 label_ptr = fi->rep->representation;
3430 /* no, make a generic label */
3431 label_ptr = label_str;
3432 proto_item_fill_label(fi, label_str);
3435 /* Does that label match? */
3436 label_len = strlen(label_ptr);
3437 for (i = 0; i < label_len; i++) {
3438 c_char = label_ptr[i];
3440 c_char = toupper(c_char);
3441 if (c_char == string[c_match]) {
3443 if (c_match == string_len) {
3444 /* No need to look further; we have a match */
3445 mdata->frame_matched = TRUE;
3452 /* Recurse into the subtree, if it exists */
3453 if (node->first_child != NULL)
3454 proto_tree_children_foreach(node, match_subtree_text, mdata);
3458 cf_find_packet_summary_line(capture_file *cf, const char *string)
3462 mdata.string = string;
3463 mdata.string_len = strlen(string);
3464 return find_packet(cf, match_summary_line, &mdata);
3468 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
3470 match_data *mdata = criterion;
3471 const gchar *string = mdata->string;
3472 size_t string_len = mdata->string_len;
3474 const char *info_column;
3475 size_t info_column_len;
3476 gboolean frame_matched = FALSE;
3482 /* Don't bother constructing the protocol tree */
3483 epan_dissect_init(&edt, FALSE, FALSE);
3484 /* Get the column information */
3485 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, &cf->cinfo);
3487 /* Find the Info column */
3488 for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
3489 if (cf->cinfo.fmt_matx[colx][COL_INFO]) {
3490 /* Found it. See if we match. */
3491 info_column = edt.pi.cinfo->col_data[colx];
3492 info_column_len = strlen(info_column);
3493 for (i = 0; i < info_column_len; i++) {
3494 c_char = info_column[i];
3496 c_char = toupper(c_char);
3497 if (c_char == string[c_match]) {
3499 if (c_match == string_len) {
3500 frame_matched = TRUE;
3509 epan_dissect_cleanup(&edt);
3510 return frame_matched;
3516 } cbs_t; /* "Counted byte string" */
3519 cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size)
3524 info.data_len = string_size;
3526 /* String or hex search? */
3528 /* String search - what type of string? */
3529 switch (cf->scs_type) {
3531 case SCS_ASCII_AND_UNICODE:
3532 return find_packet(cf, match_ascii_and_unicode, &info);
3535 return find_packet(cf, match_ascii, &info);
3538 return find_packet(cf, match_unicode, &info);
3541 g_assert_not_reached();
3545 return find_packet(cf, match_binary, &info);
3549 match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
3551 cbs_t *info = criterion;
3552 const guint8 *ascii_text = info->data;
3553 size_t textlen = info->data_len;
3554 gboolean frame_matched;
3560 frame_matched = FALSE;
3561 buf_len = fdata->pkt_len;
3562 for (i = 0; i < buf_len; i++) {
3565 c_char = toupper(c_char);
3567 if (c_char == ascii_text[c_match]) {
3569 if (c_match == textlen) {
3570 frame_matched = TRUE;
3571 cf->search_pos = i; /* Save the position of the last character
3572 for highlighting the field. */
3579 return frame_matched;
3583 match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
3585 cbs_t *info = criterion;
3586 const guint8 *ascii_text = info->data;
3587 size_t textlen = info->data_len;
3588 gboolean frame_matched;
3594 frame_matched = FALSE;
3595 buf_len = fdata->pkt_len;
3596 for (i = 0; i < buf_len; i++) {
3599 c_char = toupper(c_char);
3600 if (c_char == ascii_text[c_match]) {
3602 if (c_match == textlen) {
3603 frame_matched = TRUE;
3604 cf->search_pos = i; /* Save the position of the last character
3605 for highlighting the field. */
3611 return frame_matched;
3615 match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
3617 cbs_t *info = criterion;
3618 const guint8 *ascii_text = info->data;
3619 size_t textlen = info->data_len;
3620 gboolean frame_matched;
3626 frame_matched = FALSE;
3627 buf_len = fdata->pkt_len;
3628 for (i = 0; i < buf_len; i++) {
3631 c_char = toupper(c_char);
3632 if (c_char == ascii_text[c_match]) {
3635 if (c_match == textlen) {
3636 frame_matched = TRUE;
3637 cf->search_pos = i; /* Save the position of the last character
3638 for highlighting the field. */
3644 return frame_matched;
3648 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
3650 cbs_t *info = criterion;
3651 const guint8 *binary_data = info->data;
3652 size_t datalen = info->data_len;
3653 gboolean frame_matched;
3658 frame_matched = FALSE;
3659 buf_len = fdata->pkt_len;
3660 for (i = 0; i < buf_len; i++) {
3661 if (cf->pd[i] == binary_data[c_match]) {
3663 if (c_match == datalen) {
3664 frame_matched = TRUE;
3665 cf->search_pos = i; /* Save the position of the last character
3666 for highlighting the field. */
3672 return frame_matched;
3676 cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode)
3678 return find_packet(cf, match_dfilter, sfcode);
3682 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
3684 dfilter_t *sfcode = criterion;
3686 gboolean frame_matched;
3688 epan_dissect_init(&edt, TRUE, FALSE);
3689 epan_dissect_prime_dfilter(&edt, sfcode);
3690 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
3691 frame_matched = dfilter_apply_edt(sfcode, &edt);
3692 epan_dissect_cleanup(&edt);
3693 return frame_matched;
3697 find_packet(capture_file *cf,
3698 gboolean (*match_function)(capture_file *, frame_data *, void *),
3701 frame_data *start_fd;
3703 frame_data *new_fd = NULL;
3704 progdlg_t *progbar = NULL;
3711 GTimeVal start_time;
3712 gchar status_str[100];
3713 int progbar_nextstep;
3714 int progbar_quantum;
3717 start_fd = cf->current_frame;
3718 if (start_fd != NULL) {
3719 /* Iterate through the list of packets, starting at the packet we've
3720 picked, calling a routine to run the filter on the packet, see if
3721 it matches, and stop if so. */
3725 /* Update the progress bar when it gets to this value. */
3726 progbar_nextstep = 0;
3727 /* When we reach the value that triggers a progress bar update,
3728 bump that value by this amount. */
3729 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
3730 /* Progress so far. */
3734 g_get_current_time(&start_time);
3737 title = cf->sfilter?cf->sfilter:"";
3739 /* Create the progress bar if necessary.
3740 We check on every iteration of the loop, so that it takes no
3741 longer than the standard time to create it (otherwise, for a
3742 large file, we might take considerably longer than that standard
3743 time in order to get to the next progress bar step). */
3744 if (progbar == NULL)
3745 progbar = delayed_create_progress_dlg("Searching", title,
3746 FALSE, &stop_flag, &start_time, progbar_val);
3748 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
3749 when we update it, we have to run the GTK+ main loop to get it
3750 to repaint what's pending, and doing so may involve an "ioctl()"
3751 to see if there's any pending input from an X server, and doing
3752 that for every packet can be costly, especially on a big file. */
3753 if (count >= progbar_nextstep) {
3754 /* let's not divide by zero. I should never be started
3755 * with count == 0, so let's assert that
3757 g_assert(cf->count > 0);
3759 progbar_val = (gfloat) count / cf->count;
3761 if (progbar != NULL) {
3762 g_snprintf(status_str, sizeof(status_str),
3763 "%4u of %u packets", count, cf->count);
3764 update_progress_dlg(progbar, progbar_val, status_str);
3767 progbar_nextstep += progbar_quantum;
3771 /* Well, the user decided to abort the search. Go back to the
3772 frame where we started. */
3777 /* Go past the current frame. */
3778 if (cf->sbackward) {
3779 /* Go on to the previous frame. */
3780 fdata = fdata->prev;
3781 if (fdata == NULL) {
3783 * XXX - other apps have a bit more of a detailed message
3784 * for this, and instead of offering "OK" and "Cancel",
3785 * they offer things such as "Continue" and "Cancel";
3786 * we need an API for popping up alert boxes with
3787 * {Verb} and "Cancel".
3790 if (prefs.gui_find_wrap)
3792 simple_status("Search reached the beginning. Continuing at end.");
3793 fdata = cf->plist_end; /* wrap around */
3797 simple_status("Search reached the beginning.");
3798 fdata = start_fd; /* stay on previous packet */
3802 /* Go on to the next frame. */
3803 fdata = fdata->next;
3804 if (fdata == NULL) {
3805 if (prefs.gui_find_wrap)
3807 simple_status("Search reached the end. Continuing at beginning.");
3808 fdata = cf->plist_start; /* wrap around */
3812 simple_status("Search reached the end.");
3813 fdata = start_fd; /* stay on previous packet */
3820 /* Is this packet in the display? */
3821 if (fdata->flags.passed_dfilter) {
3822 /* Yes. Load its data. */
3823 if (!cf_read_frame(cf, fdata, &err, &err_info)) {
3824 /* Read error. Report the error, and go back to the frame
3825 where we started. */
3826 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3827 cf_read_error_message(err, err_info), cf->filename);
3832 /* Does it match the search criterion? */
3833 if ((*match_function)(cf, fdata, criterion)) {
3835 break; /* found it! */
3839 if (fdata == start_fd) {
3840 /* We're back to the frame we were on originally, and that frame
3841 doesn't match the search filter. The search failed. */
3846 /* We're done scanning the packets; destroy the progress bar if it
3848 if (progbar != NULL)
3849 destroy_progress_dlg(progbar);
3852 if (new_fd != NULL) {
3853 #ifdef NEW_PACKET_LIST
3854 /* Find and select */
3855 row = new_packet_list_find_row_from_data(fdata, TRUE);
3857 /* We found a frame. Find what row it's in. */
3858 row = packet_list_find_row_from_data(new_fd);
3859 #endif /* NEW_PACKET_LIST */
3861 /* We didn't find a row even though we know that a frame
3862 * exists that satifies the search criteria. This means that the
3863 * frame isn't being displayed currently so we can't select it. */
3864 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
3865 "%sEnd of capture exceeded!%s\n\n"
3866 "The capture file is probably not fully loaded.",
3867 simple_dialog_primary_start(), simple_dialog_primary_end());
3871 #ifndef NEW_PACKET_LIST
3872 /* Select that row, make it the focus row, and make it visible. */
3873 packet_list_set_selected_row(row);
3874 #endif /* NEW_PACKET_LIST */
3875 return TRUE; /* success */
3877 return FALSE; /* failure */
3881 cf_goto_frame(capture_file *cf, guint fnumber)
3886 for (fdata = cf->plist_start; fdata != NULL && fdata->num < fnumber; fdata = fdata->next)
3889 if (fdata == NULL) {
3890 /* we didn't find a packet with that packet number */
3891 simple_status("There is no packet number %u.", fnumber);
3892 return FALSE; /* we failed to go to that packet */
3894 if (!fdata->flags.passed_dfilter) {
3895 /* that packet currently isn't displayed */
3896 /* XXX - add it to the set of displayed packets? */
3897 simple_status("Packet number %u isn't displayed.", fnumber);
3898 return FALSE; /* we failed to go to that packet */
3901 #ifdef NEW_PACKET_LIST
3902 row = new_packet_list_find_row_from_data(fdata, TRUE);
3904 /* We found that packet, and it's currently being displayed.
3905 Find what row it's in. */
3906 row = packet_list_find_row_from_data(fdata);
3907 g_assert(row != -1);
3909 /* Select that row, make it the focus row, and make it visible. */
3910 packet_list_set_selected_row(row);
3911 #endif /* NEW_PACKET_LIST */
3912 return TRUE; /* we got to that packet */
3916 cf_goto_top_frame(capture_file *cf _U_)
3918 #ifdef NEW_PACKET_LIST
3919 /* Find and select */
3920 new_packet_list_select_first_row();
3924 frame_data *lowest_fdata = NULL;
3926 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
3927 if (fdata->flags.passed_dfilter) {
3928 lowest_fdata = fdata;
3933 if (lowest_fdata == NULL) {
3937 /* We found that packet, and it's currently being displayed.
3938 Find what row it's in. */
3939 row = packet_list_find_row_from_data(lowest_fdata);
3940 g_assert(row != -1);
3942 /* Select that row, make it the focus row, and make it visible. */
3943 packet_list_set_selected_row(row);
3944 #endif /* NEW_PACKET_LIST */
3945 return TRUE; /* we got to that packet */
3949 cf_goto_bottom_frame(capture_file *cf _U_) /* cf is unused w/ NEW_PACKET_LIST */
3951 #ifdef NEW_PACKET_LIST
3952 /* Find and select */
3953 new_packet_list_select_last_row();
3957 frame_data *highest_fdata = NULL;
3959 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
3960 if (fdata->flags.passed_dfilter) {
3961 highest_fdata = fdata;
3965 if (highest_fdata == NULL) {
3969 /* We found that packet, and it's currently being displayed.
3970 Find what row it's in. */
3971 row = packet_list_find_row_from_data(highest_fdata);
3972 g_assert(row != -1);
3974 /* Select that row, make it the focus row, and make it visible. */
3975 packet_list_set_selected_row(row);
3976 #endif /* NEW_PACKET_LIST */
3977 return TRUE; /* we got to that packet */
3981 * Go to frame specified by currently selected protocol tree item.
3984 cf_goto_framenum(capture_file *cf)
3986 header_field_info *hfinfo;
3989 if (cf->finfo_selected) {
3990 hfinfo = cf->finfo_selected->hfinfo;
3992 if (hfinfo->type == FT_FRAMENUM) {
3993 framenum = fvalue_get_uinteger(&cf->finfo_selected->value);
3995 return cf_goto_frame(cf, framenum);
4002 /* Select the packet on a given row. */
4004 cf_select_packet(capture_file *cf, int row)
4010 /* Get the frame data struct pointer for this frame */
4011 #ifdef NEW_PACKET_LIST
4012 fdata = new_packet_list_get_row_data(row);
4014 fdata = (frame_data *)packet_list_get_row_data(row);
4017 if (fdata == NULL) {
4018 /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
4019 the first entry is added to it by "real_insert_row()", that row
4020 is selected (see "real_insert_row()", in "gtk/gtkclist.c", in both
4021 our version and the vanilla GTK+ version).
4023 This means that a "select-row" signal is emitted; this causes
4024 "packet_list_select_cb()" to be called, which causes "cf_select_packet()"
4027 "cf_select_packet()" fetches, above, the data associated with the
4028 row that was selected; however, as "gtk_clist_append()", which
4029 called "real_insert_row()", hasn't yet returned, we haven't yet
4030 associated any data with that row, so we get back a null pointer.
4032 We can't assume that there's only one frame in the frame list,
4033 either, as we may be filtering the display.
4035 We therefore assume that, if "row" is 0, i.e. the first row
4036 is being selected, and "cf->first_displayed" equals
4037 "cf->last_displayed", i.e. there's only one frame being
4038 displayed, that frame is the frame we want.
4040 This means we have to set "cf->first_displayed" and
4041 "cf->last_displayed" before adding the row to the
4042 GtkCList; see the comment in "add_packet_to_packet_list()". */
4044 if (row == 0 && cf->first_displayed == cf->last_displayed)
4045 fdata = cf->first_displayed;
4048 /* If fdata _still_ isn't set simply give up. */
4049 if (fdata == NULL) {
4053 /* Get the data in that frame. */
4054 if (!cf_read_frame (cf, fdata, &err, &err_info)) {
4055 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4056 cf_read_error_message(err, err_info), cf->filename);
4060 /* Record that this frame is the current frame. */
4061 cf->current_frame = fdata;
4062 cf->current_row = row;
4064 /* Create the logical protocol tree. */
4065 if (cf->edt != NULL)
4066 epan_dissect_free(cf->edt);
4068 /* We don't need the columns here. */
4069 cf->edt = epan_dissect_new(TRUE, TRUE);
4071 tap_build_interesting(cf->edt);
4072 epan_dissect_run(cf->edt, &cf->pseudo_header, cf->pd, cf->current_frame,
4075 dfilter_macro_build_ftv_cache(cf->edt->tree);
4077 cf_callback_invoke(cf_cb_packet_selected, cf);
4080 /* Unselect the selected packet, if any. */
4082 cf_unselect_packet(capture_file *cf)
4084 /* Destroy the epan_dissect_t for the unselected packet. */
4085 if (cf->edt != NULL) {
4086 epan_dissect_free(cf->edt);
4090 /* No packet is selected. */
4091 cf->current_frame = NULL;
4092 cf->current_row = 0;
4094 cf_callback_invoke(cf_cb_packet_unselected, cf);
4096 /* No protocol tree means no selected field. */
4097 cf_unselect_field(cf);
4100 /* Unset the selected protocol tree field, if any. */
4102 cf_unselect_field(capture_file *cf)
4104 cf->finfo_selected = NULL;
4106 cf_callback_invoke(cf_cb_field_unselected, cf);
4110 * Mark a particular frame.
4113 cf_mark_frame(capture_file *cf, frame_data *frame)
4115 if (! frame->flags.marked) {
4116 frame->flags.marked = TRUE;
4117 if (cf->count > cf->marked_count)
4123 * Unmark a particular frame.
4126 cf_unmark_frame(capture_file *cf, frame_data *frame)
4128 if (frame->flags.marked) {
4129 frame->flags.marked = FALSE;
4130 if (cf->marked_count > 0)
4136 * Ignore a particular frame.
4139 cf_ignore_frame(capture_file *cf, frame_data *frame)
4141 if (! frame->flags.ignored) {
4142 frame->flags.ignored = TRUE;
4143 if (cf->count > cf->ignored_count)
4144 cf->ignored_count++;
4149 * Un-ignore a particular frame.
4152 cf_unignore_frame(capture_file *cf, frame_data *frame)
4154 if (frame->flags.ignored) {
4155 frame->flags.ignored = FALSE;
4156 if (cf->ignored_count > 0)
4157 cf->ignored_count--;
4164 } save_callback_args_t;
4167 * Save a capture to a file, in a particular format, saving either
4168 * all packets, all currently-displayed packets, or all marked packets.
4170 * Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
4171 * up a message box for the failure.
4174 save_packet(capture_file *cf _U_, frame_data *fdata,
4175 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
4178 save_callback_args_t *args = argsp;
4179 struct wtap_pkthdr hdr;
4182 /* init the wtap header for saving */
4183 hdr.ts.secs = fdata->abs_ts.secs;
4184 hdr.ts.nsecs = fdata->abs_ts.nsecs;
4185 hdr.caplen = fdata->cap_len;
4186 hdr.len = fdata->pkt_len;
4187 hdr.pkt_encap = fdata->lnk_t;
4189 /* and save the packet */
4190 if (!wtap_dump(args->pdh, &hdr, pseudo_header, pd, &err)) {
4191 cf_write_failure_alert_box(args->fname, err);
4198 * Can this capture file be saved in any format except by copying the raw data?
4201 cf_can_save_as(capture_file *cf)
4205 for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) {
4206 /* To save a file with Wiretap, Wiretap has to handle that format,
4207 and its code to handle that format must be able to write a file
4208 with this file's encapsulation type. */
4209 if (wtap_dump_can_open(ft) && wtap_dump_can_write_encap(ft, cf->lnk_t)) {
4210 /* OK, we can write it out in this type. */
4215 /* No, we couldn't save it in any format. */
4220 cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_format, gboolean compressed)
4222 gchar *from_filename;
4226 save_callback_args_t callback_args;
4228 cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
4230 /* don't write over an existing file. */
4231 /* this should've been already checked by our caller, just to be sure... */
4232 if (file_exists(fname)) {
4233 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4234 "%sCapture file: \"%s\" already exists!%s\n\n"
4235 "Please choose a different filename.",
4236 simple_dialog_primary_start(), fname, simple_dialog_primary_end());
4240 packet_range_process_init(range);
4242 if (packet_range_process_all(range) && save_format == cf->cd_t) {
4243 /* We're not filtering packets, and we're saving it in the format
4244 it's already in, so we can just move or copy the raw data. */
4246 if (cf->is_tempfile) {
4247 /* The file being saved is a temporary file from a live
4248 capture, so it doesn't need to stay around under that name;
4249 first, try renaming the capture buffer file to the new name. */
4251 if (ws_rename(cf->filename, fname) == 0) {
4252 /* That succeeded - there's no need to copy the source file. */
4253 from_filename = NULL;
4256 if (errno == EXDEV) {
4257 /* They're on different file systems, so we have to copy the
4260 from_filename = cf->filename;
4262 /* The rename failed, but not because they're on different
4263 file systems - put up an error message. (Or should we
4264 just punt and try to copy? The only reason why I'd
4265 expect the rename to fail and the copy to succeed would
4266 be if we didn't have permission to remove the file from
4267 the temporary directory, and that might be fixable - but
4268 is it worth requiring the user to go off and fix it?) */
4269 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4270 file_rename_error_message(errno), fname);
4276 from_filename = cf->filename;
4279 /* It's a permanent file, so we should copy it, and not remove the
4282 from_filename = cf->filename;
4286 /* Copy the file, if we haven't moved it. */
4287 if (!copy_file_binary_mode(from_filename, fname))
4291 /* Either we're filtering packets, or we're saving in a different
4292 format; we can't do that by copying or moving the capture file,
4293 we have to do it by writing the packets out in Wiretap. */
4294 pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap,
4297 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
4301 /* XXX - we let the user save a subset of the packets.
4303 If we do that, should we make that file the current file? If so,
4304 it means we can no longer get at the other packets. What does
4307 /* Iterate through the list of packets, processing the packets we were
4310 XXX - we've already called "packet_range_process_init(range)", but
4311 "process_specified_packets()" will do it again. Fortunately,
4312 that's harmless in this case, as we haven't done anything to
4313 "range" since we initialized it. */
4314 callback_args.pdh = pdh;
4315 callback_args.fname = fname;
4316 switch (process_specified_packets(cf, range, "Saving", "selected packets",
4317 TRUE, save_packet, &callback_args)) {
4320 /* Completed successfully. */
4324 /* The user decided to abort the saving.
4325 XXX - remove the output file? */
4329 /* Error while saving. */
4330 wtap_dump_close(pdh, &err);
4334 if (!wtap_dump_close(pdh, &err)) {
4335 cf_close_failure_alert_box(fname, err);
4340 cf_callback_invoke(cf_cb_file_save_finished, NULL);
4342 if (packet_range_process_all(range)) {
4343 /* We saved the entire capture, not just some packets from it.
4344 Open and read the file we saved it to.
4346 XXX - this is somewhat of a waste; we already have the
4347 packets, all this gets us is updated file type information
4348 (which we could just stuff into "cf"), and having the new
4349 file be the one we have opened and from which we're reading
4350 the data, and it means we have to spend time opening and
4351 reading the file, which could be a significant amount of
4352 time if the file is large. */
4353 cf->user_saved = TRUE;
4355 if ((cf_open(cf, fname, FALSE, &err)) == CF_OK) {
4356 /* XXX - report errors if this fails?
4357 What should we return if it fails or is aborted? */
4359 switch (cf_read(cf, TRUE)) {
4363 /* Just because we got an error, that doesn't mean we were unable
4364 to read any of the file; we handle what we could get from the
4368 case CF_READ_ABORTED:
4369 /* The user bailed out of re-reading the capture file; the
4370 capture file has been closed - just return (without
4371 changing any menu settings; "cf_close()" set them
4372 correctly for the "no capture file open" state). */
4375 cf_callback_invoke(cf_cb_file_save_reload_finished, cf);
4381 cf_callback_invoke(cf_cb_file_save_failed, NULL);
4386 cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
4387 gboolean for_writing, int file_type)
4390 /* Wiretap error. */
4393 case WTAP_ERR_NOT_REGULAR_FILE:
4394 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4395 "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
4399 case WTAP_ERR_RANDOM_OPEN_PIPE:
4400 /* Seen only when opening a capture file for reading. */
4401 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4402 "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.",
4406 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
4407 /* Seen only when opening a capture file for reading. */
4408 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4409 "The file \"%s\" isn't a capture file in a format Wireshark understands.",
4413 case WTAP_ERR_UNSUPPORTED:
4414 /* Seen only when opening a capture file for reading. */
4415 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4416 "The file \"%s\" isn't a capture file in a format Wireshark understands.\n"
4418 filename, err_info);
4422 case WTAP_ERR_CANT_WRITE_TO_PIPE:
4423 /* Seen only when opening a capture file for writing. */
4424 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4425 "The file \"%s\" is a pipe, and %s capture files can't be "
4426 "written to a pipe.",
4427 filename, wtap_file_type_string(file_type));
4430 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
4431 /* Seen only when opening a capture file for writing. */
4432 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4433 "Wireshark doesn't support writing capture files in that format.");
4436 case WTAP_ERR_UNSUPPORTED_ENCAP:
4438 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4439 "Wireshark can't save this capture in that format.");
4441 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4442 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.\n"
4444 filename, err_info);
4449 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
4451 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4452 "Wireshark can't save this capture in that format.");
4454 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4455 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.",
4460 case WTAP_ERR_BAD_RECORD:
4461 /* Seen only when opening a capture file for reading. */
4462 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4463 "The file \"%s\" appears to be damaged or corrupt.\n"
4465 filename, err_info);
4469 case WTAP_ERR_CANT_OPEN:
4471 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4472 "The file \"%s\" could not be created for some unknown reason.",
4475 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4476 "The file \"%s\" could not be opened for some unknown reason.",
4481 case WTAP_ERR_SHORT_READ:
4482 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4483 "The file \"%s\" appears to have been cut short"
4484 " in the middle of a packet or other data.",
4488 case WTAP_ERR_SHORT_WRITE:
4489 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4490 "A full header couldn't be written to the file \"%s\".",
4494 case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
4495 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4496 "Gzip compression not supported by this file type.");
4500 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4501 "The file \"%s\" could not be %s: %s.",
4503 for_writing ? "created" : "opened",
4504 wtap_strerror(err));
4509 open_failure_alert_box(filename, err, for_writing);
4514 file_rename_error_message(int err)
4517 static char errmsg_errno[1024+1];
4522 errmsg = "The path to the file \"%s\" doesn't exist.";
4526 errmsg = "You don't have permission to move the capture file to \"%s\".";
4530 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4531 "The file \"%%s\" could not be moved: %s.",
4532 wtap_strerror(err));
4533 errmsg = errmsg_errno;
4540 cf_read_error_message(int err, gchar *err_info)
4542 static char errmsg_errno[1024+1];
4546 case WTAP_ERR_UNSUPPORTED_ENCAP:
4547 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4548 "The file \"%%s\" has a packet with a network type that Wireshark doesn't support.\n(%s)",
4553 case WTAP_ERR_BAD_RECORD:
4554 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4555 "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
4556 wtap_strerror(err), err_info);
4561 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4562 "An error occurred while reading from the file \"%%s\": %s.",
4563 wtap_strerror(err));
4566 return errmsg_errno;
4570 cf_write_failure_alert_box(const char *filename, int err)
4573 /* Wiretap error. */
4574 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4575 "An error occurred while writing to the file \"%s\": %s.",
4576 filename, wtap_strerror(err));
4579 write_failure_alert_box(filename, err);
4583 /* Check for write errors - if the file is being written to an NFS server,
4584 a write error may not show up until the file is closed, as NFS clients
4585 might not send writes to the server until the "write()" call finishes,
4586 so that the write may fail on the server but the "write()" may succeed. */
4588 cf_close_failure_alert_box(const char *filename, int err)
4591 /* Wiretap error. */
4594 case WTAP_ERR_CANT_CLOSE:
4595 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4596 "The file \"%s\" couldn't be closed for some unknown reason.",
4600 case WTAP_ERR_SHORT_WRITE:
4601 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4602 "Not all the packets could be written to the file \"%s\".",
4607 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4608 "An error occurred while closing the file \"%s\": %s.",
4609 filename, wtap_strerror(err));
4614 We assume that a close error from the OS is really a write error. */
4615 write_failure_alert_box(filename, err);
4619 /* Reload the current capture file. */
4621 cf_reload(capture_file *cf) {
4623 gboolean is_tempfile;
4626 /* If the file could be opened, "cf_open()" calls "cf_close()"
4627 to get rid of state for the old capture file before filling in state
4628 for the new capture file. "cf_close()" will remove the file if
4629 it's a temporary file; we don't want that to happen (for one thing,
4630 it'd prevent subsequent reopens from working). Remember whether it's
4631 a temporary file, mark it as not being a temporary file, and then
4632 reopen it as the type of file it was.
4634 Also, "cf_close()" will free "cf->filename", so we must make
4635 a copy of it first. */
4636 filename = g_strdup(cf->filename);
4637 is_tempfile = cf->is_tempfile;
4638 cf->is_tempfile = FALSE;
4639 if (cf_open(cf, filename, is_tempfile, &err) == CF_OK) {
4640 switch (cf_read(cf, FALSE)) {
4644 /* Just because we got an error, that doesn't mean we were unable
4645 to read any of the file; we handle what we could get from the
4649 case CF_READ_ABORTED:
4650 /* The user bailed out of re-reading the capture file; the
4651 capture file has been closed - just free the capture file name
4652 string and return (without changing the last containing
4658 /* The open failed, so "cf->is_tempfile" wasn't set to "is_tempfile".
4659 Instead, the file was left open, so we should restore "cf->is_tempfile"
4662 XXX - change the menu? Presumably "cf_open()" will do that;
4663 make sure it does! */
4664 cf->is_tempfile = is_tempfile;
4666 /* "cf_open()" made a copy of the file name we handed it, so
4667 we should free up our copy. */
4677 * indent-tabs-mode: nil
4680 * ex: set shiftwidth=2 tabstop=8 expandtab
4681 * :indentSize=2:tabSize=8:noTabs=true: