LATER... ei_kerberos_kdc_session_key ...
[wireshark-sm.git] / ui / export_pdu_ui_utils.c
blob32b91ad8cf0e56bf7bda5758f3b725aaeb0ca881
1 /*
2 * export_pdu_ui_utils.c
3 * Routines for exported_pdu dissection
4 * Copyright 2013, Anders Broman <anders-broman@ericsson.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "config.h"
15 #include "globals.h"
16 #include "wsutil/os_version_info.h"
17 #include "wsutil/tempfile.h"
18 #include "wsutil/version_info.h"
20 #include <epan/tap.h>
21 #include <epan/prefs.h>
22 #include <epan/exported_pdu.h>
23 #include <epan/epan_dissect.h>
24 #include <wiretap/wtap.h>
25 #include <wiretap/wtap_opttypes.h>
27 #include "ui/alert_box.h"
28 #include "ui/simple_dialog.h"
29 #include "tap_export_pdu.h"
30 #include "export_pdu_ui_utils.h"
32 void
33 do_export_pdu(const char *filter, const char *temp_dir, const char *tap_name)
35 exp_pdu_t exp_pdu_tap_data;
36 char *error;
37 int import_file_fd;
38 int file_type_subtype;
39 char *capfile_name = NULL, *comment;
40 bool status;
41 int err;
42 char *err_info;
44 error = exp_pdu_pre_open(tap_name, filter, &exp_pdu_tap_data);
45 if (error) {
46 /* Error. We failed to attach to the tap. Clean up */
47 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error);
48 g_free(error);
49 return;
52 /* Choose a random name for the temporary import buffer */
53 GError *err_tempfile = NULL;
54 import_file_fd = create_tempfile(temp_dir, &capfile_name, "Wireshark_PDU_", NULL, &err_tempfile);
55 if (import_file_fd < 0) {
56 failure_alert_box("Temporary file could not be created: %s", err_tempfile->message);
57 g_error_free(err_tempfile);
58 g_free(capfile_name);
59 return;
62 /* Write a pcapng file... */
63 file_type_subtype = wtap_pcapng_file_type_subtype();
64 /* ...with this comment */
65 comment = ws_strdup_printf("Dump of PDUs from %s", cfile.filename);
66 status = exp_pdu_open(&exp_pdu_tap_data, capfile_name, file_type_subtype,
67 import_file_fd, comment, &err, &err_info);
68 g_free(comment);
69 if (!status) {
70 cfile_dump_open_failure_alert_box(capfile_name ? capfile_name : "temporary file",
71 err, err_info, file_type_subtype);
72 g_free(capfile_name);
73 return;
76 /* Run the tap */
77 cf_retap_packets(&cfile);
79 if (!exp_pdu_close(&exp_pdu_tap_data, &err, &err_info)) {
80 cfile_close_failure_alert_box(capfile_name, err, err_info);
82 * XXX - remove the temporary file and don't open it as
83 * the current capture?
87 /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
88 if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, true /* temporary file */, &err) != CF_OK) {
89 /* cf_open() has put up a dialog box for the error */
90 g_free(capfile_name);
91 return;
94 switch (cf_read(&cfile, /*reloading=*/false)) {
95 case CF_READ_OK:
96 case CF_READ_ERROR:
97 /* Just because we got an error, that doesn't mean we were unable
98 to read any of the file; we handle what we could get from the
99 file. */
100 break;
102 case CF_READ_ABORTED:
103 /* The user bailed out of re-reading the capture file; the
104 capture file has been closed - just free the capture file name
105 string and return (without changing the last containing
106 directory). */
107 break;
110 g_free(capfile_name);