6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
46 #include <epan/epan.h>
47 #include <epan/filesystem.h>
50 #include "color_filters.h"
52 #include <epan/column.h>
53 #include <epan/packet.h>
54 #include <epan/column-utils.h>
55 #include "packet-range.h"
61 #include "alert_box.h"
62 #include "simple_dialog.h"
63 #include "main_statusbar.h"
64 #include "progress_dlg.h"
66 #include <epan/prefs.h>
67 #include <epan/dfilter/dfilter.h>
68 #include <epan/epan_dissect.h>
70 #include <epan/dissectors/packet-data.h>
71 #include <epan/dissectors/packet-ber.h>
72 #include <epan/timestamp.h>
73 #include <epan/dfilter/dfilter-macro.h>
74 #include <wsutil/file_util.h>
75 #include <epan/strutil.h>
78 gboolean auto_scroll_live;
81 static guint32 cum_bytes;
82 static nstime_t first_ts;
83 static nstime_t prev_dis_ts;
84 static nstime_t prev_cap_ts;
86 static gulong computed_elapsed;
88 static void cf_reset_state(capture_file *cf);
90 static int read_packet(capture_file *cf, dfilter_t *dfcode,
91 gboolean filtering_tap_listeners, guint tap_flags, gint64 offset);
93 static void rescan_packets(capture_file *cf, const char *action, const char *action_item,
94 gboolean refilter, gboolean redissect);
101 static match_result match_protocol_tree(capture_file *cf, frame_data *fdata,
103 static void match_subtree_text(proto_node *node, gpointer data);
104 static match_result match_summary_line(capture_file *cf, frame_data *fdata,
106 static match_result match_ascii_and_unicode(capture_file *cf, frame_data *fdata,
108 static match_result match_ascii(capture_file *cf, frame_data *fdata,
110 static match_result match_unicode(capture_file *cf, frame_data *fdata,
112 static match_result match_binary(capture_file *cf, frame_data *fdata,
114 static match_result match_dfilter(capture_file *cf, frame_data *fdata,
116 static match_result match_marked(capture_file *cf, frame_data *fdata,
118 static match_result match_time_reference(capture_file *cf, frame_data *fdata,
120 static gboolean find_packet(capture_file *cf,
121 match_result (*match_function)(capture_file *, frame_data *, void *),
122 void *criterion, search_direction dir);
124 static void cf_open_failure_alert_box(const char *filename, int err,
125 gchar *err_info, gboolean for_writing,
127 static const char *file_rename_error_message(int err);
128 static void cf_write_failure_alert_box(const char *filename, int err);
129 static void cf_close_failure_alert_box(const char *filename, int err);
130 static void ref_time_packets(capture_file *cf);
131 /* Update the progress bar this many times when reading a file. */
132 #define N_PROGBAR_UPDATES 100
133 /* We read around 200k/100ms don't update the progress bar more often than that */
134 #define MIN_QUANTUM 200000
135 #define MIN_NUMBER_OF_PACKET 1500
137 /* Number of "frame_data" structures per memory chunk.
138 XXX - is this the right number? */
139 #define FRAME_DATA_CHUNK_SIZE 1024
143 * We could probably use g_signal_...() instead of the callbacks below but that
144 * would require linking our CLI programs to libgobject and creating an object
145 * instance for the signals.
148 cf_callback_t cb_fct;
150 } cf_callback_data_t;
152 static GList *cf_callbacks = NULL;
155 cf_callback_invoke(int event, gpointer data)
157 cf_callback_data_t *cb;
158 GList *cb_item = cf_callbacks;
160 /* there should be at least one interested */
161 g_assert(cb_item != NULL);
163 while(cb_item != NULL) {
165 cb->cb_fct(event, data, cb->user_data);
166 cb_item = g_list_next(cb_item);
172 cf_callback_add(cf_callback_t func, gpointer user_data)
174 cf_callback_data_t *cb;
176 cb = g_malloc(sizeof(cf_callback_data_t));
178 cb->user_data = user_data;
180 cf_callbacks = g_list_append(cf_callbacks, cb);
184 cf_callback_remove(cf_callback_t func)
186 cf_callback_data_t *cb;
187 GList *cb_item = cf_callbacks;
189 while(cb_item != NULL) {
191 if(cb->cb_fct == func) {
192 cf_callbacks = g_list_remove(cf_callbacks, cb);
196 cb_item = g_list_next(cb_item);
199 g_assert_not_reached();
203 cf_timestamp_auto_precision(capture_file *cf)
206 int prec = timestamp_get_precision();
209 /* don't try to get the file's precision if none is opened */
210 if(cf->state == FILE_CLOSED) {
214 /* if we are in auto mode, set precision of current file */
215 if(prec == TS_PREC_AUTO ||
216 prec == TS_PREC_AUTO_SEC ||
217 prec == TS_PREC_AUTO_DSEC ||
218 prec == TS_PREC_AUTO_CSEC ||
219 prec == TS_PREC_AUTO_MSEC ||
220 prec == TS_PREC_AUTO_USEC ||
221 prec == TS_PREC_AUTO_NSEC)
223 switch(wtap_file_tsprecision(cf->wth)) {
224 case(WTAP_FILE_TSPREC_SEC):
225 timestamp_set_precision(TS_PREC_AUTO_SEC);
227 case(WTAP_FILE_TSPREC_DSEC):
228 timestamp_set_precision(TS_PREC_AUTO_DSEC);
230 case(WTAP_FILE_TSPREC_CSEC):
231 timestamp_set_precision(TS_PREC_AUTO_CSEC);
233 case(WTAP_FILE_TSPREC_MSEC):
234 timestamp_set_precision(TS_PREC_AUTO_MSEC);
236 case(WTAP_FILE_TSPREC_USEC):
237 timestamp_set_precision(TS_PREC_AUTO_USEC);
239 case(WTAP_FILE_TSPREC_NSEC):
240 timestamp_set_precision(TS_PREC_AUTO_NSEC);
243 g_assert_not_reached();
246 /* Set the column widths of those columns that show the time in
247 "command-line-specified" format. */
248 for (i = 0; i < cf->cinfo.num_cols; i++) {
249 if (col_has_time_fmt(&cf->cinfo, i)) {
250 new_packet_list_resize_column(i);
256 cf_get_computed_elapsed(void)
258 return computed_elapsed;
261 static void reset_elapsed(void)
263 computed_elapsed = 0;
266 static void compute_elapsed(GTimeVal *start_time)
271 g_get_current_time(&time_now);
273 delta_time = (time_now.tv_sec - start_time->tv_sec) * 1e6 +
274 time_now.tv_usec - start_time->tv_usec;
276 computed_elapsed = (gulong) (delta_time / 1000); /* ms*/
280 cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
285 wth = wtap_open_offline(fname, err, &err_info, TRUE);
289 /* The open succeeded. Close whatever capture file we had open,
290 and fill in the information for this file. */
293 /* Cleanup all data structures used for dissection. */
294 cleanup_dissection();
295 /* Initialize all data structures used for dissection. */
298 /* We're about to start reading the file. */
299 cf->state = FILE_READ_IN_PROGRESS;
304 /* Set the file name because we need it to set the follow stream filter.
305 XXX - is that still true? We need it for other reasons, though,
307 cf->filename = g_strdup(fname);
309 /* Indicate whether it's a permanent or temporary file. */
310 cf->is_tempfile = is_tempfile;
312 /* If it's a temporary capture buffer file, mark it as not saved. */
313 cf->user_saved = !is_tempfile;
317 cf->cd_t = wtap_file_type(cf->wth);
319 cf->displayed_count = 0;
320 cf->marked_count = 0;
321 cf->ignored_count = 0;
322 cf->ref_time_count = 0;
323 cf->drops_known = FALSE;
325 cf->snap = wtap_snapshot_length(cf->wth);
327 /* Snapshot length not known. */
328 cf->has_snap = FALSE;
329 cf->snap = WTAP_MAX_PACKET_SIZE;
333 nstime_set_zero(&cf->elapsed_time);
334 nstime_set_unset(&first_ts);
335 nstime_set_unset(&prev_dis_ts);
336 nstime_set_unset(&prev_cap_ts);
339 #if GLIB_CHECK_VERSION(2,10,0)
341 /* memory chunks have been deprecated in favor of the slice allocator,
342 * which has been added in 2.10
344 cf->plist_chunk = g_mem_chunk_new("frame_data_chunk",
346 FRAME_DATA_CHUNK_SIZE * sizeof(frame_data),
348 g_assert(cf->plist_chunk);
351 /* Adjust timestamp precision if auto is selected, col width will be adjusted */
352 cf_timestamp_auto_precision(cf);
354 new_packet_list_queue_draw();
355 fileset_file_opened(fname);
357 if(cf->cd_t == WTAP_FILE_BER) {
358 /* tell the BER dissector the file name */
359 ber_set_filename(cf->filename);
365 cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
371 * Reset the state for the currently closed file, but don't do the
372 * UI callbacks; this is for use in "cf_open()", where we don't
373 * want the UI to go from "file open" to "file closed" back to
374 * "file open", we want it to go from "old file open" to "new file
375 * open and being read".
378 cf_reset_state(capture_file *cf)
380 /* Die if we're in the middle of reading a file. */
381 g_assert(cf->state != FILE_READ_IN_PROGRESS);
387 /* We have no file open... */
388 if (cf->filename != NULL) {
389 /* If it's a temporary file, remove it. */
391 ws_unlink(cf->filename);
392 g_free(cf->filename);
395 /* ...which means we have nothing to save. */
396 cf->user_saved = FALSE;
398 #if GLIB_CHECK_VERSION(2,10,0)
399 if (cf->plist_start != NULL)
400 g_slice_free_chain(frame_data, cf->plist_start, next);
402 /* memory chunks have been deprecated in favor of the slice allocator,
403 * which has been added in 2.10
405 if (cf->plist_chunk != NULL) {
406 g_mem_chunk_destroy(cf->plist_chunk);
407 cf->plist_chunk = NULL;
410 dfilter_free(cf->rfcode);
412 cf->plist_start = NULL;
413 cf->plist_end = NULL;
414 cf_unselect_packet(cf); /* nothing to select */
415 cf->first_displayed = NULL;
416 cf->last_displayed = NULL;
418 /* No frame selected, no field in that frame selected. */
419 cf->current_frame = NULL;
421 cf->finfo_selected = NULL;
423 /* Clear the packet list. */
424 new_packet_list_freeze();
425 new_packet_list_clear();
426 new_packet_list_thaw();
430 nstime_set_zero(&cf->elapsed_time);
432 reset_tap_listeners();
434 /* We have no file open. */
435 cf->state = FILE_CLOSED;
437 fileset_file_closed();
440 /* Reset everything to a pristine state */
442 cf_close(capture_file *cf)
444 /* do GUI things even if file is already closed,
445 * e.g. to cleanup things if a capture couldn't be started */
446 cf_callback_invoke(cf_cb_file_closing, cf);
448 /* close things, if not already closed before */
449 if(cf->state != FILE_CLOSED) {
450 color_filters_cleanup();
452 cleanup_dissection();
455 cf_callback_invoke(cf_cb_file_closed, cf);
458 /* an out of memory exception occured, wait for a user button press to exit */
459 static void outofmemory_cb(gpointer dialog _U_, gint btn _U_, gpointer data _U_)
465 calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos, gchar *status_str, gulong status_size)
469 progbar_val = (gfloat) file_pos / (gfloat) size;
470 if (progbar_val > 1.0) {
472 /* The file probably grew while we were reading it.
473 * Update file size, and try again.
475 size = wtap_file_size(cf->wth, NULL);
477 /* Another possibility is that we're reading a compressed file and we've
478 * read more (uncompressed) data from the file than exists in the
479 * (compressed) file. So check how much data we've actually read.
481 * This is inside this "if val > 1.0" check to avoid the lseek() when
482 * reading uncompressed files. Testing has (thus far) shown no progress
483 * bar weirdness resulting from switching from the data offset (when
484 * reading the first part of the file) to the real file position.
486 file_pos = wtap_read_so_far(cf->wth, NULL);
489 progbar_val = (gfloat) file_pos / (gfloat) size;
491 /* If it's still > 1, either "wtap_file_size()" failed (in which
492 * case there's not much we can do about it), or the file
493 * *shrank* (in which case there's not much we can do about
494 * it); just clip the progress value at 1.0.
496 if (progbar_val > 1.0f)
500 g_snprintf(status_str, status_size,
501 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
502 file_pos / 1024, size / 1024);
508 cf_read(capture_file *cf, gboolean from_save)
512 const gchar *name_ptr;
514 char errmsg_errno[1024+1];
516 progdlg_t *volatile progbar = NULL;
518 volatile gint64 size;
519 volatile float progbar_val;
521 gchar status_str[100];
522 volatile gint64 progbar_nextstep;
523 volatile gint64 progbar_quantum;
525 gboolean filtering_tap_listeners;
527 volatile int count = 0;
529 volatile int displayed_once = 0;
533 /* Compile the current display filter.
534 * We assume this will not fail since cf->dfilter is only set in
535 * cf_filter IFF the filter was valid.
537 compiled = dfilter_compile(cf->dfilter, &dfcode);
538 g_assert(!cf->dfilter || (compiled && dfcode));
540 /* Do we have any tap listeners with filters? */
541 filtering_tap_listeners = have_filtering_tap_listeners();
543 /* Get the union of the flags for all tap listeners. */
544 tap_flags = union_of_tap_listener_flags();
546 reset_tap_listeners();
548 name_ptr = get_basename(cf->filename);
550 if (from_save == FALSE)
551 cf_callback_invoke(cf_cb_file_read_started, cf);
553 cf_callback_invoke(cf_cb_file_save_started, (gpointer)name_ptr);
555 /* Find the size of the file. */
556 size = wtap_file_size(cf->wth, NULL);
558 /* Update the progress bar when it gets to this value. */
559 progbar_nextstep = 0;
560 /* When we reach the value that triggers a progress bar update,
561 bump that value by this amount. */
563 progbar_quantum = size/N_PROGBAR_UPDATES;
564 if (progbar_quantum < MIN_QUANTUM)
565 progbar_quantum = MIN_QUANTUM;
568 /* Progress so far. */
571 new_packet_list_freeze();
574 g_get_current_time(&start_time);
576 while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
579 /* Create the progress bar if necessary.
580 * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
582 if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)){
583 progbar_val = calc_progbar_val(cf, size, data_offset, status_str, sizeof(status_str));
584 if (from_save == FALSE)
585 progbar = delayed_create_progress_dlg("Loading", name_ptr,
586 TRUE, &stop_flag, &start_time, progbar_val);
588 progbar = delayed_create_progress_dlg("Saving", name_ptr,
589 TRUE, &stop_flag, &start_time, progbar_val);
592 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
593 when we update it, we have to run the GTK+ main loop to get it
594 to repaint what's pending, and doing so may involve an "ioctl()"
595 to see if there's any pending input from an X server, and doing
596 that for every packet can be costly, especially on a big file. */
597 if (data_offset >= progbar_nextstep) {
598 if (progbar != NULL) {
599 progbar_val = calc_progbar_val(cf, size, data_offset, status_str, sizeof(status_str));
600 /* update the packet lists content on the first run or frequently on very large files */
601 /* (on smaller files the display update takes longer than reading the file) */
603 if (progbar_quantum > 500000 || displayed_once == 0) {
604 if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->plist_end != NULL) {
606 new_packet_list_thaw();
607 if (auto_scroll_live)
608 new_packet_list_moveto_end();
609 new_packet_list_freeze();
612 #endif /* HAVE_LIBPCAP */
613 update_progress_dlg(progbar, progbar_val, status_str);
615 progbar_nextstep += progbar_quantum;
620 /* Well, the user decided to abort the read. He/She will be warned and
621 it might be enough for him/her to work with the already loaded
623 This is especially true for very large capture files, where you don't
624 want to wait loading the whole file (which may last minutes or even
625 hours even on fast machines) just to see that it was the wrong file. */
629 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
631 CATCH(OutOfMemoryError) {
634 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
635 "%sOut Of Memory!%s\n"
637 "Sorry, but Wireshark has to terminate now!\n"
639 "Some infos / workarounds can be found at:\n"
640 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
641 simple_dialog_primary_start(), simple_dialog_primary_end());
642 /* we have to terminate, as we cannot recover from the memory error */
643 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
645 main_window_update();
646 /* XXX - how to avoid a busy wait? */
654 /* Cleanup and release all dfilter resources */
656 dfilter_free(dfcode);
659 /* We're done reading the file; destroy the progress bar if it was created. */
661 destroy_progress_dlg(progbar);
663 /* We're done reading sequentially through the file. */
664 cf->state = FILE_READ_DONE;
666 /* Close the sequential I/O side, to free up memory it requires. */
667 wtap_sequential_close(cf->wth);
669 /* Allow the protocol dissectors to free up memory that they
670 * don't need after the sequential run-through of the packets. */
671 postseq_cleanup_all_protocols();
673 /* compute the time it took to load the file */
674 compute_elapsed(&start_time);
676 /* Set the file encapsulation type now; we don't know what it is until
677 we've looked at all the packets, as we don't know until then whether
678 there's more than one type (and thus whether it's
679 WTAP_ENCAP_PER_PACKET). */
680 cf->lnk_t = wtap_file_encap(cf->wth);
682 cf->current_frame = cf->first_displayed;
685 new_packet_list_thaw();
686 if (from_save == FALSE)
687 cf_callback_invoke(cf_cb_file_read_finished, cf);
689 cf_callback_invoke(cf_cb_file_save_finished, cf);
691 /* If we have any displayed packets to select, select the first of those
692 packets by making the first row the selected row. */
693 if (cf->first_displayed != NULL){
694 new_packet_list_select_first_row();
698 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
699 "%sFile loading was cancelled!%s\n"
701 "The remaining packets in the file were discarded.\n"
703 "As a lot of packets from the original file will be missing,\n"
704 "remember to be careful when saving the current content to a file.\n",
705 simple_dialog_primary_start(), simple_dialog_primary_end());
706 return CF_READ_ERROR;
710 /* Put up a message box noting that the read failed somewhere along
711 the line. Don't throw out the stuff we managed to read, though,
715 case WTAP_ERR_UNSUPPORTED_ENCAP:
716 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
717 "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
720 errmsg = errmsg_errno;
723 case WTAP_ERR_CANT_READ:
724 errmsg = "An attempt to read from the capture file failed for"
725 " some unknown reason.";
728 case WTAP_ERR_SHORT_READ:
729 errmsg = "The capture file appears to have been cut short"
730 " in the middle of a packet.";
733 case WTAP_ERR_BAD_RECORD:
734 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
735 "The capture file appears to be damaged or corrupt.\n(%s)",
738 errmsg = errmsg_errno;
742 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
743 "An error occurred while reading the"
744 " capture file: %s.", wtap_strerror(err));
745 errmsg = errmsg_errno;
748 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", errmsg);
749 return CF_READ_ERROR;
756 cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
758 cf_status_t cf_status;
760 cf_status = cf_open(cf, fname, is_tempfile, err);
765 cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
767 gint64 data_offset = 0;
769 volatile int newly_displayed_packets = 0;
771 gboolean filtering_tap_listeners;
775 /* Compile the current display filter.
776 * We assume this will not fail since cf->dfilter is only set in
777 * cf_filter IFF the filter was valid.
779 compiled = dfilter_compile(cf->dfilter, &dfcode);
780 g_assert(!cf->dfilter || (compiled && dfcode));
782 /* Do we have any tap listeners with filters? */
783 filtering_tap_listeners = have_filtering_tap_listeners();
785 /* Get the union of the flags for all tap listeners. */
786 tap_flags = union_of_tap_listener_flags();
790 new_packet_list_check_end();
791 /* Don't freeze/thaw the list when doing live capture */
792 /*new_packet_list_freeze();*/
794 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
796 while (to_read != 0) {
797 wtap_cleareof(cf->wth);
798 if (!wtap_read(cf->wth, err, &err_info, &data_offset)) {
801 if (cf->state == FILE_READ_ABORTED) {
802 /* Well, the user decided to exit Wireshark. Break out of the
803 loop, and let the code below (which is called even if there
804 aren't any packets left to read) exit. */
808 if (read_packet(cf, dfcode, filtering_tap_listeners, tap_flags,
809 data_offset) != -1) {
810 newly_displayed_packets++;
813 CATCH(OutOfMemoryError) {
816 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
817 "%sOut Of Memory!%s\n"
819 "Sorry, but Wireshark has to terminate now!\n"
821 "The capture file is not lost, it can be found at:\n"
824 "Some infos / workarounds can be found at:\n"
825 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
826 simple_dialog_primary_start(), simple_dialog_primary_end(), cf->filename);
827 /* we have to terminate, as we cannot recover from the memory error */
828 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
830 main_window_update();
831 /* XXX - how to avoid a busy wait? */
834 /* Don't freeze/thaw the list when doing live capture */
835 /*new_packet_list_thaw();*/
836 return CF_READ_ABORTED;
842 /* Cleanup and release all dfilter resources */
844 dfilter_free(dfcode);
847 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u",
848 cf->count, cf->state, *err);*/
850 /* Don't freeze/thaw the list when doing live capture */
851 /*new_packet_list_thaw();*/
852 /* With the new packet list the first packet
853 * isn't automatically selected.
855 if(!cf->current_frame)
856 new_packet_list_select_first_row();
858 /* moving to the end of the packet list - if the user requested so and
859 we have some new packets. */
860 if (newly_displayed_packets && auto_scroll_live && cf->plist_end != NULL)
861 new_packet_list_moveto_end();
863 if (cf->state == FILE_READ_ABORTED) {
864 /* Well, the user decided to exit Wireshark. Return CF_READ_ABORTED
865 so that our caller can kill off the capture child process;
866 this will cause an EOF on the pipe from the child, so
867 "cf_finish_tail()" will be called, and it will clean up
869 return CF_READ_ABORTED;
870 } else if (*err != 0) {
871 /* We got an error reading the capture file.
872 XXX - pop up a dialog box instead? */
873 g_warning("Error \"%s\" while reading: \"%s\"\n",
874 wtap_strerror(*err), cf->filename);
876 return CF_READ_ERROR;
882 cf_fake_continue_tail(capture_file *cf) {
883 cf->state = FILE_READ_DONE;
887 cf_finish_tail(capture_file *cf, int *err)
892 gboolean filtering_tap_listeners;
896 /* Compile the current display filter.
897 * We assume this will not fail since cf->dfilter is only set in
898 * cf_filter IFF the filter was valid.
900 compiled = dfilter_compile(cf->dfilter, &dfcode);
901 g_assert(!cf->dfilter || (compiled && dfcode));
903 /* Do we have any tap listeners with filters? */
904 filtering_tap_listeners = have_filtering_tap_listeners();
906 /* Get the union of the flags for all tap listeners. */
907 tap_flags = union_of_tap_listener_flags();
909 if(cf->wth == NULL) {
911 return CF_READ_ERROR;
914 new_packet_list_check_end();
915 /* Don't freeze/thaw the list when doing live capture */
916 /*new_packet_list_freeze();*/
918 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
919 if (cf->state == FILE_READ_ABORTED) {
920 /* Well, the user decided to abort the read. Break out of the
921 loop, and let the code below (which is called even if there
922 aren't any packets left to read) exit. */
925 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
928 /* Cleanup and release all dfilter resources */
930 dfilter_free(dfcode);
933 /* Don't freeze/thaw the list when doing live capture */
934 /*new_packet_list_thaw();*/
936 if (cf->state == FILE_READ_ABORTED) {
937 /* Well, the user decided to abort the read. We're only called
938 when the child capture process closes the pipe to us (meaning
939 it's probably exited), so we can just close the capture
940 file; we return CF_READ_ABORTED so our caller can do whatever
941 is appropriate when that happens. */
943 return CF_READ_ABORTED;
946 if (auto_scroll_live && cf->plist_end != NULL)
947 new_packet_list_moveto_end();
949 /* We're done reading sequentially through the file. */
950 cf->state = FILE_READ_DONE;
952 /* We're done reading sequentially through the file; close the
953 sequential I/O side, to free up memory it requires. */
954 wtap_sequential_close(cf->wth);
956 /* Allow the protocol dissectors to free up memory that they
957 * don't need after the sequential run-through of the packets. */
958 postseq_cleanup_all_protocols();
960 /* Set the file encapsulation type now; we don't know what it is until
961 we've looked at all the packets, as we don't know until then whether
962 there's more than one type (and thus whether it's
963 WTAP_ENCAP_PER_PACKET). */
964 cf->lnk_t = wtap_file_encap(cf->wth);
967 /* We got an error reading the capture file.
968 XXX - pop up a dialog box? */
969 return CF_READ_ERROR;
974 #endif /* HAVE_LIBPCAP */
977 cf_get_display_name(capture_file *cf)
979 const gchar *displayname;
981 /* Return a name to use in displays */
982 if (!cf->is_tempfile) {
983 /* Get the last component of the file name, and use that. */
985 displayname = get_basename(cf->filename);
987 displayname="(No file)";
990 /* The file we read is a temporary file from a live capture;
991 we don't mention its name. */
993 displayname = cf->source;
995 displayname = "(Untitled)";
1001 void cf_set_tempfile_source(capture_file *cf, gchar *source) {
1007 cf->source = g_strdup(source);
1009 cf->source = g_strdup("");
1013 const gchar *cf_get_tempfile_source(capture_file *cf) {
1021 /* XXX - use a macro instead? */
1023 cf_get_packet_count(capture_file *cf)
1028 /* XXX - use a macro instead? */
1030 cf_set_packet_count(capture_file *cf, int packet_count)
1032 cf->count = packet_count;
1035 /* XXX - use a macro instead? */
1037 cf_is_tempfile(capture_file *cf)
1039 return cf->is_tempfile;
1042 void cf_set_tempfile(capture_file *cf, gboolean is_tempfile)
1044 cf->is_tempfile = is_tempfile;
1048 /* XXX - use a macro instead? */
1049 void cf_set_drops_known(capture_file *cf, gboolean drops_known)
1051 cf->drops_known = drops_known;
1054 /* XXX - use a macro instead? */
1055 void cf_set_drops(capture_file *cf, guint32 drops)
1060 /* XXX - use a macro instead? */
1061 gboolean cf_get_drops_known(capture_file *cf)
1063 return cf->drops_known;
1066 /* XXX - use a macro instead? */
1067 guint32 cf_get_drops(capture_file *cf)
1072 void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
1074 cf->rfcode = rfcode;
1078 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
1079 dfilter_t *dfcode, gboolean filtering_tap_listeners,
1081 union wtap_pseudo_header *pseudo_header, const guchar *buf,
1083 gboolean add_to_packet_list)
1085 gboolean create_proto_tree = FALSE;
1090 cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
1092 frame_data_set_before_dissect(fdata, &cf->elapsed_time,
1093 &first_ts, &prev_dis_ts, &prev_cap_ts);
1096 + we have a display filter and are re-applying it;
1097 + we have tap listeners with filters;
1098 + we have tap listeners that require a protocol tree;
1100 allocate a protocol tree root node, so that we'll construct
1101 a protocol tree against which a filter expression can be
1103 if ((dfcode != NULL && refilter) ||
1104 filtering_tap_listeners || (tap_flags & TL_REQUIRES_PROTO_TREE))
1105 create_proto_tree = TRUE;
1107 /* Dissect the frame. */
1108 epan_dissect_init(&edt, create_proto_tree, FALSE);
1110 if (dfcode != NULL && refilter) {
1111 epan_dissect_prime_dfilter(&edt, dfcode);
1114 tap_queue_init(&edt);
1115 epan_dissect_run(&edt, pseudo_header, buf, fdata, cinfo);
1116 tap_push_tapped_queue(&edt);
1118 /* If we have a display filter, apply it if we're refiltering, otherwise
1119 leave the "passed_dfilter" flag alone.
1121 If we don't have a display filter, set "passed_dfilter" to 1. */
1122 if (dfcode != NULL) {
1124 fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, &edt) ? 1 : 0;
1127 fdata->flags.passed_dfilter = 1;
1129 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1130 cf->displayed_count++;
1132 if (add_to_packet_list) {
1133 /* We fill the needed columns from new_packet_list */
1134 row = new_packet_list_append(cinfo, fdata, &edt.pi);
1137 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1139 frame_data_set_after_dissect(fdata, &cum_bytes, &prev_dis_ts);
1141 /* If we haven't yet seen the first frame, this is it.
1143 XXX - we must do this before we add the row to the display,
1144 as, if the display's GtkCList's selection mode is
1145 GTK_SELECTION_BROWSE, when the first entry is added to it,
1146 "cf_select_packet()" will be called, and it will fetch the row
1147 data for the 0th row, and will get a null pointer rather than
1148 "fdata", as "gtk_clist_append()" won't yet have returned and
1149 thus "gtk_clist_set_row_data()" won't yet have been called.
1151 We thus need to leave behind bread crumbs so that
1152 "cf_select_packet()" can find this frame. See the comment
1153 in "cf_select_packet()". */
1154 if (cf->first_displayed == NULL)
1155 cf->first_displayed = fdata;
1157 /* This is the last frame we've seen so far. */
1158 cf->last_displayed = fdata;
1161 epan_dissect_cleanup(&edt);
1166 * Initialize the col_text and col_text_len arrays.
1169 init_col_text(frame_data *fdata, gint num_cols)
1171 fdata->col_text_len = se_alloc0(sizeof(*fdata->col_text_len) * num_cols);
1172 fdata->col_text = se_alloc0(sizeof(*fdata->col_text) * num_cols);
1175 /* read in a new packet */
1176 /* returns the row of the new packet in the packet list or -1 if not displayed */
1178 read_packet(capture_file *cf, dfilter_t *dfcode,
1179 gboolean filtering_tap_listeners, guint tap_flags, gint64 offset)
1181 const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
1182 union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
1183 const guchar *buf = wtap_buf_ptr(cf->wth);
1190 /* Allocate the next list entry, and add it to the list.
1191 * memory chunks have been deprecated in favor of the slice allocator,
1192 * which has been added in 2.10
1194 #if GLIB_CHECK_VERSION(2,10,0)
1195 fdata = g_slice_new(frame_data);
1197 fdata = g_mem_chunk_alloc(cf->plist_chunk);
1200 frame_data_init(fdata, cf->count, phdr, offset, cum_bytes);
1201 init_col_text(fdata, cf->cinfo.num_cols);
1206 epan_dissect_init(&edt, TRUE, FALSE);
1207 epan_dissect_prime_dfilter(&edt, cf->rfcode);
1208 epan_dissect_run(&edt, pseudo_header, buf, fdata, NULL);
1209 passed = dfilter_apply_edt(cf->rfcode, &edt);
1210 epan_dissect_cleanup(&edt);
1214 cap_file_add_fdata(cf, fdata);
1216 cf->f_datalen = offset + fdata->cap_len;
1218 if (!cf->redissecting) {
1219 row = add_packet_to_packet_list(fdata, cf, dfcode,
1220 filtering_tap_listeners, tap_flags,
1221 pseudo_header, buf, TRUE, TRUE);
1224 /* We didn't pass read filter so roll back count */
1227 /* XXX - if we didn't have read filters, or if we could avoid
1228 allocating the "frame_data" structure until we knew whether
1229 the frame passed the read filter, we could use a G_ALLOC_ONLY
1232 ...but, at least in one test I did, where I just made the chunk
1233 a G_ALLOC_ONLY chunk and read in a huge capture file, it didn't
1234 seem to save a noticeable amount of time or space. */
1235 #if GLIB_CHECK_VERSION(2,10,0)
1236 /* memory chunks have been deprecated in favor of the slice allocator,
1237 * which has been added in 2.10
1239 g_slice_free(frame_data,fdata);
1241 g_mem_chunk_free(cf->plist_chunk, fdata);
1249 cf_merge_files(char **out_filenamep, int in_file_count,
1250 char *const *in_filenames, int file_type, gboolean do_append)
1252 merge_in_file_t *in_files;
1258 int open_err, read_err, write_err, close_err;
1262 char errmsg_errno[1024+1];
1264 gboolean got_read_error = FALSE, got_write_error = FALSE;
1266 progdlg_t *progbar = NULL;
1268 gint64 f_len, file_pos;
1270 GTimeVal start_time;
1271 gchar status_str[100];
1272 gint64 progbar_nextstep;
1273 gint64 progbar_quantum;
1275 /* open the input files */
1276 if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
1277 &open_err, &err_info, &err_fileno)) {
1279 cf_open_failure_alert_box(in_filenames[err_fileno], open_err, err_info,
1284 if (*out_filenamep != NULL) {
1285 out_filename = *out_filenamep;
1286 out_fd = ws_open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
1290 out_fd = create_tempfile(&tmpname, "wireshark");
1293 out_filename = g_strdup(tmpname);
1294 *out_filenamep = out_filename;
1298 merge_close_in_files(in_file_count, in_files);
1300 cf_open_failure_alert_box(out_filename, open_err, NULL, TRUE, file_type);
1304 pdh = wtap_dump_fdopen(out_fd, file_type,
1305 merge_select_frame_type(in_file_count, in_files),
1306 merge_max_snapshot_length(in_file_count, in_files),
1307 FALSE /* compressed */, &open_err);
1310 merge_close_in_files(in_file_count, in_files);
1312 cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
1317 /* Get the sum of the sizes of all the files. */
1319 for (i = 0; i < in_file_count; i++)
1320 f_len += in_files[i].size;
1322 /* Update the progress bar when it gets to this value. */
1323 progbar_nextstep = 0;
1324 /* When we reach the value that triggers a progress bar update,
1325 bump that value by this amount. */
1326 progbar_quantum = f_len/N_PROGBAR_UPDATES;
1327 /* Progress so far. */
1331 g_get_current_time(&start_time);
1333 /* do the merge (or append) */
1336 wth = merge_append_read_packet(in_file_count, in_files, &read_err,
1339 wth = merge_read_packet(in_file_count, in_files, &read_err,
1343 got_read_error = TRUE;
1347 /* Get the sum of the data offsets in all of the files. */
1349 for (i = 0; i < in_file_count; i++)
1350 data_offset += in_files[i].data_offset;
1352 /* Create the progress bar if necessary.
1353 We check on every iteration of the loop, so that it takes no
1354 longer than the standard time to create it (otherwise, for a
1355 large file, we might take considerably longer than that standard
1356 time in order to get to the next progress bar step). */
1357 if (progbar == NULL) {
1358 progbar = delayed_create_progress_dlg("Merging", "files",
1359 FALSE, &stop_flag, &start_time, progbar_val);
1362 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1363 when we update it, we have to run the GTK+ main loop to get it
1364 to repaint what's pending, and doing so may involve an "ioctl()"
1365 to see if there's any pending input from an X server, and doing
1366 that for every packet can be costly, especially on a big file. */
1367 if (data_offset >= progbar_nextstep) {
1368 /* Get the sum of the seek positions in all of the files. */
1370 for (i = 0; i < in_file_count; i++)
1371 file_pos += wtap_read_so_far(in_files[i].wth, NULL);
1372 progbar_val = (gfloat) file_pos / (gfloat) f_len;
1373 if (progbar_val > 1.0f) {
1374 /* Some file probably grew while we were reading it.
1375 That "shouldn't happen", so we'll just clip the progress
1379 if (progbar != NULL) {
1380 g_snprintf(status_str, sizeof(status_str),
1381 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
1382 file_pos / 1024, f_len / 1024);
1383 update_progress_dlg(progbar, progbar_val, status_str);
1385 progbar_nextstep += progbar_quantum;
1389 /* Well, the user decided to abort the merge. */
1393 if (!wtap_dump(pdh, wtap_phdr(wth), wtap_pseudoheader(wth),
1394 wtap_buf_ptr(wth), &write_err)) {
1395 got_write_error = TRUE;
1400 /* We're done merging the files; destroy the progress bar if it was created. */
1401 if (progbar != NULL)
1402 destroy_progress_dlg(progbar);
1404 merge_close_in_files(in_file_count, in_files);
1405 if (!got_read_error && !got_write_error) {
1406 if (!wtap_dump_close(pdh, &write_err))
1407 got_write_error = TRUE;
1409 wtap_dump_close(pdh, &close_err);
1411 if (got_read_error) {
1413 * Find the file on which we got the error, and report the error.
1415 for (i = 0; i < in_file_count; i++) {
1416 if (in_files[i].state == GOT_ERROR) {
1417 /* Put up a message box noting that a read failed somewhere along
1421 case WTAP_ERR_UNSUPPORTED_ENCAP:
1422 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1423 "The capture file %%s has a packet with a network type that Wireshark doesn't support.\n(%s)",
1426 errmsg = errmsg_errno;
1429 case WTAP_ERR_CANT_READ:
1430 errmsg = "An attempt to read from the capture file %s failed for"
1431 " some unknown reason.";
1434 case WTAP_ERR_SHORT_READ:
1435 errmsg = "The capture file %s appears to have been cut short"
1436 " in the middle of a packet.";
1439 case WTAP_ERR_BAD_RECORD:
1440 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1441 "The capture file %%s appears to be damaged or corrupt.\n(%s)",
1444 errmsg = errmsg_errno;
1448 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1449 "An error occurred while reading the"
1450 " capture file %%s: %s.", wtap_strerror(read_err));
1451 errmsg = errmsg_errno;
1454 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg, in_files[i].filename);
1459 if (got_write_error) {
1460 /* Put up an alert box for the write error. */
1461 cf_write_failure_alert_box(out_filename, write_err);
1464 if (got_read_error || got_write_error || stop_flag) {
1465 /* Callers aren't expected to treat an error or an explicit abort
1466 differently - we put up error dialogs ourselves, so they don't
1474 cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
1476 const char *filter_new = dftext ? dftext : "";
1477 const char *filter_old = cf->dfilter ? cf->dfilter : "";
1479 GTimeVal start_time;
1481 /* if new filter equals old one, do nothing unless told to do so */
1482 if (!force && strcmp(filter_new, filter_old) == 0) {
1488 if (dftext == NULL) {
1489 /* The new filter is an empty filter (i.e., display all packets).
1490 * so leave dfcode==NULL
1494 * We have a filter; make a copy of it (as we'll be saving it),
1495 * and try to compile it.
1497 dftext = g_strdup(dftext);
1498 if (!dfilter_compile(dftext, &dfcode)) {
1499 /* The attempt failed; report an error. */
1500 gchar *safe_dftext = simple_dialog_format_message(dftext);
1501 gchar *safe_dfilter_error_msg = simple_dialog_format_message(
1503 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1506 "The following display filter isn't a valid display filter:\n%s\n"
1507 "See the help for a description of the display filter syntax.",
1508 simple_dialog_primary_start(), safe_dfilter_error_msg,
1509 simple_dialog_primary_end(), safe_dftext);
1510 g_free(safe_dfilter_error_msg);
1511 g_free(safe_dftext);
1517 if (dfcode == NULL) {
1518 /* Yes - free the filter text, and set it to null. */
1524 /* We have a valid filter. Replace the current filter. */
1525 g_free(cf->dfilter);
1526 cf->dfilter = dftext;
1527 g_get_current_time(&start_time);
1530 /* Now rescan the packet list, applying the new filter, but not
1531 throwing away information constructed on a previous pass. */
1532 if (dftext == NULL) {
1533 rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
1535 rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
1538 /* Cleanup and release all dfilter resources */
1539 dfilter_free(dfcode);
1545 cf_reftime_packets(capture_file *cf)
1548 ref_time_packets(cf);
1552 cf_redissect_packets(capture_file *cf)
1554 rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
1558 cf_read_frame_r(capture_file *cf, frame_data *fdata,
1559 union wtap_pseudo_header *pseudo_header, guint8 *pd)
1563 char errmsg_errno[1024+1];
1565 if (!wtap_seek_read(cf->wth, fdata->file_off, pseudo_header, pd,
1566 fdata->cap_len, &err, &err_info)) {
1569 case WTAP_ERR_UNSUPPORTED_ENCAP:
1570 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1571 "The file \"%%s\" has a packet with a network type that Wireshark doesn't support.\n(%s)",
1576 case WTAP_ERR_BAD_RECORD:
1577 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1578 "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
1579 wtap_strerror(err), err_info);
1584 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1585 "An error occurred while reading from the file \"%%s\": %s.",
1586 wtap_strerror(err));
1589 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg_errno, cf->filename);
1596 cf_read_frame(capture_file *cf, frame_data *fdata)
1598 return cf_read_frame_r(cf, fdata, &cf->pseudo_header, cf->pd);
1601 /* Rescan the list of packets, reconstructing the CList.
1603 "action" describes why we're doing this; it's used in the progress
1606 "action_item" describes what we're doing; it's used in the progress
1609 "refilter" is TRUE if we need to re-evaluate the filter expression.
1611 "redissect" is TRUE if we need to make the dissectors reconstruct
1612 any state information they have (because a preference that affects
1613 some dissector has changed, meaning some dissector might construct
1614 its state differently from the way it was constructed the last time). */
1616 rescan_packets(capture_file *cf, const char *action, const char *action_item,
1617 gboolean refilter, gboolean redissect)
1619 /* Rescan packets new packet list */
1621 progdlg_t *progbar = NULL;
1624 frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
1625 int selected_frame_num, preceding_frame_num, following_frame_num, prev_frame_num;
1626 gboolean selected_frame_seen;
1628 GTimeVal start_time;
1629 gchar status_str[100];
1630 int progbar_nextstep;
1631 int progbar_quantum;
1633 gboolean filtering_tap_listeners;
1635 gboolean add_to_packet_list = FALSE;
1638 /* Compile the current display filter.
1639 * We assume this will not fail since cf->dfilter is only set in
1640 * cf_filter IFF the filter was valid.
1642 compiled = dfilter_compile(cf->dfilter, &dfcode);
1643 g_assert(!cf->dfilter || (compiled && dfcode));
1645 /* Do we have any tap listeners with filters? */
1646 filtering_tap_listeners = have_filtering_tap_listeners();
1648 /* Get the union of the flags for all tap listeners. */
1649 tap_flags = union_of_tap_listener_flags();
1651 reset_tap_listeners();
1652 /* Which frame, if any, is the currently selected frame?
1653 XXX - should the selected frame or the focus frame be the "current"
1654 frame, that frame being the one from which "Find Frame" searches
1656 selected_frame = cf->current_frame;
1658 /* Mark frame num as not found */
1659 selected_frame_num = -1;
1661 /* Freeze the packet list while we redo it, so we don't get any
1662 screen updates while it happens. */
1663 new_packet_list_freeze();
1666 /* We need to re-initialize all the state information that protocols
1667 keep, because some preference that controls a dissector has changed,
1668 which might cause the state information to be constructed differently
1669 by that dissector. */
1671 /* We might receive new packets while redissecting, and we don't
1672 want to dissect those before their time. */
1673 cf->redissecting = TRUE;
1675 /* Cleanup all data structures used for dissection. */
1676 cleanup_dissection();
1677 /* Initialize all data structures used for dissection. */
1680 /* We need to redissect the packets so we have to discard our old
1681 * packet list store. */
1682 new_packet_list_clear();
1683 add_to_packet_list = TRUE;
1686 /* We don't yet know which will be the first and last frames displayed. */
1687 cf->first_displayed = NULL;
1688 cf->last_displayed = NULL;
1690 /* We currently don't display any packets */
1691 cf->displayed_count = 0;
1693 /* Iterate through the list of frames. Call a routine for each frame
1694 to check whether it should be displayed and, if so, add it to
1695 the display list. */
1696 nstime_set_unset(&first_ts);
1697 nstime_set_unset(&prev_dis_ts);
1698 nstime_set_unset(&prev_cap_ts);
1701 /* Update the progress bar when it gets to this value. */
1702 progbar_nextstep = 0;
1703 /* When we reach the value that triggers a progress bar update,
1704 bump that value by this amount. */
1705 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1706 /* Count of packets at which we've looked. */
1708 /* Progress so far. */
1712 g_get_current_time(&start_time);
1714 /* no previous row yet */
1715 prev_frame_num = -1;
1718 preceding_frame_num = -1;
1719 preceding_frame = NULL;
1720 following_frame_num = -1;
1721 following_frame = NULL;
1723 selected_frame_seen = FALSE;
1725 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
1726 /* Create the progress bar if necessary.
1727 We check on every iteration of the loop, so that it takes no
1728 longer than the standard time to create it (otherwise, for a
1729 large file, we might take considerably longer than that standard
1730 time in order to get to the next progress bar step). */
1731 if (progbar == NULL)
1732 progbar = delayed_create_progress_dlg(action, action_item, TRUE,
1733 &stop_flag, &start_time,
1736 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1737 when we update it, we have to run the GTK+ main loop to get it
1738 to repaint what's pending, and doing so may involve an "ioctl()"
1739 to see if there's any pending input from an X server, and doing
1740 that for every packet can be costly, especially on a big file. */
1741 if (count >= progbar_nextstep) {
1742 /* let's not divide by zero. I should never be started
1743 * with count == 0, so let's assert that
1745 g_assert(cf->count > 0);
1746 progbar_val = (gfloat) count / cf->count;
1748 if (progbar != NULL) {
1749 g_snprintf(status_str, sizeof(status_str),
1750 "%4u of %u frames", count, cf->count);
1751 update_progress_dlg(progbar, progbar_val, status_str);
1754 progbar_nextstep += progbar_quantum;
1758 /* Well, the user decided to abort the filtering. Just stop.
1760 XXX - go back to the previous filter? Users probably just
1761 want not to wait for a filtering operation to finish;
1762 unless we cancel by having no filter, reverting to the
1763 previous filter will probably be even more expensive than
1764 continuing the filtering, as it involves going back to the
1765 beginning and filtering, and even with no filter we currently
1766 have to re-generate the entire clist, which is also expensive.
1768 I'm not sure what Network Monitor does, but it doesn't appear
1769 to give you an unfiltered display if you cancel. */
1776 /* Since all state for the frame was destroyed, mark the frame
1777 * as not visited, free the GSList referring to the state
1778 * data (the per-frame data itself was freed by
1779 * "init_dissection()"), and null out the GSList pointer. */
1780 fdata->flags.visited = 0;
1781 frame_data_cleanup(fdata);
1783 /* cleanup_dissection() calls se_free_all();
1784 * And after that fdata->col_text (which is allocated using se_alloc0())
1785 * no longer points to valid memory.
1787 init_col_text(fdata, cf->cinfo.num_cols);
1790 if (!cf_read_frame(cf, fdata))
1791 break; /* error reading the frame */
1793 /* If the previous frame is displayed, and we haven't yet seen the
1794 selected frame, remember that frame - it's the closest one we've
1795 yet seen before the selected frame. */
1796 if (prev_frame_num != -1 && !selected_frame_seen && prev_frame->flags.passed_dfilter) {
1797 preceding_frame_num = prev_frame_num;
1798 preceding_frame = prev_frame;
1800 add_packet_to_packet_list(fdata, cf, dfcode, filtering_tap_listeners,
1801 tap_flags, &cf->pseudo_header, cf->pd,
1803 add_to_packet_list);
1805 /* If this frame is displayed, and this is the first frame we've
1806 seen displayed after the selected frame, remember this frame -
1807 it's the closest one we've yet seen at or after the selected
1809 if (fdata->flags.passed_dfilter && selected_frame_seen && following_frame_num == -1) {
1810 following_frame_num = fdata->num;
1811 following_frame = fdata;
1813 if (fdata == selected_frame) {
1814 selected_frame_seen = TRUE;
1815 if (fdata->flags.passed_dfilter)
1816 selected_frame_num = fdata->num;
1819 /* Remember this frame - it'll be the previous frame
1820 on the next pass through the loop. */
1821 prev_frame_num = fdata->num;
1825 /* We are done redissecting the packet list. */
1826 cf->redissecting = FALSE;
1829 /* Clear out what remains of the visited flags and per-frame data
1832 XXX - that may cause various forms of bogosity when dissecting
1833 these frames, as they won't have been seen by this sequential
1834 pass, but the only alternative I see is to keep scanning them
1835 even though the user requested that the scan stop, and that
1836 would leave the user stuck with an Wireshark grinding on
1837 until it finishes. Should we just stick them with that? */
1838 for (; fdata != NULL; fdata = fdata->next) {
1839 fdata->flags.visited = 0;
1840 frame_data_cleanup(fdata);
1844 /* We're done filtering the packets; destroy the progress bar if it
1846 if (progbar != NULL)
1847 destroy_progress_dlg(progbar);
1849 /* Unfreeze the packet list. */
1850 if (!add_to_packet_list)
1851 new_packet_list_recreate_visible_rows();
1853 /* Compute the time it took to filter the file */
1854 compute_elapsed(&start_time);
1856 new_packet_list_thaw();
1858 if (selected_frame_num == -1) {
1859 /* The selected frame didn't pass the filter. */
1860 if (selected_frame == NULL) {
1861 /* That's because there *was* no selected frame. Make the first
1862 displayed frame the current frame. */
1863 selected_frame_num = 0;
1865 /* Find the nearest displayed frame to the selected frame (whether
1866 it's before or after that frame) and make that the current frame.
1867 If the next and previous displayed frames are equidistant from the
1868 selected frame, choose the next one. */
1869 g_assert(following_frame == NULL ||
1870 following_frame->num >= selected_frame->num);
1871 g_assert(preceding_frame == NULL ||
1872 preceding_frame->num <= selected_frame->num);
1873 if (following_frame == NULL) {
1874 /* No frame after the selected frame passed the filter, so we
1875 have to select the last displayed frame before the selected
1877 selected_frame_num = preceding_frame_num;
1878 selected_frame = preceding_frame;
1879 } else if (preceding_frame == NULL) {
1880 /* No frame before the selected frame passed the filter, so we
1881 have to select the first displayed frame after the selected
1883 selected_frame_num = following_frame_num;
1884 selected_frame = following_frame;
1886 /* Frames before and after the selected frame passed the filter, so
1887 we'll select the previous frame */
1888 selected_frame_num = preceding_frame_num;
1889 selected_frame = preceding_frame;
1894 if (selected_frame_num == -1) {
1895 /* There are no frames displayed at all. */
1896 cf_unselect_packet(cf);
1898 /* Either the frame that was selected passed the filter, or we've
1899 found the nearest displayed frame to that frame. Select it, make
1900 it the focus row, and make it visible. */
1901 /* Set to invalid to force update of packet list and packet details */
1902 cf->current_row = -1;
1903 if (selected_frame_num == 0) {
1904 new_packet_list_select_first_row();
1906 new_packet_list_find_row_from_data(selected_frame, TRUE);
1910 /* Cleanup and release all dfilter resources */
1911 dfilter_free(dfcode);
1916 * Scan trough all frame data and recalculate the ref time
1917 * without rereading the file.
1918 * XXX - do we need a progres bar or is this fast enough?
1921 ref_time_packets(capture_file *cf)
1925 nstime_set_unset(&first_ts);
1926 nstime_set_unset(&prev_dis_ts);
1929 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
1930 /* just add some value here until we know if it is being displayed or not */
1931 fdata->cum_bytes = cum_bytes + fdata->pkt_len;
1937 /* If we don't have the time stamp of the first packet in the
1938 capture, it's because this is the first packet. Save the time
1939 stamp of this packet as the time stamp of the first packet. */
1940 if (nstime_is_unset(&first_ts)) {
1941 first_ts = fdata->abs_ts;
1943 /* if this frames is marked as a reference time frame, reset
1944 firstsec and firstusec to this frame */
1945 if(fdata->flags.ref_time){
1946 first_ts = fdata->abs_ts;
1949 /* If we don't have the time stamp of the previous displayed packet,
1950 it's because this is the first displayed packet. Save the time
1951 stamp of this packet as the time stamp of the previous displayed
1953 if (nstime_is_unset(&prev_dis_ts)) {
1954 prev_dis_ts = fdata->abs_ts;
1957 /* Get the time elapsed between the first packet and this packet. */
1958 nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
1960 /* If it's greater than the current elapsed time, set the elapsed time
1961 to it (we check for "greater than" so as not to be confused by
1962 time moving backwards). */
1963 if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
1964 || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
1965 cf->elapsed_time = fdata->rel_ts;
1968 /* Get the time elapsed between the previous displayed packet and
1970 nstime_delta(&fdata->del_dis_ts, &fdata->abs_ts, &prev_dis_ts);
1972 prev_dis_ts = fdata->abs_ts;
1977 if( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) ){
1978 /* This frame either passed the display filter list or is marked as
1979 a time reference frame. All time reference frames are displayed
1980 even if they dont pass the display filter */
1981 if(fdata->flags.ref_time){
1982 /* if this was a TIME REF frame we should reset the cum_bytes field */
1983 cum_bytes = fdata->pkt_len;
1984 fdata->cum_bytes = cum_bytes;
1986 /* increase cum_bytes with this packets length */
1987 cum_bytes += fdata->pkt_len;
2000 process_specified_packets(capture_file *cf, packet_range_t *range,
2001 const char *string1, const char *string2, gboolean terminate_is_stop,
2002 gboolean (*callback)(capture_file *, frame_data *,
2003 union wtap_pseudo_header *, const guint8 *, void *),
2004 void *callback_args)
2007 union wtap_pseudo_header pseudo_header;
2008 guint8 pd[WTAP_MAX_PACKET_SIZE+1];
2009 psp_return_t ret = PSP_FINISHED;
2011 progdlg_t *progbar = NULL;
2014 gboolean progbar_stop_flag;
2015 GTimeVal progbar_start_time;
2016 gchar progbar_status_str[100];
2017 int progbar_nextstep;
2018 int progbar_quantum;
2019 range_process_e process_this;
2021 /* Update the progress bar when it gets to this value. */
2022 progbar_nextstep = 0;
2023 /* When we reach the value that triggers a progress bar update,
2024 bump that value by this amount. */
2025 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2026 /* Count of packets at which we've looked. */
2028 /* Progress so far. */
2031 progbar_stop_flag = FALSE;
2032 g_get_current_time(&progbar_start_time);
2034 packet_range_process_init(range);
2036 /* Iterate through the list of packets, printing the packets that
2037 were selected by the current display filter. */
2038 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
2039 /* Create the progress bar if necessary.
2040 We check on every iteration of the loop, so that it takes no
2041 longer than the standard time to create it (otherwise, for a
2042 large file, we might take considerably longer than that standard
2043 time in order to get to the next progress bar step). */
2044 if (progbar == NULL)
2045 progbar = delayed_create_progress_dlg(string1, string2,
2048 &progbar_start_time,
2051 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2052 when we update it, we have to run the GTK+ main loop to get it
2053 to repaint what's pending, and doing so may involve an "ioctl()"
2054 to see if there's any pending input from an X server, and doing
2055 that for every packet can be costly, especially on a big file. */
2056 if (progbar_count >= progbar_nextstep) {
2057 /* let's not divide by zero. I should never be started
2058 * with count == 0, so let's assert that
2060 g_assert(cf->count > 0);
2061 progbar_val = (gfloat) progbar_count / cf->count;
2063 if (progbar != NULL) {
2064 g_snprintf(progbar_status_str, sizeof(progbar_status_str),
2065 "%4u of %u packets", progbar_count, cf->count);
2066 update_progress_dlg(progbar, progbar_val, progbar_status_str);
2069 progbar_nextstep += progbar_quantum;
2072 if (progbar_stop_flag) {
2073 /* Well, the user decided to abort the operation. Just stop,
2074 and arrange to return PSP_STOPPED to our caller, so they know
2075 it was stopped explicitly. */
2082 /* do we have to process this packet? */
2083 process_this = packet_range_process_packet(range, fdata);
2084 if (process_this == range_process_next) {
2085 /* this packet uninteresting, continue with next one */
2087 } else if (process_this == range_processing_finished) {
2088 /* all interesting packets processed, stop the loop */
2092 /* Get the packet */
2093 if (!cf_read_frame_r(cf, fdata, &pseudo_header, pd)) {
2094 /* Attempt to get the packet failed. */
2098 /* Process the packet */
2099 if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
2100 /* Callback failed. We assume it reported the error appropriately. */
2106 /* We're done printing the packets; destroy the progress bar if
2108 if (progbar != NULL)
2109 destroy_progress_dlg(progbar);
2115 gboolean construct_protocol_tree;
2117 } retap_callback_args_t;
2120 retap_packet(capture_file *cf _U_, frame_data *fdata,
2121 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2124 retap_callback_args_t *args = argsp;
2127 epan_dissect_init(&edt, args->construct_protocol_tree, FALSE);
2128 tap_queue_init(&edt);
2129 epan_dissect_run(&edt, pseudo_header, pd, fdata, args->cinfo);
2130 tap_push_tapped_queue(&edt);
2131 epan_dissect_cleanup(&edt);
2137 cf_retap_packets(capture_file *cf)
2139 packet_range_t range;
2140 retap_callback_args_t callback_args;
2141 gboolean filtering_tap_listeners;
2144 /* Do we have any tap listeners with filters? */
2145 filtering_tap_listeners = have_filtering_tap_listeners();
2147 tap_flags = union_of_tap_listener_flags();
2149 /* If any tap listeners have filters, or require the protocol tree,
2150 construct the protocol tree. */
2151 callback_args.construct_protocol_tree = filtering_tap_listeners ||
2152 (tap_flags & TL_REQUIRES_PROTO_TREE);
2154 /* If any tap listeners require the columns, construct them. */
2155 callback_args.cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
2157 /* Reset the tap listeners. */
2158 reset_tap_listeners();
2160 /* Iterate through the list of packets, dissecting all packets and
2161 re-running the taps. */
2162 packet_range_init(&range);
2163 packet_range_process_init(&range);
2164 switch (process_specified_packets(cf, &range, "Recalculating statistics on",
2165 "all packets", TRUE, retap_packet,
2168 /* Completed successfully. */
2172 /* Well, the user decided to abort the refiltering.
2173 Return CF_READ_ABORTED so our caller knows they did that. */
2174 return CF_READ_ABORTED;
2177 /* Error while retapping. */
2178 return CF_READ_ERROR;
2181 g_assert_not_reached();
2186 print_args_t *print_args;
2187 gboolean print_header_line;
2188 char *header_line_buf;
2189 int header_line_buf_len;
2190 gboolean print_formfeed;
2191 gboolean print_separator;
2195 } print_callback_args_t;
2198 print_packet(capture_file *cf, frame_data *fdata,
2199 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2202 print_callback_args_t *args = argsp;
2209 gboolean proto_tree_needed;
2210 char bookmark_name[9+10+1]; /* "__frameNNNNNNNNNN__\0" */
2211 char bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0" */
2213 /* Create the protocol tree, and make it visible, if we're printing
2214 the dissection or the hex data.
2215 XXX - do we need it if we're just printing the hex data? */
2217 args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex || have_custom_cols(&cf->cinfo);
2218 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2220 /* Fill in the column information if we're printing the summary
2222 if (args->print_args->print_summary) {
2223 col_custom_prime_edt(&edt, &cf->cinfo);
2224 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2225 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2227 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2229 if (args->print_formfeed) {
2230 if (!new_page(args->print_args->stream))
2233 if (args->print_separator) {
2234 if (!print_line(args->print_args->stream, 0, ""))
2240 * We generate bookmarks, if the output format supports them.
2241 * The name is "__frameN__".
2243 g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
2245 if (args->print_args->print_summary) {
2246 if (args->print_header_line) {
2247 if (!print_line(args->print_args->stream, 0, args->header_line_buf))
2249 args->print_header_line = FALSE; /* we might not need to print any more */
2251 cp = &args->line_buf[0];
2253 for (i = 0; i < cf->cinfo.num_cols; i++) {
2254 /* Find the length of the string for this column. */
2255 column_len = (int) strlen(cf->cinfo.col_data[i]);
2256 if (args->col_widths[i] > column_len)
2257 column_len = args->col_widths[i];
2259 /* Make sure there's room in the line buffer for the column; if not,
2260 double its length. */
2261 line_len += column_len + 1; /* "+1" for space */
2262 if (line_len > args->line_buf_len) {
2263 cp_off = (int) (cp - args->line_buf);
2264 args->line_buf_len = 2 * line_len;
2265 args->line_buf = g_realloc(args->line_buf, args->line_buf_len + 1);
2266 cp = args->line_buf + cp_off;
2269 /* Right-justify the packet number column. */
2270 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2271 g_snprintf(cp, column_len+1, "%*s", args->col_widths[i], cf->cinfo.col_data[i]);
2273 g_snprintf(cp, column_len+1, "%-*s", args->col_widths[i], cf->cinfo.col_data[i]);
2275 if (i != cf->cinfo.num_cols - 1)
2281 * Generate a bookmark, using the summary line as the title.
2283 if (!print_bookmark(args->print_args->stream, bookmark_name,
2287 if (!print_line(args->print_args->stream, 0, args->line_buf))
2291 * Generate a bookmark, using "Frame N" as the title, as we're not
2292 * printing the summary line.
2294 g_snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
2295 if (!print_bookmark(args->print_args->stream, bookmark_name,
2298 } /* if (print_summary) */
2300 if (args->print_args->print_dissections != print_dissections_none) {
2301 if (args->print_args->print_summary) {
2302 /* Separate the summary line from the tree with a blank line. */
2303 if (!print_line(args->print_args->stream, 0, ""))
2307 /* Print the information in that tree. */
2308 if (!proto_tree_print(args->print_args, &edt, args->print_args->stream))
2311 /* Print a blank line if we print anything after this (aka more than one packet). */
2312 args->print_separator = TRUE;
2314 /* Print a header line if we print any more packet summaries */
2315 args->print_header_line = TRUE;
2318 if (args->print_args->print_hex) {
2319 /* Print the full packet data as hex. */
2320 if (!print_hex_data(args->print_args->stream, &edt))
2323 /* Print a blank line if we print anything after this (aka more than one packet). */
2324 args->print_separator = TRUE;
2326 /* Print a header line if we print any more packet summaries */
2327 args->print_header_line = TRUE;
2328 } /* if (args->print_args->print_dissections != print_dissections_none) */
2330 epan_dissect_cleanup(&edt);
2332 /* do we want to have a formfeed between each packet from now on? */
2333 if(args->print_args->print_formfeed) {
2334 args->print_formfeed = TRUE;
2340 epan_dissect_cleanup(&edt);
2345 cf_print_packets(capture_file *cf, print_args_t *print_args)
2348 print_callback_args_t callback_args;
2356 callback_args.print_args = print_args;
2357 callback_args.print_header_line = TRUE;
2358 callback_args.header_line_buf = NULL;
2359 callback_args.header_line_buf_len = 256;
2360 callback_args.print_formfeed = FALSE;
2361 callback_args.print_separator = FALSE;
2362 callback_args.line_buf = NULL;
2363 callback_args.line_buf_len = 256;
2364 callback_args.col_widths = NULL;
2366 if (!print_preamble(print_args->stream, cf->filename)) {
2367 destroy_print_stream(print_args->stream);
2368 return CF_PRINT_WRITE_ERROR;
2371 if (print_args->print_summary) {
2372 /* We're printing packet summaries. Allocate the header line buffer
2373 and get the column widths. */
2374 callback_args.header_line_buf = g_malloc(callback_args.header_line_buf_len + 1);
2376 /* Find the widths for each of the columns - maximum of the
2377 width of the title and the width of the data - and construct
2378 a buffer with a line containing the column titles. */
2379 callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * cf->cinfo.num_cols);
2380 cp = &callback_args.header_line_buf[0];
2382 for (i = 0; i < cf->cinfo.num_cols; i++) {
2383 /* Don't pad the last column. */
2384 if (i == cf->cinfo.num_cols - 1)
2385 callback_args.col_widths[i] = 0;
2387 callback_args.col_widths[i] = (gint) strlen(cf->cinfo.col_title[i]);
2388 data_width = get_column_char_width(get_column_format(i));
2389 if (data_width > callback_args.col_widths[i])
2390 callback_args.col_widths[i] = data_width;
2393 /* Find the length of the string for this column. */
2394 column_len = (int) strlen(cf->cinfo.col_title[i]);
2395 if (callback_args.col_widths[i] > column_len)
2396 column_len = callback_args.col_widths[i];
2398 /* Make sure there's room in the line buffer for the column; if not,
2399 double its length. */
2400 line_len += column_len + 1; /* "+1" for space */
2401 if (line_len > callback_args.header_line_buf_len) {
2402 cp_off = (int) (cp - callback_args.header_line_buf);
2403 callback_args.header_line_buf_len = 2 * line_len;
2404 callback_args.header_line_buf = g_realloc(callback_args.header_line_buf,
2405 callback_args.header_line_buf_len + 1);
2406 cp = callback_args.header_line_buf + cp_off;
2409 /* Right-justify the packet number column. */
2410 /* if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2411 g_snprintf(cp, column_len+1, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2413 g_snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2415 if (i != cf->cinfo.num_cols - 1)
2420 /* Now start out the main line buffer with the same length as the
2421 header line buffer. */
2422 callback_args.line_buf_len = callback_args.header_line_buf_len;
2423 callback_args.line_buf = g_malloc(callback_args.line_buf_len + 1);
2424 } /* if (print_summary) */
2426 /* Iterate through the list of packets, printing the packets we were
2428 ret = process_specified_packets(cf, &print_args->range, "Printing",
2429 "selected packets", TRUE, print_packet,
2432 g_free(callback_args.header_line_buf);
2433 g_free(callback_args.line_buf);
2434 g_free(callback_args.col_widths);
2439 /* Completed successfully. */
2443 /* Well, the user decided to abort the printing.
2445 XXX - note that what got generated before they did that
2446 will get printed if we're piping to a print program; we'd
2447 have to write to a file and then hand that to the print
2448 program to make it actually not print anything. */
2452 /* Error while printing.
2454 XXX - note that what got generated before they did that
2455 will get printed if we're piping to a print program; we'd
2456 have to write to a file and then hand that to the print
2457 program to make it actually not print anything. */
2458 destroy_print_stream(print_args->stream);
2459 return CF_PRINT_WRITE_ERROR;
2462 if (!print_finale(print_args->stream)) {
2463 destroy_print_stream(print_args->stream);
2464 return CF_PRINT_WRITE_ERROR;
2467 if (!destroy_print_stream(print_args->stream))
2468 return CF_PRINT_WRITE_ERROR;
2474 write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
2475 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2481 /* Create the protocol tree, but don't fill in the column information. */
2482 epan_dissect_init(&edt, TRUE, TRUE);
2483 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2485 /* Write out the information in that tree. */
2486 proto_tree_write_pdml(&edt, fh);
2488 epan_dissect_cleanup(&edt);
2494 cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
2499 fh = ws_fopen(print_args->file, "w");
2501 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2503 write_pdml_preamble(fh);
2506 return CF_PRINT_WRITE_ERROR;
2509 /* Iterate through the list of packets, printing the packets we were
2511 ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
2512 "selected packets", TRUE,
2513 write_pdml_packet, fh);
2518 /* Completed successfully. */
2522 /* Well, the user decided to abort the printing. */
2526 /* Error while printing. */
2528 return CF_PRINT_WRITE_ERROR;
2531 write_pdml_finale(fh);
2534 return CF_PRINT_WRITE_ERROR;
2537 /* XXX - check for an error */
2544 write_psml_packet(capture_file *cf, frame_data *fdata,
2545 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2550 gboolean proto_tree_needed;
2552 /* Fill in the column information, only create the protocol tree
2553 if having custom columns. */
2554 proto_tree_needed = have_custom_cols(&cf->cinfo);
2555 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2556 col_custom_prime_edt(&edt, &cf->cinfo);
2557 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2558 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2560 /* Write out the information in that tree. */
2561 proto_tree_write_psml(&edt, fh);
2563 epan_dissect_cleanup(&edt);
2569 cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
2574 fh = ws_fopen(print_args->file, "w");
2576 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2578 write_psml_preamble(fh);
2581 return CF_PRINT_WRITE_ERROR;
2584 /* Iterate through the list of packets, printing the packets we were
2586 ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
2587 "selected packets", TRUE,
2588 write_psml_packet, fh);
2593 /* Completed successfully. */
2597 /* Well, the user decided to abort the printing. */
2601 /* Error while printing. */
2603 return CF_PRINT_WRITE_ERROR;
2606 write_psml_finale(fh);
2609 return CF_PRINT_WRITE_ERROR;
2612 /* XXX - check for an error */
2619 write_csv_packet(capture_file *cf, frame_data *fdata,
2620 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2625 gboolean proto_tree_needed;
2627 /* Fill in the column information, only create the protocol tree
2628 if having custom columns. */
2629 proto_tree_needed = have_custom_cols(&cf->cinfo);
2630 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2631 col_custom_prime_edt(&edt, &cf->cinfo);
2632 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2633 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2635 /* Write out the information in that tree. */
2636 proto_tree_write_csv(&edt, fh);
2638 epan_dissect_cleanup(&edt);
2644 cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
2649 fh = ws_fopen(print_args->file, "w");
2651 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2653 write_csv_preamble(fh);
2656 return CF_PRINT_WRITE_ERROR;
2659 /* Iterate through the list of packets, printing the packets we were
2661 ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
2662 "selected packets", TRUE,
2663 write_csv_packet, fh);
2668 /* Completed successfully. */
2672 /* Well, the user decided to abort the printing. */
2676 /* Error while printing. */
2678 return CF_PRINT_WRITE_ERROR;
2681 write_csv_finale(fh);
2684 return CF_PRINT_WRITE_ERROR;
2687 /* XXX - check for an error */
2694 write_carrays_packet(capture_file *cf _U_, frame_data *fdata,
2695 union wtap_pseudo_header *pseudo_header _U_,
2696 const guint8 *pd, void *argsp)
2700 proto_tree_write_carrays(pd, fdata->cap_len, fdata->num, fh);
2705 cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
2710 fh = ws_fopen(print_args->file, "w");
2713 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2715 write_carrays_preamble(fh);
2719 return CF_PRINT_WRITE_ERROR;
2722 /* Iterate through the list of packets, printing the packets we were
2724 ret = process_specified_packets(cf, &print_args->range,
2726 "selected packets", TRUE,
2727 write_carrays_packet, fh);
2730 /* Completed successfully. */
2733 /* Well, the user decided to abort the printing. */
2736 /* Error while printing. */
2738 return CF_PRINT_WRITE_ERROR;
2741 write_carrays_finale(fh);
2745 return CF_PRINT_WRITE_ERROR;
2753 cf_find_packet_protocol_tree(capture_file *cf, const char *string,
2754 search_direction dir)
2758 mdata.string = string;
2759 mdata.string_len = strlen(string);
2760 return find_packet(cf, match_protocol_tree, &mdata, dir);
2764 cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree, match_data *mdata)
2766 mdata->frame_matched = FALSE;
2767 mdata->string = convert_string_case(cf->sfilter, cf->case_type);
2768 mdata->string_len = strlen(mdata->string);
2770 /* Iterate through all the nodes looking for matching text */
2771 proto_tree_children_foreach(tree, match_subtree_text, mdata);
2772 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2776 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
2778 match_data *mdata = criterion;
2781 /* Load the frame's data. */
2782 if (!cf_read_frame(cf, fdata)) {
2783 /* Attempt to get the packet failed. */
2787 /* Construct the protocol tree, including the displayed text */
2788 epan_dissect_init(&edt, TRUE, TRUE);
2789 /* We don't need the column information */
2790 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
2792 /* Iterate through all the nodes, seeing if they have text that matches. */
2794 mdata->frame_matched = FALSE;
2795 proto_tree_children_foreach(edt.tree, match_subtree_text, mdata);
2796 epan_dissect_cleanup(&edt);
2797 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2801 match_subtree_text(proto_node *node, gpointer data)
2803 match_data *mdata = (match_data*) data;
2804 const gchar *string = mdata->string;
2805 size_t string_len = mdata->string_len;
2806 capture_file *cf = mdata->cf;
2807 field_info *fi = PNODE_FINFO(node);
2808 gchar label_str[ITEM_LABEL_LENGTH];
2815 g_assert(fi && "dissection with an invisible proto tree?");
2817 if (mdata->frame_matched) {
2818 /* We already had a match; don't bother doing any more work. */
2822 /* Don't match invisible entries. */
2823 if (PROTO_ITEM_IS_HIDDEN(node))
2826 /* was a free format label produced? */
2828 label_ptr = fi->rep->representation;
2830 /* no, make a generic label */
2831 label_ptr = label_str;
2832 proto_item_fill_label(fi, label_str);
2835 /* Does that label match? */
2836 label_len = strlen(label_ptr);
2837 for (i = 0; i < label_len; i++) {
2838 c_char = label_ptr[i];
2840 c_char = toupper(c_char);
2841 if (c_char == string[c_match]) {
2843 if (c_match == string_len) {
2844 /* No need to look further; we have a match */
2845 mdata->frame_matched = TRUE;
2853 /* Recurse into the subtree, if it exists */
2854 if (node->first_child != NULL)
2855 proto_tree_children_foreach(node, match_subtree_text, mdata);
2859 cf_find_packet_summary_line(capture_file *cf, const char *string,
2860 search_direction dir)
2864 mdata.string = string;
2865 mdata.string_len = strlen(string);
2866 return find_packet(cf, match_summary_line, &mdata, dir);
2870 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
2872 match_data *mdata = criterion;
2873 const gchar *string = mdata->string;
2874 size_t string_len = mdata->string_len;
2876 const char *info_column;
2877 size_t info_column_len;
2878 match_result result = MR_NOTMATCHED;
2884 /* Load the frame's data. */
2885 if (!cf_read_frame(cf, fdata)) {
2886 /* Attempt to get the packet failed. */
2890 /* Don't bother constructing the protocol tree */
2891 epan_dissect_init(&edt, FALSE, FALSE);
2892 /* Get the column information */
2893 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, &cf->cinfo);
2895 /* Find the Info column */
2896 for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
2897 if (cf->cinfo.fmt_matx[colx][COL_INFO]) {
2898 /* Found it. See if we match. */
2899 info_column = edt.pi.cinfo->col_data[colx];
2900 info_column_len = strlen(info_column);
2901 for (i = 0; i < info_column_len; i++) {
2902 c_char = info_column[i];
2904 c_char = toupper(c_char);
2905 if (c_char == string[c_match]) {
2907 if (c_match == string_len) {
2908 result = MR_MATCHED;
2917 epan_dissect_cleanup(&edt);
2924 } cbs_t; /* "Counted byte string" */
2927 cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
2928 search_direction dir)
2933 info.data_len = string_size;
2935 /* String or hex search? */
2937 /* String search - what type of string? */
2938 switch (cf->scs_type) {
2940 case SCS_ASCII_AND_UNICODE:
2941 return find_packet(cf, match_ascii_and_unicode, &info, dir);
2944 return find_packet(cf, match_ascii, &info, dir);
2947 return find_packet(cf, match_unicode, &info, dir);
2950 g_assert_not_reached();
2954 return find_packet(cf, match_binary, &info, dir);
2958 match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
2960 cbs_t *info = criterion;
2961 const guint8 *ascii_text = info->data;
2962 size_t textlen = info->data_len;
2963 match_result result;
2969 /* Load the frame's data. */
2970 if (!cf_read_frame(cf, fdata)) {
2971 /* Attempt to get the packet failed. */
2975 result = MR_NOTMATCHED;
2976 buf_len = fdata->pkt_len;
2977 for (i = 0; i < buf_len; i++) {
2980 c_char = toupper(c_char);
2982 if (c_char == ascii_text[c_match]) {
2984 if (c_match == textlen) {
2985 result = MR_MATCHED;
2986 cf->search_pos = i; /* Save the position of the last character
2987 for highlighting the field. */
2998 match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
3000 cbs_t *info = criterion;
3001 const guint8 *ascii_text = info->data;
3002 size_t textlen = info->data_len;
3003 match_result result;
3009 /* Load the frame's data. */
3010 if (!cf_read_frame(cf, fdata)) {
3011 /* Attempt to get the packet failed. */
3015 result = MR_NOTMATCHED;
3016 buf_len = fdata->pkt_len;
3017 for (i = 0; i < buf_len; i++) {
3020 c_char = toupper(c_char);
3021 if (c_char == ascii_text[c_match]) {
3023 if (c_match == textlen) {
3024 result = MR_MATCHED;
3025 cf->search_pos = i; /* Save the position of the last character
3026 for highlighting the field. */
3036 match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
3038 cbs_t *info = criterion;
3039 const guint8 *ascii_text = info->data;
3040 size_t textlen = info->data_len;
3041 match_result result;
3047 /* Load the frame's data. */
3048 if (!cf_read_frame(cf, fdata)) {
3049 /* Attempt to get the packet failed. */
3053 result = MR_NOTMATCHED;
3054 buf_len = fdata->pkt_len;
3055 for (i = 0; i < buf_len; i++) {
3058 c_char = toupper(c_char);
3059 if (c_char == ascii_text[c_match]) {
3062 if (c_match == textlen) {
3063 result = MR_MATCHED;
3064 cf->search_pos = i; /* Save the position of the last character
3065 for highlighting the field. */
3075 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
3077 cbs_t *info = criterion;
3078 const guint8 *binary_data = info->data;
3079 size_t datalen = info->data_len;
3080 match_result result;
3085 /* Load the frame's data. */
3086 if (!cf_read_frame(cf, fdata)) {
3087 /* Attempt to get the packet failed. */
3091 result = MR_NOTMATCHED;
3092 buf_len = fdata->pkt_len;
3093 for (i = 0; i < buf_len; i++) {
3094 if (cf->pd[i] == binary_data[c_match]) {
3096 if (c_match == datalen) {
3097 result = MR_MATCHED;
3098 cf->search_pos = i; /* Save the position of the last character
3099 for highlighting the field. */
3109 cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode,
3110 search_direction dir)
3112 return find_packet(cf, match_dfilter, sfcode, dir);
3116 cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
3117 search_direction dir)
3122 if (!dfilter_compile(filter, &sfcode)) {
3124 * XXX - this shouldn't happen, as the filter string is machine
3129 if (sfcode == NULL) {
3131 * XXX - this shouldn't happen, as the filter string is machine
3136 result = find_packet(cf, match_dfilter, sfcode, dir);
3137 dfilter_free(sfcode);
3142 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
3144 dfilter_t *sfcode = criterion;
3146 match_result result;
3148 /* Load the frame's data. */
3149 if (!cf_read_frame(cf, fdata)) {
3150 /* Attempt to get the packet failed. */
3154 epan_dissect_init(&edt, TRUE, FALSE);
3155 epan_dissect_prime_dfilter(&edt, sfcode);
3156 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
3157 result = dfilter_apply_edt(sfcode, &edt) ? MR_MATCHED : MR_NOTMATCHED;
3158 epan_dissect_cleanup(&edt);
3163 cf_find_packet_marked(capture_file *cf, search_direction dir)
3165 return find_packet(cf, match_marked, NULL, dir);
3169 match_marked(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3171 return fdata->flags.marked ? MR_MATCHED : MR_NOTMATCHED;
3175 cf_find_packet_time_reference(capture_file *cf, search_direction dir)
3177 return find_packet(cf, match_time_reference, NULL, dir);
3181 match_time_reference(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3183 return fdata->flags.ref_time ? MR_MATCHED : MR_NOTMATCHED;
3187 find_packet(capture_file *cf,
3188 match_result (*match_function)(capture_file *, frame_data *, void *),
3189 void *criterion, search_direction dir)
3191 frame_data *start_fd;
3193 frame_data *new_fd = NULL;
3194 progdlg_t *progbar = NULL;
3199 GTimeVal start_time;
3200 gchar status_str[100];
3201 int progbar_nextstep;
3202 int progbar_quantum;
3204 match_result result;
3206 start_fd = cf->current_frame;
3207 if (start_fd != NULL) {
3208 /* Iterate through the list of packets, starting at the packet we've
3209 picked, calling a routine to run the filter on the packet, see if
3210 it matches, and stop if so. */
3214 /* Update the progress bar when it gets to this value. */
3215 progbar_nextstep = 0;
3216 /* When we reach the value that triggers a progress bar update,
3217 bump that value by this amount. */
3218 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
3219 /* Progress so far. */
3223 g_get_current_time(&start_time);
3225 title = cf->sfilter?cf->sfilter:"";
3227 /* Create the progress bar if necessary.
3228 We check on every iteration of the loop, so that it takes no
3229 longer than the standard time to create it (otherwise, for a
3230 large file, we might take considerably longer than that standard
3231 time in order to get to the next progress bar step). */
3232 if (progbar == NULL)
3233 progbar = delayed_create_progress_dlg("Searching", title,
3234 FALSE, &stop_flag, &start_time, progbar_val);
3236 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
3237 when we update it, we have to run the GTK+ main loop to get it
3238 to repaint what's pending, and doing so may involve an "ioctl()"
3239 to see if there's any pending input from an X server, and doing
3240 that for every packet can be costly, especially on a big file. */
3241 if (count >= progbar_nextstep) {
3242 /* let's not divide by zero. I should never be started
3243 * with count == 0, so let's assert that
3245 g_assert(cf->count > 0);
3247 progbar_val = (gfloat) count / cf->count;
3249 if (progbar != NULL) {
3250 g_snprintf(status_str, sizeof(status_str),
3251 "%4u of %u packets", count, cf->count);
3252 update_progress_dlg(progbar, progbar_val, status_str);
3255 progbar_nextstep += progbar_quantum;
3259 /* Well, the user decided to abort the search. Go back to the
3260 frame where we started. */
3265 /* Go past the current frame. */
3266 if (dir == SD_BACKWARD) {
3267 /* Go on to the previous frame. */
3268 fdata = fdata->prev;
3269 if (fdata == NULL) {
3271 * XXX - other apps have a bit more of a detailed message
3272 * for this, and instead of offering "OK" and "Cancel",
3273 * they offer things such as "Continue" and "Cancel";
3274 * we need an API for popping up alert boxes with
3275 * {Verb} and "Cancel".
3278 if (prefs.gui_find_wrap)
3280 statusbar_push_temporary_msg("Search reached the beginning. Continuing at end.");
3281 fdata = cf->plist_end; /* wrap around */
3285 statusbar_push_temporary_msg("Search reached the beginning.");
3286 fdata = start_fd; /* stay on previous packet */
3290 /* Go on to the next frame. */
3291 fdata = fdata->next;
3292 if (fdata == NULL) {
3293 if (prefs.gui_find_wrap)
3295 statusbar_push_temporary_msg("Search reached the end. Continuing at beginning.");
3296 fdata = cf->plist_start; /* wrap around */
3300 statusbar_push_temporary_msg("Search reached the end.");
3301 fdata = start_fd; /* stay on previous packet */
3308 /* Is this packet in the display? */
3309 if (fdata->flags.passed_dfilter) {
3310 /* Yes. Does it match the search criterion? */
3311 result = (*match_function)(cf, fdata, criterion);
3312 if (result == MR_ERROR) {
3313 /* Error; our caller has reported the error. Go back to the frame
3314 where we started. */
3317 } else if (result == MR_MATCHED) {
3318 /* Yes. Go to the new frame. */
3324 if (fdata == start_fd) {
3325 /* We're back to the frame we were on originally, and that frame
3326 doesn't match the search filter. The search failed. */
3331 /* We're done scanning the packets; destroy the progress bar if it
3333 if (progbar != NULL)
3334 destroy_progress_dlg(progbar);
3337 if (new_fd != NULL) {
3338 /* Find and select */
3339 cf->search_in_progress = TRUE;
3340 row = new_packet_list_find_row_from_data(fdata, TRUE);
3341 cf->search_in_progress = FALSE;
3342 cf->search_pos = 0; /* Reset the position */
3344 /* We didn't find a row even though we know that a frame
3345 * exists that satifies the search criteria. This means that the
3346 * frame isn't being displayed currently so we can't select it. */
3347 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
3348 "%sEnd of capture exceeded!%s\n\n"
3349 "The capture file is probably not fully loaded.",
3350 simple_dialog_primary_start(), simple_dialog_primary_end());
3353 return TRUE; /* success */
3355 return FALSE; /* failure */
3359 cf_goto_frame(capture_file *cf, guint fnumber)
3363 for (fdata = cf->plist_start; fdata != NULL && fdata->num < fnumber; fdata = fdata->next)
3366 if (fdata == NULL) {
3367 /* we didn't find a packet with that packet number */
3368 statusbar_push_temporary_msg("There is no packet number %u.", fnumber);
3369 return FALSE; /* we failed to go to that packet */
3371 if (!fdata->flags.passed_dfilter) {
3372 /* that packet currently isn't displayed */
3373 /* XXX - add it to the set of displayed packets? */
3374 statusbar_push_temporary_msg("Packet number %u isn't displayed.", fnumber);
3375 return FALSE; /* we failed to go to that packet */
3378 new_packet_list_find_row_from_data(fdata, TRUE);
3379 return TRUE; /* we got to that packet */
3385 /* Find and select */
3386 new_packet_list_select_first_row();
3387 return TRUE; /* we got to that packet */
3391 cf_goto_bottom_frame()
3393 /* Find and select */
3394 new_packet_list_select_last_row();
3395 return TRUE; /* we got to that packet */
3399 * Go to frame specified by currently selected protocol tree item.
3402 cf_goto_framenum(capture_file *cf)
3404 header_field_info *hfinfo;
3407 if (cf->finfo_selected) {
3408 hfinfo = cf->finfo_selected->hfinfo;
3410 if (hfinfo->type == FT_FRAMENUM) {
3411 framenum = fvalue_get_uinteger(&cf->finfo_selected->value);
3413 return cf_goto_frame(cf, framenum);
3420 /* Select the packet on a given row. */
3422 cf_select_packet(capture_file *cf, int row)
3426 /* Get the frame data struct pointer for this frame */
3427 fdata = new_packet_list_get_row_data(row);
3429 if (fdata == NULL) {
3430 /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
3431 the first entry is added to it by "real_insert_row()", that row
3432 is selected (see "real_insert_row()", in "gtk/gtkclist.c", in both
3433 our version and the vanilla GTK+ version).
3435 This means that a "select-row" signal is emitted; this causes
3436 "packet_list_select_cb()" to be called, which causes "cf_select_packet()"
3439 "cf_select_packet()" fetches, above, the data associated with the
3440 row that was selected; however, as "gtk_clist_append()", which
3441 called "real_insert_row()", hasn't yet returned, we haven't yet
3442 associated any data with that row, so we get back a null pointer.
3444 We can't assume that there's only one frame in the frame list,
3445 either, as we may be filtering the display.
3447 We therefore assume that, if "row" is 0, i.e. the first row
3448 is being selected, and "cf->first_displayed" equals
3449 "cf->last_displayed", i.e. there's only one frame being
3450 displayed, that frame is the frame we want.
3452 This means we have to set "cf->first_displayed" and
3453 "cf->last_displayed" before adding the row to the
3454 GtkCList; see the comment in "add_packet_to_packet_list()". */
3456 if (row == 0 && cf->first_displayed == cf->last_displayed)
3457 fdata = cf->first_displayed;
3460 /* If fdata _still_ isn't set simply give up. */
3461 if (fdata == NULL) {
3465 /* Get the data in that frame. */
3466 if (!cf_read_frame (cf, fdata)) {
3470 /* Record that this frame is the current frame. */
3471 cf->current_frame = fdata;
3472 cf->current_row = row;
3474 /* Create the logical protocol tree. */
3475 if (cf->edt != NULL)
3476 epan_dissect_free(cf->edt);
3478 /* We don't need the columns here. */
3479 cf->edt = epan_dissect_new(TRUE, TRUE);
3481 tap_build_interesting(cf->edt);
3482 epan_dissect_run(cf->edt, &cf->pseudo_header, cf->pd, cf->current_frame,
3485 dfilter_macro_build_ftv_cache(cf->edt->tree);
3487 cf_callback_invoke(cf_cb_packet_selected, cf);
3490 /* Unselect the selected packet, if any. */
3492 cf_unselect_packet(capture_file *cf)
3494 /* Destroy the epan_dissect_t for the unselected packet. */
3495 if (cf->edt != NULL) {
3496 epan_dissect_free(cf->edt);
3500 /* No packet is selected. */
3501 cf->current_frame = NULL;
3502 cf->current_row = 0;
3504 cf_callback_invoke(cf_cb_packet_unselected, cf);
3506 /* No protocol tree means no selected field. */
3507 cf_unselect_field(cf);
3510 /* Unset the selected protocol tree field, if any. */
3512 cf_unselect_field(capture_file *cf)
3514 cf->finfo_selected = NULL;
3516 cf_callback_invoke(cf_cb_field_unselected, cf);
3520 * Mark a particular frame.
3523 cf_mark_frame(capture_file *cf, frame_data *frame)
3525 if (! frame->flags.marked) {
3526 frame->flags.marked = TRUE;
3527 if (cf->count > cf->marked_count)
3533 * Unmark a particular frame.
3536 cf_unmark_frame(capture_file *cf, frame_data *frame)
3538 if (frame->flags.marked) {
3539 frame->flags.marked = FALSE;
3540 if (cf->marked_count > 0)
3546 * Ignore a particular frame.
3549 cf_ignore_frame(capture_file *cf, frame_data *frame)
3551 if (! frame->flags.ignored) {
3552 frame->flags.ignored = TRUE;
3553 if (cf->count > cf->ignored_count)
3554 cf->ignored_count++;
3559 * Un-ignore a particular frame.
3562 cf_unignore_frame(capture_file *cf, frame_data *frame)
3564 if (frame->flags.ignored) {
3565 frame->flags.ignored = FALSE;
3566 if (cf->ignored_count > 0)
3567 cf->ignored_count--;
3574 } save_callback_args_t;
3577 * Save a capture to a file, in a particular format, saving either
3578 * all packets, all currently-displayed packets, or all marked packets.
3580 * Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
3581 * up a message box for the failure.
3584 save_packet(capture_file *cf _U_, frame_data *fdata,
3585 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
3588 save_callback_args_t *args = argsp;
3589 struct wtap_pkthdr hdr;
3592 /* init the wtap header for saving */
3593 hdr.ts.secs = fdata->abs_ts.secs;
3594 hdr.ts.nsecs = fdata->abs_ts.nsecs;
3595 hdr.caplen = fdata->cap_len;
3596 hdr.len = fdata->pkt_len;
3597 hdr.pkt_encap = fdata->lnk_t;
3599 /* and save the packet */
3600 if (!wtap_dump(args->pdh, &hdr, pseudo_header, pd, &err)) {
3601 cf_write_failure_alert_box(args->fname, err);
3608 * Can this capture file be saved in any format except by copying the raw data?
3611 cf_can_save_as(capture_file *cf)
3615 for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) {
3616 /* To save a file with Wiretap, Wiretap has to handle that format,
3617 and its code to handle that format must be able to write a file
3618 with this file's encapsulation type. */
3619 if (wtap_dump_can_open(ft) && wtap_dump_can_write_encap(ft, cf->lnk_t)) {
3620 /* OK, we can write it out in this type. */
3625 /* No, we couldn't save it in any format. */
3630 cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_format, gboolean compressed)
3632 gchar *from_filename;
3636 save_callback_args_t callback_args;
3638 cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
3640 /* don't write over an existing file. */
3641 /* this should've been already checked by our caller, just to be sure... */
3642 if (file_exists(fname)) {
3643 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3644 "%sCapture file: \"%s\" already exists!%s\n\n"
3645 "Please choose a different filename.",
3646 simple_dialog_primary_start(), fname, simple_dialog_primary_end());
3650 packet_range_process_init(range);
3652 if (packet_range_process_all(range) && save_format == cf->cd_t) {
3653 /* We're not filtering packets, and we're saving it in the format
3654 it's already in, so we can just move or copy the raw data. */
3656 if (cf->is_tempfile) {
3657 /* The file being saved is a temporary file from a live
3658 capture, so it doesn't need to stay around under that name;
3659 first, try renaming the capture buffer file to the new name. */
3661 if (ws_rename(cf->filename, fname) == 0) {
3662 /* That succeeded - there's no need to copy the source file. */
3663 from_filename = NULL;
3666 if (errno == EXDEV) {
3667 /* They're on different file systems, so we have to copy the
3670 from_filename = cf->filename;
3672 /* The rename failed, but not because they're on different
3673 file systems - put up an error message. (Or should we
3674 just punt and try to copy? The only reason why I'd
3675 expect the rename to fail and the copy to succeed would
3676 be if we didn't have permission to remove the file from
3677 the temporary directory, and that might be fixable - but
3678 is it worth requiring the user to go off and fix it?) */
3679 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3680 file_rename_error_message(errno), fname);
3686 from_filename = cf->filename;
3689 /* It's a permanent file, so we should copy it, and not remove the
3692 from_filename = cf->filename;
3696 /* Copy the file, if we haven't moved it. */
3697 if (!copy_file_binary_mode(from_filename, fname))
3701 /* Either we're filtering packets, or we're saving in a different
3702 format; we can't do that by copying or moving the capture file,
3703 we have to do it by writing the packets out in Wiretap. */
3704 pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap,
3707 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
3711 /* XXX - we let the user save a subset of the packets.
3713 If we do that, should we make that file the current file? If so,
3714 it means we can no longer get at the other packets. What does
3717 /* Iterate through the list of packets, processing the packets we were
3720 XXX - we've already called "packet_range_process_init(range)", but
3721 "process_specified_packets()" will do it again. Fortunately,
3722 that's harmless in this case, as we haven't done anything to
3723 "range" since we initialized it. */
3724 callback_args.pdh = pdh;
3725 callback_args.fname = fname;
3726 switch (process_specified_packets(cf, range, "Saving", "selected packets",
3727 TRUE, save_packet, &callback_args)) {
3730 /* Completed successfully. */
3734 /* The user decided to abort the saving.
3735 XXX - remove the output file? */
3739 /* Error while saving. */
3740 wtap_dump_close(pdh, &err);
3744 if (!wtap_dump_close(pdh, &err)) {
3745 cf_close_failure_alert_box(fname, err);
3750 cf_callback_invoke(cf_cb_file_save_finished, NULL);
3752 if (packet_range_process_all(range)) {
3753 /* We saved the entire capture, not just some packets from it.
3754 Open and read the file we saved it to.
3756 XXX - this is somewhat of a waste; we already have the
3757 packets, all this gets us is updated file type information
3758 (which we could just stuff into "cf"), and having the new
3759 file be the one we have opened and from which we're reading
3760 the data, and it means we have to spend time opening and
3761 reading the file, which could be a significant amount of
3762 time if the file is large. */
3763 cf->user_saved = TRUE;
3765 if ((cf_open(cf, fname, FALSE, &err)) == CF_OK) {
3766 /* XXX - report errors if this fails?
3767 What should we return if it fails or is aborted? */
3769 switch (cf_read(cf, TRUE)) {
3773 /* Just because we got an error, that doesn't mean we were unable
3774 to read any of the file; we handle what we could get from the
3778 case CF_READ_ABORTED:
3779 /* The user bailed out of re-reading the capture file; the
3780 capture file has been closed - just return (without
3781 changing any menu settings; "cf_close()" set them
3782 correctly for the "no capture file open" state). */
3785 cf_callback_invoke(cf_cb_file_save_reload_finished, cf);
3791 cf_callback_invoke(cf_cb_file_save_failed, NULL);
3796 cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
3797 gboolean for_writing, int file_type)
3800 /* Wiretap error. */
3803 case WTAP_ERR_NOT_REGULAR_FILE:
3804 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3805 "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
3809 case WTAP_ERR_RANDOM_OPEN_PIPE:
3810 /* Seen only when opening a capture file for reading. */
3811 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3812 "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.",
3816 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
3817 /* Seen only when opening a capture file for reading. */
3818 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3819 "The file \"%s\" isn't a capture file in a format Wireshark understands.",
3823 case WTAP_ERR_UNSUPPORTED:
3824 /* Seen only when opening a capture file for reading. */
3825 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3826 "The file \"%s\" isn't a capture file in a format Wireshark understands.\n"
3828 filename, err_info);
3832 case WTAP_ERR_CANT_WRITE_TO_PIPE:
3833 /* Seen only when opening a capture file for writing. */
3834 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3835 "The file \"%s\" is a pipe, and %s capture files can't be "
3836 "written to a pipe.",
3837 filename, wtap_file_type_string(file_type));
3840 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
3841 /* Seen only when opening a capture file for writing. */
3842 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3843 "Wireshark doesn't support writing capture files in that format.");
3846 case WTAP_ERR_UNSUPPORTED_ENCAP:
3848 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3849 "Wireshark can't save this capture in that format.");
3851 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3852 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.\n"
3854 filename, err_info);
3859 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
3861 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3862 "Wireshark can't save this capture in that format.");
3864 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3865 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.",
3870 case WTAP_ERR_BAD_RECORD:
3871 /* Seen only when opening a capture file for reading. */
3872 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3873 "The file \"%s\" appears to be damaged or corrupt.\n"
3875 filename, err_info);
3879 case WTAP_ERR_CANT_OPEN:
3881 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3882 "The file \"%s\" could not be created for some unknown reason.",
3885 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3886 "The file \"%s\" could not be opened for some unknown reason.",
3891 case WTAP_ERR_SHORT_READ:
3892 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3893 "The file \"%s\" appears to have been cut short"
3894 " in the middle of a packet or other data.",
3898 case WTAP_ERR_SHORT_WRITE:
3899 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3900 "A full header couldn't be written to the file \"%s\".",
3904 case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
3905 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3906 "Gzip compression not supported by this file type.");
3910 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3911 "The file \"%s\" could not be %s: %s.",
3913 for_writing ? "created" : "opened",
3914 wtap_strerror(err));
3919 open_failure_alert_box(filename, err, for_writing);
3924 file_rename_error_message(int err)
3927 static char errmsg_errno[1024+1];
3932 errmsg = "The path to the file \"%s\" doesn't exist.";
3936 errmsg = "You don't have permission to move the capture file to \"%s\".";
3940 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
3941 "The file \"%%s\" could not be moved: %s.",
3942 wtap_strerror(err));
3943 errmsg = errmsg_errno;
3950 cf_write_failure_alert_box(const char *filename, int err)
3953 /* Wiretap error. */
3954 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3955 "An error occurred while writing to the file \"%s\": %s.",
3956 filename, wtap_strerror(err));
3959 write_failure_alert_box(filename, err);
3963 /* Check for write errors - if the file is being written to an NFS server,
3964 a write error may not show up until the file is closed, as NFS clients
3965 might not send writes to the server until the "write()" call finishes,
3966 so that the write may fail on the server but the "write()" may succeed. */
3968 cf_close_failure_alert_box(const char *filename, int err)
3971 /* Wiretap error. */
3974 case WTAP_ERR_CANT_CLOSE:
3975 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3976 "The file \"%s\" couldn't be closed for some unknown reason.",
3980 case WTAP_ERR_SHORT_WRITE:
3981 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3982 "Not all the packets could be written to the file \"%s\".",
3987 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3988 "An error occurred while closing the file \"%s\": %s.",
3989 filename, wtap_strerror(err));
3994 We assume that a close error from the OS is really a write error. */
3995 write_failure_alert_box(filename, err);
3999 /* Reload the current capture file. */
4001 cf_reload(capture_file *cf) {
4003 gboolean is_tempfile;
4006 /* If the file could be opened, "cf_open()" calls "cf_close()"
4007 to get rid of state for the old capture file before filling in state
4008 for the new capture file. "cf_close()" will remove the file if
4009 it's a temporary file; we don't want that to happen (for one thing,
4010 it'd prevent subsequent reopens from working). Remember whether it's
4011 a temporary file, mark it as not being a temporary file, and then
4012 reopen it as the type of file it was.
4014 Also, "cf_close()" will free "cf->filename", so we must make
4015 a copy of it first. */
4016 filename = g_strdup(cf->filename);
4017 is_tempfile = cf->is_tempfile;
4018 cf->is_tempfile = FALSE;
4019 if (cf_open(cf, filename, is_tempfile, &err) == CF_OK) {
4020 switch (cf_read(cf, FALSE)) {
4024 /* Just because we got an error, that doesn't mean we were unable
4025 to read any of the file; we handle what we could get from the
4029 case CF_READ_ABORTED:
4030 /* The user bailed out of re-reading the capture file; the
4031 capture file has been closed - just free the capture file name
4032 string and return (without changing the last containing
4038 /* The open failed, so "cf->is_tempfile" wasn't set to "is_tempfile".
4039 Instead, the file was left open, so we should restore "cf->is_tempfile"
4042 XXX - change the menu? Presumably "cf_open()" will do that;
4043 make sure it does! */
4044 cf->is_tempfile = is_tempfile;
4046 /* "cf_open()" made a copy of the file name we handed it, so
4047 we should free up our copy. */
4057 * indent-tabs-mode: nil
4060 * ex: set shiftwidth=2 tabstop=8 expandtab
4061 * :indentSize=2:tabSize=8:noTabs=true: