aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ui/gtk/file_import_dlg.c17
-rw-r--r--ui/qt/import_text_dialog.cpp9
-rw-r--r--ui/tap_export_pdu.c17
-rw-r--r--wiretap/file_access.c57
-rw-r--r--wiretap/wtap.h31
5 files changed, 106 insertions, 25 deletions
diff --git a/ui/gtk/file_import_dlg.c b/ui/gtk/file_import_dlg.c
index 4700fa5b0a..958d12e221 100644
--- a/ui/gtk/file_import_dlg.c
+++ b/ui/gtk/file_import_dlg.c
@@ -452,7 +452,6 @@ header_sctp_data_rb_toggle(GtkWidget *widget, gpointer data)
static void
file_import_open(text_import_info_t *info)
{
- int import_file_fd;
char *tmpname, *capfile_name;
int err;
@@ -462,10 +461,6 @@ file_import_open(text_import_info_t *info)
wtapng_if_descr_t int_data;
GString *os_info_str;
- /* Choose a random name for the temporary import buffer */
- import_file_fd = create_tempfile(&tmpname, "import");
- capfile_name = g_strdup(tmpname);
-
/* Create data for SHB */
os_info_str = g_string_new("");
get_os_version_info(os_info_str);
@@ -515,11 +510,15 @@ file_import_open(text_import_info_t *info)
g_array_append_val(idb_inf->interface_data, int_data);
- info->wdh = wtap_dump_fdopen_ng(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG, info->encapsulation,
- info->max_frame_length, FALSE,
- shb_hdr, idb_inf, NULL, &err);
+ /* Use a random name for the temporary import buffer */
+ info->wdh = wtap_dump_open_tempfile_ng(&tmpname, "import",
+ WTAP_FILE_TYPE_SUBTYPE_PCAPNG,
+ info->encapsulation,
+ info->max_frame_length, FALSE,
+ shb_hdr, idb_inf, NULL, &err);
+ capfile_name = g_strdup(tmpname);
if (info->wdh == NULL) {
- open_failure_alert_box(capfile_name, err, TRUE);
+ open_failure_alert_box(tmpname ? tmpname : "temporary file", err, TRUE);
fclose(info->import_text_file);
goto end;
}
diff --git a/ui/qt/import_text_dialog.cpp b/ui/qt/import_text_dialog.cpp
index 1ba09cf1a0..8b4c4a2e04 100644
--- a/ui/qt/import_text_dialog.cpp
+++ b/ui/qt/import_text_dialog.cpp
@@ -115,16 +115,13 @@ QString &ImportTextDialog::capfileName() {
}
void ImportTextDialog::convertTextFile() {
- int import_file_fd;
char *tmpname;
int err;
capfile_name_.clear();
- /* Choose a random name for the temporary import buffer */
- import_file_fd = create_tempfile(&tmpname, "import");
- capfile_name_.append(tmpname);
-
- import_info_.wdh = wtap_dump_fdopen(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAP, import_info_.encapsulation, import_info_.max_frame_length, FALSE, &err);
+ /* Use a random name for the temporary import buffer */
+ import_info_.wdh = wtap_dump_open_tempfile(&tmpname, "import", WTAP_FILE_TYPE_SUBTYPE_PCAP, import_info_.encapsulation, import_info_.max_frame_length, FALSE, &err);
+ capfile_name_.append(tmpname ? tmpname : "temporary file");
qDebug() << capfile_name_ << ":" << import_info_.wdh << import_info_.encapsulation << import_info_.max_frame_length;
if (import_info_.wdh == NULL) {
open_failure_alert_box(capfile_name_.toUtf8().constData(), err, TRUE);
diff --git a/ui/tap_export_pdu.c b/ui/tap_export_pdu.c
index f1dcacc63c..b6a15e8db7 100644
--- a/ui/tap_export_pdu.c
+++ b/ui/tap_export_pdu.c
@@ -98,7 +98,6 @@ export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const
static void
exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
{
- int import_file_fd;
char *tmpname, *capfile_name;
int err;
@@ -108,10 +107,6 @@ exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
wtapng_if_descr_t int_data;
GString *os_info_str;
- /* Choose a random name for the temporary import buffer */
- import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_");
- capfile_name = g_strdup(tmpname);
-
/* Create data for SHB */
os_info_str = g_string_new("");
get_os_version_info(os_info_str);
@@ -160,15 +155,17 @@ exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
g_array_append_val(idb_inf->interface_data, int_data);
- exp_pdu_tap_data->wdh = wtap_dump_fdopen_ng(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG,
- WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE,
- FALSE, shb_hdr, idb_inf, NULL, &err);
+ /* Use a random name for the temporary import buffer */
+ exp_pdu_tap_data->wdh = wtap_dump_open_tempfile_ng(&tmpname, "Wireshark_PDU_",
+ WTAP_FILE_TYPE_SUBTYPE_PCAPNG,
+ WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE,
+ FALSE, shb_hdr, idb_inf, NULL, &err);
+ capfile_name = g_strdup(tmpname);
if (exp_pdu_tap_data->wdh == NULL) {
- open_failure_alert_box(capfile_name, err, TRUE);
+ open_failure_alert_box(capfile_name ? capfile_name : "temporary file", err, TRUE);
goto end;
}
-
/* Run the tap */
cf_retap_packets(&cfile);
diff --git a/wiretap/file_access.c b/wiretap/file_access.c
index 14c9b81b96..3ef75283bc 100644
--- a/wiretap/file_access.c
+++ b/wiretap/file_access.c
@@ -26,6 +26,7 @@
#include <errno.h>
#include <wsutil/file_util.h>
+#include <wsutil/tempfile.h>
#include "wtap-int.h"
#include "file_wrappers.h"
@@ -2240,6 +2241,62 @@ wtap_dump_open_ng(const char *filename, int file_type_subtype, int encap,
}
wtap_dumper *
+wtap_dump_open_tempfile(char **filenamep, const char *pfx,
+ int file_type_subtype, int encap,
+ int snaplen, gboolean compressed, int *err)
+{
+ return wtap_dump_open_tempfile_ng(filenamep, pfx, file_type_subtype, encap,snaplen, compressed, NULL, NULL, NULL, err);
+}
+
+wtap_dumper *
+wtap_dump_open_tempfile_ng(char **filenamep, const char *pfx,
+ int file_type_subtype, int encap,
+ int snaplen, gboolean compressed,
+ wtapng_section_t *shb_hdr,
+ wtapng_iface_descriptions_t *idb_inf,
+ wtapng_name_res_t *nrb_hdr, int *err)
+{
+ int fd;
+ char *tmpname;
+ wtap_dumper *wdh;
+ WFILE_T fh;
+
+ /* No path name for the temporary file yet. */
+ *filenamep = NULL;
+
+ /* Allocate and initialize a data structure for the output stream. */
+ wdh = wtap_dump_init_dumper(file_type_subtype, encap, snaplen, compressed,
+ shb_hdr, idb_inf, nrb_hdr, err);
+ if (wdh == NULL)
+ return NULL;
+
+ /* Choose a random name for the file */
+ fd = create_tempfile(&tmpname, pfx);
+ *filenamep = tmpname;
+
+ /* In case "fopen()" fails but doesn't set "errno", set "errno"
+ to a generic "the open failed" error. */
+ errno = WTAP_ERR_CANT_OPEN;
+ fh = wtap_dump_file_fdopen(wdh, fd);
+ if (fh == NULL) {
+ *err = errno;
+ g_free(wdh);
+ return NULL; /* can't create file */
+ }
+ wdh->fh = fh;
+
+ if (!wtap_dump_open_finish(wdh, file_type_subtype, compressed, err)) {
+ /* Get rid of the file we created; we couldn't finish
+ opening it. */
+ wtap_dump_file_close(wdh);
+ ws_unlink(tmpname);
+ g_free(wdh);
+ return NULL;
+ }
+ return wdh;
+}
+
+wtap_dumper *
wtap_dump_fdopen(int fd, int file_type_subtype, int encap, int snaplen,
gboolean compressed, int *err)
{
diff --git a/wiretap/wtap.h b/wiretap/wtap.h
index ccbc598c5e..e11c61adce 100644
--- a/wiretap/wtap.h
+++ b/wiretap/wtap.h
@@ -1941,6 +1941,37 @@ wtap_dumper* wtap_dump_open_ng(const char *filename, int file_type_subtype, int
wtapng_name_res_t *nrb_hdr, int *err);
WS_DLL_PUBLIC
+wtap_dumper* wtap_dump_open_tempfile(char **filenamep, const char *pfx,
+ int file_type_subtype, int encap, int snaplen, gboolean compressed,
+ int *err);
+
+/**
+ * @brief Creates a dumper for a temporary file.
+ *
+ * @note The shb_hdr, idb_inf, and nrb_hdr arguments will be used until
+ * wtap_dump_close() is called, but will not be free'd by the dumper. If
+ * you created them, you must free them yourself after wtap_dump_close().
+ *
+ * @param filenamep Points to a pointer that's set to point to the
+ * pathname of the temporary file; it's allocated with g_malloc()
+ * @param pfx A string to be used as the prefix for the temporary file name
+ * @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type.
+ * @param encap The WTAP_ENCAP_XXX encapsulation type (WTAP_ENCAP_PER_PACKET for multi)
+ * @param snaplen The maximum packet capture length.
+ * @param compressed True if file should be compressed.
+ * @param shb_hdr The section header block information, or NULL.
+ * @param idb_inf The interface description information, or NULL.
+ * @param nrb_hdr The name resolution comment/custom_opts information, or NULL.
+ * @param[out] err Will be set to an error code on failure.
+ * @return The newly created dumper object, or NULL on failure.
+ */
+WS_DLL_PUBLIC
+wtap_dumper* wtap_dump_open_tempfile_ng(char **filenamep, const char *pfx,
+ int file_type_subtype, int encap, int snaplen, gboolean compressed,
+ wtapng_section_t *shb_hdr, wtapng_iface_descriptions_t *idb_inf,
+ wtapng_name_res_t *nrb_hdr, int *err);
+
+WS_DLL_PUBLIC
wtap_dumper* wtap_dump_fdopen(int fd, int file_type_subtype, int encap, int snaplen,
gboolean compressed, int *err);