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"
62 #include <epan/prefs.h>
63 #include <epan/dfilter/dfilter.h>
64 #include <epan/epan_dissect.h>
66 #include <epan/dissectors/packet-data.h>
67 #include <epan/dissectors/packet-ber.h>
68 #include <epan/timestamp.h>
69 #include <epan/dfilter/dfilter-macro.h>
70 #include <wsutil/file_util.h>
71 #include <epan/strutil.h>
72 #include <epan/addr_resolv.h>
74 #include "ui/alert_box.h"
75 #include "ui/simple_dialog.h"
76 #include "ui/main_statusbar.h"
77 #include "ui/progress_dlg.h"
78 #include "ui/ui_util.h"
81 gboolean auto_scroll_live;
84 static guint32 cum_bytes;
85 static nstime_t first_ts;
86 static nstime_t prev_dis_ts;
87 static nstime_t prev_cap_ts;
89 static gulong computed_elapsed;
91 static void cf_reset_state(capture_file *cf);
93 static int read_packet(capture_file *cf, dfilter_t *dfcode,
94 gboolean filtering_tap_listeners, guint tap_flags, gint64 offset);
96 static void rescan_packets(capture_file *cf, const char *action, const char *action_item,
97 gboolean refilter, gboolean redissect);
104 static match_result match_protocol_tree(capture_file *cf, frame_data *fdata,
106 static void match_subtree_text(proto_node *node, gpointer data);
107 static match_result match_summary_line(capture_file *cf, frame_data *fdata,
109 static match_result match_ascii_and_unicode(capture_file *cf, frame_data *fdata,
111 static match_result match_ascii(capture_file *cf, frame_data *fdata,
113 static match_result match_unicode(capture_file *cf, frame_data *fdata,
115 static match_result match_binary(capture_file *cf, frame_data *fdata,
117 static match_result match_dfilter(capture_file *cf, frame_data *fdata,
119 static match_result match_marked(capture_file *cf, frame_data *fdata,
121 static match_result match_time_reference(capture_file *cf, frame_data *fdata,
123 static gboolean find_packet(capture_file *cf,
124 match_result (*match_function)(capture_file *, frame_data *, void *),
125 void *criterion, search_direction dir);
127 static void cf_open_failure_alert_box(const char *filename, int err,
128 gchar *err_info, gboolean for_writing,
130 static const char *file_rename_error_message(int err);
131 static void cf_close_failure_alert_box(const char *filename, int err);
132 static void ref_time_packets(capture_file *cf);
133 /* Update the progress bar this many times when reading a file. */
134 #define N_PROGBAR_UPDATES 100
135 /* We read around 200k/100ms don't update the progress bar more often than that */
136 #define MIN_QUANTUM 200000
137 #define MIN_NUMBER_OF_PACKET 1500
140 * We could probably use g_signal_...() instead of the callbacks below but that
141 * would require linking our CLI programs to libgobject and creating an object
142 * instance for the signals.
145 cf_callback_t cb_fct;
147 } cf_callback_data_t;
149 static GList *cf_callbacks = NULL;
152 cf_callback_invoke(int event, gpointer data)
154 cf_callback_data_t *cb;
155 GList *cb_item = cf_callbacks;
157 /* there should be at least one interested */
158 g_assert(cb_item != NULL);
160 while(cb_item != NULL) {
162 cb->cb_fct(event, data, cb->user_data);
163 cb_item = g_list_next(cb_item);
169 cf_callback_add(cf_callback_t func, gpointer user_data)
171 cf_callback_data_t *cb;
173 cb = g_malloc(sizeof(cf_callback_data_t));
175 cb->user_data = user_data;
177 cf_callbacks = g_list_append(cf_callbacks, cb);
181 cf_callback_remove(cf_callback_t func)
183 cf_callback_data_t *cb;
184 GList *cb_item = cf_callbacks;
186 while(cb_item != NULL) {
188 if(cb->cb_fct == func) {
189 cf_callbacks = g_list_remove(cf_callbacks, cb);
193 cb_item = g_list_next(cb_item);
196 g_assert_not_reached();
200 cf_timestamp_auto_precision(capture_file *cf)
203 int prec = timestamp_get_precision();
206 /* don't try to get the file's precision if none is opened */
207 if(cf->state == FILE_CLOSED) {
211 /* if we are in auto mode, set precision of current file */
212 if(prec == TS_PREC_AUTO ||
213 prec == TS_PREC_AUTO_SEC ||
214 prec == TS_PREC_AUTO_DSEC ||
215 prec == TS_PREC_AUTO_CSEC ||
216 prec == TS_PREC_AUTO_MSEC ||
217 prec == TS_PREC_AUTO_USEC ||
218 prec == TS_PREC_AUTO_NSEC)
220 switch(wtap_file_tsprecision(cf->wth)) {
221 case(WTAP_FILE_TSPREC_SEC):
222 timestamp_set_precision(TS_PREC_AUTO_SEC);
224 case(WTAP_FILE_TSPREC_DSEC):
225 timestamp_set_precision(TS_PREC_AUTO_DSEC);
227 case(WTAP_FILE_TSPREC_CSEC):
228 timestamp_set_precision(TS_PREC_AUTO_CSEC);
230 case(WTAP_FILE_TSPREC_MSEC):
231 timestamp_set_precision(TS_PREC_AUTO_MSEC);
233 case(WTAP_FILE_TSPREC_USEC):
234 timestamp_set_precision(TS_PREC_AUTO_USEC);
236 case(WTAP_FILE_TSPREC_NSEC):
237 timestamp_set_precision(TS_PREC_AUTO_NSEC);
240 g_assert_not_reached();
243 /* Set the column widths of those columns that show the time in
244 "command-line-specified" format. */
245 for (i = 0; i < cf->cinfo.num_cols; i++) {
246 if (col_has_time_fmt(&cf->cinfo, i)) {
247 new_packet_list_resize_column(i);
253 cf_get_computed_elapsed(void)
255 return computed_elapsed;
258 static void reset_elapsed(void)
260 computed_elapsed = 0;
263 static void compute_elapsed(GTimeVal *start_time)
268 g_get_current_time(&time_now);
270 delta_time = (time_now.tv_sec - start_time->tv_sec) * 1e6 +
271 time_now.tv_usec - start_time->tv_usec;
273 computed_elapsed = (gulong) (delta_time / 1000); /* ms */
277 cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
282 wth = wtap_open_offline(fname, err, &err_info, TRUE);
286 /* The open succeeded. Close whatever capture file we had open,
287 and fill in the information for this file. */
290 /* Cleanup all data structures used for dissection. */
291 cleanup_dissection();
292 /* Initialize all data structures used for dissection. */
295 /* We're about to start reading the file. */
296 cf->state = FILE_READ_IN_PROGRESS;
301 /* Set the file name because we need it to set the follow stream filter.
302 XXX - is that still true? We need it for other reasons, though,
304 cf->filename = g_strdup(fname);
306 /* Indicate whether it's a permanent or temporary file. */
307 cf->is_tempfile = is_tempfile;
309 /* No user changes yet. */
310 cf->unsaved_changes = FALSE;
314 cf->cd_t = wtap_file_type(cf->wth);
316 cf->displayed_count = 0;
317 cf->marked_count = 0;
318 cf->ignored_count = 0;
319 cf->ref_time_count = 0;
320 cf->drops_known = FALSE;
322 cf->snap = wtap_snapshot_length(cf->wth);
324 /* Snapshot length not known. */
325 cf->has_snap = FALSE;
326 cf->snap = WTAP_MAX_PACKET_SIZE;
330 /* Allocate a frame_data_sequence for the frames in this file */
331 cf->frames = new_frame_data_sequence();
333 nstime_set_zero(&cf->elapsed_time);
334 nstime_set_unset(&first_ts);
335 nstime_set_unset(&prev_dis_ts);
336 nstime_set_unset(&prev_cap_ts);
339 /* Adjust timestamp precision if auto is selected, col width will be adjusted */
340 cf_timestamp_auto_precision(cf);
342 new_packet_list_queue_draw();
343 fileset_file_opened(fname);
345 if(cf->cd_t == WTAP_FILE_BER) {
346 /* tell the BER dissector the file name */
347 ber_set_filename(cf->filename);
350 wtap_set_cb_new_ipv4(cf->wth, add_ipv4_name);
351 wtap_set_cb_new_ipv6(cf->wth, (wtap_new_ipv6_callback_t) add_ipv6_name);
356 cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
362 * Reset the state for the currently closed file, but don't do the
363 * UI callbacks; this is for use in "cf_open()", where we don't
364 * want the UI to go from "file open" to "file closed" back to
365 * "file open", we want it to go from "old file open" to "new file
366 * open and being read".
368 * XXX - currently, cf_open() calls cf_close(), rather than
372 cf_reset_state(capture_file *cf)
374 /* Die if we're in the middle of reading a file. */
375 g_assert(cf->state != FILE_READ_IN_PROGRESS);
381 /* We have no file open... */
382 if (cf->filename != NULL) {
383 /* If it's a temporary file, remove it. */
385 ws_unlink(cf->filename);
386 g_free(cf->filename);
389 /* ...which means we have no changes to that file to save. */
390 cf->unsaved_changes = FALSE;
392 dfilter_free(cf->rfcode);
394 if (cf->frames != NULL) {
395 free_frame_data_sequence(cf->frames);
398 #ifdef WANT_PACKET_EDITOR
399 if (cf->edited_frames) {
400 g_tree_destroy(cf->edited_frames);
401 cf->edited_frames = NULL;
404 cf_unselect_packet(cf); /* nothing to select */
405 cf->first_displayed = 0;
406 cf->last_displayed = 0;
408 /* No frames, no frame selected, no field in that frame selected. */
410 cf->current_frame = 0;
412 cf->finfo_selected = NULL;
414 /* Clear the packet list. */
415 new_packet_list_freeze();
416 new_packet_list_clear();
417 new_packet_list_thaw();
420 nstime_set_zero(&cf->elapsed_time);
422 reset_tap_listeners();
424 /* We have no file open. */
425 cf->state = FILE_CLOSED;
427 fileset_file_closed();
430 /* Reset everything to a pristine state */
432 cf_close(capture_file *cf)
434 if(cf->state != FILE_CLOSED) {
435 cf_callback_invoke(cf_cb_file_closing, cf);
437 /* close things, if not already closed before */
438 color_filters_cleanup();
440 cleanup_dissection();
442 cf_callback_invoke(cf_cb_file_closed, cf);
446 /* an out of memory exception occured, wait for a user button press to exit */
447 static void outofmemory_cb(gpointer dialog _U_, gint btn _U_, gpointer data _U_)
453 calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos, gchar *status_str, gulong status_size)
457 progbar_val = (gfloat) file_pos / (gfloat) size;
458 if (progbar_val > 1.0) {
460 /* The file probably grew while we were reading it.
461 * Update file size, and try again.
463 size = wtap_file_size(cf->wth, NULL);
466 progbar_val = (gfloat) file_pos / (gfloat) size;
468 /* If it's still > 1, either "wtap_file_size()" failed (in which
469 * case there's not much we can do about it), or the file
470 * *shrank* (in which case there's not much we can do about
471 * it); just clip the progress value at 1.0.
473 if (progbar_val > 1.0f)
477 g_snprintf(status_str, status_size,
478 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
479 file_pos / 1024, size / 1024);
485 cf_read(capture_file *cf, gboolean reloading)
491 char errmsg_errno[1024+1];
494 progdlg_t *volatile progbar = NULL;
496 volatile gint64 size;
497 volatile float progbar_val;
499 gchar status_str[100];
500 volatile gint64 progbar_nextstep;
501 volatile gint64 progbar_quantum;
503 gboolean filtering_tap_listeners;
505 volatile int count = 0;
507 volatile int displayed_once = 0;
511 /* Compile the current display filter.
512 * We assume this will not fail since cf->dfilter is only set in
513 * cf_filter IFF the filter was valid.
515 compiled = dfilter_compile(cf->dfilter, &dfcode);
516 g_assert(!cf->dfilter || (compiled && dfcode));
518 /* Do we have any tap listeners with filters? */
519 filtering_tap_listeners = have_filtering_tap_listeners();
521 /* Get the union of the flags for all tap listeners. */
522 tap_flags = union_of_tap_listener_flags();
524 reset_tap_listeners();
526 name_ptr = g_filename_display_basename(cf->filename);
529 cf_callback_invoke(cf_cb_file_reload_started, cf);
531 cf_callback_invoke(cf_cb_file_read_started, cf);
533 /* Record whether the file is compressed. */
534 cf->iscompressed = wtap_iscompressed(cf->wth);
536 /* Find the size of the file. */
537 size = wtap_file_size(cf->wth, NULL);
539 /* Update the progress bar when it gets to this value. */
540 progbar_nextstep = 0;
541 /* When we reach the value that triggers a progress bar update,
542 bump that value by this amount. */
544 progbar_quantum = size/N_PROGBAR_UPDATES;
545 if (progbar_quantum < MIN_QUANTUM)
546 progbar_quantum = MIN_QUANTUM;
549 /* Progress so far. */
552 /* The packet list window will be empty untill the file is completly loaded */
553 new_packet_list_freeze();
556 g_get_current_time(&start_time);
558 while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
561 file_pos = wtap_read_so_far(cf->wth);
563 /* Create the progress bar if necessary.
564 * Check whether 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, file_pos, status_str, sizeof(status_str));
569 progbar = delayed_create_progress_dlg("Reloading", name_ptr,
570 TRUE, &stop_flag, &start_time, progbar_val);
572 progbar = delayed_create_progress_dlg("Loading", 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 (file_pos >= progbar_nextstep) {
582 if (progbar != NULL) {
583 progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
584 /* update the packet bar content on the first run or frequently on very large files */
586 if (progbar_quantum > 500000 || displayed_once == 0) {
587 if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->count != 0) {
589 packets_bar_update();
592 #endif /* HAVE_LIBPCAP */
593 update_progress_dlg(progbar, progbar_val, status_str);
595 progbar_nextstep += progbar_quantum;
600 /* Well, the user decided to abort the read. He/She will be warned and
601 it might be enough for him/her to work with the already loaded
603 This is especially true for very large capture files, where you don't
604 want to wait loading the whole file (which may last minutes or even
605 hours even on fast machines) just to see that it was the wrong file. */
609 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
611 CATCH(OutOfMemoryError) {
614 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
615 "%sOut Of Memory!%s\n"
617 "Sorry, but Wireshark has to terminate now!\n"
619 "Some infos / workarounds can be found at:\n"
620 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
621 simple_dialog_primary_start(), simple_dialog_primary_end());
622 /* we have to terminate, as we cannot recover from the memory error */
623 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
625 main_window_update();
626 /* XXX - how to avoid a busy wait? */
634 /* Free the display name */
637 /* Cleanup and release all dfilter resources */
639 dfilter_free(dfcode);
642 /* We're done reading the file; destroy the progress bar if it was created. */
644 destroy_progress_dlg(progbar);
646 /* We're done reading sequentially through the file. */
647 cf->state = FILE_READ_DONE;
649 /* Close the sequential I/O side, to free up memory it requires. */
650 wtap_sequential_close(cf->wth);
652 /* Allow the protocol dissectors to free up memory that they
653 * don't need after the sequential run-through of the packets. */
654 postseq_cleanup_all_protocols();
656 /* compute the time it took to load the file */
657 compute_elapsed(&start_time);
659 /* Set the file encapsulation type now; we don't know what it is until
660 we've looked at all the packets, as we don't know until then whether
661 there's more than one type (and thus whether it's
662 WTAP_ENCAP_PER_PACKET). */
663 cf->lnk_t = wtap_file_encap(cf->wth);
665 cf->current_frame = frame_data_sequence_find(cf->frames, cf->first_displayed);
668 new_packet_list_thaw();
670 cf_callback_invoke(cf_cb_file_reload_finished, cf);
672 cf_callback_invoke(cf_cb_file_read_finished, cf);
674 /* If we have any displayed packets to select, select the first of those
675 packets by making the first row the selected row. */
676 if (cf->first_displayed != 0){
677 new_packet_list_select_first_row();
681 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
682 "%sFile loading was cancelled!%s\n"
684 "The remaining packets in the file were discarded.\n"
686 "As a lot of packets from the original file will be missing,\n"
687 "remember to be careful when saving the current content to a file.\n",
688 simple_dialog_primary_start(), simple_dialog_primary_end());
689 return CF_READ_ERROR;
693 /* Put up a message box noting that the read failed somewhere along
694 the line. Don't throw out the stuff we managed to read, though,
698 case WTAP_ERR_UNSUPPORTED:
699 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
700 "The capture file contains record data that TShark doesn't support.\n(%s)",
703 errmsg = errmsg_errno;
706 case WTAP_ERR_UNSUPPORTED_ENCAP:
707 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
708 "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
711 errmsg = errmsg_errno;
714 case WTAP_ERR_CANT_READ:
715 errmsg = "An attempt to read from the capture file failed for"
716 " some unknown reason.";
719 case WTAP_ERR_SHORT_READ:
720 errmsg = "The capture file appears to have been cut short"
721 " in the middle of a packet.";
724 case WTAP_ERR_BAD_FILE:
725 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
726 "The capture file appears to be damaged or corrupt.\n(%s)",
729 errmsg = errmsg_errno;
732 case WTAP_ERR_DECOMPRESS:
733 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
734 "The compressed capture file appears to be damaged or corrupt.\n"
737 errmsg = errmsg_errno;
741 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
742 "An error occurred while reading the"
743 " capture file: %s.", wtap_strerror(err));
744 errmsg = errmsg_errno;
747 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", errmsg);
748 return CF_READ_ERROR;
755 cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
757 cf_status_t cf_status;
759 cf_status = cf_open(cf, fname, is_tempfile, err);
764 cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
766 gint64 data_offset = 0;
768 volatile int newly_displayed_packets = 0;
770 gboolean filtering_tap_listeners;
774 /* Compile the current display filter.
775 * We assume this will not fail since cf->dfilter is only set in
776 * cf_filter IFF the filter was valid.
778 compiled = dfilter_compile(cf->dfilter, &dfcode);
779 g_assert(!cf->dfilter || (compiled && dfcode));
781 /* Do we have any tap listeners with filters? */
782 filtering_tap_listeners = have_filtering_tap_listeners();
784 /* Get the union of the flags for all tap listeners. */
785 tap_flags = union_of_tap_listener_flags();
789 new_packet_list_check_end();
790 /* Don't freeze/thaw the list when doing live capture */
791 /*new_packet_list_freeze();*/
793 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
795 while (to_read != 0) {
796 wtap_cleareof(cf->wth);
797 if (!wtap_read(cf->wth, err, &err_info, &data_offset)) {
800 if (cf->state == FILE_READ_ABORTED) {
801 /* Well, the user decided to exit Wireshark. Break out of the
802 loop, and let the code below (which is called even if there
803 aren't any packets left to read) exit. */
807 if (read_packet(cf, dfcode, filtering_tap_listeners, tap_flags,
808 data_offset) != -1) {
809 newly_displayed_packets++;
812 CATCH(OutOfMemoryError) {
815 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
816 "%sOut Of Memory!%s\n"
818 "Sorry, but Wireshark has to terminate now!\n"
820 "The capture file is not lost, it can be found at:\n"
823 "Some infos / workarounds can be found at:\n"
824 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
825 simple_dialog_primary_start(), simple_dialog_primary_end(), cf->filename);
826 /* we have to terminate, as we cannot recover from the memory error */
827 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
829 main_window_update();
830 /* XXX - how to avoid a busy wait? */
833 /* Don't freeze/thaw the list when doing live capture */
834 /*new_packet_list_thaw();*/
835 return CF_READ_ABORTED;
841 /* Cleanup and release all dfilter resources */
843 dfilter_free(dfcode);
846 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u",
847 cf->count, cf->state, *err);*/
849 /* Don't freeze/thaw the list when doing live capture */
850 /*new_packet_list_thaw();*/
851 /* With the new packet list the first packet
852 * isn't automatically selected.
854 if(!cf->current_frame)
855 new_packet_list_select_first_row();
857 /* moving to the end of the packet list - if the user requested so and
858 we have some new packets. */
859 if (newly_displayed_packets && auto_scroll_live && cf->count != 0)
860 new_packet_list_moveto_end();
862 if (cf->state == FILE_READ_ABORTED) {
863 /* Well, the user decided to exit Wireshark. Return CF_READ_ABORTED
864 so that our caller can kill off the capture child process;
865 this will cause an EOF on the pipe from the child, so
866 "cf_finish_tail()" will be called, and it will clean up
868 return CF_READ_ABORTED;
869 } else if (*err != 0) {
870 /* We got an error reading the capture file.
871 XXX - pop up a dialog box instead? */
872 g_warning("Error \"%s\" while reading: \"%s\"\n",
873 wtap_strerror(*err), cf->filename);
875 return CF_READ_ERROR;
881 cf_fake_continue_tail(capture_file *cf) {
882 cf->state = FILE_READ_DONE;
886 cf_finish_tail(capture_file *cf, int *err)
891 gboolean filtering_tap_listeners;
895 /* Compile the current display filter.
896 * We assume this will not fail since cf->dfilter is only set in
897 * cf_filter IFF the filter was valid.
899 compiled = dfilter_compile(cf->dfilter, &dfcode);
900 g_assert(!cf->dfilter || (compiled && dfcode));
902 /* Do we have any tap listeners with filters? */
903 filtering_tap_listeners = have_filtering_tap_listeners();
905 /* Get the union of the flags for all tap listeners. */
906 tap_flags = union_of_tap_listener_flags();
908 if(cf->wth == NULL) {
910 return CF_READ_ERROR;
913 new_packet_list_check_end();
914 /* Don't freeze/thaw the list when doing live capture */
915 /*new_packet_list_freeze();*/
917 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
918 if (cf->state == FILE_READ_ABORTED) {
919 /* Well, the user decided to abort the read. Break out of the
920 loop, and let the code below (which is called even if there
921 aren't any packets left to read) exit. */
924 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
927 /* Cleanup and release all dfilter resources */
929 dfilter_free(dfcode);
932 /* Don't freeze/thaw the list when doing live capture */
933 /*new_packet_list_thaw();*/
935 if (cf->state == FILE_READ_ABORTED) {
936 /* Well, the user decided to abort the read. We're only called
937 when the child capture process closes the pipe to us (meaning
938 it's probably exited), so we can just close the capture
939 file; we return CF_READ_ABORTED so our caller can do whatever
940 is appropriate when that happens. */
942 return CF_READ_ABORTED;
945 if (auto_scroll_live && cf->count != 0)
946 new_packet_list_moveto_end();
948 /* We're done reading sequentially through the file. */
949 cf->state = FILE_READ_DONE;
951 /* We're done reading sequentially through the file; close the
952 sequential I/O side, to free up memory it requires. */
953 wtap_sequential_close(cf->wth);
955 /* Allow the protocol dissectors to free up memory that they
956 * don't need after the sequential run-through of the packets. */
957 postseq_cleanup_all_protocols();
959 /* Set the file encapsulation type now; we don't know what it is until
960 we've looked at all the packets, as we don't know until then whether
961 there's more than one type (and thus whether it's
962 WTAP_ENCAP_PER_PACKET). */
963 cf->lnk_t = wtap_file_encap(cf->wth);
966 /* We got an error reading the capture file.
967 XXX - pop up a dialog box? */
968 return CF_READ_ERROR;
973 #endif /* HAVE_LIBPCAP */
976 cf_get_display_name(capture_file *cf)
980 /* Return a name to use in displays */
981 if (!cf->is_tempfile) {
982 /* Get the last component of the file name, and use that. */
984 displayname = g_filename_display_basename(cf->filename);
986 displayname=g_strdup("(No file)");
989 /* The file we read is a temporary file from a live capture or
990 a merge operation; we don't mention its name, but, if it's
991 from a capture, give the source of the capture. */
993 displayname = g_strdup(cf->source);
995 displayname = g_strdup("(Untitled)");
1001 void cf_set_tempfile_source(capture_file *cf, gchar *source) {
1007 cf->source = g_strdup(source);
1009 cf->source = g_strdup("");
1013 const gchar *cf_get_tempfile_source(capture_file *cf) {
1021 /* XXX - use a macro instead? */
1023 cf_get_packet_count(capture_file *cf)
1028 /* XXX - use a macro instead? */
1030 cf_set_packet_count(capture_file *cf, int packet_count)
1032 cf->count = packet_count;
1035 /* XXX - use a macro instead? */
1037 cf_is_tempfile(capture_file *cf)
1039 return cf->is_tempfile;
1042 void cf_set_tempfile(capture_file *cf, gboolean is_tempfile)
1044 cf->is_tempfile = is_tempfile;
1048 /* XXX - use a macro instead? */
1049 void cf_set_drops_known(capture_file *cf, gboolean drops_known)
1051 cf->drops_known = drops_known;
1054 /* XXX - use a macro instead? */
1055 void cf_set_drops(capture_file *cf, guint32 drops)
1060 /* XXX - use a macro instead? */
1061 gboolean cf_get_drops_known(capture_file *cf)
1063 return cf->drops_known;
1066 /* XXX - use a macro instead? */
1067 guint32 cf_get_drops(capture_file *cf)
1072 void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
1074 cf->rfcode = rfcode;
1078 find_and_mark_frame_depended_upon(gpointer data, gpointer user_data)
1080 frame_data *dependent_fd;
1081 guint32 dependent_frame = GPOINTER_TO_UINT(data);
1082 capture_file *cf = (capture_file *)user_data;
1084 dependent_fd = frame_data_sequence_find(cf->frames, dependent_frame);
1085 dependent_fd->flags.dependent_of_displayed = 1;
1089 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
1090 dfilter_t *dfcode, gboolean filtering_tap_listeners,
1092 union wtap_pseudo_header *pseudo_header, const guchar *buf,
1094 gboolean add_to_packet_list)
1096 gboolean create_proto_tree = FALSE;
1101 cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
1103 frame_data_set_before_dissect(fdata, &cf->elapsed_time,
1104 &first_ts, &prev_dis_ts, &prev_cap_ts);
1107 + we have a display filter and are re-applying it;
1108 + we have tap listeners with filters;
1109 + we have tap listeners that require a protocol tree;
1111 allocate a protocol tree root node, so that we'll construct
1112 a protocol tree against which a filter expression can be
1114 if ((dfcode != NULL && refilter) ||
1115 filtering_tap_listeners || (tap_flags & TL_REQUIRES_PROTO_TREE))
1116 create_proto_tree = TRUE;
1118 /* Dissect the frame. */
1119 epan_dissect_init(&edt, create_proto_tree, FALSE);
1121 if (dfcode != NULL && refilter) {
1122 epan_dissect_prime_dfilter(&edt, dfcode);
1125 tap_queue_init(&edt);
1126 epan_dissect_run(&edt, pseudo_header, buf, fdata, cinfo);
1127 tap_push_tapped_queue(&edt);
1129 /* If we have a display filter, apply it if we're refiltering, otherwise
1130 leave the "passed_dfilter" flag alone.
1132 If we don't have a display filter, set "passed_dfilter" to 1. */
1133 if (dfcode != NULL) {
1135 fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, &edt) ? 1 : 0;
1137 if (fdata->flags.passed_dfilter) {
1138 /* This frame passed the display filter but it may depend on other
1139 * (potentially not displayed) frames. Find those frames and mark them
1142 g_slist_foreach(edt.pi.dependent_frames, find_and_mark_frame_depended_upon, cf);
1146 fdata->flags.passed_dfilter = 1;
1148 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1149 cf->displayed_count++;
1151 if (add_to_packet_list) {
1152 /* We fill the needed columns from new_packet_list */
1153 row = new_packet_list_append(cinfo, fdata, &edt.pi);
1156 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1158 frame_data_set_after_dissect(fdata, &cum_bytes, &prev_dis_ts);
1160 /* If we haven't yet seen the first frame, this is it.
1162 XXX - we must do this before we add the row to the display,
1163 as, if the display's GtkCList's selection mode is
1164 GTK_SELECTION_BROWSE, when the first entry is added to it,
1165 "cf_select_packet()" will be called, and it will fetch the row
1166 data for the 0th row, and will get a null pointer rather than
1167 "fdata", as "gtk_clist_append()" won't yet have returned and
1168 thus "gtk_clist_set_row_data()" won't yet have been called.
1170 We thus need to leave behind bread crumbs so that
1171 "cf_select_packet()" can find this frame. See the comment
1172 in "cf_select_packet()". */
1173 if (cf->first_displayed == 0)
1174 cf->first_displayed = fdata->num;
1176 /* This is the last frame we've seen so far. */
1177 cf->last_displayed = fdata->num;
1180 epan_dissect_cleanup(&edt);
1184 /* read in a new packet */
1185 /* returns the row of the new packet in the packet list or -1 if not displayed */
1187 read_packet(capture_file *cf, dfilter_t *dfcode,
1188 gboolean filtering_tap_listeners, guint tap_flags, gint64 offset)
1190 const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
1191 union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
1192 const guchar *buf = wtap_buf_ptr(cf->wth);
1199 /* The frame number of this packet is one more than the count of
1200 frames in this packet. */
1201 framenum = cf->count + 1;
1203 frame_data_init(&fdlocal, framenum, phdr, offset, cum_bytes);
1208 epan_dissect_init(&edt, TRUE, FALSE);
1209 epan_dissect_prime_dfilter(&edt, cf->rfcode);
1210 epan_dissect_run(&edt, pseudo_header, buf, &fdlocal, NULL);
1211 passed = dfilter_apply_edt(cf->rfcode, &edt);
1212 epan_dissect_cleanup(&edt);
1216 /* This does a shallow copy of fdlocal, which is good enough. */
1217 fdata = frame_data_sequence_add(cf->frames, &fdlocal);
1220 cf->f_datalen = offset + fdlocal.cap_len;
1222 if (!cf->redissecting) {
1223 row = add_packet_to_packet_list(fdata, cf, dfcode,
1224 filtering_tap_listeners, tap_flags,
1225 pseudo_header, buf, TRUE, TRUE);
1233 cf_merge_files(char **out_filenamep, int in_file_count,
1234 char *const *in_filenames, int file_type, gboolean do_append)
1236 merge_in_file_t *in_files, *in_file;
1241 int open_err, read_err, write_err, close_err;
1245 char errmsg_errno[1024+1];
1247 gboolean got_read_error = FALSE, got_write_error = FALSE;
1249 progdlg_t *progbar = NULL;
1251 gint64 f_len, file_pos;
1253 GTimeVal start_time;
1254 gchar status_str[100];
1255 gint64 progbar_nextstep;
1256 gint64 progbar_quantum;
1258 /* open the input files */
1259 if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
1260 &open_err, &err_info, &err_fileno)) {
1262 cf_open_failure_alert_box(in_filenames[err_fileno], open_err, err_info,
1267 if (*out_filenamep != NULL) {
1268 out_filename = *out_filenamep;
1269 out_fd = ws_open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
1273 out_fd = create_tempfile(&tmpname, "wireshark");
1276 out_filename = g_strdup(tmpname);
1277 *out_filenamep = out_filename;
1281 merge_close_in_files(in_file_count, in_files);
1283 cf_open_failure_alert_box(out_filename, open_err, NULL, TRUE, file_type);
1287 pdh = wtap_dump_fdopen(out_fd, file_type,
1288 merge_select_frame_type(in_file_count, in_files),
1289 merge_max_snapshot_length(in_file_count, in_files),
1290 FALSE /* compressed */, &open_err);
1293 merge_close_in_files(in_file_count, in_files);
1295 cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
1300 /* Get the sum of the sizes of all the files. */
1302 for (i = 0; i < in_file_count; i++)
1303 f_len += in_files[i].size;
1305 /* Update the progress bar when it gets to this value. */
1306 progbar_nextstep = 0;
1307 /* When we reach the value that triggers a progress bar update,
1308 bump that value by this amount. */
1309 progbar_quantum = f_len/N_PROGBAR_UPDATES;
1310 /* Progress so far. */
1314 g_get_current_time(&start_time);
1316 /* do the merge (or append) */
1319 in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
1322 in_file = merge_read_packet(in_file_count, in_files, &read_err,
1324 if (in_file == NULL) {
1329 if (read_err != 0) {
1330 /* I/O error reading from in_file */
1331 got_read_error = TRUE;
1335 /* Get the sum of the data offsets in all of the files. */
1337 for (i = 0; i < in_file_count; i++)
1338 data_offset += in_files[i].data_offset;
1340 /* Create the progress bar if necessary.
1341 We check on every iteration of the loop, so that it takes no
1342 longer than the standard time to create it (otherwise, for a
1343 large file, we might take considerably longer than that standard
1344 time in order to get to the next progress bar step). */
1345 if (progbar == NULL) {
1346 progbar = delayed_create_progress_dlg("Merging", "files",
1347 FALSE, &stop_flag, &start_time, progbar_val);
1350 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1351 when we update it, we have to run the GTK+ main loop to get it
1352 to repaint what's pending, and doing so may involve an "ioctl()"
1353 to see if there's any pending input from an X server, and doing
1354 that for every packet can be costly, especially on a big file. */
1355 if (data_offset >= progbar_nextstep) {
1356 /* Get the sum of the seek positions in all of the files. */
1358 for (i = 0; i < in_file_count; i++)
1359 file_pos += wtap_read_so_far(in_files[i].wth);
1360 progbar_val = (gfloat) file_pos / (gfloat) f_len;
1361 if (progbar_val > 1.0f) {
1362 /* Some file probably grew while we were reading it.
1363 That "shouldn't happen", so we'll just clip the progress
1367 if (progbar != NULL) {
1368 g_snprintf(status_str, sizeof(status_str),
1369 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
1370 file_pos / 1024, f_len / 1024);
1371 update_progress_dlg(progbar, progbar_val, status_str);
1373 progbar_nextstep += progbar_quantum;
1377 /* Well, the user decided to abort the merge. */
1381 if (!wtap_dump(pdh, wtap_phdr(in_file->wth), wtap_pseudoheader(in_file->wth),
1382 wtap_buf_ptr(in_file->wth), &write_err)) {
1383 got_write_error = TRUE;
1388 /* We're done merging the files; destroy the progress bar if it was created. */
1389 if (progbar != NULL)
1390 destroy_progress_dlg(progbar);
1392 merge_close_in_files(in_file_count, in_files);
1393 if (!got_read_error && !got_write_error) {
1394 if (!wtap_dump_close(pdh, &write_err))
1395 got_write_error = TRUE;
1397 wtap_dump_close(pdh, &close_err);
1399 if (got_read_error) {
1401 * Find the file on which we got the error, and report the error.
1403 for (i = 0; i < in_file_count; i++) {
1404 if (in_files[i].state == GOT_ERROR) {
1405 /* Put up a message box noting that a read failed somewhere along
1409 case WTAP_ERR_UNSUPPORTED_ENCAP:
1410 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1411 "The capture file %%s has a packet with a network type that Wireshark doesn't support.\n(%s)",
1414 errmsg = errmsg_errno;
1417 case WTAP_ERR_CANT_READ:
1418 errmsg = "An attempt to read from the capture file %s failed for"
1419 " some unknown reason.";
1422 case WTAP_ERR_SHORT_READ:
1423 errmsg = "The capture file %s appears to have been cut short"
1424 " in the middle of a packet.";
1427 case WTAP_ERR_BAD_FILE:
1428 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1429 "The capture file %%s appears to be damaged or corrupt.\n(%s)",
1432 errmsg = errmsg_errno;
1435 case WTAP_ERR_DECOMPRESS:
1436 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1437 "The compressed capture file %%s appears to be damaged or corrupt.\n"
1440 errmsg = errmsg_errno;
1444 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1445 "An error occurred while reading the"
1446 " capture file %%s: %s.", wtap_strerror(read_err));
1447 errmsg = errmsg_errno;
1450 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg, in_files[i].filename);
1455 if (got_write_error) {
1456 /* Put up an alert box for the write error. */
1457 if (write_err < 0) {
1458 /* Wiretap error. */
1459 switch (write_err) {
1461 case WTAP_ERR_UNSUPPORTED_ENCAP:
1463 * This is a problem with the particular frame we're writing;
1464 * note that, and give the frame number.
1466 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1467 "Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.",
1468 in_file->packet_num, in_file->filename,
1469 wtap_file_type_string(file_type));
1473 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1474 "An error occurred while writing to the file \"%s\": %s.",
1475 out_filename, wtap_strerror(write_err));
1480 write_failure_alert_box(out_filename, write_err);
1484 if (got_read_error || got_write_error || stop_flag) {
1485 /* Callers aren't expected to treat an error or an explicit abort
1486 differently - we put up error dialogs ourselves, so they don't
1494 cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
1496 const char *filter_new = dftext ? dftext : "";
1497 const char *filter_old = cf->dfilter ? cf->dfilter : "";
1499 GTimeVal start_time;
1501 /* if new filter equals old one, do nothing unless told to do so */
1502 if (!force && strcmp(filter_new, filter_old) == 0) {
1508 if (dftext == NULL) {
1509 /* The new filter is an empty filter (i.e., display all packets).
1510 * so leave dfcode==NULL
1514 * We have a filter; make a copy of it (as we'll be saving it),
1515 * and try to compile it.
1517 dftext = g_strdup(dftext);
1518 if (!dfilter_compile(dftext, &dfcode)) {
1519 /* The attempt failed; report an error. */
1520 gchar *safe_dftext = simple_dialog_format_message(dftext);
1521 gchar *safe_dfilter_error_msg = simple_dialog_format_message(
1523 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1526 "The following display filter isn't a valid display filter:\n%s\n"
1527 "See the help for a description of the display filter syntax.",
1528 simple_dialog_primary_start(), safe_dfilter_error_msg,
1529 simple_dialog_primary_end(), safe_dftext);
1530 g_free(safe_dfilter_error_msg);
1531 g_free(safe_dftext);
1537 if (dfcode == NULL) {
1538 /* Yes - free the filter text, and set it to null. */
1544 /* We have a valid filter. Replace the current filter. */
1545 g_free(cf->dfilter);
1546 cf->dfilter = dftext;
1547 g_get_current_time(&start_time);
1550 /* Now rescan the packet list, applying the new filter, but not
1551 throwing away information constructed on a previous pass. */
1552 if (dftext == NULL) {
1553 rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
1555 rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
1558 /* Cleanup and release all dfilter resources */
1559 dfilter_free(dfcode);
1565 cf_reftime_packets(capture_file *cf)
1568 ref_time_packets(cf);
1572 cf_redissect_packets(capture_file *cf)
1574 rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
1578 cf_read_frame_r(capture_file *cf, frame_data *fdata,
1579 union wtap_pseudo_header *pseudo_header, guint8 *pd)
1583 char errmsg_errno[1024+1];
1585 #ifdef WANT_PACKET_EDITOR
1586 /* if fdata->file_off == -1 it means packet was edited, and we must find data inside edited_frames tree */
1587 if (G_UNLIKELY(fdata->file_off == -1)) {
1588 const modified_frame_data *frame = (const modified_frame_data *) g_tree_lookup(cf->edited_frames, GINT_TO_POINTER(fdata->num));
1591 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "fdata->file_off == -1, but can't find modified frame!");
1595 *pseudo_header = frame->ph;
1596 memcpy(pd, frame->pd, fdata->cap_len);
1601 if (!wtap_seek_read(cf->wth, fdata->file_off, pseudo_header, pd,
1602 fdata->cap_len, &err, &err_info)) {
1605 case WTAP_ERR_UNSUPPORTED_ENCAP:
1606 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1607 "The file \"%%s\" has a packet with a network type that Wireshark doesn't support.\n(%s)",
1612 case WTAP_ERR_BAD_FILE:
1613 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1614 "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
1615 wtap_strerror(err), err_info);
1620 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1621 "An error occurred while reading from the file \"%%s\": %s.",
1622 wtap_strerror(err));
1625 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg_errno, cf->filename);
1632 cf_read_frame(capture_file *cf, frame_data *fdata)
1634 return cf_read_frame_r(cf, fdata, &cf->pseudo_header, cf->pd);
1637 /* Rescan the list of packets, reconstructing the CList.
1639 "action" describes why we're doing this; it's used in the progress
1642 "action_item" describes what we're doing; it's used in the progress
1645 "refilter" is TRUE if we need to re-evaluate the filter expression.
1647 "redissect" is TRUE if we need to make the dissectors reconstruct
1648 any state information they have (because a preference that affects
1649 some dissector has changed, meaning some dissector might construct
1650 its state differently from the way it was constructed the last time). */
1652 rescan_packets(capture_file *cf, const char *action, const char *action_item,
1653 gboolean refilter, gboolean redissect)
1655 /* Rescan packets new packet list */
1658 progdlg_t *progbar = NULL;
1661 frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
1662 int selected_frame_num, preceding_frame_num, following_frame_num, prev_frame_num;
1663 gboolean selected_frame_seen;
1665 GTimeVal start_time;
1666 gchar status_str[100];
1667 int progbar_nextstep;
1668 int progbar_quantum;
1670 gboolean filtering_tap_listeners;
1672 gboolean add_to_packet_list = FALSE;
1675 /* Compile the current display filter.
1676 * We assume this will not fail since cf->dfilter is only set in
1677 * cf_filter IFF the filter was valid.
1679 compiled = dfilter_compile(cf->dfilter, &dfcode);
1680 g_assert(!cf->dfilter || (compiled && dfcode));
1682 /* Do we have any tap listeners with filters? */
1683 filtering_tap_listeners = have_filtering_tap_listeners();
1685 /* Get the union of the flags for all tap listeners. */
1686 tap_flags = union_of_tap_listener_flags();
1688 reset_tap_listeners();
1689 /* Which frame, if any, is the currently selected frame?
1690 XXX - should the selected frame or the focus frame be the "current"
1691 frame, that frame being the one from which "Find Frame" searches
1693 selected_frame = cf->current_frame;
1695 /* Mark frame num as not found */
1696 selected_frame_num = -1;
1698 /* Freeze the packet list while we redo it, so we don't get any
1699 screen updates while it happens. */
1700 new_packet_list_freeze();
1703 /* We need to re-initialize all the state information that protocols
1704 keep, because some preference that controls a dissector has changed,
1705 which might cause the state information to be constructed differently
1706 by that dissector. */
1708 /* We might receive new packets while redissecting, and we don't
1709 want to dissect those before their time. */
1710 cf->redissecting = TRUE;
1712 /* Cleanup all data structures used for dissection. */
1713 cleanup_dissection();
1714 /* Initialize all data structures used for dissection. */
1717 /* We need to redissect the packets so we have to discard our old
1718 * packet list store. */
1719 new_packet_list_clear();
1720 add_to_packet_list = TRUE;
1723 /* We don't yet know which will be the first and last frames displayed. */
1724 cf->first_displayed = 0;
1725 cf->last_displayed = 0;
1727 /* We currently don't display any packets */
1728 cf->displayed_count = 0;
1730 /* Iterate through the list of frames. Call a routine for each frame
1731 to check whether it should be displayed and, if so, add it to
1732 the display list. */
1733 nstime_set_unset(&first_ts);
1734 nstime_set_unset(&prev_dis_ts);
1735 nstime_set_unset(&prev_cap_ts);
1738 /* Update the progress bar when it gets to this value. */
1739 progbar_nextstep = 0;
1740 /* When we reach the value that triggers a progress bar update,
1741 bump that value by this amount. */
1742 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1743 /* Count of packets at which we've looked. */
1745 /* Progress so far. */
1749 g_get_current_time(&start_time);
1751 /* no previous row yet */
1752 prev_frame_num = -1;
1755 preceding_frame_num = -1;
1756 preceding_frame = NULL;
1757 following_frame_num = -1;
1758 following_frame = NULL;
1760 selected_frame_seen = FALSE;
1762 for (framenum = 1; framenum <= cf->count; framenum++) {
1763 fdata = frame_data_sequence_find(cf->frames, framenum);
1765 /* Create the progress bar if necessary.
1766 We check on every iteration of the loop, so that it takes no
1767 longer than the standard time to create it (otherwise, for a
1768 large file, we might take considerably longer than that standard
1769 time in order to get to the next progress bar step). */
1770 if (progbar == NULL)
1771 progbar = delayed_create_progress_dlg(action, action_item, TRUE,
1772 &stop_flag, &start_time,
1775 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1776 when we update it, we have to run the GTK+ main loop to get it
1777 to repaint what's pending, and doing so may involve an "ioctl()"
1778 to see if there's any pending input from an X server, and doing
1779 that for every packet can be costly, especially on a big file. */
1780 if (count >= progbar_nextstep) {
1781 /* let's not divide by zero. I should never be started
1782 * with count == 0, so let's assert that
1784 g_assert(cf->count > 0);
1785 progbar_val = (gfloat) count / cf->count;
1787 if (progbar != NULL) {
1788 g_snprintf(status_str, sizeof(status_str),
1789 "%4u of %u frames", count, cf->count);
1790 update_progress_dlg(progbar, progbar_val, status_str);
1793 progbar_nextstep += progbar_quantum;
1797 /* Well, the user decided to abort the filtering. Just stop.
1799 XXX - go back to the previous filter? Users probably just
1800 want not to wait for a filtering operation to finish;
1801 unless we cancel by having no filter, reverting to the
1802 previous filter will probably be even more expensive than
1803 continuing the filtering, as it involves going back to the
1804 beginning and filtering, and even with no filter we currently
1805 have to re-generate the entire clist, which is also expensive.
1807 I'm not sure what Network Monitor does, but it doesn't appear
1808 to give you an unfiltered display if you cancel. */
1815 /* Since all state for the frame was destroyed, mark the frame
1816 * as not visited, free the GSList referring to the state
1817 * data (the per-frame data itself was freed by
1818 * "init_dissection()"), and null out the GSList pointer. */
1819 fdata->flags.visited = 0;
1820 frame_data_cleanup(fdata);
1823 if (redissect || refilter) {
1824 /* If we're redissecting or refiltering then any frame dependencies
1825 * from the previous dissection/filtering are no longer valid.
1827 fdata->flags.dependent_of_displayed = 0;
1830 if (!cf_read_frame(cf, fdata))
1831 break; /* error reading the frame */
1833 /* If the previous frame is displayed, and we haven't yet seen the
1834 selected frame, remember that frame - it's the closest one we've
1835 yet seen before the selected frame. */
1836 if (prev_frame_num != -1 && !selected_frame_seen && prev_frame->flags.passed_dfilter) {
1837 preceding_frame_num = prev_frame_num;
1838 preceding_frame = prev_frame;
1840 add_packet_to_packet_list(fdata, cf, dfcode, filtering_tap_listeners,
1841 tap_flags, &cf->pseudo_header, cf->pd,
1843 add_to_packet_list);
1845 /* If this frame is displayed, and this is the first frame we've
1846 seen displayed after the selected frame, remember this frame -
1847 it's the closest one we've yet seen at or after the selected
1849 if (fdata->flags.passed_dfilter && selected_frame_seen && following_frame_num == -1) {
1850 following_frame_num = fdata->num;
1851 following_frame = fdata;
1853 if (fdata == selected_frame) {
1854 selected_frame_seen = TRUE;
1855 if (fdata->flags.passed_dfilter)
1856 selected_frame_num = fdata->num;
1859 /* Remember this frame - it'll be the previous frame
1860 on the next pass through the loop. */
1861 prev_frame_num = fdata->num;
1865 /* We are done redissecting the packet list. */
1866 cf->redissecting = FALSE;
1869 /* Clear out what remains of the visited flags and per-frame data
1872 XXX - that may cause various forms of bogosity when dissecting
1873 these frames, as they won't have been seen by this sequential
1874 pass, but the only alternative I see is to keep scanning them
1875 even though the user requested that the scan stop, and that
1876 would leave the user stuck with an Wireshark grinding on
1877 until it finishes. Should we just stick them with that? */
1878 for (; framenum <= cf->count; framenum++) {
1879 fdata = frame_data_sequence_find(cf->frames, framenum);
1880 fdata->flags.visited = 0;
1881 frame_data_cleanup(fdata);
1885 /* We're done filtering the packets; destroy the progress bar if it
1887 if (progbar != NULL)
1888 destroy_progress_dlg(progbar);
1890 /* Unfreeze the packet list. */
1891 if (!add_to_packet_list)
1892 new_packet_list_recreate_visible_rows();
1894 /* Compute the time it took to filter the file */
1895 compute_elapsed(&start_time);
1897 new_packet_list_thaw();
1899 if (selected_frame_num == -1) {
1900 /* The selected frame didn't pass the filter. */
1901 if (selected_frame == NULL) {
1902 /* That's because there *was* no selected frame. Make the first
1903 displayed frame the current frame. */
1904 selected_frame_num = 0;
1906 /* Find the nearest displayed frame to the selected frame (whether
1907 it's before or after that frame) and make that the current frame.
1908 If the next and previous displayed frames are equidistant from the
1909 selected frame, choose the next one. */
1910 g_assert(following_frame == NULL ||
1911 following_frame->num >= selected_frame->num);
1912 g_assert(preceding_frame == NULL ||
1913 preceding_frame->num <= selected_frame->num);
1914 if (following_frame == NULL) {
1915 /* No frame after the selected frame passed the filter, so we
1916 have to select the last displayed frame before the selected
1918 selected_frame_num = preceding_frame_num;
1919 selected_frame = preceding_frame;
1920 } else if (preceding_frame == NULL) {
1921 /* No frame before the selected frame passed the filter, so we
1922 have to select the first displayed frame after the selected
1924 selected_frame_num = following_frame_num;
1925 selected_frame = following_frame;
1927 /* Frames before and after the selected frame passed the filter, so
1928 we'll select the previous frame */
1929 selected_frame_num = preceding_frame_num;
1930 selected_frame = preceding_frame;
1935 if (selected_frame_num == -1) {
1936 /* There are no frames displayed at all. */
1937 cf_unselect_packet(cf);
1939 /* Either the frame that was selected passed the filter, or we've
1940 found the nearest displayed frame to that frame. Select it, make
1941 it the focus row, and make it visible. */
1942 /* Set to invalid to force update of packet list and packet details */
1943 cf->current_row = -1;
1944 if (selected_frame_num == 0) {
1945 new_packet_list_select_first_row();
1947 if (!new_packet_list_select_row_from_data(selected_frame)) {
1948 /* We didn't find a row corresponding to this frame.
1949 This means that the frame isn't being displayed currently,
1950 so we can't select it. */
1951 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
1952 "%sEnd of capture exceeded!%s\n\n"
1953 "The capture file is probably not fully dissected.",
1954 simple_dialog_primary_start(), simple_dialog_primary_end());
1959 /* Cleanup and release all dfilter resources */
1960 dfilter_free(dfcode);
1965 * Scan trough all frame data and recalculate the ref time
1966 * without rereading the file.
1967 * XXX - do we need a progres bar or is this fast enough?
1970 ref_time_packets(capture_file *cf)
1975 nstime_set_unset(&first_ts);
1976 nstime_set_unset(&prev_dis_ts);
1979 for (framenum = 1; framenum <= cf->count; framenum++) {
1980 fdata = frame_data_sequence_find(cf->frames, framenum);
1982 /* just add some value here until we know if it is being displayed or not */
1983 fdata->cum_bytes = cum_bytes + fdata->pkt_len;
1989 /* If we don't have the time stamp of the first packet in the
1990 capture, it's because this is the first packet. Save the time
1991 stamp of this packet as the time stamp of the first packet. */
1992 if (nstime_is_unset(&first_ts)) {
1993 first_ts = fdata->abs_ts;
1995 /* if this frames is marked as a reference time frame, reset
1996 firstsec and firstusec to this frame */
1997 if(fdata->flags.ref_time){
1998 first_ts = fdata->abs_ts;
2001 /* If we don't have the time stamp of the previous displayed packet,
2002 it's because this is the first displayed packet. Save the time
2003 stamp of this packet as the time stamp of the previous displayed
2005 if (nstime_is_unset(&prev_dis_ts)) {
2006 prev_dis_ts = fdata->abs_ts;
2009 /* Get the time elapsed between the first packet and this packet. */
2010 nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
2012 /* If it's greater than the current elapsed time, set the elapsed time
2013 to it (we check for "greater than" so as not to be confused by
2014 time moving backwards). */
2015 if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
2016 || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
2017 cf->elapsed_time = fdata->rel_ts;
2020 /* If this frame is displayed, get the time elapsed between the
2021 previous displayed packet and this packet. */
2022 if( fdata->flags.passed_dfilter ) {
2023 nstime_delta(&fdata->del_dis_ts, &fdata->abs_ts, &prev_dis_ts);
2024 prev_dis_ts = fdata->abs_ts;
2030 if( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) ){
2031 /* This frame either passed the display filter list or is marked as
2032 a time reference frame. All time reference frames are displayed
2033 even if they dont pass the display filter */
2034 if(fdata->flags.ref_time){
2035 /* if this was a TIME REF frame we should reset the cum_bytes field */
2036 cum_bytes = fdata->pkt_len;
2037 fdata->cum_bytes = cum_bytes;
2039 /* increase cum_bytes with this packets length */
2040 cum_bytes += fdata->pkt_len;
2053 process_specified_packets(capture_file *cf, packet_range_t *range,
2054 const char *string1, const char *string2, gboolean terminate_is_stop,
2055 gboolean (*callback)(capture_file *, frame_data *,
2056 union wtap_pseudo_header *, const guint8 *, void *),
2057 void *callback_args)
2061 union wtap_pseudo_header pseudo_header;
2062 guint8 pd[WTAP_MAX_PACKET_SIZE+1];
2063 psp_return_t ret = PSP_FINISHED;
2065 progdlg_t *progbar = NULL;
2068 gboolean progbar_stop_flag;
2069 GTimeVal progbar_start_time;
2070 gchar progbar_status_str[100];
2071 int progbar_nextstep;
2072 int progbar_quantum;
2073 range_process_e process_this;
2075 /* Update the progress bar when it gets to this value. */
2076 progbar_nextstep = 0;
2077 /* When we reach the value that triggers a progress bar update,
2078 bump that value by this amount. */
2079 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2080 /* Count of packets at which we've looked. */
2082 /* Progress so far. */
2085 progbar_stop_flag = FALSE;
2086 g_get_current_time(&progbar_start_time);
2088 packet_range_process_init(range);
2090 /* Iterate through all the packets, printing the packets that
2091 were selected by the current display filter. */
2092 for (framenum = 1; framenum <= cf->count; framenum++) {
2093 fdata = frame_data_sequence_find(cf->frames, framenum);
2095 /* Create the progress bar if necessary.
2096 We check on every iteration of the loop, so that it takes no
2097 longer than the standard time to create it (otherwise, for a
2098 large file, we might take considerably longer than that standard
2099 time in order to get to the next progress bar step). */
2100 if (progbar == NULL)
2101 progbar = delayed_create_progress_dlg(string1, string2,
2104 &progbar_start_time,
2107 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2108 when we update it, we have to run the GTK+ main loop to get it
2109 to repaint what's pending, and doing so may involve an "ioctl()"
2110 to see if there's any pending input from an X server, and doing
2111 that for every packet can be costly, especially on a big file. */
2112 if (progbar_count >= progbar_nextstep) {
2113 /* let's not divide by zero. I should never be started
2114 * with count == 0, so let's assert that
2116 g_assert(cf->count > 0);
2117 progbar_val = (gfloat) progbar_count / cf->count;
2119 if (progbar != NULL) {
2120 g_snprintf(progbar_status_str, sizeof(progbar_status_str),
2121 "%4u of %u packets", progbar_count, cf->count);
2122 update_progress_dlg(progbar, progbar_val, progbar_status_str);
2125 progbar_nextstep += progbar_quantum;
2128 if (progbar_stop_flag) {
2129 /* Well, the user decided to abort the operation. Just stop,
2130 and arrange to return PSP_STOPPED to our caller, so they know
2131 it was stopped explicitly. */
2138 /* do we have to process this packet? */
2139 process_this = packet_range_process_packet(range, fdata);
2140 if (process_this == range_process_next) {
2141 /* this packet uninteresting, continue with next one */
2143 } else if (process_this == range_processing_finished) {
2144 /* all interesting packets processed, stop the loop */
2148 /* Get the packet */
2149 if (!cf_read_frame_r(cf, fdata, &pseudo_header, pd)) {
2150 /* Attempt to get the packet failed. */
2154 /* Process the packet */
2155 if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
2156 /* Callback failed. We assume it reported the error appropriately. */
2162 /* We're done printing the packets; destroy the progress bar if
2164 if (progbar != NULL)
2165 destroy_progress_dlg(progbar);
2171 gboolean construct_protocol_tree;
2173 } retap_callback_args_t;
2176 retap_packet(capture_file *cf _U_, frame_data *fdata,
2177 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2180 retap_callback_args_t *args = argsp;
2183 epan_dissect_init(&edt, args->construct_protocol_tree, FALSE);
2184 tap_queue_init(&edt);
2185 epan_dissect_run(&edt, pseudo_header, pd, fdata, args->cinfo);
2186 tap_push_tapped_queue(&edt);
2187 epan_dissect_cleanup(&edt);
2193 cf_retap_packets(capture_file *cf)
2195 packet_range_t range;
2196 retap_callback_args_t callback_args;
2197 gboolean filtering_tap_listeners;
2200 /* Do we have any tap listeners with filters? */
2201 filtering_tap_listeners = have_filtering_tap_listeners();
2203 tap_flags = union_of_tap_listener_flags();
2205 /* If any tap listeners have filters, or require the protocol tree,
2206 construct the protocol tree. */
2207 callback_args.construct_protocol_tree = filtering_tap_listeners ||
2208 (tap_flags & TL_REQUIRES_PROTO_TREE);
2210 /* If any tap listeners require the columns, construct them. */
2211 callback_args.cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
2213 /* Reset the tap listeners. */
2214 reset_tap_listeners();
2216 /* Iterate through the list of packets, dissecting all packets and
2217 re-running the taps. */
2218 packet_range_init(&range);
2219 packet_range_process_init(&range);
2220 switch (process_specified_packets(cf, &range, "Recalculating statistics on",
2221 "all packets", TRUE, retap_packet,
2224 /* Completed successfully. */
2228 /* Well, the user decided to abort the refiltering.
2229 Return CF_READ_ABORTED so our caller knows they did that. */
2230 return CF_READ_ABORTED;
2233 /* Error while retapping. */
2234 return CF_READ_ERROR;
2237 g_assert_not_reached();
2242 print_args_t *print_args;
2243 gboolean print_header_line;
2244 char *header_line_buf;
2245 int header_line_buf_len;
2246 gboolean print_formfeed;
2247 gboolean print_separator;
2251 } print_callback_args_t;
2254 print_packet(capture_file *cf, frame_data *fdata,
2255 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2258 print_callback_args_t *args = argsp;
2265 gboolean proto_tree_needed;
2266 char bookmark_name[9+10+1]; /* "__frameNNNNNNNNNN__\0" */
2267 char bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0" */
2269 /* Create the protocol tree, and make it visible, if we're printing
2270 the dissection or the hex data.
2271 XXX - do we need it if we're just printing the hex data? */
2273 args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex || have_custom_cols(&cf->cinfo);
2274 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2276 /* Fill in the column information if we're printing the summary
2278 if (args->print_args->print_summary) {
2279 col_custom_prime_edt(&edt, &cf->cinfo);
2280 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2281 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2283 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2285 if (args->print_formfeed) {
2286 if (!new_page(args->print_args->stream))
2289 if (args->print_separator) {
2290 if (!print_line(args->print_args->stream, 0, ""))
2296 * We generate bookmarks, if the output format supports them.
2297 * The name is "__frameN__".
2299 g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
2301 if (args->print_args->print_summary) {
2302 if (args->print_header_line) {
2303 if (!print_line(args->print_args->stream, 0, args->header_line_buf))
2305 args->print_header_line = FALSE; /* we might not need to print any more */
2307 cp = &args->line_buf[0];
2309 for (i = 0; i < cf->cinfo.num_cols; i++) {
2310 /* Find the length of the string for this column. */
2311 column_len = (int) strlen(cf->cinfo.col_data[i]);
2312 if (args->col_widths[i] > column_len)
2313 column_len = args->col_widths[i];
2315 /* Make sure there's room in the line buffer for the column; if not,
2316 double its length. */
2317 line_len += column_len + 1; /* "+1" for space */
2318 if (line_len > args->line_buf_len) {
2319 cp_off = (int) (cp - args->line_buf);
2320 args->line_buf_len = 2 * line_len;
2321 args->line_buf = g_realloc(args->line_buf, args->line_buf_len + 1);
2322 cp = args->line_buf + cp_off;
2325 /* Right-justify the packet number column. */
2326 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2327 g_snprintf(cp, column_len+1, "%*s", args->col_widths[i], cf->cinfo.col_data[i]);
2329 g_snprintf(cp, column_len+1, "%-*s", args->col_widths[i], cf->cinfo.col_data[i]);
2331 if (i != cf->cinfo.num_cols - 1)
2337 * Generate a bookmark, using the summary line as the title.
2339 if (!print_bookmark(args->print_args->stream, bookmark_name,
2343 if (!print_line(args->print_args->stream, 0, args->line_buf))
2347 * Generate a bookmark, using "Frame N" as the title, as we're not
2348 * printing the summary line.
2350 g_snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
2351 if (!print_bookmark(args->print_args->stream, bookmark_name,
2354 } /* if (print_summary) */
2356 if (args->print_args->print_dissections != print_dissections_none) {
2357 if (args->print_args->print_summary) {
2358 /* Separate the summary line from the tree with a blank line. */
2359 if (!print_line(args->print_args->stream, 0, ""))
2363 /* Print the information in that tree. */
2364 if (!proto_tree_print(args->print_args, &edt, args->print_args->stream))
2367 /* Print a blank line if we print anything after this (aka more than one packet). */
2368 args->print_separator = TRUE;
2370 /* Print a header line if we print any more packet summaries */
2371 args->print_header_line = TRUE;
2374 if (args->print_args->print_hex) {
2375 /* Print the full packet data as hex. */
2376 if (!print_hex_data(args->print_args->stream, &edt))
2379 /* Print a blank line if we print anything after this (aka more than one packet). */
2380 args->print_separator = TRUE;
2382 /* Print a header line if we print any more packet summaries */
2383 args->print_header_line = TRUE;
2384 } /* if (args->print_args->print_dissections != print_dissections_none) */
2386 epan_dissect_cleanup(&edt);
2388 /* do we want to have a formfeed between each packet from now on? */
2389 if(args->print_args->print_formfeed) {
2390 args->print_formfeed = TRUE;
2396 epan_dissect_cleanup(&edt);
2401 cf_print_packets(capture_file *cf, print_args_t *print_args)
2404 print_callback_args_t callback_args;
2412 callback_args.print_args = print_args;
2413 callback_args.print_header_line = TRUE;
2414 callback_args.header_line_buf = NULL;
2415 callback_args.header_line_buf_len = 256;
2416 callback_args.print_formfeed = FALSE;
2417 callback_args.print_separator = FALSE;
2418 callback_args.line_buf = NULL;
2419 callback_args.line_buf_len = 256;
2420 callback_args.col_widths = NULL;
2422 if (!print_preamble(print_args->stream, cf->filename)) {
2423 destroy_print_stream(print_args->stream);
2424 return CF_PRINT_WRITE_ERROR;
2427 if (print_args->print_summary) {
2428 /* We're printing packet summaries. Allocate the header line buffer
2429 and get the column widths. */
2430 callback_args.header_line_buf = g_malloc(callback_args.header_line_buf_len + 1);
2432 /* Find the widths for each of the columns - maximum of the
2433 width of the title and the width of the data - and construct
2434 a buffer with a line containing the column titles. */
2435 callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * cf->cinfo.num_cols);
2436 cp = &callback_args.header_line_buf[0];
2438 for (i = 0; i < cf->cinfo.num_cols; i++) {
2439 /* Don't pad the last column. */
2440 if (i == cf->cinfo.num_cols - 1)
2441 callback_args.col_widths[i] = 0;
2443 callback_args.col_widths[i] = (gint) strlen(cf->cinfo.col_title[i]);
2444 data_width = get_column_char_width(get_column_format(i));
2445 if (data_width > callback_args.col_widths[i])
2446 callback_args.col_widths[i] = data_width;
2449 /* Find the length of the string for this column. */
2450 column_len = (int) strlen(cf->cinfo.col_title[i]);
2451 if (callback_args.col_widths[i] > column_len)
2452 column_len = callback_args.col_widths[i];
2454 /* Make sure there's room in the line buffer for the column; if not,
2455 double its length. */
2456 line_len += column_len + 1; /* "+1" for space */
2457 if (line_len > callback_args.header_line_buf_len) {
2458 cp_off = (int) (cp - callback_args.header_line_buf);
2459 callback_args.header_line_buf_len = 2 * line_len;
2460 callback_args.header_line_buf = g_realloc(callback_args.header_line_buf,
2461 callback_args.header_line_buf_len + 1);
2462 cp = callback_args.header_line_buf + cp_off;
2465 /* Right-justify the packet number column. */
2466 /* if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2467 g_snprintf(cp, column_len+1, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2469 g_snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2471 if (i != cf->cinfo.num_cols - 1)
2476 /* Now start out the main line buffer with the same length as the
2477 header line buffer. */
2478 callback_args.line_buf_len = callback_args.header_line_buf_len;
2479 callback_args.line_buf = g_malloc(callback_args.line_buf_len + 1);
2480 } /* if (print_summary) */
2482 /* Iterate through the list of packets, printing the packets we were
2484 ret = process_specified_packets(cf, &print_args->range, "Printing",
2485 "selected packets", TRUE, print_packet,
2488 g_free(callback_args.header_line_buf);
2489 g_free(callback_args.line_buf);
2490 g_free(callback_args.col_widths);
2495 /* Completed successfully. */
2499 /* Well, the user decided to abort the printing.
2501 XXX - note that what got generated before they did that
2502 will get printed if we're piping to a print program; we'd
2503 have to write to a file and then hand that to the print
2504 program to make it actually not print anything. */
2508 /* Error while printing.
2510 XXX - note that what got generated before they did that
2511 will get printed if we're piping to a print program; we'd
2512 have to write to a file and then hand that to the print
2513 program to make it actually not print anything. */
2514 destroy_print_stream(print_args->stream);
2515 return CF_PRINT_WRITE_ERROR;
2518 if (!print_finale(print_args->stream)) {
2519 destroy_print_stream(print_args->stream);
2520 return CF_PRINT_WRITE_ERROR;
2523 if (!destroy_print_stream(print_args->stream))
2524 return CF_PRINT_WRITE_ERROR;
2530 write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
2531 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2537 /* Create the protocol tree, but don't fill in the column information. */
2538 epan_dissect_init(&edt, TRUE, TRUE);
2539 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2541 /* Write out the information in that tree. */
2542 proto_tree_write_pdml(&edt, fh);
2544 epan_dissect_cleanup(&edt);
2550 cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
2555 fh = ws_fopen(print_args->file, "w");
2557 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2559 write_pdml_preamble(fh, cf->filename);
2562 return CF_PRINT_WRITE_ERROR;
2565 /* Iterate through the list of packets, printing the packets we were
2567 ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
2568 "selected packets", TRUE,
2569 write_pdml_packet, fh);
2574 /* Completed successfully. */
2578 /* Well, the user decided to abort the printing. */
2582 /* Error while printing. */
2584 return CF_PRINT_WRITE_ERROR;
2587 write_pdml_finale(fh);
2590 return CF_PRINT_WRITE_ERROR;
2593 /* XXX - check for an error */
2600 write_psml_packet(capture_file *cf, frame_data *fdata,
2601 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2606 gboolean proto_tree_needed;
2608 /* Fill in the column information, only create the protocol tree
2609 if having custom columns. */
2610 proto_tree_needed = have_custom_cols(&cf->cinfo);
2611 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2612 col_custom_prime_edt(&edt, &cf->cinfo);
2613 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2614 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2616 /* Write out the information in that tree. */
2617 proto_tree_write_psml(&edt, fh);
2619 epan_dissect_cleanup(&edt);
2625 cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
2630 fh = ws_fopen(print_args->file, "w");
2632 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2634 write_psml_preamble(fh);
2637 return CF_PRINT_WRITE_ERROR;
2640 /* Iterate through the list of packets, printing the packets we were
2642 ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
2643 "selected packets", TRUE,
2644 write_psml_packet, fh);
2649 /* Completed successfully. */
2653 /* Well, the user decided to abort the printing. */
2657 /* Error while printing. */
2659 return CF_PRINT_WRITE_ERROR;
2662 write_psml_finale(fh);
2665 return CF_PRINT_WRITE_ERROR;
2668 /* XXX - check for an error */
2675 write_csv_packet(capture_file *cf, frame_data *fdata,
2676 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2681 gboolean proto_tree_needed;
2683 /* Fill in the column information, only create the protocol tree
2684 if having custom columns. */
2685 proto_tree_needed = have_custom_cols(&cf->cinfo);
2686 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2687 col_custom_prime_edt(&edt, &cf->cinfo);
2688 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2689 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2691 /* Write out the information in that tree. */
2692 proto_tree_write_csv(&edt, fh);
2694 epan_dissect_cleanup(&edt);
2700 cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
2705 fh = ws_fopen(print_args->file, "w");
2707 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2709 write_csv_preamble(fh);
2712 return CF_PRINT_WRITE_ERROR;
2715 /* Iterate through the list of packets, printing the packets we were
2717 ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
2718 "selected packets", TRUE,
2719 write_csv_packet, fh);
2724 /* Completed successfully. */
2728 /* Well, the user decided to abort the printing. */
2732 /* Error while printing. */
2734 return CF_PRINT_WRITE_ERROR;
2737 write_csv_finale(fh);
2740 return CF_PRINT_WRITE_ERROR;
2743 /* XXX - check for an error */
2750 write_carrays_packet(capture_file *cf _U_, frame_data *fdata,
2751 union wtap_pseudo_header *pseudo_header,
2752 const guint8 *pd, void *argsp)
2757 epan_dissect_init(&edt, TRUE, TRUE);
2758 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2759 proto_tree_write_carrays(fdata->num, fh, &edt);
2760 epan_dissect_cleanup(&edt);
2766 cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
2771 fh = ws_fopen(print_args->file, "w");
2774 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2776 write_carrays_preamble(fh);
2780 return CF_PRINT_WRITE_ERROR;
2783 /* Iterate through the list of packets, printing the packets we were
2785 ret = process_specified_packets(cf, &print_args->range,
2787 "selected packets", TRUE,
2788 write_carrays_packet, fh);
2791 /* Completed successfully. */
2794 /* Well, the user decided to abort the printing. */
2797 /* Error while printing. */
2799 return CF_PRINT_WRITE_ERROR;
2802 write_carrays_finale(fh);
2806 return CF_PRINT_WRITE_ERROR;
2814 cf_find_packet_protocol_tree(capture_file *cf, const char *string,
2815 search_direction dir)
2819 mdata.string = string;
2820 mdata.string_len = strlen(string);
2821 return find_packet(cf, match_protocol_tree, &mdata, dir);
2825 cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree, match_data *mdata)
2827 mdata->frame_matched = FALSE;
2828 mdata->string = convert_string_case(cf->sfilter, cf->case_type);
2829 mdata->string_len = strlen(mdata->string);
2831 /* Iterate through all the nodes looking for matching text */
2832 proto_tree_children_foreach(tree, match_subtree_text, mdata);
2833 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2837 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
2839 match_data *mdata = criterion;
2842 /* Load the frame's data. */
2843 if (!cf_read_frame(cf, fdata)) {
2844 /* Attempt to get the packet failed. */
2848 /* Construct the protocol tree, including the displayed text */
2849 epan_dissect_init(&edt, TRUE, TRUE);
2850 /* We don't need the column information */
2851 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
2853 /* Iterate through all the nodes, seeing if they have text that matches. */
2855 mdata->frame_matched = FALSE;
2856 proto_tree_children_foreach(edt.tree, match_subtree_text, mdata);
2857 epan_dissect_cleanup(&edt);
2858 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2862 match_subtree_text(proto_node *node, gpointer data)
2864 match_data *mdata = (match_data *) data;
2865 const gchar *string = mdata->string;
2866 size_t string_len = mdata->string_len;
2867 capture_file *cf = mdata->cf;
2868 field_info *fi = PNODE_FINFO(node);
2869 gchar label_str[ITEM_LABEL_LENGTH];
2876 g_assert(fi && "dissection with an invisible proto tree?");
2878 if (mdata->frame_matched) {
2879 /* We already had a match; don't bother doing any more work. */
2883 /* Don't match invisible entries. */
2884 if (PROTO_ITEM_IS_HIDDEN(node))
2887 /* was a free format label produced? */
2889 label_ptr = fi->rep->representation;
2891 /* no, make a generic label */
2892 label_ptr = label_str;
2893 proto_item_fill_label(fi, label_str);
2896 /* Does that label match? */
2897 label_len = strlen(label_ptr);
2898 for (i = 0; i < label_len; i++) {
2899 c_char = label_ptr[i];
2901 c_char = toupper(c_char);
2902 if (c_char == string[c_match]) {
2904 if (c_match == string_len) {
2905 /* No need to look further; we have a match */
2906 mdata->frame_matched = TRUE;
2914 /* Recurse into the subtree, if it exists */
2915 if (node->first_child != NULL)
2916 proto_tree_children_foreach(node, match_subtree_text, mdata);
2920 cf_find_packet_summary_line(capture_file *cf, const char *string,
2921 search_direction dir)
2925 mdata.string = string;
2926 mdata.string_len = strlen(string);
2927 return find_packet(cf, match_summary_line, &mdata, dir);
2931 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
2933 match_data *mdata = criterion;
2934 const gchar *string = mdata->string;
2935 size_t string_len = mdata->string_len;
2937 const char *info_column;
2938 size_t info_column_len;
2939 match_result result = MR_NOTMATCHED;
2945 /* Load the frame's data. */
2946 if (!cf_read_frame(cf, fdata)) {
2947 /* Attempt to get the packet failed. */
2951 /* Don't bother constructing the protocol tree */
2952 epan_dissect_init(&edt, FALSE, FALSE);
2953 /* Get the column information */
2954 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, &cf->cinfo);
2956 /* Find the Info column */
2957 for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
2958 if (cf->cinfo.fmt_matx[colx][COL_INFO]) {
2959 /* Found it. See if we match. */
2960 info_column = edt.pi.cinfo->col_data[colx];
2961 info_column_len = strlen(info_column);
2962 for (i = 0; i < info_column_len; i++) {
2963 c_char = info_column[i];
2965 c_char = toupper(c_char);
2966 if (c_char == string[c_match]) {
2968 if (c_match == string_len) {
2969 result = MR_MATCHED;
2978 epan_dissect_cleanup(&edt);
2985 } cbs_t; /* "Counted byte string" */
2988 cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
2989 search_direction dir)
2994 info.data_len = string_size;
2996 /* String or hex search? */
2998 /* String search - what type of string? */
2999 switch (cf->scs_type) {
3001 case SCS_ASCII_AND_UNICODE:
3002 return find_packet(cf, match_ascii_and_unicode, &info, dir);
3005 return find_packet(cf, match_ascii, &info, dir);
3008 return find_packet(cf, match_unicode, &info, dir);
3011 g_assert_not_reached();
3015 return find_packet(cf, match_binary, &info, dir);
3019 match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
3021 cbs_t *info = criterion;
3022 const guint8 *ascii_text = info->data;
3023 size_t textlen = info->data_len;
3024 match_result result;
3030 /* Load the frame's data. */
3031 if (!cf_read_frame(cf, fdata)) {
3032 /* Attempt to get the packet failed. */
3036 result = MR_NOTMATCHED;
3037 buf_len = fdata->pkt_len;
3039 while (i < buf_len) {
3042 c_char = toupper(c_char);
3043 if (c_char != '\0') {
3044 if (c_char == ascii_text[c_match]) {
3046 if (c_match == textlen) {
3047 result = MR_MATCHED;
3048 cf->search_pos = i; /* Save the position of the last character
3049 for highlighting the field. */
3054 g_assert(i>=c_match);
3055 i -= (guint32)c_match;
3065 match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
3067 cbs_t *info = criterion;
3068 const guint8 *ascii_text = info->data;
3069 size_t textlen = info->data_len;
3070 match_result result;
3076 /* Load the frame's data. */
3077 if (!cf_read_frame(cf, fdata)) {
3078 /* Attempt to get the packet failed. */
3082 result = MR_NOTMATCHED;
3083 buf_len = fdata->pkt_len;
3085 while (i < buf_len) {
3088 c_char = toupper(c_char);
3089 if (c_char == ascii_text[c_match]) {
3091 if (c_match == textlen) {
3092 result = MR_MATCHED;
3093 cf->search_pos = i; /* Save the position of the last character
3094 for highlighting the field. */
3099 g_assert(i>=c_match);
3100 i -= (guint32)c_match;
3110 match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
3112 cbs_t *info = criterion;
3113 const guint8 *ascii_text = info->data;
3114 size_t textlen = info->data_len;
3115 match_result result;
3121 /* Load the frame's data. */
3122 if (!cf_read_frame(cf, fdata)) {
3123 /* Attempt to get the packet failed. */
3127 result = MR_NOTMATCHED;
3128 buf_len = fdata->pkt_len;
3130 while (i < buf_len) {
3133 c_char = toupper(c_char);
3134 if (c_char == ascii_text[c_match]) {
3136 if (c_match == textlen) {
3137 result = MR_MATCHED;
3138 cf->search_pos = i; /* Save the position of the last character
3139 for highlighting the field. */
3145 g_assert(i>=(c_match*2));
3146 i -= (guint32)c_match*2;
3155 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
3157 cbs_t *info = criterion;
3158 const guint8 *binary_data = info->data;
3159 size_t datalen = info->data_len;
3160 match_result result;
3165 /* Load the frame's data. */
3166 if (!cf_read_frame(cf, fdata)) {
3167 /* Attempt to get the packet failed. */
3171 result = MR_NOTMATCHED;
3172 buf_len = fdata->pkt_len;
3174 while (i < buf_len) {
3175 if (cf->pd[i] == binary_data[c_match]) {
3177 if (c_match == datalen) {
3178 result = MR_MATCHED;
3179 cf->search_pos = i; /* Save the position of the last character
3180 for highlighting the field. */
3185 g_assert(i>=c_match);
3186 i -= (guint32)c_match;
3195 cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode,
3196 search_direction dir)
3198 return find_packet(cf, match_dfilter, sfcode, dir);
3202 cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
3203 search_direction dir)
3208 if (!dfilter_compile(filter, &sfcode)) {
3210 * XXX - this shouldn't happen, as the filter string is machine
3215 if (sfcode == NULL) {
3217 * XXX - this shouldn't happen, as the filter string is machine
3222 result = find_packet(cf, match_dfilter, sfcode, dir);
3223 dfilter_free(sfcode);
3228 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
3230 dfilter_t *sfcode = criterion;
3232 match_result result;
3234 /* Load the frame's data. */
3235 if (!cf_read_frame(cf, fdata)) {
3236 /* Attempt to get the packet failed. */
3240 epan_dissect_init(&edt, TRUE, FALSE);
3241 epan_dissect_prime_dfilter(&edt, sfcode);
3242 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
3243 result = dfilter_apply_edt(sfcode, &edt) ? MR_MATCHED : MR_NOTMATCHED;
3244 epan_dissect_cleanup(&edt);
3249 cf_find_packet_marked(capture_file *cf, search_direction dir)
3251 return find_packet(cf, match_marked, NULL, dir);
3255 match_marked(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3257 return fdata->flags.marked ? MR_MATCHED : MR_NOTMATCHED;
3261 cf_find_packet_time_reference(capture_file *cf, search_direction dir)
3263 return find_packet(cf, match_time_reference, NULL, dir);
3267 match_time_reference(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3269 return fdata->flags.ref_time ? MR_MATCHED : MR_NOTMATCHED;
3273 find_packet(capture_file *cf,
3274 match_result (*match_function)(capture_file *, frame_data *, void *),
3275 void *criterion, search_direction dir)
3277 frame_data *start_fd;
3280 frame_data *new_fd = NULL;
3281 progdlg_t *progbar = NULL;
3286 GTimeVal start_time;
3287 gchar status_str[100];
3288 int progbar_nextstep;
3289 int progbar_quantum;
3291 match_result result;
3293 start_fd = cf->current_frame;
3294 if (start_fd != NULL) {
3295 /* Iterate through the list of packets, starting at the packet we've
3296 picked, calling a routine to run the filter on the packet, see if
3297 it matches, and stop if so. */
3299 framenum = start_fd->num;
3301 /* Update the progress bar when it gets to this value. */
3302 progbar_nextstep = 0;
3303 /* When we reach the value that triggers a progress bar update,
3304 bump that value by this amount. */
3305 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
3306 /* Progress so far. */
3310 g_get_current_time(&start_time);
3312 title = cf->sfilter?cf->sfilter:"";
3314 /* Create the progress bar if necessary.
3315 We check on every iteration of the loop, so that it takes no
3316 longer than the standard time to create it (otherwise, for a
3317 large file, we might take considerably longer than that standard
3318 time in order to get to the next progress bar step). */
3319 if (progbar == NULL)
3320 progbar = delayed_create_progress_dlg("Searching", title,
3321 FALSE, &stop_flag, &start_time, progbar_val);
3323 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
3324 when we update it, we have to run the GTK+ main loop to get it
3325 to repaint what's pending, and doing so may involve an "ioctl()"
3326 to see if there's any pending input from an X server, and doing
3327 that for every packet can be costly, especially on a big file. */
3328 if (count >= progbar_nextstep) {
3329 /* let's not divide by zero. I should never be started
3330 * with count == 0, so let's assert that
3332 g_assert(cf->count > 0);
3334 progbar_val = (gfloat) count / cf->count;
3336 if (progbar != NULL) {
3337 g_snprintf(status_str, sizeof(status_str),
3338 "%4u of %u packets", count, cf->count);
3339 update_progress_dlg(progbar, progbar_val, status_str);
3342 progbar_nextstep += progbar_quantum;
3346 /* Well, the user decided to abort the search. Go back to the
3347 frame where we started. */
3352 /* Go past the current frame. */
3353 if (dir == SD_BACKWARD) {
3354 /* Go on to the previous frame. */
3355 if (framenum == 1) {
3357 * XXX - other apps have a bit more of a detailed message
3358 * for this, and instead of offering "OK" and "Cancel",
3359 * they offer things such as "Continue" and "Cancel";
3360 * we need an API for popping up alert boxes with
3361 * {Verb} and "Cancel".
3364 if (prefs.gui_find_wrap)
3366 statusbar_push_temporary_msg("Search reached the beginning. Continuing at end.");
3367 framenum = cf->count; /* wrap around */
3371 statusbar_push_temporary_msg("Search reached the beginning.");
3372 framenum = start_fd->num; /* stay on previous packet */
3377 /* Go on to the next frame. */
3378 if (framenum == cf->count) {
3379 if (prefs.gui_find_wrap)
3381 statusbar_push_temporary_msg("Search reached the end. Continuing at beginning.");
3382 framenum = 1; /* wrap around */
3386 statusbar_push_temporary_msg("Search reached the end.");
3387 framenum = start_fd->num; /* stay on previous packet */
3392 fdata = frame_data_sequence_find(cf->frames, framenum);
3396 /* Is this packet in the display? */
3397 if (fdata->flags.passed_dfilter) {
3398 /* Yes. Does it match the search criterion? */
3399 result = (*match_function)(cf, fdata, criterion);
3400 if (result == MR_ERROR) {
3401 /* Error; our caller has reported the error. Go back to the frame
3402 where we started. */
3405 } else if (result == MR_MATCHED) {
3406 /* Yes. Go to the new frame. */
3412 if (fdata == start_fd) {
3413 /* We're back to the frame we were on originally, and that frame
3414 doesn't match the search filter. The search failed. */
3419 /* We're done scanning the packets; destroy the progress bar if it
3421 if (progbar != NULL)
3422 destroy_progress_dlg(progbar);
3425 if (new_fd != NULL) {
3426 /* Find and select */
3427 cf->search_in_progress = TRUE;
3428 found = new_packet_list_select_row_from_data(new_fd);
3429 cf->search_in_progress = FALSE;
3430 cf->search_pos = 0; /* Reset the position */
3432 /* We didn't find a row corresponding to this frame.
3433 This means that the frame isn't being displayed currently,
3434 so we can't select it. */
3435 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
3436 "%sEnd of capture exceeded!%s\n\n"
3437 "The capture file is probably not fully dissected.",
3438 simple_dialog_primary_start(), simple_dialog_primary_end());
3441 return TRUE; /* success */
3443 return FALSE; /* failure */
3447 cf_goto_frame(capture_file *cf, guint fnumber)
3451 fdata = frame_data_sequence_find(cf->frames, fnumber);
3453 if (fdata == NULL) {
3454 /* we didn't find a packet with that packet number */
3455 statusbar_push_temporary_msg("There is no packet number %u.", fnumber);
3456 return FALSE; /* we failed to go to that packet */
3458 if (!fdata->flags.passed_dfilter) {
3459 /* that packet currently isn't displayed */
3460 /* XXX - add it to the set of displayed packets? */
3461 statusbar_push_temporary_msg("Packet number %u isn't displayed.", fnumber);
3462 return FALSE; /* we failed to go to that packet */
3465 if (!new_packet_list_select_row_from_data(fdata)) {
3466 /* We didn't find a row corresponding to this frame.
3467 This means that the frame isn't being displayed currently,
3468 so we can't select it. */
3469 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
3470 "%sEnd of capture exceeded!%s\n\n"
3471 "The capture file is probably not fully dissected.",
3472 simple_dialog_primary_start(), simple_dialog_primary_end());
3475 return TRUE; /* we got to that packet */
3479 cf_goto_top_frame(void)
3481 /* Find and select */
3482 new_packet_list_select_first_row();
3483 return TRUE; /* we got to that packet */
3487 cf_goto_bottom_frame(void)
3489 /* Find and select */
3490 new_packet_list_select_last_row();
3491 return TRUE; /* we got to that packet */
3495 * Go to frame specified by currently selected protocol tree item.
3498 cf_goto_framenum(capture_file *cf)
3500 header_field_info *hfinfo;
3503 if (cf->finfo_selected) {
3504 hfinfo = cf->finfo_selected->hfinfo;
3506 if (hfinfo->type == FT_FRAMENUM) {
3507 framenum = fvalue_get_uinteger(&cf->finfo_selected->value);
3509 return cf_goto_frame(cf, framenum);
3516 /* Select the packet on a given row. */
3518 cf_select_packet(capture_file *cf, int row)
3522 /* Get the frame data struct pointer for this frame */
3523 fdata = new_packet_list_get_row_data(row);
3525 if (fdata == NULL) {
3526 /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
3527 the first entry is added to it by "real_insert_row()", that row
3528 is selected (see "real_insert_row()", in "ui/gtk/gtkclist.c", in both
3529 our version and the vanilla GTK+ version).
3531 This means that a "select-row" signal is emitted; this causes
3532 "packet_list_select_cb()" to be called, which causes "cf_select_packet()"
3535 "cf_select_packet()" fetches, above, the data associated with the
3536 row that was selected; however, as "gtk_clist_append()", which
3537 called "real_insert_row()", hasn't yet returned, we haven't yet
3538 associated any data with that row, so we get back a null pointer.
3540 We can't assume that there's only one frame in the frame list,
3541 either, as we may be filtering the display.
3543 We therefore assume that, if "row" is 0, i.e. the first row
3544 is being selected, and "cf->first_displayed" equals
3545 "cf->last_displayed", i.e. there's only one frame being
3546 displayed, that frame is the frame we want.
3548 This means we have to set "cf->first_displayed" and
3549 "cf->last_displayed" before adding the row to the
3550 GtkCList; see the comment in "add_packet_to_packet_list()". */
3552 if (row == 0 && cf->first_displayed == cf->last_displayed)
3553 fdata = frame_data_sequence_find(cf->frames, cf->first_displayed);
3556 /* If fdata _still_ isn't set simply give up. */
3557 if (fdata == NULL) {
3561 /* Get the data in that frame. */
3562 if (!cf_read_frame (cf, fdata)) {
3566 /* Record that this frame is the current frame. */
3567 cf->current_frame = fdata;
3568 cf->current_row = row;
3570 /* Create the logical protocol tree. */
3571 if (cf->edt != NULL)
3572 epan_dissect_free(cf->edt);
3574 /* We don't need the columns here. */
3575 cf->edt = epan_dissect_new(TRUE, TRUE);
3577 tap_build_interesting(cf->edt);
3578 epan_dissect_run(cf->edt, &cf->pseudo_header, cf->pd, cf->current_frame,
3581 dfilter_macro_build_ftv_cache(cf->edt->tree);
3583 cf_callback_invoke(cf_cb_packet_selected, cf);
3586 /* Unselect the selected packet, if any. */
3588 cf_unselect_packet(capture_file *cf)
3590 /* Destroy the epan_dissect_t for the unselected packet. */
3591 if (cf->edt != NULL) {
3592 epan_dissect_free(cf->edt);
3596 /* No packet is selected. */
3597 cf->current_frame = NULL;
3598 cf->current_row = 0;
3600 cf_callback_invoke(cf_cb_packet_unselected, cf);
3602 /* No protocol tree means no selected field. */
3603 cf_unselect_field(cf);
3606 /* Unset the selected protocol tree field, if any. */
3608 cf_unselect_field(capture_file *cf)
3610 cf->finfo_selected = NULL;
3612 cf_callback_invoke(cf_cb_field_unselected, cf);
3616 * Mark a particular frame.
3619 cf_mark_frame(capture_file *cf, frame_data *frame)
3621 if (! frame->flags.marked) {
3622 frame->flags.marked = TRUE;
3623 if (cf->count > cf->marked_count)
3629 * Unmark a particular frame.
3632 cf_unmark_frame(capture_file *cf, frame_data *frame)
3634 if (frame->flags.marked) {
3635 frame->flags.marked = FALSE;
3636 if (cf->marked_count > 0)
3642 * Ignore a particular frame.
3645 cf_ignore_frame(capture_file *cf, frame_data *frame)
3647 if (! frame->flags.ignored) {
3648 frame->flags.ignored = TRUE;
3649 if (cf->count > cf->ignored_count)
3650 cf->ignored_count++;
3655 * Un-ignore a particular frame.
3658 cf_unignore_frame(capture_file *cf, frame_data *frame)
3660 if (frame->flags.ignored) {
3661 frame->flags.ignored = FALSE;
3662 if (cf->ignored_count > 0)
3663 cf->ignored_count--;
3668 * Read the comment in SHB block
3672 cf_read_shb_comment(capture_file *cf)
3674 wtapng_section_t *shb_inf;
3675 const gchar *temp_str;
3677 /* Get info from SHB */
3678 shb_inf = wtap_file_get_shb_info(cf->wth);
3681 temp_str = shb_inf->opt_comment;
3689 cf_update_capture_comment(capture_file *cf, gchar *comment)
3691 wtapng_section_t *shb_inf;
3693 /* Get info from SHB */
3694 shb_inf = wtap_file_get_shb_info(cf->wth);
3696 /* See if the comment has changed or not */
3697 if (shb_inf && shb_inf->opt_comment) {
3698 if (strcmp(shb_inf->opt_comment, comment) == 0) {
3707 /* The comment has changed, let's update it */
3708 wtap_write_shb_comment(cf->wth, comment);
3709 /* Mark the file as having unsaved changes */
3710 cf->unsaved_changes = TRUE;
3717 } save_callback_args_t;
3720 * Save a capture to a file, in a particular format, saving either
3721 * all packets, all currently-displayed packets, or all marked packets.
3723 * Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
3724 * up a message box for the failure.
3727 save_packet(capture_file *cf _U_, frame_data *fdata,
3728 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
3731 save_callback_args_t *args = argsp;
3732 struct wtap_pkthdr hdr;
3735 /* init the wtap header for saving */
3736 /* XXX - these are the only flags that correspond to data that we have
3737 in the frame_data structure and that matter on a per-packet basis.
3739 For WTAP_HAS_CAP_LEN, either the file format has separate "captured"
3740 and "on the wire" lengths, or it doesn't.
3742 For WTAP_HAS_DROP_COUNT, Wiretap doesn't actually supply the value
3745 For WTAP_HAS_PACK_FLAGS, we currently don't save the FCS length
3746 from the packet flags. */
3747 hdr.presence_flags = 0;
3748 if (fdata->flags.has_ts)
3749 hdr.presence_flags |= WTAP_HAS_TS;
3750 if (fdata->flags.has_ts)
3751 hdr.presence_flags |= WTAP_HAS_INTERFACE_ID;
3752 hdr.ts.secs = fdata->abs_ts.secs;
3753 hdr.ts.nsecs = fdata->abs_ts.nsecs;
3754 hdr.caplen = fdata->cap_len;
3755 hdr.len = fdata->pkt_len;
3756 hdr.pkt_encap = fdata->lnk_t;
3758 hdr.interface_id = fdata->interface_id; /* identifier of the interface. */
3760 hdr.opt_comment = fdata->opt_comment; /* NULL if not available */
3763 hdr.pack_flags = /* XXX - 0 for now (any value for "we don't have it"?) */
3765 /* and save the packet */
3766 if (!wtap_dump(args->pdh, &hdr, pseudo_header, pd, &err)) {
3768 /* Wiretap error. */
3771 case WTAP_ERR_UNSUPPORTED_ENCAP:
3773 * This is a problem with the particular frame we're writing;
3774 * note that, and give the frame number.
3776 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3777 "Frame %u has a network type that can't be saved in a \"%s\" file.",
3778 fdata->num, wtap_file_type_string(args->file_type));
3782 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3783 "An error occurred while writing to the file \"%s\": %s.",
3784 args->fname, wtap_strerror(err));
3789 write_failure_alert_box(args->fname, err);
3797 * Can this capture file be saved in any format except by copying the raw data?
3800 cf_can_save_as(capture_file *cf)
3804 for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) {
3805 /* To save a file with Wiretap, Wiretap has to handle that format,
3806 and its code to handle that format must be able to write a file
3807 with this file's encapsulation type. */
3808 if (wtap_dump_can_open(ft) && wtap_dump_can_write_encap(ft, cf->lnk_t)) {
3809 /* OK, we can write it out in this type. */
3814 /* No, we couldn't save it in any format. */
3819 cf_save_packets(capture_file *cf, const char *fname, guint save_format,
3820 gboolean compressed, gboolean dont_reopen)
3822 gchar *fname_new = NULL;
3826 packet_range_t range;
3827 save_callback_args_t callback_args;
3829 cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
3831 if (save_format == cf->cd_t && compressed == cf->iscompressed
3832 && !cf->unsaved_changes) {
3833 /* We're saving in the format it's already in, and there are no
3834 changes we have in memory that aren't saved to the file, so
3835 we can just move or copy the raw data. */
3837 if (cf->is_tempfile) {
3838 /* The file being saved is a temporary file from a live
3839 capture, so it doesn't need to stay around under that name;
3840 first, try renaming the capture buffer file to the new name.
3841 This acts as a "safe save", in that, if the file already
3842 exists, the existing file will be removed only if the rename
3845 Sadly, on Windows, as we have the current capture file
3846 open, even MoveFileEx() with MOVEFILE_REPLACE_EXISTING
3847 (to cause the rename to remove an existing target), as
3848 done by ws_stdio_rename() (ws_rename() is #defined to
3849 be ws_stdio_rename() on Windows) will fail.
3851 According to the MSDN documentation for CreateFile(), if,
3852 when we open a capture file, we were to directly do a CreateFile(),
3853 opening with FILE_SHARE_DELETE|FILE_SHARE_READ, and then
3854 convert it to a file descriptor with _open_osfhandle(),
3855 that would allow the file to be renamed out from under us.
3857 However, that doesn't work in practice. Perhaps the problem
3858 is that the process doing the rename is the process that
3859 has the file open. */
3861 if (ws_rename(cf->filename, fname) == 0) {
3862 /* That succeeded - there's no need to copy the source file. */
3865 if (errno == EXDEV) {
3866 /* They're on different file systems, so we have to copy the
3870 /* The rename failed, but not because they're on different
3871 file systems - put up an error message. (Or should we
3872 just punt and try to copy? The only reason why I'd
3873 expect the rename to fail and the copy to succeed would
3874 be if we didn't have permission to remove the file from
3875 the temporary directory, and that might be fixable - but
3876 is it worth requiring the user to go off and fix it?) */
3877 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3878 file_rename_error_message(errno), fname);
3886 /* It's a permanent file, so we should copy it, and not remove the
3892 /* Copy the file, if we haven't moved it. If we're overwriting
3893 an existing file, we do it with a "safe save", by writing
3894 to a new file and, if the write succeeds, renaming the
3895 new file on top of the old file. */
3896 if (file_exists(fname)) {
3897 fname_new = g_strdup_printf("%s~", fname);
3898 if (!copy_file_binary_mode(cf->filename, fname_new))
3901 if (!copy_file_binary_mode(cf->filename, fname))
3906 /* Either we're saving in a different format or we're saving changes,
3907 such as added, modified, or removed comments, that haven't yet
3908 been written to the underlying file; we can't do that by copying
3909 or moving the capture file, we have to do it by writing the packets
3912 wtapng_section_t *shb_hdr = NULL;
3913 wtapng_iface_descriptions_t *idb_inf = NULL;
3915 shb_hdr = wtap_file_get_shb_info(cf->wth);
3916 idb_inf = wtap_file_get_idb_info(cf->wth);
3918 if (file_exists(fname)) {
3919 /* We're overwriting an existing file; write out to a new file,
3920 and, if that succeeds, rename the new file on top of the
3921 old file. That makes this a "safe save", so that we don't
3922 lose the old file if we have a problem writing out the new
3923 file. (If the existing file is the current capture file,
3924 we *HAVE* to do that, otherwise we're overwriting the file
3925 from which we're reading the packets that we're writing!) */
3926 fname_new = g_strdup_printf("%s~", fname);
3927 pdh = wtap_dump_open_ng(fname_new, save_format, cf->lnk_t, cf->snap,
3928 compressed, shb_hdr, idb_inf, &err);
3930 pdh = wtap_dump_open_ng(fname, save_format, cf->lnk_t, cf->snap,
3931 compressed, shb_hdr, idb_inf, &err);
3937 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
3941 /* Add address resolution */
3942 wtap_dump_set_addrinfo_list(pdh, get_addrinfo_list());
3944 /* Create a packet range that's set to the default "save everything"
3946 packet_range_init(&range);
3948 /* Iterate through the list of packets, processing all the packets. */
3949 callback_args.pdh = pdh;
3950 callback_args.fname = fname;
3951 callback_args.file_type = save_format;
3952 switch (process_specified_packets(cf, &range, "Saving", "selected packets",
3953 TRUE, save_packet, &callback_args)) {
3956 /* Completed successfully. */
3960 /* The user decided to abort the saving.
3961 XXX - remove the output file? */
3965 /* Error while saving. */
3966 wtap_dump_close(pdh, &err);
3970 if (!wtap_dump_close(pdh, &err)) {
3971 cf_close_failure_alert_box(fname, err);
3976 if (fname_new != NULL) {
3977 /* We wrote out to fname_new, and should rename it on top of
3978 fname. fname_new is now closed, so that should be possible even
3979 on Windows. However, on Windows, we first need to close whatever
3980 file descriptors we have open for fname. */
3982 wtap_fdclose(cf->wth);
3984 /* Now do the rename. */
3985 if (ws_rename(fname_new, fname) == -1) {
3986 /* Well, the rename failed. */
3987 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3988 file_rename_error_message(errno), fname);
3990 /* Attempt to reopen the random file descriptor using the
3991 current file's filename. (At this point, the sequential
3992 file descriptor is closed.) */
3993 if (!wtap_fdreopen(cf->wth, cf->filename, &err)) {
3994 /* Oh, well, we're screwed. */
3995 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3996 file_open_error_message(err, FALSE), cf->filename);
4003 cf_callback_invoke(cf_cb_file_save_finished, NULL);
4004 cf->unsaved_changes = FALSE;
4007 /* Open and read the file we saved to.
4009 XXX - this is somewhat of a waste; we already have the
4010 packets, all this gets us is updated file type information
4011 (which we could just stuff into "cf"), and having the new
4012 file be the one we have opened and from which we're reading
4013 the data, and it means we have to spend time opening and
4014 reading the file, which could be a significant amount of
4015 time if the file is large.
4017 If the capture-file-writing code were to return the
4018 seek offset of each packet it writes, we could save that
4019 in the frame_data structure for the frame, and just open
4020 the file without reading it again. */
4022 if ((cf_open(cf, fname, FALSE, &err)) == CF_OK) {
4023 /* XXX - report errors if this fails?
4024 What should we return if it fails or is aborted? */
4026 switch (cf_read(cf, TRUE)) {
4030 /* Just because we got an error, that doesn't mean we were unable
4031 to read any of the file; we handle what we could get from the
4035 case CF_READ_ABORTED:
4036 /* The user bailed out of re-reading the capture file; the
4037 capture file has been closed - just return (without
4038 changing any menu settings; "cf_close()" set them
4039 correctly for the "no capture file open" state). */
4047 if (fname_new != NULL) {
4048 /* We were trying to write to a temporary file; get rid of it if it
4049 exists. (We don't care whether this fails, as, if it fails,
4050 there's not much we can do about it. I guess if it failed for
4051 a reason other than "it doesn't exist", we could report an
4052 error, so the user knows there's a junk file that they might
4053 want to clean up.) */
4054 ws_unlink(fname_new);
4057 cf_callback_invoke(cf_cb_file_save_failed, NULL);
4062 cf_export_specified_packets(capture_file *cf, const char *fname,
4063 packet_range_t *range, guint save_format,
4064 gboolean compressed)
4066 gchar *fname_new = NULL;
4069 save_callback_args_t callback_args;
4070 wtapng_section_t *shb_hdr = NULL;
4071 wtapng_iface_descriptions_t *idb_inf = NULL;
4073 cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
4075 packet_range_process_init(range);
4077 /* We're writing out specified packets from the specified capture
4078 file to another file. Even if all captured packets are to be
4079 written, don't special-case the operation - read each packet
4080 and then write it out if it's one of the specified ones. */
4082 shb_hdr = wtap_file_get_shb_info(cf->wth);
4083 idb_inf = wtap_file_get_idb_info(cf->wth);
4085 if (file_exists(fname)) {
4086 /* We're overwriting an existing file; write out to a new file,
4087 and, if that succeeds, rename the new file on top of the
4088 old file. That makes this a "safe save", so that we don't
4089 lose the old file if we have a problem writing out the new
4090 file. (If the existing file is the current capture file,
4091 we *HAVE* to do that, otherwise we're overwriting the file
4092 from which we're reading the packets that we're writing!) */
4093 fname_new = g_strdup_printf("%s~", fname);
4094 pdh = wtap_dump_open_ng(fname_new, save_format, cf->lnk_t, cf->snap,
4095 compressed, shb_hdr, idb_inf, &err);
4097 pdh = wtap_dump_open_ng(fname, save_format, cf->lnk_t, cf->snap,
4098 compressed, shb_hdr, idb_inf, &err);
4104 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
4108 /* Add address resolution */
4109 wtap_dump_set_addrinfo_list(pdh, get_addrinfo_list());
4111 /* Iterate through the list of packets, processing the packets we were
4114 XXX - we've already called "packet_range_process_init(range)", but
4115 "process_specified_packets()" will do it again. Fortunately,
4116 that's harmless in this case, as we haven't done anything to
4117 "range" since we initialized it. */
4118 callback_args.pdh = pdh;
4119 callback_args.fname = fname;
4120 callback_args.file_type = save_format;
4121 switch (process_specified_packets(cf, range, "Writing", "specified packets",
4122 TRUE, save_packet, &callback_args)) {
4125 /* Completed successfully. */
4129 /* The user decided to abort the writing.
4130 XXX - remove the output file? */
4134 /* Error while writing. */
4135 wtap_dump_close(pdh, &err);
4139 if (!wtap_dump_close(pdh, &err)) {
4140 cf_close_failure_alert_box(fname, err);
4144 if (fname_new != NULL) {
4145 /* We wrote out to fname_new, and should rename it on top of
4146 fname; fname is now closed, so that should be possible even
4147 on Windows. Do the rename. */
4148 if (ws_rename(fname_new, fname) == -1) {
4149 /* Well, the rename failed. */
4150 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4151 file_rename_error_message(errno), fname);
4156 cf_callback_invoke(cf_cb_file_save_finished, NULL);
4160 if (fname_new != NULL) {
4161 /* We were trying to write to a temporary file; get rid of it if it
4162 exists. (We don't care whether this fails, as, if it fails,
4163 there's not much we can do about it. I guess if it failed for
4164 a reason other than "it doesn't exist", we could report an
4165 error, so the user knows there's a junk file that they might
4166 want to clean up.) */
4167 ws_unlink(fname_new);
4170 cf_callback_invoke(cf_cb_file_save_failed, NULL);
4175 cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
4176 gboolean for_writing, int file_type)
4179 /* Wiretap error. */
4182 case WTAP_ERR_NOT_REGULAR_FILE:
4183 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4184 "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
4188 case WTAP_ERR_RANDOM_OPEN_PIPE:
4189 /* Seen only when opening a capture file for reading. */
4190 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4191 "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.\n"
4192 "To capture from a pipe or FIFO use wireshark -i -",
4196 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
4197 /* Seen only when opening a capture file for reading. */
4198 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4199 "The file \"%s\" isn't a capture file in a format Wireshark understands.",
4203 case WTAP_ERR_UNSUPPORTED:
4204 /* Seen only when opening a capture file for reading. */
4205 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4206 "The file \"%s\" isn't a capture file in a format Wireshark understands.\n"
4208 filename, err_info);
4212 case WTAP_ERR_CANT_WRITE_TO_PIPE:
4213 /* Seen only when opening a capture file for writing. */
4214 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4215 "The file \"%s\" is a pipe, and %s capture files can't be "
4216 "written to a pipe.",
4217 filename, wtap_file_type_string(file_type));
4220 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
4221 /* Seen only when opening a capture file for writing. */
4222 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4223 "Wireshark doesn't support writing capture files in that format.");
4226 case WTAP_ERR_UNSUPPORTED_ENCAP:
4228 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4229 "Wireshark can't save this capture in that format.");
4231 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4232 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.\n"
4234 filename, err_info);
4239 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
4241 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4242 "Wireshark can't save this capture in that format.");
4244 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4245 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.",
4250 case WTAP_ERR_BAD_FILE:
4251 /* Seen only when opening a capture file for reading. */
4252 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4253 "The file \"%s\" appears to be damaged or corrupt.\n"
4255 filename, err_info);
4259 case WTAP_ERR_CANT_OPEN:
4261 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4262 "The file \"%s\" could not be created for some unknown reason.",
4265 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4266 "The file \"%s\" could not be opened for some unknown reason.",
4271 case WTAP_ERR_SHORT_READ:
4272 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4273 "The file \"%s\" appears to have been cut short"
4274 " in the middle of a packet or other data.",
4278 case WTAP_ERR_SHORT_WRITE:
4279 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4280 "A full header couldn't be written to the file \"%s\".",
4284 case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
4285 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4286 "This file type cannot be written as a compressed file.");
4289 case WTAP_ERR_DECOMPRESS:
4290 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4291 "The compressed file \"%s\" appears to be damaged or corrupt.\n"
4292 "(%s)", filename, err_info);
4297 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4298 "The file \"%s\" could not be %s: %s.",
4300 for_writing ? "created" : "opened",
4301 wtap_strerror(err));
4306 open_failure_alert_box(filename, err, for_writing);
4311 * XXX - whether we mention the source pathname, the target pathname,
4312 * or both depends on the error and on what we find if we look for
4313 * one or both of them.
4316 file_rename_error_message(int err)
4319 static char errmsg_errno[1024+1];
4324 /* XXX - should check whether the source exists and, if not,
4325 report it as the problem and, if so, report the destination
4327 errmsg = "The path to the file \"%s\" doesn't exist.";
4331 /* XXX - if we're doing a rename after a safe save, we should
4332 probably say something else. */
4333 errmsg = "You don't have permission to move the capture file to \"%s\".";
4337 /* XXX - this should probably mention both the source and destination
4339 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4340 "The file \"%%s\" could not be moved: %s.",
4341 wtap_strerror(err));
4342 errmsg = errmsg_errno;
4348 /* Check for write errors - if the file is being written to an NFS server,
4349 a write error may not show up until the file is closed, as NFS clients
4350 might not send writes to the server until the "write()" call finishes,
4351 so that the write may fail on the server but the "write()" may succeed. */
4353 cf_close_failure_alert_box(const char *filename, int err)
4356 /* Wiretap error. */
4359 case WTAP_ERR_CANT_CLOSE:
4360 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4361 "The file \"%s\" couldn't be closed for some unknown reason.",
4365 case WTAP_ERR_SHORT_WRITE:
4366 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4367 "Not all the packets could be written to the file \"%s\".",
4372 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4373 "An error occurred while closing the file \"%s\": %s.",
4374 filename, wtap_strerror(err));
4379 We assume that a close error from the OS is really a write error. */
4380 write_failure_alert_box(filename, err);
4384 /* Reload the current capture file. */
4386 cf_reload(capture_file *cf) {
4388 gboolean is_tempfile;
4391 /* If the file could be opened, "cf_open()" calls "cf_close()"
4392 to get rid of state for the old capture file before filling in state
4393 for the new capture file. "cf_close()" will remove the file if
4394 it's a temporary file; we don't want that to happen (for one thing,
4395 it'd prevent subsequent reopens from working). Remember whether it's
4396 a temporary file, mark it as not being a temporary file, and then
4397 reopen it as the type of file it was.
4399 Also, "cf_close()" will free "cf->filename", so we must make
4400 a copy of it first. */
4401 filename = g_strdup(cf->filename);
4402 is_tempfile = cf->is_tempfile;
4403 cf->is_tempfile = FALSE;
4404 if (cf_open(cf, filename, is_tempfile, &err) == CF_OK) {
4405 switch (cf_read(cf, TRUE)) {
4409 /* Just because we got an error, that doesn't mean we were unable
4410 to read any of the file; we handle what we could get from the
4414 case CF_READ_ABORTED:
4415 /* The user bailed out of re-reading the capture file; the
4416 capture file has been closed - just free the capture file name
4417 string and return (without changing the last containing
4423 /* The open failed, so "cf->is_tempfile" wasn't set to "is_tempfile".
4424 Instead, the file was left open, so we should restore "cf->is_tempfile"
4427 XXX - change the menu? Presumably "cf_open()" will do that;
4428 make sure it does! */
4429 cf->is_tempfile = is_tempfile;
4431 /* "cf_open()" made a copy of the file name we handed it, so
4432 we should free up our copy. */
4442 * indent-tabs-mode: nil
4445 * ex: set shiftwidth=2 tabstop=8 expandtab:
4446 * :indentSize=2:tabSize=8:noTabs=true: