4 * $Id: file.c,v 1.371 2004/04/13 18:01:39 tuexen Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
46 #ifdef HAVE_SYS_STAT_H
54 #ifdef NEED_SNPRINTF_H
55 # include "snprintf.h"
58 #ifdef NEED_STRERROR_H
62 #include <epan/epan.h>
63 #include <epan/filesystem.h>
67 #include <epan/packet.h>
73 #include "alert_box.h"
74 #include "simple_dialog.h"
75 #include "progress_dlg.h"
77 #include "statusbar.h"
79 #include <epan/dfilter/dfilter.h>
80 #include <epan/conversation.h>
82 #include <epan/epan_dissect.h>
84 #include "tap_dfilter_dlg.h"
85 #include "packet-data.h"
88 gboolean auto_scroll_live;
91 static guint32 firstsec, firstusec;
92 static guint32 prevsec, prevusec;
93 static guint32 cum_bytes = 0;
95 static void read_packet(capture_file *cf, long offset);
97 static void rescan_packets(capture_file *cf, const char *action, const char *action_item,
98 gboolean refilter, gboolean redissect);
100 static gboolean match_protocol_tree(capture_file *cf, frame_data *fdata,
102 static void match_subtree_text(proto_node *node, gpointer data);
103 static gboolean match_summary_line(capture_file *cf, frame_data *fdata,
105 static gboolean match_ascii_and_unicode(capture_file *cf, frame_data *fdata,
107 static gboolean match_ascii(capture_file *cf, frame_data *fdata,
109 static gboolean match_unicode(capture_file *cf, frame_data *fdata,
111 static gboolean match_binary(capture_file *cf, frame_data *fdata,
113 static gboolean match_dfilter(capture_file *cf, frame_data *fdata,
115 static gboolean find_packet(capture_file *cf,
116 gboolean (*match_function)(capture_file *, frame_data *, void *),
119 static void cf_open_failure_alert_box(const char *filename, int err,
120 gchar *err_info, gboolean for_writing,
122 static char *file_rename_error_message(int err);
123 static void cf_write_failure_alert_box(const char *filename, int err);
124 static void cf_close_failure_alert_box(const char *filename, int err);
125 static gboolean copy_binary_file(char *from_filename, char *to_filename);
127 /* Update the progress bar this many times when reading a file. */
128 #define N_PROGBAR_UPDATES 100
130 /* Number of "frame_data" structures per memory chunk.
131 XXX - is this the right number? */
132 #define FRAME_DATA_CHUNK_SIZE 1024
138 gboolean print_all_levels;
139 gboolean print_hex_for_data;
141 gint format; /* text or PostScript */
145 cf_open(char *fname, gboolean is_tempfile, capture_file *cf)
153 wth = wtap_open_offline(fname, &err, &err_info, TRUE);
157 /* Find the size of the file. */
159 if (fstat(fd, &cf_stat) < 0) {
165 /* The open succeeded. Close whatever capture file we had open,
166 and fill in the information for this file. */
169 /* Initialize all data structures used for dissection. */
172 /* We're about to start reading the file. */
173 cf->state = FILE_READ_IN_PROGRESS;
177 cf->f_len = cf_stat.st_size;
179 /* Set the file name because we need it to set the follow stream filter.
180 XXX - is that still true? We need it for other reasons, though,
182 cf->filename = g_strdup(fname);
184 /* Indicate whether it's a permanent or temporary file. */
185 cf->is_tempfile = is_tempfile;
187 /* If it's a temporary capture buffer file, mark it as not saved. */
188 cf->user_saved = !is_tempfile;
190 cf->cd_t = wtap_file_type(cf->wth);
192 cf->displayed_count = 0;
193 cf->marked_count = 0;
194 cf->drops_known = FALSE;
198 cf->snap = wtap_snapshot_length(cf->wth);
200 /* Snapshot length not known. */
201 cf->has_snap = FALSE;
202 cf->snap = WTAP_MAX_PACKET_SIZE;
205 firstsec = 0, firstusec = 0;
206 prevsec = 0, prevusec = 0;
208 cf->plist_chunk = g_mem_chunk_new("frame_data_chunk",
210 FRAME_DATA_CHUNK_SIZE * sizeof(frame_data),
212 g_assert(cf->plist_chunk);
217 cf_open_failure_alert_box(fname, err, err_info, FALSE, 0);
221 /* Reset everything to a pristine state */
223 cf_close(capture_file *cf)
225 /* Die if we're in the middle of reading a file. */
226 g_assert(cf->state != FILE_READ_IN_PROGRESS);
228 /* Destroy all windows, which refer to the
229 capture file we're closing. */
230 destroy_cfile_wins();
236 /* We have no file open... */
237 if (cf->filename != NULL) {
238 /* If it's a temporary file, remove it. */
240 unlink(cf->filename);
241 g_free(cf->filename);
244 /* ...which means we have nothing to save. */
245 cf->user_saved = FALSE;
247 if (cf->plist_chunk != NULL) {
248 g_mem_chunk_destroy(cf->plist_chunk);
249 cf->plist_chunk = NULL;
251 if (cf->rfcode != NULL) {
252 dfilter_free(cf->rfcode);
256 cf->plist_end = NULL;
257 unselect_packet(cf); /* nothing to select */
258 cf->first_displayed = NULL;
259 cf->last_displayed = NULL;
261 /* No frame selected, no field in that frame selected. */
262 cf->current_frame = NULL;
263 cf->finfo_selected = NULL;
265 /* Clear the packet list. */
266 packet_list_freeze();
275 /* Clear any file-related status bar messages.
276 XXX - should be "clear *ALL* file-related status bar messages;
277 will there ever be more than one on the stack? */
278 statusbar_pop_file_msg();
280 /* Restore the standard title bar message. */
281 set_main_window_name("The Ethereal Network Analyzer");
283 /* Disable all menu items that make sense only if you have a capture. */
284 set_menus_for_capture_file(FALSE);
285 set_menus_for_unsaved_capture_file(FALSE);
286 set_menus_for_captured_packets(FALSE);
287 set_menus_for_selected_packet(cf);
288 set_menus_for_capture_in_progress(FALSE);
289 set_menus_for_selected_tree_row(cf);
291 /* We have no file open. */
292 cf->state = FILE_CLOSED;
295 /* Set the file name in the status line, in the name for the main window,
296 and in the name for the main window's icon. */
298 set_display_filename(capture_file *cf)
302 static const gchar done_fmt_nodrops[] = " File: %s %s %02u:%02u:%02u";
303 static const gchar done_fmt_drops[] = " File: %s %s %02u:%02u:%02u Drops: %u";
305 gchar *win_name_fmt = "%s - Ethereal";
309 name_ptr = cf_get_display_name(cf);
311 if (!cf->is_tempfile) {
312 /* Add this filename to the list of recent files in the "Recent Files" submenu */
313 add_menu_recent_capture_file(cf->filename);
316 if (cf->f_len/1024/1024 > 10) {
317 size_str = g_strdup_printf("%ld MB", cf->f_len/1024/1024);
318 } else if (cf->f_len/1024 > 10) {
319 size_str = g_strdup_printf("%ld KB", cf->f_len/1024);
321 size_str = g_strdup_printf("%ld bytes", cf->f_len);
324 if (cf->drops_known) {
325 done_msg = g_strdup_printf(done_fmt_drops, name_ptr, size_str,
326 cf->esec/3600, cf->esec%3600/60, cf->esec%60, cf->drops);
328 done_msg = g_strdup_printf(done_fmt_nodrops, name_ptr, size_str,
329 cf->esec/3600, cf->esec%3600/60, cf->esec%60);
331 statusbar_push_file_msg(done_msg);
334 msg_len = strlen(name_ptr) + strlen(win_name_fmt) + 1;
335 win_name = g_malloc(msg_len);
336 snprintf(win_name, msg_len, win_name_fmt, name_ptr);
337 set_main_window_name(win_name);
342 cf_read(capture_file *cf)
346 gchar *name_ptr, *load_msg, *load_fmt = "%s";
348 char errmsg_errno[1024+1];
349 gchar err_str[2048+1];
351 progdlg_t *progbar = NULL;
354 * XXX - should be "off_t", but Wiretap would need more work to handle
355 * the full size of "off_t" on platforms where it's more than a "long"
363 gchar status_str[100];
364 int progbar_nextstep;
368 reset_tap_listeners();
369 tap_dfilter_dlg_update();
370 name_ptr = get_basename(cf->filename);
372 load_msg = g_strdup_printf(" Loading: %s", name_ptr);
373 statusbar_push_file_msg(load_msg);
376 load_msg = g_strdup_printf(load_fmt, name_ptr);
378 /* Update the progress bar when it gets to this value. */
379 progbar_nextstep = 0;
380 /* When we reach the value that triggers a progress bar update,
381 bump that value by this amount. */
382 progbar_quantum = cf->f_len/N_PROGBAR_UPDATES;
388 packet_list_freeze();
391 g_get_current_time(&start_time);
393 while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
394 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
395 when we update it, we have to run the GTK+ main loop to get it
396 to repaint what's pending, and doing so may involve an "ioctl()"
397 to see if there's any pending input from an X server, and doing
398 that for every packet can be costly, especially on a big file. */
399 if (data_offset >= progbar_nextstep) {
400 file_pos = lseek(cf->filed, 0, SEEK_CUR);
401 prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
402 if (prog_val > 1.0) {
403 /* The file probably grew while we were reading it.
404 Update "cf->f_len", and try again. */
405 fd = wtap_fd(cf->wth);
406 if (fstat(fd, &cf_stat) >= 0) {
407 cf->f_len = cf_stat.st_size;
408 prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
410 /* If it's still > 1, either the "fstat()" failed (in which
411 case there's not much we can do about it), or the file
412 *shrank* (in which case there's not much we can do about
413 it); just clip the progress value at 1.0. */
417 if (progbar == NULL) {
418 /* Create the progress bar if necessary */
419 progbar = delayed_create_progress_dlg("Loading", load_msg,
420 &stop_flag, &start_time, prog_val);
424 if (progbar != NULL) {
425 g_snprintf(status_str, sizeof(status_str),
426 "%luKB of %luKB", file_pos / 1024, cf->f_len / 1024);
427 update_progress_dlg(progbar, prog_val, status_str);
429 progbar_nextstep += progbar_quantum;
433 /* Well, the user decided to abort the read. Destroy the progress
434 bar, close the capture file, and return READ_ABORTED so our caller
435 can do whatever is appropriate when that happens. */
436 destroy_progress_dlg(progbar);
437 cf->state = FILE_READ_ABORTED; /* so that we're allowed to close it */
438 packet_list_thaw(); /* undo our freeze */
440 return (READ_ABORTED);
442 read_packet(cf, data_offset);
445 /* We're done reading the file; destroy the progress bar if it was created. */
449 destroy_progress_dlg(progbar);
451 /* We're done reading sequentially through the file. */
452 cf->state = FILE_READ_DONE;
454 /* Close the sequential I/O side, to free up memory it requires. */
455 wtap_sequential_close(cf->wth);
457 /* Allow the protocol dissectors to free up memory that they
458 * don't need after the sequential run-through of the packets. */
459 postseq_cleanup_all_protocols();
461 /* Set the file encapsulation type now; we don't know what it is until
462 we've looked at all the packets, as we don't know until then whether
463 there's more than one type (and thus whether it's
464 WTAP_ENCAP_PER_PACKET). */
465 cf->lnk_t = wtap_file_encap(cf->wth);
467 cf->current_frame = cf->first_displayed;
470 statusbar_pop_file_msg();
471 set_display_filename(cf);
473 /* Enable menu items that make sense if you have a capture file you've
475 set_menus_for_capture_file(TRUE);
476 set_menus_for_unsaved_capture_file(!cf->user_saved);
478 /* Enable menu items that make sense if you have some captured packets. */
479 set_menus_for_captured_packets(TRUE);
481 /* If we have any displayed packets to select, select the first of those
482 packets by making the first row the selected row. */
483 if (cf->first_displayed != NULL)
484 packet_list_select_row(0);
487 /* Put up a message box noting that the read failed somewhere along
488 the line. Don't throw out the stuff we managed to read, though,
492 case WTAP_ERR_UNSUPPORTED_ENCAP:
493 errmsg = "The capture file is for a network type that Ethereal doesn't support.";
496 case WTAP_ERR_CANT_READ:
497 errmsg = "An attempt to read from the file failed for"
498 " some unknown reason.";
501 case WTAP_ERR_SHORT_READ:
502 errmsg = "The capture file appears to have been cut short"
503 " in the middle of a packet.";
506 case WTAP_ERR_BAD_RECORD:
507 snprintf(errmsg_errno, sizeof(errmsg_errno),
508 "The capture file appears to be damaged or corrupt.\n(%s)",
510 errmsg = errmsg_errno;
514 snprintf(errmsg_errno, sizeof(errmsg_errno),
515 "An error occurred while reading the"
516 " capture file: %s.", wtap_strerror(err));
517 errmsg = errmsg_errno;
520 snprintf(err_str, sizeof err_str, errmsg);
521 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_str);
524 return (READ_SUCCESS);
529 cf_start_tail(char *fname, gboolean is_tempfile, capture_file *cf)
534 err = cf_open(fname, is_tempfile, cf);
536 /* Disable menu items that make no sense if you're currently running
538 set_menus_for_capture_in_progress(TRUE);
540 /* Enable menu items that make sense if you have some captured
541 packets (yes, I know, we don't have any *yet*). */
542 set_menus_for_captured_packets(TRUE);
544 capture_msg = g_strdup_printf(" %s: <live capture in progress>", cf->iface);
546 statusbar_push_file_msg(capture_msg);
552 cf_continue_tail(capture_file *cf, int to_read, int *err)
554 long data_offset = 0;
559 packet_list_freeze();
561 while (to_read != 0 && (wtap_read(cf->wth, err, &err_info, &data_offset))) {
562 if (cf->state == FILE_READ_ABORTED) {
563 /* Well, the user decided to exit Ethereal. Break out of the
564 loop, and let the code below (which is called even if there
565 aren't any packets left to read) exit. */
568 read_packet(cf, data_offset);
574 /* XXX - this cheats and looks inside the packet list to find the final
576 if (auto_scroll_live && cf->plist_end != NULL)
577 packet_list_moveto_end();
579 if (cf->state == FILE_READ_ABORTED) {
580 /* Well, the user decided to exit Ethereal. Return READ_ABORTED
581 so that our caller can kill off the capture child process;
582 this will cause an EOF on the pipe from the child, so
583 "cf_finish_tail()" will be called, and it will clean up
586 } else if (*err != 0) {
587 /* We got an error reading the capture file.
588 XXX - pop up a dialog box? */
591 return (READ_SUCCESS);
595 cf_finish_tail(capture_file *cf, int *err)
602 packet_list_freeze();
604 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
605 if (cf->state == FILE_READ_ABORTED) {
606 /* Well, the user decided to abort the read. Break out of the
607 loop, and let the code below (which is called even if there
608 aren't any packets left to read) exit. */
611 read_packet(cf, data_offset);
614 if (cf->state == FILE_READ_ABORTED) {
615 /* Well, the user decided to abort the read. We're only called
616 when the child capture process closes the pipe to us (meaning
617 it's probably exited), so we can just close the capture
618 file; we return READ_ABORTED so our caller can do whatever
619 is appropriate when that happens. */
625 if (auto_scroll_live && cf->plist_end != NULL)
626 /* XXX - this cheats and looks inside the packet list to find the final
628 packet_list_moveto_end();
630 /* We're done reading sequentially through the file. */
631 cf->state = FILE_READ_DONE;
633 /* we have to update the f_len field */
634 /* Find the size of the file. */
635 fd = wtap_fd(cf->wth);
636 if (fstat(fd, &cf_stat) >= 0) {
637 cf->f_len = cf_stat.st_size;
640 /* We're done reading sequentially through the file; close the
641 sequential I/O side, to free up memory it requires. */
642 wtap_sequential_close(cf->wth);
644 /* Allow the protocol dissectors to free up memory that they
645 * don't need after the sequential run-through of the packets. */
646 postseq_cleanup_all_protocols();
648 /* Set the file encapsulation type now; we don't know what it is until
649 we've looked at all the packets, as we don't know until then whether
650 there's more than one type (and thus whether it's
651 WTAP_ENCAP_PER_PACKET). */
652 cf->lnk_t = wtap_file_encap(cf->wth);
654 /* Pop the "<live capture in progress>" message off the status bar. */
655 statusbar_pop_file_msg();
657 set_display_filename(cf);
659 /* Enable menu items that make sense if you're not currently running
661 set_menus_for_capture_in_progress(FALSE);
663 /* Enable menu items that make sense if you have a capture file
664 you've finished reading. */
665 set_menus_for_capture_file(TRUE);
666 set_menus_for_unsaved_capture_file(!cf->user_saved);
669 /* We got an error reading the capture file.
670 XXX - pop up a dialog box? */
673 return (READ_SUCCESS);
676 #endif /* HAVE_LIBPCAP */
679 cf_get_display_name(capture_file *cf)
683 /* Return a name to use in displays */
684 if (!cf->is_tempfile) {
685 /* Get the last component of the file name, and use that. */
687 displayname = get_basename(cf->filename);
689 /* Add this filename to the list of recent files in the "Recent Files" submenu */
690 add_menu_recent_capture_file(cf->filename);
692 displayname="(No file)";
695 /* The file we read is a temporary file from a live capture;
696 we don't mention its name. */
697 displayname = "(Untitled)";
703 color_filter_t *colorf;
705 } apply_color_filter_args;
708 * If no color filter has been applied, apply this one.
709 * (The "if no color filter has been applied" is to handle the case where
710 * more than one color filter matches the packet.)
713 apply_color_filter(gpointer filter_arg, gpointer argp)
715 color_filter_t *colorf = filter_arg;
716 apply_color_filter_args *args = argp;
718 if (colorf->c_colorfilter != NULL && args->colorf == NULL) {
719 if (dfilter_apply_edt(colorf->c_colorfilter, args->edt))
720 args->colorf = colorf;
725 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
726 union wtap_pseudo_header *pseudo_header, const guchar *buf,
729 apply_color_filter_args args;
731 gboolean create_proto_tree = FALSE;
734 /* just add some value here until we know if it is being displayed or not */
735 fdata->cum_bytes = cum_bytes + fdata->pkt_len;
737 /* We don't yet have a color filter to apply. */
740 /* If we don't have the time stamp of the first packet in the
741 capture, it's because this is the first packet. Save the time
742 stamp of this packet as the time stamp of the first packet. */
743 if (!firstsec && !firstusec) {
744 firstsec = fdata->abs_secs;
745 firstusec = fdata->abs_usecs;
747 /* if this frames is marked as a reference time frame, reset
748 firstsec and firstusec to this frame */
749 if(fdata->flags.ref_time){
750 firstsec = fdata->abs_secs;
751 firstusec = fdata->abs_usecs;
754 /* If we don't have the time stamp of the previous displayed packet,
755 it's because this is the first displayed packet. Save the time
756 stamp of this packet as the time stamp of the previous displayed
758 if (!prevsec && !prevusec) {
759 prevsec = fdata->abs_secs;
760 prevusec = fdata->abs_usecs;
763 /* Get the time elapsed between the first packet and this packet. */
764 compute_timestamp_diff(&fdata->rel_secs, &fdata->rel_usecs,
765 fdata->abs_secs, fdata->abs_usecs, firstsec, firstusec);
767 /* If it's greater than the current elapsed time, set the elapsed time
768 to it (we check for "greater than" so as not to be confused by
769 time moving backwards). */
770 if ((gint32)cf->esec < fdata->rel_secs
771 || ((gint32)cf->esec == fdata->rel_secs && (gint32)cf->eusec < fdata->rel_usecs)) {
772 cf->esec = fdata->rel_secs;
773 cf->eusec = fdata->rel_usecs;
776 /* Get the time elapsed between the previous displayed packet and
778 compute_timestamp_diff(&fdata->del_secs, &fdata->del_usecs,
779 fdata->abs_secs, fdata->abs_usecs, prevsec, prevusec);
783 we have a display filter and are re-applying it;
785 we have a list of color filters;
787 we have tap listeners;
789 allocate a protocol tree root node, so that we'll construct
790 a protocol tree against which a filter expression can be
792 if ((cf->dfcode != NULL && refilter) || filter_list != NULL
793 || num_tap_filters != 0)
794 create_proto_tree = TRUE;
796 /* Dissect the frame. */
797 edt = epan_dissect_new(create_proto_tree, FALSE);
799 if (cf->dfcode != NULL && refilter) {
800 epan_dissect_prime_dfilter(edt, cf->dfcode);
803 filter_list_prime_edt(edt);
806 epan_dissect_run(edt, pseudo_header, buf, fdata, &cf->cinfo);
807 tap_push_tapped_queue(edt);
809 /* If we have a display filter, apply it if we're refiltering, otherwise
810 leave the "passed_dfilter" flag alone.
812 If we don't have a display filter, set "passed_dfilter" to 1. */
813 if (cf->dfcode != NULL) {
815 if (cf->dfcode != NULL)
816 fdata->flags.passed_dfilter = dfilter_apply_edt(cf->dfcode, edt) ? 1 : 0;
818 fdata->flags.passed_dfilter = 1;
821 fdata->flags.passed_dfilter = 1;
823 /* If we have color filters, and the frame is to be displayed, apply
824 the color filters. */
825 if (fdata->flags.passed_dfilter) {
826 if (filter_list != NULL) {
828 g_slist_foreach(filter_list, apply_color_filter, &args);
833 if( (fdata->flags.passed_dfilter)
834 || (edt->pi.fd->flags.ref_time) ){
835 /* This frame either passed the display filter list or is marked as
836 a time reference frame. All time reference frames are displayed
837 even if they dont pass the display filter */
838 /* if this was a TIME REF frame we should reset the cul bytes field */
839 if(edt->pi.fd->flags.ref_time){
840 cum_bytes = fdata->pkt_len;
841 fdata->cum_bytes = cum_bytes;
844 /* increase cum_bytes with this packets length */
845 cum_bytes += fdata->pkt_len;
847 epan_dissect_fill_in_columns(edt);
849 /* If we haven't yet seen the first frame, this is it.
851 XXX - we must do this before we add the row to the display,
852 as, if the display's GtkCList's selection mode is
853 GTK_SELECTION_BROWSE, when the first entry is added to it,
854 "select_packet()" will be called, and it will fetch the row
855 data for the 0th row, and will get a null pointer rather than
856 "fdata", as "gtk_clist_append()" won't yet have returned and
857 thus "gtk_clist_set_row_data()" won't yet have been called.
859 We thus need to leave behind bread crumbs so that
860 "select_packet()" can find this frame. See the comment
861 in "select_packet()". */
862 if (cf->first_displayed == NULL)
863 cf->first_displayed = fdata;
865 /* This is the last frame we've seen so far. */
866 cf->last_displayed = fdata;
868 row = packet_list_append(cf->cinfo.col_data, fdata);
870 /* If the packet matches a color filter,
871 * store matching color_filter_t object in frame data. */
872 if (filter_list != NULL && (args.colorf != NULL)) {
873 /* add the matching colorfilter to the frame data */
874 fdata->color_filter = args.colorf;
875 /* If packet is marked, use colors from preferences */
876 if (fdata->flags.marked) {
877 packet_list_set_colors(row, &prefs.gui_marked_fg, &prefs.gui_marked_bg);
878 } else /* if (filter_list != NULL && (args.colorf != NULL)) */ {
879 packet_list_set_colors(row, &(args.colorf->fg_color),
880 &(args.colorf->bg_color));
883 /* No color filter match */
884 fdata->color_filter = NULL;
885 if (fdata->flags.marked) {
886 packet_list_set_colors(row, &prefs.gui_marked_fg, &prefs.gui_marked_bg);
890 /* Set the time of the previous displayed frame to the time of this
892 prevsec = fdata->abs_secs;
893 prevusec = fdata->abs_usecs;
895 cf->displayed_count++;
897 /* This frame didn't pass the display filter, so it's not being added
898 to the clist, and thus has no row. */
901 epan_dissect_free(edt);
906 read_packet(capture_file *cf, long offset)
908 const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
909 union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
910 const guchar *buf = wtap_buf_ptr(cf->wth);
913 frame_data *plist_end;
916 /* Allocate the next list entry, and add it to the list. */
917 fdata = g_mem_chunk_alloc(cf->plist_chunk);
922 fdata->pkt_len = phdr->len;
923 fdata->cap_len = phdr->caplen;
924 fdata->file_off = offset;
925 fdata->lnk_t = phdr->pkt_encap;
926 fdata->abs_secs = phdr->ts.tv_sec;
927 fdata->abs_usecs = phdr->ts.tv_usec;
928 fdata->flags.encoding = CHAR_ASCII;
929 fdata->flags.visited = 0;
930 fdata->flags.marked = 0;
931 fdata->flags.ref_time = 0;
935 edt = epan_dissect_new(TRUE, FALSE);
936 epan_dissect_prime_dfilter(edt, cf->rfcode);
937 epan_dissect_run(edt, pseudo_header, buf, fdata, NULL);
938 passed = dfilter_apply_edt(cf->rfcode, edt);
939 epan_dissect_free(edt);
942 plist_end = cf->plist_end;
943 fdata->prev = plist_end;
944 if (plist_end != NULL)
945 plist_end->next = fdata;
948 cf->plist_end = fdata;
951 fdata->num = cf->count;
952 add_packet_to_packet_list(fdata, cf, pseudo_header, buf, TRUE);
954 /* XXX - if we didn't have read filters, or if we could avoid
955 allocating the "frame_data" structure until we knew whether
956 the frame passed the read filter, we could use a G_ALLOC_ONLY
959 ...but, at least in one test I did, where I just made the chunk
960 a G_ALLOC_ONLY chunk and read in a huge capture file, it didn't
961 seem to save a noticeable amount of time or space. */
962 g_mem_chunk_free(cf->plist_chunk, fdata);
967 filter_packets(capture_file *cf, gchar *dftext, gboolean force)
970 char *filter_new = dftext ? dftext : "";
971 char *filter_old = cf->dfilter ? cf->dfilter : "";
973 /* if new filter equals old one, do nothing unless told to do so */
974 if (!force && strcmp(filter_new, filter_old) == 0) {
978 if (dftext == NULL) {
979 /* The new filter is an empty filter (i.e., display all packets). */
983 * We have a filter; make a copy of it (as we'll be saving it),
984 * and try to compile it.
986 dftext = g_strdup(dftext);
987 if (!dfilter_compile(dftext, &dfcode)) {
988 /* The attempt failed; report an error. */
989 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
992 "The display filter \"%s\" is not a valid display filter.\n"
993 "See the help for a description of the display filter syntax.",
994 simple_dialog_primary_start(), dfilter_error_msg,
995 simple_dialog_primary_end(), dftext);
1001 if (dfcode == NULL) {
1002 /* Yes - free the filter text, and set it to null. */
1008 /* We have a valid filter. Replace the current filter. */
1009 if (cf->dfilter != NULL)
1010 g_free(cf->dfilter);
1011 cf->dfilter = dftext;
1012 if (cf->dfcode != NULL)
1013 dfilter_free(cf->dfcode);
1014 cf->dfcode = dfcode;
1016 /* Now rescan the packet list, applying the new filter, but not
1017 throwing away information constructed on a previous pass. */
1018 if (dftext == NULL) {
1019 rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
1021 rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
1027 colorize_packets(capture_file *cf)
1029 rescan_packets(cf, "Colorizing", "all packets", FALSE, FALSE);
1033 reftime_packets(capture_file *cf)
1035 rescan_packets(cf, "Updating Reftime", "all packets", FALSE, FALSE);
1039 redissect_packets(capture_file *cf)
1041 rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
1044 /* Rescan the list of packets, reconstructing the CList.
1046 "action" describes why we're doing this; it's used in the progress
1049 "action_item" describes what we're doing; it's used in the progress
1052 "refilter" is TRUE if we need to re-evaluate the filter expression.
1054 "redissect" is TRUE if we need to make the dissectors reconstruct
1055 any state information they have (because a preference that affects
1056 some dissector has changed, meaning some dissector might construct
1057 its state differently from the way it was constructed the last time). */
1059 rescan_packets(capture_file *cf, const char *action, const char *action_item,
1060 gboolean refilter, gboolean redissect)
1063 progdlg_t *progbar = NULL;
1068 frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
1069 int selected_row, prev_row, preceding_row, following_row;
1070 gboolean selected_frame_seen;
1073 GTimeVal start_time;
1074 gchar status_str[100];
1075 int progbar_nextstep;
1076 int progbar_quantum;
1079 reset_tap_listeners();
1080 /* Which frame, if any, is the currently selected frame?
1081 XXX - should the selected frame or the focus frame be the "current"
1082 frame, that frame being the one from which "Find Frame" searches
1084 selected_frame = cf->current_frame;
1086 /* We don't yet know what row that frame will be on, if any, after we
1087 rebuild the clist, however. */
1091 /* We need to re-initialize all the state information that protocols
1092 keep, because some preference that controls a dissector has changed,
1093 which might cause the state information to be constructed differently
1094 by that dissector. */
1096 /* Initialize all data structures used for dissection. */
1100 /* Freeze the packet list while we redo it, so we don't get any
1101 screen updates while it happens. */
1102 packet_list_freeze();
1105 packet_list_clear();
1107 /* We don't yet know which will be the first and last frames displayed. */
1108 cf->first_displayed = NULL;
1109 cf->last_displayed = NULL;
1111 /* We currently don't display any packets */
1112 cf->displayed_count = 0;
1114 /* Iterate through the list of frames. Call a routine for each frame
1115 to check whether it should be displayed and, if so, add it to
1116 the display list. */
1122 /* Update the progress bar when it gets to this value. */
1123 progbar_nextstep = 0;
1124 /* When we reach the value that triggers a progress bar update,
1125 bump that value by this amount. */
1126 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1127 /* Count of packets at which we've looked. */
1131 g_get_current_time(&start_time);
1133 row = -1; /* no previous row yet */
1138 preceding_frame = NULL;
1140 following_frame = NULL;
1142 selected_frame_seen = FALSE;
1144 for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
1145 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1146 when we update it, we have to run the GTK+ main loop to get it
1147 to repaint what's pending, and doing so may involve an "ioctl()"
1148 to see if there's any pending input from an X server, and doing
1149 that for every packet can be costly, especially on a big file. */
1150 if (count >= progbar_nextstep) {
1151 /* let's not divide by zero. I should never be started
1152 * with count == 0, so let's assert that
1154 g_assert(cf->count > 0);
1155 prog_val = (gfloat) count / cf->count;
1157 if (progbar == NULL)
1158 /* Create the progress bar if necessary */
1159 progbar = delayed_create_progress_dlg(action, action_item, &stop_flag,
1160 &start_time, prog_val);
1162 if (progbar != NULL) {
1163 g_snprintf(status_str, sizeof(status_str),
1164 "%4u of %u frames", count, cf->count);
1165 update_progress_dlg(progbar, prog_val, status_str);
1168 progbar_nextstep += progbar_quantum;
1172 /* Well, the user decided to abort the filtering. Just stop.
1174 XXX - go back to the previous filter? Users probably just
1175 want not to wait for a filtering operation to finish;
1176 unless we cancel by having no filter, reverting to the
1177 previous filter will probably be even more expensive than
1178 continuing the filtering, as it involves going back to the
1179 beginning and filtering, and even with no filter we currently
1180 have to re-generate the entire clist, which is also expensive.
1182 I'm not sure what Network Monitor does, but it doesn't appear
1183 to give you an unfiltered display if you cancel. */
1190 /* Since all state for the frame was destroyed, mark the frame
1191 * as not visited, free the GSList referring to the state
1192 * data (the per-frame data itself was freed by
1193 * "init_dissection()"), and null out the GSList pointer. */
1194 fdata->flags.visited = 0;
1196 g_slist_free(fdata->pfd);
1201 if (!wtap_seek_read (cf->wth, fdata->file_off, &cf->pseudo_header,
1202 cf->pd, fdata->cap_len, &err, &err_info)) {
1203 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1204 cf_read_error_message(err, err_info), cf->filename);
1208 /* If the previous frame is displayed, and we haven't yet seen the
1209 selected frame, remember that frame - it's the closest one we've
1210 yet seen before the selected frame. */
1211 if (prev_row != -1 && !selected_frame_seen) {
1212 preceding_row = prev_row;
1213 preceding_frame = prev_frame;
1215 row = add_packet_to_packet_list(fdata, cf, &cf->pseudo_header, cf->pd,
1218 /* If this frame is displayed, and this is the first frame we've
1219 seen displayed after the selected frame, remember this frame -
1220 it's the closest one we've yet seen at or after the selected
1222 if (row != -1 && selected_frame_seen && following_row == -1) {
1223 following_row = row;
1224 following_frame = fdata;
1226 if (fdata == selected_frame) {
1228 selected_frame_seen = TRUE;
1231 /* Remember this row/frame - it'll be the previous row/frame
1232 on the next pass through the loop. */
1238 /* Clear out what remains of the visited flags and per-frame data
1241 XXX - that may cause various forms of bogosity when dissecting
1242 these frames, as they won't have been seen by this sequential
1243 pass, but the only alternative I see is to keep scanning them
1244 even though the user requested that the scan stop, and that
1245 would leave the user stuck with an Ethereal grinding on
1246 until it finishes. Should we just stick them with that? */
1247 for (; fdata != NULL; fdata = fdata->next) {
1248 fdata->flags.visited = 0;
1250 g_slist_free(fdata->pfd);
1256 /* We're done filtering the packets; destroy the progress bar if it
1258 if (progbar != NULL)
1259 destroy_progress_dlg(progbar);
1261 /* Unfreeze the packet list. */
1264 if (selected_row == -1) {
1265 /* The selected frame didn't pass the filter. */
1266 if (selected_frame == NULL) {
1267 /* That's because there *was* no selected frame. Make the first
1268 displayed frame the current frame. */
1271 /* Find the nearest displayed frame to the selected frame (whether
1272 it's before or after that frame) and make that the current frame.
1273 If the next and previous displayed frames are equidistant from the
1274 selected frame, choose the next one. */
1275 g_assert(following_frame == NULL ||
1276 following_frame->num >= selected_frame->num);
1277 g_assert(preceding_frame == NULL ||
1278 preceding_frame->num <= selected_frame->num);
1279 if (following_frame == NULL) {
1280 /* No frame after the selected frame passed the filter, so we
1281 have to select the last displayed frame before the selected
1283 selected_row = preceding_row;
1284 } else if (preceding_frame == NULL) {
1285 /* No frame before the selected frame passed the filter, so we
1286 have to select the first displayed frame after the selected
1288 selected_row = following_row;
1290 /* Choose the closer of the last displayed frame before the
1291 selected frame and the first displayed frame after the
1292 selected frame; in case of a tie, choose the first displayed
1293 frame after the selected frame. */
1294 if (following_frame->num - selected_frame->num <=
1295 selected_frame->num - preceding_frame->num) {
1296 selected_row = following_row;
1298 /* The previous frame is closer to the selected frame than the
1300 selected_row = preceding_row;
1306 if (selected_row == -1) {
1307 /* There are no frames displayed at all. */
1308 unselect_packet(cf);
1310 /* Either the frame that was selected passed the filter, or we've
1311 found the nearest displayed frame to that frame. Select it, make
1312 it the focus row, and make it visible. */
1313 packet_list_set_selected_row(selected_row);
1324 process_specified_packets(capture_file *cf, packet_range_t *range,
1325 const char *string1, const char *string2,
1326 gboolean (*callback)(capture_file *, frame_data *,
1327 union wtap_pseudo_header *, const guint8 *, void *),
1328 void *callback_args)
1333 union wtap_pseudo_header pseudo_header;
1334 guint8 pd[WTAP_MAX_PACKET_SIZE+1];
1335 psp_return_t ret = PSP_FINISHED;
1337 progdlg_t *progbar = NULL;
1340 gboolean progbar_stop_flag;
1341 GTimeVal progbar_start_time;
1342 gchar progbar_status_str[100];
1343 int progbar_nextstep;
1344 int progbar_quantum;
1345 range_process_e process_this;
1347 /* Update the progress bar when it gets to this value. */
1348 progbar_nextstep = 0;
1349 /* When we reach the value that triggers a progress bar update,
1350 bump that value by this amount. */
1351 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1352 /* Count of packets at which we've looked. */
1355 progbar_stop_flag = FALSE;
1356 g_get_current_time(&progbar_start_time);
1358 packet_range_process_init(range);
1360 /* Iterate through the list of packets, printing the packets that
1361 were selected by the current display filter. */
1362 for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
1363 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1364 when we update it, we have to run the GTK+ main loop to get it
1365 to repaint what's pending, and doing so may involve an "ioctl()"
1366 to see if there's any pending input from an X server, and doing
1367 that for every packet can be costly, especially on a big file. */
1368 if (progbar_count >= progbar_nextstep) {
1369 /* let's not divide by zero. I should never be started
1370 * with count == 0, so let's assert that
1372 g_assert(cf->count > 0);
1373 progbar_val = (gfloat) progbar_count / cf->count;
1375 if (progbar == NULL)
1376 /* Create the progress bar if necessary */
1377 progbar = delayed_create_progress_dlg(string1, string2,
1379 &progbar_start_time,
1382 if (progbar != NULL) {
1383 g_snprintf(progbar_status_str, sizeof(progbar_status_str),
1384 "%4u of %u packets", progbar_count, cf->count);
1385 update_progress_dlg(progbar, progbar_val, progbar_status_str);
1388 progbar_nextstep += progbar_quantum;
1391 if (progbar_stop_flag) {
1392 /* Well, the user decided to abort the operation. Just stop,
1393 and arrange to return TRUE to our caller, so they know it
1394 was stopped explicitly. */
1401 /* do we have to process this packet? */
1402 process_this = packet_range_process_packet(range, fdata);
1403 if (process_this == range_process_next) {
1404 /* this packet uninteresting, continue with next one */
1406 } else if (process_this == range_processing_finished) {
1407 /* all interesting packets processed, stop the loop */
1411 /* Get the packet */
1412 if (!wtap_seek_read(cf->wth, fdata->file_off, &pseudo_header,
1413 pd, fdata->cap_len, &err, &err_info)) {
1414 /* Attempt to get the packet failed. */
1415 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1416 cf_read_error_message(err, err_info), cf->filename);
1420 if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
1421 /* Callback failed. We assume it reported the error appropriately. */
1427 /* We're done printing the packets; destroy the progress bar if
1429 if (progbar != NULL)
1430 destroy_progress_dlg(progbar);
1436 retap_packet(capture_file *cf _U_, frame_data *fdata,
1437 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
1440 epan_dissect_t *edt;
1442 /* If we have tap listeners, allocate a protocol tree root node, so that
1443 we'll construct a protocol tree against which a filter expression can
1445 edt = epan_dissect_new(num_tap_filters != 0, FALSE);
1446 tap_queue_init(edt);
1447 epan_dissect_run(edt, pseudo_header, pd, fdata, NULL);
1448 tap_push_tapped_queue(edt);
1449 epan_dissect_free(edt);
1455 retap_packets(capture_file *cf)
1457 packet_range_t range;
1459 /* Reset the tap listeners. */
1460 reset_tap_listeners();
1462 /* Iterate through the list of packets, dissecting all packets and
1463 re-running the taps. */
1464 packet_range_init(&range);
1465 packet_range_process_init(&range);
1466 switch (process_specified_packets(cf, &range, "Refiltering statistics on",
1467 "all packets", retap_packet,
1470 /* Completed successfully. */
1474 /* Well, the user decided to abort the refiltering.
1475 Return FALSE so our caller knows they did that. */
1479 /* Error while retapping. */
1487 print_args_t *print_args;
1489 gboolean print_separator;
1493 } print_callback_args_t;
1496 print_packet(capture_file *cf, frame_data *fdata,
1497 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
1500 print_callback_args_t *args = argsp;
1501 epan_dissect_t *edt;
1508 if (args->print_args->print_summary) {
1509 /* Fill in the column information, but don't bother creating
1510 the logical protocol tree. */
1511 edt = epan_dissect_new(FALSE, FALSE);
1512 epan_dissect_run(edt, pseudo_header, pd, fdata, &cf->cinfo);
1513 epan_dissect_fill_in_columns(edt);
1514 cp = &args->line_buf[0];
1516 for (i = 0; i < cf->cinfo.num_cols; i++) {
1517 /* Find the length of the string for this column. */
1518 column_len = strlen(cf->cinfo.col_data[i]);
1519 if (args->col_widths[i] > column_len)
1520 column_len = args->col_widths[i];
1522 /* Make sure there's room in the line buffer for the column; if not,
1523 double its length. */
1524 line_len += column_len + 1; /* "+1" for space */
1525 if (line_len > args->line_buf_len) {
1526 cp_off = cp - args->line_buf;
1527 args->line_buf_len = 2 * line_len;
1528 args->line_buf = g_realloc(args->line_buf, args->line_buf_len + 1);
1529 cp = args->line_buf + cp_off;
1532 /* Right-justify the packet number column. */
1533 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
1534 sprintf(cp, "%*s", args->col_widths[i], cf->cinfo.col_data[i]);
1536 sprintf(cp, "%-*s", args->col_widths[i], cf->cinfo.col_data[i]);
1538 if (i != cf->cinfo.num_cols - 1)
1542 print_line(args->print_fh, 0, args->print_args->format, args->line_buf);
1544 if (args->print_separator)
1545 print_line(args->print_fh, 0, args->print_args->format, "");
1547 /* Create the logical protocol tree, complete with the display
1548 representation of the items; we don't need the columns here,
1550 edt = epan_dissect_new(TRUE, TRUE);
1551 epan_dissect_run(edt, pseudo_header, pd, fdata, NULL);
1553 /* Print the information in that tree. */
1554 proto_tree_print(args->print_args, edt, args->print_fh);
1556 if (args->print_args->print_hex) {
1557 /* Print the full packet data as hex. */
1558 print_hex_data(args->print_fh, args->print_args->format, edt);
1561 /* Print a blank line if we print anything after this. */
1562 args->print_separator = TRUE;
1563 } /* if (print_summary) */
1564 epan_dissect_free(edt);
1566 return !ferror(args->print_fh);
1570 print_packets(capture_file *cf, print_args_t *print_args)
1573 print_callback_args_t callback_args;
1581 callback_args.print_fh = open_print_dest(print_args->to_file,
1583 if (callback_args.print_fh == NULL)
1584 return PP_OPEN_ERROR; /* attempt to open destination failed */
1586 print_preamble(callback_args.print_fh, print_args->format);
1587 if (ferror(callback_args.print_fh)) {
1588 close_print_dest(print_args->to_file, callback_args.print_fh);
1589 return PP_WRITE_ERROR;
1592 callback_args.print_args = print_args;
1593 callback_args.print_separator = FALSE;
1594 callback_args.line_buf = NULL;
1595 callback_args.line_buf_len = 256;
1596 callback_args.col_widths = NULL;
1597 if (print_args->print_summary) {
1598 /* We're printing packet summaries. Allocate the line buffer at
1599 its initial length. */
1600 callback_args.line_buf = g_malloc(callback_args.line_buf_len + 1);
1602 /* Find the widths for each of the columns - maximum of the
1603 width of the title and the width of the data - and print
1604 the column titles. */
1605 callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * cf->cinfo.num_cols);
1606 cp = &callback_args.line_buf[0];
1608 for (i = 0; i < cf->cinfo.num_cols; i++) {
1609 /* Don't pad the last column. */
1610 if (i == cf->cinfo.num_cols - 1)
1611 callback_args.col_widths[i] = 0;
1613 callback_args.col_widths[i] = strlen(cf->cinfo.col_title[i]);
1614 data_width = get_column_char_width(get_column_format(i));
1615 if (data_width > callback_args.col_widths[i])
1616 callback_args.col_widths[i] = data_width;
1619 /* Find the length of the string for this column. */
1620 column_len = strlen(cf->cinfo.col_title[i]);
1621 if (callback_args.col_widths[i] > column_len)
1622 column_len = callback_args.col_widths[i];
1624 /* Make sure there's room in the line buffer for the column; if not,
1625 double its length. */
1626 line_len += column_len + 1; /* "+1" for space */
1627 if (line_len > callback_args.line_buf_len) {
1628 cp_off = cp - callback_args.line_buf;
1629 callback_args.line_buf_len = 2 * line_len;
1630 callback_args.line_buf = g_realloc(callback_args.line_buf,
1631 callback_args.line_buf_len + 1);
1632 cp = callback_args.line_buf + cp_off;
1635 /* Right-justify the packet number column. */
1636 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
1637 sprintf(cp, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
1639 sprintf(cp, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
1641 if (i != cf->cinfo.num_cols - 1)
1645 print_line(callback_args.print_fh, 0, print_args->format,
1646 callback_args.line_buf);
1647 } /* if (print_summary) */
1649 /* Iterate through the list of packets, printing the packets we were
1651 ret = process_specified_packets(cf, &print_args->range, "Printing",
1652 "selected packets", print_packet,
1655 if (callback_args.col_widths != NULL)
1656 g_free(callback_args.col_widths);
1657 if (callback_args.line_buf != NULL)
1658 g_free(callback_args.line_buf);
1663 /* Completed successfully. */
1667 /* Well, the user decided to abort the printing.
1669 XXX - note that what got generated before they did that
1670 will get printed if we're piping to a print program; we'd
1671 have to write to a file and then hand that to the print
1672 program to make it actually not print anything. */
1676 /* Error while printing.
1678 XXX - note that what got generated before they did that
1679 will get printed if we're piping to a print program; we'd
1680 have to write to a file and then hand that to the print
1681 program to make it actually not print anything. */
1682 close_print_dest(print_args->to_file, callback_args.print_fh);
1683 return PP_WRITE_ERROR;
1686 print_finale(callback_args.print_fh, print_args->format);
1687 if (ferror(callback_args.print_fh)) {
1688 close_print_dest(print_args->to_file, callback_args.print_fh);
1689 return PP_WRITE_ERROR;
1692 close_print_dest(print_args->to_file, callback_args.print_fh);
1697 /* Scan through the packet list and change all columns that use the
1698 "command-line-specified" time stamp format to use the current
1699 value of that format. */
1701 change_time_formats(capture_file *cf)
1704 progdlg_t *progbar = NULL;
1710 GTimeVal start_time;
1711 gchar status_str[100];
1712 int progbar_nextstep;
1713 int progbar_quantum;
1715 gboolean sorted_by_frame_column;
1717 /* Are there any columns with time stamps in the "command-line-specified"
1720 XXX - we have to force the "column is writable" flag on, as it
1721 might be off from the last frame that was dissected. */
1722 col_set_writable(&cf->cinfo, TRUE);
1723 if (!check_col(&cf->cinfo, COL_CLS_TIME)) {
1724 /* No, there aren't any columns in that format, so we have no work
1728 first = cf->cinfo.col_first[COL_CLS_TIME];
1729 g_assert(first >= 0);
1730 last = cf->cinfo.col_last[COL_CLS_TIME];
1732 /* Freeze the packet list while we redo it, so we don't get any
1733 screen updates while it happens. */
1734 packet_list_freeze();
1736 /* Update the progress bar when it gets to this value. */
1737 progbar_nextstep = 0;
1738 /* When we reach the value that triggers a progress bar update,
1739 bump that value by this amount. */
1740 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1741 /* Count of packets at which we've looked. */
1744 /* If the rows are currently sorted by the frame column then we know
1745 * the row number of each packet: it's the row number of the previously
1746 * displayed packet + 1.
1748 * Otherwise, if the display is sorted by a different column then we have
1749 * to use the O(N) packet_list_find_row_from_data() (thus making the job
1750 * of changing the time display format O(N**2)).
1752 * (XXX - In fact it's still O(N**2) because gtk_clist_set_text() takes
1753 * the row number and walks that many elements down the clist to find
1754 * the appropriate element.)
1756 sorted_by_frame_column = FALSE;
1757 for (i = 0; i < cf->cinfo.num_cols; i++) {
1758 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
1760 sorted_by_frame_column = (i == packet_list_get_sort_column());
1766 g_get_current_time(&start_time);
1768 /* Iterate through the list of packets, checking whether the packet
1769 is in a row of the summary list and, if so, whether there are
1770 any columns that show the time in the "command-line-specified"
1771 format and, if so, update that row. */
1772 for (fdata = cf->plist, row = -1; fdata != NULL; fdata = fdata->next) {
1773 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1774 when we update it, we have to run the GTK+ main loop to get it
1775 to repaint what's pending, and doing so may involve an "ioctl()"
1776 to see if there's any pending input from an X server, and doing
1777 that for every packet can be costly, especially on a big file. */
1778 if (count >= progbar_nextstep) {
1779 /* let's not divide by zero. I should never be started
1780 * with count == 0, so let's assert that
1782 g_assert(cf->count > 0);
1784 prog_val = (gfloat) count / cf->count;
1786 if (progbar == NULL)
1787 /* Create the progress bar if necessary */
1788 progbar = delayed_create_progress_dlg("Changing", "time display",
1789 &stop_flag, &start_time, prog_val);
1791 if (progbar != NULL) {
1792 g_snprintf(status_str, sizeof(status_str),
1793 "%4u of %u packets", count, cf->count);
1794 update_progress_dlg(progbar, prog_val, status_str);
1797 progbar_nextstep += progbar_quantum;
1801 /* Well, the user decided to abort the redisplay. Just stop.
1803 XXX - this leaves the time field in the old format in
1804 frames we haven't yet processed. So it goes; should we
1805 simply not offer them the option of stopping? */
1811 /* Find what row this packet is in. */
1812 if (!sorted_by_frame_column) {
1813 /* This function is O(N), so we try to avoid using it... */
1814 row = packet_list_find_row_from_data(fdata);
1816 /* ...which we do by maintaining a count of packets that are
1817 being displayed (i.e., that have passed the display filter),
1818 and using the current value of that count as the row number
1819 (which is why we can only do it when the display is sorted
1820 by the frame number). */
1821 if (fdata->flags.passed_dfilter)
1828 /* This packet is in the summary list, on row "row". */
1830 for (i = first; i <= last; i++) {
1831 if (cf->cinfo.fmt_matx[i][COL_CLS_TIME]) {
1832 /* This is one of the columns that shows the time in
1833 "command-line-specified" format; update it. */
1834 cf->cinfo.col_buf[i][0] = '\0';
1835 col_set_cls_time(fdata, &cf->cinfo, i);
1836 packet_list_set_text(row, i, cf->cinfo.col_data[i]);
1842 /* We're done redisplaying the packets; destroy the progress bar if it
1844 if (progbar != NULL)
1845 destroy_progress_dlg(progbar);
1847 /* Set the column widths of those columns that show the time in
1848 "command-line-specified" format. */
1849 for (i = first; i <= last; i++) {
1850 if (cf->cinfo.fmt_matx[i][COL_CLS_TIME]) {
1851 packet_list_set_cls_time_width(i);
1855 /* Unfreeze the packet list. */
1863 gboolean frame_matched;
1867 find_packet_protocol_tree(capture_file *cf, const char *string)
1871 mdata.string = string;
1872 mdata.string_len = strlen(string);
1873 return find_packet(cf, match_protocol_tree, &mdata);
1877 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
1879 match_data *mdata = criterion;
1880 epan_dissect_t *edt;
1882 /* Construct the protocol tree, including the displayed text */
1883 edt = epan_dissect_new(TRUE, TRUE);
1884 /* We don't need the column information */
1885 epan_dissect_run(edt, &cf->pseudo_header, cf->pd, fdata, NULL);
1887 /* Iterate through all the nodes, seeing if they have text that matches. */
1889 mdata->frame_matched = FALSE;
1890 proto_tree_children_foreach(edt->tree, match_subtree_text, mdata);
1891 epan_dissect_free(edt);
1892 return mdata->frame_matched;
1896 match_subtree_text(proto_node *node, gpointer data)
1898 match_data *mdata = (match_data*) data;
1899 const gchar *string = mdata->string;
1900 size_t string_len = mdata->string_len;
1901 capture_file *cf = mdata->cf;
1902 field_info *fi = PITEM_FINFO(node);
1903 gchar label_str[ITEM_LABEL_LENGTH];
1910 if (mdata->frame_matched) {
1911 /* We already had a match; don't bother doing any more work. */
1915 /* Don't match invisible entries. */
1919 /* was a free format label produced? */
1921 label_ptr = fi->rep->representation;
1923 /* no, make a generic label */
1924 label_ptr = label_str;
1925 proto_item_fill_label(fi, label_str);
1928 /* Does that label match? */
1929 label_len = strlen(label_ptr);
1930 for (i = 0; i < label_len; i++) {
1931 c_char = label_ptr[i];
1933 c_char = toupper(c_char);
1934 if (c_char == string[c_match]) {
1936 if (c_match == string_len) {
1937 /* No need to look further; we have a match */
1938 mdata->frame_matched = TRUE;
1945 /* Recurse into the subtree, if it exists */
1946 if (node->first_child != NULL)
1947 proto_tree_children_foreach(node, match_subtree_text, mdata);
1951 find_packet_summary_line(capture_file *cf, const char *string)
1955 mdata.string = string;
1956 mdata.string_len = strlen(string);
1957 return find_packet(cf, match_summary_line, &mdata);
1961 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
1963 match_data *mdata = criterion;
1964 const gchar *string = mdata->string;
1965 size_t string_len = mdata->string_len;
1966 epan_dissect_t *edt;
1967 const char *info_column;
1968 size_t info_column_len;
1969 gboolean frame_matched = FALSE;
1975 /* Don't bother constructing the protocol tree */
1976 edt = epan_dissect_new(FALSE, FALSE);
1977 /* Get the column information */
1978 epan_dissect_run(edt, &cf->pseudo_header, cf->pd, fdata, &cf->cinfo);
1980 /* Find the Info column */
1981 for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
1982 if (cf->cinfo.fmt_matx[colx][COL_INFO]) {
1983 /* Found it. See if we match. */
1984 info_column = edt->pi.cinfo->col_data[colx];
1985 info_column_len = strlen(info_column);
1986 for (i = 0; i < info_column_len; i++) {
1987 c_char = info_column[i];
1989 c_char = toupper(c_char);
1990 if (c_char == string[c_match]) {
1992 if (c_match == string_len) {
1993 frame_matched = TRUE;
2002 epan_dissect_free(edt);
2003 return frame_matched;
2009 } cbs_t; /* "Counted byte string" */
2012 find_packet_data(capture_file *cf, const guint8 *string, size_t string_size)
2017 info.data_len = string_size;
2019 /* String or hex search? */
2021 /* String search - what type of string? */
2022 switch (cf->scs_type) {
2024 case SCS_ASCII_AND_UNICODE:
2025 return find_packet(cf, match_ascii_and_unicode, &info);
2028 return find_packet(cf, match_ascii, &info);
2031 return find_packet(cf, match_unicode, &info);
2034 g_assert_not_reached();
2038 return find_packet(cf, match_binary, &info);
2042 match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
2044 cbs_t *info = criterion;
2045 const char *ascii_text = info->data;
2046 size_t textlen = info->data_len;
2047 gboolean frame_matched;
2053 frame_matched = FALSE;
2054 buf_len = fdata->pkt_len;
2055 for (i = 0; i < buf_len; i++) {
2058 c_char = toupper(c_char);
2060 if (c_char == ascii_text[c_match]) {
2062 if (c_match == textlen) {
2063 frame_matched = TRUE;
2070 return frame_matched;
2074 match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
2076 cbs_t *info = criterion;
2077 const char *ascii_text = info->data;
2078 size_t textlen = info->data_len;
2079 gboolean frame_matched;
2085 frame_matched = FALSE;
2086 buf_len = fdata->pkt_len;
2087 for (i = 0; i < buf_len; i++) {
2090 c_char = toupper(c_char);
2091 if (c_char == ascii_text[c_match]) {
2093 if (c_match == textlen) {
2094 frame_matched = TRUE;
2100 return frame_matched;
2104 match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
2106 cbs_t *info = criterion;
2107 const char *ascii_text = info->data;
2108 size_t textlen = info->data_len;
2109 gboolean frame_matched;
2115 frame_matched = FALSE;
2116 buf_len = fdata->pkt_len;
2117 for (i = 0; i < buf_len; i++) {
2120 c_char = toupper(c_char);
2121 if (c_char == ascii_text[c_match]) {
2124 if (c_match == textlen) {
2125 frame_matched = TRUE;
2131 return frame_matched;
2135 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
2137 cbs_t *info = criterion;
2138 const guint8 *binary_data = info->data;
2139 size_t datalen = info->data_len;
2140 gboolean frame_matched;
2145 frame_matched = FALSE;
2146 buf_len = fdata->pkt_len;
2147 for (i = 0; i < buf_len; i++) {
2148 if (cf->pd[i] == binary_data[c_match]) {
2150 if (c_match == datalen) {
2151 frame_matched = TRUE;
2157 return frame_matched;
2161 find_packet_dfilter(capture_file *cf, dfilter_t *sfcode)
2163 return find_packet(cf, match_dfilter, sfcode);
2167 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
2169 dfilter_t *sfcode = criterion;
2170 epan_dissect_t *edt;
2171 gboolean frame_matched;
2173 edt = epan_dissect_new(TRUE, FALSE);
2174 epan_dissect_prime_dfilter(edt, sfcode);
2175 epan_dissect_run(edt, &cf->pseudo_header, cf->pd, fdata, NULL);
2176 frame_matched = dfilter_apply_edt(sfcode, edt);
2177 epan_dissect_free(edt);
2178 return frame_matched;
2182 find_packet(capture_file *cf,
2183 gboolean (*match_function)(capture_file *, frame_data *, void *),
2186 frame_data *start_fd;
2188 frame_data *new_fd = NULL;
2189 progdlg_t *progbar = NULL;
2196 GTimeVal start_time;
2197 gchar status_str[100];
2198 int progbar_nextstep;
2199 int progbar_quantum;
2201 start_fd = cf->current_frame;
2202 if (start_fd != NULL) {
2203 /* Iterate through the list of packets, starting at the packet we've
2204 picked, calling a routine to run the filter on the packet, see if
2205 it matches, and stop if so. */
2209 progbar_nextstep = 0;
2210 /* When we reach the value that triggers a progress bar update,
2211 bump that value by this amount. */
2212 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2215 g_get_current_time(&start_time);
2219 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2220 when we update it, we have to run the GTK+ main loop to get it
2221 to repaint what's pending, and doing so may involve an "ioctl()"
2222 to see if there's any pending input from an X server, and doing
2223 that for every packet can be costly, especially on a big file. */
2224 if (count >= progbar_nextstep) {
2225 /* let's not divide by zero. I should never be started
2226 * with count == 0, so let's assert that
2228 g_assert(cf->count > 0);
2230 prog_val = (gfloat) count / cf->count;
2232 /* Create the progress bar if necessary */
2233 if (progbar == NULL)
2234 progbar = delayed_create_progress_dlg("Searching", cf->sfilter,
2235 &stop_flag, &start_time, prog_val);
2237 if (progbar != NULL) {
2238 g_snprintf(status_str, sizeof(status_str),
2239 "%4u of %u packets", count, cf->count);
2240 update_progress_dlg(progbar, prog_val, status_str);
2243 progbar_nextstep += progbar_quantum;
2247 /* Well, the user decided to abort the search. Go back to the
2248 frame where we started. */
2253 /* Go past the current frame. */
2254 if (cf->sbackward) {
2255 /* Go on to the previous frame. */
2256 fdata = fdata->prev;
2257 if (fdata == NULL) {
2259 * XXX - other apps have a bit more of a detailed message
2260 * for this, and instead of offering "OK" and "Cancel",
2261 * they offer things such as "Continue" and "Cancel";
2262 * we need an API for popping up alert boxes with
2263 * {Verb} and "Cancel".
2265 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
2266 "%sBeginning of capture exceeded!%s\n\n"
2267 "Search is continued from the end of the capture.",
2268 simple_dialog_primary_start(), simple_dialog_primary_end());
2269 fdata = cf->plist_end; /* wrap around */
2272 /* Go on to the next frame. */
2273 fdata = fdata->next;
2274 if (fdata == NULL) {
2275 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
2276 "%sEnd of capture exceeded!%s\n\n"
2277 "Search is continued from the start of the capture.",
2278 simple_dialog_primary_start(), simple_dialog_primary_end());
2279 fdata = cf->plist; /* wrap around */
2285 /* Is this packet in the display? */
2286 if (fdata->flags.passed_dfilter) {
2287 /* Yes. Load its data. */
2288 if (!wtap_seek_read(cf->wth, fdata->file_off, &cf->pseudo_header,
2289 cf->pd, fdata->cap_len, &err, &err_info)) {
2290 /* Read error. Report the error, and go back to the frame
2291 where we started. */
2292 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2293 cf_read_error_message(err, err_info), cf->filename);
2298 /* Does it match the search criterion? */
2299 if ((*match_function)(cf, fdata, criterion)) {
2301 break; /* found it! */
2305 if (fdata == start_fd) {
2306 /* We're back to the frame we were on originally, and that frame
2307 doesn't match the search filter. The search failed. */
2312 /* We're done scanning the packets; destroy the progress bar if it
2314 if (progbar != NULL)
2315 destroy_progress_dlg(progbar);
2318 if (new_fd != NULL) {
2319 /* We found a frame. Find what row it's in. */
2320 row = packet_list_find_row_from_data(new_fd);
2321 g_assert(row != -1);
2323 /* Select that row, make it the focus row, and make it visible. */
2324 packet_list_set_selected_row(row);
2325 return TRUE; /* success */
2327 return FALSE; /* failure */
2331 goto_frame(capture_file *cf, guint fnumber)
2336 for (fdata = cf->plist; fdata != NULL && fdata->num < fnumber; fdata = fdata->next)
2339 if (fdata == NULL) {
2340 /* we didn't find a packet with that packet number */
2341 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2342 "There is no packet with that packet number.");
2343 return FALSE; /* we failed to go to that packet */
2345 if (!fdata->flags.passed_dfilter) {
2346 /* that packet currently isn't displayed */
2347 /* XXX - add it to the set of displayed packets? */
2348 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2349 "That packet is not currently being displayed.");
2350 return FALSE; /* we failed to go to that packet */
2353 /* We found that packet, and it's currently being displayed.
2354 Find what row it's in. */
2355 row = packet_list_find_row_from_data(fdata);
2356 g_assert(row != -1);
2358 /* Select that row, make it the focus row, and make it visible. */
2359 packet_list_set_selected_row(row);
2360 return TRUE; /* we got to that packet */
2364 goto_top_frame(capture_file *cf)
2368 frame_data *lowest_fdata = NULL;
2370 for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
2371 if (fdata->flags.passed_dfilter) {
2372 lowest_fdata = fdata;
2377 if (lowest_fdata == NULL) {
2381 /* We found that packet, and it's currently being displayed.
2382 Find what row it's in. */
2383 row = packet_list_find_row_from_data(lowest_fdata);
2384 g_assert(row != -1);
2386 /* Select that row, make it the focus row, and make it visible. */
2387 packet_list_set_selected_row(row);
2388 return TRUE; /* we got to that packet */
2392 goto_bottom_frame(capture_file *cf)
2396 frame_data *highest_fdata = NULL;
2398 for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
2399 if (fdata->flags.passed_dfilter) {
2400 highest_fdata = fdata;
2404 if (highest_fdata == NULL) {
2408 /* We found that packet, and it's currently being displayed.
2409 Find what row it's in. */
2410 row = packet_list_find_row_from_data(highest_fdata);
2411 g_assert(row != -1);
2413 /* Select that row, make it the focus row, and make it visible. */
2414 packet_list_set_selected_row(row);
2415 return TRUE; /* we got to that packet */
2418 /* Select the packet on a given row. */
2420 select_packet(capture_file *cf, int row)
2426 /* Get the frame data struct pointer for this frame */
2427 fdata = (frame_data *)packet_list_get_row_data(row);
2429 if (fdata == NULL) {
2430 /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
2431 the first entry is added to it by "real_insert_row()", that row
2432 is selected (see "real_insert_row()", in "gtk/gtkclist.c", in both
2433 our version and the vanilla GTK+ version).
2435 This means that a "select-row" signal is emitted; this causes
2436 "packet_list_select_cb()" to be called, which causes "select_packet()"
2439 "select_packet()" fetches, above, the data associated with the
2440 row that was selected; however, as "gtk_clist_append()", which
2441 called "real_insert_row()", hasn't yet returned, we haven't yet
2442 associated any data with that row, so we get back a null pointer.
2444 We can't assume that there's only one frame in the frame list,
2445 either, as we may be filtering the display.
2447 We therefore assume that, if "row" is 0, i.e. the first row
2448 is being selected, and "cf->first_displayed" equals
2449 "cf->last_displayed", i.e. there's only one frame being
2450 displayed, that frame is the frame we want.
2452 This means we have to set "cf->first_displayed" and
2453 "cf->last_displayed" before adding the row to the
2454 GtkCList; see the comment in "add_packet_to_packet_list()". */
2456 if (row == 0 && cf->first_displayed == cf->last_displayed)
2457 fdata = cf->first_displayed;
2460 /* Get the data in that frame. */
2461 if (!wtap_seek_read (cf->wth, fdata->file_off, &cf->pseudo_header,
2462 cf->pd, fdata->cap_len, &err, &err_info)) {
2463 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2464 cf_read_error_message(err, err_info), cf->filename);
2468 /* Record that this frame is the current frame. */
2469 cf->current_frame = fdata;
2471 /* Create the logical protocol tree. */
2472 if (cf->edt != NULL) {
2473 epan_dissect_free(cf->edt);
2476 /* We don't need the columns here. */
2477 cf->edt = epan_dissect_new(TRUE, TRUE);
2478 epan_dissect_run(cf->edt, &cf->pseudo_header, cf->pd, cf->current_frame,
2481 /* Display the GUI protocol tree and hex dump.
2482 XXX - why do we dump core if we call "proto_tree_draw()"
2483 before calling "add_byte_views()"? */
2484 add_main_byte_views(cf->edt);
2485 main_proto_tree_draw(cf->edt->tree);
2487 /* A packet is selected. */
2488 set_menus_for_selected_packet(cf);
2491 /* Unselect the selected packet, if any. */
2493 unselect_packet(capture_file *cf)
2495 /* Destroy the epan_dissect_t for the unselected packet. */
2496 if (cf->edt != NULL) {
2497 epan_dissect_free(cf->edt);
2501 /* Clear out the display of that packet. */
2502 clear_tree_and_hex_views();
2504 /* No packet is selected. */
2505 cf->current_frame = NULL;
2506 set_menus_for_selected_packet(cf);
2508 /* No protocol tree means no selected field. */
2512 /* Unset the selected protocol tree field, if any. */
2514 unselect_field(capture_file *cf)
2516 statusbar_pop_field_msg();
2517 cf->finfo_selected = NULL;
2518 set_menus_for_selected_tree_row(cf);
2522 * Mark a particular frame.
2525 mark_frame(capture_file *cf, frame_data *frame)
2527 frame->flags.marked = TRUE;
2532 * Unmark a particular frame.
2535 unmark_frame(capture_file *cf, frame_data *frame)
2537 frame->flags.marked = FALSE;
2544 } save_callback_args_t;
2547 * Save a capture to a file, in a particular format, saving either
2548 * all packets, all currently-displayed packets, or all marked packets.
2550 * Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
2551 * up a message box for the failure.
2554 save_packet(capture_file *cf _U_, frame_data *fdata,
2555 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2558 save_callback_args_t *args = argsp;
2559 struct wtap_pkthdr hdr;
2562 /* init the wtap header for saving */
2563 hdr.ts.tv_sec = fdata->abs_secs;
2564 hdr.ts.tv_usec = fdata->abs_usecs;
2565 hdr.caplen = fdata->cap_len;
2566 hdr.len = fdata->pkt_len;
2567 hdr.pkt_encap = fdata->lnk_t;
2569 /* and save the packet */
2570 if (!wtap_dump(args->pdh, &hdr, pseudo_header, pd, &err)) {
2571 cf_write_failure_alert_box(args->fname, err);
2578 cf_save(char *fname, capture_file *cf, packet_range_t *range, guint save_format)
2580 gchar *from_filename;
2581 gchar *name_ptr, *save_msg, *save_fmt = " Saving: %s...";
2586 struct stat infile, outfile;
2587 save_callback_args_t callback_args;
2589 name_ptr = get_basename(fname);
2590 msg_len = strlen(name_ptr) + strlen(save_fmt) + 2;
2591 save_msg = g_malloc(msg_len);
2592 snprintf(save_msg, msg_len, save_fmt, name_ptr);
2593 statusbar_push_file_msg(save_msg);
2597 * Check that the from file is not the same as to file
2598 * We do it here so we catch all cases ...
2599 * Unfortunately, the file requester gives us an absolute file
2600 * name and the read file name may be relative (if supplied on
2601 * the command line). From Joerg Mayer.
2603 * This is a bit tricky on win32. The st_ino field is documented as:
2604 * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
2605 * but it *is* set to zero if stat() returns without an error,
2606 * so this is working, but maybe not quite the way expected. ULFL
2608 infile.st_ino = 1; /* These prevent us from getting equality */
2609 outfile.st_ino = 2; /* If one or other of the files is not accessible */
2610 stat(cf->filename, &infile);
2611 stat(fname, &outfile);
2612 if (infile.st_ino == outfile.st_ino) {
2613 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2614 "%sCapture file: \"%s\" already exists!%s\n\n"
2615 "Please choose a different filename.",
2616 simple_dialog_primary_start(), fname, simple_dialog_primary_end());
2620 packet_range_process_init(range);
2623 * if (!save_filtered && !save_marked && !save_manual_range &&
2624 * !save_marked_range && !save_curr && save_format == cf->cd_t) {
2627 if (packet_range_process_all(range) && save_format == cf->cd_t) {
2628 /* We're not filtering packets, and we're saving it in the format
2629 it's already in, so we can just move or copy the raw data. */
2631 if (cf->is_tempfile) {
2632 /* The file being saved is a temporary file from a live
2633 capture, so it doesn't need to stay around under that name;
2634 first, try renaming the capture buffer file to the new name. */
2636 if (rename(cf->filename, fname) == 0) {
2637 /* That succeeded - there's no need to copy the source file. */
2638 from_filename = NULL;
2641 if (errno == EXDEV) {
2642 /* They're on different file systems, so we have to copy the
2645 from_filename = cf->filename;
2647 /* The rename failed, but not because they're on different
2648 file systems - put up an error message. (Or should we
2649 just punt and try to copy? The only reason why I'd
2650 expect the rename to fail and the copy to succeed would
2651 be if we didn't have permission to remove the file from
2652 the temporary directory, and that might be fixable - but
2653 is it worth requiring the user to go off and fix it?) */
2654 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2655 file_rename_error_message(errno), fname);
2661 from_filename = cf->filename;
2664 /* It's a permanent file, so we should copy it, and not remove the
2667 from_filename = cf->filename;
2671 /* Copy the file, if we haven't moved it. */
2672 if (!copy_binary_file(from_filename, fname))
2676 /* Either we're filtering packets, or we're saving in a different
2677 format; we can't do that by copying or moving the capture file,
2678 we have to do it by writing the packets out in Wiretap. */
2679 pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap, &err);
2681 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
2685 /* XXX - we let the user save a subset of the packets.
2687 If we do that, should we make that file the current file? If so,
2688 it means we can no longer get at the other packets. What does
2691 /* Iterate through the list of packets, processing the packets we were
2694 XXX - we've already called "packet_range_process_init(range)", but
2695 "process_specified_packets()" will do it again. Fortunately,
2696 that's harmless in this case, as we haven't done anything to
2697 "range" since we initialized it. */
2698 callback_args.pdh = pdh;
2699 callback_args.fname = fname;
2700 switch (process_specified_packets(cf, range, "Saving",
2701 "selected packets", save_packet,
2705 /* Completed successfully. */
2709 /* The user decided to abort the saving.
2710 XXX - remove the output file? */
2714 /* Error while saving. */
2715 wtap_dump_close(pdh, &err);
2719 if (!wtap_dump_close(pdh, &err)) {
2720 cf_close_failure_alert_box(fname, err);
2725 /* Pop the "Saving:" message off the status bar. */
2726 statusbar_pop_file_msg();
2728 if (packet_range_process_all(range)) {
2729 /* We saved the entire capture, not just some packets from it.
2730 Open and read the file we saved it to.
2732 XXX - this is somewhat of a waste; we already have the
2733 packets, all this gets us is updated file type information
2734 (which we could just stuff into "cf"), and having the new
2735 file be the one we have opened and from which we're reading
2736 the data, and it means we have to spend time opening and
2737 reading the file, which could be a significant amount of
2738 time if the file is large. */
2739 cf->user_saved = TRUE;
2741 if ((err = cf_open(fname, FALSE, cf)) == 0) {
2742 /* XXX - report errors if this fails?
2743 What should we return if it fails or is aborted? */
2744 switch (cf_read(cf)) {
2748 /* Just because we got an error, that doesn't mean we were unable
2749 to read any of the file; we handle what we could get from the
2754 /* The user bailed out of re-reading the capture file; the
2755 capture file has been closed - just return (without
2756 changing any menu settings; "cf_close()" set them
2757 correctly for the "no capture file open" state). */
2760 set_menus_for_unsaved_capture_file(FALSE);
2766 /* Pop the "Saving:" message off the status bar. */
2767 statusbar_pop_file_msg();
2772 cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
2773 gboolean for_writing, int file_type)
2776 /* Wiretap error. */
2779 case WTAP_ERR_NOT_REGULAR_FILE:
2780 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2781 "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
2785 case WTAP_ERR_RANDOM_OPEN_PIPE:
2786 /* Seen only when opening a capture file for reading. */
2787 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2788 "The file \"%s\" is a pipe or FIFO; Ethereal cannot read pipe or FIFO files.",
2792 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
2793 /* Seen only when opening a capture file for reading. */
2794 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2795 "The file \"%s\" is not a capture file in a format Ethereal understands.",
2799 case WTAP_ERR_UNSUPPORTED:
2800 /* Seen only when opening a capture file for reading. */
2801 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2802 "The file \"%s\" is not a capture file in a format Ethereal understands.\n"
2804 filename, err_info);
2808 case WTAP_ERR_CANT_WRITE_TO_PIPE:
2809 /* Seen only when opening a capture file for writing. */
2810 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2811 "The file \"%s\" is a pipe, and %s capture files cannot be "
2812 "written to a pipe.",
2813 filename, wtap_file_type_string(file_type));
2816 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
2817 /* Seen only when opening a capture file for writing. */
2818 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2819 "Ethereal does not support writing capture files in that format.");
2822 case WTAP_ERR_UNSUPPORTED_ENCAP:
2824 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2825 "Ethereal cannot save this capture in that format.");
2827 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2828 "The file \"%s\" is a capture for a network type that Ethereal doesn't support.\n"
2830 filename, err_info);
2835 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
2837 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2838 "Ethereal cannot save this capture in that format.");
2840 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2841 "The file \"%s\" is a capture for a network type that Ethereal doesn't support.",
2846 case WTAP_ERR_BAD_RECORD:
2847 /* Seen only when opening a capture file for reading. */
2848 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2849 "The file \"%s\" appears to be damaged or corrupt.\n"
2851 filename, err_info);
2855 case WTAP_ERR_CANT_OPEN:
2857 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2858 "The file \"%s\" could not be created for some unknown reason.",
2861 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2862 "The file \"%s\" could not be opened for some unknown reason.",
2867 case WTAP_ERR_SHORT_READ:
2868 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2869 "The file \"%s\" appears to have been cut short"
2870 " in the middle of a packet or other data.",
2874 case WTAP_ERR_SHORT_WRITE:
2875 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2876 "A full header couldn't be written to the file \"%s\".",
2881 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2882 "The file \"%s\" could not be %s: %s.",
2884 for_writing ? "created" : "opened",
2885 wtap_strerror(err));
2890 open_failure_alert_box(filename, err, for_writing);
2895 file_rename_error_message(int err)
2898 static char errmsg_errno[1024+1];
2903 errmsg = "The path to the file \"%s\" does not exist.";
2907 errmsg = "You do not have permission to move the capture file to \"%s\".";
2911 snprintf(errmsg_errno, sizeof(errmsg_errno),
2912 "The file \"%%s\" could not be moved: %s.",
2913 wtap_strerror(err));
2914 errmsg = errmsg_errno;
2921 cf_read_error_message(int err, gchar *err_info)
2923 static char errmsg_errno[1024+1];
2927 case WTAP_ERR_UNSUPPORTED:
2928 case WTAP_ERR_UNSUPPORTED_ENCAP:
2929 case WTAP_ERR_BAD_RECORD:
2930 snprintf(errmsg_errno, sizeof(errmsg_errno),
2931 "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
2932 wtap_strerror(err), err_info);
2936 snprintf(errmsg_errno, sizeof(errmsg_errno),
2937 "An error occurred while reading from the file \"%%s\": %s.",
2938 wtap_strerror(err));
2941 return errmsg_errno;
2945 cf_write_failure_alert_box(const char *filename, int err)
2948 /* Wiretap error. */
2949 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2950 "An error occurred while writing to the file \"%s\": %s.",
2951 filename, wtap_strerror(err));
2954 write_failure_alert_box(filename, err);
2958 /* Check for write errors - if the file is being written to an NFS server,
2959 a write error may not show up until the file is closed, as NFS clients
2960 might not send writes to the server until the "write()" call finishes,
2961 so that the write may fail on the server but the "write()" may succeed. */
2963 cf_close_failure_alert_box(const char *filename, int err)
2966 /* Wiretap error. */
2969 case WTAP_ERR_CANT_CLOSE:
2970 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2971 "The file \"%s\" couldn't be closed for some unknown reason.",
2975 case WTAP_ERR_SHORT_WRITE:
2976 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2977 "Not all the packets could be written to the file \"%s\".",
2982 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2983 "An error occurred while closing the file \"%s\": %s.",
2984 filename, wtap_strerror(err));
2989 We assume that a close error from the OS is really a write error. */
2990 write_failure_alert_box(filename, err);
2995 /* Copies a file in binary mode, for those operating systems that care about
2997 * Returns TRUE on success, FALSE on failure. If a failure, it also
2998 * displays a simple dialog window with the error message.
3001 copy_binary_file(char *from_filename, char *to_filename)
3003 int from_fd, to_fd, nread, nwritten, err;
3006 /* Copy the raw bytes of the file. */
3007 from_fd = open(from_filename, O_RDONLY | O_BINARY);
3009 open_failure_alert_box(from_filename, errno, FALSE);
3013 /* Use open() instead of creat() so that we can pass the O_BINARY
3014 flag, which is relevant on Win32; it appears that "creat()"
3015 may open the file in text mode, not binary mode, but we want
3016 to copy the raw bytes of the file, so we need the output file
3017 to be open in binary mode. */
3018 to_fd = open(to_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
3020 open_failure_alert_box(to_filename, errno, TRUE);
3025 while ((nread = read(from_fd, pd, sizeof pd)) > 0) {
3026 nwritten = write(to_fd, pd, nread);
3027 if (nwritten < nread) {
3031 err = WTAP_ERR_SHORT_WRITE;
3032 write_failure_alert_box(to_filename, err);
3040 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3041 "An error occurred while reading from the file \"%s\": %s.",
3042 from_filename, strerror(err));
3048 if (close(to_fd) < 0) {
3049 write_failure_alert_box(to_filename, errno);