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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
44 #include <wsutil/tempfile.h>
45 #include <wsutil/file_util.h>
46 #include <wsutil/filesystem.h>
48 #include <wiretap/merge.h>
50 #include <epan/exceptions.h>
51 #include <epan/epan-int.h>
52 #include <epan/epan.h>
53 #include <epan/column.h>
54 #include <epan/packet.h>
55 #include <epan/column-utils.h>
56 #include <epan/expert.h>
57 #include <epan/prefs.h>
58 #include <epan/dfilter/dfilter.h>
59 #include <epan/epan_dissect.h>
61 #include <epan/dissectors/packet-data.h>
62 #include <epan/dissectors/packet-ber.h>
63 #include <epan/timestamp.h>
64 #include <epan/dfilter/dfilter-macro.h>
65 #include <epan/strutil.h>
66 #include <epan/addr_resolv.h>
69 #include "color_filters.h"
73 #include "frame_tvbuff.h"
75 #include "ui/alert_box.h"
76 #include "ui/simple_dialog.h"
77 #include "ui/main_statusbar.h"
78 #include "ui/progress_dlg.h"
79 #include "ui/ui_util.h"
81 #include "version_info.h"
83 /* Needed for addrinfo */
84 #ifdef HAVE_SYS_TYPES_H
85 # include <sys/types.h>
88 #ifdef HAVE_SYS_SOCKET_H
89 #include <sys/socket.h>
92 #ifdef HAVE_NETINET_IN_H
93 # include <netinet/in.h>
100 #ifdef HAVE_WINSOCK2_H
101 # include <winsock2.h>
104 #if defined(_WIN32) && defined(INET6)
105 # include <ws2tcpip.h>
109 gboolean auto_scroll_live;
112 static void cf_reset_state(capture_file *cf);
114 static int read_packet(capture_file *cf, dfilter_t *dfcode, epan_dissect_t *edt,
115 column_info *cinfo, gint64 offset);
117 static void rescan_packets(capture_file *cf, const char *action, const char *action_item, gboolean redissect);
124 static match_result match_protocol_tree(capture_file *cf, frame_data *fdata,
126 static void match_subtree_text(proto_node *node, gpointer data);
127 static match_result match_summary_line(capture_file *cf, frame_data *fdata,
129 static match_result match_narrow_and_wide(capture_file *cf, frame_data *fdata,
131 static match_result match_narrow(capture_file *cf, frame_data *fdata,
133 static match_result match_wide(capture_file *cf, frame_data *fdata,
135 static match_result match_binary(capture_file *cf, frame_data *fdata,
137 static match_result match_dfilter(capture_file *cf, frame_data *fdata,
139 static match_result match_marked(capture_file *cf, frame_data *fdata,
141 static match_result match_time_reference(capture_file *cf, frame_data *fdata,
143 static gboolean find_packet(capture_file *cf,
144 match_result (*match_function)(capture_file *, frame_data *, void *),
145 void *criterion, search_direction dir);
147 static const char *cf_get_user_packet_comment(capture_file *cf, const frame_data *fd);
149 static void cf_open_failure_alert_box(const char *filename, int err,
150 gchar *err_info, gboolean for_writing,
152 static void cf_rename_failure_alert_box(const char *filename, int err);
153 static void cf_close_failure_alert_box(const char *filename, int err);
154 static void ref_time_packets(capture_file *cf);
155 /* Update the progress bar this many times when reading a file. */
156 #define N_PROGBAR_UPDATES 100
157 /* We read around 200k/100ms don't update the progress bar more often than that */
158 #define MIN_QUANTUM 200000
159 #define MIN_NUMBER_OF_PACKET 1500
162 * We could probably use g_signal_...() instead of the callbacks below but that
163 * would require linking our CLI programs to libgobject and creating an object
164 * instance for the signals.
167 cf_callback_t cb_fct;
169 } cf_callback_data_t;
171 static GList *cf_callbacks = NULL;
174 cf_callback_invoke(int event, gpointer data)
176 cf_callback_data_t *cb;
177 GList *cb_item = cf_callbacks;
179 /* there should be at least one interested */
180 g_assert(cb_item != NULL);
182 while (cb_item != NULL) {
183 cb = (cf_callback_data_t *)cb_item->data;
184 cb->cb_fct(event, data, cb->user_data);
185 cb_item = g_list_next(cb_item);
191 cf_callback_add(cf_callback_t func, gpointer user_data)
193 cf_callback_data_t *cb;
195 cb = g_new(cf_callback_data_t,1);
197 cb->user_data = user_data;
199 cf_callbacks = g_list_prepend(cf_callbacks, cb);
203 cf_callback_remove(cf_callback_t func)
205 cf_callback_data_t *cb;
206 GList *cb_item = cf_callbacks;
208 while (cb_item != NULL) {
209 cb = (cf_callback_data_t *)cb_item->data;
210 if (cb->cb_fct == func) {
211 cf_callbacks = g_list_remove(cf_callbacks, cb);
215 cb_item = g_list_next(cb_item);
218 g_assert_not_reached();
222 cf_timestamp_auto_precision(capture_file *cf)
225 int prec = timestamp_get_precision();
228 /* don't try to get the file's precision if none is opened */
229 if (cf->state == FILE_CLOSED) {
233 /* if we are in auto mode, set precision of current file */
234 if (prec == TS_PREC_AUTO ||
235 prec == TS_PREC_AUTO_SEC ||
236 prec == TS_PREC_AUTO_DSEC ||
237 prec == TS_PREC_AUTO_CSEC ||
238 prec == TS_PREC_AUTO_MSEC ||
239 prec == TS_PREC_AUTO_USEC ||
240 prec == TS_PREC_AUTO_NSEC)
242 switch(wtap_file_tsprecision(cf->wth)) {
243 case(WTAP_FILE_TSPREC_SEC):
244 timestamp_set_precision(TS_PREC_AUTO_SEC);
246 case(WTAP_FILE_TSPREC_DSEC):
247 timestamp_set_precision(TS_PREC_AUTO_DSEC);
249 case(WTAP_FILE_TSPREC_CSEC):
250 timestamp_set_precision(TS_PREC_AUTO_CSEC);
252 case(WTAP_FILE_TSPREC_MSEC):
253 timestamp_set_precision(TS_PREC_AUTO_MSEC);
255 case(WTAP_FILE_TSPREC_USEC):
256 timestamp_set_precision(TS_PREC_AUTO_USEC);
258 case(WTAP_FILE_TSPREC_NSEC):
259 timestamp_set_precision(TS_PREC_AUTO_NSEC);
262 g_assert_not_reached();
265 /* Set the column widths of those columns that show the time in
266 "command-line-specified" format. */
267 for (i = 0; i < cf->cinfo.num_cols; i++) {
268 if (col_has_time_fmt(&cf->cinfo, i)) {
269 packet_list_resize_column(i);
275 cf_get_computed_elapsed(capture_file *cf)
277 return cf->computed_elapsed;
281 * GLIB_CHECK_VERSION(2,28,0) adds g_get_real_time which could minimize or
284 static void compute_elapsed(capture_file *cf, GTimeVal *start_time)
289 g_get_current_time(&time_now);
291 delta_time = (time_now.tv_sec - start_time->tv_sec) * 1e6 +
292 time_now.tv_usec - start_time->tv_usec;
294 cf->computed_elapsed = (gulong) (delta_time / 1000); /* ms */
297 static const nstime_t *
298 ws_get_frame_ts(void *data, guint32 frame_num)
300 capture_file *cf = (capture_file *) data;
302 if (cf->prev_dis && cf->prev_dis->num == frame_num)
303 return &cf->prev_dis->abs_ts;
305 if (cf->prev_cap && cf->prev_cap->num == frame_num)
306 return &cf->prev_cap->abs_ts;
309 frame_data *fd = frame_data_sequence_find(cf->frames, frame_num);
311 return (fd) ? &fd->abs_ts : NULL;
318 ws_get_user_comment(void *data, const frame_data *fd)
320 capture_file *cf = (capture_file *) data;
322 return cf_get_user_packet_comment(cf, fd);
326 ws_epan_new(capture_file *cf)
328 epan_t *epan = epan_new();
331 epan->get_frame_ts = ws_get_frame_ts;
332 epan->get_interface_name = cap_file_get_interface_name;
333 epan->get_user_comment = ws_get_user_comment;
339 cf_open(capture_file *cf, const char *fname, unsigned int type, gboolean is_tempfile, int *err)
344 wth = wtap_open_offline(fname, type, err, &err_info, TRUE);
348 /* The open succeeded. Close whatever capture file we had open,
349 and fill in the information for this file. */
352 /* XXX - we really want to initialize this after we've read all
353 the packets, so we know how much we'll ultimately need. */
354 buffer_init(&cf->buf, 1500);
356 /* Create new epan session for dissection.
357 * (The old one was freed in cf_close().)
359 cf->epan = ws_epan_new(cf);
361 /* We're about to start reading the file. */
362 cf->state = FILE_READ_IN_PROGRESS;
367 /* Set the file name because we need it to set the follow stream filter.
368 XXX - is that still true? We need it for other reasons, though,
370 cf->filename = g_strdup(fname);
372 /* Indicate whether it's a permanent or temporary file. */
373 cf->is_tempfile = is_tempfile;
375 /* No user changes yet. */
376 cf->unsaved_changes = FALSE;
378 cf->computed_elapsed = 0;
380 cf->cd_t = wtap_file_type_subtype(cf->wth);
381 cf->linktypes = g_array_sized_new(FALSE, FALSE, (guint) sizeof(int), 1);
383 cf->packet_comment_count = 0;
384 cf->displayed_count = 0;
385 cf->marked_count = 0;
386 cf->ignored_count = 0;
387 cf->ref_time_count = 0;
388 cf->drops_known = FALSE;
390 cf->snap = wtap_snapshot_length(cf->wth);
392 /* Snapshot length not known. */
393 cf->has_snap = FALSE;
394 cf->snap = WTAP_MAX_PACKET_SIZE;
398 /* Allocate a frame_data_sequence for the frames in this file */
399 cf->frames = new_frame_data_sequence();
401 nstime_set_zero(&cf->elapsed_time);
407 /* Adjust timestamp precision if auto is selected, col width will be adjusted */
408 cf_timestamp_auto_precision(cf);
410 packet_list_queue_draw();
411 cf_callback_invoke(cf_cb_file_opened, cf);
413 if (cf->cd_t == WTAP_FILE_TYPE_SUBTYPE_BER) {
414 /* tell the BER dissector the file name */
415 ber_set_filename(cf->filename);
418 wtap_set_cb_new_ipv4(cf->wth, add_ipv4_name);
419 wtap_set_cb_new_ipv6(cf->wth, (wtap_new_ipv6_callback_t) add_ipv6_name);
424 cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
429 * Add an encapsulation type to cf->linktypes.
432 cf_add_encapsulation_type(capture_file *cf, int encap)
436 for (i = 0; i < cf->linktypes->len; i++) {
437 if (g_array_index(cf->linktypes, gint, i) == encap)
438 return; /* it's already there */
440 /* It's not already there - add it. */
441 g_array_append_val(cf->linktypes, encap);
445 * Reset the state for the currently closed file, but don't do the
446 * UI callbacks; this is for use in "cf_open()", where we don't
447 * want the UI to go from "file open" to "file closed" back to
448 * "file open", we want it to go from "old file open" to "new file
449 * open and being read".
451 * XXX - currently, cf_open() calls cf_close(), rather than
455 cf_reset_state(capture_file *cf)
457 /* Die if we're in the middle of reading a file. */
458 g_assert(cf->state != FILE_READ_IN_PROGRESS);
464 /* We have no file open... */
465 if (cf->filename != NULL) {
466 /* If it's a temporary file, remove it. */
468 ws_unlink(cf->filename);
469 g_free(cf->filename);
472 /* ...which means we have no changes to that file to save. */
473 cf->unsaved_changes = FALSE;
475 /* Free up the packet buffer. */
476 buffer_free(&cf->buf);
478 dfilter_free(cf->rfcode);
480 if (cf->frames != NULL) {
481 free_frame_data_sequence(cf->frames);
484 #ifdef WANT_PACKET_EDITOR
485 if (cf->edited_frames) {
486 g_tree_destroy(cf->edited_frames);
487 cf->edited_frames = NULL;
490 if (cf->frames_user_comments) {
491 g_tree_destroy(cf->frames_user_comments);
492 cf->frames_user_comments = NULL;
494 cf_unselect_packet(cf); /* nothing to select */
495 cf->first_displayed = 0;
496 cf->last_displayed = 0;
498 /* No frames, no frame selected, no field in that frame selected. */
500 cf->current_frame = 0;
502 cf->finfo_selected = NULL;
504 /* No frame link-layer types, either. */
505 g_array_free(cf->linktypes, TRUE);
506 cf->linktypes = NULL;
508 /* Clear the packet list. */
509 packet_list_freeze();
514 nstime_set_zero(&cf->elapsed_time);
516 reset_tap_listeners();
518 /* We have no file open. */
519 cf->state = FILE_CLOSED;
522 /* Reset everything to a pristine state */
524 cf_close(capture_file *cf)
526 if (cf->state != FILE_CLOSED) {
527 cf_callback_invoke(cf_cb_file_closing, cf);
529 /* close things, if not already closed before */
530 color_filters_cleanup();
535 cf_callback_invoke(cf_cb_file_closed, cf);
540 calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos, gchar *status_str, gulong status_size)
544 progbar_val = (gfloat) file_pos / (gfloat) size;
545 if (progbar_val > 1.0) {
547 /* The file probably grew while we were reading it.
548 * Update file size, and try again.
550 size = wtap_file_size(cf->wth, NULL);
553 progbar_val = (gfloat) file_pos / (gfloat) size;
555 /* If it's still > 1, either "wtap_file_size()" failed (in which
556 * case there's not much we can do about it), or the file
557 * *shrank* (in which case there's not much we can do about
558 * it); just clip the progress value at 1.0.
560 if (progbar_val > 1.0f)
564 g_snprintf(status_str, status_size,
565 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
566 file_pos / 1024, size / 1024);
572 cf_read(capture_file *cf, gboolean reloading)
577 progdlg_t *progbar = NULL;
582 volatile gboolean create_proto_tree;
586 /* Compile the current display filter.
587 * We assume this will not fail since cf->dfilter is only set in
588 * cf_filter IFF the filter was valid.
590 compiled = dfilter_compile(cf->dfilter, &dfcode);
591 g_assert(!cf->dfilter || (compiled && dfcode));
593 /* Get the union of the flags for all tap listeners. */
594 tap_flags = union_of_tap_listener_flags();
596 (dfcode != NULL || have_filtering_tap_listeners() || (tap_flags & TL_REQUIRES_PROTO_TREE));
598 reset_tap_listeners();
600 name_ptr = g_filename_display_basename(cf->filename);
603 cf_callback_invoke(cf_cb_file_reload_started, cf);
605 cf_callback_invoke(cf_cb_file_read_started, cf);
607 /* Record whether the file is compressed.
608 XXX - do we know this at open time? */
609 cf->iscompressed = wtap_iscompressed(cf->wth);
611 /* The packet list window will be empty until the file is completly loaded */
612 packet_list_freeze();
615 g_get_current_time(&start_time);
617 epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
621 int displayed_once = 0;
629 gint64 progbar_quantum;
630 gint64 progbar_nextstep;
632 gchar status_str[100];
636 cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
638 /* Find the size of the file. */
639 size = wtap_file_size(cf->wth, NULL);
641 /* Update the progress bar when it gets to this value. */
642 progbar_nextstep = 0;
643 /* When we reach the value that triggers a progress bar update,
644 bump that value by this amount. */
646 progbar_quantum = size/N_PROGBAR_UPDATES;
647 if (progbar_quantum < MIN_QUANTUM)
648 progbar_quantum = MIN_QUANTUM;
652 while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
655 file_pos = wtap_read_so_far(cf->wth);
657 /* Create the progress bar if necessary.
658 * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
660 if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)) {
661 progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
663 progbar = delayed_create_progress_dlg(cf->window, "Reloading", name_ptr,
664 TRUE, &stop_flag, &start_time, progbar_val);
666 progbar = delayed_create_progress_dlg(cf->window, "Loading", name_ptr,
667 TRUE, &stop_flag, &start_time, progbar_val);
670 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
671 when we update it, we have to run the GTK+ main loop to get it
672 to repaint what's pending, and doing so may involve an "ioctl()"
673 to see if there's any pending input from an X server, and doing
674 that for every packet can be costly, especially on a big file. */
675 if (file_pos >= progbar_nextstep) {
676 if (progbar != NULL) {
677 progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
678 /* update the packet bar content on the first run or frequently on very large files */
680 if (progbar_quantum > 500000 || displayed_once == 0) {
681 if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->count != 0) {
683 packets_bar_update();
686 #endif /* HAVE_LIBPCAP */
687 update_progress_dlg(progbar, progbar_val, status_str);
689 progbar_nextstep += progbar_quantum;
694 /* Well, the user decided to abort the read. He/She will be warned and
695 it might be enough for him/her to work with the already loaded
697 This is especially true for very large capture files, where you don't
698 want to wait loading the whole file (which may last minutes or even
699 hours even on fast machines) just to see that it was the wrong file. */
702 read_packet(cf, dfcode, &edt, cinfo, data_offset);
705 CATCH(OutOfMemoryError) {
706 simple_message_box(ESD_TYPE_ERROR, NULL,
707 "Some infos / workarounds can be found at:\n"
708 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
709 "Sorry, but Wireshark has run out of memory and has to terminate now!");
711 /* Could we close the current capture and free up memory from that? */
713 /* we have to terminate, as we cannot recover from the memory error */
719 /* Free the display name */
722 /* Cleanup and release all dfilter resources */
723 if (dfcode != NULL) {
724 dfilter_free(dfcode);
727 epan_dissect_cleanup(&edt);
729 /* We're done reading the file; destroy the progress bar if it was created. */
731 destroy_progress_dlg(progbar);
733 /* We're done reading sequentially through the file. */
734 cf->state = FILE_READ_DONE;
736 /* Close the sequential I/O side, to free up memory it requires. */
737 wtap_sequential_close(cf->wth);
739 /* Allow the protocol dissectors to free up memory that they
740 * don't need after the sequential run-through of the packets. */
741 postseq_cleanup_all_protocols();
743 /* compute the time it took to load the file */
744 compute_elapsed(cf, &start_time);
746 /* Set the file encapsulation type now; we don't know what it is until
747 we've looked at all the packets, as we don't know until then whether
748 there's more than one type (and thus whether it's
749 WTAP_ENCAP_PER_PACKET). */
750 cf->lnk_t = wtap_file_encap(cf->wth);
752 cf->current_frame = frame_data_sequence_find(cf->frames, cf->first_displayed);
757 cf_callback_invoke(cf_cb_file_reload_finished, cf);
759 cf_callback_invoke(cf_cb_file_read_finished, cf);
761 /* If we have any displayed packets to select, select the first of those
762 packets by making the first row the selected row. */
763 if (cf->first_displayed != 0) {
764 packet_list_select_first_row();
768 simple_message_box(ESD_TYPE_WARN, NULL,
769 "The remaining packets in the file were discarded.\n"
771 "As a lot of packets from the original file will be missing,\n"
772 "remember to be careful when saving the current content to a file.\n",
773 "File loading was cancelled!");
774 return CF_READ_ERROR;
778 /* Put up a message box noting that the read failed somewhere along
779 the line. Don't throw out the stuff we managed to read, though,
783 case WTAP_ERR_UNSUPPORTED:
784 simple_error_message_box(
785 "The capture file contains record data that Wireshark doesn't support.\n(%s)",
790 case WTAP_ERR_UNSUPPORTED_ENCAP:
791 simple_error_message_box(
792 "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
797 case WTAP_ERR_CANT_READ:
798 simple_error_message_box(
799 "An attempt to read from the capture file failed for"
800 " some unknown reason.");
803 case WTAP_ERR_SHORT_READ:
804 simple_error_message_box(
805 "The capture file appears to have been cut short"
806 " in the middle of a packet.");
809 case WTAP_ERR_BAD_FILE:
810 simple_error_message_box(
811 "The capture file appears to be damaged or corrupt.\n(%s)",
816 case WTAP_ERR_DECOMPRESS:
817 simple_error_message_box(
818 "The compressed capture file appears to be damaged or corrupt.\n"
824 simple_error_message_box(
825 "An error occurred while reading the"
826 " capture file: %s.", wtap_strerror(err));
829 return CF_READ_ERROR;
836 cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
839 int newly_displayed_packets = 0;
842 gboolean create_proto_tree;
846 /* Compile the current display filter.
847 * We assume this will not fail since cf->dfilter is only set in
848 * cf_filter IFF the filter was valid.
850 compiled = dfilter_compile(cf->dfilter, &dfcode);
851 g_assert(!cf->dfilter || (compiled && dfcode));
853 /* Get the union of the flags for all tap listeners. */
854 tap_flags = union_of_tap_listener_flags();
856 (dfcode != NULL || have_filtering_tap_listeners() || (tap_flags & TL_REQUIRES_PROTO_TREE));
860 packet_list_check_end();
861 /* Don't freeze/thaw the list when doing live capture */
862 /*packet_list_freeze();*/
864 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
866 epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
869 gint64 data_offset = 0;
872 cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
874 while (to_read != 0) {
875 wtap_cleareof(cf->wth);
876 if (!wtap_read(cf->wth, err, &err_info, &data_offset)) {
879 if (cf->state == FILE_READ_ABORTED) {
880 /* Well, the user decided to exit Wireshark. Break out of the
881 loop, and let the code below (which is called even if there
882 aren't any packets left to read) exit. */
885 if (read_packet(cf, dfcode, &edt, (column_info *) cinfo, data_offset) != -1) {
886 newly_displayed_packets++;
891 CATCH(OutOfMemoryError) {
892 simple_message_box(ESD_TYPE_ERROR, NULL,
893 "Some infos / workarounds can be found at:\n"
894 "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
895 "Sorry, but Wireshark has run out of memory and has to terminate now!");
897 /* Could we close the current capture and free up memory from that? */
898 return CF_READ_ABORTED;
900 /* we have to terminate, as we cannot recover from the memory error */
906 /* Update the file encapsulation; it might have changed based on the
907 packets we've read. */
908 cf->lnk_t = wtap_file_encap(cf->wth);
910 /* Cleanup and release all dfilter resources */
911 if (dfcode != NULL) {
912 dfilter_free(dfcode);
915 epan_dissect_cleanup(&edt);
917 /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u",
918 cf->count, cf->state, *err);*/
920 /* Don't freeze/thaw the list when doing live capture */
921 /*packet_list_thaw();*/
922 /* With the new packet list the first packet
923 * isn't automatically selected.
925 if (!cf->current_frame)
926 packet_list_select_first_row();
928 /* moving to the end of the packet list - if the user requested so and
929 we have some new packets. */
930 if (newly_displayed_packets && auto_scroll_live && cf->count != 0)
931 packet_list_moveto_end();
933 if (cf->state == FILE_READ_ABORTED) {
934 /* Well, the user decided to exit Wireshark. Return CF_READ_ABORTED
935 so that our caller can kill off the capture child process;
936 this will cause an EOF on the pipe from the child, so
937 "cf_finish_tail()" will be called, and it will clean up
939 return CF_READ_ABORTED;
940 } else if (*err != 0) {
941 /* We got an error reading the capture file.
942 XXX - pop up a dialog box instead? */
943 g_warning("Error \"%s\" while reading: \"%s\" (\"%s\")",
944 wtap_strerror(*err), err_info, cf->filename);
947 return CF_READ_ERROR;
953 cf_fake_continue_tail(capture_file *cf) {
954 cf->state = FILE_READ_DONE;
958 cf_finish_tail(capture_file *cf, int *err)
965 gboolean create_proto_tree;
969 /* Compile the current display filter.
970 * We assume this will not fail since cf->dfilter is only set in
971 * cf_filter IFF the filter was valid.
973 compiled = dfilter_compile(cf->dfilter, &dfcode);
974 g_assert(!cf->dfilter || (compiled && dfcode));
976 /* Get the union of the flags for all tap listeners. */
977 tap_flags = union_of_tap_listener_flags();
978 cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
980 (dfcode != NULL || have_filtering_tap_listeners() || (tap_flags & TL_REQUIRES_PROTO_TREE));
982 if (cf->wth == NULL) {
984 return CF_READ_ERROR;
987 packet_list_check_end();
988 /* Don't freeze/thaw the list when doing live capture */
989 /*packet_list_freeze();*/
991 epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
993 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
994 if (cf->state == FILE_READ_ABORTED) {
995 /* Well, the user decided to abort the read. Break out of the
996 loop, and let the code below (which is called even if there
997 aren't any packets left to read) exit. */
1000 read_packet(cf, dfcode, &edt, cinfo, data_offset);
1003 /* Cleanup and release all dfilter resources */
1004 if (dfcode != NULL) {
1005 dfilter_free(dfcode);
1008 epan_dissect_cleanup(&edt);
1010 /* Don't freeze/thaw the list when doing live capture */
1011 /*packet_list_thaw();*/
1013 if (cf->state == FILE_READ_ABORTED) {
1014 /* Well, the user decided to abort the read. We're only called
1015 when the child capture process closes the pipe to us (meaning
1016 it's probably exited), so we can just close the capture
1017 file; we return CF_READ_ABORTED so our caller can do whatever
1018 is appropriate when that happens. */
1020 return CF_READ_ABORTED;
1023 if (auto_scroll_live && cf->count != 0)
1024 packet_list_moveto_end();
1026 /* We're done reading sequentially through the file. */
1027 cf->state = FILE_READ_DONE;
1029 /* We're done reading sequentially through the file; close the
1030 sequential I/O side, to free up memory it requires. */
1031 wtap_sequential_close(cf->wth);
1033 /* Allow the protocol dissectors to free up memory that they
1034 * don't need after the sequential run-through of the packets. */
1035 postseq_cleanup_all_protocols();
1037 /* Update the file encapsulation; it might have changed based on the
1038 packets we've read. */
1039 cf->lnk_t = wtap_file_encap(cf->wth);
1041 /* Update the details in the file-set dialog, as the capture file
1042 * has likely grown since we first stat-ed it */
1043 fileset_update_file(cf->filename);
1046 /* We got an error reading the capture file.
1047 XXX - pop up a dialog box? */
1049 g_warning("Error \"%s\" while reading: \"%s\" (\"%s\")",
1050 wtap_strerror(*err), err_info, cf->filename);
1052 return CF_READ_ERROR;
1057 #endif /* HAVE_LIBPCAP */
1060 cf_get_display_name(capture_file *cf)
1064 /* Return a name to use in displays */
1065 if (!cf->is_tempfile) {
1066 /* Get the last component of the file name, and use that. */
1068 displayname = g_filename_display_basename(cf->filename);
1070 displayname=g_strdup("(No file)");
1073 /* The file we read is a temporary file from a live capture or
1074 a merge operation; we don't mention its name, but, if it's
1075 from a capture, give the source of the capture. */
1077 displayname = g_strdup(cf->source);
1079 displayname = g_strdup("(Untitled)");
1085 void cf_set_tempfile_source(capture_file *cf, gchar *source) {
1091 cf->source = g_strdup(source);
1093 cf->source = g_strdup("");
1097 const gchar *cf_get_tempfile_source(capture_file *cf) {
1105 /* XXX - use a macro instead? */
1107 cf_get_packet_count(capture_file *cf)
1112 /* XXX - use a macro instead? */
1114 cf_set_packet_count(capture_file *cf, int packet_count)
1116 cf->count = packet_count;
1119 /* XXX - use a macro instead? */
1121 cf_is_tempfile(capture_file *cf)
1123 return cf->is_tempfile;
1126 void cf_set_tempfile(capture_file *cf, gboolean is_tempfile)
1128 cf->is_tempfile = is_tempfile;
1132 /* XXX - use a macro instead? */
1133 void cf_set_drops_known(capture_file *cf, gboolean drops_known)
1135 cf->drops_known = drops_known;
1138 /* XXX - use a macro instead? */
1139 void cf_set_drops(capture_file *cf, guint32 drops)
1144 /* XXX - use a macro instead? */
1145 gboolean cf_get_drops_known(capture_file *cf)
1147 return cf->drops_known;
1150 /* XXX - use a macro instead? */
1151 guint32 cf_get_drops(capture_file *cf)
1156 void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
1158 cf->rfcode = rfcode;
1162 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
1163 epan_dissect_t *edt, dfilter_t *dfcode, column_info *cinfo,
1164 struct wtap_pkthdr *phdr, const guint8 *buf, gboolean add_to_packet_list)
1168 frame_data_set_before_dissect(fdata, &cf->elapsed_time,
1169 &cf->ref, cf->prev_dis);
1170 cf->prev_cap = fdata;
1172 if (dfcode != NULL) {
1173 epan_dissect_prime_dfilter(edt, dfcode);
1176 /* Dissect the frame. */
1177 epan_dissect_run_with_taps(edt, phdr, frame_tvbuff_new(fdata, buf), fdata, cinfo);
1179 /* If we don't have a display filter, set "passed_dfilter" to 1. */
1180 if (dfcode != NULL) {
1181 fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, edt) ? 1 : 0;
1183 if (fdata->flags.passed_dfilter) {
1184 /* This frame passed the display filter but it may depend on other
1185 * (potentially not displayed) frames. Find those frames and mark them
1188 g_slist_foreach(edt->pi.dependent_frames, find_and_mark_frame_depended_upon, cf->frames);
1191 fdata->flags.passed_dfilter = 1;
1193 if (fdata->flags.passed_dfilter || fdata->flags.ref_time)
1194 cf->displayed_count++;
1196 if (add_to_packet_list) {
1197 /* We fill the needed columns from new_packet_list */
1198 row = packet_list_append(cinfo, fdata);
1201 if (fdata->flags.passed_dfilter || fdata->flags.ref_time)
1203 frame_data_set_after_dissect(fdata, &cf->cum_bytes);
1204 cf->prev_dis = fdata;
1206 /* If we haven't yet seen the first frame, this is it.
1208 XXX - we must do this before we add the row to the display,
1209 as, if the display's GtkCList's selection mode is
1210 GTK_SELECTION_BROWSE, when the first entry is added to it,
1211 "cf_select_packet()" will be called, and it will fetch the row
1212 data for the 0th row, and will get a null pointer rather than
1213 "fdata", as "gtk_clist_append()" won't yet have returned and
1214 thus "gtk_clist_set_row_data()" won't yet have been called.
1216 We thus need to leave behind bread crumbs so that
1217 "cf_select_packet()" can find this frame. See the comment
1218 in "cf_select_packet()". */
1219 if (cf->first_displayed == 0)
1220 cf->first_displayed = fdata->num;
1222 /* This is the last frame we've seen so far. */
1223 cf->last_displayed = fdata->num;
1226 epan_dissect_reset(edt);
1230 /* read in a new packet */
1231 /* returns the row of the new packet in the packet list or -1 if not displayed */
1233 read_packet(capture_file *cf, dfilter_t *dfcode, epan_dissect_t *edt,
1234 column_info *cinfo, gint64 offset)
1236 struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
1237 const guint8 *buf = wtap_buf_ptr(cf->wth);
1244 /* Add this packet's link-layer encapsulation type to cf->linktypes, if
1245 it's not already there.
1246 XXX - yes, this is O(N), so if every packet had a different
1247 link-layer encapsulation type, it'd be O(N^2) to read the file, but
1248 there are probably going to be a small number of encapsulation types
1250 cf_add_encapsulation_type(cf, phdr->pkt_encap);
1252 /* The frame number of this packet is one more than the count of
1253 frames in the file so far. */
1254 framenum = cf->count + 1;
1256 frame_data_init(&fdlocal, framenum, phdr, offset, cf->cum_bytes);
1260 epan_dissect_t rf_edt;
1262 epan_dissect_init(&rf_edt, cf->epan, TRUE, FALSE);
1263 epan_dissect_prime_dfilter(&rf_edt, cf->rfcode);
1264 epan_dissect_run(&rf_edt, phdr, frame_tvbuff_new(&fdlocal, buf), &fdlocal, NULL);
1265 passed = dfilter_apply_edt(cf->rfcode, &rf_edt);
1266 epan_dissect_cleanup(&rf_edt);
1270 /* This does a shallow copy of fdlocal, which is good enough. */
1271 fdata = frame_data_sequence_add(cf->frames, &fdlocal);
1274 if (phdr->opt_comment != NULL)
1275 cf->packet_comment_count++;
1276 cf->f_datalen = offset + fdlocal.cap_len;
1278 if (!cf->redissecting) {
1279 row = add_packet_to_packet_list(fdata, cf, edt, dfcode,
1280 cinfo, phdr, buf, TRUE);
1288 cf_merge_files(char **out_filenamep, int in_file_count,
1289 char *const *in_filenames, int file_type, gboolean do_append)
1291 merge_in_file_t *in_files, *in_file;
1296 int open_err, read_err, write_err, close_err;
1300 gboolean got_read_error = FALSE, got_write_error = FALSE;
1302 progdlg_t *progbar = NULL;
1304 gint64 f_len, file_pos;
1306 GTimeVal start_time;
1307 gchar status_str[100];
1308 gint64 progbar_nextstep;
1309 gint64 progbar_quantum;
1310 gchar *display_basename;
1311 int selected_frame_type;
1312 gboolean fake_interface_ids = FALSE;
1314 /* open the input files */
1315 if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
1316 &open_err, &err_info, &err_fileno)) {
1318 cf_open_failure_alert_box(in_filenames[err_fileno], open_err, err_info,
1323 if (*out_filenamep != NULL) {
1324 out_filename = *out_filenamep;
1325 out_fd = ws_open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
1329 out_fd = create_tempfile(&tmpname, "wireshark");
1332 out_filename = g_strdup(tmpname);
1333 *out_filenamep = out_filename;
1337 merge_close_in_files(in_file_count, in_files);
1339 cf_open_failure_alert_box(out_filename, open_err, NULL, TRUE, file_type);
1343 selected_frame_type = merge_select_frame_type(in_file_count, in_files);
1345 /* If we are trying to merge a number of libpcap files with different encapsulation types
1346 * change the output file type to pcapng and create SHB and IDB:s for the new file use the
1347 * interface index stored in in_files per file to change the phdr before writing the datablock.
1348 * XXX should it be an option to convert to pcapng?
1350 * We need something similar when merging pcapng files possibly with an option to say
1351 * the same interface(s) used in all in files. SHBs comments should be merged together.
1353 if ((selected_frame_type == WTAP_ENCAP_PER_PACKET)&&(file_type == WTAP_FILE_TYPE_SUBTYPE_PCAP)) {
1354 /* Write output in pcapng format */
1355 wtapng_section_t *shb_hdr;
1356 wtapng_iface_descriptions_t *idb_inf, *idb_inf_merge_file;
1357 wtapng_if_descr_t int_data, *file_int_data;
1358 GString *comment_gstr;
1360 fake_interface_ids = TRUE;
1361 /* Create SHB info */
1362 shb_hdr = wtap_file_get_shb_info(in_files[0].wth);
1363 comment_gstr = g_string_new("");
1364 g_string_append_printf(comment_gstr, "%s \n",shb_hdr->opt_comment);
1365 g_string_append_printf(comment_gstr, "File created by merging: \n");
1366 file_type = WTAP_FILE_TYPE_SUBTYPE_PCAPNG;
1368 for (i = 0; i < in_file_count; i++) {
1369 g_string_append_printf(comment_gstr, "File%d: %s \n",i+1,in_files[i].filename);
1371 shb_hdr->section_length = -1;
1373 shb_hdr->opt_comment = g_string_free(comment_gstr, FALSE); /* NULL if not available */
1374 shb_hdr->shb_hardware = NULL; /* NULL if not available, UTF-8 string containing the */
1375 /* description of the hardware used to create this section. */
1376 shb_hdr->shb_os = NULL; /* NULL if not available, UTF-8 string containing the name */
1377 /* of the operating system used to create this section. */
1378 shb_hdr->shb_user_appl = "Wireshark"; /* NULL if not available, UTF-8 string containing the name */
1379 /* of the application used to create this section. */
1381 /* create fake IDB info */
1382 idb_inf = g_new(wtapng_iface_descriptions_t,1);
1383 idb_inf->number_of_interfaces = in_file_count; /* TODO make this the number of DIFFERENT encapsulation types
1384 * check that snaplength is the same too?
1386 idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
1388 for (i = 0; i < in_file_count; i++) {
1389 idb_inf_merge_file = wtap_file_get_idb_info(in_files[i].wth);
1390 /* read the interface data from the in file to our combined interfca data */
1391 file_int_data = &g_array_index (idb_inf_merge_file->interface_data, wtapng_if_descr_t, 0);
1392 int_data.wtap_encap = file_int_data->wtap_encap;
1393 int_data.time_units_per_second = file_int_data->time_units_per_second;
1394 int_data.link_type = file_int_data->link_type;
1395 int_data.snap_len = file_int_data->snap_len;
1396 int_data.if_name = g_strdup(file_int_data->if_name);
1397 int_data.opt_comment = NULL;
1398 int_data.if_description = NULL;
1399 int_data.if_speed = 0;
1400 int_data.if_tsresol = 6;
1401 int_data.if_filter_str = NULL;
1402 int_data.bpf_filter_len = 0;
1403 int_data.if_filter_bpf_bytes = NULL;
1404 int_data.if_os = NULL;
1405 int_data.if_fcslen = -1;
1406 int_data.num_stat_entries = 0; /* Number of ISB:s */
1407 int_data.interface_statistics = NULL;
1409 g_array_append_val(idb_inf->interface_data, int_data);
1410 g_free(idb_inf_merge_file);
1412 /* Set fake interface Id in per file data */
1413 in_files[i].interface_id = i;
1416 pdh = wtap_dump_fdopen_ng(out_fd, file_type,
1417 selected_frame_type,
1418 merge_max_snapshot_length(in_file_count, in_files),
1419 FALSE /* compressed */, shb_hdr, idb_inf /* wtapng_iface_descriptions_t *idb_inf */, &open_err);
1423 merge_close_in_files(in_file_count, in_files);
1425 cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
1432 pdh = wtap_dump_fdopen(out_fd, file_type,
1433 selected_frame_type,
1434 merge_max_snapshot_length(in_file_count, in_files),
1435 FALSE /* compressed */, &open_err);
1438 merge_close_in_files(in_file_count, in_files);
1440 cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
1446 /* Get the sum of the sizes of all the files. */
1448 for (i = 0; i < in_file_count; i++)
1449 f_len += in_files[i].size;
1451 /* Update the progress bar when it gets to this value. */
1452 progbar_nextstep = 0;
1453 /* When we reach the value that triggers a progress bar update,
1454 bump that value by this amount. */
1455 progbar_quantum = f_len/N_PROGBAR_UPDATES;
1456 /* Progress so far. */
1460 g_get_current_time(&start_time);
1462 /* do the merge (or append) */
1465 in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
1468 in_file = merge_read_packet(in_file_count, in_files, &read_err,
1470 if (in_file == NULL) {
1475 if (read_err != 0) {
1476 /* I/O error reading from in_file */
1477 got_read_error = TRUE;
1481 /* Get the sum of the data offsets in all of the files. */
1483 for (i = 0; i < in_file_count; i++)
1484 data_offset += in_files[i].data_offset;
1486 /* Create the progress bar if necessary.
1487 We check on every iteration of the loop, so that it takes no
1488 longer than the standard time to create it (otherwise, for a
1489 large file, we might take considerably longer than that standard
1490 time in order to get to the next progress bar step). */
1491 if (progbar == NULL) {
1492 progbar = delayed_create_progress_dlg(NULL, "Merging", "files",
1493 FALSE, &stop_flag, &start_time, progbar_val);
1496 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1497 when we update it, we have to run the GTK+ main loop to get it
1498 to repaint what's pending, and doing so may involve an "ioctl()"
1499 to see if there's any pending input from an X server, and doing
1500 that for every packet can be costly, especially on a big file. */
1501 if (data_offset >= progbar_nextstep) {
1502 /* Get the sum of the seek positions in all of the files. */
1504 for (i = 0; i < in_file_count; i++)
1505 file_pos += wtap_read_so_far(in_files[i].wth);
1506 progbar_val = (gfloat) file_pos / (gfloat) f_len;
1507 if (progbar_val > 1.0f) {
1508 /* Some file probably grew while we were reading it.
1509 That "shouldn't happen", so we'll just clip the progress
1513 if (progbar != NULL) {
1514 g_snprintf(status_str, sizeof(status_str),
1515 "%" G_GINT64_MODIFIER "dKB of %" G_GINT64_MODIFIER "dKB",
1516 file_pos / 1024, f_len / 1024);
1517 update_progress_dlg(progbar, progbar_val, status_str);
1519 progbar_nextstep += progbar_quantum;
1523 /* Well, the user decided to abort the merge. */
1527 /* If we have WTAP_ENCAP_PER_PACKET and the infiles are of type
1528 * WTAP_FILE_TYPE_SUBTYPE_PCAP, we need to set the interface id
1529 * in the paket header = the interface index we used in the IDBs
1530 * interface description for this file(encapsulation type).
1532 if (fake_interface_ids) {
1533 struct wtap_pkthdr *phdr;
1535 phdr = wtap_phdr(in_file->wth);
1536 phdr->interface_id = in_file->interface_id;
1537 phdr->presence_flags = phdr->presence_flags | WTAP_HAS_INTERFACE_ID;
1539 if (!wtap_dump(pdh, wtap_phdr(in_file->wth),
1540 wtap_buf_ptr(in_file->wth), &write_err)) {
1541 got_write_error = TRUE;
1546 /* We're done merging the files; destroy the progress bar if it was created. */
1547 if (progbar != NULL)
1548 destroy_progress_dlg(progbar);
1550 merge_close_in_files(in_file_count, in_files);
1551 if (!got_write_error) {
1552 if (!wtap_dump_close(pdh, &write_err))
1553 got_write_error = TRUE;
1556 * We already got a write error; no need to report another
1557 * write error on close.
1559 * Don't overwrite the earlier write error.
1561 (void)wtap_dump_close(pdh, &close_err);
1564 if (got_read_error) {
1566 * Find the file on which we got the error, and report the error.
1568 for (i = 0; i < in_file_count; i++) {
1569 if (in_files[i].state == GOT_ERROR) {
1570 /* Put up a message box noting that a read failed somewhere along
1572 display_basename = g_filename_display_basename(in_files[i].filename);
1575 case WTAP_ERR_UNSUPPORTED_ENCAP:
1576 simple_error_message_box(
1577 "The capture file %s has a packet with a network type that Wireshark doesn't support.\n(%s)",
1578 display_basename, err_info);
1582 case WTAP_ERR_CANT_READ:
1583 simple_error_message_box(
1584 "An attempt to read from the capture file %s failed for"
1585 " some unknown reason.", display_basename);
1588 case WTAP_ERR_SHORT_READ:
1589 simple_error_message_box(
1590 "The capture file %s appears to have been cut short"
1591 " in the middle of a packet.", display_basename);
1594 case WTAP_ERR_BAD_FILE:
1595 simple_error_message_box(
1596 "The capture file %s appears to be damaged or corrupt.\n(%s)",
1597 display_basename, err_info);
1601 case WTAP_ERR_DECOMPRESS:
1602 simple_error_message_box(
1603 "The compressed capture file %s appears to be damaged or corrupt.\n"
1604 "(%s)", display_basename, err_info);
1609 simple_error_message_box(
1610 "An error occurred while reading the"
1611 " capture file %s: %s.",
1612 display_basename, wtap_strerror(read_err));
1615 g_free(display_basename);
1620 if (got_write_error) {
1621 /* Put up an alert box for the write error. */
1622 if (write_err < 0) {
1623 /* Wiretap error. */
1624 switch (write_err) {
1626 case WTAP_ERR_UNSUPPORTED_ENCAP:
1628 * This is a problem with the particular frame we're writing and
1629 * the file type and subtype we're writing; note that, and report
1630 * the frame number and file type/subtype.
1632 display_basename = g_filename_display_basename(in_file ? in_file->filename : "UNKNOWN");
1633 simple_error_message_box(
1634 "Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.",
1635 in_file ? in_file->packet_num : 0, display_basename,
1636 wtap_file_type_subtype_string(file_type));
1637 g_free(display_basename);
1640 case WTAP_ERR_PACKET_TOO_LARGE:
1642 * This is a problem with the particular frame we're writing and
1643 * the file type and subtype we're writing; note that, and report
1644 * the frame number and file type/subtype.
1646 display_basename = g_filename_display_basename(in_file ? in_file->filename : "UNKNOWN");
1647 simple_error_message_box(
1648 "Frame %u of \"%s\" is too large for a \"%s\" file.",
1649 in_file ? in_file->packet_num : 0, display_basename,
1650 wtap_file_type_subtype_string(file_type));
1651 g_free(display_basename);
1655 display_basename = g_filename_display_basename(out_filename);
1656 simple_error_message_box(
1657 "An error occurred while writing to the file \"%s\": %s.",
1658 out_filename, wtap_strerror(write_err));
1659 g_free(display_basename);
1664 write_failure_alert_box(out_filename, write_err);
1668 if (got_read_error || got_write_error || stop_flag) {
1669 /* Callers aren't expected to treat an error or an explicit abort
1670 differently - we put up error dialogs ourselves, so they don't
1678 cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
1680 const char *filter_new = dftext ? dftext : "";
1681 const char *filter_old = cf->dfilter ? cf->dfilter : "";
1683 GTimeVal start_time;
1685 /* if new filter equals old one, do nothing unless told to do so */
1686 if (!force && strcmp(filter_new, filter_old) == 0) {
1692 if (dftext == NULL) {
1693 /* The new filter is an empty filter (i.e., display all packets).
1694 * so leave dfcode==NULL
1698 * We have a filter; make a copy of it (as we'll be saving it),
1699 * and try to compile it.
1701 dftext = g_strdup(dftext);
1702 if (!dfilter_compile(dftext, &dfcode)) {
1703 /* The attempt failed; report an error. */
1704 simple_message_box(ESD_TYPE_ERROR, NULL,
1705 "See the help for a description of the display filter syntax.",
1706 "\"%s\" isn't a valid display filter: %s",
1707 dftext, dfilter_error_msg);
1713 if (dfcode == NULL) {
1714 /* Yes - free the filter text, and set it to null. */
1720 /* We have a valid filter. Replace the current filter. */
1721 g_free(cf->dfilter);
1722 cf->dfilter = dftext;
1723 g_get_current_time(&start_time);
1726 /* Now rescan the packet list, applying the new filter, but not
1727 throwing away information constructed on a previous pass. */
1728 if (dftext == NULL) {
1729 rescan_packets(cf, "Resetting", "Filter", FALSE);
1731 rescan_packets(cf, "Filtering", dftext, FALSE);
1734 /* Cleanup and release all dfilter resources */
1735 dfilter_free(dfcode);
1741 cf_reftime_packets(capture_file *cf)
1743 ref_time_packets(cf);
1747 cf_redissect_packets(capture_file *cf)
1749 if (cf->state != FILE_CLOSED) {
1750 rescan_packets(cf, "Reprocessing", "all packets", TRUE);
1755 cf_read_frame_r(capture_file *cf, const frame_data *fdata,
1756 struct wtap_pkthdr *phdr, Buffer *buf)
1760 gchar *display_basename;
1762 #ifdef WANT_PACKET_EDITOR
1763 /* if fdata->file_off == -1 it means packet was edited, and we must find data inside edited_frames tree */
1764 if (G_UNLIKELY(fdata->file_off == -1)) {
1765 const modified_frame_data *frame = (const modified_frame_data *) g_tree_lookup(cf->edited_frames, GINT_TO_POINTER(fdata->num));
1768 simple_error_message_box("fdata->file_off == -1, but can't find modified frame!");
1772 *phdr = frame->phdr;
1773 buffer_assure_space(buf, frame->phdr.caplen);
1774 memcpy(buffer_start_ptr(buf), frame->pd, frame->phdr.caplen);
1779 if (!wtap_seek_read(cf->wth, fdata->file_off, phdr, buf, &err, &err_info)) {
1780 display_basename = g_filename_display_basename(cf->filename);
1783 case WTAP_ERR_UNSUPPORTED_ENCAP:
1784 simple_error_message_box("The file \"%s\" has a packet with a network type that Wireshark doesn't support.\n(%s)",
1785 display_basename, err_info);
1789 case WTAP_ERR_BAD_FILE:
1790 simple_error_message_box("An error occurred while reading from the file \"%s\": %s.\n(%s)",
1791 display_basename, wtap_strerror(err), err_info);
1796 simple_error_message_box(
1797 "An error occurred while reading from the file \"%s\": %s.",
1798 display_basename, wtap_strerror(err));
1801 g_free(display_basename);
1808 cf_read_frame(capture_file *cf, frame_data *fdata)
1810 return cf_read_frame_r(cf, fdata, &cf->phdr, &cf->buf);
1813 /* Rescan the list of packets, reconstructing the CList.
1815 "action" describes why we're doing this; it's used in the progress
1818 "action_item" describes what we're doing; it's used in the progress
1821 "redissect" is TRUE if we need to make the dissectors reconstruct
1822 any state information they have (because a preference that affects
1823 some dissector has changed, meaning some dissector might construct
1824 its state differently from the way it was constructed the last time). */
1826 rescan_packets(capture_file *cf, const char *action, const char *action_item, gboolean redissect)
1828 /* Rescan packets new packet list */
1831 progdlg_t *progbar = NULL;
1834 frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
1835 int selected_frame_num, preceding_frame_num, following_frame_num, prev_frame_num;
1836 gboolean selected_frame_seen;
1838 GTimeVal start_time;
1839 gchar status_str[100];
1840 int progbar_nextstep;
1841 int progbar_quantum;
1845 gboolean create_proto_tree;
1847 gboolean add_to_packet_list = FALSE;
1849 guint32 frames_count;
1851 /* Compile the current display filter.
1852 * We assume this will not fail since cf->dfilter is only set in
1853 * cf_filter IFF the filter was valid.
1855 compiled = dfilter_compile(cf->dfilter, &dfcode);
1856 g_assert(!cf->dfilter || (compiled && dfcode));
1858 /* Get the union of the flags for all tap listeners. */
1859 tap_flags = union_of_tap_listener_flags();
1860 cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
1862 (dfcode != NULL || have_filtering_tap_listeners() || (tap_flags & TL_REQUIRES_PROTO_TREE));
1864 reset_tap_listeners();
1865 /* Which frame, if any, is the currently selected frame?
1866 XXX - should the selected frame or the focus frame be the "current"
1867 frame, that frame being the one from which "Find Frame" searches
1869 selected_frame = cf->current_frame;
1871 /* Mark frame num as not found */
1872 selected_frame_num = -1;
1874 /* Freeze the packet list while we redo it, so we don't get any
1875 screen updates while it happens. */
1876 packet_list_freeze();
1879 /* We need to re-initialize all the state information that protocols
1880 keep, because some preference that controls a dissector has changed,
1881 which might cause the state information to be constructed differently
1882 by that dissector. */
1884 /* We might receive new packets while redissecting, and we don't
1885 want to dissect those before their time. */
1886 cf->redissecting = TRUE;
1888 /* 'reset' dissection session */
1889 epan_free(cf->epan);
1890 cf->epan = ws_epan_new(cf);
1892 /* We need to redissect the packets so we have to discard our old
1893 * packet list store. */
1894 packet_list_clear();
1895 add_to_packet_list = TRUE;
1898 /* We don't yet know which will be the first and last frames displayed. */
1899 cf->first_displayed = 0;
1900 cf->last_displayed = 0;
1902 /* We currently don't display any packets */
1903 cf->displayed_count = 0;
1905 /* Iterate through the list of frames. Call a routine for each frame
1906 to check whether it should be displayed and, if so, add it to
1907 the display list. */
1909 cf->prev_dis = NULL;
1910 cf->prev_cap = NULL;
1913 /* Update the progress bar when it gets to this value. */
1914 progbar_nextstep = 0;
1915 /* When we reach the value that triggers a progress bar update,
1916 bump that value by this amount. */
1917 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1918 /* Count of packets at which we've looked. */
1920 /* Progress so far. */
1924 g_get_current_time(&start_time);
1926 /* no previous row yet */
1927 prev_frame_num = -1;
1930 preceding_frame_num = -1;
1931 preceding_frame = NULL;
1932 following_frame_num = -1;
1933 following_frame = NULL;
1935 selected_frame_seen = FALSE;
1937 frames_count = cf->count;
1939 epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
1941 for (framenum = 1; framenum <= frames_count; framenum++) {
1942 fdata = frame_data_sequence_find(cf->frames, framenum);
1944 /* Create the progress bar if necessary.
1945 We check on every iteration of the loop, so that it takes no
1946 longer than the standard time to create it (otherwise, for a
1947 large file, we might take considerably longer than that standard
1948 time in order to get to the next progress bar step). */
1949 if (progbar == NULL)
1950 progbar = delayed_create_progress_dlg(cf->window, action, action_item, TRUE,
1951 &stop_flag, &start_time,
1954 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1955 when we update it, we have to run the GTK+ main loop to get it
1956 to repaint what's pending, and doing so may involve an "ioctl()"
1957 to see if there's any pending input from an X server, and doing
1958 that for every packet can be costly, especially on a big file. */
1959 if (count >= progbar_nextstep) {
1960 /* let's not divide by zero. I should never be started
1961 * with count == 0, so let's assert that
1963 g_assert(cf->count > 0);
1964 progbar_val = (gfloat) count / frames_count;
1966 if (progbar != NULL) {
1967 g_snprintf(status_str, sizeof(status_str),
1968 "%4u of %u frames", count, frames_count);
1969 update_progress_dlg(progbar, progbar_val, status_str);
1972 progbar_nextstep += progbar_quantum;
1976 /* Well, the user decided to abort the filtering. Just stop.
1978 XXX - go back to the previous filter? Users probably just
1979 want not to wait for a filtering operation to finish;
1980 unless we cancel by having no filter, reverting to the
1981 previous filter will probably be even more expensive than
1982 continuing the filtering, as it involves going back to the
1983 beginning and filtering, and even with no filter we currently
1984 have to re-generate the entire clist, which is also expensive.
1986 I'm not sure what Network Monitor does, but it doesn't appear
1987 to give you an unfiltered display if you cancel. */
1994 /* Since all state for the frame was destroyed, mark the frame
1995 * as not visited, free the GSList referring to the state
1996 * data (the per-frame data itself was freed by
1997 * "init_dissection()"), and null out the GSList pointer. */
1998 frame_data_reset(fdata);
1999 frames_count = cf->count;
2002 /* Frame dependencies from the previous dissection/filtering are no longer valid. */
2003 fdata->flags.dependent_of_displayed = 0;
2005 if (!cf_read_frame(cf, fdata))
2006 break; /* error reading the frame */
2008 /* If the previous frame is displayed, and we haven't yet seen the
2009 selected frame, remember that frame - it's the closest one we've
2010 yet seen before the selected frame. */
2011 if (prev_frame_num != -1 && !selected_frame_seen && prev_frame->flags.passed_dfilter) {
2012 preceding_frame_num = prev_frame_num;
2013 preceding_frame = prev_frame;
2016 add_packet_to_packet_list(fdata, cf, &edt, dfcode,
2018 buffer_start_ptr(&cf->buf),
2019 add_to_packet_list);
2021 /* If this frame is displayed, and this is the first frame we've
2022 seen displayed after the selected frame, remember this frame -
2023 it's the closest one we've yet seen at or after the selected
2025 if (fdata->flags.passed_dfilter && selected_frame_seen && following_frame_num == -1) {
2026 following_frame_num = fdata->num;
2027 following_frame = fdata;
2029 if (fdata == selected_frame) {
2030 selected_frame_seen = TRUE;
2031 if (fdata->flags.passed_dfilter)
2032 selected_frame_num = fdata->num;
2035 /* Remember this frame - it'll be the previous frame
2036 on the next pass through the loop. */
2037 prev_frame_num = fdata->num;
2041 epan_dissect_cleanup(&edt);
2043 /* We are done redissecting the packet list. */
2044 cf->redissecting = FALSE;
2047 frames_count = cf->count;
2048 /* Clear out what remains of the visited flags and per-frame data
2051 XXX - that may cause various forms of bogosity when dissecting
2052 these frames, as they won't have been seen by this sequential
2053 pass, but the only alternative I see is to keep scanning them
2054 even though the user requested that the scan stop, and that
2055 would leave the user stuck with an Wireshark grinding on
2056 until it finishes. Should we just stick them with that? */
2057 for (; framenum <= frames_count; framenum++) {
2058 fdata = frame_data_sequence_find(cf->frames, framenum);
2059 frame_data_reset(fdata);
2063 /* We're done filtering the packets; destroy the progress bar if it
2065 if (progbar != NULL)
2066 destroy_progress_dlg(progbar);
2068 /* Unfreeze the packet list. */
2069 if (!add_to_packet_list)
2070 packet_list_recreate_visible_rows();
2072 /* Compute the time it took to filter the file */
2073 compute_elapsed(cf, &start_time);
2077 if (selected_frame_num == -1) {
2078 /* The selected frame didn't pass the filter. */
2079 if (selected_frame == NULL) {
2080 /* That's because there *was* no selected frame. Make the first
2081 displayed frame the current frame. */
2082 selected_frame_num = 0;
2084 /* Find the nearest displayed frame to the selected frame (whether
2085 it's before or after that frame) and make that the current frame.
2086 If the next and previous displayed frames are equidistant from the
2087 selected frame, choose the next one. */
2088 g_assert(following_frame == NULL ||
2089 following_frame->num >= selected_frame->num);
2090 g_assert(preceding_frame == NULL ||
2091 preceding_frame->num <= selected_frame->num);
2092 if (following_frame == NULL) {
2093 /* No frame after the selected frame passed the filter, so we
2094 have to select the last displayed frame before the selected
2096 selected_frame_num = preceding_frame_num;
2097 selected_frame = preceding_frame;
2098 } else if (preceding_frame == NULL) {
2099 /* No frame before the selected frame passed the filter, so we
2100 have to select the first displayed frame after the selected
2102 selected_frame_num = following_frame_num;
2103 selected_frame = following_frame;
2105 /* Frames before and after the selected frame passed the filter, so
2106 we'll select the previous frame */
2107 selected_frame_num = preceding_frame_num;
2108 selected_frame = preceding_frame;
2113 if (selected_frame_num == -1) {
2114 /* There are no frames displayed at all. */
2115 cf_unselect_packet(cf);
2117 /* Either the frame that was selected passed the filter, or we've
2118 found the nearest displayed frame to that frame. Select it, make
2119 it the focus row, and make it visible. */
2120 /* Set to invalid to force update of packet list and packet details */
2121 cf->current_row = -1;
2122 if (selected_frame_num == 0) {
2123 packet_list_select_first_row();
2125 if (!packet_list_select_row_from_data(selected_frame)) {
2126 /* We didn't find a row corresponding to this frame.
2127 This means that the frame isn't being displayed currently,
2128 so we can't select it. */
2129 simple_message_box(ESD_TYPE_INFO, NULL,
2130 "The capture file is probably not fully dissected.",
2131 "End of capture exceeded!");
2136 /* Cleanup and release all dfilter resources */
2137 dfilter_free(dfcode);
2142 * Scan trough all frame data and recalculate the ref time
2143 * without rereading the file.
2144 * XXX - do we need a progres bar or is this fast enough?
2147 ref_time_packets(capture_file *cf)
2154 cf->prev_dis = NULL;
2157 for (framenum = 1; framenum <= cf->count; framenum++) {
2158 fdata = frame_data_sequence_find(cf->frames, framenum);
2160 /* just add some value here until we know if it is being displayed or not */
2161 fdata->cum_bytes = cf->cum_bytes + fdata->pkt_len;
2167 /* If we don't have the time stamp of the first packet in the
2168 capture, it's because this is the first packet. Save the time
2169 stamp of this packet as the time stamp of the first packet. */
2170 if (cf->ref == NULL)
2172 /* if this frames is marked as a reference time frame, reset
2173 firstsec and firstusec to this frame */
2174 if (fdata->flags.ref_time)
2177 /* If we don't have the time stamp of the previous displayed packet,
2178 it's because this is the first displayed packet. Save the time
2179 stamp of this packet as the time stamp of the previous displayed
2181 if (cf->prev_dis == NULL) {
2182 cf->prev_dis = fdata;
2185 /* Get the time elapsed between the first packet and this packet. */
2186 fdata->frame_ref_num = (fdata != cf->ref) ? cf->ref->num : 0;
2187 nstime_delta(&rel_ts, &fdata->abs_ts, &cf->ref->abs_ts);
2189 /* If it's greater than the current elapsed time, set the elapsed time
2190 to it (we check for "greater than" so as not to be confused by
2191 time moving backwards). */
2192 if ((gint32)cf->elapsed_time.secs < rel_ts.secs
2193 || ((gint32)cf->elapsed_time.secs == rel_ts.secs && (gint32)cf->elapsed_time.nsecs < rel_ts.nsecs)) {
2194 cf->elapsed_time = rel_ts;
2197 /* If this frame is displayed, get the time elapsed between the
2198 previous displayed packet and this packet. */
2199 if ( fdata->flags.passed_dfilter ) {
2200 fdata->prev_dis_num = cf->prev_dis->num;
2201 cf->prev_dis = fdata;
2207 if ( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) ) {
2208 /* This frame either passed the display filter list or is marked as
2209 a time reference frame. All time reference frames are displayed
2210 even if they dont pass the display filter */
2211 if (fdata->flags.ref_time) {
2212 /* if this was a TIME REF frame we should reset the cum_bytes field */
2213 cf->cum_bytes = fdata->pkt_len;
2214 fdata->cum_bytes = cf->cum_bytes;
2216 /* increase cum_bytes with this packets length */
2217 cf->cum_bytes += fdata->pkt_len;
2230 process_specified_packets(capture_file *cf, packet_range_t *range,
2231 const char *string1, const char *string2, gboolean terminate_is_stop,
2232 gboolean (*callback)(capture_file *, frame_data *,
2233 struct wtap_pkthdr *, const guint8 *, void *),
2234 void *callback_args)
2239 psp_return_t ret = PSP_FINISHED;
2241 progdlg_t *progbar = NULL;
2244 gboolean progbar_stop_flag;
2245 GTimeVal progbar_start_time;
2246 gchar progbar_status_str[100];
2247 int progbar_nextstep;
2248 int progbar_quantum;
2249 range_process_e process_this;
2250 struct wtap_pkthdr phdr;
2252 memset(&phdr, 0, sizeof(struct wtap_pkthdr));
2253 buffer_init(&buf, 1500);
2255 /* Update the progress bar when it gets to this value. */
2256 progbar_nextstep = 0;
2257 /* When we reach the value that triggers a progress bar update,
2258 bump that value by this amount. */
2259 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2260 /* Count of packets at which we've looked. */
2262 /* Progress so far. */
2265 progbar_stop_flag = FALSE;
2266 g_get_current_time(&progbar_start_time);
2269 packet_range_process_init(range);
2271 /* Iterate through all the packets, printing the packets that
2272 were selected by the current display filter. */
2273 for (framenum = 1; framenum <= cf->count; framenum++) {
2274 fdata = frame_data_sequence_find(cf->frames, framenum);
2276 /* Create the progress bar if necessary.
2277 We check on every iteration of the loop, so that it takes no
2278 longer than the standard time to create it (otherwise, for a
2279 large file, we might take considerably longer than that standard
2280 time in order to get to the next progress bar step). */
2281 if (progbar == NULL)
2282 progbar = delayed_create_progress_dlg(cf->window, string1, string2,
2285 &progbar_start_time,
2288 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2289 when we update it, we have to run the GTK+ main loop to get it
2290 to repaint what's pending, and doing so may involve an "ioctl()"
2291 to see if there's any pending input from an X server, and doing
2292 that for every packet can be costly, especially on a big file. */
2293 if (progbar_count >= progbar_nextstep) {
2294 /* let's not divide by zero. I should never be started
2295 * with count == 0, so let's assert that
2297 g_assert(cf->count > 0);
2298 progbar_val = (gfloat) progbar_count / cf->count;
2300 if (progbar != NULL) {
2301 g_snprintf(progbar_status_str, sizeof(progbar_status_str),
2302 "%4u of %u packets", progbar_count, cf->count);
2303 update_progress_dlg(progbar, progbar_val, progbar_status_str);
2306 progbar_nextstep += progbar_quantum;
2309 if (progbar_stop_flag) {
2310 /* Well, the user decided to abort the operation. Just stop,
2311 and arrange to return PSP_STOPPED to our caller, so they know
2312 it was stopped explicitly. */
2319 if (range != NULL) {
2320 /* do we have to process this packet? */
2321 process_this = packet_range_process_packet(range, fdata);
2322 if (process_this == range_process_next) {
2323 /* this packet uninteresting, continue with next one */
2325 } else if (process_this == range_processing_finished) {
2326 /* all interesting packets processed, stop the loop */
2331 /* Get the packet */
2332 if (!cf_read_frame_r(cf, fdata, &phdr, &buf)) {
2333 /* Attempt to get the packet failed. */
2337 /* Process the packet */
2338 if (!callback(cf, fdata, &phdr, buffer_start_ptr(&buf), callback_args)) {
2339 /* Callback failed. We assume it reported the error appropriately. */
2345 /* We're done printing the packets; destroy the progress bar if
2347 if (progbar != NULL)
2348 destroy_progress_dlg(progbar);
2358 } retap_callback_args_t;
2361 retap_packet(capture_file *cf _U_, frame_data *fdata,
2362 struct wtap_pkthdr *phdr, const guint8 *pd,
2365 retap_callback_args_t *args = (retap_callback_args_t *)argsp;
2367 epan_dissect_run_with_taps(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, args->cinfo);
2368 epan_dissect_reset(&args->edt);
2374 cf_retap_packets(capture_file *cf)
2376 packet_range_t range;
2377 retap_callback_args_t callback_args;
2378 gboolean construct_protocol_tree;
2379 gboolean filtering_tap_listeners;
2383 /* Do we have any tap listeners with filters? */
2384 filtering_tap_listeners = have_filtering_tap_listeners();
2386 tap_flags = union_of_tap_listener_flags();
2388 /* If any tap listeners have filters, or require the protocol tree,
2389 construct the protocol tree. */
2390 construct_protocol_tree = filtering_tap_listeners ||
2391 (tap_flags & TL_REQUIRES_PROTO_TREE);
2393 /* If any tap listeners require the columns, construct them. */
2394 callback_args.cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
2396 /* Reset the tap listeners. */
2397 reset_tap_listeners();
2399 epan_dissect_init(&callback_args.edt, cf->epan, construct_protocol_tree, FALSE);
2401 /* Iterate through the list of packets, dissecting all packets and
2402 re-running the taps. */
2403 packet_range_init(&range, cf);
2404 packet_range_process_init(&range);
2406 ret = process_specified_packets(cf, &range, "Recalculating statistics on",
2407 "all packets", TRUE, retap_packet,
2410 epan_dissect_cleanup(&callback_args.edt);
2414 /* Completed successfully. */
2418 /* Well, the user decided to abort the refiltering.
2419 Return CF_READ_ABORTED so our caller knows they did that. */
2420 return CF_READ_ABORTED;
2423 /* Error while retapping. */
2424 return CF_READ_ERROR;
2427 g_assert_not_reached();
2432 print_args_t *print_args;
2433 gboolean print_header_line;
2434 char *header_line_buf;
2435 int header_line_buf_len;
2436 gboolean print_formfeed;
2437 gboolean print_separator;
2441 int num_visible_cols;
2444 } print_callback_args_t;
2447 print_packet(capture_file *cf, frame_data *fdata,
2448 struct wtap_pkthdr *phdr, const guint8 *pd,
2451 print_callback_args_t *args = (print_callback_args_t *)argsp;
2457 char bookmark_name[9+10+1]; /* "__frameNNNNNNNNNN__\0" */
2458 char bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0" */
2460 /* Fill in the column information if we're printing the summary
2462 if (args->print_args->print_summary) {
2463 col_custom_prime_edt(&args->edt, &cf->cinfo);
2464 epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, &cf->cinfo);
2465 epan_dissect_fill_in_columns(&args->edt, FALSE, TRUE);
2467 epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, NULL);
2469 if (args->print_formfeed) {
2470 if (!new_page(args->print_args->stream))
2473 if (args->print_separator) {
2474 if (!print_line(args->print_args->stream, 0, ""))
2480 * We generate bookmarks, if the output format supports them.
2481 * The name is "__frameN__".
2483 g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
2485 if (args->print_args->print_summary) {
2486 if (!args->print_args->print_col_headings)
2487 args->print_header_line = FALSE;
2488 if (args->print_header_line) {
2489 if (!print_line(args->print_args->stream, 0, args->header_line_buf))
2491 args->print_header_line = FALSE; /* we might not need to print any more */
2493 cp = &args->line_buf[0];
2495 for (i = 0; i < args->num_visible_cols; i++) {
2496 /* Find the length of the string for this column. */
2497 column_len = (int) strlen(cf->cinfo.col_data[args->visible_cols[i]]);
2498 if (args->col_widths[i] > column_len)
2499 column_len = args->col_widths[i];
2501 /* Make sure there's room in the line buffer for the column; if not,
2502 double its length. */
2503 line_len += column_len + 1; /* "+1" for space */
2504 if (line_len > args->line_buf_len) {
2505 cp_off = (int) (cp - args->line_buf);
2506 args->line_buf_len = 2 * line_len;
2507 args->line_buf = (char *)g_realloc(args->line_buf, args->line_buf_len + 1);
2508 cp = args->line_buf + cp_off;
2511 /* Right-justify the packet number column. */
2512 if (cf->cinfo.col_fmt[args->visible_cols[i]] == COL_NUMBER)
2513 g_snprintf(cp, column_len+1, "%*s", args->col_widths[i], cf->cinfo.col_data[args->visible_cols[i]]);
2515 g_snprintf(cp, column_len+1, "%-*s", args->col_widths[i], cf->cinfo.col_data[args->visible_cols[i]]);
2517 if (i != args->num_visible_cols - 1)
2523 * Generate a bookmark, using the summary line as the title.
2525 if (!print_bookmark(args->print_args->stream, bookmark_name,
2529 if (!print_line(args->print_args->stream, 0, args->line_buf))
2533 * Generate a bookmark, using "Frame N" as the title, as we're not
2534 * printing the summary line.
2536 g_snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
2537 if (!print_bookmark(args->print_args->stream, bookmark_name,
2540 } /* if (print_summary) */
2542 if (args->print_args->print_dissections != print_dissections_none) {
2543 if (args->print_args->print_summary) {
2544 /* Separate the summary line from the tree with a blank line. */
2545 if (!print_line(args->print_args->stream, 0, ""))
2549 /* Print the information in that tree. */
2550 if (!proto_tree_print(args->print_args, &args->edt, args->print_args->stream))
2553 /* Print a blank line if we print anything after this (aka more than one packet). */
2554 args->print_separator = TRUE;
2556 /* Print a header line if we print any more packet summaries */
2557 if (args->print_args->print_col_headings)
2558 args->print_header_line = TRUE;
2561 if (args->print_args->print_hex) {
2562 if (args->print_args->print_summary || (args->print_args->print_dissections != print_dissections_none)) {
2563 if (!print_line(args->print_args->stream, 0, ""))
2566 /* Print the full packet data as hex. */
2567 if (!print_hex_data(args->print_args->stream, &args->edt))
2570 /* Print a blank line if we print anything after this (aka more than one packet). */
2571 args->print_separator = TRUE;
2573 /* Print a header line if we print any more packet summaries */
2574 if (args->print_args->print_col_headings)
2575 args->print_header_line = TRUE;
2576 } /* if (args->print_args->print_dissections != print_dissections_none) */
2578 epan_dissect_reset(&args->edt);
2580 /* do we want to have a formfeed between each packet from now on? */
2581 if (args->print_args->print_formfeed) {
2582 args->print_formfeed = TRUE;
2588 epan_dissect_reset(&args->edt);
2593 cf_print_packets(capture_file *cf, print_args_t *print_args)
2595 print_callback_args_t callback_args;
2598 int i, cp_off, column_len, line_len;
2599 int num_visible_col = 0, last_visible_col = 0, visible_col_count;
2603 gboolean proto_tree_needed;
2605 callback_args.print_args = print_args;
2606 callback_args.print_header_line = print_args->print_col_headings;
2607 callback_args.header_line_buf = NULL;
2608 callback_args.header_line_buf_len = 256;
2609 callback_args.print_formfeed = FALSE;
2610 callback_args.print_separator = FALSE;
2611 callback_args.line_buf = NULL;
2612 callback_args.line_buf_len = 256;
2613 callback_args.col_widths = NULL;
2614 callback_args.num_visible_cols = 0;
2615 callback_args.visible_cols = NULL;
2617 if (!print_preamble(print_args->stream, cf->filename, wireshark_gitversion)) {
2618 destroy_print_stream(print_args->stream);
2619 return CF_PRINT_WRITE_ERROR;
2622 if (print_args->print_summary) {
2623 /* We're printing packet summaries. Allocate the header line buffer
2624 and get the column widths. */
2625 callback_args.header_line_buf = (char *)g_malloc(callback_args.header_line_buf_len + 1);
2627 /* Find the number of visible columns and the last visible column */
2628 for (i = 0; i < prefs.num_cols; i++) {
2630 clp = g_list_nth(prefs.col_list, i);
2631 if (clp == NULL) /* Sanity check, Invalid column requested */
2634 cfmt = (fmt_data *) clp->data;
2635 if (cfmt->visible) {
2637 last_visible_col = i;
2641 /* Find the widths for each of the columns - maximum of the
2642 width of the title and the width of the data - and construct
2643 a buffer with a line containing the column titles. */
2644 callback_args.num_visible_cols = num_visible_col;
2645 callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * num_visible_col);
2646 callback_args.visible_cols = (gint *) g_malloc(sizeof(gint) * num_visible_col);
2647 cp = &callback_args.header_line_buf[0];
2649 visible_col_count = 0;
2650 for (i = 0; i < cf->cinfo.num_cols; i++) {
2652 clp = g_list_nth(prefs.col_list, i);
2653 if (clp == NULL) /* Sanity check, Invalid column requested */
2656 cfmt = (fmt_data *) clp->data;
2657 if (cfmt->visible == FALSE)
2660 /* Save the order of visible columns */
2661 callback_args.visible_cols[visible_col_count] = i;
2663 /* Don't pad the last column. */
2664 if (i == last_visible_col)
2665 callback_args.col_widths[visible_col_count] = 0;
2667 callback_args.col_widths[visible_col_count] = (gint) strlen(cf->cinfo.col_title[i]);
2668 data_width = get_column_char_width(get_column_format(i));
2669 if (data_width > callback_args.col_widths[visible_col_count])
2670 callback_args.col_widths[visible_col_count] = data_width;
2673 /* Find the length of the string for this column. */
2674 column_len = (int) strlen(cf->cinfo.col_title[i]);
2675 if (callback_args.col_widths[i] > column_len)
2676 column_len = callback_args.col_widths[visible_col_count];
2678 /* Make sure there's room in the line buffer for the column; if not,
2679 double its length. */
2680 line_len += column_len + 1; /* "+1" for space */
2681 if (line_len > callback_args.header_line_buf_len) {
2682 cp_off = (int) (cp - callback_args.header_line_buf);
2683 callback_args.header_line_buf_len = 2 * line_len;
2684 callback_args.header_line_buf = (char *)g_realloc(callback_args.header_line_buf,
2685 callback_args.header_line_buf_len + 1);
2686 cp = callback_args.header_line_buf + cp_off;
2689 /* Right-justify the packet number column. */
2690 /* if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2691 g_snprintf(cp, column_len+1, "%*s", callback_args.col_widths[visible_col_count], cf->cinfo.col_title[i]);
2693 g_snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[visible_col_count], cf->cinfo.col_title[i]);
2695 if (i != cf->cinfo.num_cols - 1)
2698 visible_col_count++;
2702 /* Now start out the main line buffer with the same length as the
2703 header line buffer. */
2704 callback_args.line_buf_len = callback_args.header_line_buf_len;
2705 callback_args.line_buf = (char *)g_malloc(callback_args.line_buf_len + 1);
2706 } /* if (print_summary) */
2708 /* Create the protocol tree, and make it visible, if we're printing
2709 the dissection or the hex data.
2710 XXX - do we need it if we're just printing the hex data? */
2712 callback_args.print_args->print_dissections != print_dissections_none ||
2713 callback_args.print_args->print_hex ||
2714 have_custom_cols(&cf->cinfo);
2715 epan_dissect_init(&callback_args.edt, cf->epan, proto_tree_needed, proto_tree_needed);
2717 /* Iterate through the list of packets, printing the packets we were
2719 ret = process_specified_packets(cf, &print_args->range, "Printing",
2720 "selected packets", TRUE, print_packet,
2722 epan_dissect_cleanup(&callback_args.edt);
2723 g_free(callback_args.header_line_buf);
2724 g_free(callback_args.line_buf);
2725 g_free(callback_args.col_widths);
2726 g_free(callback_args.visible_cols);
2731 /* Completed successfully. */
2735 /* Well, the user decided to abort the printing.
2737 XXX - note that what got generated before they did that
2738 will get printed if we're piping to a print program; we'd
2739 have to write to a file and then hand that to the print
2740 program to make it actually not print anything. */
2744 /* Error while printing.
2746 XXX - note that what got generated before they did that
2747 will get printed if we're piping to a print program; we'd
2748 have to write to a file and then hand that to the print
2749 program to make it actually not print anything. */
2750 destroy_print_stream(print_args->stream);
2751 return CF_PRINT_WRITE_ERROR;
2754 if (!print_finale(print_args->stream)) {
2755 destroy_print_stream(print_args->stream);
2756 return CF_PRINT_WRITE_ERROR;
2759 if (!destroy_print_stream(print_args->stream))
2760 return CF_PRINT_WRITE_ERROR;
2768 } write_packet_callback_args_t;
2771 write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
2772 struct wtap_pkthdr *phdr, const guint8 *pd,
2775 write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
2777 /* Create the protocol tree, but don't fill in the column information. */
2778 epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, NULL);
2780 /* Write out the information in that tree. */
2781 proto_tree_write_pdml(&args->edt, args->fh);
2783 epan_dissect_reset(&args->edt);
2785 return !ferror(args->fh);
2789 cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
2791 write_packet_callback_args_t callback_args;
2795 fh = ws_fopen(print_args->file, "w");
2797 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2799 write_pdml_preamble(fh, cf->filename);
2802 return CF_PRINT_WRITE_ERROR;
2805 callback_args.fh = fh;
2806 epan_dissect_init(&callback_args.edt, cf->epan, TRUE, TRUE);
2808 /* Iterate through the list of packets, printing the packets we were
2810 ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
2811 "selected packets", TRUE,
2812 write_pdml_packet, &callback_args);
2814 epan_dissect_cleanup(&callback_args.edt);
2819 /* Completed successfully. */
2823 /* Well, the user decided to abort the printing. */
2827 /* Error while printing. */
2829 return CF_PRINT_WRITE_ERROR;
2832 write_pdml_finale(fh);
2835 return CF_PRINT_WRITE_ERROR;
2838 /* XXX - check for an error */
2845 write_psml_packet(capture_file *cf, frame_data *fdata,
2846 struct wtap_pkthdr *phdr, const guint8 *pd,
2849 write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
2851 col_custom_prime_edt(&args->edt, &cf->cinfo);
2852 epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, &cf->cinfo);
2853 epan_dissect_fill_in_columns(&args->edt, FALSE, TRUE);
2855 /* Write out the information in that tree. */
2856 proto_tree_write_psml(&args->edt, args->fh);
2858 epan_dissect_reset(&args->edt);
2860 return !ferror(args->fh);
2864 cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
2866 write_packet_callback_args_t callback_args;
2870 gboolean proto_tree_needed;
2872 fh = ws_fopen(print_args->file, "w");
2874 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2876 write_psml_preamble(fh);
2879 return CF_PRINT_WRITE_ERROR;
2882 callback_args.fh = fh;
2884 /* Fill in the column information, only create the protocol tree
2885 if having custom columns. */
2886 proto_tree_needed = have_custom_cols(&cf->cinfo);
2887 epan_dissect_init(&callback_args.edt, cf->epan, proto_tree_needed, proto_tree_needed);
2889 /* Iterate through the list of packets, printing the packets we were
2891 ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
2892 "selected packets", TRUE,
2893 write_psml_packet, &callback_args);
2895 epan_dissect_cleanup(&callback_args.edt);
2900 /* Completed successfully. */
2904 /* Well, the user decided to abort the printing. */
2908 /* Error while printing. */
2910 return CF_PRINT_WRITE_ERROR;
2913 write_psml_finale(fh);
2916 return CF_PRINT_WRITE_ERROR;
2919 /* XXX - check for an error */
2926 write_csv_packet(capture_file *cf, frame_data *fdata,
2927 struct wtap_pkthdr *phdr, const guint8 *pd,
2930 write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
2932 /* Fill in the column information */
2933 col_custom_prime_edt(&args->edt, &cf->cinfo);
2934 epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, &cf->cinfo);
2935 epan_dissect_fill_in_columns(&args->edt, FALSE, TRUE);
2937 /* Write out the information in that tree. */
2938 proto_tree_write_csv(&args->edt, args->fh);
2940 epan_dissect_reset(&args->edt);
2942 return !ferror(args->fh);
2946 cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
2948 write_packet_callback_args_t callback_args;
2949 gboolean proto_tree_needed;
2953 fh = ws_fopen(print_args->file, "w");
2955 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
2957 write_csv_preamble(fh);
2960 return CF_PRINT_WRITE_ERROR;
2963 callback_args.fh = fh;
2965 /* only create the protocol tree if having custom columns. */
2966 proto_tree_needed = have_custom_cols(&cf->cinfo);
2967 epan_dissect_init(&callback_args.edt, cf->epan, proto_tree_needed, proto_tree_needed);
2969 /* Iterate through the list of packets, printing the packets we were
2971 ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
2972 "selected packets", TRUE,
2973 write_csv_packet, &callback_args);
2975 epan_dissect_cleanup(&callback_args.edt);
2980 /* Completed successfully. */
2984 /* Well, the user decided to abort the printing. */
2988 /* Error while printing. */
2990 return CF_PRINT_WRITE_ERROR;
2993 write_csv_finale(fh);
2996 return CF_PRINT_WRITE_ERROR;
2999 /* XXX - check for an error */
3006 write_carrays_packet(capture_file *cf _U_, frame_data *fdata,
3007 struct wtap_pkthdr *phdr,
3008 const guint8 *pd, void *argsp)
3010 write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
3012 epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, NULL);
3013 proto_tree_write_carrays(fdata->num, args->fh, &args->edt);
3014 epan_dissect_reset(&args->edt);
3016 return !ferror(args->fh);
3020 cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
3022 write_packet_callback_args_t callback_args;
3026 fh = ws_fopen(print_args->file, "w");
3029 return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
3031 write_carrays_preamble(fh);
3035 return CF_PRINT_WRITE_ERROR;
3038 callback_args.fh = fh;
3039 epan_dissect_init(&callback_args.edt, cf->epan, TRUE, TRUE);
3041 /* Iterate through the list of packets, printing the packets we were
3043 ret = process_specified_packets(cf, &print_args->range,
3045 "selected packets", TRUE,
3046 write_carrays_packet, &callback_args);
3048 epan_dissect_cleanup(&callback_args.edt);
3052 /* Completed successfully. */
3055 /* Well, the user decided to abort the printing. */
3058 /* Error while printing. */
3060 return CF_PRINT_WRITE_ERROR;
3063 write_carrays_finale(fh);
3067 return CF_PRINT_WRITE_ERROR;
3075 cf_find_packet_protocol_tree(capture_file *cf, const char *string,
3076 search_direction dir)
3080 mdata.string = string;
3081 mdata.string_len = strlen(string);
3082 return find_packet(cf, match_protocol_tree, &mdata, dir);
3086 cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree, match_data *mdata)
3088 mdata->frame_matched = FALSE;
3089 mdata->string = convert_string_case(cf->sfilter, cf->case_type);
3090 mdata->string_len = strlen(mdata->string);
3092 /* Iterate through all the nodes looking for matching text */
3093 proto_tree_children_foreach(tree, match_subtree_text, mdata);
3094 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
3098 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
3100 match_data *mdata = (match_data *)criterion;
3103 /* Load the frame's data. */
3104 if (!cf_read_frame(cf, fdata)) {
3105 /* Attempt to get the packet failed. */
3109 /* Construct the protocol tree, including the displayed text */
3110 epan_dissect_init(&edt, cf->epan, TRUE, TRUE);
3111 /* We don't need the column information */
3112 epan_dissect_run(&edt, &cf->phdr, frame_tvbuff_new_buffer(fdata, &cf->buf), fdata, NULL);
3114 /* Iterate through all the nodes, seeing if they have text that matches. */
3116 mdata->frame_matched = FALSE;
3117 proto_tree_children_foreach(edt.tree, match_subtree_text, mdata);
3118 epan_dissect_cleanup(&edt);
3119 return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
3123 match_subtree_text(proto_node *node, gpointer data)
3125 match_data *mdata = (match_data *) data;
3126 const gchar *string = mdata->string;
3127 size_t string_len = mdata->string_len;
3128 capture_file *cf = mdata->cf;
3129 field_info *fi = PNODE_FINFO(node);
3130 gchar label_str[ITEM_LABEL_LENGTH];
3137 /* dissection with an invisible proto tree? */
3140 if (mdata->frame_matched) {
3141 /* We already had a match; don't bother doing any more work. */
3145 /* Don't match invisible entries. */
3146 if (PROTO_ITEM_IS_HIDDEN(node))
3149 /* was a free format label produced? */
3151 label_ptr = fi->rep->representation;
3153 /* no, make a generic label */
3154 label_ptr = label_str;
3155 proto_item_fill_label(fi, label_str);
3158 /* Does that label match? */
3159 label_len = strlen(label_ptr);
3160 for (i = 0; i < label_len; i++) {
3161 c_char = label_ptr[i];
3163 c_char = toupper(c_char);
3164 if (c_char == string[c_match]) {
3166 if (c_match == string_len) {
3167 /* No need to look further; we have a match */
3168 mdata->frame_matched = TRUE;
3176 /* Recurse into the subtree, if it exists */
3177 if (node->first_child != NULL)
3178 proto_tree_children_foreach(node, match_subtree_text, mdata);
3182 cf_find_packet_summary_line(capture_file *cf, const char *string,
3183 search_direction dir)
3187 mdata.string = string;
3188 mdata.string_len = strlen(string);
3189 return find_packet(cf, match_summary_line, &mdata, dir);
3193 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
3195 match_data *mdata = (match_data *)criterion;
3196 const gchar *string = mdata->string;
3197 size_t string_len = mdata->string_len;
3199 const char *info_column;
3200 size_t info_column_len;
3201 match_result result = MR_NOTMATCHED;
3207 /* Load the frame's data. */
3208 if (!cf_read_frame(cf, fdata)) {
3209 /* Attempt to get the packet failed. */
3213 /* Don't bother constructing the protocol tree */
3214 epan_dissect_init(&edt, cf->epan, FALSE, FALSE);
3215 /* Get the column information */
3216 epan_dissect_run(&edt, &cf->phdr, frame_tvbuff_new_buffer(fdata, &cf->buf), fdata,
3219 /* Find the Info column */
3220 for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
3221 if (cf->cinfo.fmt_matx[colx][COL_INFO]) {
3222 /* Found it. See if we match. */
3223 info_column = edt.pi.cinfo->col_data[colx];
3224 info_column_len = strlen(info_column);
3225 for (i = 0; i < info_column_len; i++) {
3226 c_char = info_column[i];
3228 c_char = toupper(c_char);
3229 if (c_char == string[c_match]) {
3231 if (c_match == string_len) {
3232 result = MR_MATCHED;
3241 epan_dissect_cleanup(&edt);
3248 } cbs_t; /* "Counted byte string" */
3252 * The current match_* routines only support ASCII case insensitivity and don't
3253 * convert UTF-8 inputs to UTF-16 for matching.
3255 * We could modify them to use the GLib Unicode routines or the International
3256 * Components for Unicode library but it's not apparent that we could do so
3257 * without consuming a lot more CPU and memory or that searching would be
3258 * significantly better.
3262 cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
3263 search_direction dir)
3268 info.data_len = string_size;
3270 /* String or hex search? */
3272 /* String search - what type of string? */
3273 switch (cf->scs_type) {
3275 case SCS_NARROW_AND_WIDE:
3276 return find_packet(cf, match_narrow_and_wide, &info, dir);
3279 return find_packet(cf, match_narrow, &info, dir);
3282 return find_packet(cf, match_wide, &info, dir);
3285 g_assert_not_reached();
3289 return find_packet(cf, match_binary, &info, dir);
3293 match_narrow_and_wide(capture_file *cf, frame_data *fdata, void *criterion)
3295 cbs_t *info = (cbs_t *)criterion;
3296 const guint8 *ascii_text = info->data;
3297 size_t textlen = info->data_len;
3298 match_result result;
3305 /* Load the frame's data. */
3306 if (!cf_read_frame(cf, fdata)) {
3307 /* Attempt to get the packet failed. */
3311 result = MR_NOTMATCHED;
3312 buf_len = fdata->cap_len;
3313 pd = buffer_start_ptr(&cf->buf);
3315 while (i < buf_len) {
3318 c_char = toupper(c_char);
3319 if (c_char != '\0') {
3320 if (c_char == ascii_text[c_match]) {
3322 if (c_match == textlen) {
3323 result = MR_MATCHED;
3324 cf->search_pos = i; /* Save the position of the last character
3325 for highlighting the field. */
3330 g_assert(i>=c_match);
3331 i -= (guint32)c_match;
3341 match_narrow(capture_file *cf, frame_data *fdata, void *criterion)
3344 cbs_t *info = (cbs_t *)criterion;
3345 const guint8 *ascii_text = info->data;
3346 size_t textlen = info->data_len;
3347 match_result result;
3353 /* Load the frame's data. */
3354 if (!cf_read_frame(cf, fdata)) {
3355 /* Attempt to get the packet failed. */
3359 result = MR_NOTMATCHED;
3360 buf_len = fdata->cap_len;
3361 pd = buffer_start_ptr(&cf->buf);
3363 while (i < buf_len) {
3366 c_char = toupper(c_char);
3367 if (c_char == ascii_text[c_match]) {
3369 if (c_match == textlen) {
3370 result = MR_MATCHED;
3371 cf->search_pos = i; /* Save the position of the last character
3372 for highlighting the field. */
3377 g_assert(i>=c_match);
3378 i -= (guint32)c_match;
3388 match_wide(capture_file *cf, frame_data *fdata, void *criterion)
3390 cbs_t *info = (cbs_t *)criterion;
3391 const guint8 *ascii_text = info->data;
3392 size_t textlen = info->data_len;
3393 match_result result;
3400 /* Load the frame's data. */
3401 if (!cf_read_frame(cf, fdata)) {
3402 /* Attempt to get the packet failed. */
3406 result = MR_NOTMATCHED;
3407 buf_len = fdata->cap_len;
3408 pd = buffer_start_ptr(&cf->buf);
3410 while (i < buf_len) {
3413 c_char = toupper(c_char);
3414 if (c_char == ascii_text[c_match]) {
3416 if (c_match == textlen) {
3417 result = MR_MATCHED;
3418 cf->search_pos = i; /* Save the position of the last character
3419 for highlighting the field. */
3425 g_assert(i>=(c_match*2));
3426 i -= (guint32)c_match*2;
3435 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
3437 cbs_t *info = (cbs_t *)criterion;
3438 const guint8 *binary_data = info->data;
3439 size_t datalen = info->data_len;
3440 match_result result;
3446 /* Load the frame's data. */
3447 if (!cf_read_frame(cf, fdata)) {
3448 /* Attempt to get the packet failed. */
3452 result = MR_NOTMATCHED;
3453 buf_len = fdata->cap_len;
3454 pd = buffer_start_ptr(&cf->buf);
3456 while (i < buf_len) {
3457 if (pd[i] == binary_data[c_match]) {
3459 if (c_match == datalen) {
3460 result = MR_MATCHED;
3461 cf->search_pos = i; /* Save the position of the last character
3462 for highlighting the field. */
3467 g_assert(i>=c_match);
3468 i -= (guint32)c_match;
3477 cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode,
3478 search_direction dir)
3480 return find_packet(cf, match_dfilter, sfcode, dir);
3484 cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
3485 search_direction dir)
3490 if (!dfilter_compile(filter, &sfcode)) {
3492 * XXX - this shouldn't happen, as the filter string is machine
3497 if (sfcode == NULL) {
3499 * XXX - this shouldn't happen, as the filter string is machine
3504 result = find_packet(cf, match_dfilter, sfcode, dir);
3505 dfilter_free(sfcode);
3510 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
3512 dfilter_t *sfcode = (dfilter_t *)criterion;
3514 match_result result;
3516 /* Load the frame's data. */
3517 if (!cf_read_frame(cf, fdata)) {
3518 /* Attempt to get the packet failed. */
3522 epan_dissect_init(&edt, cf->epan, TRUE, FALSE);
3523 epan_dissect_prime_dfilter(&edt, sfcode);
3524 epan_dissect_run(&edt, &cf->phdr, frame_tvbuff_new_buffer(fdata, &cf->buf), fdata, NULL);
3525 result = dfilter_apply_edt(sfcode, &edt) ? MR_MATCHED : MR_NOTMATCHED;
3526 epan_dissect_cleanup(&edt);
3531 cf_find_packet_marked(capture_file *cf, search_direction dir)
3533 return find_packet(cf, match_marked, NULL, dir);
3537 match_marked(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3539 return fdata->flags.marked ? MR_MATCHED : MR_NOTMATCHED;
3543 cf_find_packet_time_reference(capture_file *cf, search_direction dir)
3545 return find_packet(cf, match_time_reference, NULL, dir);
3549 match_time_reference(capture_file *cf _U_, frame_data *fdata, void *criterion _U_)
3551 return fdata->flags.ref_time ? MR_MATCHED : MR_NOTMATCHED;
3555 find_packet(capture_file *cf,
3556 match_result (*match_function)(capture_file *, frame_data *, void *),
3557 void *criterion, search_direction dir)
3559 frame_data *start_fd;
3562 frame_data *new_fd = NULL;
3563 progdlg_t *progbar = NULL;
3568 GTimeVal start_time;
3569 gchar status_str[100];
3570 int progbar_nextstep;
3571 int progbar_quantum;
3573 match_result result;
3575 start_fd = cf->current_frame;
3576 if (start_fd != NULL) {
3577 /* Iterate through the list of packets, starting at the packet we've
3578 picked, calling a routine to run the filter on the packet, see if
3579 it matches, and stop if so. */
3581 framenum = start_fd->num;
3583 /* Update the progress bar when it gets to this value. */
3584 progbar_nextstep = 0;
3585 /* When we reach the value that triggers a progress bar update,
3586 bump that value by this amount. */
3587 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
3588 /* Progress so far. */
3592 g_get_current_time(&start_time);
3594 title = cf->sfilter?cf->sfilter:"";
3596 /* Create the progress bar if necessary.
3597 We check on every iteration of the loop, so that it takes no
3598 longer than the standard time to create it (otherwise, for a
3599 large file, we might take considerably longer than that standard
3600 time in order to get to the next progress bar step). */
3601 if (progbar == NULL)
3602 progbar = delayed_create_progress_dlg(cf->window, "Searching", title,
3603 FALSE, &stop_flag, &start_time, progbar_val);
3605 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
3606 when we update it, we have to run the GTK+ main loop to get it
3607 to repaint what's pending, and doing so may involve an "ioctl()"
3608 to see if there's any pending input from an X server, and doing
3609 that for every packet can be costly, especially on a big file. */
3610 if (count >= progbar_nextstep) {
3611 /* let's not divide by zero. I should never be started
3612 * with count == 0, so let's assert that
3614 g_assert(cf->count > 0);
3616 progbar_val = (gfloat) count / cf->count;
3618 if (progbar != NULL) {
3619 g_snprintf(status_str, sizeof(status_str),
3620 "%4u of %u packets", count, cf->count);
3621 update_progress_dlg(progbar, progbar_val, status_str);
3624 progbar_nextstep += progbar_quantum;
3628 /* Well, the user decided to abort the search. Go back to the
3629 frame where we started. */
3634 /* Go past the current frame. */
3635 if (dir == SD_BACKWARD) {
3636 /* Go on to the previous frame. */
3637 if (framenum == 1) {
3639 * XXX - other apps have a bit more of a detailed message
3640 * for this, and instead of offering "OK" and "Cancel",
3641 * they offer things such as "Continue" and "Cancel";
3642 * we need an API for popping up alert boxes with
3643 * {Verb} and "Cancel".
3646 if (prefs.gui_find_wrap)
3648 statusbar_push_temporary_msg("Search reached the beginning. Continuing at end.");
3649 framenum = cf->count; /* wrap around */
3653 statusbar_push_temporary_msg("Search reached the beginning.");
3654 framenum = start_fd->num; /* stay on previous packet */
3659 /* Go on to the next frame. */
3660 if (framenum == cf->count) {
3661 if (prefs.gui_find_wrap)
3663 statusbar_push_temporary_msg("Search reached the end. Continuing at beginning.");
3664 framenum = 1; /* wrap around */
3668 statusbar_push_temporary_msg("Search reached the end.");
3669 framenum = start_fd->num; /* stay on previous packet */
3674 fdata = frame_data_sequence_find(cf->frames, framenum);
3678 /* Is this packet in the display? */
3679 if (fdata->flags.passed_dfilter) {
3680 /* Yes. Does it match the search criterion? */
3681 result = (*match_function)(cf, fdata, criterion);
3682 if (result == MR_ERROR) {
3683 /* Error; our caller has reported the error. Go back to the frame
3684 where we started. */
3687 } else if (result == MR_MATCHED) {
3688 /* Yes. Go to the new frame. */
3694 if (fdata == start_fd) {
3695 /* We're back to the frame we were on originally, and that frame
3696 doesn't match the search filter. The search failed. */
3701 /* We're done scanning the packets; destroy the progress bar if it
3703 if (progbar != NULL)
3704 destroy_progress_dlg(progbar);
3707 if (new_fd != NULL) {
3708 /* Find and select */
3709 cf->search_in_progress = TRUE;
3710 found = packet_list_select_row_from_data(new_fd);
3711 cf->search_in_progress = FALSE;
3712 cf->search_pos = 0; /* Reset the position */
3714 /* We didn't find a row corresponding to this frame.
3715 This means that the frame isn't being displayed currently,
3716 so we can't select it. */
3717 simple_message_box(ESD_TYPE_INFO, NULL,
3718 "The capture file is probably not fully dissected.",
3719 "End of capture exceeded!");
3722 return TRUE; /* success */
3724 return FALSE; /* failure */
3728 cf_goto_frame(capture_file *cf, guint fnumber)
3732 fdata = frame_data_sequence_find(cf->frames, fnumber);
3734 if (fdata == NULL) {
3735 /* we didn't find a packet with that packet number */
3736 statusbar_push_temporary_msg("There is no packet number %u.", fnumber);
3737 return FALSE; /* we failed to go to that packet */
3739 if (!fdata->flags.passed_dfilter) {
3740 /* that packet currently isn't displayed */
3741 /* XXX - add it to the set of displayed packets? */
3742 statusbar_push_temporary_msg("Packet number %u isn't displayed.", fnumber);
3743 return FALSE; /* we failed to go to that packet */
3746 if (!packet_list_select_row_from_data(fdata)) {
3747 /* We didn't find a row corresponding to this frame.
3748 This means that the frame isn't being displayed currently,
3749 so we can't select it. */
3750 simple_message_box(ESD_TYPE_INFO, NULL,
3751 "The capture file is probably not fully dissected.",
3752 "End of capture exceeded!");
3755 return TRUE; /* we got to that packet */
3759 cf_goto_top_frame(void)
3761 /* Find and select */
3762 packet_list_select_first_row();
3763 return TRUE; /* we got to that packet */
3767 cf_goto_bottom_frame(void)
3769 /* Find and select */
3770 packet_list_select_last_row();
3771 return TRUE; /* we got to that packet */
3775 * Go to frame specified by currently selected protocol tree item.
3778 cf_goto_framenum(capture_file *cf)
3780 header_field_info *hfinfo;
3783 if (cf->finfo_selected) {
3784 hfinfo = cf->finfo_selected->hfinfo;
3786 if (hfinfo->type == FT_FRAMENUM) {
3787 framenum = fvalue_get_uinteger(&cf->finfo_selected->value);
3789 return cf_goto_frame(cf, framenum);
3796 /* Select the packet on a given row. */
3798 cf_select_packet(capture_file *cf, int row)
3800 epan_dissect_t *old_edt;
3803 /* Get the frame data struct pointer for this frame */
3804 fdata = packet_list_get_row_data(row);
3806 if (fdata == NULL) {
3807 /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
3808 the first entry is added to it by "real_insert_row()", that row
3809 is selected (see "real_insert_row()", in "ui/gtk/gtkclist.c", in both
3810 our version and the vanilla GTK+ version).
3812 This means that a "select-row" signal is emitted; this causes
3813 "packet_list_select_cb()" to be called, which causes "cf_select_packet()"
3816 "cf_select_packet()" fetches, above, the data associated with the
3817 row that was selected; however, as "gtk_clist_append()", which
3818 called "real_insert_row()", hasn't yet returned, we haven't yet
3819 associated any data with that row, so we get back a null pointer.
3821 We can't assume that there's only one frame in the frame list,
3822 either, as we may be filtering the display.
3824 We therefore assume that, if "row" is 0, i.e. the first row
3825 is being selected, and "cf->first_displayed" equals
3826 "cf->last_displayed", i.e. there's only one frame being
3827 displayed, that frame is the frame we want.
3829 This means we have to set "cf->first_displayed" and
3830 "cf->last_displayed" before adding the row to the
3831 GtkCList; see the comment in "add_packet_to_packet_list()". */
3833 if (row == 0 && cf->first_displayed == cf->last_displayed)
3834 fdata = frame_data_sequence_find(cf->frames, cf->first_displayed);
3837 /* If fdata _still_ isn't set simply give up. */
3838 if (fdata == NULL) {
3842 /* Get the data in that frame. */
3843 if (!cf_read_frame (cf, fdata)) {
3847 /* Record that this frame is the current frame. */
3848 cf->current_frame = fdata;
3849 cf->current_row = row;
3852 /* Create the logical protocol tree. */
3853 /* We don't need the columns here. */
3854 cf->edt = epan_dissect_new(cf->epan, TRUE, TRUE);
3856 tap_build_interesting(cf->edt);
3857 epan_dissect_run(cf->edt, &cf->phdr, frame_tvbuff_new_buffer(cf->current_frame, &cf->buf),
3858 cf->current_frame, NULL);
3860 dfilter_macro_build_ftv_cache(cf->edt->tree);
3862 cf_callback_invoke(cf_cb_packet_selected, cf);
3864 if (old_edt != NULL)
3865 epan_dissect_free(old_edt);
3869 /* Unselect the selected packet, if any. */
3871 cf_unselect_packet(capture_file *cf)
3873 epan_dissect_t *old_edt = cf->edt;
3877 /* No packet is selected. */
3878 cf->current_frame = NULL;
3879 cf->current_row = 0;
3881 cf_callback_invoke(cf_cb_packet_unselected, cf);
3883 /* No protocol tree means no selected field. */
3884 cf_unselect_field(cf);
3886 /* Destroy the epan_dissect_t for the unselected packet. */
3887 if (old_edt != NULL)
3888 epan_dissect_free(old_edt);
3891 /* Unset the selected protocol tree field, if any. */
3893 cf_unselect_field(capture_file *cf)
3895 cf->finfo_selected = NULL;
3897 cf_callback_invoke(cf_cb_field_unselected, cf);
3901 * Mark a particular frame.
3904 cf_mark_frame(capture_file *cf, frame_data *frame)
3906 if (! frame->flags.marked) {
3907 frame->flags.marked = TRUE;
3908 if (cf->count > cf->marked_count)
3914 * Unmark a particular frame.
3917 cf_unmark_frame(capture_file *cf, frame_data *frame)
3919 if (frame->flags.marked) {
3920 frame->flags.marked = FALSE;
3921 if (cf->marked_count > 0)
3927 * Ignore a particular frame.
3930 cf_ignore_frame(capture_file *cf, frame_data *frame)
3932 if (! frame->flags.ignored) {
3933 frame->flags.ignored = TRUE;
3934 if (cf->count > cf->ignored_count)
3935 cf->ignored_count++;
3940 * Un-ignore a particular frame.
3943 cf_unignore_frame(capture_file *cf, frame_data *frame)
3945 if (frame->flags.ignored) {
3946 frame->flags.ignored = FALSE;
3947 if (cf->ignored_count > 0)
3948 cf->ignored_count--;
3953 * Read the comment in SHB block
3957 cf_read_shb_comment(capture_file *cf)
3959 wtapng_section_t *shb_inf;
3960 const gchar *temp_str;
3962 /* Get info from SHB */
3963 shb_inf = wtap_file_get_shb_info(cf->wth);
3964 if (shb_inf == NULL)
3966 temp_str = shb_inf->opt_comment;
3974 cf_update_capture_comment(capture_file *cf, gchar *comment)
3976 wtapng_section_t *shb_inf;
3978 /* Get info from SHB */
3979 shb_inf = wtap_file_get_shb_info(cf->wth);
3981 /* See if the comment has changed or not */
3982 if (shb_inf && shb_inf->opt_comment) {
3983 if (strcmp(shb_inf->opt_comment, comment) == 0) {
3992 /* The comment has changed, let's update it */
3993 wtap_write_shb_comment(cf->wth, comment);
3994 /* Mark the file as having unsaved changes */
3995 cf->unsaved_changes = TRUE;
3999 cf_get_user_packet_comment(capture_file *cf, const frame_data *fd)
4001 if (cf->frames_user_comments)
4002 return (const char *)g_tree_lookup(cf->frames_user_comments, fd);
4009 cf_get_comment(capture_file *cf, const frame_data *fd)
4011 /* fetch user comment */
4012 if (fd->flags.has_user_comment)
4013 return g_strdup(cf_get_user_packet_comment(cf, fd));
4015 /* fetch phdr comment */
4016 if (fd->flags.has_phdr_comment) {
4017 struct wtap_pkthdr phdr; /* Packet header */
4018 Buffer buf; /* Packet data */
4020 phdr.opt_comment = NULL;
4022 buffer_init(&buf, 1500);
4023 if (!cf_read_frame_r(cf, fd, &phdr, &buf))
4024 { /* XXX, what we can do here? */ }
4027 return phdr.opt_comment;
4033 frame_cmp(gconstpointer a, gconstpointer b, gpointer user_data _U_)
4035 const frame_data *fdata1 = (const frame_data *) a;
4036 const frame_data *fdata2 = (const frame_data *) b;
4038 return (fdata1->num < fdata2->num) ? -1 :
4039 (fdata1->num > fdata2->num) ? 1 :
4044 cf_set_user_packet_comment(capture_file *cf, frame_data *fd, const gchar *new_comment)
4046 char *pkt_comment = cf_get_comment(cf, fd);
4048 /* Check if the comment has changed */
4049 if (!g_strcmp0(pkt_comment, new_comment)) {
4050 g_free(pkt_comment);
4053 g_free(pkt_comment);
4056 cf->packet_comment_count--;
4059 cf->packet_comment_count++;
4061 fd->flags.has_user_comment = TRUE;
4063 if (!cf->frames_user_comments)
4064 cf->frames_user_comments = g_tree_new_full(frame_cmp, NULL, NULL, g_free);
4066 /* insert new packet comment */
4067 g_tree_replace(cf->frames_user_comments, fd, g_strdup(new_comment));
4069 expert_update_comment_count(cf->packet_comment_count);
4071 /* OK, we have unsaved changes. */
4072 cf->unsaved_changes = TRUE;
4077 * What types of comments does this capture file have?
4080 cf_comment_types(capture_file *cf)
4082 guint32 comment_types = 0;
4084 if (cf_read_shb_comment(cf) != NULL)
4085 comment_types |= WTAP_COMMENT_PER_SECTION;
4086 if (cf->packet_comment_count != 0)
4087 comment_types |= WTAP_COMMENT_PER_PACKET;
4088 return comment_types;
4095 } save_callback_args_t;
4098 * Save a capture to a file, in a particular format, saving either
4099 * all packets, all currently-displayed packets, or all marked packets.
4101 * Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
4102 * up a message box for the failure.
4105 save_packet(capture_file *cf _U_, frame_data *fdata,
4106 struct wtap_pkthdr *phdr, const guint8 *pd,
4109 save_callback_args_t *args = (save_callback_args_t *)argsp;
4110 struct wtap_pkthdr hdr;
4112 gchar *display_basename;
4113 const char *pkt_comment;
4115 if (fdata->flags.has_user_comment)
4116 pkt_comment = cf_get_user_packet_comment(cf, fdata);
4118 pkt_comment = phdr->opt_comment;
4120 /* init the wtap header for saving */
4121 /* TODO: reuse phdr */
4122 /* XXX - these are the only flags that correspond to data that we have
4123 in the frame_data structure and that matter on a per-packet basis.
4125 For WTAP_HAS_CAP_LEN, either the file format has separate "captured"
4126 and "on the wire" lengths, or it doesn't.
4128 For WTAP_HAS_DROP_COUNT, Wiretap doesn't actually supply the value
4131 For WTAP_HAS_PACK_FLAGS, we currently don't save the FCS length
4132 from the packet flags. */
4133 hdr.presence_flags = 0;
4134 if (fdata->flags.has_ts)
4135 hdr.presence_flags |= WTAP_HAS_TS;
4136 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID)
4137 hdr.presence_flags |= WTAP_HAS_INTERFACE_ID;
4138 if (phdr->presence_flags & WTAP_HAS_PACK_FLAGS)
4139 hdr.presence_flags |= WTAP_HAS_PACK_FLAGS;
4140 hdr.ts.secs = fdata->abs_ts.secs;
4141 hdr.ts.nsecs = fdata->abs_ts.nsecs;
4142 hdr.caplen = fdata->cap_len;
4143 hdr.len = fdata->pkt_len;
4144 hdr.pkt_encap = fdata->lnk_t;
4146 hdr.interface_id = phdr->interface_id; /* identifier of the interface. */
4148 hdr.pack_flags = phdr->pack_flags;
4149 hdr.opt_comment = g_strdup(pkt_comment);
4152 hdr.pseudo_header = phdr->pseudo_header;
4155 hdr.pack_flags = /* XXX - 0 for now (any value for "we don't have it"?) */
4157 /* and save the packet */
4158 if (!wtap_dump(args->pdh, &hdr, pd, &err)) {
4160 /* Wiretap error. */
4163 case WTAP_ERR_UNSUPPORTED_ENCAP:
4165 * This is a problem with the particular frame we're writing and
4166 * the file type and subtype we're writing; note that, and report
4167 * the frame number and file type/subtype.
4169 simple_error_message_box(
4170 "Frame %u has a network type that can't be saved in a \"%s\" file.",
4171 fdata->num, wtap_file_type_subtype_string(args->file_type));
4174 case WTAP_ERR_PACKET_TOO_LARGE:
4176 * This is a problem with the particular frame we're writing and
4177 * the file type and subtype we're writing; note that, and report
4178 * the frame number and file type/subtype.
4180 simple_error_message_box(
4181 "Frame %u is larger than Wireshark supports in a \"%s\" file.",
4182 fdata->num, wtap_file_type_subtype_string(args->file_type));
4186 display_basename = g_filename_display_basename(args->fname);
4187 simple_error_message_box(
4188 "An error occurred while writing to the file \"%s\": %s.",
4189 display_basename, wtap_strerror(err));
4190 g_free(display_basename);
4195 write_failure_alert_box(args->fname, err);
4200 g_free(hdr.opt_comment);
4205 * Can this capture file be written out in any format using Wiretap
4206 * rather than by copying the raw data?
4209 cf_can_write_with_wiretap(capture_file *cf)
4211 /* We don't care whether we support the comments in this file or not;
4212 if we can't, we'll offer the user the option of discarding the
4214 return wtap_dump_can_write(cf->linktypes, 0);
4218 * Should we let the user do a save?
4222 * the file has unsaved changes, and we can save it in some
4223 * format through Wiretap
4227 * the file is a temporary file and has no unsaved changes (so
4228 * that "saving" it just means copying it).
4230 * XXX - we shouldn't allow files to be edited if they can't be saved,
4231 * so cf->unsaved_changes should be true only if the file can be saved.
4233 * We don't care whether we support the comments in this file or not;
4234 * if we can't, we'll offer the user the option of discarding the
4238 cf_can_save(capture_file *cf)
4240 if (cf->unsaved_changes && wtap_dump_can_write(cf->linktypes, 0)) {
4241 /* Saved changes, and we can write it out with Wiretap. */
4245 if (cf->is_tempfile && !cf->unsaved_changes) {
4247 * Temporary file with no unsaved changes, so we can just do a
4253 /* Nothing to save. */
4258 * Should we let the user do a "save as"?
4262 * we can save it in some format through Wiretap
4266 * the file is a temporary file and has no unsaved changes (so
4267 * that "saving" it just means copying it).
4269 * XXX - we shouldn't allow files to be edited if they can't be saved,
4270 * so cf->unsaved_changes should be true only if the file can be saved.
4272 * We don't care whether we support the comments in this file or not;
4273 * if we can't, we'll offer the user the option of discarding the
4277 cf_can_save_as(capture_file *cf)
4279 if (wtap_dump_can_write(cf->linktypes, 0)) {
4280 /* We can write it out with Wiretap. */
4284 if (cf->is_tempfile && !cf->unsaved_changes) {
4286 * Temporary file with no unsaved changes, so we can just do a
4292 /* Nothing to save. */
4297 * Does this file have unsaved data?
4300 cf_has_unsaved_data(capture_file *cf)
4303 * If this is a temporary file, or a file with unsaved changes, it
4306 return cf->is_tempfile || cf->unsaved_changes;
4310 * Quick scan to find packet offsets.
4312 static cf_read_status_t
4313 rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
4315 const struct wtap_pkthdr *phdr;
4320 progdlg_t *progbar = NULL;
4324 GTimeVal start_time;
4325 gchar status_str[100];
4326 gint64 progbar_nextstep;
4327 gint64 progbar_quantum;
4332 int displayed_once = 0;
4335 /* Close the old handle. */
4336 wtap_close(cf->wth);
4338 /* Open the new file. */
4339 cf->wth = wtap_open_offline(fname, WTAP_TYPE_AUTO, err, &err_info, TRUE);
4340 if (cf->wth == NULL) {
4341 cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
4342 return CF_READ_ERROR;
4345 /* We're scanning a file whose contents should be the same as what
4346 we had before, so we don't discard dissection state etc.. */
4349 /* Set the file name because we need it to set the follow stream filter.
4350 XXX - is that still true? We need it for other reasons, though,
4352 cf->filename = g_strdup(fname);
4354 /* Indicate whether it's a permanent or temporary file. */
4355 cf->is_tempfile = is_tempfile;
4357 /* No user changes yet. */
4358 cf->unsaved_changes = FALSE;
4360 cf->cd_t = wtap_file_type_subtype(cf->wth);
4361 cf->linktypes = g_array_sized_new(FALSE, FALSE, (guint) sizeof(int), 1);
4363 cf->snap = wtap_snapshot_length(cf->wth);
4364 if (cf->snap == 0) {
4365 /* Snapshot length not known. */
4366 cf->has_snap = FALSE;
4367 cf->snap = WTAP_MAX_PACKET_SIZE;
4369 cf->has_snap = TRUE;
4371 name_ptr = g_filename_display_basename(cf->filename);
4373 cf_callback_invoke(cf_cb_file_rescan_started, cf);
4375 /* Record whether the file is compressed.
4376 XXX - do we know this at open time? */
4377 cf->iscompressed = wtap_iscompressed(cf->wth);
4379 /* Find the size of the file. */
4380 size = wtap_file_size(cf->wth, NULL);
4382 /* Update the progress bar when it gets to this value. */
4383 progbar_nextstep = 0;
4384 /* When we reach the value that triggers a progress bar update,
4385 bump that value by this amount. */
4387 progbar_quantum = size/N_PROGBAR_UPDATES;
4388 if (progbar_quantum < MIN_QUANTUM)
4389 progbar_quantum = MIN_QUANTUM;
4391 progbar_quantum = 0;
4394 g_get_current_time(&start_time);
4397 phdr = wtap_phdr(cf->wth);
4398 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
4400 fdata = frame_data_sequence_find(cf->frames, framenum);
4401 fdata->file_off = data_offset;
4404 file_pos = wtap_read_so_far(cf->wth);
4406 /* Create the progress bar if necessary.
4407 * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
4409 if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)) {
4410 progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
4411 progbar = delayed_create_progress_dlg(cf->window, "Rescanning", name_ptr,
4412 TRUE, &stop_flag, &start_time, progbar_val);
4415 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
4416 when we update it, we have to run the GTK+ main loop to get it
4417 to repaint what's pending, and doing so may involve an "ioctl()"
4418 to see if there's any pending input from an X server, and doing
4419 that for every packet can be costly, especially on a big file. */
4420 if (file_pos >= progbar_nextstep) {
4421 if (progbar != NULL) {
4422 progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
4423 /* update the packet bar content on the first run or frequently on very large files */
4425 if (progbar_quantum > 500000 || displayed_once == 0) {
4426 if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->count != 0) {
4428 packets_bar_update();
4431 #endif /* HAVE_LIBPCAP */
4432 update_progress_dlg(progbar, progbar_val, status_str);
4434 progbar_nextstep += progbar_quantum;
4439 /* Well, the user decided to abort the rescan. Sadly, as this
4440 isn't a reread, recovering is difficult, so we'll just
4441 close the current capture. */
4445 /* Add this packet's link-layer encapsulation type to cf->linktypes, if
4446 it's not already there.
4447 XXX - yes, this is O(N), so if every packet had a different
4448 link-layer encapsulation type, it'd be O(N^2) to read the file, but
4449 there are probably going to be a small number of encapsulation types
4451 cf_add_encapsulation_type(cf, phdr->pkt_encap);
4454 /* Free the display name */
4457 /* We're done reading the file; destroy the progress bar if it was created. */
4458 if (progbar != NULL)
4459 destroy_progress_dlg(progbar);
4461 /* We're done reading sequentially through the file. */
4462 cf->state = FILE_READ_DONE;
4464 /* Close the sequential I/O side, to free up memory it requires. */
4465 wtap_sequential_close(cf->wth);
4467 /* compute the time it took to load the file */
4468 compute_elapsed(cf, &start_time);
4470 /* Set the file encapsulation type now; we don't know what it is until
4471 we've looked at all the packets, as we don't know until then whether
4472 there's more than one type (and thus whether it's
4473 WTAP_ENCAP_PER_PACKET). */
4474 cf->lnk_t = wtap_file_encap(cf->wth);
4476 cf_callback_invoke(cf_cb_file_rescan_finished, cf);
4479 /* Our caller will give up at this point. */
4480 return CF_READ_ABORTED;
4484 /* Put up a message box noting that the read failed somewhere along
4485 the line. Don't throw out the stuff we managed to read, though,
4489 case WTAP_ERR_UNSUPPORTED:
4490 simple_error_message_box(
4491 "The capture file contains record data that Wireshark doesn't support.\n(%s)",
4496 case WTAP_ERR_UNSUPPORTED_ENCAP:
4497 simple_error_message_box(
4498 "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
4503 case WTAP_ERR_CANT_READ:
4504 simple_error_message_box(
4505 "An attempt to read from the capture file failed for"
4506 " some unknown reason.");
4509 case WTAP_ERR_SHORT_READ:
4510 simple_error_message_box(
4511 "The capture file appears to have been cut short"
4512 " in the middle of a packet.");
4515 case WTAP_ERR_BAD_FILE:
4516 simple_error_message_box(
4517 "The capture file appears to be damaged or corrupt.\n(%s)",
4522 case WTAP_ERR_DECOMPRESS:
4523 simple_error_message_box(
4524 "The compressed capture file appears to be damaged or corrupt.\n"
4530 simple_error_message_box(
4531 "An error occurred while reading the"
4532 " capture file: %s.", wtap_strerror(*err));
4535 return CF_READ_ERROR;
4541 cf_save_packets(capture_file *cf, const char *fname, guint save_format,
4542 gboolean compressed, gboolean discard_comments,
4543 gboolean dont_reopen)
4546 gchar *fname_new = NULL;
4549 addrinfo_lists_t *addr_lists;
4553 gchar *display_basename;
4560 save_callback_args_t callback_args;
4562 cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
4564 addr_lists = get_addrinfo_list();
4566 if (save_format == cf->cd_t && compressed == cf->iscompressed
4567 && !discard_comments && !cf->unsaved_changes
4568 && !(addr_lists && wtap_dump_has_name_resolution(save_format))) {
4569 /* We're saving in the format it's already in, and we're
4570 not discarding comments, and there are no changes we have
4571 in memory that aren't saved to the file, and we have no name
4572 resolution blocks to write, so we can just move or copy the raw data. */
4574 if (cf->is_tempfile) {
4575 /* The file being saved is a temporary file from a live
4576 capture, so it doesn't need to stay around under that name;
4577 first, try renaming the capture buffer file to the new name.
4578 This acts as a "safe save", in that, if the file already
4579 exists, the existing file will be removed only if the rename
4582 Sadly, on Windows, as we have the current capture file
4583 open, even MoveFileEx() with MOVEFILE_REPLACE_EXISTING
4584 (to cause the rename to remove an existing target), as
4585 done by ws_stdio_rename() (ws_rename() is #defined to
4586 be ws_stdio_rename() on Windows) will fail.
4588 According to the MSDN documentation for CreateFile(), if,
4589 when we open a capture file, we were to directly do a CreateFile(),
4590 opening with FILE_SHARE_DELETE|FILE_SHARE_READ, and then
4591 convert it to a file descriptor with _open_osfhandle(),
4592 that would allow the file to be renamed out from under us.
4594 However, that doesn't work in practice. Perhaps the problem
4595 is that the process doing the rename is the process that
4596 has the file open. */
4598 if (ws_rename(cf->filename, fname) == 0) {
4599 /* That succeeded - there's no need to copy the source file. */
4600 how_to_save = SAVE_WITH_MOVE;
4602 if (errno == EXDEV) {
4603 /* They're on different file systems, so we have to copy the
4605 how_to_save = SAVE_WITH_COPY;
4607 /* The rename failed, but not because they're on different
4608 file systems - put up an error message. (Or should we
4609 just punt and try to copy? The only reason why I'd
4610 expect the rename to fail and the copy to succeed would
4611 be if we didn't have permission to remove the file from
4612 the temporary directory, and that might be fixable - but
4613 is it worth requiring the user to go off and fix it?) */
4614 cf_rename_failure_alert_box(fname, errno);
4619 how_to_save = SAVE_WITH_COPY;
4622 /* It's a permanent file, so we should copy it, and not remove the
4624 how_to_save = SAVE_WITH_COPY;
4627 if (how_to_save == SAVE_WITH_COPY) {
4628 /* Copy the file, if we haven't moved it. If we're overwriting
4629 an existing file, we do it with a "safe save", by writing
4630 to a new file and, if the write succeeds, renaming the
4631 new file on top of the old file. */
4632 if (file_exists(fname)) {
4633 fname_new = g_strdup_printf("%s~", fname);
4634 if (!copy_file_binary_mode(cf->filename, fname_new))
4637 if (!copy_file_binary_mode(cf->filename, fname))
4642 /* Either we're saving in a different format or we're saving changes,
4643 such as added, modified, or removed comments, that haven't yet
4644 been written to the underlying file; we can't do that by copying
4645 or moving the capture file, we have to do it by writing the packets
4648 wtapng_section_t *shb_hdr = NULL;
4649 wtapng_iface_descriptions_t *idb_inf = NULL;
4652 shb_hdr = wtap_file_get_shb_info(cf->wth);
4653 idb_inf = wtap_file_get_idb_info(cf->wth);
4655 /* Determine what file encapsulation type we should use. */
4656 encap = wtap_dump_file_encap_type(cf->linktypes);
4658 if (file_exists(fname)) {
4659 /* We're overwriting an existing file; write out to a new file,
4660 and, if that succeeds, rename the new file on top of the
4661 old file. That makes this a "safe save", so that we don't
4662 lose the old file if we have a problem writing out the new
4663 file. (If the existing file is the current capture file,
4664 we *HAVE* to do that, otherwise we're overwriting the file
4665 from which we're reading the packets that we're writing!) */
4666 fname_new = g_strdup_printf("%s~", fname);
4667 pdh = wtap_dump_open_ng(fname_new, save_format, encap, cf->snap,
4668 compressed, shb_hdr, idb_inf, &err);
4670 pdh = wtap_dump_open_ng(fname, save_format, encap, cf->snap,
4671 compressed, shb_hdr, idb_inf, &err);
4677 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
4681 /* Add address resolution */
4682 wtap_dump_set_addrinfo_list(pdh, addr_lists);
4684 /* Iterate through the list of packets, processing all the packets. */
4685 callback_args.pdh = pdh;
4686 callback_args.fname = fname;
4687 callback_args.file_type = save_format;
4688 switch (process_specified_packets(cf, NULL, "Saving", "packets",
4689 TRUE, save_packet, &callback_args)) {
4692 /* Completed successfully. */
4696 /* The user decided to abort the saving.
4697 If we're writing to a temporary file, remove it.
4698 XXX - should we do so even if we're not writing to a
4700 wtap_dump_close(pdh, &err);
4701 if (fname_new != NULL)
4702 ws_unlink(fname_new);
4703 cf_callback_invoke(cf_cb_file_save_stopped, NULL);
4704 return CF_WRITE_ABORTED;
4707 /* Error while saving.
4708 If we're writing to a temporary file, remove it. */
4709 if (fname_new != NULL)
4710 ws_unlink(fname_new);
4711 wtap_dump_close(pdh, &err);
4715 if (!wtap_dump_close(pdh, &err)) {
4716 cf_close_failure_alert_box(fname, err);
4720 how_to_save = SAVE_WITH_WTAP;
4723 if (fname_new != NULL) {
4724 /* We wrote out to fname_new, and should rename it on top of
4725 fname. fname_new is now closed, so that should be possible even
4726 on Windows. However, on Windows, we first need to close whatever
4727 file descriptors we have open for fname. */
4729 wtap_fdclose(cf->wth);
4731 /* Now do the rename. */
4732 if (ws_rename(fname_new, fname) == -1) {
4733 /* Well, the rename failed. */
4734 cf_rename_failure_alert_box(fname, errno);
4736 /* Attempt to reopen the random file descriptor using the
4737 current file's filename. (At this point, the sequential
4738 file descriptor is closed.) */
4739 if (!wtap_fdreopen(cf->wth, cf->filename, &err)) {
4740 /* Oh, well, we're screwed. */
4741 display_basename = g_filename_display_basename(cf->filename);
4742 simple_error_message_box(
4743 file_open_error_message(err, FALSE), display_basename);
4744 g_free(display_basename);
4751 cf_callback_invoke(cf_cb_file_save_finished, NULL);
4752 cf->unsaved_changes = FALSE;
4755 switch (how_to_save) {
4757 case SAVE_WITH_MOVE:
4758 /* We just moved the file, so the wtap structure refers to the
4759 new file, and all the information other than the filename
4760 and the "is temporary" status applies to the new file; just
4762 g_free(cf->filename);
4763 cf->filename = g_strdup(fname);
4764 cf->is_tempfile = FALSE;
4765 cf_callback_invoke(cf_cb_file_fast_save_finished, cf);
4768 case SAVE_WITH_COPY:
4769 /* We just copied the file, s all the information other than
4770 the wtap structure, the filename, and the "is temporary"
4771 status applies to the new file; just update that. */
4772 wtap_close(cf->wth);
4773 cf->wth = wtap_open_offline(fname, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
4774 if (cf->wth == NULL) {
4775 cf_open_failure_alert_box(fname, err, err_info, FALSE, 0);
4778 g_free(cf->filename);
4779 cf->filename = g_strdup(fname);
4780 cf->is_tempfile = FALSE;
4782 cf_callback_invoke(cf_cb_file_fast_save_finished, cf);
4785 case SAVE_WITH_WTAP:
4786 /* Open and read the file we saved to.
4788 XXX - this is somewhat of a waste; we already have the
4789 packets, all this gets us is updated file type information
4790 (which we could just stuff into "cf"), and having the new
4791 file be the one we have opened and from which we're reading
4792 the data, and it means we have to spend time opening and
4793 reading the file, which could be a significant amount of
4794 time if the file is large.
4796 If the capture-file-writing code were to return the
4797 seek offset of each packet it writes, we could save that
4798 in the frame_data structure for the frame, and just open
4799 the file without reading it again...
4801 ...as long as, for gzipped files, the process of writing
4802 out the file *also* generates the information needed to
4803 support fast random access to the compressed file. */
4804 if (rescan_file(cf, fname, FALSE, &err) != CF_READ_OK) {
4805 /* The rescan failed; just close the file. Either
4806 a dialog was popped up for the failure, so the
4807 user knows what happened, or they stopped the
4808 rescan, in which case they know what happened. */
4814 /* If we were told to discard the comments, do so. */
4815 if (discard_comments) {
4816 /* Remove SHB comment, if any. */
4817 wtap_write_shb_comment(cf->wth, NULL);
4819 /* remove all user comments */
4820 for (framenum = 1; framenum <= cf->count; framenum++) {
4821 fdata = frame_data_sequence_find(cf->frames, framenum);
4823 fdata->flags.has_phdr_comment = FALSE;
4824 fdata->flags.has_user_comment = FALSE;
4827 if (cf->frames_user_comments) {
4828 g_tree_destroy(cf->frames_user_comments);
4829 cf->frames_user_comments = NULL;
4832 cf->packet_comment_count = 0;
4838 if (fname_new != NULL) {
4839 /* We were trying to write to a temporary file; get rid of it if it
4840 exists. (We don't care whether this fails, as, if it fails,
4841 there's not much we can do about it. I guess if it failed for
4842 a reason other than "it doesn't exist", we could report an
4843 error, so the user knows there's a junk file that they might
4844 want to clean up.) */
4845 ws_unlink(fname_new);
4848 cf_callback_invoke(cf_cb_file_save_failed, NULL);
4849 return CF_WRITE_ERROR;
4853 cf_export_specified_packets(capture_file *cf, const char *fname,
4854 packet_range_t *range, guint save_format,
4855 gboolean compressed)
4857 gchar *fname_new = NULL;
4860 save_callback_args_t callback_args;
4861 wtapng_section_t *shb_hdr;
4862 wtapng_iface_descriptions_t *idb_inf;
4865 cf_callback_invoke(cf_cb_file_export_specified_packets_started, (gpointer)fname);
4867 packet_range_process_init(range);
4869 /* We're writing out specified packets from the specified capture
4870 file to another file. Even if all captured packets are to be
4871 written, don't special-case the operation - read each packet
4872 and then write it out if it's one of the specified ones. */
4874 shb_hdr = wtap_file_get_shb_info(cf->wth);
4875 idb_inf = wtap_file_get_idb_info(cf->wth);
4877 /* Determine what file encapsulation type we should use. */
4878 encap = wtap_dump_file_encap_type(cf->linktypes);
4880 if (file_exists(fname)) {
4881 /* We're overwriting an existing file; write out to a new file,
4882 and, if that succeeds, rename the new file on top of the
4883 old file. That makes this a "safe save", so that we don't
4884 lose the old file if we have a problem writing out the new
4885 file. (If the existing file is the current capture file,
4886 we *HAVE* to do that, otherwise we're overwriting the file
4887 from which we're reading the packets that we're writing!) */
4888 fname_new = g_strdup_printf("%s~", fname);
4889 pdh = wtap_dump_open_ng(fname_new, save_format, encap, cf->snap,
4890 compressed, shb_hdr, idb_inf, &err);
4892 pdh = wtap_dump_open_ng(fname, save_format, encap, cf->snap,
4893 compressed, shb_hdr, idb_inf, &err);
4899 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
4903 /* Add address resolution */
4904 wtap_dump_set_addrinfo_list(pdh, get_addrinfo_list());
4906 /* Iterate through the list of packets, processing the packets we were
4909 XXX - we've already called "packet_range_process_init(range)", but
4910 "process_specified_packets()" will do it again. Fortunately,
4911 that's harmless in this case, as we haven't done anything to
4912 "range" since we initialized it. */
4913 callback_args.pdh = pdh;
4914 callback_args.fname = fname;
4915 callback_args.file_type = save_format;
4916 switch (process_specified_packets(cf, range, "Writing", "specified packets",
4917 TRUE, save_packet, &callback_args)) {
4920 /* Completed successfully. */
4924 /* The user decided to abort the saving.
4925 If we're writing to a temporary file, remove it.
4926 XXX - should we do so even if we're not writing to a
4928 wtap_dump_close(pdh, &err);
4929 if (fname_new != NULL)
4930 ws_unlink(fname_new);
4931 cf_callback_invoke(cf_cb_file_export_specified_packets_stopped, NULL);
4932 return CF_WRITE_ABORTED;
4936 /* Error while saving.
4937 If we're writing to a temporary file, remove it. */
4938 if (fname_new != NULL)
4939 ws_unlink(fname_new);
4940 wtap_dump_close(pdh, &err);
4944 if (!wtap_dump_close(pdh, &err)) {
4945 cf_close_failure_alert_box(fname, err);
4949 if (fname_new != NULL) {
4950 /* We wrote out to fname_new, and should rename it on top of
4951 fname; fname is now closed, so that should be possible even
4952 on Windows. Do the rename. */
4953 if (ws_rename(fname_new, fname) == -1) {
4954 /* Well, the rename failed. */
4955 cf_rename_failure_alert_box(fname, errno);
4960 cf_callback_invoke(cf_cb_file_export_specified_packets_finished, NULL);
4964 if (fname_new != NULL) {
4965 /* We were trying to write to a temporary file; get rid of it if it
4966 exists. (We don't care whether this fails, as, if it fails,
4967 there's not much we can do about it. I guess if it failed for
4968 a reason other than "it doesn't exist", we could report an
4969 error, so the user knows there's a junk file that they might
4970 want to clean up.) */
4971 ws_unlink(fname_new);
4974 cf_callback_invoke(cf_cb_file_export_specified_packets_failed, NULL);
4975 return CF_WRITE_ERROR;
4979 cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
4980 gboolean for_writing, int file_type)
4982 gchar *display_basename;
4985 /* Wiretap error. */
4986 display_basename = g_filename_display_basename(filename);
4989 case WTAP_ERR_NOT_REGULAR_FILE:
4990 simple_error_message_box(
4991 "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
4995 case WTAP_ERR_RANDOM_OPEN_PIPE:
4996 /* Seen only when opening a capture file for reading. */
4997 simple_error_message_box(
4998 "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.\n"
4999 "To capture from a pipe or FIFO use wireshark -i -",
5003 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
5004 /* Seen only when opening a capture file for reading. */
5005 simple_error_message_box(
5006 "The file \"%s\" isn't a capture file in a format Wireshark understands.",
5010 case WTAP_ERR_UNSUPPORTED:
5011 /* Seen only when opening a capture file for reading. */
5012 simple_error_message_box(
5013 "The file \"%s\" isn't a capture file in a format Wireshark understands.\n"
5015 display_basename, err_info);
5019 case WTAP_ERR_CANT_WRITE_TO_PIPE:
5020 /* Seen only when opening a capture file for writing. */
5021 simple_error_message_box(
5022 "The file \"%s\" is a pipe, and %s capture files can't be "
5023 "written to a pipe.",
5024 display_basename, wtap_file_type_subtype_string(file_type));
5027 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
5028 /* Seen only when opening a capture file for writing. */
5029 simple_error_message_box(
5030 "Wireshark doesn't support writing capture files in that format.");
5033 case WTAP_ERR_UNSUPPORTED_ENCAP:
5035 simple_error_message_box("Wireshark can't save this capture in that format.");
5037 simple_error_message_box(
5038 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.\n"
5040 display_basename, err_info);
5045 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
5047 simple_error_message_box(
5048 "Wireshark can't save this capture in that format.");
5050 simple_error_message_box(
5051 "The file \"%s\" is a capture for a network type that Wireshark doesn't support.",
5056 case WTAP_ERR_BAD_FILE:
5057 /* Seen only when opening a capture file for reading. */
5058 simple_error_message_box(
5059 "The file \"%s\" appears to be damaged or corrupt.\n"
5061 display_basename, err_info);
5065 case WTAP_ERR_CANT_OPEN:
5067 simple_error_message_box(
5068 "The file \"%s\" could not be created for some unknown reason.",
5071 simple_error_message_box(
5072 "The file \"%s\" could not be opened for some unknown reason.",
5077 case WTAP_ERR_SHORT_READ:
5078 simple_error_message_box(
5079 "The file \"%s\" appears to have been cut short"
5080 " in the middle of a packet or other data.",
5084 case WTAP_ERR_SHORT_WRITE:
5085 simple_error_message_box(
5086 "A full header couldn't be written to the file \"%s\".",
5090 case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
5091 simple_error_message_box(
5092 "This file type cannot be written as a compressed file.");
5095 case WTAP_ERR_DECOMPRESS:
5096 simple_error_message_box(
5097 "The compressed file \"%s\" appears to be damaged or corrupt.\n"
5098 "(%s)", display_basename, err_info);
5103 simple_error_message_box(
5104 "The file \"%s\" could not be %s: %s.",
5106 for_writing ? "created" : "opened",
5107 wtap_strerror(err));
5110 g_free(display_basename);
5113 open_failure_alert_box(filename, err, for_writing);
5118 * XXX - whether we mention the source pathname, the target pathname,
5119 * or both depends on the error and on what we find if we look for
5120 * one or both of them.
5123 cf_rename_failure_alert_box(const char *filename, int err)
5125 gchar *display_basename;
5127 display_basename = g_filename_display_basename(filename);
5131 /* XXX - should check whether the source exists and, if not,
5132 report it as the problem and, if so, report the destination
5134 simple_error_message_box("The path to the file \"%s\" doesn't exist.",
5139 /* XXX - if we're doing a rename after a safe save, we should
5140 probably say something else. */
5141 simple_error_message_box("You don't have permission to move the capture file to \"%s\".",
5146 /* XXX - this should probably mention both the source and destination
5148 simple_error_message_box("The file \"%s\" could not be moved: %s.",
5149 display_basename, wtap_strerror(err));
5152 g_free(display_basename);
5155 /* Check for write errors - if the file is being written to an NFS server,
5156 a write error may not show up until the file is closed, as NFS clients
5157 might not send writes to the server until the "write()" call finishes,
5158 so that the write may fail on the server but the "write()" may succeed. */
5160 cf_close_failure_alert_box(const char *filename, int err)
5162 gchar *display_basename;
5165 /* Wiretap error. */
5166 display_basename = g_filename_display_basename(filename);
5169 case WTAP_ERR_CANT_CLOSE:
5170 simple_error_message_box(
5171 "The file \"%s\" couldn't be closed for some unknown reason.",
5175 case WTAP_ERR_SHORT_WRITE:
5176 simple_error_message_box(
5177 "Not all the packets could be written to the file \"%s\".",
5182 simple_error_message_box(
5183 "An error occurred while closing the file \"%s\": %s.",
5184 display_basename, wtap_strerror(err));
5187 g_free(display_basename);
5190 We assume that a close error from the OS is really a write error. */
5191 write_failure_alert_box(filename, err);
5195 /* Reload the current capture file. */
5197 cf_reload(capture_file *cf) {
5199 gboolean is_tempfile;
5202 /* If the file could be opened, "cf_open()" calls "cf_close()"
5203 to get rid of state for the old capture file before filling in state
5204 for the new capture file. "cf_close()" will remove the file if
5205 it's a temporary file; we don't want that to happen (for one thing,
5206 it'd prevent subsequent reopens from working). Remember whether it's
5207 a temporary file, mark it as not being a temporary file, and then
5208 reopen it as the type of file it was.
5210 Also, "cf_close()" will free "cf->filename", so we must make
5211 a copy of it first. */
5212 filename = g_strdup(cf->filename);
5213 is_tempfile = cf->is_tempfile;
5214 cf->is_tempfile = FALSE;
5215 if (cf_open(cf, filename, WTAP_TYPE_AUTO, is_tempfile, &err) == CF_OK) {
5216 switch (cf_read(cf, TRUE)) {
5220 /* Just because we got an error, that doesn't mean we were unable
5221 to read any of the file; we handle what we could get from the
5225 case CF_READ_ABORTED:
5226 /* The user bailed out of re-reading the capture file; the
5227 capture file has been closed - just free the capture file name
5228 string and return (without changing the last containing
5234 /* The open failed, so "cf->is_tempfile" wasn't set to "is_tempfile".
5235 Instead, the file was left open, so we should restore "cf->is_tempfile"
5238 XXX - change the menu? Presumably "cf_open()" will do that;
5239 make sure it does! */
5240 cf->is_tempfile = is_tempfile;
5242 /* "cf_open()" made a copy of the file name we handed it, so
5243 we should free up our copy. */
5253 * indent-tabs-mode: nil
5256 * ex: set shiftwidth=2 tabstop=8 expandtab:
5257 * :indentSize=2:tabSize=8:noTabs=true: