import_text: Check writable encap types against pcapng

Since Import from Hex Dump creates a pcapng temporary file, use
the list of encapsulations we can write to pcapng instead of pcap.

In particular, this makes WTAP_ENCAP_SYSTEMD_JOURNAL possible, so make
text_import capable of writing that encapsulation by using the proper
rec_type and block. It's not clear why someone would have a binary
hex dump of this text based format, but it works.
This commit is contained in:
John Thacker 2022-01-12 20:43:14 -05:00 committed by A Wireshark GitLab Utility
parent 816f858361
commit 3f6c273e11
2 changed files with 37 additions and 18 deletions

View File

@ -72,7 +72,7 @@ ImportTextDialog::ImportTextDialog(QWidget *parent) :
{
int encap;
int i;
int pcap_file_type_subtype;
int file_type_subtype;
ti_ui_->setupUi(this);
setWindowTitle(wsApp->windowTitleString(tr("Import From Hex Dump")));
@ -139,16 +139,16 @@ ImportTextDialog::ImportTextDialog(QWidget *parent) :
* of "for (all encapsulation types)".
*/
import_info_.encapsulation = WTAP_ENCAP_ETHERNET;
pcap_file_type_subtype = wtap_pcap_file_type_subtype();
file_type_subtype = wtap_pcapng_file_type_subtype();
for (encap = import_info_.encapsulation; encap < wtap_get_num_encap_types(); encap++)
{
/* Check if we can write to a PCAP file
/* Check if we can write to a pcapng file
*
* Exclude wtap encapsulations that require a pseudo header,
* because we won't setup one from the text we import and
* wiretap doesn't allow us to write 'raw' frames
*/
if (wtap_dump_can_write_encap(pcap_file_type_subtype, encap) &&
if (wtap_dump_can_write_encap(file_type_subtype, encap) &&
!wtap_encap_requires_phdr(encap)) {
const char *name;
/* If it has got a name */

View File

@ -699,18 +699,32 @@ write_current_packet(gboolean cont)
memset(&rec, 0, sizeof rec);
rec.rec_type = REC_TYPE_PACKET;
rec.block = wtap_block_create(WTAP_BLOCK_PACKET);
rec.ts.secs = ts_sec;
rec.ts.nsecs = ts_nsec;
rec.rec_header.packet_header.caplen = rec.rec_header.packet_header.len = prefix_length + curr_offset + eth_trailer_length;
rec.rec_header.packet_header.pkt_encap = info_p->encapsulation;
rec.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS;
if (has_direction) {
wtap_block_add_uint32_option(rec.block, OPT_PKT_FLAGS, direction);
}
if (has_seqno) {
wtap_block_add_uint64_option(rec.block, OPT_PKT_PACKETID, seqno);
if (info_p->encapsulation == WTAP_ENCAP_SYSTEMD_JOURNAL) {
rec.rec_type = REC_TYPE_SYSTEMD_JOURNAL_EXPORT;
rec.block = wtap_block_create(WTAP_BLOCK_SYSTEMD_JOURNAL_EXPORT);
rec.rec_header.systemd_journal_export_header.record_len = prefix_length + curr_offset + eth_trailer_length;
rec.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_TS;
/* XXX: Ignore our direction, packet id, and timestamp. For a
* systemd Journal Export Block the timestamp comes from the
* __REALTIME_TIMESTAMP= field. We don't check to see if that
* field is there (it MUST be, but we don't check whether our
* input is malformed in general), but since the presence flags
* aren't really used when writing, it doesn't matter.
*/
} else {
rec.rec_type = REC_TYPE_PACKET;
rec.block = wtap_block_create(WTAP_BLOCK_PACKET);
rec.rec_header.packet_header.caplen = rec.rec_header.packet_header.len = prefix_length + curr_offset + eth_trailer_length;
rec.ts.secs = ts_sec;
rec.ts.nsecs = ts_nsec;
rec.rec_header.packet_header.pkt_encap = info_p->encapsulation;
rec.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS;
if (has_direction) {
wtap_block_add_uint32_option(rec.block, OPT_PKT_FLAGS, direction);
}
if (has_seqno) {
wtap_block_add_uint64_option(rec.block, OPT_PKT_PACKETID, seqno);
}
}
if (!wtap_dump(info_p->wdh, &rec, packet_buf, &err, &err_info)) {
@ -1784,8 +1798,13 @@ text_import_pre_open(wtap_dump_params * const params, int file_type_subtype, con
g_array_append_val(params->shb_hdrs, shb_hdr);
}
/* wtap_dumper will create a dummy interface block if needed, but since
* we have the option of including the interface name, create it ourself.
/* wtap_dump_init_dumper() will create a interface block if the file type
* supports it and one isn't created already, but since we have the
* option of including the interface name, create it ourself.
*
* (XXX: IDBs should be optional for wtap_dump_init_dumper(), e.g. if
* the encap type is WTAP_ENCAP_SYSTEMD_JOURNAL, which doesn't use
* interfaces. But it's not, so always create it here.)
*/
if (wtap_file_type_subtype_supports_block(file_type_subtype, WTAP_BLOCK_IF_ID_AND_INFO) != BLOCK_NOT_SUPPORTED) {
int_data = wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO);