summaryrefslogtreecommitdiffstats
path: root/ui/export_pdu_ui_utils.c
blob: 51780dd858a966540139dd9243c97c3e77dfc4e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * export_pdu_ui_utils.c
 * Routines for exported_pdu dissection
 * Copyright 2013, Anders Broman <anders-broman@ericsson.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include "globals.h"
#include "wsutil/os_version_info.h"
#include "wsutil/tempfile.h"
#include "wsutil/version_info.h"

#include <epan/tap.h>
#include <epan/prefs.h>
#include <epan/exported_pdu.h>
#include <epan/epan_dissect.h>
#include <wiretap/wtap.h>
#include <wiretap/wtap_opttypes.h>

#include "ui/alert_box.h"
#include "ui/simple_dialog.h"
#include "tap_export_pdu.h"
#include "export_pdu_ui_utils.h"

void
do_export_pdu(const char *filter, const gchar *temp_dir, const gchar *tap_name)
{
    exp_pdu_t exp_pdu_tap_data;
    char *error;
    int   import_file_fd;
    int   file_type_subtype;
    char *capfile_name = NULL, *comment;
    gboolean status;
    int   err;
    gchar *err_info;

    error = exp_pdu_pre_open(tap_name, filter, &exp_pdu_tap_data);
    if (error) {
        /* Error.  We failed to attach to the tap. Clean up */
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error);
        g_free(error);
        return;
    }

    /* Choose a random name for the temporary import buffer */
    GError *err_tempfile = NULL;
    import_file_fd = create_tempfile(temp_dir, &capfile_name, "Wireshark_PDU_", NULL, &err_tempfile);
    if (import_file_fd < 0) {
        failure_alert_box("Temporary file could not be created: %s", err_tempfile->message);
        g_error_free(err_tempfile);
        g_free(capfile_name);
        return;
    }

    /* Write a pcapng file... */
    file_type_subtype = wtap_pcapng_file_type_subtype();
    /* ...with this comment */
    comment = ws_strdup_printf("Dump of PDUs from %s", cfile.filename);
    status = exp_pdu_open(&exp_pdu_tap_data, capfile_name, file_type_subtype,
                          import_file_fd, comment, &err, &err_info);
    g_free(comment);
    if (!status) {
        cfile_dump_open_failure_alert_box(capfile_name ? capfile_name : "temporary file",
                                          err, err_info, file_type_subtype);
        g_free(capfile_name);
        return;
    }

    /* Run the tap */
    cf_retap_packets(&cfile);

    if (!exp_pdu_close(&exp_pdu_tap_data, &err, &err_info)) {
        cfile_close_failure_alert_box(capfile_name, err, err_info);
        /*
         * XXX - remove the temporary file and don't open it as
         * the current capture?
         */
    }

    /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
    if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
        /* cf_open() has put up a dialog box for the error */
        g_free(capfile_name);
        return;
    }

    switch (cf_read(&cfile, /*reloading=*/FALSE)) {
    case CF_READ_OK:
    case CF_READ_ERROR:
        /* Just because we got an error, that doesn't mean we were unable
        to read any of the file; we handle what we could get from the
        file. */
        break;

    case CF_READ_ABORTED:
        /* The user bailed out of re-reading the capture file; the
        capture file has been closed - just free the capture file name
        string and return (without changing the last containing
        directory). */
        break;
    }

    g_free(capfile_name);
}