2014-01-20 05:51:10 +00:00
|
|
|
/* tap_export_pdu.c
|
2014-07-15 20:15:27 +00:00
|
|
|
* Routines for exporting PDUs to file
|
2014-01-20 05:51:10 +00:00
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-04-30 07:47:58 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2014-01-20 05:51:10 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <epan/tap.h>
|
|
|
|
#include <epan/exported_pdu.h>
|
2014-12-19 13:08:38 +00:00
|
|
|
#include <epan/epan_dissect.h>
|
2016-04-29 13:23:28 +00:00
|
|
|
#include <wiretap/wtap.h>
|
2016-01-26 01:17:21 +00:00
|
|
|
#include <wiretap/wtap_opttypes.h>
|
2021-03-15 20:01:03 +00:00
|
|
|
#include <wsutil/os_version_info.h>
|
2021-03-15 21:03:24 +00:00
|
|
|
#include <wsutil/report_message.h>
|
2021-03-15 20:01:03 +00:00
|
|
|
|
2021-07-03 11:58:30 +00:00
|
|
|
#include "ui/version_info.h"
|
2014-01-20 05:51:10 +00:00
|
|
|
|
|
|
|
#include "tap_export_pdu.h"
|
|
|
|
|
|
|
|
/* Main entry point to the tap */
|
2019-01-01 03:36:12 +00:00
|
|
|
static tap_packet_status
|
2022-06-09 15:47:35 +00:00
|
|
|
export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const void *data, tap_flags_t flags _U_)
|
2014-01-20 05:51:10 +00:00
|
|
|
{
|
|
|
|
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;
|
2018-02-09 00:19:12 +00:00
|
|
|
wtap_rec rec;
|
2014-01-20 05:51:10 +00:00
|
|
|
int err;
|
2014-12-18 00:02:50 +00:00
|
|
|
gchar *err_info;
|
2014-01-20 05:51:10 +00:00
|
|
|
int buffer_len;
|
|
|
|
guint8 *packet_buf;
|
2021-03-15 21:03:24 +00:00
|
|
|
tap_packet_status status = TAP_PACKET_DONT_REDRAW; /* no GUI, nothing to redraw */
|
2014-01-20 05:51:10 +00:00
|
|
|
|
2021-03-15 21:52:12 +00:00
|
|
|
/*
|
|
|
|
* Count this packet.
|
|
|
|
*/
|
|
|
|
exp_pdu_tap_data->framenum++;
|
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
memset(&rec, 0, sizeof rec);
|
2014-03-18 22:01:22 +00:00
|
|
|
buffer_len = exp_pdu_data->tvb_captured_length + exp_pdu_data->tlv_buffer_len;
|
2014-01-20 05:51:10 +00:00
|
|
|
packet_buf = (guint8 *)g_malloc(buffer_len);
|
|
|
|
|
|
|
|
if(exp_pdu_data->tlv_buffer_len > 0){
|
|
|
|
memcpy(packet_buf, exp_pdu_data->tlv_buffer, exp_pdu_data->tlv_buffer_len);
|
|
|
|
}
|
2015-06-24 04:35:46 +00:00
|
|
|
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);
|
2014-01-20 05:51:10 +00:00
|
|
|
}
|
2018-02-09 00:19:12 +00:00
|
|
|
rec.rec_type = REC_TYPE_PACKET;
|
2021-04-29 21:27:49 +00:00
|
|
|
rec.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS;
|
2018-02-09 00:19:12 +00:00
|
|
|
rec.ts.secs = pinfo->abs_ts.secs;
|
|
|
|
rec.ts.nsecs = pinfo->abs_ts.nsecs;
|
|
|
|
rec.rec_header.packet_header.caplen = buffer_len;
|
|
|
|
rec.rec_header.packet_header.len = exp_pdu_data->tvb_reported_length + exp_pdu_data->tlv_buffer_len;
|
2014-01-20 05:51:10 +00:00
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
rec.rec_header.packet_header.pkt_encap = exp_pdu_tap_data->pkt_encap;
|
2014-12-19 13:08:38 +00:00
|
|
|
|
2021-04-29 11:23:21 +00:00
|
|
|
/* rec.opt_block is not modified by wtap_dump, but if for some reason the
|
2021-07-08 05:43:29 +00:00
|
|
|
* epan_get_modified_block() or pinfo->rec->block are invalidated,
|
2019-01-24 11:08:57 +00:00
|
|
|
* copying it here does not hurt. (Can invalidation really happen?) */
|
2021-07-08 05:43:29 +00:00
|
|
|
if (pinfo->fd->has_modified_block) {
|
|
|
|
rec.block = epan_get_modified_block(edt->session, pinfo->fd);
|
|
|
|
rec.block_was_modified = TRUE;
|
2021-08-30 02:12:13 +00:00
|
|
|
} else {
|
2021-04-29 11:23:21 +00:00
|
|
|
rec.block = pinfo->rec->block;
|
2017-06-01 08:34:25 +00:00
|
|
|
}
|
2014-12-19 13:08:38 +00:00
|
|
|
|
2018-02-09 00:19:12 +00:00
|
|
|
/* XXX: should the rec.rec_header.packet_header.pseudo_header be set to the pinfo's pseudo-header? */
|
|
|
|
if (!wtap_dump(exp_pdu_tap_data->wdh, &rec, packet_buf, &err, &err_info)) {
|
2021-03-15 21:52:12 +00:00
|
|
|
report_cfile_write_failure(NULL, exp_pdu_tap_data->pathname,
|
|
|
|
err, err_info, exp_pdu_tap_data->framenum,
|
2021-03-15 21:03:24 +00:00
|
|
|
wtap_dump_file_type_subtype(exp_pdu_tap_data->wdh));
|
|
|
|
status = TAP_PACKET_FAILED;
|
2014-12-18 00:02:50 +00:00
|
|
|
}
|
2014-01-20 05:51:10 +00:00
|
|
|
|
|
|
|
g_free(packet_buf);
|
|
|
|
|
2021-03-15 21:03:24 +00:00
|
|
|
return status;
|
2014-01-20 05:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 01:48:46 +00:00
|
|
|
gboolean
|
2021-03-15 21:52:12 +00:00
|
|
|
exp_pdu_open(exp_pdu_t *exp_pdu_tap_data, char *pathname,
|
|
|
|
int file_type_subtype, int fd, const char *comment,
|
|
|
|
int *err, gchar **err_info)
|
2014-01-20 05:51:10 +00:00
|
|
|
{
|
|
|
|
/* pcapng defs */
|
2016-07-14 23:01:57 +00:00
|
|
|
wtap_block_t shb_hdr;
|
|
|
|
wtap_block_t int_data;
|
2016-01-26 01:17:21 +00:00
|
|
|
wtapng_if_descr_mandatory_t *int_data_mand;
|
2014-01-20 05:51:10 +00:00
|
|
|
GString *os_info_str;
|
2016-05-20 02:28:43 +00:00
|
|
|
gsize opt_len;
|
2016-07-26 11:58:55 +00:00
|
|
|
gchar *opt_str;
|
2014-01-20 05:51:10 +00:00
|
|
|
|
2015-01-13 23:13:40 +00:00
|
|
|
/*
|
2021-02-23 09:18:31 +00:00
|
|
|
* If the file format supports a section block, and the section
|
|
|
|
* block supports comments, create data for it.
|
2015-01-13 23:13:40 +00:00
|
|
|
*/
|
2021-02-23 09:18:31 +00:00
|
|
|
if (wtap_file_type_subtype_supports_block(file_type_subtype,
|
|
|
|
WTAP_BLOCK_SECTION) != BLOCK_NOT_SUPPORTED &&
|
|
|
|
wtap_file_type_subtype_supports_option(file_type_subtype,
|
|
|
|
WTAP_BLOCK_SECTION,
|
|
|
|
OPT_COMMENT) != OPTION_NOT_SUPPORTED) {
|
|
|
|
os_info_str = g_string_new("");
|
|
|
|
get_os_version_info(os_info_str);
|
|
|
|
|
|
|
|
shb_hdr = wtap_block_create(WTAP_BLOCK_SECTION);
|
|
|
|
|
|
|
|
/* options */
|
|
|
|
wtap_block_add_string_option(shb_hdr, OPT_COMMENT, comment, strlen(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, "%s",
|
|
|
|
get_appname_and_version());
|
|
|
|
|
|
|
|
exp_pdu_tap_data->shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
|
|
|
|
g_array_append_val(exp_pdu_tap_data->shb_hdrs, shb_hdr);
|
|
|
|
} else {
|
|
|
|
exp_pdu_tap_data->shb_hdrs = NULL;
|
2016-07-26 11:58:55 +00:00
|
|
|
}
|
2021-02-23 09:18:31 +00:00
|
|
|
|
2015-01-13 23:13:40 +00:00
|
|
|
/*
|
2021-03-14 20:13:37 +00:00
|
|
|
* Create fake interface information for files that support (meaning
|
|
|
|
* "require") interface information and per-packet interface IDs.
|
2015-01-13 23:13:40 +00:00
|
|
|
*/
|
2021-02-23 09:18:31 +00:00
|
|
|
if (wtap_file_type_subtype_supports_block(file_type_subtype,
|
|
|
|
WTAP_BLOCK_IF_ID_AND_INFO) != BLOCK_NOT_SUPPORTED) {
|
|
|
|
exp_pdu_tap_data->idb_inf = g_new(wtapng_iface_descriptions_t,1);
|
|
|
|
exp_pdu_tap_data->idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
|
|
|
|
|
|
|
|
/* create the fake interface data */
|
|
|
|
int_data = wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO);
|
|
|
|
int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data);
|
2021-03-13 03:12:40 +00:00
|
|
|
int_data_mand->wtap_encap = exp_pdu_tap_data->pkt_encap;
|
2021-02-23 09:18:31 +00:00
|
|
|
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(exp_pdu_tap_data->idb_inf->interface_data, int_data);
|
|
|
|
} else {
|
|
|
|
exp_pdu_tap_data->idb_inf = NULL;
|
|
|
|
}
|
2016-05-31 03:42:41 +00:00
|
|
|
|
2018-11-14 00:10:53 +00:00
|
|
|
const wtap_dump_params params = {
|
2021-03-13 03:12:40 +00:00
|
|
|
.encap = exp_pdu_tap_data->pkt_encap,
|
2018-11-14 00:10:53 +00:00
|
|
|
.snaplen = WTAP_MAX_PACKET_SIZE_STANDARD,
|
2018-11-11 14:49:12 +00:00
|
|
|
.shb_hdrs = exp_pdu_tap_data->shb_hdrs,
|
|
|
|
.idb_inf = exp_pdu_tap_data->idb_inf,
|
|
|
|
};
|
2018-08-01 08:05:05 +00:00
|
|
|
if (fd == 1) {
|
2021-02-23 09:18:31 +00:00
|
|
|
exp_pdu_tap_data->wdh = wtap_dump_open_stdout(file_type_subtype,
|
2020-10-14 01:48:46 +00:00
|
|
|
WTAP_UNCOMPRESSED, ¶ms, err, err_info);
|
2018-08-01 08:05:05 +00:00
|
|
|
} else {
|
2021-02-23 09:18:31 +00:00
|
|
|
exp_pdu_tap_data->wdh = wtap_dump_fdopen(fd, file_type_subtype,
|
2020-10-14 01:48:46 +00:00
|
|
|
WTAP_UNCOMPRESSED, ¶ms, err, err_info);
|
2014-01-20 05:51:10 +00:00
|
|
|
}
|
2020-10-14 01:48:46 +00:00
|
|
|
if (exp_pdu_tap_data->wdh == NULL)
|
|
|
|
return FALSE;
|
2014-01-20 05:51:10 +00:00
|
|
|
|
2021-03-15 21:52:12 +00:00
|
|
|
exp_pdu_tap_data->pathname = pathname;
|
|
|
|
exp_pdu_tap_data->framenum = 0; /* No frames written yet */
|
2020-10-14 01:48:46 +00:00
|
|
|
return TRUE;
|
2016-04-29 13:23:28 +00:00
|
|
|
}
|
2014-01-20 05:51:10 +00:00
|
|
|
|
2020-10-14 01:48:46 +00:00
|
|
|
gboolean
|
|
|
|
exp_pdu_close(exp_pdu_t *exp_pdu_tap_data, int *err, gchar **err_info)
|
2016-04-29 13:23:28 +00:00
|
|
|
{
|
2020-10-14 01:48:46 +00:00
|
|
|
gboolean status;
|
|
|
|
|
2022-03-14 19:11:24 +00:00
|
|
|
status = wtap_dump_close(exp_pdu_tap_data->wdh, NULL, err, err_info);
|
2014-01-20 05:51:10 +00:00
|
|
|
|
2018-08-02 23:17:07 +00:00
|
|
|
wtap_block_array_free(exp_pdu_tap_data->shb_hdrs);
|
|
|
|
wtap_free_idb_info(exp_pdu_tap_data->idb_inf);
|
|
|
|
|
2014-01-20 05:51:10 +00:00
|
|
|
remove_tap_listener(exp_pdu_tap_data);
|
2020-10-14 01:48:46 +00:00
|
|
|
return status;
|
2014-01-20 05:51:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 13:23:28 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
exp_pdu_pre_open(const char *tap_name, const char *filter, exp_pdu_t *exp_pdu_tap_data)
|
2014-01-20 05:51:10 +00:00
|
|
|
{
|
2016-06-19 09:01:56 +00:00
|
|
|
GString *error_string;
|
|
|
|
|
2014-01-20 05:51:10 +00:00
|
|
|
/* Register this tap listener now */
|
2016-06-19 09:01:56 +00:00
|
|
|
error_string = register_tap_listener(tap_name, /* The name of the tap we want to listen to */
|
2014-01-20 05:51:10 +00:00
|
|
|
exp_pdu_tap_data, /* instance identifier/pointer to a struct holding
|
|
|
|
* all state variables */
|
|
|
|
filter, /* pointer to a filter string */
|
2016-04-29 13:23:28 +00:00
|
|
|
TL_REQUIRES_PROTO_TREE, /* flags for the tap listener */
|
2014-01-20 05:51:10 +00:00
|
|
|
NULL,
|
|
|
|
export_pdu_packet,
|
2018-07-21 00:07:19 +00:00
|
|
|
NULL,
|
2014-01-20 05:51:10 +00:00
|
|
|
NULL);
|
2016-06-19 09:01:56 +00:00
|
|
|
if (error_string != NULL)
|
|
|
|
return g_string_free(error_string, FALSE);
|
|
|
|
|
2021-08-28 00:39:07 +00:00
|
|
|
exp_pdu_tap_data->pkt_encap = export_pdu_tap_get_encap(tap_name);
|
|
|
|
|
2016-06-19 09:01:56 +00:00
|
|
|
return NULL;
|
2014-01-20 05:51:10 +00:00
|
|
|
}
|