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
142 /* this callback mechanism should possibly be replaced by the g_signal_...() stuff (if I only would know how :-) */
144 cf_callback_t cb_fct;
146 } cf_callback_data_t;
148 static GList *cf_callbacks = NULL;
151 cf_callback_invoke(int event, gpointer data)
153 cf_callback_data_t *cb;
154 GList *cb_item = cf_callbacks;
156 /* there should be at least one interested */
157 g_assert(cb_item != NULL);
159 while(cb_item != NULL) {
161 cb->cb_fct(event, data, cb->user_data);
162 cb_item = g_list_next(cb_item);
168 cf_callback_add(cf_callback_t func, gpointer user_data)
170 cf_callback_data_t *cb;
172 cb = g_malloc(sizeof(cf_callback_data_t));
174 cb->user_data = user_data;
176 cf_callbacks = g_list_append(cf_callbacks, cb);
180 cf_callback_remove(cf_callback_t func)
182 cf_callback_data_t *cb;
183 GList *cb_item = cf_callbacks;
185 while(cb_item != NULL) {
187 if(cb->cb_fct == func) {
188 cf_callbacks = g_list_remove(cf_callbacks, cb);
192 cb_item = g_list_next(cb_item);
195 g_assert_not_reached();
199 cf_timestamp_auto_precision(capture_file *cf)
202 int prec = timestamp_get_precision();
205 /* don't try to get the file's precision if none is opened */
206 if(cf->state == FILE_CLOSED) {
210 /* if we are in auto mode, set precision of current file */
211 if(prec == TS_PREC_AUTO ||
212 prec == TS_PREC_AUTO_SEC ||
213 prec == TS_PREC_AUTO_DSEC ||
214 prec == TS_PREC_AUTO_CSEC ||
215 prec == TS_PREC_AUTO_MSEC ||
216 prec == TS_PREC_AUTO_USEC ||
217 prec == TS_PREC_AUTO_NSEC)
219 switch(wtap_file_tsprecision(cf->wth)) {
220 case(WTAP_FILE_TSPREC_SEC):
221 timestamp_set_precision(TS_PREC_AUTO_SEC);
223 case(WTAP_FILE_TSPREC_DSEC):
224 timestamp_set_precision(TS_PREC_AUTO_DSEC);
226 case(WTAP_FILE_TSPREC_CSEC):
227 timestamp_set_precision(TS_PREC_AUTO_CSEC);
229 case(WTAP_FILE_TSPREC_MSEC):
230 timestamp_set_precision(TS_PREC_AUTO_MSEC);
232 case(WTAP_FILE_TSPREC_USEC):
233 timestamp_set_precision(TS_PREC_AUTO_USEC);
235 case(WTAP_FILE_TSPREC_NSEC):
236 timestamp_set_precision(TS_PREC_AUTO_NSEC);
239 g_assert_not_reached();
242 /* Set the column widths of those columns that show the time in
243 "command-line-specified" format. */
244 for (i = 0; i < cf->cinfo.num_cols; i++) {
245 if (col_has_time_fmt(&cf->cinfo, i)) {
246 new_packet_list_resize_column(i);
252 cf_get_computed_elapsed(void)
254 return computed_elapsed;
257 static void reset_elapsed(void)
259 computed_elapsed = 0;
262 static void compute_elapsed(GTimeVal *start_time)
267 g_get_current_time(&time_now);
269 delta_time = (time_now.tv_sec - start_time->tv_sec) * 1e6 +
270 time_now.tv_usec - start_time->tv_usec;
272 computed_elapsed = (gulong) (delta_time / 1000); /* ms*/
276 cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
281 wth = wtap_open_offline(fname, err, &err_info, TRUE);
285 /* The open succeeded. Close whatever capture file we had open,
286 and fill in the information for this file. */
289 /* Cleanup all data structures used for dissection. */
290 cleanup_dissection();
291 /* Initialize all data structures used for dissection. */
294 /* We're about to start reading the file. */
295 cf->state = FILE_READ_IN_PROGRESS;
300 /* Set the file name because we need it to set the follow stream filter.
301 XXX - is that still true? We need it for other reasons, though,
303 cf->filename = g_strdup(fname);
305 /* Indicate whether it's a permanent or temporary file. */
306 cf->is_tempfile = is_tempfile;
308 /* If it's a temporary capture buffer file, mark it as not saved. */
309 cf->user_saved = !is_tempfile;
313 cf->cd_t = wtap_file_type(cf->wth);
315 cf->displayed_count = 0;
316 cf->marked_count = 0;
317 cf->ignored_count = 0;
318 cf->ref_time_count = 0;
319 cf->drops_known = FALSE;
321 cf->snap = wtap_snapshot_length(cf->wth);
323 /* Snapshot length not known. */
324 cf->has_snap = FALSE;
325 cf->snap = WTAP_MAX_PACKET_SIZE;
329 nstime_set_zero(&cf->elapsed_time);
330 nstime_set_unset(&first_ts);
331 nstime_set_unset(&prev_dis_ts);
332 nstime_set_unset(&prev_cap_ts);
335 #if GLIB_CHECK_VERSION(2,10,0)
337 /* memory chunks have been deprecated in favor of the slice allocator,
338 * which has been added in 2.10
340 cf->plist_chunk = g_mem_chunk_new("frame_data_chunk",
342 FRAME_DATA_CHUNK_SIZE * sizeof(frame_data),
344 g_assert(cf->plist_chunk);
347 /* Adjust timestamp precision if auto is selected, col width will be adjusted */
348 cf_timestamp_auto_precision(cf);
350 new_packet_list_queue_draw();
351 fileset_file_opened(fname);
353 if(cf->cd_t == WTAP_FILE_BER) {
354 /* tell the BER dissector the file name */
355 ber_set_filename(cf->filename);
361 cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
367 * Reset the state for the currently closed file, but don't do the
368 * UI callbacks; this is for use in "cf_open()", where we don't
369 * want the UI to go from "file open" to "file closed" back to
370 * "file open", we want it to go from "old file open" to "new file
371 * open and being read".
374 cf_reset_state(capture_file *cf)
376 /* Die if we're in the middle of reading a file. */
377 g_assert(cf->state != FILE_READ_IN_PROGRESS);
383 /* We have no file open... */
384 if (cf->filename != NULL) {
385 /* If it's a temporary file, remove it. */
387 ws_unlink(cf->filename);
388 g_free(cf->filename);
391 /* ...which means we have nothing to save. */
392 cf->user_saved = FALSE;
394 #if GLIB_CHECK_VERSION(2,10,0)
395 if (cf->plist_start != NULL)
396 g_slice_free_chain(frame_data, cf->plist_start, next);
398 /* memory chunks have been deprecated in favor of the slice allocator,
399 * which has been added in 2.10
401 if (cf->plist_chunk != NULL) {
402 g_mem_chunk_destroy(cf->plist_chunk);
403 cf->plist_chunk = NULL;
406 dfilter_free(cf->rfcode);
408 cf->plist_start = NULL;
409 cf->plist_end = NULL;
410 cf_unselect_packet(cf); /* nothing to select */
411 cf->first_displayed = NULL;
412 cf->last_displayed = NULL;
414 /* No frame selected, no field in that frame selected. */
415 cf->current_frame = NULL;
417 cf->finfo_selected = NULL;
419 /* Clear the packet list. */
420 new_packet_list_freeze();
421 new_packet_list_clear();
422 new_packet_list_thaw();
426 nstime_set_zero(&cf->elapsed_time);
428 reset_tap_listeners();
430 /* We have no file open. */
431 cf->state = FILE_CLOSED;
433 fileset_file_closed();
436 /* Reset everything to a pristine state */
438 cf_close(capture_file *cf)
440 /* do GUI things even if file is already closed,
441 * e.g. to cleanup things if a capture couldn't be started */
442 cf_callback_invoke(cf_cb_file_closing, cf);
444 /* close things, if not already closed before */
445 if(cf->state != FILE_CLOSED) {
446 color_filters_cleanup();
448 cleanup_dissection();
451 cf_callback_invoke(cf_cb_file_closed, cf);
454 /* an out of memory exception occured, wait for a user button press to exit */
455 static void outofmemory_cb(gpointer dialog _U_, gint btn _U_, gpointer data _U_)
461 calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos, gchar *status_str, gulong status_size)
465 progbar_val = (gfloat) file_pos / (gfloat) size;
466 if (progbar_val > 1.0) {
468 /* The file probably grew while we were reading it.
469 * Update file size, and try again.
471 size = wtap_file_size(cf->wth, NULL);
473 /* Another possibility is that we're reading a compressed file and we've
474 * read more (uncompressed) data from the file than exists in the
475 * (compressed) file. So check how much data we've actually read.
477 * This is inside this "if val > 1.0" check to avoid the lseek() when
478 * reading uncompressed files. Testing has (thus far) shown no progress
479 * bar weirdness resulting from switching from the data offset (when
480 * reading the first part of the file) to the real file position.
482 file_pos = wtap_read_so_far(cf->wth, NULL);
485 progbar_val = (gfloat) file_pos / (gfloat) size;
487 /* If it's still > 1, either "wtap_file_size()" failed (in which
488 * case there's not much we can do about it), or the file
489 * *shrank* (in which case there's not much we can do about
490 * it); just clip the progress value at 1.0.
492 if (progbar_val > 1.0f)
496 g_snprintf(status_str, status_size,
497 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
498 file_pos / 1024, size / 1024);
504 cf_read(capture_file *cf, gboolean from_save)
508 const gchar *name_ptr;
510 char errmsg_errno[1024+1];
512 progdlg_t *volatile progbar = NULL;
514 volatile gint64 size;
515 volatile float progbar_val;
517 gchar status_str[100];
518 volatile gint64 progbar_nextstep;
519 volatile gint64 progbar_quantum;
521 gboolean filtering_tap_listeners;
523 volatile int count = 0;
525 volatile int displayed_once = 0;
529 /* Compile the current display filter.
530 * We assume this will not fail since cf->dfilter is only set in
531 * cf_filter IFF the filter was valid.
533 compiled = dfilter_compile(cf->dfilter, &dfcode);
534 g_assert(!cf->dfilter || (compiled && dfcode));
536 /* Do we have any tap listeners with filters? */
537 filtering_tap_listeners = have_filtering_tap_listeners();
539 /* Get the union of the flags for all tap listeners. */
540 tap_flags = union_of_tap_listener_flags();
542 reset_tap_listeners();
544 name_ptr = get_basename(cf->filename);
546 if (from_save == FALSE)
547 cf_callback_invoke(cf_cb_file_read_started, cf);
549 cf_callback_invoke(cf_cb_file_save_started, (gpointer)name_ptr);
551 /* Find the size of the file. */
552 size = wtap_file_size(cf->wth, NULL);
554 /* Update the progress bar when it gets to this value. */
555 progbar_nextstep = 0;
556 /* When we reach the value that triggers a progress bar update,
557 bump that value by this amount. */
559 progbar_quantum = size/N_PROGBAR_UPDATES;
560 if (progbar_quantum < MIN_QUANTUM)
561 progbar_quantum = MIN_QUANTUM;
564 /* Progress so far. */
567 new_packet_list_freeze();
570 g_get_current_time(&start_time);
572 while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
575 /* Create the progress bar if necessary.
576 * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
578 if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)){
579 progbar_val = calc_progbar_val(cf, size, data_offset, status_str, sizeof(status_str));
580 if (from_save == FALSE)
581 progbar = delayed_create_progress_dlg("Loading", name_ptr,
582 TRUE, &stop_flag, &start_time, progbar_val);
584 progbar = delayed_create_progress_dlg("Saving", name_ptr,
585 TRUE, &stop_flag, &start_time, progbar_val);
588 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
589 when we update it, we have to run the GTK+ main loop to get it
590 to repaint what's pending, and doing so may involve an "ioctl()"
591 to see if there's any pending input from an X server, and doing
592 that for every packet can be costly, especially on a big file. */
593 if (data_offset >= progbar_nextstep) {
594 if (progbar != NULL) {
595 progbar_val = calc_progbar_val(cf, size, data_offset, status_str, sizeof(status_str));
596 /* update the packet lists content on the first run or frequently on very large files */
597 /* (on smaller files the display update takes longer than reading the file) */
599 if (progbar_quantum > 500000 || displayed_once == 0) {
600 if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->plist_end != NULL) {
602 new_packet_list_thaw();
603 if (auto_scroll_live)
604 new_packet_list_moveto_end();
605 new_packet_list_freeze();
608 #endif /* HAVE_LIBPCAP */
609 update_progress_dlg(progbar, progbar_val, status_str);
611 progbar_nextstep += progbar_quantum;
616 /* Well, the user decided to abort the read. He/She will be warned and
617 it might be enough for him/her to work with the already loaded
619 This is especially true for very large capture files, where you don't
620 want to wait loading the whole file (which may last minutes or even
621 hours even on fast machines) just to see that it was the wrong file. */
625 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
627 CATCH(OutOfMemoryError) {
630 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
631 "%sOut Of Memory!%s\n"
633 "Sorry, but Wireshark has to terminate now!\n"
635 "Some infos / workarounds can be found at:\n"
636 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
637 simple_dialog_primary_start(), simple_dialog_primary_end());
638 /* we have to terminate, as we cannot recover from the memory error */
639 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
641 main_window_update();
642 /* XXX - how to avoid a busy wait? */
650 /* Cleanup and release all dfilter resources */
652 dfilter_free(dfcode);
655 /* We're done reading the file; destroy the progress bar if it was created. */
657 destroy_progress_dlg(progbar);
659 /* We're done reading sequentially through the file. */
660 cf->state = FILE_READ_DONE;
662 /* Close the sequential I/O side, to free up memory it requires. */
663 wtap_sequential_close(cf->wth);
665 /* Allow the protocol dissectors to free up memory that they
666 * don't need after the sequential run-through of the packets. */
667 postseq_cleanup_all_protocols();
669 /* compute the time it took to load the file */
670 compute_elapsed(&start_time);
672 /* Set the file encapsulation type now; we don't know what it is until
673 we've looked at all the packets, as we don't know until then whether
674 there's more than one type (and thus whether it's
675 WTAP_ENCAP_PER_PACKET). */
676 cf->lnk_t = wtap_file_encap(cf->wth);
678 cf->current_frame = cf->first_displayed;
681 new_packet_list_thaw();
682 if (from_save == FALSE)
683 cf_callback_invoke(cf_cb_file_read_finished, cf);
685 cf_callback_invoke(cf_cb_file_save_finished, cf);
687 /* If we have any displayed packets to select, select the first of those
688 packets by making the first row the selected row. */
689 if (cf->first_displayed != NULL){
690 new_packet_list_select_first_row();
694 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
695 "%sFile loading was cancelled!%s\n"
697 "The remaining packets in the file were discarded.\n"
699 "As a lot of packets from the original file will be missing,\n"
700 "remember to be careful when saving the current content to a file.\n",
701 simple_dialog_primary_start(), simple_dialog_primary_end());
702 return CF_READ_ERROR;
706 /* Put up a message box noting that the read failed somewhere along
707 the line. Don't throw out the stuff we managed to read, though,
711 case WTAP_ERR_UNSUPPORTED_ENCAP:
712 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
713 "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
716 errmsg = errmsg_errno;
719 case WTAP_ERR_CANT_READ:
720 errmsg = "An attempt to read from the capture file failed for"
721 " some unknown reason.";
724 case WTAP_ERR_SHORT_READ:
725 errmsg = "The capture file appears to have been cut short"
726 " in the middle of a packet.";
729 case WTAP_ERR_BAD_RECORD:
730 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
731 "The capture file appears to be damaged or corrupt.\n(%s)",
734 errmsg = errmsg_errno;
738 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
739 "An error occurred while reading the"
740 " capture file: %s.", wtap_strerror(err));
741 errmsg = errmsg_errno;
744 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", errmsg);
745 return CF_READ_ERROR;
752 cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
754 cf_status_t cf_status;
756 cf_status = cf_open(cf, fname, is_tempfile, err);
761 cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
763 gint64 data_offset = 0;
765 volatile int newly_displayed_packets = 0;
767 gboolean filtering_tap_listeners;
771 /* Compile the current display filter.
772 * We assume this will not fail since cf->dfilter is only set in
773 * cf_filter IFF the filter was valid.
775 compiled = dfilter_compile(cf->dfilter, &dfcode);
776 g_assert(!cf->dfilter || (compiled && dfcode));
778 /* Do we have any tap listeners with filters? */
779 filtering_tap_listeners = have_filtering_tap_listeners();
781 /* Get the union of the flags for all tap listeners. */
782 tap_flags = union_of_tap_listener_flags();
786 new_packet_list_check_end();
787 /* Don't freeze/thaw the list when doing live capture */
788 /*new_packet_list_freeze();*/
790 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
792 while (to_read != 0) {
793 wtap_cleareof(cf->wth);
794 if (!wtap_read(cf->wth, err, &err_info, &data_offset)) {
797 if (cf->state == FILE_READ_ABORTED) {
798 /* Well, the user decided to exit Wireshark. Break out of the
799 loop, and let the code below (which is called even if there
800 aren't any packets left to read) exit. */
804 if (read_packet(cf, dfcode, filtering_tap_listeners, tap_flags,
805 data_offset) != -1) {
806 newly_displayed_packets++;
809 CATCH(OutOfMemoryError) {
812 dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
813 "%sOut Of Memory!%s\n"
815 "Sorry, but Wireshark has to terminate now!\n"
817 "The capture file is not lost, it can be found at:\n"
820 "Some infos / workarounds can be found at:\n"
821 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
822 simple_dialog_primary_start(), simple_dialog_primary_end(), cf->filename);
823 /* we have to terminate, as we cannot recover from the memory error */
824 simple_dialog_set_cb(dialog, outofmemory_cb, NULL);
826 main_window_update();
827 /* XXX - how to avoid a busy wait? */
830 /* Don't freeze/thaw the list when doing live capture */
831 /*new_packet_list_thaw();*/
832 return CF_READ_ABORTED;
838 /* Cleanup and release all dfilter resources */
840 dfilter_free(dfcode);
843 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u",
844 cf->count, cf->state, *err);*/
846 /* Don't freeze/thaw the list when doing live capture */
847 /*new_packet_list_thaw();*/
848 /* With the new packet list the first packet
849 * isn't automatically selected.
851 if(!cf->current_frame)
852 new_packet_list_select_first_row();
854 /* moving to the end of the packet list - if the user requested so and
855 we have some new packets. */
856 if (newly_displayed_packets && auto_scroll_live && cf->plist_end != NULL)
857 new_packet_list_moveto_end();
859 if (cf->state == FILE_READ_ABORTED) {
860 /* Well, the user decided to exit Wireshark. Return CF_READ_ABORTED
861 so that our caller can kill off the capture child process;
862 this will cause an EOF on the pipe from the child, so
863 "cf_finish_tail()" will be called, and it will clean up
865 return CF_READ_ABORTED;
866 } else if (*err != 0) {
867 /* We got an error reading the capture file.
868 XXX - pop up a dialog box instead? */
869 g_warning("Error \"%s\" while reading: \"%s\"\n",
870 wtap_strerror(*err), cf->filename);
872 return CF_READ_ERROR;
878 cf_fake_continue_tail(capture_file *cf) {
879 cf->state = FILE_READ_DONE;
883 cf_finish_tail(capture_file *cf, int *err)
888 gboolean filtering_tap_listeners;
892 /* Compile the current display filter.
893 * We assume this will not fail since cf->dfilter is only set in
894 * cf_filter IFF the filter was valid.
896 compiled = dfilter_compile(cf->dfilter, &dfcode);
897 g_assert(!cf->dfilter || (compiled && dfcode));
899 /* Do we have any tap listeners with filters? */
900 filtering_tap_listeners = have_filtering_tap_listeners();
902 /* Get the union of the flags for all tap listeners. */
903 tap_flags = union_of_tap_listener_flags();
905 if(cf->wth == NULL) {
907 return CF_READ_ERROR;
910 new_packet_list_check_end();
911 /* Don't freeze/thaw the list when doing live capture */
912 /*new_packet_list_freeze();*/
914 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
915 if (cf->state == FILE_READ_ABORTED) {
916 /* Well, the user decided to abort the read. Break out of the
917 loop, and let the code below (which is called even if there
918 aren't any packets left to read) exit. */
921 read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
924 /* Cleanup and release all dfilter resources */
926 dfilter_free(dfcode);
929 /* Don't freeze/thaw the list when doing live capture */
930 /*new_packet_list_thaw();*/
932 if (cf->state == FILE_READ_ABORTED) {
933 /* Well, the user decided to abort the read. We're only called
934 when the child capture process closes the pipe to us (meaning
935 it's probably exited), so we can just close the capture
936 file; we return CF_READ_ABORTED so our caller can do whatever
937 is appropriate when that happens. */
939 return CF_READ_ABORTED;
942 if (auto_scroll_live && cf->plist_end != NULL)
943 new_packet_list_moveto_end();
945 /* We're done reading sequentially through the file. */
946 cf->state = FILE_READ_DONE;
948 /* We're done reading sequentially through the file; close the
949 sequential I/O side, to free up memory it requires. */
950 wtap_sequential_close(cf->wth);
952 /* Allow the protocol dissectors to free up memory that they
953 * don't need after the sequential run-through of the packets. */
954 postseq_cleanup_all_protocols();
956 /* Set the file encapsulation type now; we don't know what it is until
957 we've looked at all the packets, as we don't know until then whether
958 there's more than one type (and thus whether it's
959 WTAP_ENCAP_PER_PACKET). */
960 cf->lnk_t = wtap_file_encap(cf->wth);
963 /* We got an error reading the capture file.
964 XXX - pop up a dialog box? */
965 return CF_READ_ERROR;
970 #endif /* HAVE_LIBPCAP */
973 cf_get_display_name(capture_file *cf)
975 const gchar *displayname;
977 /* Return a name to use in displays */
978 if (!cf->is_tempfile) {
979 /* Get the last component of the file name, and use that. */
981 displayname = get_basename(cf->filename);
983 displayname="(No file)";
986 /* The file we read is a temporary file from a live capture;
987 we don't mention its name. */
989 displayname = cf->source;
991 displayname = "(Untitled)";
997 void cf_set_tempfile_source(capture_file *cf, gchar *source) {
1003 cf->source = g_strdup(source);
1005 cf->source = g_strdup("");
1009 const gchar *cf_get_tempfile_source(capture_file *cf) {
1017 /* XXX - use a macro instead? */
1019 cf_get_packet_count(capture_file *cf)
1024 /* XXX - use a macro instead? */
1026 cf_set_packet_count(capture_file *cf, int packet_count)
1028 cf->count = packet_count;
1031 /* XXX - use a macro instead? */
1033 cf_is_tempfile(capture_file *cf)
1035 return cf->is_tempfile;
1038 void cf_set_tempfile(capture_file *cf, gboolean is_tempfile)
1040 cf->is_tempfile = is_tempfile;
1044 /* XXX - use a macro instead? */
1045 void cf_set_drops_known(capture_file *cf, gboolean drops_known)
1047 cf->drops_known = drops_known;
1050 /* XXX - use a macro instead? */
1051 void cf_set_drops(capture_file *cf, guint32 drops)
1056 /* XXX - use a macro instead? */
1057 gboolean cf_get_drops_known(capture_file *cf)
1059 return cf->drops_known;
1062 /* XXX - use a macro instead? */
1063 guint32 cf_get_drops(capture_file *cf)
1068 void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
1070 cf->rfcode = rfcode;
1074 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
1075 dfilter_t *dfcode, gboolean filtering_tap_listeners,
1077 union wtap_pseudo_header *pseudo_header, const guchar *buf,
1079 gboolean add_to_packet_list)
1081 gboolean create_proto_tree = FALSE;
1086 cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
1088 frame_data_set_before_dissect(fdata, &cf->elapsed_time,
1089 &first_ts, &prev_dis_ts, &prev_cap_ts);
1092 + we have a display filter and are re-applying it;
1093 + we have tap listeners with filters;
1094 + we have tap listeners that require a protocol tree;
1096 allocate a protocol tree root node, so that we'll construct
1097 a protocol tree against which a filter expression can be
1099 if ((dfcode != NULL && refilter) ||
1100 filtering_tap_listeners || (tap_flags & TL_REQUIRES_PROTO_TREE))
1101 create_proto_tree = TRUE;
1103 /* Dissect the frame. */
1104 epan_dissect_init(&edt, create_proto_tree, FALSE);
1106 if (dfcode != NULL && refilter) {
1107 epan_dissect_prime_dfilter(&edt, dfcode);
1110 tap_queue_init(&edt);
1111 epan_dissect_run(&edt, pseudo_header, buf, fdata, cinfo);
1112 tap_push_tapped_queue(&edt);
1114 /* If we have a display filter, apply it if we're refiltering, otherwise
1115 leave the "passed_dfilter" flag alone.
1117 If we don't have a display filter, set "passed_dfilter" to 1. */
1118 if (dfcode != NULL) {
1120 fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, &edt) ? 1 : 0;
1123 fdata->flags.passed_dfilter = 1;
1125 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1126 cf->displayed_count++;
1128 if (add_to_packet_list) {
1129 /* We fill the needed columns from new_packet_list */
1130 row = new_packet_list_append(cinfo, fdata, &edt.pi);
1133 if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
1135 frame_data_set_after_dissect(fdata, &cum_bytes, &prev_dis_ts);
1137 /* If we haven't yet seen the first frame, this is it.
1139 XXX - we must do this before we add the row to the display,
1140 as, if the display's GtkCList's selection mode is
1141 GTK_SELECTION_BROWSE, when the first entry is added to it,
1142 "cf_select_packet()" will be called, and it will fetch the row
1143 data for the 0th row, and will get a null pointer rather than
1144 "fdata", as "gtk_clist_append()" won't yet have returned and
1145 thus "gtk_clist_set_row_data()" won't yet have been called.
1147 We thus need to leave behind bread crumbs so that
1148 "cf_select_packet()" can find this frame. See the comment
1149 in "cf_select_packet()". */
1150 if (cf->first_displayed == NULL)
1151 cf->first_displayed = fdata;
1153 /* This is the last frame we've seen so far. */
1154 cf->last_displayed = fdata;
1157 epan_dissect_cleanup(&edt);
1162 * Initialize the col_text and col_text_len arrays.
1165 init_col_text(frame_data *fdata, gint num_cols)
1167 fdata->col_text_len = se_alloc0(sizeof(*fdata->col_text_len) * num_cols);
1168 fdata->col_text = se_alloc0(sizeof(*fdata->col_text) * num_cols);
1171 /* read in a new packet */
1172 /* returns the row of the new packet in the packet list or -1 if not displayed */
1174 read_packet(capture_file *cf, dfilter_t *dfcode,
1175 gboolean filtering_tap_listeners, guint tap_flags, gint64 offset)
1177 const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
1178 union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
1179 const guchar *buf = wtap_buf_ptr(cf->wth);
1186 /* Allocate the next list entry, and add it to the list.
1187 * memory chunks have been deprecated in favor of the slice allocator,
1188 * which has been added in 2.10
1190 #if GLIB_CHECK_VERSION(2,10,0)
1191 fdata = g_slice_new(frame_data);
1193 fdata = g_mem_chunk_alloc(cf->plist_chunk);
1196 frame_data_init(fdata, cf->count, phdr, offset, cum_bytes);
1197 init_col_text(fdata, cf->cinfo.num_cols);
1202 epan_dissect_init(&edt, TRUE, FALSE);
1203 epan_dissect_prime_dfilter(&edt, cf->rfcode);
1204 epan_dissect_run(&edt, pseudo_header, buf, fdata, NULL);
1205 passed = dfilter_apply_edt(cf->rfcode, &edt);
1206 epan_dissect_cleanup(&edt);
1210 cap_file_add_fdata(cf, fdata);
1212 cf->f_datalen = offset + fdata->cap_len;
1214 if (!cf->redissecting) {
1215 row = add_packet_to_packet_list(fdata, cf, dfcode,
1216 filtering_tap_listeners, tap_flags,
1217 pseudo_header, buf, TRUE, TRUE);
1220 /* We didn't pass read filter so roll back count */
1223 /* XXX - if we didn't have read filters, or if we could avoid
1224 allocating the "frame_data" structure until we knew whether
1225 the frame passed the read filter, we could use a G_ALLOC_ONLY
1228 ...but, at least in one test I did, where I just made the chunk
1229 a G_ALLOC_ONLY chunk and read in a huge capture file, it didn't
1230 seem to save a noticeable amount of time or space. */
1231 #if GLIB_CHECK_VERSION(2,10,0)
1232 /* memory chunks have been deprecated in favor of the slice allocator,
1233 * which has been added in 2.10
1235 g_slice_free(frame_data,fdata);
1237 g_mem_chunk_free(cf->plist_chunk, fdata);
1245 cf_merge_files(char **out_filenamep, int in_file_count,
1246 char *const *in_filenames, int file_type, gboolean do_append)
1248 merge_in_file_t *in_files;
1254 int open_err, read_err, write_err, close_err;
1258 char errmsg_errno[1024+1];
1260 gboolean got_read_error = FALSE, got_write_error = FALSE;
1262 progdlg_t *progbar = NULL;
1264 gint64 f_len, file_pos;
1266 GTimeVal start_time;
1267 gchar status_str[100];
1268 gint64 progbar_nextstep;
1269 gint64 progbar_quantum;
1271 /* open the input files */
1272 if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
1273 &open_err, &err_info, &err_fileno)) {
1275 cf_open_failure_alert_box(in_filenames[err_fileno], open_err, err_info,
1280 if (*out_filenamep != NULL) {
1281 out_filename = *out_filenamep;
1282 out_fd = ws_open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
1286 out_fd = create_tempfile(&tmpname, "wireshark");
1289 out_filename = g_strdup(tmpname);
1290 *out_filenamep = out_filename;
1294 merge_close_in_files(in_file_count, in_files);
1296 cf_open_failure_alert_box(out_filename, open_err, NULL, TRUE, file_type);
1300 pdh = wtap_dump_fdopen(out_fd, file_type,
1301 merge_select_frame_type(in_file_count, in_files),
1302 merge_max_snapshot_length(in_file_count, in_files),
1303 FALSE /* compressed */, &open_err);
1306 merge_close_in_files(in_file_count, in_files);
1308 cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
1313 /* Get the sum of the sizes of all the files. */
1315 for (i = 0; i < in_file_count; i++)
1316 f_len += in_files[i].size;
1318 /* Update the progress bar when it gets to this value. */
1319 progbar_nextstep = 0;
1320 /* When we reach the value that triggers a progress bar update,
1321 bump that value by this amount. */
1322 progbar_quantum = f_len/N_PROGBAR_UPDATES;
1323 /* Progress so far. */
1327 g_get_current_time(&start_time);
1329 /* do the merge (or append) */
1332 wth = merge_append_read_packet(in_file_count, in_files, &read_err,
1335 wth = merge_read_packet(in_file_count, in_files, &read_err,
1339 got_read_error = TRUE;
1343 /* Get the sum of the data offsets in all of the files. */
1345 for (i = 0; i < in_file_count; i++)
1346 data_offset += in_files[i].data_offset;
1348 /* Create the progress bar if necessary.
1349 We check on every iteration of the loop, so that it takes no
1350 longer than the standard time to create it (otherwise, for a
1351 large file, we might take considerably longer than that standard
1352 time in order to get to the next progress bar step). */
1353 if (progbar == NULL) {
1354 progbar = delayed_create_progress_dlg("Merging", "files",
1355 FALSE, &stop_flag, &start_time, progbar_val);
1358 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1359 when we update it, we have to run the GTK+ main loop to get it
1360 to repaint what's pending, and doing so may involve an "ioctl()"
1361 to see if there's any pending input from an X server, and doing
1362 that for every packet can be costly, especially on a big file. */
1363 if (data_offset >= progbar_nextstep) {
1364 /* Get the sum of the seek positions in all of the files. */
1366 for (i = 0; i < in_file_count; i++)
1367 file_pos += wtap_read_so_far(in_files[i].wth, NULL);
1368 progbar_val = (gfloat) file_pos / (gfloat) f_len;
1369 if (progbar_val > 1.0f) {
1370 /* Some file probably grew while we were reading it.
1371 That "shouldn't happen", so we'll just clip the progress
1375 if (progbar != NULL) {
1376 g_snprintf(status_str, sizeof(status_str),
1377 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
1378 file_pos / 1024, f_len / 1024);
1379 update_progress_dlg(progbar, progbar_val, status_str);
1381 progbar_nextstep += progbar_quantum;
1385 /* Well, the user decided to abort the merge. */
1389 if (!wtap_dump(pdh, wtap_phdr(wth), wtap_pseudoheader(wth),
1390 wtap_buf_ptr(wth), &write_err)) {
1391 got_write_error = TRUE;
1396 /* We're done merging the files; destroy the progress bar if it was created. */
1397 if (progbar != NULL)
1398 destroy_progress_dlg(progbar);
1400 merge_close_in_files(in_file_count, in_files);
1401 if (!got_read_error && !got_write_error) {
1402 if (!wtap_dump_close(pdh, &write_err))
1403 got_write_error = TRUE;
1405 wtap_dump_close(pdh, &close_err);
1407 if (got_read_error) {
1409 * Find the file on which we got the error, and report the error.
1411 for (i = 0; i < in_file_count; i++) {
1412 if (in_files[i].state == GOT_ERROR) {
1413 /* Put up a message box noting that a read failed somewhere along
1417 case WTAP_ERR_UNSUPPORTED_ENCAP:
1418 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1419 "The capture file %%s has a packet with a network type that Wireshark doesn't support.\n(%s)",
1422 errmsg = errmsg_errno;
1425 case WTAP_ERR_CANT_READ:
1426 errmsg = "An attempt to read from the capture file %s failed for"
1427 " some unknown reason.";
1430 case WTAP_ERR_SHORT_READ:
1431 errmsg = "The capture file %s appears to have been cut short"
1432 " in the middle of a packet.";
1435 case WTAP_ERR_BAD_RECORD:
1436 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1437 "The capture file %%s appears to be damaged or corrupt.\n(%s)",
1440 errmsg = errmsg_errno;
1444 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1445 "An error occurred while reading the"
1446 " capture file %%s: %s.", wtap_strerror(read_err));
1447 errmsg = errmsg_errno;
1450 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg, in_files[i].filename);
1455 if (got_write_error) {
1456 /* Put up an alert box for the write error. */
1457 cf_write_failure_alert_box(out_filename, write_err);
1460 if (got_read_error || got_write_error || stop_flag) {
1461 /* Callers aren't expected to treat an error or an explicit abort
1462 differently - we put up error dialogs ourselves, so they don't
1470 cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
1472 const char *filter_new = dftext ? dftext : "";
1473 const char *filter_old = cf->dfilter ? cf->dfilter : "";
1475 GTimeVal start_time;
1477 /* if new filter equals old one, do nothing unless told to do so */
1478 if (!force && strcmp(filter_new, filter_old) == 0) {
1484 if (dftext == NULL) {
1485 /* The new filter is an empty filter (i.e., display all packets).
1486 * so leave dfcode==NULL
1490 * We have a filter; make a copy of it (as we'll be saving it),
1491 * and try to compile it.
1493 dftext = g_strdup(dftext);
1494 if (!dfilter_compile(dftext, &dfcode)) {
1495 /* The attempt failed; report an error. */
1496 gchar *safe_dftext = simple_dialog_format_message(dftext);
1497 gchar *safe_dfilter_error_msg = simple_dialog_format_message(
1499 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1502 "The following display filter isn't a valid display filter:\n%s\n"
1503 "See the help for a description of the display filter syntax.",
1504 simple_dialog_primary_start(), safe_dfilter_error_msg,
1505 simple_dialog_primary_end(), safe_dftext);
1506 g_free(safe_dfilter_error_msg);
1507 g_free(safe_dftext);
1513 if (dfcode == NULL) {
1514 /* Yes - free the filter text, and set it to null. */
1520 /* We have a valid filter. Replace the current filter. */
1521 g_free(cf->dfilter);
1522 cf->dfilter = dftext;
1523 g_get_current_time(&start_time);
1526 /* Now rescan the packet list, applying the new filter, but not
1527 throwing away information constructed on a previous pass. */
1528 if (dftext == NULL) {
1529 rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
1531 rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
1534 /* Cleanup and release all dfilter resources */
1535 dfilter_free(dfcode);
1541 cf_reftime_packets(capture_file *cf)
1544 ref_time_packets(cf);
1548 cf_redissect_packets(capture_file *cf)
1550 rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
1554 cf_read_frame_r(capture_file *cf, frame_data *fdata,
1555 union wtap_pseudo_header *pseudo_header, guint8 *pd)
1559 char errmsg_errno[1024+1];
1561 if (!wtap_seek_read(cf->wth, fdata->file_off, pseudo_header, pd,
1562 fdata->cap_len, &err, &err_info)) {
1565 case WTAP_ERR_UNSUPPORTED_ENCAP:
1566 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1567 "The file \"%%s\" has a packet with a network type that Wireshark doesn't support.\n(%s)",
1572 case WTAP_ERR_BAD_RECORD:
1573 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1574 "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
1575 wtap_strerror(err), err_info);
1580 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1581 "An error occurred while reading from the file \"%%s\": %s.",
1582 wtap_strerror(err));
1585 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, errmsg_errno, cf->filename);
1592 cf_read_frame(capture_file *cf, frame_data *fdata)
1594 return cf_read_frame_r(cf, fdata, &cf->pseudo_header, cf->pd);
1597 /* Rescan the list of packets, reconstructing the CList.
1599 "action" describes why we're doing this; it's used in the progress
1602 "action_item" describes what we're doing; it's used in the progress
1605 "refilter" is TRUE if we need to re-evaluate the filter expression.
1607 "redissect" is TRUE if we need to make the dissectors reconstruct
1608 any state information they have (because a preference that affects
1609 some dissector has changed, meaning some dissector might construct
1610 its state differently from the way it was constructed the last time). */
1612 rescan_packets(capture_file *cf, const char *action, const char *action_item,
1613 gboolean refilter, gboolean redissect)
1615 /* Rescan packets new packet list */
1617 progdlg_t *progbar = NULL;
1620 frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
1621 int selected_frame_num, preceding_frame_num, following_frame_num, prev_frame_num;
1622 gboolean selected_frame_seen;
1624 GTimeVal start_time;
1625 gchar status_str[100];
1626 int progbar_nextstep;
1627 int progbar_quantum;
1629 gboolean filtering_tap_listeners;
1631 gboolean add_to_packet_list = FALSE;
1634 /* Compile the current display filter.
1635 * We assume this will not fail since cf->dfilter is only set in
1636 * cf_filter IFF the filter was valid.
1638 compiled = dfilter_compile(cf->dfilter, &dfcode);
1639 g_assert(!cf->dfilter || (compiled && dfcode));
1641 /* Do we have any tap listeners with filters? */
1642 filtering_tap_listeners = have_filtering_tap_listeners();
1644 /* Get the union of the flags for all tap listeners. */
1645 tap_flags = union_of_tap_listener_flags();
1647 reset_tap_listeners();
1648 /* Which frame, if any, is the currently selected frame?
1649 XXX - should the selected frame or the focus frame be the "current"
1650 frame, that frame being the one from which "Find Frame" searches
1652 selected_frame = cf->current_frame;
1654 /* Mark frame num as not found */
1655 selected_frame_num = -1;
1657 /* Freeze the packet list while we redo it, so we don't get any
1658 screen updates while it happens. */
1659 new_packet_list_freeze();
1662 /* We need to re-initialize all the state information that protocols
1663 keep, because some preference that controls a dissector has changed,
1664 which might cause the state information to be constructed differently
1665 by that dissector. */
1667 /* We might receive new packets while redissecting, and we don't
1668 want to dissect those before their time. */
1669 cf->redissecting = TRUE;
1671 /* Cleanup all data structures used for dissection. */
1672 cleanup_dissection();
1673 /* Initialize all data structures used for dissection. */
1676 /* We need to redissect the packets so we have to discard our old
1677 * packet list store. */
1678 new_packet_list_clear();
1679 add_to_packet_list = TRUE;
1682 /* We don't yet know which will be the first and last frames displayed. */
1683 cf->first_displayed = NULL;
1684 cf->last_displayed = NULL;
1686 /* We currently don't display any packets */
1687 cf->displayed_count = 0;
1689 /* Iterate through the list of frames. Call a routine for each frame
1690 to check whether it should be displayed and, if so, add it to
1691 the display list. */
1692 nstime_set_unset(&first_ts);
1693 nstime_set_unset(&prev_dis_ts);
1694 nstime_set_unset(&prev_cap_ts);
1697 /* Update the progress bar when it gets to this value. */
1698 progbar_nextstep = 0;
1699 /* When we reach the value that triggers a progress bar update,
1700 bump that value by this amount. */
1701 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1702 /* Count of packets at which we've looked. */
1704 /* Progress so far. */
1708 g_get_current_time(&start_time);
1710 /* no previous row yet */
1711 prev_frame_num = -1;
1714 preceding_frame_num = -1;
1715 preceding_frame = NULL;
1716 following_frame_num = -1;
1717 following_frame = NULL;
1719 selected_frame_seen = FALSE;
1721 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
1722 /* Create the progress bar if necessary.
1723 We check on every iteration of the loop, so that it takes no
1724 longer than the standard time to create it (otherwise, for a
1725 large file, we might take considerably longer than that standard
1726 time in order to get to the next progress bar step). */
1727 if (progbar == NULL)
1728 progbar = delayed_create_progress_dlg(action, action_item, TRUE,
1729 &stop_flag, &start_time,
1732 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1733 when we update it, we have to run the GTK+ main loop to get it
1734 to repaint what's pending, and doing so may involve an "ioctl()"
1735 to see if there's any pending input from an X server, and doing
1736 that for every packet can be costly, especially on a big file. */
1737 if (count >= progbar_nextstep) {
1738 /* let's not divide by zero. I should never be started
1739 * with count == 0, so let's assert that
1741 g_assert(cf->count > 0);
1742 progbar_val = (gfloat) count / cf->count;
1744 if (progbar != NULL) {
1745 g_snprintf(status_str, sizeof(status_str),
1746 "%4u of %u frames", count, cf->count);
1747 update_progress_dlg(progbar, progbar_val, status_str);
1750 progbar_nextstep += progbar_quantum;
1754 /* Well, the user decided to abort the filtering. Just stop.
1756 XXX - go back to the previous filter? Users probably just
1757 want not to wait for a filtering operation to finish;
1758 unless we cancel by having no filter, reverting to the
1759 previous filter will probably be even more expensive than
1760 continuing the filtering, as it involves going back to the
1761 beginning and filtering, and even with no filter we currently
1762 have to re-generate the entire clist, which is also expensive.
1764 I'm not sure what Network Monitor does, but it doesn't appear
1765 to give you an unfiltered display if you cancel. */
1772 /* Since all state for the frame was destroyed, mark the frame
1773 * as not visited, free the GSList referring to the state
1774 * data (the per-frame data itself was freed by
1775 * "init_dissection()"), and null out the GSList pointer. */
1776 fdata->flags.visited = 0;
1777 frame_data_cleanup(fdata);
1779 /* cleanup_dissection() calls se_free_all();
1780 * And after that fdata->col_text (which is allocated using se_alloc0())
1781 * no longer points to valid memory.
1783 init_col_text(fdata, cf->cinfo.num_cols);
1786 if (!cf_read_frame(cf, fdata))
1787 break; /* error reading the frame */
1789 /* If the previous frame is displayed, and we haven't yet seen the
1790 selected frame, remember that frame - it's the closest one we've
1791 yet seen before the selected frame. */
1792 if (prev_frame_num != -1 && !selected_frame_seen && prev_frame->flags.passed_dfilter) {
1793 preceding_frame_num = prev_frame_num;
1794 preceding_frame = prev_frame;
1796 add_packet_to_packet_list(fdata, cf, dfcode, filtering_tap_listeners,
1797 tap_flags, &cf->pseudo_header, cf->pd,
1799 add_to_packet_list);
1801 /* If this frame is displayed, and this is the first frame we've
1802 seen displayed after the selected frame, remember this frame -
1803 it's the closest one we've yet seen at or after the selected
1805 if (fdata->flags.passed_dfilter && selected_frame_seen && following_frame_num == -1) {
1806 following_frame_num = fdata->num;
1807 following_frame = fdata;
1809 if (fdata == selected_frame) {
1810 selected_frame_seen = TRUE;
1811 if (fdata->flags.passed_dfilter)
1812 selected_frame_num = fdata->num;
1815 /* Remember this frame - it'll be the previous frame
1816 on the next pass through the loop. */
1817 prev_frame_num = fdata->num;
1821 /* We are done redissecting the packet list. */
1822 cf->redissecting = FALSE;
1825 /* Clear out what remains of the visited flags and per-frame data
1828 XXX - that may cause various forms of bogosity when dissecting
1829 these frames, as they won't have been seen by this sequential
1830 pass, but the only alternative I see is to keep scanning them
1831 even though the user requested that the scan stop, and that
1832 would leave the user stuck with an Wireshark grinding on
1833 until it finishes. Should we just stick them with that? */
1834 for (; fdata != NULL; fdata = fdata->next) {
1835 fdata->flags.visited = 0;
1836 frame_data_cleanup(fdata);
1840 /* We're done filtering the packets; destroy the progress bar if it
1842 if (progbar != NULL)
1843 destroy_progress_dlg(progbar);
1845 /* Unfreeze the packet list. */
1846 if (!add_to_packet_list)
1847 new_packet_list_recreate_visible_rows();
1849 /* Compute the time it took to filter the file */
1850 compute_elapsed(&start_time);
1852 new_packet_list_thaw();
1854 if (selected_frame_num == -1) {
1855 /* The selected frame didn't pass the filter. */
1856 if (selected_frame == NULL) {
1857 /* That's because there *was* no selected frame. Make the first
1858 displayed frame the current frame. */
1859 selected_frame_num = 0;
1861 /* Find the nearest displayed frame to the selected frame (whether
1862 it's before or after that frame) and make that the current frame.
1863 If the next and previous displayed frames are equidistant from the
1864 selected frame, choose the next one. */
1865 g_assert(following_frame == NULL ||
1866 following_frame->num >= selected_frame->num);
1867 g_assert(preceding_frame == NULL ||
1868 preceding_frame->num <= selected_frame->num);
1869 if (following_frame == NULL) {
1870 /* No frame after the selected frame passed the filter, so we
1871 have to select the last displayed frame before the selected
1873 selected_frame_num = preceding_frame_num;
1874 selected_frame = preceding_frame;
1875 } else if (preceding_frame == NULL) {
1876 /* No frame before the selected frame passed the filter, so we
1877 have to select the first displayed frame after the selected
1879 selected_frame_num = following_frame_num;
1880 selected_frame = following_frame;
1882 /* Frames before and after the selected frame passed the filter, so
1883 we'll select the previous frame */
1884 selected_frame_num = preceding_frame_num;
1885 selected_frame = preceding_frame;
1890 if (selected_frame_num == -1) {
1891 /* There are no frames displayed at all. */
1892 cf_unselect_packet(cf);
1894 /* Either the frame that was selected passed the filter, or we've
1895 found the nearest displayed frame to that frame. Select it, make
1896 it the focus row, and make it visible. */
1897 /* Set to invalid to force update of packet list and packet details */
1898 cf->current_row = -1;
1899 if (selected_frame_num == 0) {
1900 new_packet_list_select_first_row();
1902 new_packet_list_find_row_from_data(selected_frame, TRUE);
1906 /* Cleanup and release all dfilter resources */
1907 dfilter_free(dfcode);
1912 * Scan trough all frame data and recalculate the ref time
1913 * without rereading the file.
1914 * XXX - do we need a progres bar or is this fast enough?
1917 ref_time_packets(capture_file *cf)
1921 nstime_set_unset(&first_ts);
1922 nstime_set_unset(&prev_dis_ts);
1925 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
1926 /* just add some value here until we know if it is being displayed or not */
1927 fdata->cum_bytes = cum_bytes + fdata->pkt_len;
1933 /* If we don't have the time stamp of the first packet in the
1934 capture, it's because this is the first packet. Save the time
1935 stamp of this packet as the time stamp of the first packet. */
1936 if (nstime_is_unset(&first_ts)) {
1937 first_ts = fdata->abs_ts;
1939 /* if this frames is marked as a reference time frame, reset
1940 firstsec and firstusec to this frame */
1941 if(fdata->flags.ref_time){
1942 first_ts = fdata->abs_ts;
1945 /* If we don't have the time stamp of the previous displayed packet,
1946 it's because this is the first displayed packet. Save the time
1947 stamp of this packet as the time stamp of the previous displayed
1949 if (nstime_is_unset(&prev_dis_ts)) {
1950 prev_dis_ts = fdata->abs_ts;
1953 /* Get the time elapsed between the first packet and this packet. */
1954 nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
1956 /* If it's greater than the current elapsed time, set the elapsed time
1957 to it (we check for "greater than" so as not to be confused by
1958 time moving backwards). */
1959 if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
1960 || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
1961 cf->elapsed_time = fdata->rel_ts;
1964 /* Get the time elapsed between the previous displayed packet and
1966 nstime_delta(&fdata->del_dis_ts, &fdata->abs_ts, &prev_dis_ts);
1968 prev_dis_ts = fdata->abs_ts;
1973 if( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) ){
1974 /* This frame either passed the display filter list or is marked as
1975 a time reference frame. All time reference frames are displayed
1976 even if they dont pass the display filter */
1977 if(fdata->flags.ref_time){
1978 /* if this was a TIME REF frame we should reset the cum_bytes field */
1979 cum_bytes = fdata->pkt_len;
1980 fdata->cum_bytes = cum_bytes;
1982 /* increase cum_bytes with this packets length */
1983 cum_bytes += fdata->pkt_len;
1996 process_specified_packets(capture_file *cf, packet_range_t *range,
1997 const char *string1, const char *string2, gboolean terminate_is_stop,
1998 gboolean (*callback)(capture_file *, frame_data *,
1999 union wtap_pseudo_header *, const guint8 *, void *),
2000 void *callback_args)
2003 union wtap_pseudo_header pseudo_header;
2004 guint8 pd[WTAP_MAX_PACKET_SIZE+1];
2005 psp_return_t ret = PSP_FINISHED;
2007 progdlg_t *progbar = NULL;
2010 gboolean progbar_stop_flag;
2011 GTimeVal progbar_start_time;
2012 gchar progbar_status_str[100];
2013 int progbar_nextstep;
2014 int progbar_quantum;
2015 range_process_e process_this;
2017 /* Update the progress bar when it gets to this value. */
2018 progbar_nextstep = 0;
2019 /* When we reach the value that triggers a progress bar update,
2020 bump that value by this amount. */
2021 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2022 /* Count of packets at which we've looked. */
2024 /* Progress so far. */
2027 progbar_stop_flag = FALSE;
2028 g_get_current_time(&progbar_start_time);
2030 packet_range_process_init(range);
2032 /* Iterate through the list of packets, printing the packets that
2033 were selected by the current display filter. */
2034 for (fdata = cf->plist_start; fdata != NULL; fdata = fdata->next) {
2035 /* Create the progress bar if necessary.
2036 We check on every iteration of the loop, so that it takes no
2037 longer than the standard time to create it (otherwise, for a
2038 large file, we might take considerably longer than that standard
2039 time in order to get to the next progress bar step). */
2040 if (progbar == NULL)
2041 progbar = delayed_create_progress_dlg(string1, string2,
2044 &progbar_start_time,
2047 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2048 when we update it, we have to run the GTK+ main loop to get it
2049 to repaint what's pending, and doing so may involve an "ioctl()"
2050 to see if there's any pending input from an X server, and doing
2051 that for every packet can be costly, especially on a big file. */
2052 if (progbar_count >= progbar_nextstep) {
2053 /* let's not divide by zero. I should never be started
2054 * with count == 0, so let's assert that
2056 g_assert(cf->count > 0);
2057 progbar_val = (gfloat) progbar_count / cf->count;
2059 if (progbar != NULL) {
2060 g_snprintf(progbar_status_str, sizeof(progbar_status_str),
2061 "%4u of %u packets", progbar_count, cf->count);
2062 update_progress_dlg(progbar, progbar_val, progbar_status_str);
2065 progbar_nextstep += progbar_quantum;
2068 if (progbar_stop_flag) {
2069 /* Well, the user decided to abort the operation. Just stop,
2070 and arrange to return PSP_STOPPED to our caller, so they know
2071 it was stopped explicitly. */
2078 /* do we have to process this packet? */
2079 process_this = packet_range_process_packet(range, fdata);
2080 if (process_this == range_process_next) {
2081 /* this packet uninteresting, continue with next one */
2083 } else if (process_this == range_processing_finished) {
2084 /* all interesting packets processed, stop the loop */
2088 /* Get the packet */
2089 if (!cf_read_frame_r(cf, fdata, &pseudo_header, pd)) {
2090 /* Attempt to get the packet failed. */
2094 /* Process the packet */
2095 if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
2096 /* Callback failed. We assume it reported the error appropriately. */
2102 /* We're done printing the packets; destroy the progress bar if
2104 if (progbar != NULL)
2105 destroy_progress_dlg(progbar);
2111 gboolean construct_protocol_tree;
2113 } retap_callback_args_t;
2116 retap_packet(capture_file *cf _U_, frame_data *fdata,
2117 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2120 retap_callback_args_t *args = argsp;
2123 epan_dissect_init(&edt, args->construct_protocol_tree, FALSE);
2124 tap_queue_init(&edt);
2125 epan_dissect_run(&edt, pseudo_header, pd, fdata, args->cinfo);
2126 tap_push_tapped_queue(&edt);
2127 epan_dissect_cleanup(&edt);
2133 cf_retap_packets(capture_file *cf)
2135 packet_range_t range;
2136 retap_callback_args_t callback_args;
2137 gboolean filtering_tap_listeners;
2140 /* Do we have any tap listeners with filters? */
2141 filtering_tap_listeners = have_filtering_tap_listeners();
2143 tap_flags = union_of_tap_listener_flags();
2145 /* If any tap listeners have filters, or require the protocol tree,
2146 construct the protocol tree. */
2147 callback_args.construct_protocol_tree = filtering_tap_listeners ||
2148 (tap_flags & TL_REQUIRES_PROTO_TREE);
2150 /* If any tap listeners require the columns, construct them. */
2151 callback_args.cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
2153 /* Reset the tap listeners. */
2154 reset_tap_listeners();
2156 /* Iterate through the list of packets, dissecting all packets and
2157 re-running the taps. */
2158 packet_range_init(&range);
2159 packet_range_process_init(&range);
2160 switch (process_specified_packets(cf, &range, "Recalculating statistics on",
2161 "all packets", TRUE, retap_packet,
2164 /* Completed successfully. */
2168 /* Well, the user decided to abort the refiltering.
2169 Return CF_READ_ABORTED so our caller knows they did that. */
2170 return CF_READ_ABORTED;
2173 /* Error while retapping. */
2174 return CF_READ_ERROR;
2177 g_assert_not_reached();
2182 print_args_t *print_args;
2183 gboolean print_header_line;
2184 char *header_line_buf;
2185 int header_line_buf_len;
2186 gboolean print_formfeed;
2187 gboolean print_separator;
2191 } print_callback_args_t;
2194 print_packet(capture_file *cf, frame_data *fdata,
2195 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2198 print_callback_args_t *args = argsp;
2205 gboolean proto_tree_needed;
2206 char bookmark_name[9+10+1]; /* "__frameNNNNNNNNNN__\0" */
2207 char bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0" */
2209 /* Create the protocol tree, and make it visible, if we're printing
2210 the dissection or the hex data.
2211 XXX - do we need it if we're just printing the hex data? */
2213 args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex || have_custom_cols(&cf->cinfo);
2214 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2216 /* Fill in the column information if we're printing the summary
2218 if (args->print_args->print_summary) {
2219 col_custom_prime_edt(&edt, &cf->cinfo);
2220 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2221 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2223 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2225 if (args->print_formfeed) {
2226 if (!new_page(args->print_args->stream))
2229 if (args->print_separator) {
2230 if (!print_line(args->print_args->stream, 0, ""))
2236 * We generate bookmarks, if the output format supports them.
2237 * The name is "__frameN__".
2239 g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
2241 if (args->print_args->print_summary) {
2242 if (args->print_header_line) {
2243 if (!print_line(args->print_args->stream, 0, args->header_line_buf))
2245 args->print_header_line = FALSE; /* we might not need to print any more */
2247 cp = &args->line_buf[0];
2249 for (i = 0; i < cf->cinfo.num_cols; i++) {
2250 /* Find the length of the string for this column. */
2251 column_len = (int) strlen(cf->cinfo.col_data[i]);
2252 if (args->col_widths[i] > column_len)
2253 column_len = args->col_widths[i];
2255 /* Make sure there's room in the line buffer for the column; if not,
2256 double its length. */
2257 line_len += column_len + 1; /* "+1" for space */
2258 if (line_len > args->line_buf_len) {
2259 cp_off = (int) (cp - args->line_buf);
2260 args->line_buf_len = 2 * line_len;
2261 args->line_buf = g_realloc(args->line_buf, args->line_buf_len + 1);
2262 cp = args->line_buf + cp_off;
2265 /* Right-justify the packet number column. */
2266 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2267 g_snprintf(cp, column_len+1, "%*s", args->col_widths[i], cf->cinfo.col_data[i]);
2269 g_snprintf(cp, column_len+1, "%-*s", args->col_widths[i], cf->cinfo.col_data[i]);
2271 if (i != cf->cinfo.num_cols - 1)
2277 * Generate a bookmark, using the summary line as the title.
2279 if (!print_bookmark(args->print_args->stream, bookmark_name,
2283 if (!print_line(args->print_args->stream, 0, args->line_buf))
2287 * Generate a bookmark, using "Frame N" as the title, as we're not
2288 * printing the summary line.
2290 g_snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
2291 if (!print_bookmark(args->print_args->stream, bookmark_name,
2294 } /* if (print_summary) */
2296 if (args->print_args->print_dissections != print_dissections_none) {
2297 if (args->print_args->print_summary) {
2298 /* Separate the summary line from the tree with a blank line. */
2299 if (!print_line(args->print_args->stream, 0, ""))
2303 /* Print the information in that tree. */
2304 if (!proto_tree_print(args->print_args, &edt, args->print_args->stream))
2307 /* Print a blank line if we print anything after this (aka more than one packet). */
2308 args->print_separator = TRUE;
2310 /* Print a header line if we print any more packet summaries */
2311 args->print_header_line = TRUE;
2314 if (args->print_args->print_hex) {
2315 /* Print the full packet data as hex. */
2316 if (!print_hex_data(args->print_args->stream, &edt))
2319 /* Print a blank line if we print anything after this (aka more than one packet). */
2320 args->print_separator = TRUE;
2322 /* Print a header line if we print any more packet summaries */
2323 args->print_header_line = TRUE;
2324 } /* if (args->print_args->print_dissections != print_dissections_none) */
2326 epan_dissect_cleanup(&edt);
2328 /* do we want to have a formfeed between each packet from now on? */
2329 if(args->print_args->print_formfeed) {
2330 args->print_formfeed = TRUE;
2336 epan_dissect_cleanup(&edt);
2341 cf_print_packets(capture_file *cf, print_args_t *print_args)
2344 print_callback_args_t callback_args;
2352 callback_args.print_args = print_args;
2353 callback_args.print_header_line = TRUE;
2354 callback_args.header_line_buf = NULL;
2355 callback_args.header_line_buf_len = 256;
2356 callback_args.print_formfeed = FALSE;
2357 callback_args.print_separator = FALSE;
2358 callback_args.line_buf = NULL;
2359 callback_args.line_buf_len = 256;
2360 callback_args.col_widths = NULL;
2362 if (!print_preamble(print_args->stream, cf->filename)) {
2363 destroy_print_stream(print_args->stream);
2364 return CF_PRINT_WRITE_ERROR;
2367 if (print_args->print_summary) {
2368 /* We're printing packet summaries. Allocate the header line buffer
2369 and get the column widths. */
2370 callback_args.header_line_buf = g_malloc(callback_args.header_line_buf_len + 1);
2372 /* Find the widths for each of the columns - maximum of the
2373 width of the title and the width of the data - and construct
2374 a buffer with a line containing the column titles. */
2375 callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * cf->cinfo.num_cols);
2376 cp = &callback_args.header_line_buf[0];
2378 for (i = 0; i < cf->cinfo.num_cols; i++) {
2379 /* Don't pad the last column. */
2380 if (i == cf->cinfo.num_cols - 1)
2381 callback_args.col_widths[i] = 0;
2383 callback_args.col_widths[i] = (gint) strlen(cf->cinfo.col_title[i]);
2384 data_width = get_column_char_width(get_column_format(i));
2385 if (data_width > callback_args.col_widths[i])
2386 callback_args.col_widths[i] = data_width;
2389 /* Find the length of the string for this column. */
2390 column_len = (int) strlen(cf->cinfo.col_title[i]);
2391 if (callback_args.col_widths[i] > column_len)
2392 column_len = callback_args.col_widths[i];
2394 /* Make sure there's room in the line buffer for the column; if not,
2395 double its length. */
2396 line_len += column_len + 1; /* "+1" for space */
2397 if (line_len > callback_args.header_line_buf_len) {
2398 cp_off = (int) (cp - callback_args.header_line_buf);
2399 callback_args.header_line_buf_len = 2 * line_len;
2400 callback_args.header_line_buf = g_realloc(callback_args.header_line_buf,
2401 callback_args.header_line_buf_len + 1);
2402 cp = callback_args.header_line_buf + cp_off;
2405 /* Right-justify the packet number column. */
2406 /* if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2407 g_snprintf(cp, column_len+1, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2409 g_snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
2411 if (i != cf->cinfo.num_cols - 1)
2416 /* Now start out the main line buffer with the same length as the
2417 header line buffer. */
2418 callback_args.line_buf_len = callback_args.header_line_buf_len;
2419 callback_args.line_buf = g_malloc(callback_args.line_buf_len + 1);
2420 } /* if (print_summary) */
2422 /* Iterate through the list of packets, printing the packets we were
2424 ret = process_specified_packets(cf, &print_args->range, "Printing",
2425 "selected packets", TRUE, print_packet,
2428 g_free(callback_args.header_line_buf);
2429 g_free(callback_args.line_buf);
2430 g_free(callback_args.col_widths);
2435 /* Completed successfully. */
2439 /* Well, the user decided to abort the printing.
2441 XXX - note that what got generated before they did that
2442 will get printed if we're piping to a print program; we'd
2443 have to write to a file and then hand that to the print
2444 program to make it actually not print anything. */
2448 /* Error while printing.
2450 XXX - note that what got generated before they did that
2451 will get printed if we're piping to a print program; we'd
2452 have to write to a file and then hand that to the print
2453 program to make it actually not print anything. */
2454 destroy_print_stream(print_args->stream);
2455 return CF_PRINT_WRITE_ERROR;
2458 if (!print_finale(print_args->stream)) {
2459 destroy_print_stream(print_args->stream);
2460 return CF_PRINT_WRITE_ERROR;
2463 if (!destroy_print_stream(print_args->stream))
2464 return CF_PRINT_WRITE_ERROR;
2470 write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
2471 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2477 /* Create the protocol tree, but don't fill in the column information. */
2478 epan_dissect_init(&edt, TRUE, TRUE);
2479 epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
2481 /* Write out the information in that tree. */
2482 proto_tree_write_pdml(&edt, fh);
2484 epan_dissect_cleanup(&edt);
2490 cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
2495 fh = ws_fopen(print_args->file, "w");
2497 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2499 write_pdml_preamble(fh);
2502 return CF_PRINT_WRITE_ERROR;
2505 /* Iterate through the list of packets, printing the packets we were
2507 ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
2508 "selected packets", TRUE,
2509 write_pdml_packet, fh);
2514 /* Completed successfully. */
2518 /* Well, the user decided to abort the printing. */
2522 /* Error while printing. */
2524 return CF_PRINT_WRITE_ERROR;
2527 write_pdml_finale(fh);
2530 return CF_PRINT_WRITE_ERROR;
2533 /* XXX - check for an error */
2540 write_psml_packet(capture_file *cf, frame_data *fdata,
2541 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2546 gboolean proto_tree_needed;
2548 /* Fill in the column information, only create the protocol tree
2549 if having custom columns. */
2550 proto_tree_needed = have_custom_cols(&cf->cinfo);
2551 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2552 col_custom_prime_edt(&edt, &cf->cinfo);
2553 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2554 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2556 /* Write out the information in that tree. */
2557 proto_tree_write_psml(&edt, fh);
2559 epan_dissect_cleanup(&edt);
2565 cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
2570 fh = ws_fopen(print_args->file, "w");
2572 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2574 write_psml_preamble(fh);
2577 return CF_PRINT_WRITE_ERROR;
2580 /* Iterate through the list of packets, printing the packets we were
2582 ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
2583 "selected packets", TRUE,
2584 write_psml_packet, fh);
2589 /* Completed successfully. */
2593 /* Well, the user decided to abort the printing. */
2597 /* Error while printing. */
2599 return CF_PRINT_WRITE_ERROR;
2602 write_psml_finale(fh);
2605 return CF_PRINT_WRITE_ERROR;
2608 /* XXX - check for an error */
2615 write_csv_packet(capture_file *cf, frame_data *fdata,
2616 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2621 gboolean proto_tree_needed;
2623 /* Fill in the column information, only create the protocol tree
2624 if having custom columns. */
2625 proto_tree_needed = have_custom_cols(&cf->cinfo);
2626 epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
2627 col_custom_prime_edt(&edt, &cf->cinfo);
2628 epan_dissect_run(&edt, pseudo_header, pd, fdata, &cf->cinfo);
2629 epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
2631 /* Write out the information in that tree. */
2632 proto_tree_write_csv(&edt, fh);
2634 epan_dissect_cleanup(&edt);
2640 cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
2645 fh = ws_fopen(print_args->file, "w");
2647 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2649 write_csv_preamble(fh);
2652 return CF_PRINT_WRITE_ERROR;
2655 /* Iterate through the list of packets, printing the packets we were
2657 ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
2658 "selected packets", TRUE,
2659 write_csv_packet, fh);
2664 /* Completed successfully. */
2668 /* Well, the user decided to abort the printing. */
2672 /* Error while printing. */
2674 return CF_PRINT_WRITE_ERROR;
2677 write_csv_finale(fh);
2680 return CF_PRINT_WRITE_ERROR;
2683 /* XXX - check for an error */
2690 write_carrays_packet(capture_file *cf _U_, frame_data *fdata,
2691 union wtap_pseudo_header *pseudo_header _U_,
2692 const guint8 *pd, void *argsp)
2696 proto_tree_write_carrays(pd, fdata->cap_len, fdata->num, fh);
2701 cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
2706 fh = ws_fopen(print_args->file, "w");
2709 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2711 write_carrays_preamble(fh);
2715 return CF_PRINT_WRITE_ERROR;
2718 /* Iterate through the list of packets, printing the packets we were
2720 ret = process_specified_packets(cf, &print_args->range,
2722 "selected packets", TRUE,
2723 write_carrays_packet, fh);
2726 /* Completed successfully. */
2729 /* Well, the user decided to abort the printing. */
2732 /* Error while printing. */
2734 return CF_PRINT_WRITE_ERROR;
2737 write_carrays_finale(fh);
2741 return CF_PRINT_WRITE_ERROR;
2749 cf_find_packet_protocol_tree(capture_file *cf, const char *string,
2750 search_direction dir)
2754 mdata.string = string;
2755 mdata.string_len = strlen(string);
2756 return find_packet(cf, match_protocol_tree, &mdata, dir);
2760 cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree, match_data *mdata)
2762 mdata->frame_matched = FALSE;
2763 mdata->string = convert_string_case(cf->sfilter, cf->case_type);
2764 mdata->string_len = strlen(mdata->string);
2766 /* Iterate through all the nodes looking for matching text */
2767 proto_tree_children_foreach(tree, match_subtree_text, mdata);
2768 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2772 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
2774 match_data *mdata = criterion;
2777 /* Load the frame's data. */
2778 if (!cf_read_frame(cf, fdata)) {
2779 /* Attempt to get the packet failed. */
2783 /* Construct the protocol tree, including the displayed text */
2784 epan_dissect_init(&edt, TRUE, TRUE);
2785 /* We don't need the column information */
2786 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
2788 /* Iterate through all the nodes, seeing if they have text that matches. */
2790 mdata->frame_matched = FALSE;
2791 proto_tree_children_foreach(edt.tree, match_subtree_text, mdata);
2792 epan_dissect_cleanup(&edt);
2793 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
2797 match_subtree_text(proto_node *node, gpointer data)
2799 match_data *mdata = (match_data*) data;
2800 const gchar *string = mdata->string;
2801 size_t string_len = mdata->string_len;
2802 capture_file *cf = mdata->cf;
2803 field_info *fi = PNODE_FINFO(node);
2804 gchar label_str[ITEM_LABEL_LENGTH];
2811 g_assert(fi && "dissection with an invisible proto tree?");
2813 if (mdata->frame_matched) {
2814 /* We already had a match; don't bother doing any more work. */
2818 /* Don't match invisible entries. */
2819 if (PROTO_ITEM_IS_HIDDEN(node))
2822 /* was a free format label produced? */
2824 label_ptr = fi->rep->representation;
2826 /* no, make a generic label */
2827 label_ptr = label_str;
2828 proto_item_fill_label(fi, label_str);
2831 /* Does that label match? */
2832 label_len = strlen(label_ptr);
2833 for (i = 0; i < label_len; i++) {
2834 c_char = label_ptr[i];
2836 c_char = toupper(c_char);
2837 if (c_char == string[c_match]) {
2839 if (c_match == string_len) {
2840 /* No need to look further; we have a match */
2841 mdata->frame_matched = TRUE;
2849 /* Recurse into the subtree, if it exists */
2850 if (node->first_child != NULL)
2851 proto_tree_children_foreach(node, match_subtree_text, mdata);
2855 cf_find_packet_summary_line(capture_file *cf, const char *string,
2856 search_direction dir)
2860 mdata.string = string;
2861 mdata.string_len = strlen(string);
2862 return find_packet(cf, match_summary_line, &mdata, dir);
2866 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
2868 match_data *mdata = criterion;
2869 const gchar *string = mdata->string;
2870 size_t string_len = mdata->string_len;
2872 const char *info_column;
2873 size_t info_column_len;
2874 match_result result = MR_NOTMATCHED;
2880 /* Load the frame's data. */
2881 if (!cf_read_frame(cf, fdata)) {
2882 /* Attempt to get the packet failed. */
2886 /* Don't bother constructing the protocol tree */
2887 epan_dissect_init(&edt, FALSE, FALSE);
2888 /* Get the column information */
2889 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, &cf->cinfo);
2891 /* Find the Info column */
2892 for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
2893 if (cf->cinfo.fmt_matx[colx][COL_INFO]) {
2894 /* Found it. See if we match. */
2895 info_column = edt.pi.cinfo->col_data[colx];
2896 info_column_len = strlen(info_column);
2897 for (i = 0; i < info_column_len; i++) {
2898 c_char = info_column[i];
2900 c_char = toupper(c_char);
2901 if (c_char == string[c_match]) {
2903 if (c_match == string_len) {
2904 result = MR_MATCHED;
2913 epan_dissect_cleanup(&edt);
2920 } cbs_t; /* "Counted byte string" */
2923 cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
2924 search_direction dir)
2929 info.data_len = string_size;
2931 /* String or hex search? */
2933 /* String search - what type of string? */
2934 switch (cf->scs_type) {
2936 case SCS_ASCII_AND_UNICODE:
2937 return find_packet(cf, match_ascii_and_unicode, &info, dir);
2940 return find_packet(cf, match_ascii, &info, dir);
2943 return find_packet(cf, match_unicode, &info, dir);
2946 g_assert_not_reached();
2950 return find_packet(cf, match_binary, &info, dir);
2954 match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
2956 cbs_t *info = criterion;
2957 const guint8 *ascii_text = info->data;
2958 size_t textlen = info->data_len;
2959 match_result result;
2965 /* Load the frame's data. */
2966 if (!cf_read_frame(cf, fdata)) {
2967 /* Attempt to get the packet failed. */
2971 result = MR_NOTMATCHED;
2972 buf_len = fdata->pkt_len;
2973 for (i = 0; i < buf_len; i++) {
2976 c_char = toupper(c_char);
2978 if (c_char == ascii_text[c_match]) {
2980 if (c_match == textlen) {
2981 result = MR_MATCHED;
2982 cf->search_pos = i; /* Save the position of the last character
2983 for highlighting the field. */
2994 match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
2996 cbs_t *info = criterion;
2997 const guint8 *ascii_text = info->data;
2998 size_t textlen = info->data_len;
2999 match_result result;
3005 /* Load the frame's data. */
3006 if (!cf_read_frame(cf, fdata)) {
3007 /* Attempt to get the packet failed. */
3011 result = MR_NOTMATCHED;
3012 buf_len = fdata->pkt_len;
3013 for (i = 0; i < buf_len; i++) {
3016 c_char = toupper(c_char);
3017 if (c_char == ascii_text[c_match]) {
3019 if (c_match == textlen) {
3020 result = MR_MATCHED;
3021 cf->search_pos = i; /* Save the position of the last character
3022 for highlighting the field. */
3032 match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
3034 cbs_t *info = criterion;
3035 const guint8 *ascii_text = info->data;
3036 size_t textlen = info->data_len;
3037 match_result result;
3043 /* Load the frame's data. */
3044 if (!cf_read_frame(cf, fdata)) {
3045 /* Attempt to get the packet failed. */
3049 result = MR_NOTMATCHED;
3050 buf_len = fdata->pkt_len;
3051 for (i = 0; i < buf_len; i++) {
3054 c_char = toupper(c_char);
3055 if (c_char == ascii_text[c_match]) {
3058 if (c_match == textlen) {
3059 result = MR_MATCHED;
3060 cf->search_pos = i; /* Save the position of the last character
3061 for highlighting the field. */
3071 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
3073 cbs_t *info = criterion;
3074 const guint8 *binary_data = info->data;
3075 size_t datalen = info->data_len;
3076 match_result result;
3081 /* Load the frame's data. */
3082 if (!cf_read_frame(cf, fdata)) {
3083 /* Attempt to get the packet failed. */
3087 result = MR_NOTMATCHED;
3088 buf_len = fdata->pkt_len;
3089 for (i = 0; i < buf_len; i++) {
3090 if (cf->pd[i] == binary_data[c_match]) {
3092 if (c_match == datalen) {
3093 result = MR_MATCHED;
3094 cf->search_pos = i; /* Save the position of the last character
3095 for highlighting the field. */
3105 cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode,
3106 search_direction dir)
3108 return find_packet(cf, match_dfilter, sfcode, dir);
3112 cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
3113 search_direction dir)
3118 if (!dfilter_compile(filter, &sfcode)) {
3120 * XXX - this shouldn't happen, as the filter string is machine
3125 if (sfcode == NULL) {
3127 * XXX - this shouldn't happen, as the filter string is machine
3132 result = find_packet(cf, match_dfilter, sfcode, dir);
3133 dfilter_free(sfcode);
3138 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
3140 dfilter_t *sfcode = criterion;
3142 match_result result;
3144 /* Load the frame's data. */
3145 if (!cf_read_frame(cf, fdata)) {
3146 /* Attempt to get the packet failed. */
3150 epan_dissect_init(&edt, TRUE, FALSE);
3151 epan_dissect_prime_dfilter(&edt, sfcode);
3152 epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
3153 result = dfilter_apply_edt(sfcode, &edt) ? MR_MATCHED : MR_NOTMATCHED;
3154 epan_dissect_cleanup(&edt);
3159 cf_find_packet_marked(capture_file *cf, search_direction dir)
3161 return find_packet(cf, match_marked, NULL, dir);
3165 match_marked(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3167 return fdata->flags.marked ? MR_MATCHED : MR_NOTMATCHED;
3171 cf_find_packet_time_reference(capture_file *cf, search_direction dir)
3173 return find_packet(cf, match_time_reference, NULL, dir);
3177 match_time_reference(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3179 return fdata->flags.ref_time ? MR_MATCHED : MR_NOTMATCHED;
3183 find_packet(capture_file *cf,
3184 match_result (*match_function)(capture_file *, frame_data *, void *),
3185 void *criterion, search_direction dir)
3187 frame_data *start_fd;
3189 frame_data *new_fd = NULL;
3190 progdlg_t *progbar = NULL;
3195 GTimeVal start_time;
3196 gchar status_str[100];
3197 int progbar_nextstep;
3198 int progbar_quantum;
3200 match_result result;
3202 start_fd = cf->current_frame;
3203 if (start_fd != NULL) {
3204 /* Iterate through the list of packets, starting at the packet we've
3205 picked, calling a routine to run the filter on the packet, see if
3206 it matches, and stop if so. */
3210 /* Update the progress bar when it gets to this value. */
3211 progbar_nextstep = 0;
3212 /* When we reach the value that triggers a progress bar update,
3213 bump that value by this amount. */
3214 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
3215 /* Progress so far. */
3219 g_get_current_time(&start_time);
3221 title = cf->sfilter?cf->sfilter:"";
3223 /* Create the progress bar if necessary.
3224 We check on every iteration of the loop, so that it takes no
3225 longer than the standard time to create it (otherwise, for a
3226 large file, we might take considerably longer than that standard
3227 time in order to get to the next progress bar step). */
3228 if (progbar == NULL)
3229 progbar = delayed_create_progress_dlg("Searching", title,
3230 FALSE, &stop_flag, &start_time, progbar_val);
3232 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
3233 when we update it, we have to run the GTK+ main loop to get it
3234 to repaint what's pending, and doing so may involve an "ioctl()"
3235 to see if there's any pending input from an X server, and doing
3236 that for every packet can be costly, especially on a big file. */
3237 if (count >= progbar_nextstep) {
3238 /* let's not divide by zero. I should never be started
3239 * with count == 0, so let's assert that
3241 g_assert(cf->count > 0);
3243 progbar_val = (gfloat) count / cf->count;
3245 if (progbar != NULL) {
3246 g_snprintf(status_str, sizeof(status_str),
3247 "%4u of %u packets", count, cf->count);
3248 update_progress_dlg(progbar, progbar_val, status_str);
3251 progbar_nextstep += progbar_quantum;
3255 /* Well, the user decided to abort the search. Go back to the
3256 frame where we started. */
3261 /* Go past the current frame. */
3262 if (dir == SD_BACKWARD) {
3263 /* Go on to the previous frame. */
3264 fdata = fdata->prev;
3265 if (fdata == NULL) {
3267 * XXX - other apps have a bit more of a detailed message
3268 * for this, and instead of offering "OK" and "Cancel",
3269 * they offer things such as "Continue" and "Cancel";
3270 * we need an API for popping up alert boxes with
3271 * {Verb} and "Cancel".
3274 if (prefs.gui_find_wrap)
3276 statusbar_push_temporary_msg("Search reached the beginning. Continuing at end.");
3277 fdata = cf->plist_end; /* wrap around */
3281 statusbar_push_temporary_msg("Search reached the beginning.");
3282 fdata = start_fd; /* stay on previous packet */
3286 /* Go on to the next frame. */
3287 fdata = fdata->next;
3288 if (fdata == NULL) {
3289 if (prefs.gui_find_wrap)
3291 statusbar_push_temporary_msg("Search reached the end. Continuing at beginning.");
3292 fdata = cf->plist_start; /* wrap around */
3296 statusbar_push_temporary_msg("Search reached the end.");
3297 fdata = start_fd; /* stay on previous packet */
3304 /* Is this packet in the display? */
3305 if (fdata->flags.passed_dfilter) {
3306 /* Yes. Does it match the search criterion? */
3307 result = (*match_function)(cf, fdata, criterion);
3308 if (result == MR_ERROR) {
3309 /* Error; our caller has reported the error. Go back to the frame
3310 where we started. */
3313 } else if (result == MR_MATCHED) {
3314 /* Yes. Go to the new frame. */
3320 if (fdata == start_fd) {
3321 /* We're back to the frame we were on originally, and that frame
3322 doesn't match the search filter. The search failed. */
3327 /* We're done scanning the packets; destroy the progress bar if it
3329 if (progbar != NULL)
3330 destroy_progress_dlg(progbar);
3333 if (new_fd != NULL) {
3334 /* Find and select */
3335 cf->search_in_progress = TRUE;
3336 row = new_packet_list_find_row_from_data(fdata, TRUE);
3337 cf->search_in_progress = FALSE;
3338 cf->search_pos = 0; /* Reset the position */
3340 /* We didn't find a row even though we know that a frame
3341 * exists that satifies the search criteria. This means that the
3342 * frame isn't being displayed currently so we can't select it. */
3343 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
3344 "%sEnd of capture exceeded!%s\n\n"
3345 "The capture file is probably not fully loaded.",
3346 simple_dialog_primary_start(), simple_dialog_primary_end());
3349 return TRUE; /* success */
3351 return FALSE; /* failure */
3355 cf_goto_frame(capture_file *cf, guint fnumber)
3359 for (fdata = cf->plist_start; fdata != NULL && fdata->num < fnumber; fdata = fdata->next)
3362 if (fdata == NULL) {
3363 /* we didn't find a packet with that packet number */
3364 statusbar_push_temporary_msg("There is no packet number %u.", fnumber);
3365 return FALSE; /* we failed to go to that packet */
3367 if (!fdata->flags.passed_dfilter) {
3368 /* that packet currently isn't displayed */
3369 /* XXX - add it to the set of displayed packets? */
3370 statusbar_push_temporary_msg("Packet number %u isn't displayed.", fnumber);
3371 return FALSE; /* we failed to go to that packet */
3374 new_packet_list_find_row_from_data(fdata, TRUE);
3375 return TRUE; /* we got to that packet */
3381 /* Find and select */
3382 new_packet_list_select_first_row();
3383 return TRUE; /* we got to that packet */
3387 cf_goto_bottom_frame()
3389 /* Find and select */
3390 new_packet_list_select_last_row();
3391 return TRUE; /* we got to that packet */
3395 * Go to frame specified by currently selected protocol tree item.
3398 cf_goto_framenum(capture_file *cf)
3400 header_field_info *hfinfo;
3403 if (cf->finfo_selected) {
3404 hfinfo = cf->finfo_selected->hfinfo;
3406 if (hfinfo->type == FT_FRAMENUM) {
3407 framenum = fvalue_get_uinteger(&cf->finfo_selected->value);
3409 return cf_goto_frame(cf, framenum);
3416 /* Select the packet on a given row. */
3418 cf_select_packet(capture_file *cf, int row)
3422 /* Get the frame data struct pointer for this frame */
3423 fdata = new_packet_list_get_row_data(row);
3425 if (fdata == NULL) {
3426 /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
3427 the first entry is added to it by "real_insert_row()", that row
3428 is selected (see "real_insert_row()", in "gtk/gtkclist.c", in both
3429 our version and the vanilla GTK+ version).
3431 This means that a "select-row" signal is emitted; this causes
3432 "packet_list_select_cb()" to be called, which causes "cf_select_packet()"
3435 "cf_select_packet()" fetches, above, the data associated with the
3436 row that was selected; however, as "gtk_clist_append()", which
3437 called "real_insert_row()", hasn't yet returned, we haven't yet
3438 associated any data with that row, so we get back a null pointer.
3440 We can't assume that there's only one frame in the frame list,
3441 either, as we may be filtering the display.
3443 We therefore assume that, if "row" is 0, i.e. the first row
3444 is being selected, and "cf->first_displayed" equals
3445 "cf->last_displayed", i.e. there's only one frame being
3446 displayed, that frame is the frame we want.
3448 This means we have to set "cf->first_displayed" and
3449 "cf->last_displayed" before adding the row to the
3450 GtkCList; see the comment in "add_packet_to_packet_list()". */
3452 if (row == 0 && cf->first_displayed == cf->last_displayed)
3453 fdata = cf->first_displayed;
3456 /* If fdata _still_ isn't set simply give up. */
3457 if (fdata == NULL) {
3461 /* Get the data in that frame. */
3462 if (!cf_read_frame (cf, fdata)) {
3466 /* Record that this frame is the current frame. */
3467 cf->current_frame = fdata;
3468 cf->current_row = row;
3470 /* Create the logical protocol tree. */
3471 if (cf->edt != NULL)
3472 epan_dissect_free(cf->edt);
3474 /* We don't need the columns here. */
3475 cf->edt = epan_dissect_new(TRUE, TRUE);
3477 tap_build_interesting(cf->edt);
3478 epan_dissect_run(cf->edt, &cf->pseudo_header, cf->pd, cf->current_frame,
3481 dfilter_macro_build_ftv_cache(cf->edt->tree);
3483 cf_callback_invoke(cf_cb_packet_selected, cf);
3486 /* Unselect the selected packet, if any. */
3488 cf_unselect_packet(capture_file *cf)
3490 /* Destroy the epan_dissect_t for the unselected packet. */
3491 if (cf->edt != NULL) {
3492 epan_dissect_free(cf->edt);
3496 /* No packet is selected. */
3497 cf->current_frame = NULL;
3498 cf->current_row = 0;
3500 cf_callback_invoke(cf_cb_packet_unselected, cf);
3502 /* No protocol tree means no selected field. */
3503 cf_unselect_field(cf);
3506 /* Unset the selected protocol tree field, if any. */
3508 cf_unselect_field(capture_file *cf)
3510 cf->finfo_selected = NULL;
3512 cf_callback_invoke(cf_cb_field_unselected, cf);
3516 * Mark a particular frame.
3519 cf_mark_frame(capture_file *cf, frame_data *frame)
3521 if (! frame->flags.marked) {
3522 frame->flags.marked = TRUE;
3523 if (cf->count > cf->marked_count)
3529 * Unmark a particular frame.
3532 cf_unmark_frame(capture_file *cf, frame_data *frame)
3534 if (frame->flags.marked) {
3535 frame->flags.marked = FALSE;
3536 if (cf->marked_count > 0)
3542 * Ignore a particular frame.
3545 cf_ignore_frame(capture_file *cf, frame_data *frame)
3547 if (! frame->flags.ignored) {
3548 frame->flags.ignored = TRUE;
3549 if (cf->count > cf->ignored_count)
3550 cf->ignored_count++;
3555 * Un-ignore a particular frame.
3558 cf_unignore_frame(capture_file *cf, frame_data *frame)
3560 if (frame->flags.ignored) {
3561 frame->flags.ignored = FALSE;
3562 if (cf->ignored_count > 0)
3563 cf->ignored_count--;
3570 } save_callback_args_t;
3573 * Save a capture to a file, in a particular format, saving either
3574 * all packets, all currently-displayed packets, or all marked packets.
3576 * Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
3577 * up a message box for the failure.
3580 save_packet(capture_file *cf _U_, frame_data *fdata,
3581 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
3584 save_callback_args_t *args = argsp;
3585 struct wtap_pkthdr hdr;
3588 /* init the wtap header for saving */
3589 hdr.ts.secs = fdata->abs_ts.secs;
3590 hdr.ts.nsecs = fdata->abs_ts.nsecs;
3591 hdr.caplen = fdata->cap_len;
3592 hdr.len = fdata->pkt_len;
3593 hdr.pkt_encap = fdata->lnk_t;
3595 /* and save the packet */
3596 if (!wtap_dump(args->pdh, &hdr, pseudo_header, pd, &err)) {
3597 cf_write_failure_alert_box(args->fname, err);
3604 * Can this capture file be saved in any format except by copying the raw data?
3607 cf_can_save_as(capture_file *cf)
3611 for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) {
3612 /* To save a file with Wiretap, Wiretap has to handle that format,
3613 and its code to handle that format must be able to write a file
3614 with this file's encapsulation type. */
3615 if (wtap_dump_can_open(ft) && wtap_dump_can_write_encap(ft, cf->lnk_t)) {
3616 /* OK, we can write it out in this type. */
3621 /* No, we couldn't save it in any format. */
3626 cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_format, gboolean compressed)
3628 gchar *from_filename;
3632 save_callback_args_t callback_args;
3634 cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
3636 /* don't write over an existing file. */
3637 /* this should've been already checked by our caller, just to be sure... */
3638 if (file_exists(fname)) {
3639 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3640 "%sCapture file: \"%s\" already exists!%s\n\n"
3641 "Please choose a different filename.",
3642 simple_dialog_primary_start(), fname, simple_dialog_primary_end());
3646 packet_range_process_init(range);
3648 if (packet_range_process_all(range) && save_format == cf->cd_t) {
3649 /* We're not filtering packets, and we're saving it in the format
3650 it's already in, so we can just move or copy the raw data. */
3652 if (cf->is_tempfile) {
3653 /* The file being saved is a temporary file from a live
3654 capture, so it doesn't need to stay around under that name;
3655 first, try renaming the capture buffer file to the new name. */
3657 if (ws_rename(cf->filename, fname) == 0) {
3658 /* That succeeded - there's no need to copy the source file. */
3659 from_filename = NULL;
3662 if (errno == EXDEV) {
3663 /* They're on different file systems, so we have to copy the
3666 from_filename = cf->filename;
3668 /* The rename failed, but not because they're on different
3669 file systems - put up an error message. (Or should we
3670 just punt and try to copy? The only reason why I'd
3671 expect the rename to fail and the copy to succeed would
3672 be if we didn't have permission to remove the file from
3673 the temporary directory, and that might be fixable - but
3674 is it worth requiring the user to go off and fix it?) */
3675 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3676 file_rename_error_message(errno), fname);
3682 from_filename = cf->filename;
3685 /* It's a permanent file, so we should copy it, and not remove the
3688 from_filename = cf->filename;
3692 /* Copy the file, if we haven't moved it. */
3693 if (!copy_file_binary_mode(from_filename, fname))
3697 /* Either we're filtering packets, or we're saving in a different
3698 format; we can't do that by copying or moving the capture file,
3699 we have to do it by writing the packets out in Wiretap. */
3700 pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap,
3703 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
3707 /* XXX - we let the user save a subset of the packets.
3709 If we do that, should we make that file the current file? If so,
3710 it means we can no longer get at the other packets. What does
3713 /* Iterate through the list of packets, processing the packets we were
3716 XXX - we've already called "packet_range_process_init(range)", but
3717 "process_specified_packets()" will do it again. Fortunately,
3718 that's harmless in this case, as we haven't done anything to
3719 "range" since we initialized it. */
3720 callback_args.pdh = pdh;
3721 callback_args.fname = fname;
3722 switch (process_specified_packets(cf, range, "Saving", "selected packets",
3723 TRUE, save_packet, &callback_args)) {
3726 /* Completed successfully. */
3730 /* The user decided to abort the saving.
3731 XXX - remove the output file? */
3735 /* Error while saving. */
3736 wtap_dump_close(pdh, &err);
3740 if (!wtap_dump_close(pdh, &err)) {
3741 cf_close_failure_alert_box(fname, err);
3746 cf_callback_invoke(cf_cb_file_save_finished, NULL);
3748 if (packet_range_process_all(range)) {
3749 /* We saved the entire capture, not just some packets from it.
3750 Open and read the file we saved it to.
3752 XXX - this is somewhat of a waste; we already have the
3753 packets, all this gets us is updated file type information
3754 (which we could just stuff into "cf"), and having the new
3755 file be the one we have opened and from which we're reading
3756 the data, and it means we have to spend time opening and
3757 reading the file, which could be a significant amount of
3758 time if the file is large. */
3759 cf->user_saved = TRUE;
3761 if ((cf_open(cf, fname, FALSE, &err)) == CF_OK) {
3762 /* XXX - report errors if this fails?
3763 What should we return if it fails or is aborted? */
3765 switch (cf_read(cf, TRUE)) {
3769 /* Just because we got an error, that doesn't mean we were unable
3770 to read any of the file; we handle what we could get from the
3774 case CF_READ_ABORTED:
3775 /* The user bailed out of re-reading the capture file; the
3776 capture file has been closed - just return (without
3777 changing any menu settings; "cf_close()" set them
3778 correctly for the "no capture file open" state). */
3781 cf_callback_invoke(cf_cb_file_save_reload_finished, cf);
3787 cf_callback_invoke(cf_cb_file_save_failed, NULL);
3792 cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
3793 gboolean for_writing, int file_type)
3796 /* Wiretap error. */
3799 case WTAP_ERR_NOT_REGULAR_FILE:
3800 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3801 "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
3805 case WTAP_ERR_RANDOM_OPEN_PIPE:
3806 /* Seen only when opening a capture file for reading. */
3807 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3808 "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.",
3812 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
3813 /* Seen only when opening a capture file for reading. */
3814 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3815 "The file \"%s\" isn't a capture file in a format Wireshark understands.",
3819 case WTAP_ERR_UNSUPPORTED:
3820 /* Seen only when opening a capture file for reading. */
3821 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3822 "The file \"%s\" isn't a capture file in a format Wireshark understands.\n"
3824 filename, err_info);
3828 case WTAP_ERR_CANT_WRITE_TO_PIPE:
3829 /* Seen only when opening a capture file for writing. */
3830 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3831 "The file \"%s\" is a pipe, and %s capture files can't be "
3832 "written to a pipe.",
3833 filename, wtap_file_type_string(file_type));
3836 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
3837 /* Seen only when opening a capture file for writing. */
3838 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3839 "Wireshark doesn't support writing capture files in that format.");
3842 case WTAP_ERR_UNSUPPORTED_ENCAP:
3844 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3845 "Wireshark can't save this capture in that format.");
3847 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3848 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.\n"
3850 filename, err_info);
3855 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
3857 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3858 "Wireshark can't save this capture in that format.");
3860 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3861 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.",
3866 case WTAP_ERR_BAD_RECORD:
3867 /* Seen only when opening a capture file for reading. */
3868 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3869 "The file \"%s\" appears to be damaged or corrupt.\n"
3871 filename, err_info);
3875 case WTAP_ERR_CANT_OPEN:
3877 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3878 "The file \"%s\" could not be created for some unknown reason.",
3881 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3882 "The file \"%s\" could not be opened for some unknown reason.",
3887 case WTAP_ERR_SHORT_READ:
3888 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3889 "The file \"%s\" appears to have been cut short"
3890 " in the middle of a packet or other data.",
3894 case WTAP_ERR_SHORT_WRITE:
3895 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3896 "A full header couldn't be written to the file \"%s\".",
3900 case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
3901 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3902 "Gzip compression not supported by this file type.");
3906 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3907 "The file \"%s\" could not be %s: %s.",
3909 for_writing ? "created" : "opened",
3910 wtap_strerror(err));
3915 open_failure_alert_box(filename, err, for_writing);
3920 file_rename_error_message(int err)
3923 static char errmsg_errno[1024+1];
3928 errmsg = "The path to the file \"%s\" doesn't exist.";
3932 errmsg = "You don't have permission to move the capture file to \"%s\".";
3936 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
3937 "The file \"%%s\" could not be moved: %s.",
3938 wtap_strerror(err));
3939 errmsg = errmsg_errno;
3946 cf_write_failure_alert_box(const char *filename, int err)
3949 /* Wiretap error. */
3950 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3951 "An error occurred while writing to the file \"%s\": %s.",
3952 filename, wtap_strerror(err));
3955 write_failure_alert_box(filename, err);
3959 /* Check for write errors - if the file is being written to an NFS server,
3960 a write error may not show up until the file is closed, as NFS clients
3961 might not send writes to the server until the "write()" call finishes,
3962 so that the write may fail on the server but the "write()" may succeed. */
3964 cf_close_failure_alert_box(const char *filename, int err)
3967 /* Wiretap error. */
3970 case WTAP_ERR_CANT_CLOSE:
3971 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3972 "The file \"%s\" couldn't be closed for some unknown reason.",
3976 case WTAP_ERR_SHORT_WRITE:
3977 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3978 "Not all the packets could be written to the file \"%s\".",
3983 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3984 "An error occurred while closing the file \"%s\": %s.",
3985 filename, wtap_strerror(err));
3990 We assume that a close error from the OS is really a write error. */
3991 write_failure_alert_box(filename, err);
3995 /* Reload the current capture file. */
3997 cf_reload(capture_file *cf) {
3999 gboolean is_tempfile;
4002 /* If the file could be opened, "cf_open()" calls "cf_close()"
4003 to get rid of state for the old capture file before filling in state
4004 for the new capture file. "cf_close()" will remove the file if
4005 it's a temporary file; we don't want that to happen (for one thing,
4006 it'd prevent subsequent reopens from working). Remember whether it's
4007 a temporary file, mark it as not being a temporary file, and then
4008 reopen it as the type of file it was.
4010 Also, "cf_close()" will free "cf->filename", so we must make
4011 a copy of it first. */
4012 filename = g_strdup(cf->filename);
4013 is_tempfile = cf->is_tempfile;
4014 cf->is_tempfile = FALSE;
4015 if (cf_open(cf, filename, is_tempfile, &err) == CF_OK) {
4016 switch (cf_read(cf, FALSE)) {
4020 /* Just because we got an error, that doesn't mean we were unable
4021 to read any of the file; we handle what we could get from the
4025 case CF_READ_ABORTED:
4026 /* The user bailed out of re-reading the capture file; the
4027 capture file has been closed - just free the capture file name
4028 string and return (without changing the last containing
4034 /* The open failed, so "cf->is_tempfile" wasn't set to "is_tempfile".
4035 Instead, the file was left open, so we should restore "cf->is_tempfile"
4038 XXX - change the menu? Presumably "cf_open()" will do that;
4039 make sure it does! */
4040 cf->is_tempfile = is_tempfile;
4042 /* "cf_open()" made a copy of the file name we handed it, so
4043 we should free up our copy. */
4053 * indent-tabs-mode: nil
4056 * ex: set shiftwidth=2 tabstop=8 expandtab
4057 * :indentSize=2:tabSize=8:noTabs=true: