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);
315 cf->linktypes = g_array_sized_new(FALSE, FALSE, (guint) sizeof(int), 1);
317 cf->displayed_count = 0;
318 cf->marked_count = 0;
319 cf->ignored_count = 0;
320 cf->ref_time_count = 0;
321 cf->drops_known = FALSE;
323 cf->snap = wtap_snapshot_length(cf->wth);
325 /* Snapshot length not known. */
326 cf->has_snap = FALSE;
327 cf->snap = WTAP_MAX_PACKET_SIZE;
331 /* Allocate a frame_data_sequence for the frames in this file */
332 cf->frames = new_frame_data_sequence();
334 nstime_set_zero(&cf->elapsed_time);
335 nstime_set_unset(&first_ts);
336 nstime_set_unset(&prev_dis_ts);
337 nstime_set_unset(&prev_cap_ts);
340 /* Adjust timestamp precision if auto is selected, col width will be adjusted */
341 cf_timestamp_auto_precision(cf);
343 new_packet_list_queue_draw();
344 fileset_file_opened(fname);
346 if(cf->cd_t == WTAP_FILE_BER) {
347 /* tell the BER dissector the file name */
348 ber_set_filename(cf->filename);
351 wtap_set_cb_new_ipv4(cf->wth, add_ipv4_name);
352 wtap_set_cb_new_ipv6(cf->wth, (wtap_new_ipv6_callback_t) add_ipv6_name);
357 cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
362 * Add an encapsulation type to cf->linktypes.
365 cf_add_encapsulation_type(capture_file *cf, int encap)
369 for (i = 0; i < cf->linktypes->len; i++) {
370 if (g_array_index(cf->linktypes, gint, i) == encap)
371 return; /* it's already there */
373 /* It's not already there - add it. */
374 g_array_append_val(cf->linktypes, encap);
378 * Reset the state for the currently closed file, but don't do the
379 * UI callbacks; this is for use in "cf_open()", where we don't
380 * want the UI to go from "file open" to "file closed" back to
381 * "file open", we want it to go from "old file open" to "new file
382 * open and being read".
384 * XXX - currently, cf_open() calls cf_close(), rather than
388 cf_reset_state(capture_file *cf)
390 /* Die if we're in the middle of reading a file. */
391 g_assert(cf->state != FILE_READ_IN_PROGRESS);
397 /* We have no file open... */
398 if (cf->filename != NULL) {
399 /* If it's a temporary file, remove it. */
401 ws_unlink(cf->filename);
402 g_free(cf->filename);
405 /* ...which means we have no changes to that file to save. */
406 cf->unsaved_changes = FALSE;
408 dfilter_free(cf->rfcode);
410 if (cf->frames != NULL) {
411 free_frame_data_sequence(cf->frames);
414 #ifdef WANT_PACKET_EDITOR
415 if (cf->edited_frames) {
416 g_tree_destroy(cf->edited_frames);
417 cf->edited_frames = NULL;
420 cf_unselect_packet(cf); /* nothing to select */
421 cf->first_displayed = 0;
422 cf->last_displayed = 0;
424 /* No frames, no frame selected, no field in that frame selected. */
426 cf->current_frame = 0;
428 cf->finfo_selected = NULL;
430 /* No frame link-layer types, either. */
431 g_array_free(cf->linktypes, TRUE);
432 cf->linktypes = NULL;
434 /* Clear the packet list. */
435 new_packet_list_freeze();
436 new_packet_list_clear();
437 new_packet_list_thaw();
440 nstime_set_zero(&cf->elapsed_time);
442 reset_tap_listeners();
444 /* We have no file open. */
445 cf->state = FILE_CLOSED;
447 fileset_file_closed();
450 /* Reset everything to a pristine state */
452 cf_close(capture_file *cf)
454 if(cf->state != FILE_CLOSED) {
455 cf_callback_invoke(cf_cb_file_closing, cf);
457 /* close things, if not already closed before */
458 color_filters_cleanup();
460 cleanup_dissection();
462 cf_callback_invoke(cf_cb_file_closed, cf);
466 /* an out of memory exception occured, wait for a user button press to exit */
467 static void outofmemory_cb(gpointer dialog _U_, gint btn _U_, gpointer data _U_)
473 calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos, gchar *status_str, gulong status_size)
477 progbar_val = (gfloat) file_pos / (gfloat) size;
478 if (progbar_val > 1.0) {
480 /* The file probably grew while we were reading it.
481 * Update file size, and try again.
483 size = wtap_file_size(cf->wth, NULL);
486 progbar_val = (gfloat) file_pos / (gfloat) size;
488 /* If it's still > 1, either "wtap_file_size()" failed (in which
489 * case there's not much we can do about it), or the file
490 * *shrank* (in which case there's not much we can do about
491 * it); just clip the progress value at 1.0.
493 if (progbar_val > 1.0f)
497 g_snprintf(status_str, status_size,
498 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
499 file_pos / 1024, size / 1024);
505 cf_read(capture_file *cf, gboolean reloading)
511 char errmsg_errno[1024+1];
514 progdlg_t *volatile progbar = NULL;
516 volatile gint64 size;
517 volatile float progbar_val;
519 gchar status_str[100];
520 volatile gint64 progbar_nextstep;
521 volatile gint64 progbar_quantum;
523 gboolean filtering_tap_listeners;
525 volatile int count = 0;
527 volatile int displayed_once = 0;
531 /* Compile the current display filter.
532 * We assume this will not fail since cf->dfilter is only set in
533 * cf_filter IFF the filter was valid.
535 compiled = dfilter_compile(cf->dfilter, &dfcode);
536 g_assert(!cf->dfilter || (compiled && dfcode));
538 /* Do we have any tap listeners with filters? */
539 filtering_tap_listeners = have_filtering_tap_listeners();
541 /* Get the union of the flags for all tap listeners. */
542 tap_flags = union_of_tap_listener_flags();
544 reset_tap_listeners();
546 name_ptr = g_filename_display_basename(cf->filename);
549 cf_callback_invoke(cf_cb_file_reload_started, cf);
551 cf_callback_invoke(cf_cb_file_read_started, cf);
553 /* Record whether the file is compressed.
554 XXX - do we know this at open time? */
555 cf->iscompressed = wtap_iscompressed(cf->wth);
557 /* Find the size of the file. */
558 size = wtap_file_size(cf->wth, NULL);
560 /* Update the progress bar when it gets to this value. */
561 progbar_nextstep = 0;
562 /* When we reach the value that triggers a progress bar update,
563 bump that value by this amount. */
565 progbar_quantum = size/N_PROGBAR_UPDATES;
566 if (progbar_quantum < MIN_QUANTUM)
567 progbar_quantum = MIN_QUANTUM;
570 /* Progress so far. */
573 /* The packet list window will be empty until the file is completly loaded */
574 new_packet_list_freeze();
577 g_get_current_time(&start_time);
579 while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
582 file_pos = wtap_read_so_far(cf->wth);
584 /* Create the progress bar if necessary.
585 * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
587 if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)){
588 progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
590 progbar = delayed_create_progress_dlg("Reloading", name_ptr,
591 TRUE, &stop_flag, &start_time, progbar_val);
593 progbar = delayed_create_progress_dlg("Loading", name_ptr,
594 TRUE, &stop_flag, &start_time, progbar_val);
597 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
598 when we update it, we have to run the GTK+ main loop to get it
599 to repaint what's pending, and doing so may involve an "ioctl()"
600 to see if there's any pending input from an X server, and doing
601 that for every packet can be costly, especially on a big file. */
602 if (file_pos >= progbar_nextstep) {
603 if (progbar != NULL) {
604 progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
605 /* update the packet bar content on the first run or frequently on very large files */
607 if (progbar_quantum > 500000 || displayed_once == 0) {
608 if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->count != 0) {
610 packets_bar_update();
613 #endif /* HAVE_LIBPCAP */
614 update_progress_dlg(progbar, progbar_val, status_str);
616 progbar_nextstep += progbar_quantum;
621 /* Well, the user decided to abort the read. He/She will be warned and
622 it might be enough for him/her to work with the already loaded
624 This is especially true for very large capture files, where you don't
625 want to wait loading the whole file (which may last minutes or even
626 hours even on fast machines) just to see that it was the wrong file. */
630 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
632 CATCH(OutOfMemoryError) {
635 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
636 "%sOut Of Memory!%s\n"
638 "Sorry, but Wireshark has to terminate now!\n"
640 "Some infos / workarounds can be found at:\n"
641 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
642 simple_dialog_primary_start(), simple_dialog_primary_end());
643 /* we have to terminate, as we cannot recover from the memory error */
644 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
646 main_window_update();
647 /* XXX - how to avoid a busy wait? */
655 /* Free the display name */
658 /* Cleanup and release all dfilter resources */
660 dfilter_free(dfcode);
663 /* We're done reading the file; destroy the progress bar if it was created. */
665 destroy_progress_dlg(progbar);
667 /* We're done reading sequentially through the file. */
668 cf->state = FILE_READ_DONE;
670 /* Close the sequential I/O side, to free up memory it requires. */
671 wtap_sequential_close(cf->wth);
673 /* Allow the protocol dissectors to free up memory that they
674 * don't need after the sequential run-through of the packets. */
675 postseq_cleanup_all_protocols();
677 /* compute the time it took to load the file */
678 compute_elapsed(&start_time);
680 /* Set the file encapsulation type now; we don't know what it is until
681 we've looked at all the packets, as we don't know until then whether
682 there's more than one type (and thus whether it's
683 WTAP_ENCAP_PER_PACKET). */
684 cf->lnk_t = wtap_file_encap(cf->wth);
686 cf->current_frame = frame_data_sequence_find(cf->frames, cf->first_displayed);
689 new_packet_list_thaw();
691 cf_callback_invoke(cf_cb_file_reload_finished, cf);
693 cf_callback_invoke(cf_cb_file_read_finished, cf);
695 /* If we have any displayed packets to select, select the first of those
696 packets by making the first row the selected row. */
697 if (cf->first_displayed != 0){
698 new_packet_list_select_first_row();
702 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
703 "%sFile loading was cancelled!%s\n"
705 "The remaining packets in the file were discarded.\n"
707 "As a lot of packets from the original file will be missing,\n"
708 "remember to be careful when saving the current content to a file.\n",
709 simple_dialog_primary_start(), simple_dialog_primary_end());
710 return CF_READ_ERROR;
714 /* Put up a message box noting that the read failed somewhere along
715 the line. Don't throw out the stuff we managed to read, though,
719 case WTAP_ERR_UNSUPPORTED:
720 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
721 "The capture file contains record data that TShark doesn't support.\n(%s)",
724 errmsg = errmsg_errno;
727 case WTAP_ERR_UNSUPPORTED_ENCAP:
728 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
729 "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
732 errmsg = errmsg_errno;
735 case WTAP_ERR_CANT_READ:
736 errmsg = "An attempt to read from the capture file failed for"
737 " some unknown reason.";
740 case WTAP_ERR_SHORT_READ:
741 errmsg = "The capture file appears to have been cut short"
742 " in the middle of a packet.";
745 case WTAP_ERR_BAD_FILE:
746 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
747 "The capture file appears to be damaged or corrupt.\n(%s)",
750 errmsg = errmsg_errno;
753 case WTAP_ERR_DECOMPRESS:
754 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
755 "The compressed capture file appears to be damaged or corrupt.\n"
758 errmsg = errmsg_errno;
762 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
763 "An error occurred while reading the"
764 " capture file: %s.", wtap_strerror(err));
765 errmsg = errmsg_errno;
768 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", errmsg);
769 return CF_READ_ERROR;
776 cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
778 cf_status_t cf_status;
780 cf_status = cf_open(cf, fname, is_tempfile, err);
785 cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
787 gint64 data_offset = 0;
789 volatile int newly_displayed_packets = 0;
791 gboolean filtering_tap_listeners;
795 /* Compile the current display filter.
796 * We assume this will not fail since cf->dfilter is only set in
797 * cf_filter IFF the filter was valid.
799 compiled = dfilter_compile(cf->dfilter, &dfcode);
800 g_assert(!cf->dfilter || (compiled && dfcode));
802 /* Do we have any tap listeners with filters? */
803 filtering_tap_listeners = have_filtering_tap_listeners();
805 /* Get the union of the flags for all tap listeners. */
806 tap_flags = union_of_tap_listener_flags();
810 new_packet_list_check_end();
811 /* Don't freeze/thaw the list when doing live capture */
812 /*new_packet_list_freeze();*/
814 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
816 while (to_read != 0) {
817 wtap_cleareof(cf->wth);
818 if (!wtap_read(cf->wth, err, &err_info, &data_offset)) {
821 if (cf->state == FILE_READ_ABORTED) {
822 /* Well, the user decided to exit Wireshark. Break out of the
823 loop, and let the code below (which is called even if there
824 aren't any packets left to read) exit. */
828 if (read_packet(cf, dfcode, filtering_tap_listeners, tap_flags,
829 data_offset) != -1) {
830 newly_displayed_packets++;
833 CATCH(OutOfMemoryError) {
836 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
837 "%sOut Of Memory!%s\n"
839 "Sorry, but Wireshark has to terminate now!\n"
841 "The capture file is not lost, it can be found at:\n"
844 "Some infos / workarounds can be found at:\n"
845 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
846 simple_dialog_primary_start(), simple_dialog_primary_end(), cf->filename);
847 /* we have to terminate, as we cannot recover from the memory error */
848 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
850 main_window_update();
851 /* XXX - how to avoid a busy wait? */
854 /* Don't freeze/thaw the list when doing live capture */
855 /*new_packet_list_thaw();*/
856 return CF_READ_ABORTED;
862 /* Update the file encapsulation; it might have changed based on the
863 packets we've read. */
864 cf->lnk_t = wtap_file_encap(cf->wth);
866 /* Cleanup and release all dfilter resources */
868 dfilter_free(dfcode);
871 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u",
872 cf->count, cf->state, *err);*/
874 /* Don't freeze/thaw the list when doing live capture */
875 /*new_packet_list_thaw();*/
876 /* With the new packet list the first packet
877 * isn't automatically selected.
879 if(!cf->current_frame)
880 new_packet_list_select_first_row();
882 /* moving to the end of the packet list - if the user requested so and
883 we have some new packets. */
884 if (newly_displayed_packets && auto_scroll_live && cf->count != 0)
885 new_packet_list_moveto_end();
887 if (cf->state == FILE_READ_ABORTED) {
888 /* Well, the user decided to exit Wireshark. Return CF_READ_ABORTED
889 so that our caller can kill off the capture child process;
890 this will cause an EOF on the pipe from the child, so
891 "cf_finish_tail()" will be called, and it will clean up
893 return CF_READ_ABORTED;
894 } else if (*err != 0) {
895 /* We got an error reading the capture file.
896 XXX - pop up a dialog box instead? */
897 g_warning("Error \"%s\" while reading: \"%s\"\n",
898 wtap_strerror(*err), cf->filename);
900 return CF_READ_ERROR;
906 cf_fake_continue_tail(capture_file *cf) {
907 cf->state = FILE_READ_DONE;
911 cf_finish_tail(capture_file *cf, int *err)
916 gboolean filtering_tap_listeners;
920 /* Compile the current display filter.
921 * We assume this will not fail since cf->dfilter is only set in
922 * cf_filter IFF the filter was valid.
924 compiled = dfilter_compile(cf->dfilter, &dfcode);
925 g_assert(!cf->dfilter || (compiled && dfcode));
927 /* Do we have any tap listeners with filters? */
928 filtering_tap_listeners = have_filtering_tap_listeners();
930 /* Get the union of the flags for all tap listeners. */
931 tap_flags = union_of_tap_listener_flags();
933 if(cf->wth == NULL) {
935 return CF_READ_ERROR;
938 new_packet_list_check_end();
939 /* Don't freeze/thaw the list when doing live capture */
940 /*new_packet_list_freeze();*/
942 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
943 if (cf->state == FILE_READ_ABORTED) {
944 /* Well, the user decided to abort the read. Break out of the
945 loop, and let the code below (which is called even if there
946 aren't any packets left to read) exit. */
949 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
952 /* Cleanup and release all dfilter resources */
954 dfilter_free(dfcode);
957 /* Don't freeze/thaw the list when doing live capture */
958 /*new_packet_list_thaw();*/
960 if (cf->state == FILE_READ_ABORTED) {
961 /* Well, the user decided to abort the read. We're only called
962 when the child capture process closes the pipe to us (meaning
963 it's probably exited), so we can just close the capture
964 file; we return CF_READ_ABORTED so our caller can do whatever
965 is appropriate when that happens. */
967 return CF_READ_ABORTED;
970 if (auto_scroll_live && cf->count != 0)
971 new_packet_list_moveto_end();
973 /* We're done reading sequentially through the file. */
974 cf->state = FILE_READ_DONE;
976 /* We're done reading sequentially through the file; close the
977 sequential I/O side, to free up memory it requires. */
978 wtap_sequential_close(cf->wth);
980 /* Allow the protocol dissectors to free up memory that they
981 * don't need after the sequential run-through of the packets. */
982 postseq_cleanup_all_protocols();
984 /* Update the file encapsulation; it might have changed based on the
985 packets we've read. */
986 cf->lnk_t = wtap_file_encap(cf->wth);
989 /* We got an error reading the capture file.
990 XXX - pop up a dialog box? */
991 return CF_READ_ERROR;
996 #endif /* HAVE_LIBPCAP */
999 cf_get_display_name(capture_file *cf)
1003 /* Return a name to use in displays */
1004 if (!cf->is_tempfile) {
1005 /* Get the last component of the file name, and use that. */
1007 displayname = g_filename_display_basename(cf->filename);
1009 displayname=g_strdup("(No file)");
1012 /* The file we read is a temporary file from a live capture or
1013 a merge operation; we don't mention its name, but, if it's
1014 from a capture, give the source of the capture. */
1016 displayname = g_strdup(cf->source);
1018 displayname = g_strdup("(Untitled)");
1024 void cf_set_tempfile_source(capture_file *cf, gchar *source) {
1030 cf->source = g_strdup(source);
1032 cf->source = g_strdup("");
1036 const gchar *cf_get_tempfile_source(capture_file *cf) {
1044 /* XXX - use a macro instead? */
1046 cf_get_packet_count(capture_file *cf)
1051 /* XXX - use a macro instead? */
1053 cf_set_packet_count(capture_file *cf, int packet_count)
1055 cf->count = packet_count;
1058 /* XXX - use a macro instead? */
1060 cf_is_tempfile(capture_file *cf)
1062 return cf->is_tempfile;
1065 void cf_set_tempfile(capture_file *cf, gboolean is_tempfile)
1067 cf->is_tempfile = is_tempfile;
1071 /* XXX - use a macro instead? */
1072 void cf_set_drops_known(capture_file *cf, gboolean drops_known)
1074 cf->drops_known = drops_known;
1077 /* XXX - use a macro instead? */
1078 void cf_set_drops(capture_file *cf, guint32 drops)
1083 /* XXX - use a macro instead? */
1084 gboolean cf_get_drops_known(capture_file *cf)
1086 return cf->drops_known;
1089 /* XXX - use a macro instead? */
1090 guint32 cf_get_drops(capture_file *cf)
1095 void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
1097 cf->rfcode = rfcode;
1101 find_and_mark_frame_depended_upon(gpointer data, gpointer user_data)
1103 frame_data *dependent_fd;
1104 guint32 dependent_frame = GPOINTER_TO_UINT(data);
1105 capture_file *cf = (capture_file *)user_data;
1107 dependent_fd = frame_data_sequence_find(cf->frames, dependent_frame);
1108 dependent_fd->flags.dependent_of_displayed = 1;
1112 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
1113 dfilter_t *dfcode, gboolean filtering_tap_listeners,
1115 union wtap_pseudo_header *pseudo_header, const guchar *buf,
1117 gboolean add_to_packet_list)
1119 gboolean create_proto_tree = FALSE;
1124 cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
1126 frame_data_set_before_dissect(fdata, &cf->elapsed_time,
1127 &first_ts, &prev_dis_ts, &prev_cap_ts);
1130 + we have a display filter and are re-applying it;
1131 + we have tap listeners with filters;
1132 + we have tap listeners that require a protocol tree;
1134 allocate a protocol tree root node, so that we'll construct
1135 a protocol tree against which a filter expression can be
1137 if ((dfcode != NULL && refilter) ||
1138 filtering_tap_listeners || (tap_flags & TL_REQUIRES_PROTO_TREE))
1139 create_proto_tree = TRUE;
1141 /* Dissect the frame. */
1142 epan_dissect_init(&edt, create_proto_tree, FALSE);
1144 if (dfcode != NULL && refilter) {
1145 epan_dissect_prime_dfilter(&edt, dfcode);
1148 tap_queue_init(&edt);
1149 epan_dissect_run(&edt, pseudo_header, buf, fdata, cinfo);
1150 tap_push_tapped_queue(&edt);
1152 /* If we have a display filter, apply it if we're refiltering, otherwise
1153 leave the "passed_dfilter" flag alone.
1155 If we don't have a display filter, set "passed_dfilter" to 1. */
1156 if (dfcode != NULL) {
1158 fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, &edt) ? 1 : 0;
1160 if (fdata->flags.passed_dfilter) {
1161 /* This frame passed the display filter but it may depend on other
1162 * (potentially not displayed) frames. Find those frames and mark them
1165 g_slist_foreach(edt.pi.dependent_frames, find_and_mark_frame_depended_upon, cf);
1169 fdata->flags.passed_dfilter = 1;
1171 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1172 cf->displayed_count++;
1174 if (add_to_packet_list) {
1175 /* We fill the needed columns from new_packet_list */
1176 row = new_packet_list_append(cinfo, fdata, &edt.pi);
1179 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1181 frame_data_set_after_dissect(fdata, &cum_bytes, &prev_dis_ts);
1183 /* If we haven't yet seen the first frame, this is it.
1185 XXX - we must do this before we add the row to the display,
1186 as, if the display's GtkCList's selection mode is
1187 GTK_SELECTION_BROWSE, when the first entry is added to it,
1188 "cf_select_packet()" will be called, and it will fetch the row
1189 data for the 0th row, and will get a null pointer rather than
1190 "fdata", as "gtk_clist_append()" won't yet have returned and
1191 thus "gtk_clist_set_row_data()" won't yet have been called.
1193 We thus need to leave behind bread crumbs so that
1194 "cf_select_packet()" can find this frame. See the comment
1195 in "cf_select_packet()". */
1196 if (cf->first_displayed == 0)
1197 cf->first_displayed = fdata->num;
1199 /* This is the last frame we've seen so far. */
1200 cf->last_displayed = fdata->num;
1203 epan_dissect_cleanup(&edt);
1207 /* read in a new packet */
1208 /* returns the row of the new packet in the packet list or -1 if not displayed */
1210 read_packet(capture_file *cf, dfilter_t *dfcode,
1211 gboolean filtering_tap_listeners, guint tap_flags, gint64 offset)
1213 const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
1214 union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
1215 const guchar *buf = wtap_buf_ptr(cf->wth);
1222 /* Add this packet's link-layer encapsulation type to cf->linktypes, if
1223 it's not already there.
1224 XXX - yes, this is O(N), so if every packet had a different
1225 link-layer encapsulation type, it'd be O(N^2) to read the file, but
1226 there are probably going to be a small number of encapsulation types
1228 cf_add_encapsulation_type(cf, phdr->pkt_encap);
1230 /* The frame number of this packet is one more than the count of
1231 frames in the file so far. */
1232 framenum = cf->count + 1;
1234 frame_data_init(&fdlocal, framenum, phdr, offset, cum_bytes);
1239 epan_dissect_init(&edt, TRUE, FALSE);
1240 epan_dissect_prime_dfilter(&edt, cf->rfcode);
1241 epan_dissect_run(&edt, pseudo_header, buf, &fdlocal, NULL);
1242 passed = dfilter_apply_edt(cf->rfcode, &edt);
1243 epan_dissect_cleanup(&edt);
1247 /* This does a shallow copy of fdlocal, which is good enough. */
1248 fdata = frame_data_sequence_add(cf->frames, &fdlocal);
1251 cf->f_datalen = offset + fdlocal.cap_len;
1253 if (!cf->redissecting) {
1254 row = add_packet_to_packet_list(fdata, cf, dfcode,
1255 filtering_tap_listeners, tap_flags,
1256 pseudo_header, buf, TRUE, TRUE);
1264 cf_merge_files(char **out_filenamep, int in_file_count,
1265 char *const *in_filenames, int file_type, gboolean do_append)
1267 merge_in_file_t *in_files, *in_file;
1272 int open_err, read_err, write_err, close_err;
1276 char errmsg_errno[1024+1];
1278 gboolean got_read_error = FALSE, got_write_error = FALSE;
1280 progdlg_t *progbar = NULL;
1282 gint64 f_len, file_pos;
1284 GTimeVal start_time;
1285 gchar status_str[100];
1286 gint64 progbar_nextstep;
1287 gint64 progbar_quantum;
1289 /* open the input files */
1290 if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
1291 &open_err, &err_info, &err_fileno)) {
1293 cf_open_failure_alert_box(in_filenames[err_fileno], open_err, err_info,
1298 if (*out_filenamep != NULL) {
1299 out_filename = *out_filenamep;
1300 out_fd = ws_open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
1304 out_fd = create_tempfile(&tmpname, "wireshark");
1307 out_filename = g_strdup(tmpname);
1308 *out_filenamep = out_filename;
1312 merge_close_in_files(in_file_count, in_files);
1314 cf_open_failure_alert_box(out_filename, open_err, NULL, TRUE, file_type);
1318 pdh = wtap_dump_fdopen(out_fd, file_type,
1319 merge_select_frame_type(in_file_count, in_files),
1320 merge_max_snapshot_length(in_file_count, in_files),
1321 FALSE /* compressed */, &open_err);
1324 merge_close_in_files(in_file_count, in_files);
1326 cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
1331 /* Get the sum of the sizes of all the files. */
1333 for (i = 0; i < in_file_count; i++)
1334 f_len += in_files[i].size;
1336 /* Update the progress bar when it gets to this value. */
1337 progbar_nextstep = 0;
1338 /* When we reach the value that triggers a progress bar update,
1339 bump that value by this amount. */
1340 progbar_quantum = f_len/N_PROGBAR_UPDATES;
1341 /* Progress so far. */
1345 g_get_current_time(&start_time);
1347 /* do the merge (or append) */
1350 in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
1353 in_file = merge_read_packet(in_file_count, in_files, &read_err,
1355 if (in_file == NULL) {
1360 if (read_err != 0) {
1361 /* I/O error reading from in_file */
1362 got_read_error = TRUE;
1366 /* Get the sum of the data offsets in all of the files. */
1368 for (i = 0; i < in_file_count; i++)
1369 data_offset += in_files[i].data_offset;
1371 /* Create the progress bar if necessary.
1372 We check on every iteration of the loop, so that it takes no
1373 longer than the standard time to create it (otherwise, for a
1374 large file, we might take considerably longer than that standard
1375 time in order to get to the next progress bar step). */
1376 if (progbar == NULL) {
1377 progbar = delayed_create_progress_dlg("Merging", "files",
1378 FALSE, &stop_flag, &start_time, progbar_val);
1381 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1382 when we update it, we have to run the GTK+ main loop to get it
1383 to repaint what's pending, and doing so may involve an "ioctl()"
1384 to see if there's any pending input from an X server, and doing
1385 that for every packet can be costly, especially on a big file. */
1386 if (data_offset >= progbar_nextstep) {
1387 /* Get the sum of the seek positions in all of the files. */
1389 for (i = 0; i < in_file_count; i++)
1390 file_pos += wtap_read_so_far(in_files[i].wth);
1391 progbar_val = (gfloat) file_pos / (gfloat) f_len;
1392 if (progbar_val > 1.0f) {
1393 /* Some file probably grew while we were reading it.
1394 That "shouldn't happen", so we'll just clip the progress
1398 if (progbar != NULL) {
1399 g_snprintf(status_str, sizeof(status_str),
1400 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
1401 file_pos / 1024, f_len / 1024);
1402 update_progress_dlg(progbar, progbar_val, status_str);
1404 progbar_nextstep += progbar_quantum;
1408 /* Well, the user decided to abort the merge. */
1412 if (!wtap_dump(pdh, wtap_phdr(in_file->wth), wtap_pseudoheader(in_file->wth),
1413 wtap_buf_ptr(in_file->wth), &write_err)) {
1414 got_write_error = TRUE;
1419 /* We're done merging the files; destroy the progress bar if it was created. */
1420 if (progbar != NULL)
1421 destroy_progress_dlg(progbar);
1423 merge_close_in_files(in_file_count, in_files);
1424 if (!got_read_error && !got_write_error) {
1425 if (!wtap_dump_close(pdh, &write_err))
1426 got_write_error = TRUE;
1428 wtap_dump_close(pdh, &close_err);
1430 if (got_read_error) {
1432 * Find the file on which we got the error, and report the error.
1434 for (i = 0; i < in_file_count; i++) {
1435 if (in_files[i].state == GOT_ERROR) {
1436 /* Put up a message box noting that a read failed somewhere along
1440 case WTAP_ERR_UNSUPPORTED_ENCAP:
1441 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1442 "The capture file %%s has a packet with a network type that Wireshark doesn't support.\n(%s)",
1445 errmsg = errmsg_errno;
1448 case WTAP_ERR_CANT_READ:
1449 errmsg = "An attempt to read from the capture file %s failed for"
1450 " some unknown reason.";
1453 case WTAP_ERR_SHORT_READ:
1454 errmsg = "The capture file %s appears to have been cut short"
1455 " in the middle of a packet.";
1458 case WTAP_ERR_BAD_FILE:
1459 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1460 "The capture file %%s appears to be damaged or corrupt.\n(%s)",
1463 errmsg = errmsg_errno;
1466 case WTAP_ERR_DECOMPRESS:
1467 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1468 "The compressed capture file %%s appears to be damaged or corrupt.\n"
1471 errmsg = errmsg_errno;
1475 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1476 "An error occurred while reading the"
1477 " capture file %%s: %s.", wtap_strerror(read_err));
1478 errmsg = errmsg_errno;
1481 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg, in_files[i].filename);
1486 if (got_write_error) {
1487 /* Put up an alert box for the write error. */
1488 if (write_err < 0) {
1489 /* Wiretap error. */
1490 switch (write_err) {
1492 case WTAP_ERR_UNSUPPORTED_ENCAP:
1494 * This is a problem with the particular frame we're writing;
1495 * note that, and give the frame number.
1497 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1498 "Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.",
1499 in_file->packet_num, in_file->filename,
1500 wtap_file_type_string(file_type));
1504 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1505 "An error occurred while writing to the file \"%s\": %s.",
1506 out_filename, wtap_strerror(write_err));
1511 write_failure_alert_box(out_filename, write_err);
1515 if (got_read_error || got_write_error || stop_flag) {
1516 /* Callers aren't expected to treat an error or an explicit abort
1517 differently - we put up error dialogs ourselves, so they don't
1525 cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
1527 const char *filter_new = dftext ? dftext : "";
1528 const char *filter_old = cf->dfilter ? cf->dfilter : "";
1530 GTimeVal start_time;
1532 /* if new filter equals old one, do nothing unless told to do so */
1533 if (!force && strcmp(filter_new, filter_old) == 0) {
1539 if (dftext == NULL) {
1540 /* The new filter is an empty filter (i.e., display all packets).
1541 * so leave dfcode==NULL
1545 * We have a filter; make a copy of it (as we'll be saving it),
1546 * and try to compile it.
1548 dftext = g_strdup(dftext);
1549 if (!dfilter_compile(dftext, &dfcode)) {
1550 /* The attempt failed; report an error. */
1551 gchar *safe_dftext = simple_dialog_format_message(dftext);
1552 gchar *safe_dfilter_error_msg = simple_dialog_format_message(
1554 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1557 "The following display filter isn't a valid display filter:\n%s\n"
1558 "See the help for a description of the display filter syntax.",
1559 simple_dialog_primary_start(), safe_dfilter_error_msg,
1560 simple_dialog_primary_end(), safe_dftext);
1561 g_free(safe_dfilter_error_msg);
1562 g_free(safe_dftext);
1568 if (dfcode == NULL) {
1569 /* Yes - free the filter text, and set it to null. */
1575 /* We have a valid filter. Replace the current filter. */
1576 g_free(cf->dfilter);
1577 cf->dfilter = dftext;
1578 g_get_current_time(&start_time);
1581 /* Now rescan the packet list, applying the new filter, but not
1582 throwing away information constructed on a previous pass. */
1583 if (dftext == NULL) {
1584 rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
1586 rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
1589 /* Cleanup and release all dfilter resources */
1590 dfilter_free(dfcode);
1596 cf_reftime_packets(capture_file *cf)
1599 ref_time_packets(cf);
1603 cf_redissect_packets(capture_file *cf)
1605 rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
1609 cf_read_frame_r(capture_file *cf, frame_data *fdata,
1610 union wtap_pseudo_header *pseudo_header, guint8 *pd)
1614 char errmsg_errno[1024+1];
1616 #ifdef WANT_PACKET_EDITOR
1617 /* if fdata->file_off == -1 it means packet was edited, and we must find data inside edited_frames tree */
1618 if (G_UNLIKELY(fdata->file_off == -1)) {
1619 const modified_frame_data *frame = (const modified_frame_data *) g_tree_lookup(cf->edited_frames, GINT_TO_POINTER(fdata->num));
1622 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "fdata->file_off == -1, but can't find modified frame!");
1626 *pseudo_header = frame->ph;
1627 memcpy(pd, frame->pd, fdata->cap_len);
1632 if (!wtap_seek_read(cf->wth, fdata->file_off, pseudo_header, pd,
1633 fdata->cap_len, &err, &err_info)) {
1636 case WTAP_ERR_UNSUPPORTED_ENCAP:
1637 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1638 "The file \"%%s\" has a packet with a network type that Wireshark doesn't support.\n(%s)",
1643 case WTAP_ERR_BAD_FILE:
1644 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1645 "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
1646 wtap_strerror(err), err_info);
1651 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1652 "An error occurred while reading from the file \"%%s\": %s.",
1653 wtap_strerror(err));
1656 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg_errno, cf->filename);
1663 cf_read_frame(capture_file *cf, frame_data *fdata)
1665 return cf_read_frame_r(cf, fdata, &cf->pseudo_header, cf->pd);
1668 /* Rescan the list of packets, reconstructing the CList.
1670 "action" describes why we're doing this; it's used in the progress
1673 "action_item" describes what we're doing; it's used in the progress
1676 "refilter" is TRUE if we need to re-evaluate the filter expression.
1678 "redissect" is TRUE if we need to make the dissectors reconstruct
1679 any state information they have (because a preference that affects
1680 some dissector has changed, meaning some dissector might construct
1681 its state differently from the way it was constructed the last time). */
1683 rescan_packets(capture_file *cf, const char *action, const char *action_item,
1684 gboolean refilter, gboolean redissect)
1686 /* Rescan packets new packet list */
1689 progdlg_t *progbar = NULL;
1692 frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
1693 int selected_frame_num, preceding_frame_num, following_frame_num, prev_frame_num;
1694 gboolean selected_frame_seen;
1696 GTimeVal start_time;
1697 gchar status_str[100];
1698 int progbar_nextstep;
1699 int progbar_quantum;
1701 gboolean filtering_tap_listeners;
1703 gboolean add_to_packet_list = FALSE;
1706 /* Compile the current display filter.
1707 * We assume this will not fail since cf->dfilter is only set in
1708 * cf_filter IFF the filter was valid.
1710 compiled = dfilter_compile(cf->dfilter, &dfcode);
1711 g_assert(!cf->dfilter || (compiled && dfcode));
1713 /* Do we have any tap listeners with filters? */
1714 filtering_tap_listeners = have_filtering_tap_listeners();
1716 /* Get the union of the flags for all tap listeners. */
1717 tap_flags = union_of_tap_listener_flags();
1719 reset_tap_listeners();
1720 /* Which frame, if any, is the currently selected frame?
1721 XXX - should the selected frame or the focus frame be the "current"
1722 frame, that frame being the one from which "Find Frame" searches
1724 selected_frame = cf->current_frame;
1726 /* Mark frame num as not found */
1727 selected_frame_num = -1;
1729 /* Freeze the packet list while we redo it, so we don't get any
1730 screen updates while it happens. */
1731 new_packet_list_freeze();
1734 /* We need to re-initialize all the state information that protocols
1735 keep, because some preference that controls a dissector has changed,
1736 which might cause the state information to be constructed differently
1737 by that dissector. */
1739 /* We might receive new packets while redissecting, and we don't
1740 want to dissect those before their time. */
1741 cf->redissecting = TRUE;
1743 /* Cleanup all data structures used for dissection. */
1744 cleanup_dissection();
1745 /* Initialize all data structures used for dissection. */
1748 /* We need to redissect the packets so we have to discard our old
1749 * packet list store. */
1750 new_packet_list_clear();
1751 add_to_packet_list = TRUE;
1754 /* We don't yet know which will be the first and last frames displayed. */
1755 cf->first_displayed = 0;
1756 cf->last_displayed = 0;
1758 /* We currently don't display any packets */
1759 cf->displayed_count = 0;
1761 /* Iterate through the list of frames. Call a routine for each frame
1762 to check whether it should be displayed and, if so, add it to
1763 the display list. */
1764 nstime_set_unset(&first_ts);
1765 nstime_set_unset(&prev_dis_ts);
1766 nstime_set_unset(&prev_cap_ts);
1769 /* Update the progress bar when it gets to this value. */
1770 progbar_nextstep = 0;
1771 /* When we reach the value that triggers a progress bar update,
1772 bump that value by this amount. */
1773 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1774 /* Count of packets at which we've looked. */
1776 /* Progress so far. */
1780 g_get_current_time(&start_time);
1782 /* no previous row yet */
1783 prev_frame_num = -1;
1786 preceding_frame_num = -1;
1787 preceding_frame = NULL;
1788 following_frame_num = -1;
1789 following_frame = NULL;
1791 selected_frame_seen = FALSE;
1793 for (framenum = 1; framenum <= cf->count; framenum++) {
1794 fdata = frame_data_sequence_find(cf->frames, framenum);
1796 /* Create the progress bar if necessary.
1797 We check on every iteration of the loop, so that it takes no
1798 longer than the standard time to create it (otherwise, for a
1799 large file, we might take considerably longer than that standard
1800 time in order to get to the next progress bar step). */
1801 if (progbar == NULL)
1802 progbar = delayed_create_progress_dlg(action, action_item, TRUE,
1803 &stop_flag, &start_time,
1806 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1807 when we update it, we have to run the GTK+ main loop to get it
1808 to repaint what's pending, and doing so may involve an "ioctl()"
1809 to see if there's any pending input from an X server, and doing
1810 that for every packet can be costly, especially on a big file. */
1811 if (count >= progbar_nextstep) {
1812 /* let's not divide by zero. I should never be started
1813 * with count == 0, so let's assert that
1815 g_assert(cf->count > 0);
1816 progbar_val = (gfloat) count / cf->count;
1818 if (progbar != NULL) {
1819 g_snprintf(status_str, sizeof(status_str),
1820 "%4u of %u frames", count, cf->count);
1821 update_progress_dlg(progbar, progbar_val, status_str);
1824 progbar_nextstep += progbar_quantum;
1828 /* Well, the user decided to abort the filtering. Just stop.
1830 XXX - go back to the previous filter? Users probably just
1831 want not to wait for a filtering operation to finish;
1832 unless we cancel by having no filter, reverting to the
1833 previous filter will probably be even more expensive than
1834 continuing the filtering, as it involves going back to the
1835 beginning and filtering, and even with no filter we currently
1836 have to re-generate the entire clist, which is also expensive.
1838 I'm not sure what Network Monitor does, but it doesn't appear
1839 to give you an unfiltered display if you cancel. */
1846 /* Since all state for the frame was destroyed, mark the frame
1847 * as not visited, free the GSList referring to the state
1848 * data (the per-frame data itself was freed by
1849 * "init_dissection()"), and null out the GSList pointer. */
1850 fdata->flags.visited = 0;
1851 frame_data_cleanup(fdata);
1854 if (redissect || refilter) {
1855 /* If we're redissecting or refiltering then any frame dependencies
1856 * from the previous dissection/filtering are no longer valid.
1858 fdata->flags.dependent_of_displayed = 0;
1861 if (!cf_read_frame(cf, fdata))
1862 break; /* error reading the frame */
1864 /* If the previous frame is displayed, and we haven't yet seen the
1865 selected frame, remember that frame - it's the closest one we've
1866 yet seen before the selected frame. */
1867 if (prev_frame_num != -1 && !selected_frame_seen && prev_frame->flags.passed_dfilter) {
1868 preceding_frame_num = prev_frame_num;
1869 preceding_frame = prev_frame;
1871 add_packet_to_packet_list(fdata, cf, dfcode, filtering_tap_listeners,
1872 tap_flags, &cf->pseudo_header, cf->pd,
1874 add_to_packet_list);
1876 /* If this frame is displayed, and this is the first frame we've
1877 seen displayed after the selected frame, remember this frame -
1878 it's the closest one we've yet seen at or after the selected
1880 if (fdata->flags.passed_dfilter && selected_frame_seen && following_frame_num == -1) {
1881 following_frame_num = fdata->num;
1882 following_frame = fdata;
1884 if (fdata == selected_frame) {
1885 selected_frame_seen = TRUE;
1886 if (fdata->flags.passed_dfilter)
1887 selected_frame_num = fdata->num;
1890 /* Remember this frame - it'll be the previous frame
1891 on the next pass through the loop. */
1892 prev_frame_num = fdata->num;
1896 /* We are done redissecting the packet list. */
1897 cf->redissecting = FALSE;
1900 /* Clear out what remains of the visited flags and per-frame data
1903 XXX - that may cause various forms of bogosity when dissecting
1904 these frames, as they won't have been seen by this sequential
1905 pass, but the only alternative I see is to keep scanning them
1906 even though the user requested that the scan stop, and that
1907 would leave the user stuck with an Wireshark grinding on
1908 until it finishes. Should we just stick them with that? */
1909 for (; framenum <= cf->count; framenum++) {
1910 fdata = frame_data_sequence_find(cf->frames, framenum);
1911 fdata->flags.visited = 0;
1912 frame_data_cleanup(fdata);
1916 /* We're done filtering the packets; destroy the progress bar if it
1918 if (progbar != NULL)
1919 destroy_progress_dlg(progbar);
1921 /* Unfreeze the packet list. */
1922 if (!add_to_packet_list)
1923 new_packet_list_recreate_visible_rows();
1925 /* Compute the time it took to filter the file */
1926 compute_elapsed(&start_time);
1928 new_packet_list_thaw();
1930 if (selected_frame_num == -1) {
1931 /* The selected frame didn't pass the filter. */
1932 if (selected_frame == NULL) {
1933 /* That's because there *was* no selected frame. Make the first
1934 displayed frame the current frame. */
1935 selected_frame_num = 0;
1937 /* Find the nearest displayed frame to the selected frame (whether
1938 it's before or after that frame) and make that the current frame.
1939 If the next and previous displayed frames are equidistant from the
1940 selected frame, choose the next one. */
1941 g_assert(following_frame == NULL ||
1942 following_frame->num >= selected_frame->num);
1943 g_assert(preceding_frame == NULL ||
1944 preceding_frame->num <= selected_frame->num);
1945 if (following_frame == NULL) {
1946 /* No frame after the selected frame passed the filter, so we
1947 have to select the last displayed frame before the selected
1949 selected_frame_num = preceding_frame_num;
1950 selected_frame = preceding_frame;
1951 } else if (preceding_frame == NULL) {
1952 /* No frame before the selected frame passed the filter, so we
1953 have to select the first displayed frame after the selected
1955 selected_frame_num = following_frame_num;
1956 selected_frame = following_frame;
1958 /* Frames before and after the selected frame passed the filter, so
1959 we'll select the previous frame */
1960 selected_frame_num = preceding_frame_num;
1961 selected_frame = preceding_frame;
1966 if (selected_frame_num == -1) {
1967 /* There are no frames displayed at all. */
1968 cf_unselect_packet(cf);
1970 /* Either the frame that was selected passed the filter, or we've
1971 found the nearest displayed frame to that frame. Select it, make
1972 it the focus row, and make it visible. */
1973 /* Set to invalid to force update of packet list and packet details */
1974 cf->current_row = -1;
1975 if (selected_frame_num == 0) {
1976 new_packet_list_select_first_row();
1978 if (!new_packet_list_select_row_from_data(selected_frame)) {
1979 /* We didn't find a row corresponding to this frame.
1980 This means that the frame isn't being displayed currently,
1981 so we can't select it. */
1982 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
1983 "%sEnd of capture exceeded!%s\n\n"
1984 "The capture file is probably not fully dissected.",
1985 simple_dialog_primary_start(), simple_dialog_primary_end());
1990 /* Cleanup and release all dfilter resources */
1991 dfilter_free(dfcode);
1996 * Scan trough all frame data and recalculate the ref time
1997 * without rereading the file.
1998 * XXX - do we need a progres bar or is this fast enough?
2001 ref_time_packets(capture_file *cf)
2006 nstime_set_unset(&first_ts);
2007 nstime_set_unset(&prev_dis_ts);
2010 for (framenum = 1; framenum <= cf->count; framenum++) {
2011 fdata = frame_data_sequence_find(cf->frames, framenum);
2013 /* just add some value here until we know if it is being displayed or not */
2014 fdata->cum_bytes = cum_bytes + fdata->pkt_len;
2020 /* If we don't have the time stamp of the first packet in the
2021 capture, it's because this is the first packet. Save the time
2022 stamp of this packet as the time stamp of the first packet. */
2023 if (nstime_is_unset(&first_ts)) {
2024 first_ts = fdata->abs_ts;
2026 /* if this frames is marked as a reference time frame, reset
2027 firstsec and firstusec to this frame */
2028 if(fdata->flags.ref_time){
2029 first_ts = fdata->abs_ts;
2032 /* If we don't have the time stamp of the previous displayed packet,
2033 it's because this is the first displayed packet. Save the time
2034 stamp of this packet as the time stamp of the previous displayed
2036 if (nstime_is_unset(&prev_dis_ts)) {
2037 prev_dis_ts = fdata->abs_ts;
2040 /* Get the time elapsed between the first packet and this packet. */
2041 nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
2043 /* If it's greater than the current elapsed time, set the elapsed time
2044 to it (we check for "greater than" so as not to be confused by
2045 time moving backwards). */
2046 if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
2047 || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
2048 cf->elapsed_time = fdata->rel_ts;
2051 /* If this frame is displayed, get the time elapsed between the
2052 previous displayed packet and this packet. */
2053 if( fdata->flags.passed_dfilter ) {
2054 nstime_delta(&fdata->del_dis_ts, &fdata->abs_ts, &prev_dis_ts);
2055 prev_dis_ts = fdata->abs_ts;
2061 if( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) ){
2062 /* This frame either passed the display filter list or is marked as
2063 a time reference frame. All time reference frames are displayed
2064 even if they dont pass the display filter */
2065 if(fdata->flags.ref_time){
2066 /* if this was a TIME REF frame we should reset the cum_bytes field */
2067 cum_bytes = fdata->pkt_len;
2068 fdata->cum_bytes = cum_bytes;
2070 /* increase cum_bytes with this packets length */
2071 cum_bytes += fdata->pkt_len;
2084 process_specified_packets(capture_file *cf, packet_range_t *range,
2085 const char *string1, const char *string2, gboolean terminate_is_stop,
2086 gboolean (*callback)(capture_file *, frame_data *,
2087 union wtap_pseudo_header *, const guint8 *, void *),
2088 void *callback_args)
2092 union wtap_pseudo_header pseudo_header;
2093 guint8 pd[WTAP_MAX_PACKET_SIZE+1];
2094 psp_return_t ret = PSP_FINISHED;
2096 progdlg_t *progbar = NULL;
2099 gboolean progbar_stop_flag;
2100 GTimeVal progbar_start_time;
2101 gchar progbar_status_str[100];
2102 int progbar_nextstep;
2103 int progbar_quantum;
2104 range_process_e process_this;
2106 /* Update the progress bar when it gets to this value. */
2107 progbar_nextstep = 0;
2108 /* When we reach the value that triggers a progress bar update,
2109 bump that value by this amount. */
2110 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2111 /* Count of packets at which we've looked. */
2113 /* Progress so far. */
2116 progbar_stop_flag = FALSE;
2117 g_get_current_time(&progbar_start_time);
2120 packet_range_process_init(range);
2122 /* Iterate through all the packets, printing the packets that
2123 were selected by the current display filter. */
2124 for (framenum = 1; framenum <= cf->count; framenum++) {
2125 fdata = frame_data_sequence_find(cf->frames, framenum);
2127 /* Create the progress bar if necessary.
2128 We check on every iteration of the loop, so that it takes no
2129 longer than the standard time to create it (otherwise, for a
2130 large file, we might take considerably longer than that standard
2131 time in order to get to the next progress bar step). */
2132 if (progbar == NULL)
2133 progbar = delayed_create_progress_dlg(string1, string2,
2136 &progbar_start_time,
2139 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2140 when we update it, we have to run the GTK+ main loop to get it
2141 to repaint what's pending, and doing so may involve an "ioctl()"
2142 to see if there's any pending input from an X server, and doing
2143 that for every packet can be costly, especially on a big file. */
2144 if (progbar_count >= progbar_nextstep) {
2145 /* let's not divide by zero. I should never be started
2146 * with count == 0, so let's assert that
2148 g_assert(cf->count > 0);
2149 progbar_val = (gfloat) progbar_count / cf->count;
2151 if (progbar != NULL) {
2152 g_snprintf(progbar_status_str, sizeof(progbar_status_str),
2153 "%4u of %u packets", progbar_count, cf->count);
2154 update_progress_dlg(progbar, progbar_val, progbar_status_str);
2157 progbar_nextstep += progbar_quantum;
2160 if (progbar_stop_flag) {
2161 /* Well, the user decided to abort the operation. Just stop,
2162 and arrange to return PSP_STOPPED to our caller, so they know
2163 it was stopped explicitly. */
2170 if (range != NULL) {
2171 /* do we have to process this packet? */
2172 process_this = packet_range_process_packet(range, fdata);
2173 if (process_this == range_process_next) {
2174 /* this packet uninteresting, continue with next one */
2176 } else if (process_this == range_processing_finished) {
2177 /* all interesting packets processed, stop the loop */
2182 /* Get the packet */
2183 if (!cf_read_frame_r(cf, fdata, &pseudo_header, pd)) {
2184 /* Attempt to get the packet failed. */
2188 /* Process the packet */
2189 if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
2190 /* Callback failed. We assume it reported the error appropriately. */
2196 /* We're done printing the packets; destroy the progress bar if
2198 if (progbar != NULL)
2199 destroy_progress_dlg(progbar);
2205 gboolean construct_protocol_tree;
2207 } retap_callback_args_t;
2210 retap_packet(capture_file *cf _U_, frame_data *fdata,
2211 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2214 retap_callback_args_t *args = argsp;
2217 epan_dissect_init(&edt, args->construct_protocol_tree, FALSE);
2218 tap_queue_init(&edt);
2219 epan_dissect_run(&edt, pseudo_header, pd, fdata, args->cinfo);
2220 tap_push_tapped_queue(&edt);
2221 epan_dissect_cleanup(&edt);
2227 cf_retap_packets(capture_file *cf)
2229 packet_range_t range;
2230 retap_callback_args_t callback_args;
2231 gboolean filtering_tap_listeners;
2234 /* Do we have any tap listeners with filters? */
2235 filtering_tap_listeners = have_filtering_tap_listeners();
2237 tap_flags = union_of_tap_listener_flags();
2239 /* If any tap listeners have filters, or require the protocol tree,
2240 construct the protocol tree. */
2241 callback_args.construct_protocol_tree = filtering_tap_listeners ||
2242 (tap_flags & TL_REQUIRES_PROTO_TREE);
2244 /* If any tap listeners require the columns, construct them. */
2245 callback_args.cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
2247 /* Reset the tap listeners. */
2248 reset_tap_listeners();
2250 /* Iterate through the list of packets, dissecting all packets and
2251 re-running the taps. */
2252 packet_range_init(&range);
2253 packet_range_process_init(&range);
2254 switch (process_specified_packets(cf, &range, "Recalculating statistics on",
2255 "all packets", TRUE, retap_packet,
2258 /* Completed successfully. */
2262 /* Well, the user decided to abort the refiltering.
2263 Return CF_READ_ABORTED so our caller knows they did that. */
2264 return CF_READ_ABORTED;
2267 /* Error while retapping. */
2268 return CF_READ_ERROR;
2271 g_assert_not_reached();
2276 print_args_t *print_args;
2277 gboolean print_header_line;
2278 char *header_line_buf;
2279 int header_line_buf_len;
2280 gboolean print_formfeed;
2281 gboolean print_separator;
2285 } print_callback_args_t;
2288 print_packet(capture_file *cf, frame_data *fdata,
2289 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2292 print_callback_args_t *args = argsp;
2299 gboolean proto_tree_needed;
2300 char bookmark_name[9+10+1]; /* "__frameNNNNNNNNNN__\0" */
2301 char bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0" */
2303 /* Create the protocol tree, and make it visible, if we're printing
2304 the dissection or the hex data.
2305 XXX - do we need it if we're just printing the hex data? */
2307 args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex || have_custom_cols(&cf->cinfo);
2308 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2310 /* Fill in the column information if we're printing the summary
2312 if (args->print_args->print_summary) {
2313 col_custom_prime_edt(&edt, &cf->cinfo);
2314 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2315 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2317 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2319 if (args->print_formfeed) {
2320 if (!new_page(args->print_args->stream))
2323 if (args->print_separator) {
2324 if (!print_line(args->print_args->stream, 0, ""))
2330 * We generate bookmarks, if the output format supports them.
2331 * The name is "__frameN__".
2333 g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
2335 if (args->print_args->print_summary) {
2336 if (args->print_header_line) {
2337 if (!print_line(args->print_args->stream, 0, args->header_line_buf))
2339 args->print_header_line = FALSE; /* we might not need to print any more */
2341 cp = &args->line_buf[0];
2343 for (i = 0; i < cf->cinfo.num_cols; i++) {
2344 /* Find the length of the string for this column. */
2345 column_len = (int) strlen(cf->cinfo.col_data[i]);
2346 if (args->col_widths[i] > column_len)
2347 column_len = args->col_widths[i];
2349 /* Make sure there's room in the line buffer for the column; if not,
2350 double its length. */
2351 line_len += column_len + 1; /* "+1" for space */
2352 if (line_len > args->line_buf_len) {
2353 cp_off = (int) (cp - args->line_buf);
2354 args->line_buf_len = 2 * line_len;
2355 args->line_buf = g_realloc(args->line_buf, args->line_buf_len + 1);
2356 cp = args->line_buf + cp_off;
2359 /* Right-justify the packet number column. */
2360 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2361 g_snprintf(cp, column_len+1, "%*s", args->col_widths[i], cf->cinfo.col_data[i]);
2363 g_snprintf(cp, column_len+1, "%-*s", args->col_widths[i], cf->cinfo.col_data[i]);
2365 if (i != cf->cinfo.num_cols - 1)
2371 * Generate a bookmark, using the summary line as the title.
2373 if (!print_bookmark(args->print_args->stream, bookmark_name,
2377 if (!print_line(args->print_args->stream, 0, args->line_buf))
2381 * Generate a bookmark, using "Frame N" as the title, as we're not
2382 * printing the summary line.
2384 g_snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
2385 if (!print_bookmark(args->print_args->stream, bookmark_name,
2388 } /* if (print_summary) */
2390 if (args->print_args->print_dissections != print_dissections_none) {
2391 if (args->print_args->print_summary) {
2392 /* Separate the summary line from the tree with a blank line. */
2393 if (!print_line(args->print_args->stream, 0, ""))
2397 /* Print the information in that tree. */
2398 if (!proto_tree_print(args->print_args, &edt, args->print_args->stream))
2401 /* Print a blank line if we print anything after this (aka more than one packet). */
2402 args->print_separator = TRUE;
2404 /* Print a header line if we print any more packet summaries */
2405 args->print_header_line = TRUE;
2408 if (args->print_args->print_hex) {
2409 /* Print the full packet data as hex. */
2410 if (!print_hex_data(args->print_args->stream, &edt))
2413 /* Print a blank line if we print anything after this (aka more than one packet). */
2414 args->print_separator = TRUE;
2416 /* Print a header line if we print any more packet summaries */
2417 args->print_header_line = TRUE;
2418 } /* if (args->print_args->print_dissections != print_dissections_none) */
2420 epan_dissect_cleanup(&edt);
2422 /* do we want to have a formfeed between each packet from now on? */
2423 if(args->print_args->print_formfeed) {
2424 args->print_formfeed = TRUE;
2430 epan_dissect_cleanup(&edt);
2435 cf_print_packets(capture_file *cf, print_args_t *print_args)
2438 print_callback_args_t callback_args;
2446 callback_args.print_args = print_args;
2447 callback_args.print_header_line = TRUE;
2448 callback_args.header_line_buf = NULL;
2449 callback_args.header_line_buf_len = 256;
2450 callback_args.print_formfeed = FALSE;
2451 callback_args.print_separator = FALSE;
2452 callback_args.line_buf = NULL;
2453 callback_args.line_buf_len = 256;
2454 callback_args.col_widths = NULL;
2456 if (!print_preamble(print_args->stream, cf->filename)) {
2457 destroy_print_stream(print_args->stream);
2458 return CF_PRINT_WRITE_ERROR;
2461 if (print_args->print_summary) {
2462 /* We're printing packet summaries. Allocate the header line buffer
2463 and get the column widths. */
2464 callback_args.header_line_buf = g_malloc(callback_args.header_line_buf_len + 1);
2466 /* Find the widths for each of the columns - maximum of the
2467 width of the title and the width of the data - and construct
2468 a buffer with a line containing the column titles. */
2469 callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * cf->cinfo.num_cols);
2470 cp = &callback_args.header_line_buf[0];
2472 for (i = 0; i < cf->cinfo.num_cols; i++) {
2473 /* Don't pad the last column. */
2474 if (i == cf->cinfo.num_cols - 1)
2475 callback_args.col_widths[i] = 0;
2477 callback_args.col_widths[i] = (gint) strlen(cf->cinfo.col_title[i]);
2478 data_width = get_column_char_width(get_column_format(i));
2479 if (data_width > callback_args.col_widths[i])
2480 callback_args.col_widths[i] = data_width;
2483 /* Find the length of the string for this column. */
2484 column_len = (int) strlen(cf->cinfo.col_title[i]);
2485 if (callback_args.col_widths[i] > column_len)
2486 column_len = callback_args.col_widths[i];
2488 /* Make sure there's room in the line buffer for the column; if not,
2489 double its length. */
2490 line_len += column_len + 1; /* "+1" for space */
2491 if (line_len > callback_args.header_line_buf_len) {
2492 cp_off = (int) (cp - callback_args.header_line_buf);
2493 callback_args.header_line_buf_len = 2 * line_len;
2494 callback_args.header_line_buf = g_realloc(callback_args.header_line_buf,
2495 callback_args.header_line_buf_len + 1);
2496 cp = callback_args.header_line_buf + cp_off;
2499 /* Right-justify the packet number column. */
2500 /* if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2501 g_snprintf(cp, column_len+1, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2503 g_snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2505 if (i != cf->cinfo.num_cols - 1)
2510 /* Now start out the main line buffer with the same length as the
2511 header line buffer. */
2512 callback_args.line_buf_len = callback_args.header_line_buf_len;
2513 callback_args.line_buf = g_malloc(callback_args.line_buf_len + 1);
2514 } /* if (print_summary) */
2516 /* Iterate through the list of packets, printing the packets we were
2518 ret = process_specified_packets(cf, &print_args->range, "Printing",
2519 "selected packets", TRUE, print_packet,
2522 g_free(callback_args.header_line_buf);
2523 g_free(callback_args.line_buf);
2524 g_free(callback_args.col_widths);
2529 /* Completed successfully. */
2533 /* Well, the user decided to abort the printing.
2535 XXX - note that what got generated before they did that
2536 will get printed if we're piping to a print program; we'd
2537 have to write to a file and then hand that to the print
2538 program to make it actually not print anything. */
2542 /* Error while printing.
2544 XXX - note that what got generated before they did that
2545 will get printed if we're piping to a print program; we'd
2546 have to write to a file and then hand that to the print
2547 program to make it actually not print anything. */
2548 destroy_print_stream(print_args->stream);
2549 return CF_PRINT_WRITE_ERROR;
2552 if (!print_finale(print_args->stream)) {
2553 destroy_print_stream(print_args->stream);
2554 return CF_PRINT_WRITE_ERROR;
2557 if (!destroy_print_stream(print_args->stream))
2558 return CF_PRINT_WRITE_ERROR;
2564 write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
2565 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2571 /* Create the protocol tree, but don't fill in the column information. */
2572 epan_dissect_init(&edt, TRUE, TRUE);
2573 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2575 /* Write out the information in that tree. */
2576 proto_tree_write_pdml(&edt, fh);
2578 epan_dissect_cleanup(&edt);
2584 cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
2589 fh = ws_fopen(print_args->file, "w");
2591 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2593 write_pdml_preamble(fh, cf->filename);
2596 return CF_PRINT_WRITE_ERROR;
2599 /* Iterate through the list of packets, printing the packets we were
2601 ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
2602 "selected packets", TRUE,
2603 write_pdml_packet, fh);
2608 /* Completed successfully. */
2612 /* Well, the user decided to abort the printing. */
2616 /* Error while printing. */
2618 return CF_PRINT_WRITE_ERROR;
2621 write_pdml_finale(fh);
2624 return CF_PRINT_WRITE_ERROR;
2627 /* XXX - check for an error */
2634 write_psml_packet(capture_file *cf, frame_data *fdata,
2635 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2640 gboolean proto_tree_needed;
2642 /* Fill in the column information, only create the protocol tree
2643 if having custom columns. */
2644 proto_tree_needed = have_custom_cols(&cf->cinfo);
2645 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2646 col_custom_prime_edt(&edt, &cf->cinfo);
2647 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2648 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2650 /* Write out the information in that tree. */
2651 proto_tree_write_psml(&edt, fh);
2653 epan_dissect_cleanup(&edt);
2659 cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
2664 fh = ws_fopen(print_args->file, "w");
2666 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2668 write_psml_preamble(fh);
2671 return CF_PRINT_WRITE_ERROR;
2674 /* Iterate through the list of packets, printing the packets we were
2676 ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
2677 "selected packets", TRUE,
2678 write_psml_packet, fh);
2683 /* Completed successfully. */
2687 /* Well, the user decided to abort the printing. */
2691 /* Error while printing. */
2693 return CF_PRINT_WRITE_ERROR;
2696 write_psml_finale(fh);
2699 return CF_PRINT_WRITE_ERROR;
2702 /* XXX - check for an error */
2709 write_csv_packet(capture_file *cf, frame_data *fdata,
2710 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2715 gboolean proto_tree_needed;
2717 /* Fill in the column information, only create the protocol tree
2718 if having custom columns. */
2719 proto_tree_needed = have_custom_cols(&cf->cinfo);
2720 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2721 col_custom_prime_edt(&edt, &cf->cinfo);
2722 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2723 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2725 /* Write out the information in that tree. */
2726 proto_tree_write_csv(&edt, fh);
2728 epan_dissect_cleanup(&edt);
2734 cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
2739 fh = ws_fopen(print_args->file, "w");
2741 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2743 write_csv_preamble(fh);
2746 return CF_PRINT_WRITE_ERROR;
2749 /* Iterate through the list of packets, printing the packets we were
2751 ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
2752 "selected packets", TRUE,
2753 write_csv_packet, fh);
2758 /* Completed successfully. */
2762 /* Well, the user decided to abort the printing. */
2766 /* Error while printing. */
2768 return CF_PRINT_WRITE_ERROR;
2771 write_csv_finale(fh);
2774 return CF_PRINT_WRITE_ERROR;
2777 /* XXX - check for an error */
2784 write_carrays_packet(capture_file *cf _U_, frame_data *fdata,
2785 union wtap_pseudo_header *pseudo_header,
2786 const guint8 *pd, void *argsp)
2791 epan_dissect_init(&edt, TRUE, TRUE);
2792 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2793 proto_tree_write_carrays(fdata->num, fh, &edt);
2794 epan_dissect_cleanup(&edt);
2800 cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
2805 fh = ws_fopen(print_args->file, "w");
2808 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2810 write_carrays_preamble(fh);
2814 return CF_PRINT_WRITE_ERROR;
2817 /* Iterate through the list of packets, printing the packets we were
2819 ret = process_specified_packets(cf, &print_args->range,
2821 "selected packets", TRUE,
2822 write_carrays_packet, fh);
2825 /* Completed successfully. */
2828 /* Well, the user decided to abort the printing. */
2831 /* Error while printing. */
2833 return CF_PRINT_WRITE_ERROR;
2836 write_carrays_finale(fh);
2840 return CF_PRINT_WRITE_ERROR;
2848 cf_find_packet_protocol_tree(capture_file *cf, const char *string,
2849 search_direction dir)
2853 mdata.string = string;
2854 mdata.string_len = strlen(string);
2855 return find_packet(cf, match_protocol_tree, &mdata, dir);
2859 cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree, match_data *mdata)
2861 mdata->frame_matched = FALSE;
2862 mdata->string = convert_string_case(cf->sfilter, cf->case_type);
2863 mdata->string_len = strlen(mdata->string);
2865 /* Iterate through all the nodes looking for matching text */
2866 proto_tree_children_foreach(tree, match_subtree_text, mdata);
2867 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2871 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
2873 match_data *mdata = criterion;
2876 /* Load the frame's data. */
2877 if (!cf_read_frame(cf, fdata)) {
2878 /* Attempt to get the packet failed. */
2882 /* Construct the protocol tree, including the displayed text */
2883 epan_dissect_init(&edt, TRUE, TRUE);
2884 /* We don't need the column information */
2885 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
2887 /* Iterate through all the nodes, seeing if they have text that matches. */
2889 mdata->frame_matched = FALSE;
2890 proto_tree_children_foreach(edt.tree, match_subtree_text, mdata);
2891 epan_dissect_cleanup(&edt);
2892 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2896 match_subtree_text(proto_node *node, gpointer data)
2898 match_data *mdata = (match_data *) data;
2899 const gchar *string = mdata->string;
2900 size_t string_len = mdata->string_len;
2901 capture_file *cf = mdata->cf;
2902 field_info *fi = PNODE_FINFO(node);
2903 gchar label_str[ITEM_LABEL_LENGTH];
2910 g_assert(fi && "dissection with an invisible proto tree?");
2912 if (mdata->frame_matched) {
2913 /* We already had a match; don't bother doing any more work. */
2917 /* Don't match invisible entries. */
2918 if (PROTO_ITEM_IS_HIDDEN(node))
2921 /* was a free format label produced? */
2923 label_ptr = fi->rep->representation;
2925 /* no, make a generic label */
2926 label_ptr = label_str;
2927 proto_item_fill_label(fi, label_str);
2930 /* Does that label match? */
2931 label_len = strlen(label_ptr);
2932 for (i = 0; i < label_len; i++) {
2933 c_char = label_ptr[i];
2935 c_char = toupper(c_char);
2936 if (c_char == string[c_match]) {
2938 if (c_match == string_len) {
2939 /* No need to look further; we have a match */
2940 mdata->frame_matched = TRUE;
2948 /* Recurse into the subtree, if it exists */
2949 if (node->first_child != NULL)
2950 proto_tree_children_foreach(node, match_subtree_text, mdata);
2954 cf_find_packet_summary_line(capture_file *cf, const char *string,
2955 search_direction dir)
2959 mdata.string = string;
2960 mdata.string_len = strlen(string);
2961 return find_packet(cf, match_summary_line, &mdata, dir);
2965 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
2967 match_data *mdata = criterion;
2968 const gchar *string = mdata->string;
2969 size_t string_len = mdata->string_len;
2971 const char *info_column;
2972 size_t info_column_len;
2973 match_result result = MR_NOTMATCHED;
2979 /* Load the frame's data. */
2980 if (!cf_read_frame(cf, fdata)) {
2981 /* Attempt to get the packet failed. */
2985 /* Don't bother constructing the protocol tree */
2986 epan_dissect_init(&edt, FALSE, FALSE);
2987 /* Get the column information */
2988 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, &cf->cinfo);
2990 /* Find the Info column */
2991 for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
2992 if (cf->cinfo.fmt_matx[colx][COL_INFO]) {
2993 /* Found it. See if we match. */
2994 info_column = edt.pi.cinfo->col_data[colx];
2995 info_column_len = strlen(info_column);
2996 for (i = 0; i < info_column_len; i++) {
2997 c_char = info_column[i];
2999 c_char = toupper(c_char);
3000 if (c_char == string[c_match]) {
3002 if (c_match == string_len) {
3003 result = MR_MATCHED;
3012 epan_dissect_cleanup(&edt);
3019 } cbs_t; /* "Counted byte string" */
3022 cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
3023 search_direction dir)
3028 info.data_len = string_size;
3030 /* String or hex search? */
3032 /* String search - what type of string? */
3033 switch (cf->scs_type) {
3035 case SCS_ASCII_AND_UNICODE:
3036 return find_packet(cf, match_ascii_and_unicode, &info, dir);
3039 return find_packet(cf, match_ascii, &info, dir);
3042 return find_packet(cf, match_unicode, &info, dir);
3045 g_assert_not_reached();
3049 return find_packet(cf, match_binary, &info, dir);
3053 match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
3055 cbs_t *info = criterion;
3056 const guint8 *ascii_text = info->data;
3057 size_t textlen = info->data_len;
3058 match_result result;
3064 /* Load the frame's data. */
3065 if (!cf_read_frame(cf, fdata)) {
3066 /* Attempt to get the packet failed. */
3070 result = MR_NOTMATCHED;
3071 buf_len = fdata->pkt_len;
3073 while (i < buf_len) {
3076 c_char = toupper(c_char);
3077 if (c_char != '\0') {
3078 if (c_char == ascii_text[c_match]) {
3080 if (c_match == textlen) {
3081 result = MR_MATCHED;
3082 cf->search_pos = i; /* Save the position of the last character
3083 for highlighting the field. */
3088 g_assert(i>=c_match);
3089 i -= (guint32)c_match;
3099 match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
3101 cbs_t *info = criterion;
3102 const guint8 *ascii_text = info->data;
3103 size_t textlen = info->data_len;
3104 match_result result;
3110 /* Load the frame's data. */
3111 if (!cf_read_frame(cf, fdata)) {
3112 /* Attempt to get the packet failed. */
3116 result = MR_NOTMATCHED;
3117 buf_len = fdata->pkt_len;
3119 while (i < buf_len) {
3122 c_char = toupper(c_char);
3123 if (c_char == ascii_text[c_match]) {
3125 if (c_match == textlen) {
3126 result = MR_MATCHED;
3127 cf->search_pos = i; /* Save the position of the last character
3128 for highlighting the field. */
3133 g_assert(i>=c_match);
3134 i -= (guint32)c_match;
3144 match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
3146 cbs_t *info = criterion;
3147 const guint8 *ascii_text = info->data;
3148 size_t textlen = info->data_len;
3149 match_result result;
3155 /* Load the frame's data. */
3156 if (!cf_read_frame(cf, fdata)) {
3157 /* Attempt to get the packet failed. */
3161 result = MR_NOTMATCHED;
3162 buf_len = fdata->pkt_len;
3164 while (i < buf_len) {
3167 c_char = toupper(c_char);
3168 if (c_char == ascii_text[c_match]) {
3170 if (c_match == textlen) {
3171 result = MR_MATCHED;
3172 cf->search_pos = i; /* Save the position of the last character
3173 for highlighting the field. */
3179 g_assert(i>=(c_match*2));
3180 i -= (guint32)c_match*2;
3189 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
3191 cbs_t *info = criterion;
3192 const guint8 *binary_data = info->data;
3193 size_t datalen = info->data_len;
3194 match_result result;
3199 /* Load the frame's data. */
3200 if (!cf_read_frame(cf, fdata)) {
3201 /* Attempt to get the packet failed. */
3205 result = MR_NOTMATCHED;
3206 buf_len = fdata->pkt_len;
3208 while (i < buf_len) {
3209 if (cf->pd[i] == binary_data[c_match]) {
3211 if (c_match == datalen) {
3212 result = MR_MATCHED;
3213 cf->search_pos = i; /* Save the position of the last character
3214 for highlighting the field. */
3219 g_assert(i>=c_match);
3220 i -= (guint32)c_match;
3229 cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode,
3230 search_direction dir)
3232 return find_packet(cf, match_dfilter, sfcode, dir);
3236 cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
3237 search_direction dir)
3242 if (!dfilter_compile(filter, &sfcode)) {
3244 * XXX - this shouldn't happen, as the filter string is machine
3249 if (sfcode == NULL) {
3251 * XXX - this shouldn't happen, as the filter string is machine
3256 result = find_packet(cf, match_dfilter, sfcode, dir);
3257 dfilter_free(sfcode);
3262 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
3264 dfilter_t *sfcode = criterion;
3266 match_result result;
3268 /* Load the frame's data. */
3269 if (!cf_read_frame(cf, fdata)) {
3270 /* Attempt to get the packet failed. */
3274 epan_dissect_init(&edt, TRUE, FALSE);
3275 epan_dissect_prime_dfilter(&edt, sfcode);
3276 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
3277 result = dfilter_apply_edt(sfcode, &edt) ? MR_MATCHED : MR_NOTMATCHED;
3278 epan_dissect_cleanup(&edt);
3283 cf_find_packet_marked(capture_file *cf, search_direction dir)
3285 return find_packet(cf, match_marked, NULL, dir);
3289 match_marked(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3291 return fdata->flags.marked ? MR_MATCHED : MR_NOTMATCHED;
3295 cf_find_packet_time_reference(capture_file *cf, search_direction dir)
3297 return find_packet(cf, match_time_reference, NULL, dir);
3301 match_time_reference(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3303 return fdata->flags.ref_time ? MR_MATCHED : MR_NOTMATCHED;
3307 find_packet(capture_file *cf,
3308 match_result (*match_function)(capture_file *, frame_data *, void *),
3309 void *criterion, search_direction dir)
3311 frame_data *start_fd;
3314 frame_data *new_fd = NULL;
3315 progdlg_t *progbar = NULL;
3320 GTimeVal start_time;
3321 gchar status_str[100];
3322 int progbar_nextstep;
3323 int progbar_quantum;
3325 match_result result;
3327 start_fd = cf->current_frame;
3328 if (start_fd != NULL) {
3329 /* Iterate through the list of packets, starting at the packet we've
3330 picked, calling a routine to run the filter on the packet, see if
3331 it matches, and stop if so. */
3333 framenum = start_fd->num;
3335 /* Update the progress bar when it gets to this value. */
3336 progbar_nextstep = 0;
3337 /* When we reach the value that triggers a progress bar update,
3338 bump that value by this amount. */
3339 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
3340 /* Progress so far. */
3344 g_get_current_time(&start_time);
3346 title = cf->sfilter?cf->sfilter:"";
3348 /* Create the progress bar if necessary.
3349 We check on every iteration of the loop, so that it takes no
3350 longer than the standard time to create it (otherwise, for a
3351 large file, we might take considerably longer than that standard
3352 time in order to get to the next progress bar step). */
3353 if (progbar == NULL)
3354 progbar = delayed_create_progress_dlg("Searching", title,
3355 FALSE, &stop_flag, &start_time, progbar_val);
3357 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
3358 when we update it, we have to run the GTK+ main loop to get it
3359 to repaint what's pending, and doing so may involve an "ioctl()"
3360 to see if there's any pending input from an X server, and doing
3361 that for every packet can be costly, especially on a big file. */
3362 if (count >= progbar_nextstep) {
3363 /* let's not divide by zero. I should never be started
3364 * with count == 0, so let's assert that
3366 g_assert(cf->count > 0);
3368 progbar_val = (gfloat) count / cf->count;
3370 if (progbar != NULL) {
3371 g_snprintf(status_str, sizeof(status_str),
3372 "%4u of %u packets", count, cf->count);
3373 update_progress_dlg(progbar, progbar_val, status_str);
3376 progbar_nextstep += progbar_quantum;
3380 /* Well, the user decided to abort the search. Go back to the
3381 frame where we started. */
3386 /* Go past the current frame. */
3387 if (dir == SD_BACKWARD) {
3388 /* Go on to the previous frame. */
3389 if (framenum == 1) {
3391 * XXX - other apps have a bit more of a detailed message
3392 * for this, and instead of offering "OK" and "Cancel",
3393 * they offer things such as "Continue" and "Cancel";
3394 * we need an API for popping up alert boxes with
3395 * {Verb} and "Cancel".
3398 if (prefs.gui_find_wrap)
3400 statusbar_push_temporary_msg("Search reached the beginning. Continuing at end.");
3401 framenum = cf->count; /* wrap around */
3405 statusbar_push_temporary_msg("Search reached the beginning.");
3406 framenum = start_fd->num; /* stay on previous packet */
3411 /* Go on to the next frame. */
3412 if (framenum == cf->count) {
3413 if (prefs.gui_find_wrap)
3415 statusbar_push_temporary_msg("Search reached the end. Continuing at beginning.");
3416 framenum = 1; /* wrap around */
3420 statusbar_push_temporary_msg("Search reached the end.");
3421 framenum = start_fd->num; /* stay on previous packet */
3426 fdata = frame_data_sequence_find(cf->frames, framenum);
3430 /* Is this packet in the display? */
3431 if (fdata->flags.passed_dfilter) {
3432 /* Yes. Does it match the search criterion? */
3433 result = (*match_function)(cf, fdata, criterion);
3434 if (result == MR_ERROR) {
3435 /* Error; our caller has reported the error. Go back to the frame
3436 where we started. */
3439 } else if (result == MR_MATCHED) {
3440 /* Yes. Go to the new frame. */
3446 if (fdata == start_fd) {
3447 /* We're back to the frame we were on originally, and that frame
3448 doesn't match the search filter. The search failed. */
3453 /* We're done scanning the packets; destroy the progress bar if it
3455 if (progbar != NULL)
3456 destroy_progress_dlg(progbar);
3459 if (new_fd != NULL) {
3460 /* Find and select */
3461 cf->search_in_progress = TRUE;
3462 found = new_packet_list_select_row_from_data(new_fd);
3463 cf->search_in_progress = FALSE;
3464 cf->search_pos = 0; /* Reset the position */
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; /* success */
3477 return FALSE; /* failure */
3481 cf_goto_frame(capture_file *cf, guint fnumber)
3485 fdata = frame_data_sequence_find(cf->frames, fnumber);
3487 if (fdata == NULL) {
3488 /* we didn't find a packet with that packet number */
3489 statusbar_push_temporary_msg("There is no packet number %u.", fnumber);
3490 return FALSE; /* we failed to go to that packet */
3492 if (!fdata->flags.passed_dfilter) {
3493 /* that packet currently isn't displayed */
3494 /* XXX - add it to the set of displayed packets? */
3495 statusbar_push_temporary_msg("Packet number %u isn't displayed.", fnumber);
3496 return FALSE; /* we failed to go to that packet */
3499 if (!new_packet_list_select_row_from_data(fdata)) {
3500 /* We didn't find a row corresponding to this frame.
3501 This means that the frame isn't being displayed currently,
3502 so we can't select it. */
3503 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
3504 "%sEnd of capture exceeded!%s\n\n"
3505 "The capture file is probably not fully dissected.",
3506 simple_dialog_primary_start(), simple_dialog_primary_end());
3509 return TRUE; /* we got to that packet */
3513 cf_goto_top_frame(void)
3515 /* Find and select */
3516 new_packet_list_select_first_row();
3517 return TRUE; /* we got to that packet */
3521 cf_goto_bottom_frame(void)
3523 /* Find and select */
3524 new_packet_list_select_last_row();
3525 return TRUE; /* we got to that packet */
3529 * Go to frame specified by currently selected protocol tree item.
3532 cf_goto_framenum(capture_file *cf)
3534 header_field_info *hfinfo;
3537 if (cf->finfo_selected) {
3538 hfinfo = cf->finfo_selected->hfinfo;
3540 if (hfinfo->type == FT_FRAMENUM) {
3541 framenum = fvalue_get_uinteger(&cf->finfo_selected->value);
3543 return cf_goto_frame(cf, framenum);
3550 /* Select the packet on a given row. */
3552 cf_select_packet(capture_file *cf, int row)
3554 epan_dissect_t *old_edt;
3557 /* Get the frame data struct pointer for this frame */
3558 fdata = new_packet_list_get_row_data(row);
3560 if (fdata == NULL) {
3561 /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
3562 the first entry is added to it by "real_insert_row()", that row
3563 is selected (see "real_insert_row()", in "ui/gtk/gtkclist.c", in both
3564 our version and the vanilla GTK+ version).
3566 This means that a "select-row" signal is emitted; this causes
3567 "packet_list_select_cb()" to be called, which causes "cf_select_packet()"
3570 "cf_select_packet()" fetches, above, the data associated with the
3571 row that was selected; however, as "gtk_clist_append()", which
3572 called "real_insert_row()", hasn't yet returned, we haven't yet
3573 associated any data with that row, so we get back a null pointer.
3575 We can't assume that there's only one frame in the frame list,
3576 either, as we may be filtering the display.
3578 We therefore assume that, if "row" is 0, i.e. the first row
3579 is being selected, and "cf->first_displayed" equals
3580 "cf->last_displayed", i.e. there's only one frame being
3581 displayed, that frame is the frame we want.
3583 This means we have to set "cf->first_displayed" and
3584 "cf->last_displayed" before adding the row to the
3585 GtkCList; see the comment in "add_packet_to_packet_list()". */
3587 if (row == 0 && cf->first_displayed == cf->last_displayed)
3588 fdata = frame_data_sequence_find(cf->frames, cf->first_displayed);
3591 /* If fdata _still_ isn't set simply give up. */
3592 if (fdata == NULL) {
3596 /* Get the data in that frame. */
3597 if (!cf_read_frame (cf, fdata)) {
3601 /* Record that this frame is the current frame. */
3602 cf->current_frame = fdata;
3603 cf->current_row = row;
3606 /* Create the logical protocol tree. */
3607 /* We don't need the columns here. */
3608 cf->edt = epan_dissect_new(TRUE, TRUE);
3610 tap_build_interesting(cf->edt);
3611 epan_dissect_run(cf->edt, &cf->pseudo_header, cf->pd, cf->current_frame,
3614 dfilter_macro_build_ftv_cache(cf->edt->tree);
3616 cf_callback_invoke(cf_cb_packet_selected, cf);
3618 if (old_edt != NULL)
3619 epan_dissect_free(old_edt);
3623 /* Unselect the selected packet, if any. */
3625 cf_unselect_packet(capture_file *cf)
3627 epan_dissect_t *old_edt = cf->edt;
3631 /* No packet is selected. */
3632 cf->current_frame = NULL;
3633 cf->current_row = 0;
3635 cf_callback_invoke(cf_cb_packet_unselected, cf);
3637 /* No protocol tree means no selected field. */
3638 cf_unselect_field(cf);
3640 /* Destroy the epan_dissect_t for the unselected packet. */
3641 if (old_edt != NULL)
3642 epan_dissect_free(old_edt);
3645 /* Unset the selected protocol tree field, if any. */
3647 cf_unselect_field(capture_file *cf)
3649 cf->finfo_selected = NULL;
3651 cf_callback_invoke(cf_cb_field_unselected, cf);
3655 * Mark a particular frame.
3658 cf_mark_frame(capture_file *cf, frame_data *frame)
3660 if (! frame->flags.marked) {
3661 frame->flags.marked = TRUE;
3662 if (cf->count > cf->marked_count)
3668 * Unmark a particular frame.
3671 cf_unmark_frame(capture_file *cf, frame_data *frame)
3673 if (frame->flags.marked) {
3674 frame->flags.marked = FALSE;
3675 if (cf->marked_count > 0)
3681 * Ignore a particular frame.
3684 cf_ignore_frame(capture_file *cf, frame_data *frame)
3686 if (! frame->flags.ignored) {
3687 frame->flags.ignored = TRUE;
3688 if (cf->count > cf->ignored_count)
3689 cf->ignored_count++;
3694 * Un-ignore a particular frame.
3697 cf_unignore_frame(capture_file *cf, frame_data *frame)
3699 if (frame->flags.ignored) {
3700 frame->flags.ignored = FALSE;
3701 if (cf->ignored_count > 0)
3702 cf->ignored_count--;
3707 * Read the comment in SHB block
3711 cf_read_shb_comment(capture_file *cf)
3713 wtapng_section_t *shb_inf;
3714 const gchar *temp_str;
3716 /* Get info from SHB */
3717 shb_inf = wtap_file_get_shb_info(cf->wth);
3720 temp_str = shb_inf->opt_comment;
3728 cf_update_capture_comment(capture_file *cf, gchar *comment)
3730 wtapng_section_t *shb_inf;
3732 /* Get info from SHB */
3733 shb_inf = wtap_file_get_shb_info(cf->wth);
3735 /* See if the comment has changed or not */
3736 if (shb_inf && shb_inf->opt_comment) {
3737 if (strcmp(shb_inf->opt_comment, comment) == 0) {
3746 /* The comment has changed, let's update it */
3747 wtap_write_shb_comment(cf->wth, comment);
3748 /* Mark the file as having unsaved changes */
3749 cf->unsaved_changes = TRUE;
3756 } save_callback_args_t;
3759 * Save a capture to a file, in a particular format, saving either
3760 * all packets, all currently-displayed packets, or all marked packets.
3762 * Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
3763 * up a message box for the failure.
3766 save_packet(capture_file *cf _U_, frame_data *fdata,
3767 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
3770 save_callback_args_t *args = argsp;
3771 struct wtap_pkthdr hdr;
3774 /* init the wtap header for saving */
3775 /* XXX - these are the only flags that correspond to data that we have
3776 in the frame_data structure and that matter on a per-packet basis.
3778 For WTAP_HAS_CAP_LEN, either the file format has separate "captured"
3779 and "on the wire" lengths, or it doesn't.
3781 For WTAP_HAS_DROP_COUNT, Wiretap doesn't actually supply the value
3784 For WTAP_HAS_PACK_FLAGS, we currently don't save the FCS length
3785 from the packet flags. */
3786 hdr.presence_flags = 0;
3787 if (fdata->flags.has_ts)
3788 hdr.presence_flags |= WTAP_HAS_TS;
3789 if (fdata->flags.has_ts)
3790 hdr.presence_flags |= WTAP_HAS_INTERFACE_ID;
3791 hdr.ts.secs = fdata->abs_ts.secs;
3792 hdr.ts.nsecs = fdata->abs_ts.nsecs;
3793 hdr.caplen = fdata->cap_len;
3794 hdr.len = fdata->pkt_len;
3795 hdr.pkt_encap = fdata->lnk_t;
3797 hdr.interface_id = fdata->interface_id; /* identifier of the interface. */
3799 hdr.opt_comment = fdata->opt_comment; /* NULL if not available */
3802 hdr.pack_flags = /* XXX - 0 for now (any value for "we don't have it"?) */
3804 /* and save the packet */
3805 if (!wtap_dump(args->pdh, &hdr, pseudo_header, pd, &err)) {
3807 /* Wiretap error. */
3810 case WTAP_ERR_UNSUPPORTED_ENCAP:
3812 * This is a problem with the particular frame we're writing;
3813 * note that, and give the frame number.
3815 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3816 "Frame %u has a network type that can't be saved in a \"%s\" file.",
3817 fdata->num, wtap_file_type_string(args->file_type));
3821 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3822 "An error occurred while writing to the file \"%s\": %s.",
3823 args->fname, wtap_strerror(err));
3828 write_failure_alert_box(args->fname, err);
3836 * Can this capture file be saved in any format except by copying the raw data?
3839 cf_can_save_as(capture_file *cf)
3843 for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) {
3844 /* To save a file with Wiretap, Wiretap has to handle that format,
3845 and its code to handle that format must be able to write a file
3846 with this file's encapsulation type. */
3847 if (wtap_dump_can_open(ft) && wtap_dump_can_write_encap(ft, cf->lnk_t)) {
3848 /* OK, we can write it out in this type. */
3853 /* No, we couldn't save it in any format. */
3858 * Quick scan to find packet offsets.
3860 static cf_read_status_t
3861 rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
3863 const struct wtap_pkthdr *phdr;
3867 char errmsg_errno[1024+1];
3870 progdlg_t *volatile progbar = NULL;
3872 volatile gint64 size;
3873 volatile float progbar_val;
3874 GTimeVal start_time;
3875 gchar status_str[100];
3876 volatile gint64 progbar_nextstep;
3877 volatile gint64 progbar_quantum;
3880 volatile int count = 0;
3882 volatile int displayed_once = 0;
3885 /* Close the old handle. */
3886 wtap_close(cf->wth);
3888 /* Open the new file. */
3889 cf->wth = wtap_open_offline(fname, err, &err_info, TRUE);
3890 if (cf->wth == NULL) {
3891 cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
3892 return CF_READ_ERROR;
3895 /* We're scanning a file whose contents should be the same as what
3896 we had before, so we don't discard dissection state etc.. */
3899 /* Set the file name because we need it to set the follow stream filter.
3900 XXX - is that still true? We need it for other reasons, though,
3902 cf->filename = g_strdup(fname);
3904 /* Indicate whether it's a permanent or temporary file. */
3905 cf->is_tempfile = is_tempfile;
3907 /* No user changes yet. */
3908 cf->unsaved_changes = FALSE;
3910 cf->cd_t = wtap_file_type(cf->wth);
3911 cf->linktypes = g_array_sized_new(FALSE, FALSE, (guint) sizeof(int), 1);
3913 cf->snap = wtap_snapshot_length(cf->wth);
3914 if (cf->snap == 0) {
3915 /* Snapshot length not known. */
3916 cf->has_snap = FALSE;
3917 cf->snap = WTAP_MAX_PACKET_SIZE;
3919 cf->has_snap = TRUE;
3921 name_ptr = g_filename_display_basename(cf->filename);
3923 cf_callback_invoke(cf_cb_file_rescan_started, cf);
3925 /* Record whether the file is compressed.
3926 XXX - do we know this at open time? */
3927 cf->iscompressed = wtap_iscompressed(cf->wth);
3929 /* Find the size of the file. */
3930 size = wtap_file_size(cf->wth, NULL);
3932 /* Update the progress bar when it gets to this value. */
3933 progbar_nextstep = 0;
3934 /* When we reach the value that triggers a progress bar update,
3935 bump that value by this amount. */
3937 progbar_quantum = size/N_PROGBAR_UPDATES;
3938 if (progbar_quantum < MIN_QUANTUM)
3939 progbar_quantum = MIN_QUANTUM;
3941 progbar_quantum = 0;
3942 /* Progress so far. */
3946 g_get_current_time(&start_time);
3949 phdr = wtap_phdr(cf->wth);
3950 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
3952 fdata = frame_data_sequence_find(cf->frames, framenum);
3953 fdata->file_off = data_offset;
3956 file_pos = wtap_read_so_far(cf->wth);
3958 /* Create the progress bar if necessary.
3959 * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
3961 if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)){
3962 progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
3963 progbar = delayed_create_progress_dlg("Rescanning", name_ptr,
3964 TRUE, &stop_flag, &start_time, progbar_val);
3967 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
3968 when we update it, we have to run the GTK+ main loop to get it
3969 to repaint what's pending, and doing so may involve an "ioctl()"
3970 to see if there's any pending input from an X server, and doing
3971 that for every packet can be costly, especially on a big file. */
3972 if (file_pos >= progbar_nextstep) {
3973 if (progbar != NULL) {
3974 progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
3975 /* update the packet bar content on the first run or frequently on very large files */
3977 if (progbar_quantum > 500000 || displayed_once == 0) {
3978 if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->count != 0) {
3980 packets_bar_update();
3983 #endif /* HAVE_LIBPCAP */
3984 update_progress_dlg(progbar, progbar_val, status_str);
3986 progbar_nextstep += progbar_quantum;
3991 /* Well, the user decided to abort the rescan. Sadly, as this
3992 isn't a reread, recovering is difficult, so we'll just
3993 close the current capture. */
3997 /* Add this packet's link-layer encapsulation type to cf->linktypes, if
3998 it's not already there.
3999 XXX - yes, this is O(N), so if every packet had a different
4000 link-layer encapsulation type, it'd be O(N^2) to read the file, but
4001 there are probably going to be a small number of encapsulation types
4003 cf_add_encapsulation_type(cf, phdr->pkt_encap);
4006 /* Free the display name */
4009 /* We're done reading the file; destroy the progress bar if it was created. */
4010 if (progbar != NULL)
4011 destroy_progress_dlg(progbar);
4013 /* We're done reading sequentially through the file. */
4014 cf->state = FILE_READ_DONE;
4016 /* Close the sequential I/O side, to free up memory it requires. */
4017 wtap_sequential_close(cf->wth);
4019 /* compute the time it took to load the file */
4020 compute_elapsed(&start_time);
4022 /* Set the file encapsulation type now; we don't know what it is until
4023 we've looked at all the packets, as we don't know until then whether
4024 there's more than one type (and thus whether it's
4025 WTAP_ENCAP_PER_PACKET). */
4026 cf->lnk_t = wtap_file_encap(cf->wth);
4028 cf_callback_invoke(cf_cb_file_rescan_finished, cf);
4031 /* Our caller will give up at this point. */
4032 return CF_READ_ABORTED;
4036 /* Put up a message box noting that the read failed somewhere along
4037 the line. Don't throw out the stuff we managed to read, though,
4041 case WTAP_ERR_UNSUPPORTED:
4042 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4043 "The capture file contains record data that TShark doesn't support.\n(%s)",
4046 errmsg = errmsg_errno;
4049 case WTAP_ERR_UNSUPPORTED_ENCAP:
4050 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4051 "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
4054 errmsg = errmsg_errno;
4057 case WTAP_ERR_CANT_READ:
4058 errmsg = "An attempt to read from the capture file failed for"
4059 " some unknown reason.";
4062 case WTAP_ERR_SHORT_READ:
4063 errmsg = "The capture file appears to have been cut short"
4064 " in the middle of a packet.";
4067 case WTAP_ERR_BAD_FILE:
4068 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4069 "The capture file appears to be damaged or corrupt.\n(%s)",
4072 errmsg = errmsg_errno;
4075 case WTAP_ERR_DECOMPRESS:
4076 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4077 "The compressed capture file appears to be damaged or corrupt.\n"
4080 errmsg = errmsg_errno;
4084 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4085 "An error occurred while reading the"
4086 " capture file: %s.", wtap_strerror(*err));
4087 errmsg = errmsg_errno;
4090 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", errmsg);
4091 return CF_READ_ERROR;
4097 cf_save_packets(capture_file *cf, const char *fname, guint save_format,
4098 gboolean compressed, gboolean dont_reopen)
4100 gchar *fname_new = NULL;
4109 save_callback_args_t callback_args;
4111 cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
4113 if (save_format == cf->cd_t && compressed == cf->iscompressed
4114 && !cf->unsaved_changes) {
4115 /* We're saving in the format it's already in, and there are no
4116 changes we have in memory that aren't saved to the file, so
4117 we can just move or copy the raw data. */
4119 if (cf->is_tempfile) {
4120 /* The file being saved is a temporary file from a live
4121 capture, so it doesn't need to stay around under that name;
4122 first, try renaming the capture buffer file to the new name.
4123 This acts as a "safe save", in that, if the file already
4124 exists, the existing file will be removed only if the rename
4127 Sadly, on Windows, as we have the current capture file
4128 open, even MoveFileEx() with MOVEFILE_REPLACE_EXISTING
4129 (to cause the rename to remove an existing target), as
4130 done by ws_stdio_rename() (ws_rename() is #defined to
4131 be ws_stdio_rename() on Windows) will fail.
4133 According to the MSDN documentation for CreateFile(), if,
4134 when we open a capture file, we were to directly do a CreateFile(),
4135 opening with FILE_SHARE_DELETE|FILE_SHARE_READ, and then
4136 convert it to a file descriptor with _open_osfhandle(),
4137 that would allow the file to be renamed out from under us.
4139 However, that doesn't work in practice. Perhaps the problem
4140 is that the process doing the rename is the process that
4141 has the file open. */
4143 if (ws_rename(cf->filename, fname) == 0) {
4144 /* That succeeded - there's no need to copy the source file. */
4145 how_to_save = SAVE_WITH_MOVE;
4147 if (errno == EXDEV) {
4148 /* They're on different file systems, so we have to copy the
4150 how_to_save = SAVE_WITH_COPY;
4152 /* The rename failed, but not because they're on different
4153 file systems - put up an error message. (Or should we
4154 just punt and try to copy? The only reason why I'd
4155 expect the rename to fail and the copy to succeed would
4156 be if we didn't have permission to remove the file from
4157 the temporary directory, and that might be fixable - but
4158 is it worth requiring the user to go off and fix it?) */
4159 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4160 file_rename_error_message(errno), fname);
4165 how_to_save = SAVE_WITH_COPY;
4168 /* It's a permanent file, so we should copy it, and not remove the
4170 how_to_save = SAVE_WITH_COPY;
4173 if (how_to_save == SAVE_WITH_COPY) {
4174 /* Copy the file, if we haven't moved it. If we're overwriting
4175 an existing file, we do it with a "safe save", by writing
4176 to a new file and, if the write succeeds, renaming the
4177 new file on top of the old file. */
4178 if (file_exists(fname)) {
4179 fname_new = g_strdup_printf("%s~", fname);
4180 if (!copy_file_binary_mode(cf->filename, fname_new))
4183 if (!copy_file_binary_mode(cf->filename, fname))
4188 /* Either we're saving in a different format or we're saving changes,
4189 such as added, modified, or removed comments, that haven't yet
4190 been written to the underlying file; we can't do that by copying
4191 or moving the capture file, we have to do it by writing the packets
4194 wtapng_section_t *shb_hdr = NULL;
4195 wtapng_iface_descriptions_t *idb_inf = NULL;
4197 shb_hdr = wtap_file_get_shb_info(cf->wth);
4198 idb_inf = wtap_file_get_idb_info(cf->wth);
4200 if (file_exists(fname)) {
4201 /* We're overwriting an existing file; write out to a new file,
4202 and, if that succeeds, rename the new file on top of the
4203 old file. That makes this a "safe save", so that we don't
4204 lose the old file if we have a problem writing out the new
4205 file. (If the existing file is the current capture file,
4206 we *HAVE* to do that, otherwise we're overwriting the file
4207 from which we're reading the packets that we're writing!) */
4208 fname_new = g_strdup_printf("%s~", fname);
4209 pdh = wtap_dump_open_ng(fname_new, save_format, cf->lnk_t, cf->snap,
4210 compressed, shb_hdr, idb_inf, &err);
4212 pdh = wtap_dump_open_ng(fname, save_format, cf->lnk_t, cf->snap,
4213 compressed, shb_hdr, idb_inf, &err);
4219 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
4223 /* Add address resolution */
4224 wtap_dump_set_addrinfo_list(pdh, get_addrinfo_list());
4226 /* Iterate through the list of packets, processing all the packets. */
4227 callback_args.pdh = pdh;
4228 callback_args.fname = fname;
4229 callback_args.file_type = save_format;
4230 switch (process_specified_packets(cf, NULL, "Saving", "packets",
4231 TRUE, save_packet, &callback_args)) {
4234 /* Completed successfully. */
4238 /* The user decided to abort the saving.
4239 If we're writing to a temporary file, remove it.
4240 XXX - should we do so even if we're not writing to a
4242 wtap_dump_close(pdh, &err);
4243 if (fname_new != NULL)
4244 ws_unlink(fname_new);
4245 cf_callback_invoke(cf_cb_file_save_stopped, NULL);
4246 return CF_WRITE_ABORTED;
4249 /* Error while saving.
4250 If we're writing to a temporary file, remove it. */
4251 if (fname_new != NULL)
4252 ws_unlink(fname_new);
4253 wtap_dump_close(pdh, &err);
4257 if (!wtap_dump_close(pdh, &err)) {
4258 cf_close_failure_alert_box(fname, err);
4262 how_to_save = SAVE_WITH_WTAP;
4265 if (fname_new != NULL) {
4266 /* We wrote out to fname_new, and should rename it on top of
4267 fname. fname_new is now closed, so that should be possible even
4268 on Windows. However, on Windows, we first need to close whatever
4269 file descriptors we have open for fname. */
4271 wtap_fdclose(cf->wth);
4273 /* Now do the rename. */
4274 if (ws_rename(fname_new, fname) == -1) {
4275 /* Well, the rename failed. */
4276 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4277 file_rename_error_message(errno), fname);
4279 /* Attempt to reopen the random file descriptor using the
4280 current file's filename. (At this point, the sequential
4281 file descriptor is closed.) */
4282 if (!wtap_fdreopen(cf->wth, cf->filename, &err)) {
4283 /* Oh, well, we're screwed. */
4284 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4285 file_open_error_message(err, FALSE), cf->filename);
4292 cf_callback_invoke(cf_cb_file_save_finished, NULL);
4293 cf->unsaved_changes = FALSE;
4296 switch (how_to_save) {
4298 case SAVE_WITH_MOVE:
4299 /* We just moved the file, so the wtap structure refers to the
4300 new file, and all the information other than the filename
4301 and the "is temporary" status applies to the new file; just
4303 g_free(cf->filename);
4304 cf->filename = g_strdup(fname);
4305 cf->is_tempfile = FALSE;
4306 cf_callback_invoke(cf_cb_file_fast_save_finished, cf);
4309 case SAVE_WITH_COPY:
4310 /* We just copied the file, s all the information other than
4311 the wtap structure, the filename, and the "is temporary"
4312 status applies to the new file; just update that. */
4313 wtap_close(cf->wth);
4314 cf->wth = wtap_open_offline(fname, &err, &err_info, TRUE);
4315 if (cf->wth == NULL) {
4316 cf_open_failure_alert_box(fname, err, err_info, FALSE, 0);
4319 g_free(cf->filename);
4320 cf->filename = g_strdup(fname);
4321 cf->is_tempfile = FALSE;
4323 cf_callback_invoke(cf_cb_file_fast_save_finished, cf);
4326 case SAVE_WITH_WTAP:
4327 /* Open and read the file we saved to.
4329 XXX - this is somewhat of a waste; we already have the
4330 packets, all this gets us is updated file type information
4331 (which we could just stuff into "cf"), and having the new
4332 file be the one we have opened and from which we're reading
4333 the data, and it means we have to spend time opening and
4334 reading the file, which could be a significant amount of
4335 time if the file is large.
4337 If the capture-file-writing code were to return the
4338 seek offset of each packet it writes, we could save that
4339 in the frame_data structure for the frame, and just open
4340 the file without reading it again...
4342 ...as long as, for gzipped files, the process of writing
4343 out the file *also* generates the information needed to
4344 support fast random access to the compressed file. */
4345 if (rescan_file(cf, fname, FALSE, &err) != CF_READ_OK) {
4346 /* The rescan failed; just close the file. Either
4347 a dialog was popped up for the failure, so the
4348 user knows what happened, or they stopped the
4349 rescan, in which case they know what happened. */
4358 if (fname_new != NULL) {
4359 /* We were trying to write to a temporary file; get rid of it if it
4360 exists. (We don't care whether this fails, as, if it fails,
4361 there's not much we can do about it. I guess if it failed for
4362 a reason other than "it doesn't exist", we could report an
4363 error, so the user knows there's a junk file that they might
4364 want to clean up.) */
4365 ws_unlink(fname_new);
4368 cf_callback_invoke(cf_cb_file_save_failed, NULL);
4369 return CF_WRITE_ERROR;
4373 cf_export_specified_packets(capture_file *cf, const char *fname,
4374 packet_range_t *range, guint save_format,
4375 gboolean compressed)
4377 gchar *fname_new = NULL;
4380 save_callback_args_t callback_args;
4381 wtapng_section_t *shb_hdr = NULL;
4382 wtapng_iface_descriptions_t *idb_inf = NULL;
4384 cf_callback_invoke(cf_cb_file_export_specified_packets_started, (gpointer)fname);
4386 packet_range_process_init(range);
4388 /* We're writing out specified packets from the specified capture
4389 file to another file. Even if all captured packets are to be
4390 written, don't special-case the operation - read each packet
4391 and then write it out if it's one of the specified ones. */
4393 shb_hdr = wtap_file_get_shb_info(cf->wth);
4394 idb_inf = wtap_file_get_idb_info(cf->wth);
4396 if (file_exists(fname)) {
4397 /* We're overwriting an existing file; write out to a new file,
4398 and, if that succeeds, rename the new file on top of the
4399 old file. That makes this a "safe save", so that we don't
4400 lose the old file if we have a problem writing out the new
4401 file. (If the existing file is the current capture file,
4402 we *HAVE* to do that, otherwise we're overwriting the file
4403 from which we're reading the packets that we're writing!) */
4404 fname_new = g_strdup_printf("%s~", fname);
4405 pdh = wtap_dump_open_ng(fname_new, save_format, cf->lnk_t, cf->snap,
4406 compressed, shb_hdr, idb_inf, &err);
4408 pdh = wtap_dump_open_ng(fname, save_format, cf->lnk_t, cf->snap,
4409 compressed, shb_hdr, idb_inf, &err);
4415 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
4419 /* Add address resolution */
4420 wtap_dump_set_addrinfo_list(pdh, get_addrinfo_list());
4422 /* Iterate through the list of packets, processing the packets we were
4425 XXX - we've already called "packet_range_process_init(range)", but
4426 "process_specified_packets()" will do it again. Fortunately,
4427 that's harmless in this case, as we haven't done anything to
4428 "range" since we initialized it. */
4429 callback_args.pdh = pdh;
4430 callback_args.fname = fname;
4431 callback_args.file_type = save_format;
4432 switch (process_specified_packets(cf, range, "Writing", "specified packets",
4433 TRUE, save_packet, &callback_args)) {
4436 /* Completed successfully. */
4440 /* The user decided to abort the saving.
4441 If we're writing to a temporary file, remove it.
4442 XXX - should we do so even if we're not writing to a
4444 wtap_dump_close(pdh, &err);
4445 if (fname_new != NULL)
4446 ws_unlink(fname_new);
4447 cf_callback_invoke(cf_cb_file_export_specified_packets_stopped, NULL);
4448 return CF_WRITE_ABORTED;
4452 /* Error while saving.
4453 If we're writing to a temporary file, remove it. */
4454 if (fname_new != NULL)
4455 ws_unlink(fname_new);
4456 wtap_dump_close(pdh, &err);
4460 if (!wtap_dump_close(pdh, &err)) {
4461 cf_close_failure_alert_box(fname, err);
4465 if (fname_new != NULL) {
4466 /* We wrote out to fname_new, and should rename it on top of
4467 fname; fname is now closed, so that should be possible even
4468 on Windows. Do the rename. */
4469 if (ws_rename(fname_new, fname) == -1) {
4470 /* Well, the rename failed. */
4471 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4472 file_rename_error_message(errno), fname);
4477 cf_callback_invoke(cf_cb_file_export_specified_packets_finished, NULL);
4481 if (fname_new != NULL) {
4482 /* We were trying to write to a temporary file; get rid of it if it
4483 exists. (We don't care whether this fails, as, if it fails,
4484 there's not much we can do about it. I guess if it failed for
4485 a reason other than "it doesn't exist", we could report an
4486 error, so the user knows there's a junk file that they might
4487 want to clean up.) */
4488 ws_unlink(fname_new);
4491 cf_callback_invoke(cf_cb_file_export_specified_packets_failed, NULL);
4492 return CF_WRITE_ERROR;
4496 cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
4497 gboolean for_writing, int file_type)
4500 /* Wiretap error. */
4503 case WTAP_ERR_NOT_REGULAR_FILE:
4504 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4505 "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
4509 case WTAP_ERR_RANDOM_OPEN_PIPE:
4510 /* Seen only when opening a capture file for reading. */
4511 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4512 "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.\n"
4513 "To capture from a pipe or FIFO use wireshark -i -",
4517 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
4518 /* Seen only when opening a capture file for reading. */
4519 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4520 "The file \"%s\" isn't a capture file in a format Wireshark understands.",
4524 case WTAP_ERR_UNSUPPORTED:
4525 /* Seen only when opening a capture file for reading. */
4526 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4527 "The file \"%s\" isn't a capture file in a format Wireshark understands.\n"
4529 filename, err_info);
4533 case WTAP_ERR_CANT_WRITE_TO_PIPE:
4534 /* Seen only when opening a capture file for writing. */
4535 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4536 "The file \"%s\" is a pipe, and %s capture files can't be "
4537 "written to a pipe.",
4538 filename, wtap_file_type_string(file_type));
4541 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
4542 /* Seen only when opening a capture file for writing. */
4543 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4544 "Wireshark doesn't support writing capture files in that format.");
4547 case WTAP_ERR_UNSUPPORTED_ENCAP:
4549 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4550 "Wireshark can't save this capture in that format.");
4552 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4553 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.\n"
4555 filename, err_info);
4560 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
4562 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4563 "Wireshark can't save this capture in that format.");
4565 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4566 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.",
4571 case WTAP_ERR_BAD_FILE:
4572 /* Seen only when opening a capture file for reading. */
4573 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4574 "The file \"%s\" appears to be damaged or corrupt.\n"
4576 filename, err_info);
4580 case WTAP_ERR_CANT_OPEN:
4582 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4583 "The file \"%s\" could not be created for some unknown reason.",
4586 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4587 "The file \"%s\" could not be opened for some unknown reason.",
4592 case WTAP_ERR_SHORT_READ:
4593 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4594 "The file \"%s\" appears to have been cut short"
4595 " in the middle of a packet or other data.",
4599 case WTAP_ERR_SHORT_WRITE:
4600 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4601 "A full header couldn't be written to the file \"%s\".",
4605 case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
4606 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4607 "This file type cannot be written as a compressed file.");
4610 case WTAP_ERR_DECOMPRESS:
4611 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4612 "The compressed file \"%s\" appears to be damaged or corrupt.\n"
4613 "(%s)", filename, err_info);
4618 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4619 "The file \"%s\" could not be %s: %s.",
4621 for_writing ? "created" : "opened",
4622 wtap_strerror(err));
4627 open_failure_alert_box(filename, err, for_writing);
4632 * XXX - whether we mention the source pathname, the target pathname,
4633 * or both depends on the error and on what we find if we look for
4634 * one or both of them.
4637 file_rename_error_message(int err)
4640 static char errmsg_errno[1024+1];
4645 /* XXX - should check whether the source exists and, if not,
4646 report it as the problem and, if so, report the destination
4648 errmsg = "The path to the file \"%s\" doesn't exist.";
4652 /* XXX - if we're doing a rename after a safe save, we should
4653 probably say something else. */
4654 errmsg = "You don't have permission to move the capture file to \"%s\".";
4658 /* XXX - this should probably mention both the source and destination
4660 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
4661 "The file \"%%s\" could not be moved: %s.",
4662 wtap_strerror(err));
4663 errmsg = errmsg_errno;
4669 /* Check for write errors - if the file is being written to an NFS server,
4670 a write error may not show up until the file is closed, as NFS clients
4671 might not send writes to the server until the "write()" call finishes,
4672 so that the write may fail on the server but the "write()" may succeed. */
4674 cf_close_failure_alert_box(const char *filename, int err)
4677 /* Wiretap error. */
4680 case WTAP_ERR_CANT_CLOSE:
4681 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4682 "The file \"%s\" couldn't be closed for some unknown reason.",
4686 case WTAP_ERR_SHORT_WRITE:
4687 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4688 "Not all the packets could be written to the file \"%s\".",
4693 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
4694 "An error occurred while closing the file \"%s\": %s.",
4695 filename, wtap_strerror(err));
4700 We assume that a close error from the OS is really a write error. */
4701 write_failure_alert_box(filename, err);
4705 /* Reload the current capture file. */
4707 cf_reload(capture_file *cf) {
4709 gboolean is_tempfile;
4712 /* If the file could be opened, "cf_open()" calls "cf_close()"
4713 to get rid of state for the old capture file before filling in state
4714 for the new capture file. "cf_close()" will remove the file if
4715 it's a temporary file; we don't want that to happen (for one thing,
4716 it'd prevent subsequent reopens from working). Remember whether it's
4717 a temporary file, mark it as not being a temporary file, and then
4718 reopen it as the type of file it was.
4720 Also, "cf_close()" will free "cf->filename", so we must make
4721 a copy of it first. */
4722 filename = g_strdup(cf->filename);
4723 is_tempfile = cf->is_tempfile;
4724 cf->is_tempfile = FALSE;
4725 if (cf_open(cf, filename, is_tempfile, &err) == CF_OK) {
4726 switch (cf_read(cf, TRUE)) {
4730 /* Just because we got an error, that doesn't mean we were unable
4731 to read any of the file; we handle what we could get from the
4735 case CF_READ_ABORTED:
4736 /* The user bailed out of re-reading the capture file; the
4737 capture file has been closed - just free the capture file name
4738 string and return (without changing the last containing
4744 /* The open failed, so "cf->is_tempfile" wasn't set to "is_tempfile".
4745 Instead, the file was left open, so we should restore "cf->is_tempfile"
4748 XXX - change the menu? Presumably "cf_open()" will do that;
4749 make sure it does! */
4750 cf->is_tempfile = is_tempfile;
4752 /* "cf_open()" made a copy of the file name we handed it, so
4753 we should free up our copy. */
4763 * indent-tabs-mode: nil
4766 * ex: set shiftwidth=2 tabstop=8 expandtab:
4767 * :indentSize=2:tabSize=8:noTabs=true: