3 * Definitions for file structures and routines
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
15 #include <wiretap/wtap.h>
16 #include <epan/epan.h>
17 #include <epan/print.h>
18 #include <epan/fifo_string_cache.h>
19 #include <ui/packet_range.h>
23 #endif /* __cplusplus */
25 /** Return values from functions that only can succeed or fail. */
27 CF_OK
, /**< operation succeeded */
28 CF_ERROR
/**< operation got an error (function may provide err with details) */
31 /** Return values from functions that read capture files. */
33 CF_READ_OK
, /**< operation succeeded */
34 CF_READ_ERROR
, /**< operation got an error (function may provide err with details) */
35 CF_READ_ABORTED
/**< operation aborted by user */
38 /** Return values from functions that write out packets. */
40 CF_WRITE_OK
, /**< operation succeeded */
41 CF_WRITE_ERROR
, /**< operation got an error (function may provide err with details) */
42 CF_WRITE_ABORTED
/**< operation aborted by user */
45 /** Return values from functions that print sets of packets. */
47 CF_PRINT_OK
, /**< print operation succeeded */
48 CF_PRINT_OPEN_ERROR
, /**< print operation failed while opening printer */
49 CF_PRINT_WRITE_ERROR
/**< print operation failed while writing to the printer */
56 cf_cb_file_read_started
,
57 cf_cb_file_read_finished
,
58 cf_cb_file_reload_started
,
59 cf_cb_file_reload_finished
,
60 cf_cb_file_rescan_started
,
61 cf_cb_file_rescan_finished
,
62 cf_cb_file_retap_started
,
63 cf_cb_file_retap_finished
,
64 cf_cb_file_merge_started
, /* Qt only */
65 cf_cb_file_merge_finished
, /* Qt only */
66 cf_cb_file_fast_save_finished
,
67 cf_cb_file_save_started
,
68 cf_cb_file_save_finished
,
69 cf_cb_file_save_failed
,
70 cf_cb_file_save_stopped
73 typedef void (*cf_callback_t
) (int event
, void *data
, void *user_data
);
80 field_info
*prev_finfo
;
86 * Set maximum number of records per capture file.
88 * @param max_records maximum number of records to support.
91 cf_set_max_records(unsigned max_records
);
94 * Add a capture file event callback.
96 * @param func The function to be called for each event.
97 * The function will be passed three parameters: The event type (event),
98 * event-dependent data (data), and user-supplied data (user_data).
99 * Event-dependent data may be a capture_file pointer, character pointer,
101 * @param user_data User-supplied data to pass to the callback. May be NULL.
105 cf_callback_add(cf_callback_t func
, void *user_data
);
108 * Remove a capture file event callback.
110 * @param func The function to be removed.
111 * @param user_data User-supplied data. Must be the same value supplied to cf_callback_add.
115 cf_callback_remove(cf_callback_t func
, void *user_data
);
118 * Open a capture file.
120 * @param cf the capture file to be opened
121 * @param fname the filename to be opened
122 * @param type WTAP_TYPE_AUTO for automatic or index to direct open routine
123 * @param is_tempfile is this a temporary file?
124 * @param err error code
125 * @return one of cf_status_t
127 cf_status_t
cf_open(capture_file
*cf
, const char *fname
, unsigned int type
, bool is_tempfile
, int *err
);
130 * Close a capture file.
132 * @param cf the capture file to be closed
134 void cf_close(capture_file
*cf
);
137 * Reload a capture file.
139 * @param cf the capture file to be reloaded
140 * @return one of cf_status_t
142 cf_status_t
cf_reload(capture_file
*cf
);
145 * Read all packets of a capture file into the internal structures.
147 * @param cf the capture file to be read
148 * @param reloading reread asked for from cf_save_records()
149 * @return one of cf_read_status_t
151 cf_read_status_t
cf_read(capture_file
*cf
, bool reloading
);
154 * Read the information for a record. It will pop up an alert box
155 * if there's an error.
157 * @param cf the capture file from which to read the record
158 * @param fdata the frame_data structure for the record in question
159 * @param rec pointer to a wtap_rec structure to contain the
160 * record's information
161 * @return true if the read succeeded, false if there was an error
163 bool cf_read_record(capture_file
*cf
, const frame_data
*fdata
,
166 /** Same as cf_read_record() but does not pop alert box on error */
167 bool cf_read_record_no_alert(capture_file
*cf
, const frame_data
*fdata
,
172 * Read the information for the current record into a capture_file
173 * structure's rec for the current record.
174 * It will pop up an alert box if there's an error.
176 * @param cf the capture file from which to read the record
177 * @return true if the read succeeded, false if there was an error
179 bool cf_read_current_record(capture_file
*cf
);
182 * Read packets from the "end" of a capture file.
184 * @param cf the capture file to be read from
185 * @param to_read the number of packets to read
186 * @param rec pointer to wtap_rec to use when reading
187 * @param err the error code, if an error had occurred
188 * @return one of cf_read_status_t
190 cf_read_status_t
cf_continue_tail(capture_file
*cf
, volatile int to_read
,
191 wtap_rec
*rec
, int *err
,
192 fifo_string_cache_t
*frame_dup_cache
, GChecksum
*frame_cksum
);
195 * Fake reading packets from the "end" of a capture file.
197 * @param cf the capture file to be read from
199 void cf_fake_continue_tail(capture_file
*cf
);
202 * Finish reading from "end" of a capture file.
204 * @param cf the capture file to be read from
205 * @param rec pointer to wtap_rec to use when reading
206 * @param err the error code, if an error had occurred
207 * @return one of cf_read_status_t
209 cf_read_status_t
cf_finish_tail(capture_file
*cf
, wtap_rec
*rec
,
211 fifo_string_cache_t
*frame_dup_cache
, GChecksum
*frame_cksum
);
214 * Determine whether this capture file (or a range of it) can be written
215 * in any format using Wiretap rather than by copying the raw data.
217 * @param cf the capture file to check
218 * @return true if it can be written, false if it can't
220 bool cf_can_write_with_wiretap(capture_file
*cf
);
223 * Determine whether this capture file can be saved with a "save" operation;
224 * if there's nothing unsaved, it can't.
226 * @param cf the capture file to check
227 * @return true if it can be saved, false if it can't
229 bool cf_can_save(capture_file
*cf
);
232 * Determine whether this capture file can be saved with a "save as" operation.
234 * @param cf the capture file to check
235 * @return true if it can be saved, false if it can't
237 bool cf_can_save_as(capture_file
*cf
);
240 * Determine whether this capture file has unsaved data.
242 * @param cf the capture file to check
243 * @return true if it has unsaved data, false if it doesn't
245 bool cf_has_unsaved_data(capture_file
*cf
);
248 * Save all packets in a capture file to a new file, and, if that succeeds,
249 * make that file the current capture file. If there's already a file with
250 * that name, do a "safe save", writing to a temporary file in the same
251 * directory and, if the write succeeds, renaming the new file on top of the
252 * old file, so that if the write fails, the old file is still intact.
254 * @param cf the capture file to save to
255 * @param fname the filename to save to
256 * @param save_format the format of the file to save (libpcap, ...)
257 * @param compression_type type of compression to use when writing, if any
258 * @param discard_comments true if we should discard comments if the save
259 * succeeds (because we saved in a format that doesn't support
261 * @param dont_reopen true if it shouldn't reopen and make that file the
262 * current capture file
263 * @return one of cf_write_status_t
265 cf_write_status_t
cf_save_records(capture_file
* cf
, const char *fname
,
266 unsigned save_format
,
267 wtap_compression_type compression_type
,
268 bool discard_comments
,
272 * Export some or all packets from a capture file to a new file. If there's
273 * already a file with that name, do a "safe save", writing to a temporary
274 * file in the same directory and, if the write succeeds, renaming the new
275 * file on top of the old file, so that if the write fails, the old file is
278 * @param cf the capture file to write to
279 * @param fname the filename to write to
280 * @param range the range of packets to write
281 * @param save_format the format of the file to write (libpcap, ...)
282 * @param compression_type type of compression to use when writing, if any
283 * @return one of cf_write_status_t
285 cf_write_status_t
cf_export_specified_packets(capture_file
*cf
,
287 packet_range_t
*range
,
288 unsigned save_format
,
289 wtap_compression_type compression_type
);
292 * Get a displayable name of the capture file.
294 * @param cf the capture file
295 * @return the displayable name (must be g_free'd)
297 char *cf_get_display_name(capture_file
*cf
);
300 * Get a name that can be used to generate a file name from the
301 * capture file name. It's based on the displayable name, so it's
302 * UTF-8; if it ends with a suffix that's used by a file type libwiretap
303 * can read, we strip that suffix off.
305 * @param cf the capture file
306 * @return the base name (must be g_free'd)
308 char *cf_get_basename(capture_file
*cf
);
311 * Set the source of the capture data for temporary files, e.g.
312 * "Interface eth0" or "Pipe from Pong"
314 * @param cf the capture file
315 * @param source the source description. this will be copied internally.
317 void cf_set_tempfile_source(capture_file
*cf
, char *source
);
320 * Get the source of the capture data for temporary files. Guaranteed to
321 * return a non-null value. The returned value should not be freed.
323 * @param cf the capture file
325 const char *cf_get_tempfile_source(capture_file
*cf
);
328 * Get the number of packets in the capture file.
330 * @param cf the capture file
331 * @return the number of packets in the capture file
333 int cf_get_packet_count(capture_file
*cf
);
336 * Is this capture file a temporary file?
338 * @param cf the capture file
339 * @return true if it's a temporary file, false otherwise
341 bool cf_is_tempfile(capture_file
*cf
);
344 * Set flag, that this file is a tempfile.
346 void cf_set_tempfile(capture_file
*cf
, bool is_tempfile
);
349 * Set flag, if the number of packet drops while capturing are known or not.
351 * @param cf the capture file
352 * @param drops_known true if the number of packet drops are known, false otherwise
354 void cf_set_drops_known(capture_file
*cf
, bool drops_known
);
357 * Set the number of packet drops while capturing.
359 * @param cf the capture file
360 * @param drops the number of packet drops occurred while capturing
362 void cf_set_drops(capture_file
*cf
, uint32_t drops
);
365 * Get flag state, if the number of packet drops while capturing are known or not.
367 * @param cf the capture file
368 * @return true if the number of packet drops are known, false otherwise
370 bool cf_get_drops_known(capture_file
*cf
);
373 * Get the number of packet drops while capturing.
375 * @param cf the capture file
376 * @return the number of packet drops occurred while capturing
378 uint32_t cf_get_drops(capture_file
*cf
);
381 * Set the read filter.
382 * @todo this shouldn't be required, remove it somehow
384 * @param cf the capture file
385 * @param rfcode the readfilter
387 void cf_set_rfcode(capture_file
*cf
, dfilter_t
*rfcode
);
390 * "Display Filter" packets in the capture file.
392 * @param cf the capture file
393 * @param dfilter the display filter
394 * @param force true if do in any case, false only if dfilter changed
395 * @return one of cf_status_t
397 cf_status_t
cf_filter_packets(capture_file
*cf
, char *dfilter
, bool force
);
400 * Scan through all frame data and recalculate the ref time
401 * without rereading the file.
403 * @param cf the capture file
405 void cf_reftime_packets(capture_file
*cf
);
408 * Return the time it took to load the file (in msec).
410 unsigned long cf_get_computed_elapsed(capture_file
*cf
);
413 * "Something" has changed, rescan all packets.
415 * @param cf the capture file
417 void cf_redissect_packets(capture_file
*cf
);
420 * Rescan all packets and just run taps - don't reconstruct the display.
422 * @param cf the capture file
423 * @return one of cf_read_status_t
425 cf_read_status_t
cf_retap_packets(capture_file
*cf
);
427 /* print_range, enum which frames should be printed */
429 print_range_selected_only
, /* selected frame(s) only (currently only one) */
430 print_range_marked_only
, /* marked frames only */
431 print_range_all_displayed
, /* all frames currently displayed */
432 print_range_all_captured
/* all frames in capture */
436 print_stream_t
*stream
; /* the stream to which we're printing */
437 print_format_e format
; /* plain text or PostScript */
438 bool to_file
; /* true if we're printing to a file */
439 char *file
; /* file output pathname */
440 char *cmd
; /* print command string (not win32) */
441 packet_range_t range
;
443 bool print_summary
; /* true if we should print summary line. */
444 bool print_col_headings
; /* true if we should print column headings */
445 print_dissections_e print_dissections
;
446 bool print_hex
; /* true if we should print hex data;
447 * false if we should print only if not dissected. */
448 unsigned hexdump_options
; /* Hexdump options if print_hex is true. */
449 bool print_formfeed
; /* true if a formfeed should be printed before
454 * Print the capture file.
456 * @param cf the capture file
457 * @param print_args the arguments what and how to print
458 * @param show_progress_bar true if a progress bar is to be shown
459 * @return one of cf_print_status_t
461 cf_print_status_t
cf_print_packets(capture_file
*cf
, print_args_t
*print_args
,
462 bool show_progress_bar
);
465 * Print (export) the capture file into PDML format.
467 * @param cf the capture file
468 * @param print_args the arguments what and how to export
469 * @return one of cf_print_status_t
471 cf_print_status_t
cf_write_pdml_packets(capture_file
*cf
, print_args_t
*print_args
);
474 * Print (export) the capture file into PSML format.
476 * @param cf the capture file
477 * @param print_args the arguments what and how to export
478 * @return one of cf_print_status_t
480 cf_print_status_t
cf_write_psml_packets(capture_file
*cf
, print_args_t
*print_args
);
483 * Print (export) the capture file into CSV format.
485 * @param cf the capture file
486 * @param print_args the arguments what and how to export
487 * @return one of cf_print_status_t
489 cf_print_status_t
cf_write_csv_packets(capture_file
*cf
, print_args_t
*print_args
);
492 * Print (export) the capture file into C Arrays format.
494 * @param cf the capture file
495 * @param print_args the arguments what and how to export
496 * @return one of cf_print_status_t
498 cf_print_status_t
cf_write_carrays_packets(capture_file
*cf
, print_args_t
*print_args
);
501 * Print (export) the capture file into JSON format.
503 * @param cf the capture file
504 * @param print_args the arguments what and how to export
505 * @return one of cf_print_status_t
507 cf_print_status_t
cf_write_json_packets(capture_file
*cf
, print_args_t
*print_args
);
510 * Find packet with a protocol tree item that contains a specified text string.
512 * @param cf the capture file
513 * @param string the string to find
514 * @param dir direction in which to search
515 * @param multiple whether to look for the next occurrence of the same string
516 * in the current packet, or to only match once per frame
517 * @return true if a packet was found, false otherwise
519 bool cf_find_packet_protocol_tree(capture_file
*cf
, const char *string
,
520 search_direction dir
, bool multiple
);
523 * Find field with a label that contains the text string cfile->sfilter in
526 * @param cf the capture file
527 * @param tree the protocol tree
528 * @return The first field in the tree that matched the string if found, NULL otherwise
530 extern field_info
* cf_find_string_protocol_tree(capture_file
*cf
, proto_tree
*tree
);
533 * Find packet whose summary line contains a specified text string.
535 * @param cf the capture file
536 * @param string the string to find
537 * @param dir direction in which to search
538 * @return true if a packet was found, false otherwise
540 bool cf_find_packet_summary_line(capture_file
*cf
, const char *string
,
541 search_direction dir
);
544 * Find packet whose data contains a specified byte string.
546 * @param cf the capture file
547 * @param string the string to find
548 * @param string_size the size of the string to find
549 * @param dir direction in which to search
550 * @param multiple whether to look for the next occurrence of the same string
551 * in the current packet, or to only match once per frame
552 * @return true if a packet was found, false otherwise
554 bool cf_find_packet_data(capture_file
*cf
, const uint8_t *string
,
555 size_t string_size
, search_direction dir
,
559 * Find packet that matches a compiled display filter.
561 * @param cf the capture file
562 * @param sfcode the display filter to match
563 * @param dir direction in which to search
564 * @param start_current whether to start searching from the current frame
565 * @return true if a packet was found, false otherwise
567 bool cf_find_packet_dfilter(capture_file
*cf
, dfilter_t
*sfcode
,
568 search_direction dir
, bool start_current
);
571 * Find packet that matches a display filter given as a text string.
573 * @param cf the capture file
574 * @param filter the display filter to match
575 * @param dir direction in which to search
576 * @return true if a packet was found, false otherwise
579 cf_find_packet_dfilter_string(capture_file
*cf
, const char *filter
,
580 search_direction dir
);
583 * Find marked packet.
585 * @param cf the capture file
586 * @param dir direction in which to search
587 * @return true if a packet was found, false otherwise
589 bool cf_find_packet_marked(capture_file
*cf
, search_direction dir
);
592 * Find time-reference packet.
594 * @param cf the capture file
595 * @param dir direction in which to search
596 * @return true if a packet was found, false otherwise
598 bool cf_find_packet_time_reference(capture_file
*cf
, search_direction dir
);
601 * GoTo Packet with the given row.
603 * @param cf the capture file
604 * @param row the row to go to
605 * @param exact if true, fail if the row exists and is filtered (not displayed)
606 * if false, go to the nearest displayed packet instead
607 * @return true if this row exists, false otherwise
609 bool cf_goto_frame(capture_file
*cf
, unsigned row
, bool exact
);
612 * Go to frame specified by currently selected protocol tree field.
613 * (Go To Corresponding Packet)
614 * @todo this is ugly and should be improved!
616 * @param cf the capture file
617 * @return true if this packet exists, false otherwise
619 bool cf_goto_framenum(capture_file
*cf
);
622 * Select the packet in the given row.
624 * @param cf the capture file
625 * @param frame the frame to be selected
627 void cf_select_packet(capture_file
*cf
, frame_data
*frame
);
630 * Unselect all packets, if any.
632 * @param cf the capture file
634 void cf_unselect_packet(capture_file
*cf
);
637 * Mark a particular frame in a particular capture.
639 * @param cf the capture file
640 * @param frame the frame to be marked
642 void cf_mark_frame(capture_file
*cf
, frame_data
*frame
);
645 * Unmark a particular frame in a particular capture.
647 * @param cf the capture file
648 * @param frame the frame to be unmarked
650 void cf_unmark_frame(capture_file
*cf
, frame_data
*frame
);
653 * Ignore a particular frame in a particular capture.
655 * @param cf the capture file
656 * @param frame the frame to be ignored
658 void cf_ignore_frame(capture_file
*cf
, frame_data
*frame
);
661 * Unignore a particular frame in a particular capture.
663 * @param cf the capture file
664 * @param frame the frame to be unignored
666 void cf_unignore_frame(capture_file
*cf
, frame_data
*frame
);
669 * Merge two or more capture files into a temporary file.
670 * @todo is this the right place for this function? It doesn't have to do a lot with capture_file.
672 * @param pd_window Window pointer suitable for use by delayed_create_progress_dlg.
673 * @param out_filenamep Points to a pointer that's set to point to the
674 * pathname of the temporary file; it's allocated with g_malloc()
675 * @param in_file_count the number of input files to merge
676 * @param in_filenames array of input filenames
677 * @param file_type the output filetype
678 * @param do_append false to merge chronologically, true simply append
679 * @return one of cf_status_t
682 cf_merge_files_to_tempfile(void *pd_window
, const char *temp_dir
, char **out_filenamep
,
683 int in_file_count
, const char *const *in_filenames
,
684 int file_type
, bool do_append
);
687 * Update(replace) the comment on a capture from the SHB data block
688 * XXX - should support multiple sections.
690 * @param cf the capture file
691 * @param comment the string replacing the old comment
693 void cf_update_section_comment(capture_file
*cf
, char *comment
);
696 * Update(replace) the comments on a capture from the SHB data block
698 * @param cf the capture file
699 * @param shb_idx the index of the SHB (0-indexed)
700 * @param comments a NULL-terminated string array of comments. The function
701 * takes ownership of the string array and frees it and the contents.
703 void cf_update_section_comments(capture_file
*cf
, unsigned shb_idx
, char **comments
);
706 * Get the packet block for a packet (record).
707 * If the block has been edited, it returns the result of the edit,
708 * otherwise it returns the block from the file.
710 * @param cf the capture file
711 * @param fd the frame_data structure for the frame
712 * @returns A block (use wtap_block_unref to free) or NULL if there is none.
714 wtap_block_t
cf_get_packet_block(capture_file
*cf
, const frame_data
*fd
);
717 * Update(replace) the block on a capture from a frame
719 * @param cf the capture file
720 * @param fd the frame_data structure for the frame
721 * @param new_block the block replacing the old block
723 * @return true if the block is modified for the first time. false if
724 * the block was already modified before, in which case the caller is
725 * responsible for updating the comment count.
727 bool cf_set_modified_block(capture_file
*cf
, frame_data
*fd
, const wtap_block_t new_block
);
730 * What types of comments does this file have?
732 * @param cf the capture file
733 * @return bitset of WTAP_COMMENT_ values
735 uint32_t cf_comment_types(capture_file
*cf
);
738 * Add a resolved address to this file's list of resolved addresses.
740 * @param cf the capture file
741 * @param addr a string representing an IPv4 or IPv6 address
742 * @param name a string containing a name corresponding to that address
743 * @return true if it succeeds, false if not
745 bool cf_add_ip_name_from_string(capture_file
*cf
, const char *addr
, const char *name
);
749 #endif /* __cplusplus */