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.
25 /* With MSVC and a libethereal.dll this file needs to import some variables
26 in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */
27 #define _NEED_VAR_IMPORT_
50 #ifdef HAVE_SYS_STAT_H
58 #ifdef NEED_SNPRINTF_H
59 # include "snprintf.h"
62 #ifdef NEED_STRERROR_H
66 #include <epan/epan.h>
67 #include <epan/filesystem.h>
70 #include "color_filters.h"
71 #include <epan/column.h>
72 #include <epan/packet.h>
73 #include "packet-range.h"
79 #include "alert_box.h"
80 #include "simple_dialog.h"
81 #include "progress_dlg.h"
83 #include "statusbar.h"
84 #include <epan/prefs.h>
85 #include <epan/dfilter/dfilter.h>
86 #include <epan/conversation.h>
88 #include <epan/epan_dissect.h>
90 #include "tap_dfilter_dlg.h"
91 #include <epan/dissectors/packet-data.h>
93 /* Win32 needs the O_BINARY flag for open() */
99 gboolean auto_scroll_live;
102 static guint32 firstsec, firstusec;
103 static guint32 prevsec, prevusec;
104 static guint32 cum_bytes = 0;
106 static void read_packet(capture_file *cf, long offset);
108 static void rescan_packets(capture_file *cf, const char *action, const char *action_item,
109 gboolean refilter, gboolean redissect);
111 static gboolean match_protocol_tree(capture_file *cf, frame_data *fdata,
113 static void match_subtree_text(proto_node *node, gpointer data);
114 static gboolean match_summary_line(capture_file *cf, frame_data *fdata,
116 static gboolean match_ascii_and_unicode(capture_file *cf, frame_data *fdata,
118 static gboolean match_ascii(capture_file *cf, frame_data *fdata,
120 static gboolean match_unicode(capture_file *cf, frame_data *fdata,
122 static gboolean match_binary(capture_file *cf, frame_data *fdata,
124 static gboolean match_dfilter(capture_file *cf, frame_data *fdata,
126 static gboolean find_packet(capture_file *cf,
127 gboolean (*match_function)(capture_file *, frame_data *, void *),
130 static void cf_open_failure_alert_box(const char *filename, int err,
131 gchar *err_info, gboolean for_writing,
133 static char *file_rename_error_message(int err);
134 static void cf_write_failure_alert_box(const char *filename, int err);
135 static void cf_close_failure_alert_box(const char *filename, int err);
136 static gboolean copy_binary_file(char *from_filename, char *to_filename);
138 /* Update the progress bar this many times when reading a file. */
139 #define N_PROGBAR_UPDATES 100
141 /* Number of "frame_data" structures per memory chunk.
142 XXX - is this the right number? */
143 #define FRAME_DATA_CHUNK_SIZE 1024
147 cf_open(char *fname, gboolean is_tempfile, capture_file *cf)
155 wth = wtap_open_offline(fname, &err, &err_info, TRUE);
159 /* Find the size of the file. */
161 if (fstat(fd, &cf_stat) < 0) {
167 /* The open succeeded. Close whatever capture file we had open,
168 and fill in the information for this file. */
171 /* Initialize all data structures used for dissection. */
174 /* We're about to start reading the file. */
175 cf->state = FILE_READ_IN_PROGRESS;
179 cf->f_len = cf_stat.st_size;
181 /* Set the file name because we need it to set the follow stream filter.
182 XXX - is that still true? We need it for other reasons, though,
184 cf->filename = g_strdup(fname);
186 /* Indicate whether it's a permanent or temporary file. */
187 cf->is_tempfile = is_tempfile;
189 /* If it's a temporary capture buffer file, mark it as not saved. */
190 cf->user_saved = !is_tempfile;
192 cf->cd_t = wtap_file_type(cf->wth);
194 cf->displayed_count = 0;
195 cf->marked_count = 0;
196 cf->drops_known = FALSE;
200 cf->snap = wtap_snapshot_length(cf->wth);
202 /* Snapshot length not known. */
203 cf->has_snap = FALSE;
204 cf->snap = WTAP_MAX_PACKET_SIZE;
207 firstsec = 0, firstusec = 0;
208 prevsec = 0, prevusec = 0;
210 cf->plist_chunk = g_mem_chunk_new("frame_data_chunk",
212 FRAME_DATA_CHUNK_SIZE * sizeof(frame_data),
214 g_assert(cf->plist_chunk);
219 cf_open_failure_alert_box(fname, err, err_info, FALSE, 0);
223 /* Reset everything to a pristine state */
225 cf_close(capture_file *cf)
227 /* Die if we're in the middle of reading a file. */
228 g_assert(cf->state != FILE_READ_IN_PROGRESS);
230 /* Destroy all windows, which refer to the
231 capture file we're closing. */
232 destroy_cfile_wins();
238 /* We have no file open... */
239 if (cf->filename != NULL) {
240 /* If it's a temporary file, remove it. */
242 unlink(cf->filename);
243 g_free(cf->filename);
246 /* ...which means we have nothing to save. */
247 cf->user_saved = FALSE;
249 if (cf->plist_chunk != NULL) {
250 g_mem_chunk_destroy(cf->plist_chunk);
251 cf->plist_chunk = NULL;
253 if (cf->rfcode != NULL) {
254 dfilter_free(cf->rfcode);
258 cf->plist_end = NULL;
259 unselect_packet(cf); /* nothing to select */
260 cf->first_displayed = NULL;
261 cf->last_displayed = NULL;
263 /* No frame selected, no field in that frame selected. */
264 cf->current_frame = NULL;
265 cf->finfo_selected = NULL;
267 /* Clear the packet list. */
268 packet_list_freeze();
277 /* Clear any file-related status bar messages.
278 XXX - should be "clear *ALL* file-related status bar messages;
279 will there ever be more than one on the stack? */
280 statusbar_pop_file_msg();
282 /* Restore the standard title bar message. */
283 set_main_window_name("The Ethereal Network Analyzer");
285 /* Disable all menu items that make sense only if you have a capture. */
286 set_menus_for_capture_file(FALSE);
287 set_menus_for_unsaved_capture_file(FALSE);
288 set_menus_for_captured_packets(FALSE);
289 set_menus_for_selected_packet(cf);
290 set_menus_for_capture_in_progress(FALSE);
291 set_menus_for_selected_tree_row(cf);
293 reset_tap_listeners();
295 /* We have no file open. */
296 cf->state = FILE_CLOSED;
299 /* Set the file name in the status line, in the name for the main window,
300 and in the name for the main window's icon. */
302 set_display_filename(capture_file *cf)
304 const gchar *name_ptr;
306 static const gchar done_fmt_nodrops[] = " File: %s %s %02u:%02u:%02u";
307 static const gchar done_fmt_drops[] = " File: %s %s %02u:%02u:%02u Drops: %u";
309 gchar *win_name_fmt = "%s - Ethereal";
313 name_ptr = cf_get_display_name(cf);
315 if (!cf->is_tempfile) {
316 /* Add this filename to the list of recent files in the "Recent Files" submenu */
317 add_menu_recent_capture_file(cf->filename);
320 if (cf->f_len/1024/1024 > 10) {
321 size_str = g_strdup_printf("%ld MB", cf->f_len/1024/1024);
322 } else if (cf->f_len/1024 > 10) {
323 size_str = g_strdup_printf("%ld KB", cf->f_len/1024);
325 size_str = g_strdup_printf("%ld bytes", cf->f_len);
328 if (cf->drops_known) {
329 done_msg = g_strdup_printf(done_fmt_drops, name_ptr, size_str,
330 cf->esec/3600, cf->esec%3600/60, cf->esec%60, cf->drops);
332 done_msg = g_strdup_printf(done_fmt_nodrops, name_ptr, size_str,
333 cf->esec/3600, cf->esec%3600/60, cf->esec%60);
335 statusbar_push_file_msg(done_msg);
338 msg_len = strlen(name_ptr) + strlen(win_name_fmt) + 1;
339 win_name = g_malloc(msg_len);
340 snprintf(win_name, msg_len, win_name_fmt, name_ptr);
341 set_main_window_name(win_name);
346 cf_read(capture_file *cf)
350 const gchar *name_ptr;
351 gchar *load_msg, *load_fmt = "%s";
353 char errmsg_errno[1024+1];
354 gchar err_str[2048+1];
356 progdlg_t *progbar = NULL;
359 * XXX - should be "off_t", but Wiretap would need more work to handle
360 * the full size of "off_t" on platforms where it's more than a "long"
368 gchar status_str[100];
369 int progbar_nextstep;
373 reset_tap_listeners();
374 tap_dfilter_dlg_update();
375 name_ptr = get_basename(cf->filename);
377 load_msg = g_strdup_printf(" Loading: %s", name_ptr);
378 statusbar_push_file_msg(load_msg);
381 load_msg = g_strdup_printf(load_fmt, name_ptr);
383 /* Update the progress bar when it gets to this value. */
384 progbar_nextstep = 0;
385 /* When we reach the value that triggers a progress bar update,
386 bump that value by this amount. */
387 progbar_quantum = cf->f_len/N_PROGBAR_UPDATES;
389 packet_list_freeze();
392 g_get_current_time(&start_time);
394 while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
395 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
396 when we update it, we have to run the GTK+ main loop to get it
397 to repaint what's pending, and doing so may involve an "ioctl()"
398 to see if there's any pending input from an X server, and doing
399 that for every packet can be costly, especially on a big file. */
400 if (data_offset >= progbar_nextstep) {
401 file_pos = lseek(cf->filed, 0, SEEK_CUR);
402 prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
403 if (prog_val > 1.0) {
404 /* The file probably grew while we were reading it.
405 Update "cf->f_len", and try again. */
406 fd = wtap_fd(cf->wth);
407 if (fstat(fd, &cf_stat) >= 0) {
408 cf->f_len = cf_stat.st_size;
409 prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
411 /* If it's still > 1, either the "fstat()" failed (in which
412 case there's not much we can do about it), or the file
413 *shrank* (in which case there's not much we can do about
414 it); just clip the progress value at 1.0. */
418 if (progbar == NULL) {
419 /* Create the progress bar if necessary */
420 progbar = delayed_create_progress_dlg("Loading", load_msg,
421 &stop_flag, &start_time, prog_val);
425 if (progbar != NULL) {
426 g_snprintf(status_str, sizeof(status_str),
427 "%luKB of %luKB", file_pos / 1024, cf->f_len / 1024);
428 update_progress_dlg(progbar, prog_val, status_str);
430 progbar_nextstep += progbar_quantum;
434 /* Well, the user decided to abort the read. Destroy the progress
435 bar, close the capture file, and return READ_ABORTED so our caller
436 can do whatever is appropriate when that happens. */
437 destroy_progress_dlg(progbar);
438 cf->state = FILE_READ_ABORTED; /* so that we're allowed to close it */
439 packet_list_thaw(); /* undo our freeze */
441 return (READ_ABORTED);
443 read_packet(cf, data_offset);
446 /* We're done reading the file; destroy the progress bar if it was created. */
450 destroy_progress_dlg(progbar);
452 /* We're done reading sequentially through the file. */
453 cf->state = FILE_READ_DONE;
455 /* Close the sequential I/O side, to free up memory it requires. */
456 wtap_sequential_close(cf->wth);
458 /* Allow the protocol dissectors to free up memory that they
459 * don't need after the sequential run-through of the packets. */
460 postseq_cleanup_all_protocols();
462 /* Set the file encapsulation type now; we don't know what it is until
463 we've looked at all the packets, as we don't know until then whether
464 there's more than one type (and thus whether it's
465 WTAP_ENCAP_PER_PACKET). */
466 cf->lnk_t = wtap_file_encap(cf->wth);
468 cf->current_frame = cf->first_displayed;
471 statusbar_pop_file_msg();
472 set_display_filename(cf);
474 /* Enable menu items that make sense if you have a capture file you've
476 set_menus_for_capture_file(TRUE);
477 set_menus_for_unsaved_capture_file(!cf->user_saved);
479 /* Enable menu items that make sense if you have some captured packets. */
480 set_menus_for_captured_packets(TRUE);
482 /* If we have any displayed packets to select, select the first of those
483 packets by making the first row the selected row. */
484 if (cf->first_displayed != NULL)
485 packet_list_select_row(0);
488 /* Put up a message box noting that the read failed somewhere along
489 the line. Don't throw out the stuff we managed to read, though,
493 case WTAP_ERR_UNSUPPORTED_ENCAP:
494 snprintf(errmsg_errno, sizeof(errmsg_errno),
495 "The capture file has a packet with a network type that Ethereal doesn't support.\n(%s)",
498 errmsg = errmsg_errno;
501 case WTAP_ERR_CANT_READ:
502 errmsg = "An attempt to read from the capture file failed for"
503 " some unknown reason.";
506 case WTAP_ERR_SHORT_READ:
507 errmsg = "The capture file appears to have been cut short"
508 " in the middle of a packet.";
511 case WTAP_ERR_BAD_RECORD:
512 snprintf(errmsg_errno, sizeof(errmsg_errno),
513 "The capture file appears to be damaged or corrupt.\n(%s)",
516 errmsg = errmsg_errno;
520 snprintf(errmsg_errno, sizeof(errmsg_errno),
521 "An error occurred while reading the"
522 " capture file: %s.", wtap_strerror(err));
523 errmsg = errmsg_errno;
526 snprintf(err_str, sizeof err_str, errmsg);
527 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_str);
530 return (READ_SUCCESS);
535 cf_start_tail(char *fname, gboolean is_tempfile, capture_file *cf)
540 err = cf_open(fname, is_tempfile, cf);
542 /* Disable menu items that make no sense if you're currently running
544 set_menus_for_capture_in_progress(TRUE);
546 /* Enable menu items that make sense if you have some captured
547 packets (yes, I know, we don't have any *yet*). */
548 set_menus_for_captured_packets(TRUE);
550 capture_msg = g_strdup_printf(" %s: <live capture in progress>", cf->iface);
552 statusbar_push_file_msg(capture_msg);
560 cf_continue_tail(capture_file *cf, int to_read, int *err)
562 long data_offset = 0;
567 packet_list_freeze();
569 while (to_read != 0 && (wtap_read(cf->wth, err, &err_info, &data_offset))) {
570 if (cf->state == FILE_READ_ABORTED) {
571 /* Well, the user decided to exit Ethereal. Break out of the
572 loop, and let the code below (which is called even if there
573 aren't any packets left to read) exit. */
576 read_packet(cf, data_offset);
582 /* XXX - this cheats and looks inside the packet list to find the final
584 if (auto_scroll_live && cf->plist_end != NULL)
585 packet_list_moveto_end();
587 if (cf->state == FILE_READ_ABORTED) {
588 /* Well, the user decided to exit Ethereal. Return READ_ABORTED
589 so that our caller can kill off the capture child process;
590 this will cause an EOF on the pipe from the child, so
591 "cf_finish_tail()" will be called, and it will clean up
594 } else if (*err != 0) {
595 /* We got an error reading the capture file.
596 XXX - pop up a dialog box? */
599 return (READ_SUCCESS);
603 cf_finish_tail(capture_file *cf, int *err)
610 packet_list_freeze();
612 while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
613 if (cf->state == FILE_READ_ABORTED) {
614 /* Well, the user decided to abort the read. Break out of the
615 loop, and let the code below (which is called even if there
616 aren't any packets left to read) exit. */
619 read_packet(cf, data_offset);
622 if (cf->state == FILE_READ_ABORTED) {
623 /* Well, the user decided to abort the read. We're only called
624 when the child capture process closes the pipe to us (meaning
625 it's probably exited), so we can just close the capture
626 file; we return READ_ABORTED so our caller can do whatever
627 is appropriate when that happens. */
633 if (auto_scroll_live && cf->plist_end != NULL)
634 /* XXX - this cheats and looks inside the packet list to find the final
636 packet_list_moveto_end();
638 /* We're done reading sequentially through the file. */
639 cf->state = FILE_READ_DONE;
641 /* we have to update the f_len field */
642 /* Find the size of the file. */
643 fd = wtap_fd(cf->wth);
644 if (fstat(fd, &cf_stat) >= 0) {
645 cf->f_len = cf_stat.st_size;
648 /* We're done reading sequentially through the file; close the
649 sequential I/O side, to free up memory it requires. */
650 wtap_sequential_close(cf->wth);
652 /* Allow the protocol dissectors to free up memory that they
653 * don't need after the sequential run-through of the packets. */
654 postseq_cleanup_all_protocols();
656 /* Set the file encapsulation type now; we don't know what it is until
657 we've looked at all the packets, as we don't know until then whether
658 there's more than one type (and thus whether it's
659 WTAP_ENCAP_PER_PACKET). */
660 cf->lnk_t = wtap_file_encap(cf->wth);
662 /* Pop the "<live capture in progress>" message off the status bar. */
663 statusbar_pop_file_msg();
665 set_display_filename(cf);
667 /* Enable menu items that make sense if you're not currently running
669 set_menus_for_capture_in_progress(FALSE);
671 /* Enable menu items that make sense if you have a capture file
672 you've finished reading. */
673 set_menus_for_capture_file(TRUE);
674 set_menus_for_unsaved_capture_file(!cf->user_saved);
677 /* We got an error reading the capture file.
678 XXX - pop up a dialog box? */
681 return (READ_SUCCESS);
684 #endif /* HAVE_LIBPCAP */
687 cf_get_display_name(capture_file *cf)
689 const gchar *displayname;
691 /* Return a name to use in displays */
692 if (!cf->is_tempfile) {
693 /* Get the last component of the file name, and use that. */
695 displayname = get_basename(cf->filename);
697 /* Add this filename to the list of recent files in the "Recent Files" submenu */
698 add_menu_recent_capture_file(cf->filename);
700 displayname="(No file)";
703 /* The file we read is a temporary file from a live capture;
704 we don't mention its name. */
705 displayname = "(Untitled)";
711 color_filter_t *colorf;
713 } apply_color_filter_args;
716 * If no color filter has been applied, apply this one.
717 * (The "if no color filter has been applied" is to handle the case where
718 * more than one color filter matches the packet.)
721 apply_color_filter(gpointer filter_arg, gpointer argp)
723 color_filter_t *colorf = filter_arg;
724 apply_color_filter_args *args = argp;
726 if (colorf->c_colorfilter != NULL && args->colorf == NULL) {
727 if (dfilter_apply_edt(colorf->c_colorfilter, args->edt))
728 args->colorf = colorf;
733 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
734 union wtap_pseudo_header *pseudo_header, const guchar *buf,
737 apply_color_filter_args args;
739 gboolean create_proto_tree = FALSE;
742 /* just add some value here until we know if it is being displayed or not */
743 fdata->cum_bytes = cum_bytes + fdata->pkt_len;
745 /* We don't yet have a color filter to apply. */
748 /* If we don't have the time stamp of the first packet in the
749 capture, it's because this is the first packet. Save the time
750 stamp of this packet as the time stamp of the first packet. */
751 if (!firstsec && !firstusec) {
752 firstsec = fdata->abs_secs;
753 firstusec = fdata->abs_usecs;
755 /* if this frames is marked as a reference time frame, reset
756 firstsec and firstusec to this frame */
757 if(fdata->flags.ref_time){
758 firstsec = fdata->abs_secs;
759 firstusec = fdata->abs_usecs;
762 /* If we don't have the time stamp of the previous displayed packet,
763 it's because this is the first displayed packet. Save the time
764 stamp of this packet as the time stamp of the previous displayed
766 if (!prevsec && !prevusec) {
767 prevsec = fdata->abs_secs;
768 prevusec = fdata->abs_usecs;
771 /* Get the time elapsed between the first packet and this packet. */
772 compute_timestamp_diff(&fdata->rel_secs, &fdata->rel_usecs,
773 fdata->abs_secs, fdata->abs_usecs, firstsec, firstusec);
775 /* If it's greater than the current elapsed time, set the elapsed time
776 to it (we check for "greater than" so as not to be confused by
777 time moving backwards). */
778 if ((gint32)cf->esec < fdata->rel_secs
779 || ((gint32)cf->esec == fdata->rel_secs && (gint32)cf->eusec < fdata->rel_usecs)) {
780 cf->esec = fdata->rel_secs;
781 cf->eusec = fdata->rel_usecs;
784 /* Get the time elapsed between the previous displayed packet and
786 compute_timestamp_diff(&fdata->del_secs, &fdata->del_usecs,
787 fdata->abs_secs, fdata->abs_usecs, prevsec, prevusec);
791 we have a display filter and are re-applying it;
793 we have a list of color filters;
795 we have tap listeners;
797 allocate a protocol tree root node, so that we'll construct
798 a protocol tree against which a filter expression can be
800 if ((cf->dfcode != NULL && refilter) || filter_list != NULL
801 || num_tap_filters != 0)
802 create_proto_tree = TRUE;
804 /* Dissect the frame. */
805 edt = epan_dissect_new(create_proto_tree, FALSE);
807 if (cf->dfcode != NULL && refilter) {
808 epan_dissect_prime_dfilter(edt, cf->dfcode);
811 filter_list_prime_edt(edt);
814 epan_dissect_run(edt, pseudo_header, buf, fdata, &cf->cinfo);
815 tap_push_tapped_queue(edt);
817 /* If we have a display filter, apply it if we're refiltering, otherwise
818 leave the "passed_dfilter" flag alone.
820 If we don't have a display filter, set "passed_dfilter" to 1. */
821 if (cf->dfcode != NULL) {
823 if (cf->dfcode != NULL)
824 fdata->flags.passed_dfilter = dfilter_apply_edt(cf->dfcode, edt) ? 1 : 0;
826 fdata->flags.passed_dfilter = 1;
829 fdata->flags.passed_dfilter = 1;
831 /* If we have color filters, and the frame is to be displayed, apply
832 the color filters. */
833 if (fdata->flags.passed_dfilter) {
834 if (filter_list != NULL) {
836 g_slist_foreach(filter_list, apply_color_filter, &args);
841 if( (fdata->flags.passed_dfilter)
842 || (edt->pi.fd->flags.ref_time) ){
843 /* This frame either passed the display filter list or is marked as
844 a time reference frame. All time reference frames are displayed
845 even if they dont pass the display filter */
846 /* if this was a TIME REF frame we should reset the cul bytes field */
847 if(edt->pi.fd->flags.ref_time){
848 cum_bytes = fdata->pkt_len;
849 fdata->cum_bytes = cum_bytes;
852 /* increase cum_bytes with this packets length */
853 cum_bytes += fdata->pkt_len;
855 epan_dissect_fill_in_columns(edt);
857 /* If we haven't yet seen the first frame, this is it.
859 XXX - we must do this before we add the row to the display,
860 as, if the display's GtkCList's selection mode is
861 GTK_SELECTION_BROWSE, when the first entry is added to it,
862 "select_packet()" will be called, and it will fetch the row
863 data for the 0th row, and will get a null pointer rather than
864 "fdata", as "gtk_clist_append()" won't yet have returned and
865 thus "gtk_clist_set_row_data()" won't yet have been called.
867 We thus need to leave behind bread crumbs so that
868 "select_packet()" can find this frame. See the comment
869 in "select_packet()". */
870 if (cf->first_displayed == NULL)
871 cf->first_displayed = fdata;
873 /* This is the last frame we've seen so far. */
874 cf->last_displayed = fdata;
876 row = packet_list_append(cf->cinfo.col_data, fdata);
878 /* If the packet matches a color filter,
879 * store matching color_filter_t object in frame data. */
880 if (filter_list != NULL && (args.colorf != NULL)) {
881 /* add the matching colorfilter to the frame data */
882 fdata->color_filter = args.colorf;
883 /* If packet is marked, use colors from preferences */
884 if (fdata->flags.marked) {
885 packet_list_set_colors(row, &prefs.gui_marked_fg, &prefs.gui_marked_bg);
886 } else /* if (filter_list != NULL && (args.colorf != NULL)) */ {
887 packet_list_set_colors(row, &(args.colorf->fg_color),
888 &(args.colorf->bg_color));
891 /* No color filter match */
892 fdata->color_filter = NULL;
893 if (fdata->flags.marked) {
894 packet_list_set_colors(row, &prefs.gui_marked_fg, &prefs.gui_marked_bg);
898 /* Set the time of the previous displayed frame to the time of this
900 prevsec = fdata->abs_secs;
901 prevusec = fdata->abs_usecs;
903 cf->displayed_count++;
905 /* This frame didn't pass the display filter, so it's not being added
906 to the clist, and thus has no row. */
909 epan_dissect_free(edt);
914 read_packet(capture_file *cf, long offset)
916 const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
917 union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
918 const guchar *buf = wtap_buf_ptr(cf->wth);
921 frame_data *plist_end;
924 /* Allocate the next list entry, and add it to the list. */
925 fdata = g_mem_chunk_alloc(cf->plist_chunk);
930 fdata->pkt_len = phdr->len;
931 fdata->cap_len = phdr->caplen;
932 fdata->file_off = offset;
933 fdata->lnk_t = phdr->pkt_encap;
934 fdata->abs_secs = phdr->ts.tv_sec;
935 fdata->abs_usecs = phdr->ts.tv_usec;
936 fdata->flags.encoding = CHAR_ASCII;
937 fdata->flags.visited = 0;
938 fdata->flags.marked = 0;
939 fdata->flags.ref_time = 0;
943 edt = epan_dissect_new(TRUE, FALSE);
944 epan_dissect_prime_dfilter(edt, cf->rfcode);
945 epan_dissect_run(edt, pseudo_header, buf, fdata, NULL);
946 passed = dfilter_apply_edt(cf->rfcode, edt);
947 epan_dissect_free(edt);
950 plist_end = cf->plist_end;
951 fdata->prev = plist_end;
952 if (plist_end != NULL)
953 plist_end->next = fdata;
956 cf->plist_end = fdata;
959 fdata->num = cf->count;
960 add_packet_to_packet_list(fdata, cf, pseudo_header, buf, TRUE);
962 /* XXX - if we didn't have read filters, or if we could avoid
963 allocating the "frame_data" structure until we knew whether
964 the frame passed the read filter, we could use a G_ALLOC_ONLY
967 ...but, at least in one test I did, where I just made the chunk
968 a G_ALLOC_ONLY chunk and read in a huge capture file, it didn't
969 seem to save a noticeable amount of time or space. */
970 g_mem_chunk_free(cf->plist_chunk, fdata);
975 cf_merge_files(const char *out_filename, int out_fd, int in_file_count,
976 char *const *in_filenames, int file_type, gboolean do_append)
978 merge_in_file_t *in_files;
981 int open_err, read_err, write_err, close_err;
985 char errmsg_errno[1024+1];
986 gchar err_str[2048+1];
988 gboolean got_read_error = FALSE, got_write_error = FALSE;
990 progdlg_t *progbar = NULL;
993 * XXX - should be "off_t", but Wiretap would need more work to handle
994 * the full size of "off_t" on platforms where it's more than a "long"
997 long f_len, file_pos;
1000 gchar status_str[100];
1001 int progbar_nextstep;
1002 int progbar_quantum;
1004 /* open the input files */
1005 if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
1006 &open_err, &err_info, &err_fileno)) {
1008 cf_open_failure_alert_box(in_filenames[err_fileno], open_err, err_info,
1013 pdh = wtap_dump_fdopen(out_fd, file_type,
1014 merge_select_frame_type(in_file_count, in_files),
1015 merge_max_snapshot_length(in_file_count, in_files), &open_err);
1017 merge_close_in_files(in_file_count, in_files);
1019 cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
1024 /* Get the sum of the sizes of all the files. */
1026 for (i = 0; i < in_file_count; i++)
1027 f_len += in_files[i].size;
1029 /* Update the progress bar when it gets to this value. */
1030 progbar_nextstep = 0;
1031 /* When we reach the value that triggers a progress bar update,
1032 bump that value by this amount. */
1033 progbar_quantum = f_len/N_PROGBAR_UPDATES;
1036 g_get_current_time(&start_time);
1038 /* do the merge (or append) */
1041 wth = merge_append_read_packet(in_file_count, in_files, &read_err,
1044 wth = merge_read_packet(in_file_count, in_files, &read_err,
1048 got_read_error = TRUE;
1052 /* Get the sum of the data offsets in all of the files. */
1054 for (i = 0; i < in_file_count; i++)
1055 data_offset += in_files[i].data_offset;
1057 if (data_offset >= progbar_nextstep) {
1058 /* Get the sum of the seek positions in all of the files. */
1060 for (i = 0; i < in_file_count; i++)
1061 file_pos += lseek(wtap_fd(in_files[i].wth), 0, SEEK_CUR);
1062 prog_val = (gfloat) file_pos / (gfloat) f_len;
1063 if (prog_val > 1.0) {
1064 /* Some file probably grew while we were reading it.
1065 That "shouldn't happen", so we'll just clip the progress
1069 if (progbar == NULL) {
1070 /* Create the progress bar if necessary */
1071 progbar = delayed_create_progress_dlg("Merging", "files",
1072 &stop_flag, &start_time, prog_val);
1074 if (progbar != NULL) {
1075 g_snprintf(status_str, sizeof(status_str),
1076 "%luKB of %luKB", file_pos / 1024, f_len / 1024);
1077 update_progress_dlg(progbar, prog_val, status_str);
1079 progbar_nextstep += progbar_quantum;
1082 if (!wtap_dump(pdh, wtap_phdr(wth), wtap_pseudoheader(wth),
1083 wtap_buf_ptr(wth), &write_err)) {
1084 got_write_error = TRUE;
1089 /* We're done merging the files; destroy the progress bar if it was created. */
1090 if (progbar != NULL)
1091 destroy_progress_dlg(progbar);
1093 merge_close_in_files(in_file_count, in_files);
1094 if (!got_read_error && !got_write_error) {
1095 if (!wtap_dump_close(pdh, &write_err))
1096 got_write_error = TRUE;
1098 wtap_dump_close(pdh, &close_err);
1100 if (got_read_error) {
1102 * Find the file on which we got the error, and report the error.
1104 for (i = 0; i < in_file_count; i++) {
1105 if (in_files[i].state == GOT_ERROR) {
1106 /* Put up a message box noting that a read failed somewhere along
1110 case WTAP_ERR_UNSUPPORTED_ENCAP:
1111 snprintf(errmsg_errno, sizeof(errmsg_errno),
1112 "The capture file %%s has a packet with a network type that Ethereal doesn't support.\n(%s)",
1115 errmsg = errmsg_errno;
1118 case WTAP_ERR_CANT_READ:
1119 errmsg = "An attempt to read from the capture file %s failed for"
1120 " some unknown reason.";
1123 case WTAP_ERR_SHORT_READ:
1124 errmsg = "The capture file %s appears to have been cut short"
1125 " in the middle of a packet.";
1128 case WTAP_ERR_BAD_RECORD:
1129 snprintf(errmsg_errno, sizeof(errmsg_errno),
1130 "The capture file %%s appears to be damaged or corrupt.\n(%s)",
1133 errmsg = errmsg_errno;
1137 snprintf(errmsg_errno, sizeof(errmsg_errno),
1138 "An error occurred while reading the"
1139 " capture file %%s: %s.", wtap_strerror(read_err));
1140 errmsg = errmsg_errno;
1143 snprintf(err_str, sizeof err_str, errmsg, in_files[i].filename);
1144 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_str);
1149 if (got_write_error) {
1150 /* Put up an alert box for the write error. */
1151 cf_write_failure_alert_box(out_filename, write_err);
1154 return (!got_read_error && !got_write_error);
1158 filter_packets(capture_file *cf, gchar *dftext, gboolean force)
1161 char *filter_new = dftext ? dftext : "";
1162 char *filter_old = cf->dfilter ? cf->dfilter : "";
1164 /* if new filter equals old one, do nothing unless told to do so */
1165 if (!force && strcmp(filter_new, filter_old) == 0) {
1169 if (dftext == NULL) {
1170 /* The new filter is an empty filter (i.e., display all packets). */
1174 * We have a filter; make a copy of it (as we'll be saving it),
1175 * and try to compile it.
1177 dftext = g_strdup(dftext);
1178 if (!dfilter_compile(dftext, &dfcode)) {
1179 /* The attempt failed; report an error. */
1180 gchar *safe_dftext = simple_dialog_format_message(dftext);
1181 gchar *safe_dfilter_error_msg = simple_dialog_format_message(
1183 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1186 "The following display filter isn't a valid display filter:\n%s\n"
1187 "See the help for a description of the display filter syntax.",
1188 simple_dialog_primary_start(), safe_dfilter_error_msg,
1189 simple_dialog_primary_end(), safe_dftext);
1190 g_free(safe_dfilter_error_msg);
1191 g_free(safe_dftext);
1197 if (dfcode == NULL) {
1198 /* Yes - free the filter text, and set it to null. */
1204 /* We have a valid filter. Replace the current filter. */
1205 if (cf->dfilter != NULL)
1206 g_free(cf->dfilter);
1207 cf->dfilter = dftext;
1208 if (cf->dfcode != NULL)
1209 dfilter_free(cf->dfcode);
1210 cf->dfcode = dfcode;
1212 /* Now rescan the packet list, applying the new filter, but not
1213 throwing away information constructed on a previous pass. */
1214 if (dftext == NULL) {
1215 rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
1217 rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
1223 colorize_packets(capture_file *cf)
1225 rescan_packets(cf, "Colorizing", "all packets", FALSE, FALSE);
1229 reftime_packets(capture_file *cf)
1231 rescan_packets(cf, "Updating Reftime", "all packets", FALSE, FALSE);
1235 redissect_packets(capture_file *cf)
1237 rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
1240 /* Rescan the list of packets, reconstructing the CList.
1242 "action" describes why we're doing this; it's used in the progress
1245 "action_item" describes what we're doing; it's used in the progress
1248 "refilter" is TRUE if we need to re-evaluate the filter expression.
1250 "redissect" is TRUE if we need to make the dissectors reconstruct
1251 any state information they have (because a preference that affects
1252 some dissector has changed, meaning some dissector might construct
1253 its state differently from the way it was constructed the last time). */
1255 rescan_packets(capture_file *cf, const char *action, const char *action_item,
1256 gboolean refilter, gboolean redissect)
1259 progdlg_t *progbar = NULL;
1264 frame_data *selected_frame, *preceding_frame, *following_frame, *prev_frame;
1265 int selected_row, prev_row, preceding_row, following_row;
1266 gboolean selected_frame_seen;
1269 GTimeVal start_time;
1270 gchar status_str[100];
1271 int progbar_nextstep;
1272 int progbar_quantum;
1275 reset_tap_listeners();
1276 /* Which frame, if any, is the currently selected frame?
1277 XXX - should the selected frame or the focus frame be the "current"
1278 frame, that frame being the one from which "Find Frame" searches
1280 selected_frame = cf->current_frame;
1282 /* We don't yet know what row that frame will be on, if any, after we
1283 rebuild the clist, however. */
1287 /* We need to re-initialize all the state information that protocols
1288 keep, because some preference that controls a dissector has changed,
1289 which might cause the state information to be constructed differently
1290 by that dissector. */
1292 /* Initialize all data structures used for dissection. */
1296 /* Freeze the packet list while we redo it, so we don't get any
1297 screen updates while it happens. */
1298 packet_list_freeze();
1301 packet_list_clear();
1303 /* We don't yet know which will be the first and last frames displayed. */
1304 cf->first_displayed = NULL;
1305 cf->last_displayed = NULL;
1307 /* We currently don't display any packets */
1308 cf->displayed_count = 0;
1310 /* Iterate through the list of frames. Call a routine for each frame
1311 to check whether it should be displayed and, if so, add it to
1312 the display list. */
1318 /* Update the progress bar when it gets to this value. */
1319 progbar_nextstep = 0;
1320 /* When we reach the value that triggers a progress bar update,
1321 bump that value by this amount. */
1322 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1323 /* Count of packets at which we've looked. */
1327 g_get_current_time(&start_time);
1329 row = -1; /* no previous row yet */
1334 preceding_frame = NULL;
1336 following_frame = NULL;
1338 selected_frame_seen = FALSE;
1340 for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
1341 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1342 when we update it, we have to run the GTK+ main loop to get it
1343 to repaint what's pending, and doing so may involve an "ioctl()"
1344 to see if there's any pending input from an X server, and doing
1345 that for every packet can be costly, especially on a big file. */
1346 if (count >= progbar_nextstep) {
1347 /* let's not divide by zero. I should never be started
1348 * with count == 0, so let's assert that
1350 g_assert(cf->count > 0);
1351 prog_val = (gfloat) count / cf->count;
1353 if (progbar == NULL)
1354 /* Create the progress bar if necessary */
1355 progbar = delayed_create_progress_dlg(action, action_item, &stop_flag,
1356 &start_time, prog_val);
1358 if (progbar != NULL) {
1359 g_snprintf(status_str, sizeof(status_str),
1360 "%4u of %u frames", count, cf->count);
1361 update_progress_dlg(progbar, prog_val, status_str);
1364 progbar_nextstep += progbar_quantum;
1368 /* Well, the user decided to abort the filtering. Just stop.
1370 XXX - go back to the previous filter? Users probably just
1371 want not to wait for a filtering operation to finish;
1372 unless we cancel by having no filter, reverting to the
1373 previous filter will probably be even more expensive than
1374 continuing the filtering, as it involves going back to the
1375 beginning and filtering, and even with no filter we currently
1376 have to re-generate the entire clist, which is also expensive.
1378 I'm not sure what Network Monitor does, but it doesn't appear
1379 to give you an unfiltered display if you cancel. */
1386 /* Since all state for the frame was destroyed, mark the frame
1387 * as not visited, free the GSList referring to the state
1388 * data (the per-frame data itself was freed by
1389 * "init_dissection()"), and null out the GSList pointer. */
1390 fdata->flags.visited = 0;
1392 g_slist_free(fdata->pfd);
1397 if (!wtap_seek_read (cf->wth, fdata->file_off, &cf->pseudo_header,
1398 cf->pd, fdata->cap_len, &err, &err_info)) {
1399 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1400 cf_read_error_message(err, err_info), cf->filename);
1404 /* If the previous frame is displayed, and we haven't yet seen the
1405 selected frame, remember that frame - it's the closest one we've
1406 yet seen before the selected frame. */
1407 if (prev_row != -1 && !selected_frame_seen) {
1408 preceding_row = prev_row;
1409 preceding_frame = prev_frame;
1411 row = add_packet_to_packet_list(fdata, cf, &cf->pseudo_header, cf->pd,
1414 /* If this frame is displayed, and this is the first frame we've
1415 seen displayed after the selected frame, remember this frame -
1416 it's the closest one we've yet seen at or after the selected
1418 if (row != -1 && selected_frame_seen && following_row == -1) {
1419 following_row = row;
1420 following_frame = fdata;
1422 if (fdata == selected_frame) {
1424 selected_frame_seen = TRUE;
1427 /* Remember this row/frame - it'll be the previous row/frame
1428 on the next pass through the loop. */
1434 /* Clear out what remains of the visited flags and per-frame data
1437 XXX - that may cause various forms of bogosity when dissecting
1438 these frames, as they won't have been seen by this sequential
1439 pass, but the only alternative I see is to keep scanning them
1440 even though the user requested that the scan stop, and that
1441 would leave the user stuck with an Ethereal grinding on
1442 until it finishes. Should we just stick them with that? */
1443 for (; fdata != NULL; fdata = fdata->next) {
1444 fdata->flags.visited = 0;
1446 g_slist_free(fdata->pfd);
1452 /* We're done filtering the packets; destroy the progress bar if it
1454 if (progbar != NULL)
1455 destroy_progress_dlg(progbar);
1457 /* Unfreeze the packet list. */
1460 if (selected_row == -1) {
1461 /* The selected frame didn't pass the filter. */
1462 if (selected_frame == NULL) {
1463 /* That's because there *was* no selected frame. Make the first
1464 displayed frame the current frame. */
1467 /* Find the nearest displayed frame to the selected frame (whether
1468 it's before or after that frame) and make that the current frame.
1469 If the next and previous displayed frames are equidistant from the
1470 selected frame, choose the next one. */
1471 g_assert(following_frame == NULL ||
1472 following_frame->num >= selected_frame->num);
1473 g_assert(preceding_frame == NULL ||
1474 preceding_frame->num <= selected_frame->num);
1475 if (following_frame == NULL) {
1476 /* No frame after the selected frame passed the filter, so we
1477 have to select the last displayed frame before the selected
1479 selected_row = preceding_row;
1480 } else if (preceding_frame == NULL) {
1481 /* No frame before the selected frame passed the filter, so we
1482 have to select the first displayed frame after the selected
1484 selected_row = following_row;
1486 /* Choose the closer of the last displayed frame before the
1487 selected frame and the first displayed frame after the
1488 selected frame; in case of a tie, choose the first displayed
1489 frame after the selected frame. */
1490 if (following_frame->num - selected_frame->num <=
1491 selected_frame->num - preceding_frame->num) {
1492 selected_row = following_row;
1494 /* The previous frame is closer to the selected frame than the
1496 selected_row = preceding_row;
1502 if (selected_row == -1) {
1503 /* There are no frames displayed at all. */
1504 unselect_packet(cf);
1506 /* Either the frame that was selected passed the filter, or we've
1507 found the nearest displayed frame to that frame. Select it, make
1508 it the focus row, and make it visible. */
1509 packet_list_set_selected_row(selected_row);
1520 process_specified_packets(capture_file *cf, packet_range_t *range,
1521 const char *string1, const char *string2,
1522 gboolean (*callback)(capture_file *, frame_data *,
1523 union wtap_pseudo_header *, const guint8 *, void *),
1524 void *callback_args)
1529 union wtap_pseudo_header pseudo_header;
1530 guint8 pd[WTAP_MAX_PACKET_SIZE+1];
1531 psp_return_t ret = PSP_FINISHED;
1533 progdlg_t *progbar = NULL;
1536 gboolean progbar_stop_flag;
1537 GTimeVal progbar_start_time;
1538 gchar progbar_status_str[100];
1539 int progbar_nextstep;
1540 int progbar_quantum;
1541 range_process_e process_this;
1543 /* Update the progress bar when it gets to this value. */
1544 progbar_nextstep = 0;
1545 /* When we reach the value that triggers a progress bar update,
1546 bump that value by this amount. */
1547 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
1548 /* Count of packets at which we've looked. */
1551 progbar_stop_flag = FALSE;
1552 g_get_current_time(&progbar_start_time);
1554 packet_range_process_init(range);
1556 /* Iterate through the list of packets, printing the packets that
1557 were selected by the current display filter. */
1558 for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
1559 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
1560 when we update it, we have to run the GTK+ main loop to get it
1561 to repaint what's pending, and doing so may involve an "ioctl()"
1562 to see if there's any pending input from an X server, and doing
1563 that for every packet can be costly, especially on a big file. */
1564 if (progbar_count >= progbar_nextstep) {
1565 /* let's not divide by zero. I should never be started
1566 * with count == 0, so let's assert that
1568 g_assert(cf->count > 0);
1569 progbar_val = (gfloat) progbar_count / cf->count;
1571 if (progbar == NULL)
1572 /* Create the progress bar if necessary */
1573 progbar = delayed_create_progress_dlg(string1, string2,
1575 &progbar_start_time,
1578 if (progbar != NULL) {
1579 g_snprintf(progbar_status_str, sizeof(progbar_status_str),
1580 "%4u of %u packets", progbar_count, cf->count);
1581 update_progress_dlg(progbar, progbar_val, progbar_status_str);
1584 progbar_nextstep += progbar_quantum;
1587 if (progbar_stop_flag) {
1588 /* Well, the user decided to abort the operation. Just stop,
1589 and arrange to return TRUE to our caller, so they know it
1590 was stopped explicitly. */
1597 /* do we have to process this packet? */
1598 process_this = packet_range_process_packet(range, fdata);
1599 if (process_this == range_process_next) {
1600 /* this packet uninteresting, continue with next one */
1602 } else if (process_this == range_processing_finished) {
1603 /* all interesting packets processed, stop the loop */
1607 /* Get the packet */
1608 if (!wtap_seek_read(cf->wth, fdata->file_off, &pseudo_header,
1609 pd, fdata->cap_len, &err, &err_info)) {
1610 /* Attempt to get the packet failed. */
1611 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1612 cf_read_error_message(err, err_info), cf->filename);
1616 /* Process the packet */
1617 if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
1618 /* Callback failed. We assume it reported the error appropriately. */
1624 /* We're done printing the packets; destroy the progress bar if
1626 if (progbar != NULL)
1627 destroy_progress_dlg(progbar);
1633 retap_packet(capture_file *cf _U_, frame_data *fdata,
1634 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
1637 epan_dissect_t *edt;
1639 /* If we have tap listeners, allocate a protocol tree root node, so that
1640 we'll construct a protocol tree against which a filter expression can
1642 edt = epan_dissect_new(num_tap_filters != 0, FALSE);
1643 tap_queue_init(edt);
1644 epan_dissect_run(edt, pseudo_header, pd, fdata, NULL);
1645 tap_push_tapped_queue(edt);
1646 epan_dissect_free(edt);
1652 retap_packets(capture_file *cf)
1654 packet_range_t range;
1656 /* Reset the tap listeners. */
1657 reset_tap_listeners();
1659 /* Iterate through the list of packets, dissecting all packets and
1660 re-running the taps. */
1661 packet_range_init(&range);
1662 packet_range_process_init(&range);
1663 switch (process_specified_packets(cf, &range, "Refiltering statistics on",
1664 "all packets", retap_packet,
1667 /* Completed successfully. */
1671 /* Well, the user decided to abort the refiltering.
1672 Return FALSE so our caller knows they did that. */
1676 /* Error while retapping. */
1684 print_args_t *print_args;
1685 gboolean print_header_line;
1686 char *header_line_buf;
1687 int header_line_buf_len;
1688 gboolean print_formfeed;
1689 gboolean print_separator;
1693 } print_callback_args_t;
1696 print_packet(capture_file *cf, frame_data *fdata,
1697 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
1700 print_callback_args_t *args = argsp;
1701 epan_dissect_t *edt;
1707 gboolean proto_tree_needed;
1708 char bookmark_name[9+10+1]; /* "__frameNNNNNNNNNN__\0" */
1709 char bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0" */
1711 /* Create the protocol tree, and make it visible, if we're printing
1712 the dissection or the hex data.
1713 XXX - do we need it if we're just printing the hex data? */
1715 args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex;
1716 edt = epan_dissect_new(proto_tree_needed, proto_tree_needed);
1718 /* Fill in the column information if we're printing the summary
1720 if (args->print_args->print_summary) {
1721 epan_dissect_run(edt, pseudo_header, pd, fdata, &cf->cinfo);
1722 epan_dissect_fill_in_columns(edt);
1724 epan_dissect_run(edt, pseudo_header, pd, fdata, NULL);
1726 if (args->print_formfeed) {
1727 if (!new_page(args->print_args->stream))
1730 if (args->print_separator) {
1731 if (!print_line(args->print_args->stream, 0, ""))
1737 * We generate bookmarks, if the output format supports them.
1738 * The name is "__frameN__".
1740 sprintf(bookmark_name, "__frame%u__", fdata->num);
1742 if (args->print_args->print_summary) {
1743 if (args->print_header_line) {
1744 if (!print_line(args->print_args->stream, 0, args->header_line_buf))
1746 args->print_header_line = FALSE; /* we might not need to print any more */
1748 cp = &args->line_buf[0];
1750 for (i = 0; i < cf->cinfo.num_cols; i++) {
1751 /* Find the length of the string for this column. */
1752 column_len = strlen(cf->cinfo.col_data[i]);
1753 if (args->col_widths[i] > column_len)
1754 column_len = args->col_widths[i];
1756 /* Make sure there's room in the line buffer for the column; if not,
1757 double its length. */
1758 line_len += column_len + 1; /* "+1" for space */
1759 if (line_len > args->line_buf_len) {
1760 cp_off = cp - args->line_buf;
1761 args->line_buf_len = 2 * line_len;
1762 args->line_buf = g_realloc(args->line_buf, args->line_buf_len + 1);
1763 cp = args->line_buf + cp_off;
1766 /* Right-justify the packet number column. */
1767 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
1768 sprintf(cp, "%*s", args->col_widths[i], cf->cinfo.col_data[i]);
1770 sprintf(cp, "%-*s", args->col_widths[i], cf->cinfo.col_data[i]);
1772 if (i != cf->cinfo.num_cols - 1)
1778 * Generate a bookmark, using the summary line as the title.
1780 if (!print_bookmark(args->print_args->stream, bookmark_name,
1784 if (!print_line(args->print_args->stream, 0, args->line_buf))
1788 * Generate a bookmark, using "Frame N" as the title, as we're not
1789 * printing the summary line.
1791 sprintf(bookmark_title, "Frame %u", fdata->num);
1792 if (!print_bookmark(args->print_args->stream, bookmark_name,
1795 } /* if (print_summary) */
1797 if (args->print_args->print_dissections != print_dissections_none) {
1798 if (args->print_args->print_summary) {
1799 /* Separate the summary line from the tree with a blank line. */
1800 if (!print_line(args->print_args->stream, 0, ""))
1804 /* Print the information in that tree. */
1805 if (!proto_tree_print(args->print_args, edt, args->print_args->stream))
1808 /* Print a blank line if we print anything after this (aka more than one packet). */
1809 args->print_separator = TRUE;
1811 /* Print a header line if we print any more packet summaries */
1812 args->print_header_line = TRUE;
1815 if (args->print_args->print_hex) {
1816 /* Print the full packet data as hex. */
1817 if (!print_hex_data(args->print_args->stream, edt))
1820 /* Print a blank line if we print anything after this (aka more than one packet). */
1821 args->print_separator = TRUE;
1823 /* Print a header line if we print any more packet summaries */
1824 args->print_header_line = TRUE;
1825 } /* if (args->print_args->print_dissections != print_dissections_none) */
1827 epan_dissect_free(edt);
1829 /* do we want to have a formfeed between each packet from now on? */
1830 if(args->print_args->print_formfeed) {
1831 args->print_formfeed = TRUE;
1837 epan_dissect_free(edt);
1842 print_packets(capture_file *cf, print_args_t *print_args)
1845 print_callback_args_t callback_args;
1853 callback_args.print_args = print_args;
1854 callback_args.print_header_line = TRUE;
1855 callback_args.header_line_buf = NULL;
1856 callback_args.header_line_buf_len = 256;
1857 callback_args.print_formfeed = FALSE;
1858 callback_args.print_separator = FALSE;
1859 callback_args.line_buf = NULL;
1860 callback_args.line_buf_len = 256;
1861 callback_args.col_widths = NULL;
1863 if (!print_preamble(print_args->stream, cf->filename)) {
1864 destroy_print_stream(print_args->stream);
1865 return PP_WRITE_ERROR;
1868 if (print_args->print_summary) {
1869 /* We're printing packet summaries. Allocate the header line buffer
1870 and get the column widths. */
1871 callback_args.header_line_buf = g_malloc(callback_args.header_line_buf_len + 1);
1873 /* Find the widths for each of the columns - maximum of the
1874 width of the title and the width of the data - and construct
1875 a buffer with a line containing the column titles. */
1876 callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * cf->cinfo.num_cols);
1877 cp = &callback_args.header_line_buf[0];
1879 for (i = 0; i < cf->cinfo.num_cols; i++) {
1880 /* Don't pad the last column. */
1881 if (i == cf->cinfo.num_cols - 1)
1882 callback_args.col_widths[i] = 0;
1884 callback_args.col_widths[i] = strlen(cf->cinfo.col_title[i]);
1885 data_width = get_column_char_width(get_column_format(i));
1886 if (data_width > callback_args.col_widths[i])
1887 callback_args.col_widths[i] = data_width;
1890 /* Find the length of the string for this column. */
1891 column_len = strlen(cf->cinfo.col_title[i]);
1892 if (callback_args.col_widths[i] > column_len)
1893 column_len = callback_args.col_widths[i];
1895 /* Make sure there's room in the line buffer for the column; if not,
1896 double its length. */
1897 line_len += column_len + 1; /* "+1" for space */
1898 if (line_len > callback_args.header_line_buf_len) {
1899 cp_off = cp - callback_args.header_line_buf;
1900 callback_args.header_line_buf_len = 2 * line_len;
1901 callback_args.header_line_buf = g_realloc(callback_args.header_line_buf,
1902 callback_args.header_line_buf_len + 1);
1903 cp = callback_args.header_line_buf + cp_off;
1906 /* Right-justify the packet number column. */
1907 /* if (cf->cinfo.col_fmt[i] == COL_NUMBER)
1908 sprintf(cp, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
1910 sprintf(cp, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
1912 if (i != cf->cinfo.num_cols - 1)
1917 /* Now start out the main line buffer with the same length as the
1918 header line buffer. */
1919 callback_args.line_buf_len = callback_args.header_line_buf_len;
1920 callback_args.line_buf = g_malloc(callback_args.line_buf_len + 1);
1921 } /* if (print_summary) */
1923 /* Iterate through the list of packets, printing the packets we were
1925 ret = process_specified_packets(cf, &print_args->range, "Printing",
1926 "selected packets", print_packet,
1929 if (callback_args.header_line_buf != NULL)
1930 g_free(callback_args.header_line_buf);
1931 if (callback_args.line_buf != NULL)
1932 g_free(callback_args.line_buf);
1933 if (callback_args.col_widths != NULL)
1934 g_free(callback_args.col_widths);
1939 /* Completed successfully. */
1943 /* Well, the user decided to abort the printing.
1945 XXX - note that what got generated before they did that
1946 will get printed if we're piping to a print program; we'd
1947 have to write to a file and then hand that to the print
1948 program to make it actually not print anything. */
1952 /* Error while printing.
1954 XXX - note that what got generated before they did that
1955 will get printed if we're piping to a print program; we'd
1956 have to write to a file and then hand that to the print
1957 program to make it actually not print anything. */
1958 destroy_print_stream(print_args->stream);
1959 return PP_WRITE_ERROR;
1962 if (!print_finale(print_args->stream)) {
1963 destroy_print_stream(print_args->stream);
1964 return PP_WRITE_ERROR;
1967 if (!destroy_print_stream(print_args->stream))
1968 return PP_WRITE_ERROR;
1974 write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
1975 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
1979 epan_dissect_t *edt;
1981 /* Create the protocol tree, but don't fill in the column information. */
1982 edt = epan_dissect_new(TRUE, TRUE);
1983 epan_dissect_run(edt, pseudo_header, pd, fdata, NULL);
1985 /* Write out the information in that tree. */
1986 proto_tree_write_pdml(edt, fh);
1988 epan_dissect_free(edt);
1994 write_pdml_packets(capture_file *cf, print_args_t *print_args)
1999 fh = fopen(print_args->file, "w");
2001 return PP_OPEN_ERROR; /* attempt to open destination failed */
2003 write_pdml_preamble(fh);
2006 return PP_WRITE_ERROR;
2009 /* Iterate through the list of packets, printing the packets we were
2011 ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
2012 "selected packets", write_pdml_packet,
2018 /* Completed successfully. */
2022 /* Well, the user decided to abort the printing. */
2026 /* Error while printing. */
2028 return PP_WRITE_ERROR;
2031 write_pdml_finale(fh);
2034 return PP_WRITE_ERROR;
2037 /* XXX - check for an error */
2044 write_psml_packet(capture_file *cf, frame_data *fdata,
2045 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
2049 epan_dissect_t *edt;
2051 /* Fill in the column information, but don't create the protocol tree. */
2052 edt = epan_dissect_new(FALSE, FALSE);
2053 epan_dissect_run(edt, pseudo_header, pd, fdata, &cf->cinfo);
2055 /* Write out the information in that tree. */
2056 proto_tree_write_psml(edt, fh);
2058 epan_dissect_free(edt);
2064 write_psml_packets(capture_file *cf, print_args_t *print_args)
2069 fh = fopen(print_args->file, "w");
2071 return PP_OPEN_ERROR; /* attempt to open destination failed */
2073 write_psml_preamble(fh);
2076 return PP_WRITE_ERROR;
2079 /* Iterate through the list of packets, printing the packets we were
2081 ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
2082 "selected packets", write_psml_packet,
2088 /* Completed successfully. */
2092 /* Well, the user decided to abort the printing. */
2096 /* Error while printing. */
2098 return PP_WRITE_ERROR;
2101 write_psml_finale(fh);
2104 return PP_WRITE_ERROR;
2107 /* XXX - check for an error */
2113 /* Scan through the packet list and change all columns that use the
2114 "command-line-specified" time stamp format to use the current
2115 value of that format. */
2117 change_time_formats(capture_file *cf)
2120 progdlg_t *progbar = NULL;
2126 GTimeVal start_time;
2127 gchar status_str[100];
2128 int progbar_nextstep;
2129 int progbar_quantum;
2131 gboolean sorted_by_frame_column;
2133 /* Are there any columns with time stamps in the "command-line-specified"
2136 XXX - we have to force the "column is writable" flag on, as it
2137 might be off from the last frame that was dissected. */
2138 col_set_writable(&cf->cinfo, TRUE);
2139 if (!check_col(&cf->cinfo, COL_CLS_TIME)) {
2140 /* No, there aren't any columns in that format, so we have no work
2144 first = cf->cinfo.col_first[COL_CLS_TIME];
2145 g_assert(first >= 0);
2146 last = cf->cinfo.col_last[COL_CLS_TIME];
2148 /* Freeze the packet list while we redo it, so we don't get any
2149 screen updates while it happens. */
2150 packet_list_freeze();
2152 /* Update the progress bar when it gets to this value. */
2153 progbar_nextstep = 0;
2154 /* When we reach the value that triggers a progress bar update,
2155 bump that value by this amount. */
2156 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2157 /* Count of packets at which we've looked. */
2160 /* If the rows are currently sorted by the frame column then we know
2161 * the row number of each packet: it's the row number of the previously
2162 * displayed packet + 1.
2164 * Otherwise, if the display is sorted by a different column then we have
2165 * to use the O(N) packet_list_find_row_from_data() (thus making the job
2166 * of changing the time display format O(N**2)).
2168 * (XXX - In fact it's still O(N**2) because gtk_clist_set_text() takes
2169 * the row number and walks that many elements down the clist to find
2170 * the appropriate element.)
2172 sorted_by_frame_column = FALSE;
2173 for (i = 0; i < cf->cinfo.num_cols; i++) {
2174 if (cf->cinfo.col_fmt[i] == COL_NUMBER)
2176 sorted_by_frame_column = (i == packet_list_get_sort_column());
2182 g_get_current_time(&start_time);
2184 /* Iterate through the list of packets, checking whether the packet
2185 is in a row of the summary list and, if so, whether there are
2186 any columns that show the time in the "command-line-specified"
2187 format and, if so, update that row. */
2188 for (fdata = cf->plist, row = -1; fdata != NULL; fdata = fdata->next) {
2189 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2190 when we update it, we have to run the GTK+ main loop to get it
2191 to repaint what's pending, and doing so may involve an "ioctl()"
2192 to see if there's any pending input from an X server, and doing
2193 that for every packet can be costly, especially on a big file. */
2194 if (count >= progbar_nextstep) {
2195 /* let's not divide by zero. I should never be started
2196 * with count == 0, so let's assert that
2198 g_assert(cf->count > 0);
2200 prog_val = (gfloat) count / cf->count;
2202 if (progbar == NULL)
2203 /* Create the progress bar if necessary */
2204 progbar = delayed_create_progress_dlg("Changing", "time display",
2205 &stop_flag, &start_time, prog_val);
2207 if (progbar != NULL) {
2208 g_snprintf(status_str, sizeof(status_str),
2209 "%4u of %u packets", count, cf->count);
2210 update_progress_dlg(progbar, prog_val, status_str);
2213 progbar_nextstep += progbar_quantum;
2217 /* Well, the user decided to abort the redisplay. Just stop.
2219 XXX - this leaves the time field in the old format in
2220 frames we haven't yet processed. So it goes; should we
2221 simply not offer them the option of stopping? */
2227 /* Find what row this packet is in. */
2228 if (!sorted_by_frame_column) {
2229 /* This function is O(N), so we try to avoid using it... */
2230 row = packet_list_find_row_from_data(fdata);
2232 /* ...which we do by maintaining a count of packets that are
2233 being displayed (i.e., that have passed the display filter),
2234 and using the current value of that count as the row number
2235 (which is why we can only do it when the display is sorted
2236 by the frame number). */
2237 if (fdata->flags.passed_dfilter)
2244 /* This packet is in the summary list, on row "row". */
2246 for (i = first; i <= last; i++) {
2247 if (cf->cinfo.fmt_matx[i][COL_CLS_TIME]) {
2248 /* This is one of the columns that shows the time in
2249 "command-line-specified" format; update it. */
2250 cf->cinfo.col_buf[i][0] = '\0';
2251 col_set_cls_time(fdata, &cf->cinfo, i);
2252 packet_list_set_text(row, i, cf->cinfo.col_data[i]);
2258 /* We're done redisplaying the packets; destroy the progress bar if it
2260 if (progbar != NULL)
2261 destroy_progress_dlg(progbar);
2263 /* Set the column widths of those columns that show the time in
2264 "command-line-specified" format. */
2265 for (i = first; i <= last; i++) {
2266 if (cf->cinfo.fmt_matx[i][COL_CLS_TIME]) {
2267 packet_list_set_cls_time_width(i);
2271 /* Unfreeze the packet list. */
2279 gboolean frame_matched;
2283 find_packet_protocol_tree(capture_file *cf, const char *string)
2287 mdata.string = string;
2288 mdata.string_len = strlen(string);
2289 return find_packet(cf, match_protocol_tree, &mdata);
2293 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
2295 match_data *mdata = criterion;
2296 epan_dissect_t *edt;
2298 /* Construct the protocol tree, including the displayed text */
2299 edt = epan_dissect_new(TRUE, TRUE);
2300 /* We don't need the column information */
2301 epan_dissect_run(edt, &cf->pseudo_header, cf->pd, fdata, NULL);
2303 /* Iterate through all the nodes, seeing if they have text that matches. */
2305 mdata->frame_matched = FALSE;
2306 proto_tree_children_foreach(edt->tree, match_subtree_text, mdata);
2307 epan_dissect_free(edt);
2308 return mdata->frame_matched;
2312 match_subtree_text(proto_node *node, gpointer data)
2314 match_data *mdata = (match_data*) data;
2315 const gchar *string = mdata->string;
2316 size_t string_len = mdata->string_len;
2317 capture_file *cf = mdata->cf;
2318 field_info *fi = PITEM_FINFO(node);
2319 gchar label_str[ITEM_LABEL_LENGTH];
2326 if (mdata->frame_matched) {
2327 /* We already had a match; don't bother doing any more work. */
2331 /* Don't match invisible entries. */
2332 if (PROTO_ITEM_IS_HIDDEN(node))
2335 /* was a free format label produced? */
2337 label_ptr = fi->rep->representation;
2339 /* no, make a generic label */
2340 label_ptr = label_str;
2341 proto_item_fill_label(fi, label_str);
2344 /* Does that label match? */
2345 label_len = strlen(label_ptr);
2346 for (i = 0; i < label_len; i++) {
2347 c_char = label_ptr[i];
2349 c_char = toupper(c_char);
2350 if (c_char == string[c_match]) {
2352 if (c_match == string_len) {
2353 /* No need to look further; we have a match */
2354 mdata->frame_matched = TRUE;
2361 /* Recurse into the subtree, if it exists */
2362 if (node->first_child != NULL)
2363 proto_tree_children_foreach(node, match_subtree_text, mdata);
2367 find_packet_summary_line(capture_file *cf, const char *string)
2371 mdata.string = string;
2372 mdata.string_len = strlen(string);
2373 return find_packet(cf, match_summary_line, &mdata);
2377 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
2379 match_data *mdata = criterion;
2380 const gchar *string = mdata->string;
2381 size_t string_len = mdata->string_len;
2382 epan_dissect_t *edt;
2383 const char *info_column;
2384 size_t info_column_len;
2385 gboolean frame_matched = FALSE;
2391 /* Don't bother constructing the protocol tree */
2392 edt = epan_dissect_new(FALSE, FALSE);
2393 /* Get the column information */
2394 epan_dissect_run(edt, &cf->pseudo_header, cf->pd, fdata, &cf->cinfo);
2396 /* Find the Info column */
2397 for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
2398 if (cf->cinfo.fmt_matx[colx][COL_INFO]) {
2399 /* Found it. See if we match. */
2400 info_column = edt->pi.cinfo->col_data[colx];
2401 info_column_len = strlen(info_column);
2402 for (i = 0; i < info_column_len; i++) {
2403 c_char = info_column[i];
2405 c_char = toupper(c_char);
2406 if (c_char == string[c_match]) {
2408 if (c_match == string_len) {
2409 frame_matched = TRUE;
2418 epan_dissect_free(edt);
2419 return frame_matched;
2425 } cbs_t; /* "Counted byte string" */
2428 find_packet_data(capture_file *cf, const guint8 *string, size_t string_size)
2433 info.data_len = string_size;
2435 /* String or hex search? */
2437 /* String search - what type of string? */
2438 switch (cf->scs_type) {
2440 case SCS_ASCII_AND_UNICODE:
2441 return find_packet(cf, match_ascii_and_unicode, &info);
2444 return find_packet(cf, match_ascii, &info);
2447 return find_packet(cf, match_unicode, &info);
2450 g_assert_not_reached();
2454 return find_packet(cf, match_binary, &info);
2458 match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
2460 cbs_t *info = criterion;
2461 const char *ascii_text = info->data;
2462 size_t textlen = info->data_len;
2463 gboolean frame_matched;
2469 frame_matched = FALSE;
2470 buf_len = fdata->pkt_len;
2471 for (i = 0; i < buf_len; i++) {
2474 c_char = toupper(c_char);
2476 if (c_char == ascii_text[c_match]) {
2478 if (c_match == textlen) {
2479 frame_matched = TRUE;
2486 return frame_matched;
2490 match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
2492 cbs_t *info = criterion;
2493 const char *ascii_text = info->data;
2494 size_t textlen = info->data_len;
2495 gboolean frame_matched;
2501 frame_matched = FALSE;
2502 buf_len = fdata->pkt_len;
2503 for (i = 0; i < buf_len; i++) {
2506 c_char = toupper(c_char);
2507 if (c_char == ascii_text[c_match]) {
2509 if (c_match == textlen) {
2510 frame_matched = TRUE;
2516 return frame_matched;
2520 match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
2522 cbs_t *info = criterion;
2523 const char *ascii_text = info->data;
2524 size_t textlen = info->data_len;
2525 gboolean frame_matched;
2531 frame_matched = FALSE;
2532 buf_len = fdata->pkt_len;
2533 for (i = 0; i < buf_len; i++) {
2536 c_char = toupper(c_char);
2537 if (c_char == ascii_text[c_match]) {
2540 if (c_match == textlen) {
2541 frame_matched = TRUE;
2547 return frame_matched;
2551 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
2553 cbs_t *info = criterion;
2554 const guint8 *binary_data = info->data;
2555 size_t datalen = info->data_len;
2556 gboolean frame_matched;
2561 frame_matched = FALSE;
2562 buf_len = fdata->pkt_len;
2563 for (i = 0; i < buf_len; i++) {
2564 if (cf->pd[i] == binary_data[c_match]) {
2566 if (c_match == datalen) {
2567 frame_matched = TRUE;
2573 return frame_matched;
2577 find_packet_dfilter(capture_file *cf, dfilter_t *sfcode)
2579 return find_packet(cf, match_dfilter, sfcode);
2583 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
2585 dfilter_t *sfcode = criterion;
2586 epan_dissect_t *edt;
2587 gboolean frame_matched;
2589 edt = epan_dissect_new(TRUE, FALSE);
2590 epan_dissect_prime_dfilter(edt, sfcode);
2591 epan_dissect_run(edt, &cf->pseudo_header, cf->pd, fdata, NULL);
2592 frame_matched = dfilter_apply_edt(sfcode, edt);
2593 epan_dissect_free(edt);
2594 return frame_matched;
2598 find_packet(capture_file *cf,
2599 gboolean (*match_function)(capture_file *, frame_data *, void *),
2602 frame_data *start_fd;
2604 frame_data *new_fd = NULL;
2605 progdlg_t *progbar = NULL;
2612 GTimeVal start_time;
2613 gchar status_str[100];
2614 int progbar_nextstep;
2615 int progbar_quantum;
2617 start_fd = cf->current_frame;
2618 if (start_fd != NULL) {
2619 /* Iterate through the list of packets, starting at the packet we've
2620 picked, calling a routine to run the filter on the packet, see if
2621 it matches, and stop if so. */
2625 progbar_nextstep = 0;
2626 /* When we reach the value that triggers a progress bar update,
2627 bump that value by this amount. */
2628 progbar_quantum = cf->count/N_PROGBAR_UPDATES;
2631 g_get_current_time(&start_time);
2635 /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
2636 when we update it, we have to run the GTK+ main loop to get it
2637 to repaint what's pending, and doing so may involve an "ioctl()"
2638 to see if there's any pending input from an X server, and doing
2639 that for every packet can be costly, especially on a big file. */
2640 if (count >= progbar_nextstep) {
2641 /* let's not divide by zero. I should never be started
2642 * with count == 0, so let's assert that
2644 g_assert(cf->count > 0);
2646 prog_val = (gfloat) count / cf->count;
2648 /* Create the progress bar if necessary */
2649 if (progbar == NULL)
2650 progbar = delayed_create_progress_dlg("Searching", cf->sfilter,
2651 &stop_flag, &start_time, prog_val);
2653 if (progbar != NULL) {
2654 g_snprintf(status_str, sizeof(status_str),
2655 "%4u of %u packets", count, cf->count);
2656 update_progress_dlg(progbar, prog_val, status_str);
2659 progbar_nextstep += progbar_quantum;
2663 /* Well, the user decided to abort the search. Go back to the
2664 frame where we started. */
2669 /* Go past the current frame. */
2670 if (cf->sbackward) {
2671 /* Go on to the previous frame. */
2672 fdata = fdata->prev;
2673 if (fdata == NULL) {
2675 * XXX - other apps have a bit more of a detailed message
2676 * for this, and instead of offering "OK" and "Cancel",
2677 * they offer things such as "Continue" and "Cancel";
2678 * we need an API for popping up alert boxes with
2679 * {Verb} and "Cancel".
2682 if (prefs.gui_find_wrap)
2684 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
2685 "%sBeginning of capture exceeded!%s\n\n"
2686 "Search is continued from the end of the capture.",
2687 simple_dialog_primary_start(), simple_dialog_primary_end());
2688 fdata = cf->plist_end; /* wrap around */
2692 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
2693 "%sBeginning of capture exceeded!%s\n\n"
2694 "Try searching forwards.",
2695 simple_dialog_primary_start(), simple_dialog_primary_end());
2696 fdata = start_fd; /* stay on previous packet */
2700 /* Go on to the next frame. */
2701 fdata = fdata->next;
2702 if (fdata == NULL) {
2703 if (prefs.gui_find_wrap)
2705 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
2706 "%sEnd of capture exceeded!%s\n\n"
2707 "Search is continued from the start of the capture.",
2708 simple_dialog_primary_start(), simple_dialog_primary_end());
2709 fdata = cf->plist; /* wrap around */
2713 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
2714 "%sEnd of capture exceeded!%s\n\n"
2715 "Try searching backwards.",
2716 simple_dialog_primary_start(), simple_dialog_primary_end());
2717 fdata = start_fd; /* stay on previous packet */
2724 /* Is this packet in the display? */
2725 if (fdata->flags.passed_dfilter) {
2726 /* Yes. Load its data. */
2727 if (!wtap_seek_read(cf->wth, fdata->file_off, &cf->pseudo_header,
2728 cf->pd, fdata->cap_len, &err, &err_info)) {
2729 /* Read error. Report the error, and go back to the frame
2730 where we started. */
2731 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2732 cf_read_error_message(err, err_info), cf->filename);
2737 /* Does it match the search criterion? */
2738 if ((*match_function)(cf, fdata, criterion)) {
2740 break; /* found it! */
2744 if (fdata == start_fd) {
2745 /* We're back to the frame we were on originally, and that frame
2746 doesn't match the search filter. The search failed. */
2751 /* We're done scanning the packets; destroy the progress bar if it
2753 if (progbar != NULL)
2754 destroy_progress_dlg(progbar);
2757 if (new_fd != NULL) {
2758 /* We found a frame. Find what row it's in. */
2759 row = packet_list_find_row_from_data(new_fd);
2760 g_assert(row != -1);
2762 /* Select that row, make it the focus row, and make it visible. */
2763 packet_list_set_selected_row(row);
2764 return TRUE; /* success */
2766 return FALSE; /* failure */
2770 goto_frame(capture_file *cf, guint fnumber)
2775 for (fdata = cf->plist; fdata != NULL && fdata->num < fnumber; fdata = fdata->next)
2778 if (fdata == NULL) {
2779 /* we didn't find a packet with that packet number */
2780 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2781 "There is no packet with that packet number.");
2782 return FALSE; /* we failed to go to that packet */
2784 if (!fdata->flags.passed_dfilter) {
2785 /* that packet currently isn't displayed */
2786 /* XXX - add it to the set of displayed packets? */
2787 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2788 "That packet isn't currently being displayed.");
2789 return FALSE; /* we failed to go to that packet */
2792 /* We found that packet, and it's currently being displayed.
2793 Find what row it's in. */
2794 row = packet_list_find_row_from_data(fdata);
2795 g_assert(row != -1);
2797 /* Select that row, make it the focus row, and make it visible. */
2798 packet_list_set_selected_row(row);
2799 return TRUE; /* we got to that packet */
2803 goto_top_frame(capture_file *cf)
2807 frame_data *lowest_fdata = NULL;
2809 for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
2810 if (fdata->flags.passed_dfilter) {
2811 lowest_fdata = fdata;
2816 if (lowest_fdata == NULL) {
2820 /* We found that packet, and it's currently being displayed.
2821 Find what row it's in. */
2822 row = packet_list_find_row_from_data(lowest_fdata);
2823 g_assert(row != -1);
2825 /* Select that row, make it the focus row, and make it visible. */
2826 packet_list_set_selected_row(row);
2827 return TRUE; /* we got to that packet */
2831 goto_bottom_frame(capture_file *cf)
2835 frame_data *highest_fdata = NULL;
2837 for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
2838 if (fdata->flags.passed_dfilter) {
2839 highest_fdata = fdata;
2843 if (highest_fdata == NULL) {
2847 /* We found that packet, and it's currently being displayed.
2848 Find what row it's in. */
2849 row = packet_list_find_row_from_data(highest_fdata);
2850 g_assert(row != -1);
2852 /* Select that row, make it the focus row, and make it visible. */
2853 packet_list_set_selected_row(row);
2854 return TRUE; /* we got to that packet */
2858 * Go to frame specified by currently selected protocol tree item.
2861 goto_framenum(capture_file *cf)
2863 header_field_info *hfinfo;
2866 if (cf->finfo_selected) {
2867 hfinfo = cf->finfo_selected->hfinfo;
2869 if (hfinfo->type == FT_FRAMENUM) {
2870 framenum = fvalue_get_integer(&cf->finfo_selected->value);
2872 goto_frame(cf, framenum);
2877 /* Select the packet on a given row. */
2879 select_packet(capture_file *cf, int row)
2885 /* Get the frame data struct pointer for this frame */
2886 fdata = (frame_data *)packet_list_get_row_data(row);
2888 if (fdata == NULL) {
2889 /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
2890 the first entry is added to it by "real_insert_row()", that row
2891 is selected (see "real_insert_row()", in "gtk/gtkclist.c", in both
2892 our version and the vanilla GTK+ version).
2894 This means that a "select-row" signal is emitted; this causes
2895 "packet_list_select_cb()" to be called, which causes "select_packet()"
2898 "select_packet()" fetches, above, the data associated with the
2899 row that was selected; however, as "gtk_clist_append()", which
2900 called "real_insert_row()", hasn't yet returned, we haven't yet
2901 associated any data with that row, so we get back a null pointer.
2903 We can't assume that there's only one frame in the frame list,
2904 either, as we may be filtering the display.
2906 We therefore assume that, if "row" is 0, i.e. the first row
2907 is being selected, and "cf->first_displayed" equals
2908 "cf->last_displayed", i.e. there's only one frame being
2909 displayed, that frame is the frame we want.
2911 This means we have to set "cf->first_displayed" and
2912 "cf->last_displayed" before adding the row to the
2913 GtkCList; see the comment in "add_packet_to_packet_list()". */
2915 if (row == 0 && cf->first_displayed == cf->last_displayed)
2916 fdata = cf->first_displayed;
2919 /* Get the data in that frame. */
2920 if (!wtap_seek_read (cf->wth, fdata->file_off, &cf->pseudo_header,
2921 cf->pd, fdata->cap_len, &err, &err_info)) {
2922 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
2923 cf_read_error_message(err, err_info), cf->filename);
2927 /* Record that this frame is the current frame. */
2928 cf->current_frame = fdata;
2930 /* Create the logical protocol tree. */
2931 if (cf->edt != NULL) {
2932 epan_dissect_free(cf->edt);
2935 /* We don't need the columns here. */
2936 cf->edt = epan_dissect_new(TRUE, TRUE);
2937 epan_dissect_run(cf->edt, &cf->pseudo_header, cf->pd, cf->current_frame,
2940 /* Display the GUI protocol tree and hex dump.
2941 XXX - why do we dump core if we call "proto_tree_draw()"
2942 before calling "add_byte_views()"? */
2943 add_main_byte_views(cf->edt);
2944 main_proto_tree_draw(cf->edt->tree);
2946 /* A packet is selected. */
2947 set_menus_for_selected_packet(cf);
2950 /* Unselect the selected packet, if any. */
2952 unselect_packet(capture_file *cf)
2954 /* Destroy the epan_dissect_t for the unselected packet. */
2955 if (cf->edt != NULL) {
2956 epan_dissect_free(cf->edt);
2960 /* Clear out the display of that packet. */
2961 clear_tree_and_hex_views();
2963 /* No packet is selected. */
2964 cf->current_frame = NULL;
2965 set_menus_for_selected_packet(cf);
2967 /* No protocol tree means no selected field. */
2971 /* Unset the selected protocol tree field, if any. */
2973 unselect_field(capture_file *cf)
2975 statusbar_pop_field_msg();
2976 cf->finfo_selected = NULL;
2977 set_menus_for_selected_tree_row(cf);
2981 * Mark a particular frame.
2984 mark_frame(capture_file *cf, frame_data *frame)
2986 if (! frame->flags.marked) {
2987 frame->flags.marked = TRUE;
2988 if (cf->count > cf->marked_count)
2994 * Unmark a particular frame.
2997 unmark_frame(capture_file *cf, frame_data *frame)
2999 if (frame->flags.marked) {
3000 frame->flags.marked = FALSE;
3001 if (cf->marked_count > 0)
3009 } save_callback_args_t;
3012 * Save a capture to a file, in a particular format, saving either
3013 * all packets, all currently-displayed packets, or all marked packets.
3015 * Returns TRUE if it succeeds, FALSE otherwise; if it fails, it pops
3016 * up a message box for the failure.
3019 save_packet(capture_file *cf _U_, frame_data *fdata,
3020 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
3023 save_callback_args_t *args = argsp;
3024 struct wtap_pkthdr hdr;
3027 /* init the wtap header for saving */
3028 hdr.ts.tv_sec = fdata->abs_secs;
3029 hdr.ts.tv_usec = fdata->abs_usecs;
3030 hdr.caplen = fdata->cap_len;
3031 hdr.len = fdata->pkt_len;
3032 hdr.pkt_encap = fdata->lnk_t;
3034 /* and save the packet */
3035 if (!wtap_dump(args->pdh, &hdr, pseudo_header, pd, &err)) {
3036 cf_write_failure_alert_box(args->fname, err);
3043 cf_save(char *fname, capture_file *cf, packet_range_t *range, guint save_format)
3045 gchar *from_filename;
3046 const gchar *name_ptr;
3047 gchar *save_msg, *save_fmt = " Saving: %s...";
3052 struct stat infile, outfile;
3053 save_callback_args_t callback_args;
3055 name_ptr = get_basename(fname);
3056 msg_len = strlen(name_ptr) + strlen(save_fmt) + 2;
3057 save_msg = g_malloc(msg_len);
3058 snprintf(save_msg, msg_len, save_fmt, name_ptr);
3059 statusbar_push_file_msg(save_msg);
3063 * Check that the from file is not the same as to file
3064 * We do it here so we catch all cases ...
3065 * Unfortunately, the file requester gives us an absolute file
3066 * name and the read file name may be relative (if supplied on
3067 * the command line). From Joerg Mayer.
3069 * This is a bit tricky on win32. The st_ino field is documented as:
3070 * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
3071 * but it *is* set to zero if stat() returns without an error,
3072 * so this is working, but maybe not quite the way expected. ULFL
3074 infile.st_ino = 1; /* These prevent us from getting equality */
3075 outfile.st_ino = 2; /* If one or other of the files is not accessible */
3076 stat(cf->filename, &infile);
3077 stat(fname, &outfile);
3078 if (infile.st_ino == outfile.st_ino) {
3079 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3080 "%sCapture file: \"%s\" already exists!%s\n\n"
3081 "Please choose a different filename.",
3082 simple_dialog_primary_start(), fname, simple_dialog_primary_end());
3086 packet_range_process_init(range);
3089 * if (!save_filtered && !save_marked && !save_manual_range &&
3090 * !save_marked_range && !save_curr && save_format == cf->cd_t) {
3093 if (packet_range_process_all(range) && save_format == cf->cd_t) {
3094 /* We're not filtering packets, and we're saving it in the format
3095 it's already in, so we can just move or copy the raw data. */
3097 if (cf->is_tempfile) {
3098 /* The file being saved is a temporary file from a live
3099 capture, so it doesn't need to stay around under that name;
3100 first, try renaming the capture buffer file to the new name. */
3102 if (rename(cf->filename, fname) == 0) {
3103 /* That succeeded - there's no need to copy the source file. */
3104 from_filename = NULL;
3107 if (errno == EXDEV) {
3108 /* They're on different file systems, so we have to copy the
3111 from_filename = cf->filename;
3113 /* The rename failed, but not because they're on different
3114 file systems - put up an error message. (Or should we
3115 just punt and try to copy? The only reason why I'd
3116 expect the rename to fail and the copy to succeed would
3117 be if we didn't have permission to remove the file from
3118 the temporary directory, and that might be fixable - but
3119 is it worth requiring the user to go off and fix it?) */
3120 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3121 file_rename_error_message(errno), fname);
3127 from_filename = cf->filename;
3130 /* It's a permanent file, so we should copy it, and not remove the
3133 from_filename = cf->filename;
3137 /* Copy the file, if we haven't moved it. */
3138 if (!copy_binary_file(from_filename, fname))
3142 /* Either we're filtering packets, or we're saving in a different
3143 format; we can't do that by copying or moving the capture file,
3144 we have to do it by writing the packets out in Wiretap. */
3145 pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap, &err);
3147 cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
3151 /* XXX - we let the user save a subset of the packets.
3153 If we do that, should we make that file the current file? If so,
3154 it means we can no longer get at the other packets. What does
3157 /* Iterate through the list of packets, processing the packets we were
3160 XXX - we've already called "packet_range_process_init(range)", but
3161 "process_specified_packets()" will do it again. Fortunately,
3162 that's harmless in this case, as we haven't done anything to
3163 "range" since we initialized it. */
3164 callback_args.pdh = pdh;
3165 callback_args.fname = fname;
3166 switch (process_specified_packets(cf, range, "Saving",
3167 "selected packets", save_packet,
3171 /* Completed successfully. */
3175 /* The user decided to abort the saving.
3176 XXX - remove the output file? */
3180 /* Error while saving. */
3181 wtap_dump_close(pdh, &err);
3185 if (!wtap_dump_close(pdh, &err)) {
3186 cf_close_failure_alert_box(fname, err);
3191 /* Pop the "Saving:" message off the status bar. */
3192 statusbar_pop_file_msg();
3194 if (packet_range_process_all(range)) {
3195 /* We saved the entire capture, not just some packets from it.
3196 Open and read the file we saved it to.
3198 XXX - this is somewhat of a waste; we already have the
3199 packets, all this gets us is updated file type information
3200 (which we could just stuff into "cf"), and having the new
3201 file be the one we have opened and from which we're reading
3202 the data, and it means we have to spend time opening and
3203 reading the file, which could be a significant amount of
3204 time if the file is large. */
3205 cf->user_saved = TRUE;
3207 if ((err = cf_open(fname, FALSE, cf)) == 0) {
3208 /* XXX - report errors if this fails?
3209 What should we return if it fails or is aborted? */
3210 switch (cf_read(cf)) {
3214 /* Just because we got an error, that doesn't mean we were unable
3215 to read any of the file; we handle what we could get from the
3220 /* The user bailed out of re-reading the capture file; the
3221 capture file has been closed - just return (without
3222 changing any menu settings; "cf_close()" set them
3223 correctly for the "no capture file open" state). */
3226 set_menus_for_unsaved_capture_file(FALSE);
3232 /* Pop the "Saving:" message off the status bar. */
3233 statusbar_pop_file_msg();
3238 cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
3239 gboolean for_writing, int file_type)
3242 /* Wiretap error. */
3245 case WTAP_ERR_NOT_REGULAR_FILE:
3246 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3247 "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
3251 case WTAP_ERR_RANDOM_OPEN_PIPE:
3252 /* Seen only when opening a capture file for reading. */
3253 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3254 "The file \"%s\" is a pipe or FIFO; Ethereal can't read pipe or FIFO files.",
3258 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
3259 /* Seen only when opening a capture file for reading. */
3260 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3261 "The file \"%s\" isn't a capture file in a format Ethereal understands.",
3265 case WTAP_ERR_UNSUPPORTED:
3266 /* Seen only when opening a capture file for reading. */
3267 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3268 "The file \"%s\" isn't a capture file in a format Ethereal understands.\n"
3270 filename, err_info);
3274 case WTAP_ERR_CANT_WRITE_TO_PIPE:
3275 /* Seen only when opening a capture file for writing. */
3276 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3277 "The file \"%s\" is a pipe, and %s capture files can't be "
3278 "written to a pipe.",
3279 filename, wtap_file_type_string(file_type));
3282 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
3283 /* Seen only when opening a capture file for writing. */
3284 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3285 "Ethereal doesn't support writing capture files in that format.");
3288 case WTAP_ERR_UNSUPPORTED_ENCAP:
3290 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3291 "Ethereal can't save this capture in that format.");
3293 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3294 "The file \"%s\" is a capture for a network type that Ethereal doesn't support.\n"
3296 filename, err_info);
3301 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
3303 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3304 "Ethereal can't save this capture in that format.");
3306 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3307 "The file \"%s\" is a capture for a network type that Ethereal doesn't support.",
3312 case WTAP_ERR_BAD_RECORD:
3313 /* Seen only when opening a capture file for reading. */
3314 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3315 "The file \"%s\" appears to be damaged or corrupt.\n"
3317 filename, err_info);
3321 case WTAP_ERR_CANT_OPEN:
3323 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3324 "The file \"%s\" could not be created for some unknown reason.",
3327 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3328 "The file \"%s\" could not be opened for some unknown reason.",
3333 case WTAP_ERR_SHORT_READ:
3334 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3335 "The file \"%s\" appears to have been cut short"
3336 " in the middle of a packet or other data.",
3340 case WTAP_ERR_SHORT_WRITE:
3341 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3342 "A full header couldn't be written to the file \"%s\".",
3347 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3348 "The file \"%s\" could not be %s: %s.",
3350 for_writing ? "created" : "opened",
3351 wtap_strerror(err));
3356 open_failure_alert_box(filename, err, for_writing);
3361 file_rename_error_message(int err)
3364 static char errmsg_errno[1024+1];
3369 errmsg = "The path to the file \"%s\" doesn't exist.";
3373 errmsg = "You don't have permission to move the capture file to \"%s\".";
3377 snprintf(errmsg_errno, sizeof(errmsg_errno),
3378 "The file \"%%s\" could not be moved: %s.",
3379 wtap_strerror(err));
3380 errmsg = errmsg_errno;
3387 cf_read_error_message(int err, gchar *err_info)
3389 static char errmsg_errno[1024+1];
3393 case WTAP_ERR_UNSUPPORTED_ENCAP:
3394 snprintf(errmsg_errno, sizeof(errmsg_errno),
3395 "The file \"%%s\" has a packet with a network type that Ethereal doesn't support.\n(%s)",
3399 case WTAP_ERR_BAD_RECORD:
3400 snprintf(errmsg_errno, sizeof(errmsg_errno),
3401 "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
3402 wtap_strerror(err), err_info);
3406 snprintf(errmsg_errno, sizeof(errmsg_errno),
3407 "An error occurred while reading from the file \"%%s\": %s.",
3408 wtap_strerror(err));
3411 return errmsg_errno;
3415 cf_write_failure_alert_box(const char *filename, int err)
3418 /* Wiretap error. */
3419 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3420 "An error occurred while writing to the file \"%s\": %s.",
3421 filename, wtap_strerror(err));
3424 write_failure_alert_box(filename, err);
3428 /* Check for write errors - if the file is being written to an NFS server,
3429 a write error may not show up until the file is closed, as NFS clients
3430 might not send writes to the server until the "write()" call finishes,
3431 so that the write may fail on the server but the "write()" may succeed. */
3433 cf_close_failure_alert_box(const char *filename, int err)
3436 /* Wiretap error. */
3439 case WTAP_ERR_CANT_CLOSE:
3440 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3441 "The file \"%s\" couldn't be closed for some unknown reason.",
3445 case WTAP_ERR_SHORT_WRITE:
3446 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3447 "Not all the packets could be written to the file \"%s\".",
3452 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3453 "An error occurred while closing the file \"%s\": %s.",
3454 filename, wtap_strerror(err));
3459 We assume that a close error from the OS is really a write error. */
3460 write_failure_alert_box(filename, err);
3464 /* Reload the current capture file. */
3468 gboolean is_tempfile;
3470 /* If the file could be opened, "cf_open()" calls "cf_close()"
3471 to get rid of state for the old capture file before filling in state
3472 for the new capture file. "cf_close()" will remove the file if
3473 it's a temporary file; we don't want that to happen (for one thing,
3474 it'd prevent subsequent reopens from working). Remember whether it's
3475 a temporary file, mark it as not being a temporary file, and then
3476 reopen it as the type of file it was.
3478 Also, "cf_close()" will free "cfile.filename", so we must make
3479 a copy of it first. */
3480 filename = g_strdup(cfile.filename);
3481 is_tempfile = cfile.is_tempfile;
3482 cfile.is_tempfile = FALSE;
3483 if (cf_open(filename, is_tempfile, &cfile) == 0) {
3484 switch (cf_read(&cfile)) {
3488 /* Just because we got an error, that doesn't mean we were unable
3489 to read any of the file; we handle what we could get from the
3494 /* The user bailed out of re-reading the capture file; the
3495 capture file has been closed - just free the capture file name
3496 string and return (without changing the last containing
3502 /* The open failed, so "cfile.is_tempfile" wasn't set to "is_tempfile".
3503 Instead, the file was left open, so we should restore "cfile.is_tempfile"
3506 XXX - change the menu? Presumably "cf_open()" will do that;
3507 make sure it does! */
3508 cfile.is_tempfile = is_tempfile;
3510 /* "cf_open()" made a copy of the file name we handed it, so
3511 we should free up our copy. */
3515 /* Copies a file in binary mode, for those operating systems that care about
3517 * Returns TRUE on success, FALSE on failure. If a failure, it also
3518 * displays a simple dialog window with the error message.
3521 copy_binary_file(char *from_filename, char *to_filename)
3523 int from_fd, to_fd, nread, nwritten, err;
3526 /* Copy the raw bytes of the file. */
3527 from_fd = open(from_filename, O_RDONLY | O_BINARY);
3529 open_failure_alert_box(from_filename, errno, FALSE);
3533 /* Use open() instead of creat() so that we can pass the O_BINARY
3534 flag, which is relevant on Win32; it appears that "creat()"
3535 may open the file in text mode, not binary mode, but we want
3536 to copy the raw bytes of the file, so we need the output file
3537 to be open in binary mode. */
3538 to_fd = open(to_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
3540 open_failure_alert_box(to_filename, errno, TRUE);
3545 while ((nread = read(from_fd, pd, sizeof pd)) > 0) {
3546 nwritten = write(to_fd, pd, nread);
3547 if (nwritten < nread) {
3551 err = WTAP_ERR_SHORT_WRITE;
3552 write_failure_alert_box(to_filename, err);
3560 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
3561 "An error occurred while reading from the file \"%s\": %s.",
3562 from_filename, strerror(err));
3568 if (close(to_fd) < 0) {
3569 write_failure_alert_box(to_filename, errno);