#include "config.h"
-
-#include "globals.h"
-#include "pcap-encap.h"
-#include "wsutil/tempfile.h"
+#include "wiretap/pcap-encap.h"
#include "wsutil/os_version_info.h"
-#include "wsutil/ws_version_info.h"
+#include "version_info.h"
#include <epan/tap.h>
#include <epan/exported_pdu.h>
+#include <epan/epan_dissect.h>
+#include <wiretap/wtap.h>
+#include <wiretap/wtap_opttypes.h>
+#include <wiretap/pcapng.h>
-#include "ui/alert_box.h"
-#include "ui/simple_dialog.h"
#include "tap_export_pdu.h"
/* Main entry point to the tap */
-static int
-export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
+static gboolean
+export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const void *data)
{
const exp_pdu_data_t *exp_pdu_data = (const exp_pdu_data_t *)data;
exp_pdu_t *exp_pdu_tap_data = (exp_pdu_t *)tapdata;
memcpy(packet_buf, exp_pdu_data->tlv_buffer, exp_pdu_data->tlv_buffer_len);
g_free(exp_pdu_data->tlv_buffer);
}
- if(exp_pdu_data->tvb_length > 0){
- tvb_memcpy(exp_pdu_data->pdu_tvb, packet_buf+exp_pdu_data->tlv_buffer_len, 0, exp_pdu_data->tvb_length);
+ if(exp_pdu_data->tvb_captured_length > 0){
+ tvb_memcpy(exp_pdu_data->pdu_tvb, packet_buf+exp_pdu_data->tlv_buffer_len, 0, exp_pdu_data->tvb_captured_length);
}
pkthdr.rec_type = REC_TYPE_PACKET;
- pkthdr.ts.secs = pinfo->fd->abs_ts.secs;
- pkthdr.ts.nsecs = pinfo->fd->abs_ts.nsecs;
+ pkthdr.ts.secs = pinfo->abs_ts.secs;
+ pkthdr.ts.nsecs = pinfo->abs_ts.nsecs;
pkthdr.caplen = buffer_len;
pkthdr.len = exp_pdu_data->tvb_reported_length + exp_pdu_data->tlv_buffer_len;
pkthdr.pkt_encap = exp_pdu_tap_data->pkt_encap;
- pkthdr.opt_comment = g_strdup(pinfo->pkt_comment);
+
+ if (pinfo->fd->flags.has_user_comment) {
+ pkthdr.opt_comment = g_strdup(epan_get_user_comment(edt->session, pinfo->fd));
+ pkthdr.has_comment_changed = TRUE;
+ } else if (pinfo->fd->flags.has_phdr_comment) {
+ pkthdr.opt_comment = g_strdup(pinfo->phdr->opt_comment);
+ }
+
pkthdr.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS|WTAP_HAS_PACK_FLAGS;
/* XXX: should the pkthdr.pseudo_header be set to the pinfo's pseudo-header? */
return FALSE; /* Do not redraw */
}
-static void
-exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
+int
+exp_pdu_open(exp_pdu_t *exp_pdu_tap_data, int fd, char *comment)
{
- int import_file_fd;
- char *tmpname, *capfile_name;
+
int err;
/* pcapng defs */
- wtapng_section_t *shb_hdr;
+ wtap_block_t shb_hdr;
+ GArray *shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
wtapng_iface_descriptions_t *idb_inf;
- wtapng_if_descr_t int_data;
+ wtap_block_t int_data;
+ wtapng_if_descr_mandatory_t *int_data_mand;
GString *os_info_str;
- char appname[100];
-
- /* Choose a random name for the temporary import buffer */
- import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_");
- capfile_name = g_strdup(tmpname);
+ gsize opt_len;
+ gchar *opt_str;
/* Create data for SHB */
os_info_str = g_string_new("");
get_os_version_info(os_info_str);
- g_snprintf(appname, sizeof(appname), "Wireshark %s", get_ws_vcs_version_info());
+ shb_hdr = wtap_block_create(WTAP_BLOCK_NG_SECTION);
- shb_hdr = g_new(wtapng_section_t,1);
- shb_hdr->section_length = -1;
/* options */
- shb_hdr->opt_comment = g_strdup_printf("Dump of PDUs from %s", cfile.filename);
- shb_hdr->shb_hardware = NULL; /* UTF-8 string containing the
- * description of the hardware used to create this section.
- */
- shb_hdr->shb_os = os_info_str->str; /* UTF-8 string containing the name
- * of the operating system used to create this section.
- */
- g_string_free(os_info_str, FALSE); /* The actual string is not freed */
- shb_hdr->shb_user_appl = appname; /* UTF-8 string containing the name
- * of the application used to create this section.
- */
-
+ wtap_block_add_string_option(shb_hdr, OPT_COMMENT, comment, strlen(comment));
+ g_free(comment);
+
+ /*
+ * UTF-8 string containing the name of the operating system used to create
+ * this section.
+ */
+ opt_len = os_info_str->len;
+ opt_str = g_string_free(os_info_str, FALSE);
+ if (opt_str) {
+ wtap_block_add_string_option(shb_hdr, OPT_SHB_OS, opt_str, opt_len);
+ g_free(opt_str);
+ }
+ /*
+ * UTF-8 string containing the name of the application used to create
+ * this section.
+ */
+ wtap_block_add_string_option_format(shb_hdr, OPT_SHB_USERAPPL, "Wireshark %s", get_ws_vcs_version_info());
/* Create fake IDB info */
idb_inf = g_new(wtapng_iface_descriptions_t,1);
- idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
+ idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
/* create the fake interface data */
- int_data.wtap_encap = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
- int_data.time_units_per_second = 1000000; /* default microsecond resolution */
- int_data.link_type = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
- int_data.snap_len = WTAP_MAX_PACKET_SIZE;
- int_data.if_name = g_strdup("Fake IF, PDU->Export");
- int_data.opt_comment = NULL;
- int_data.if_description = NULL;
- int_data.if_speed = 0;
- int_data.if_tsresol = 6;
- int_data.if_filter_str = NULL;
- int_data.bpf_filter_len = 0;
- int_data.if_filter_bpf_bytes = NULL;
- int_data.if_os = NULL;
- int_data.if_fcslen = -1;
- int_data.num_stat_entries = 0; /* Number of ISB:s */
- int_data.interface_statistics = NULL;
+ int_data = wtap_block_create(WTAP_BLOCK_IF_DESCR);
+ int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data);
+ int_data_mand->wtap_encap = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
+ int_data_mand->time_units_per_second = 1000000000; /* default nanosecond resolution */
+ int_data_mand->snap_len = WTAP_MAX_PACKET_SIZE_STANDARD;
+
+ wtap_block_add_string_option(int_data, OPT_IDB_NAME, "Fake IF, PDU->Export", strlen("Fake IF, PDU->Export"));
+ wtap_block_add_uint8_option(int_data, OPT_IDB_TSRESOL, 9);
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, &err);
+ g_array_append_val(shb_hdrs, shb_hdr);
+
+ /* Use a random name for the temporary import buffer */
+ exp_pdu_tap_data->wdh = wtap_dump_fdopen_ng(fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE_STANDARD, FALSE,
+ shb_hdrs, idb_inf, NULL, &err);
if (exp_pdu_tap_data->wdh == NULL) {
- open_failure_alert_box(capfile_name, err, TRUE);
- goto end;
+ g_assert(err != 0);
+ return err;
}
+ return 0;
+}
- /* Run the tap */
- cf_retap_packets(&cfile);
-
-
- if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
- write_failure_alert_box(capfile_name, err);
- }
+int
+exp_pdu_close(exp_pdu_t *exp_pdu_tap_data)
+{
+ int err = 0;
+ if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err))
+ g_assert(err != 0);
remove_tap_listener(exp_pdu_tap_data);
-
- /* 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) {
- open_failure_alert_box(capfile_name, err, FALSE);
- goto end;
- }
-
- switch (cf_read(&cfile, 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;
- }
-
-end:
- g_free(capfile_name);
+ return err;
}
-gboolean
-do_export_pdu(const char *filter, gchar *tap_name, exp_pdu_t *exp_pdu_tap_data)
+
+char *
+exp_pdu_pre_open(const char *tap_name, const char *filter, exp_pdu_t *exp_pdu_tap_data)
{
GString *error_string;
+ /* XXX: can we always assume WTAP_ENCAP_WIRESHARK_UPPER_PDU? */
+ exp_pdu_tap_data->pkt_encap = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
+
/* Register this tap listener now */
error_string = register_tap_listener(tap_name, /* The name of the tap we want to listen to */
exp_pdu_tap_data, /* instance identifier/pointer to a struct holding
* all state variables */
filter, /* pointer to a filter string */
- TL_REQUIRES_NOTHING, /* flags for the tap listener */
+ TL_REQUIRES_PROTO_TREE, /* flags for the tap listener */
NULL,
export_pdu_packet,
NULL);
- if (error_string){
- /* Error. We failed to attach to the tap. Clean up */
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
- g_string_free(error_string, TRUE);
- return FALSE;
- }
+ if (error_string != NULL)
+ return g_string_free(error_string, FALSE);
- exp_pdu_file_open(exp_pdu_tap_data);
- return TRUE;
+ return NULL;
}
+
/*
* Editor modelines
*