4e8c3ef7e4a60e0da7c699f526ac670cea3ab0c1
[metze/wireshark/wip.git] / file.h
1 /* file.h
2  * Definitions for file structures and routines
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #ifndef __FILE_H__
26 #define __FILE_H__
27
28 #include "packet-range.h"
29 #include "wiretap/wtap.h"
30 #include <epan/dfilter/dfilter.h>
31 #include "print.h"
32 #include <errno.h>
33 #include <epan/epan.h>
34
35 #include "cfile.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif /* __cplusplus */
40
41 /** Return values from functions that only can succeed or fail. */
42 typedef enum {
43     CF_OK,      /**< operation succeeded */
44     CF_ERROR    /**< operation got an error (function may provide err with details) */
45 } cf_status_t;
46
47 /** Return values from functions that read capture files. */
48 typedef enum {
49     CF_READ_OK,      /**< operation succeeded */
50     CF_READ_ERROR,   /**< operation got an error (function may provide err with details) */
51     CF_READ_ABORTED  /**< operation aborted by user */
52 } cf_read_status_t;
53
54 /** Return values from functions that write out packets. */
55 typedef enum {
56     CF_WRITE_OK,      /**< operation succeeded */
57     CF_WRITE_ERROR,   /**< operation got an error (function may provide err with details) */
58     CF_WRITE_ABORTED, /**< operation aborted by user */
59 } cf_write_status_t;
60
61 /** Return values from functions that print sets of packets. */
62 typedef enum {
63         CF_PRINT_OK,            /**< print operation succeeded */
64         CF_PRINT_OPEN_ERROR,    /**< print operation failed while opening printer */
65         CF_PRINT_WRITE_ERROR    /**< print operation failed while writing to the printer */
66 } cf_print_status_t;
67
68 typedef enum {
69     cf_cb_file_closing,
70     cf_cb_file_closed,
71     cf_cb_file_read_started,
72     cf_cb_file_read_finished,
73     cf_cb_file_reload_started,
74     cf_cb_file_reload_finished,
75     cf_cb_file_rescan_started,
76     cf_cb_file_rescan_finished,
77     cf_cb_file_fast_save_finished,
78     cf_cb_packet_selected,
79     cf_cb_packet_unselected,
80     cf_cb_field_unselected,
81     cf_cb_file_save_started,
82     cf_cb_file_save_finished,
83     cf_cb_file_save_failed,
84     cf_cb_file_save_stopped,
85     cf_cb_file_export_specified_packets_started,
86     cf_cb_file_export_specified_packets_finished,
87     cf_cb_file_export_specified_packets_failed,
88     cf_cb_file_export_specified_packets_stopped
89 } cf_cbs;
90
91 typedef void (*cf_callback_t) (gint event, gpointer data, gpointer user_data);
92
93 typedef struct {
94     const char    *string;
95     size_t         string_len;
96     capture_file  *cf;
97     gboolean       frame_matched;
98     field_info    *finfo;
99 } match_data;
100
101 extern void
102 cf_callback_add(cf_callback_t func, gpointer user_data);
103
104 extern void
105 cf_callback_remove(cf_callback_t func);
106
107 /**
108  * Open a capture file.
109  *
110  * @param cf the capture file to be opened
111  * @param fname the filename to be opened
112  * @param is_tempfile is this a temporary file?
113  * @param err error code
114  * @return one of cf_status_t
115  */
116 cf_status_t cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err);
117
118 /**
119  * Close a capture file.
120  *
121  * @param cf the capture file to be closed
122  */
123 void cf_close(capture_file *cf);
124
125 /**
126  * Reload a capture file.
127  *
128  * @param cf the capture file to be reloaded
129  */
130 void cf_reload(capture_file *cf);
131
132 /**
133  * Read all packets of a capture file into the internal structures.
134  *
135  * @param cf the capture file to be read
136  * @param from_save reread asked from cf_save_packets
137  * @return one of cf_read_status_t
138  */
139 cf_read_status_t cf_read(capture_file *cf, gboolean from_save);
140
141 /**
142  * Read the pseudo-header and raw data for a packet.  It will pop
143  * up an alert box if there's an error.
144  *
145  * @param cf the capture file from which to read the packet
146  * @param fdata the frame_data structure for the packet in question
147  * @param pseudo_header pointer to a wtap_pseudo_header union into
148  * which to read the packet's pseudo-header
149  * @param pd a guin8 array into which to read the packet's raw data
150  * @return TRUE if the read succeeded, FALSE if there was an error
151  */
152 gboolean cf_read_frame_r(capture_file *cf, frame_data *fdata,
153                          union wtap_pseudo_header *pseudo_header, guint8 *pd);
154
155 /**
156  * Read the pseudo-header and raw data for a packet into a
157  * capture_file structure's pseudo_header and pd members.
158  * It will pop up an alert box if there's an error.
159  *
160  * @param cf the capture file from which to read the packet
161  * @param fdata the frame_data structure for the packet in question
162  * @return TRUE if the read succeeded, FALSE if there was an error
163  */
164 gboolean cf_read_frame(capture_file *cf, frame_data *fdata);
165
166 /**
167  * Start reading from the end of a capture file.
168  * This is used in "Update list of packets in Real-Time".
169  *
170  * @param cf the capture file to be read from
171  * @param fname the filename to be read from
172  * @param is_tempfile is this a temporary file?
173  * @param err the error code, if an error had occured
174  * @return one of cf_status_t
175  */
176 cf_status_t cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *err);
177
178 /**
179  * Read packets from the "end" of a capture file.
180  *
181  * @param cf the capture file to be read from
182  * @param to_read the number of packets to read
183  * @param err the error code, if an error had occured
184  * @return one of cf_read_status_t
185  */
186 cf_read_status_t cf_continue_tail(capture_file *cf, volatile int to_read, int *err);
187
188 /**
189  * Fake reading packets from the "end" of a capture file.
190  *
191  * @param cf the capture file to be read from
192  */
193 void cf_fake_continue_tail(capture_file *cf);
194
195 /**
196  * Finish reading from "end" of a capture file.
197  *
198  * @param cf the capture file to be read from
199  * @param err the error code, if an error had occured
200  * @return one of cf_read_status_t
201  */
202 cf_read_status_t cf_finish_tail(capture_file *cf, int *err);
203
204 /**
205  * Determine whether this capture file (or a range of it) can be saved
206  * in any format using Wiretap rather than by copying the raw data.
207  *
208  * @param cf the capture file to check
209  * @return TRUE if it can be saved, FALSE if it can't
210  */
211 gboolean cf_can_write_with_wiretap(capture_file *cf);
212
213 /**
214  * Save all packets in a capture file to a new file, and, if that succeeds,
215  * make that file the current capture file.  If there's already a file with
216  * that name, do a "safe save", writing to a temporary file in the same
217  * directory and, if the write succeeds, renaming the new file on top of the
218  * old file, so that if the write fails, the old file is still intact.
219  *
220  * @param cf the capture file to save to
221  * @param fname the filename to save to
222  * @param save_format the format of the file to save (libpcap, ...)
223  * @param compressed whether to gzip compress the file
224  * @discard_comments TRUE if we should discard comments if the save
225  * succeeds (because we saved in a format that doesn't support
226  * comments)
227  * @param dont_reopen TRUE if it shouldn't reopen and make that file the
228  * current capture file
229  * @return one of cf_write_status_t
230  */
231 cf_write_status_t cf_save_packets(capture_file * cf, const char *fname,
232                                   guint save_format, gboolean compressed,
233                                   gboolean discard_comments,
234                                   gboolean dont_reopen);
235
236 /**
237  * Export some or all packets from a capture file to a new file.  If there's
238  * already a file with that name, do a "safe save", writing to a temporary
239  * file in the same directory and, if the write succeeds, renaming the new
240  * file on top of the old file, so that if the write fails, the old file is
241  * still intact.
242  *
243  * @param cf the capture file to write to
244  * @param fname the filename to write to
245  * @param range the range of packets to write
246  * @param save_format the format of the file to write (libpcap, ...)
247  * @param compressed whether to gzip compress the file
248  * @return one of cf_write_status_t
249  */
250 cf_write_status_t cf_export_specified_packets(capture_file *cf,
251                                               const char *fname,
252                                               packet_range_t *range,
253                                               guint save_format,
254                                               gboolean compressed);
255
256 /**
257  * Get a displayable name of the capture file.
258  *
259  * @param cf the capture file
260  * @return the displayable name (must be g_free'd)
261  */
262 gchar *cf_get_display_name(capture_file *cf);
263
264 /**
265  * Set the source of the capture data for temporary files, e.g.
266  * "Interface eth0" or "Pipe from Pong"
267  *
268  * @param cf the capture file
269  * @param source the source description. this will be copied internally.
270  */
271 void cf_set_tempfile_source(capture_file *cf, gchar *source);
272
273 /**
274  * Get the source of the capture data for temporary files. Guaranteed to
275  * return a non-null value. The returned value should not be freed.
276  *
277  * @param cf the capture file
278  */
279 const gchar *cf_get_tempfile_source(capture_file *cf);
280
281 /**
282  * Get the number of packets in the capture file.
283  *
284  * @param cf the capture file
285  * @return the number of packets in the capture file
286  */
287 int cf_get_packet_count(capture_file *cf);
288
289 /**
290  * Set the number of packets in the capture file.
291  *
292  * @param cf the capture file
293  * @param packet_count the number of packets in the capture file
294  */
295 void cf_set_packet_count(capture_file *cf, int packet_count);
296
297 /**
298  * Is this capture file a temporary file?
299  *
300  * @param cf the capture file
301  * @return TRUE if it's a temporary file, FALSE otherwise
302  */
303 gboolean cf_is_tempfile(capture_file *cf);
304
305 /**
306  * Set flag, that this file is a tempfile.
307  */
308 void cf_set_tempfile(capture_file *cf, gboolean is_tempfile);
309
310 /**
311  * Set flag, if the number of packet drops while capturing are known or not.
312  *
313  * @param cf the capture file
314  * @param drops_known TRUE if the number of packet drops are known, FALSE otherwise
315  */
316 void cf_set_drops_known(capture_file *cf, gboolean drops_known);
317
318 /**
319  * Set the number of packet drops while capturing.
320  *
321  * @param cf the capture file
322  * @param drops the number of packet drops occured while capturing
323  */
324 void cf_set_drops(capture_file *cf, guint32 drops);
325
326 /**
327  * Get flag state, if the number of packet drops while capturing are known or not.
328  *
329  * @param cf the capture file
330  * @return TRUE if the number of packet drops are known, FALSE otherwise
331  */
332 gboolean cf_get_drops_known(capture_file *cf);
333
334 /**
335  * Get the number of packet drops while capturing.
336  *
337  * @param cf the capture file
338  * @return the number of packet drops occured while capturing
339  */
340 guint32 cf_get_drops(capture_file *cf);
341
342 /**
343  * Set the read filter.
344  * @todo this shouldn't be required, remove it somehow
345  *
346  * @param cf the capture file
347  * @param rfcode the readfilter
348  */
349 void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode);
350
351 /**
352  * "Display Filter" packets in the capture file.
353  *
354  * @param cf the capture file
355  * @param dfilter the display filter
356  * @param force TRUE if do in any case, FALSE only if dfilter changed
357  * @return one of cf_status_t
358  */
359 cf_status_t cf_filter_packets(capture_file *cf, gchar *dfilter, gboolean force);
360
361 /**
362  * At least one "Refence Time" flag has changed, rescan all packets.
363  *
364  * @param cf the capture file
365  */
366 void cf_reftime_packets(capture_file *cf);
367
368 /**
369  * Return the time it took to load the file
370  */
371 gulong cf_get_computed_elapsed(void);
372
373 /**
374  * "Something" has changed, rescan all packets.
375  *
376  * @param cf the capture file
377  */
378 void cf_redissect_packets(capture_file *cf);
379
380 /**
381  * Rescan all packets and just run taps - don't reconstruct the display.
382  *
383  * @param cf the capture file
384  * @return one of cf_read_status_t
385  */
386 cf_read_status_t cf_retap_packets(capture_file *cf);
387
388 /**
389  * Adjust timestamp precision if auto is selected.
390  *
391  * @param cf the capture file
392  */
393 void cf_timestamp_auto_precision(capture_file *cf);
394
395 /**
396  * Print the capture file.
397  *
398  * @param cf the capture file
399  * @param print_args the arguments what and how to print
400  * @return one of cf_print_status_t
401  */
402 cf_print_status_t cf_print_packets(capture_file *cf, print_args_t *print_args);
403
404 /**
405  * Print (export) the capture file into PDML format.
406  *
407  * @param cf the capture file
408  * @param print_args the arguments what and how to export
409  * @return one of cf_print_status_t
410  */
411 cf_print_status_t cf_write_pdml_packets(capture_file *cf, print_args_t *print_args);
412
413 /**
414  * Print (export) the capture file into PSML format.
415  *
416  * @param cf the capture file
417  * @param print_args the arguments what and how to export
418  * @return one of cf_print_status_t
419  */
420 cf_print_status_t cf_write_psml_packets(capture_file *cf, print_args_t *print_args);
421
422 /**
423  * Print (export) the capture file into CSV format.
424  *
425  * @param cf the capture file
426  * @param print_args the arguments what and how to export
427  * @return one of cf_print_status_t
428  */
429 cf_print_status_t cf_write_csv_packets(capture_file *cf, print_args_t *print_args);
430
431 /**
432  * Print (export) the capture file into C Arrays format.
433  *
434  * @param cf the capture file
435  * @param print_args the arguments what and how to export
436  * @return one of cf_print_status_t
437  */
438 cf_print_status_t cf_write_carrays_packets(capture_file *cf, print_args_t *print_args);
439
440 /**
441  * Find packet with a protocol tree item that contains a specified text string.
442  *
443  * @param cf the capture file
444  * @param string the string to find
445  * @param dir direction in which to search
446  * @return TRUE if a packet was found, FALSE otherwise
447  */
448 gboolean cf_find_packet_protocol_tree(capture_file *cf, const char *string,
449                                       search_direction dir);
450
451 /**
452  * Find field with a label that contains text string cfile->sfilter.
453  *
454  * @param cf the capture file
455  * @param tree the protocol tree
456  * @param mdata the first field (mdata->finfo) that matched the string
457  * @return TRUE if a packet was found, FALSE otherwise
458  */
459 extern gboolean cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree,
460                                              match_data *mdata);
461
462 /**
463  * Find packet whose summary line contains a specified text string.
464  *
465  * @param cf the capture file
466  * @param string the string to find
467  * @param dir direction in which to search
468  * @return TRUE if a packet was found, FALSE otherwise
469  */
470 gboolean cf_find_packet_summary_line(capture_file *cf, const char *string,
471                                      search_direction dir);
472
473 /**
474  * Find packet whose data contains a specified byte string.
475  *
476  * @param cf the capture file
477  * @param string the string to find
478  * @param string_size the size of the string to find
479  * @param dir direction in which to search
480  * @return TRUE if a packet was found, FALSE otherwise
481  */
482 gboolean cf_find_packet_data(capture_file *cf, const guint8 *string,
483                              size_t string_size, search_direction dir);
484
485 /**
486  * Find packet that matches a compiled display filter.
487  *
488  * @param cf the capture file
489  * @param sfcode the display filter to match
490  * @param dir direction in which to search
491  * @return TRUE if a packet was found, FALSE otherwise
492  */
493 gboolean cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode,
494                                 search_direction dir);
495
496 /**
497  * Find packet that matches a display filter given as a text string.
498  *
499  * @param cf the capture file
500  * @param filter the display filter to match
501  * @param dir direction in which to search
502  * @return TRUE if a packet was found, FALSE otherwise
503  */
504 gboolean
505 cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
506                               search_direction dir);
507
508 /**
509  * Find marked packet.
510  *
511  * @param cf the capture file
512  * @param dir direction in which to search
513  * @return TRUE if a packet was found, FALSE otherwise
514  */
515 gboolean cf_find_packet_marked(capture_file *cf, search_direction dir);
516
517 /**
518  * Find time-reference packet.
519  *
520  * @param cf the capture file
521  * @param dir direction in which to search
522  * @return TRUE if a packet was found, FALSE otherwise
523  */
524 gboolean cf_find_packet_time_reference(capture_file *cf, search_direction dir);
525
526 /**
527  * GoTo Packet in first row.
528  *
529  * @return TRUE if the first row exists, FALSE otherwise
530  */
531 gboolean cf_goto_top_frame(void);
532
533 /**
534  * GoTo Packet in last row.
535  *
536  * @return TRUE if last row exists, FALSE otherwise
537  */
538 gboolean cf_goto_bottom_frame(void);
539
540 /**
541  * GoTo Packet with the given row.
542  *
543  * @param cf the capture file
544  * @param row the row to go to
545  * @return TRUE if this row exists, FALSE otherwise
546  */
547 gboolean cf_goto_frame(capture_file *cf, guint row);
548
549 /**
550  * Go to frame specified by currently selected protocol tree field.
551  * (Go To Corresponding Packet)
552  * @todo this is ugly and should be improved!
553  *
554  * @param cf the capture file
555  * @return TRUE if this packet exists, FALSE otherwise
556  */
557 gboolean cf_goto_framenum(capture_file *cf);
558
559 /**
560  * Select the packet in the given row.
561  *
562  * @param cf the capture file
563  * @param row the row to select
564  */
565 void cf_select_packet(capture_file *cf, int row);
566
567 /**
568  * Unselect all packets, if any.
569  *
570  * @param cf the capture file
571  */
572 void cf_unselect_packet(capture_file *cf);
573
574 /**
575  * Unselect all protocol tree fields, if any.
576  *
577  * @param cf the capture file
578  */
579 void cf_unselect_field(capture_file *cf);
580
581 /**
582  * Mark a particular frame in a particular capture.
583  *
584  * @param cf the capture file
585  * @param frame the frame to be marked
586  */
587 void cf_mark_frame(capture_file *cf, frame_data *frame);
588
589 /**
590  * Unmark a particular frame in a particular capture.
591  *
592  * @param cf the capture file
593  * @param frame the frame to be unmarked
594  */
595 void cf_unmark_frame(capture_file *cf, frame_data *frame);
596
597 /**
598  * Ignore a particular frame in a particular capture.
599  *
600  * @param cf the capture file
601  * @param frame the frame to be ignored
602  */
603 void cf_ignore_frame(capture_file *cf, frame_data *frame);
604
605 /**
606  * Unignore a particular frame in a particular capture.
607  *
608  * @param cf the capture file
609  * @param frame the frame to be unignored
610  */
611 void cf_unignore_frame(capture_file *cf, frame_data *frame);
612
613 /**
614  * Merge two (or more) capture files into one.
615  * @todo is this the right place for this function? It doesn't have to do a lot with capture_file.
616  *
617  * @param out_filename pointer to output filename; if output filename is
618  * NULL, a temporary file name is generated and *out_filename is set
619  * to point to the generated file name
620  * @param in_file_count the number of input files to merge
621  * @param in_filenames array of input filenames
622  * @param file_type the output filetype
623  * @param do_append FALSE to merge chronologically, TRUE simply append
624  * @return one of cf_status_t
625  */
626 cf_status_t
627 cf_merge_files(char **out_filename, int in_file_count,
628                char *const *in_filenames, int file_type, gboolean do_append);
629
630
631 /**
632  * Get the comment on a capture from the SHB data block
633  *
634  * @param cf the capture file
635  */
636 const gchar* cf_read_shb_comment(capture_file *cf);
637
638 /**
639  * Update(replace) the comment on a capture from the SHB data block
640  *
641  * @param cf the capture file
642  * @param comment the string replacing the old comment
643  */
644 void cf_update_capture_comment(capture_file *cf, gchar *comment);
645
646 /**
647  * Update(replace) the comment on a capture from a frame
648  *
649  * @param cf the capture file
650  * @param fdata the frame_data structure for the frame
651  * @param comment the string replacing the old comment
652  */
653 void cf_update_packet_comment(capture_file *cf, frame_data *fdata, gchar *comment);
654
655 /**
656  * Does this capture file have any comments?
657  *
658  * @param cf the capture file
659  * @return TRUE if it does, FALSE if it doesn't
660  */
661 gboolean cf_has_comments(capture_file *cf);
662
663 #if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
664 void read_keytab_file(const char *);
665 #endif
666
667 #ifdef __cplusplus
668 }
669 #endif /* __cplusplus */
670
671 #endif /* file.h */