From 03d6f39a995f49ef48614937da901febf172341f Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Sun, 14 Mar 2021 14:22:16 -0700 Subject: [PATCH] libwiretap: make wtap_wtap_encap_to_pcap_encap() private to the library. Only a tiny amount of code outside libwiretap needs to know about pcap/pcapng LINKTYPE_ values, and all that code needs to know is, for a given LINKTYPE_ value, what the corresponding WTAP_ENCAP_ value is. Nothing should need to know, for a given WTAP_ENCAP_ value, what its LINKTYPE_ value is. Make it the case that nothing *does* need to know, for a given WTAP_ENCAP_ value, what its LINKTYPE_ value is. Export wtap_dump_can_write_encap() and use *that*, in the "import hex dump" code, what formats can be written to a pcap file. --- debian/libwiretap0.symbols | 2 +- ui/qt/import_text_dialog.cpp | 14 ++++++++++++-- wiretap/file_access.c | 17 +++++++++-------- wiretap/pcap-encap.h | 2 +- wiretap/wtap.h | 11 +++++++++-- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/debian/libwiretap0.symbols b/debian/libwiretap0.symbols index 8aa5322746..eecf527fff 100644 --- a/debian/libwiretap0.symbols +++ b/debian/libwiretap0.symbols @@ -68,6 +68,7 @@ libwiretap.so.0 libwiretap0 #MINVER# wtap_dump_can_compress@Base 1.9.1 wtap_dump_can_open@Base 1.9.1 wtap_dump_can_write@Base 1.9.1 + wtap_dump_can_write_encap@Base 3.5.0 wtap_dump_close@Base 1.9.1 wtap_dump_discard_decryption_secrets@Base 3.0.0 wtap_dump_fdopen@Base 1.9.1 @@ -156,4 +157,3 @@ libwiretap.so.0 libwiretap0 #MINVER# wtap_strerror@Base 1.9.1 wtap_tsprec_string@Base 1.99.9 wtap_write_shb_comment@Base 1.9.1 - wtap_wtap_encap_to_pcap_encap@Base 1.9.1 diff --git a/ui/qt/import_text_dialog.cpp b/ui/qt/import_text_dialog.cpp index 80e301c6eb..dc2916de57 100644 --- a/ui/qt/import_text_dialog.cpp +++ b/ui/qt/import_text_dialog.cpp @@ -56,6 +56,7 @@ ImportTextDialog::ImportTextDialog(QWidget *parent) : { int encap; int i; + int pcap_file_type_subtype; ti_ui_->setupUi(this); setWindowTitle(wsApp->windowTitleString(tr("Import From Hex Dump"))); @@ -85,8 +86,16 @@ ImportTextDialog::ImportTextDialog(QWidget *parent) : if (rb) encap_buttons_.append(rb); } - /* Scan all Wiretap encapsulation types */ + /* + * Scan all Wiretap encapsulation types. + * + * XXX - this "knows" that WTAP_ENCAP_ETHERNET is the first encapsulation + * type, skipping the special non-types WTAP_ENCAP_PER_PACKET and + * WTAP_ENCAP_UNKNOWN. We need a better way to express the notion + * of "for (all encapsulation types)". + */ import_info_.encapsulation = WTAP_ENCAP_ETHERNET; + pcap_file_type_subtype = wtap_pcap_file_type_subtype(); for (encap = import_info_.encapsulation; encap < wtap_get_num_encap_types(); encap++) { /* Check if we can write to a PCAP file @@ -95,7 +104,8 @@ ImportTextDialog::ImportTextDialog(QWidget *parent) : * because we won't setup one from the text we import and * wiretap doesn't allow us to write 'raw' frames */ - if ((wtap_wtap_encap_to_pcap_encap(encap) > 0) && !wtap_encap_requires_phdr(encap)) { + if (wtap_dump_can_write_encap(pcap_file_type_subtype, encap) && + !wtap_encap_requires_phdr(encap)) { const char *name; /* If it has got a name */ if ((name = wtap_encap_description(encap))) diff --git a/wiretap/file_access.c b/wiretap/file_access.c index a6e0fdc7d3..d74c746587 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -1433,24 +1433,25 @@ wtap_dump_file_encap_type(const GArray *file_encaps) return encap; } -static gboolean -wtap_dump_can_write_encap(int filetype, int encap) +gboolean +wtap_dump_can_write_encap(int file_type_subtype, int encap) { int result = 0; - if (filetype < 0 || filetype >= (int)file_type_subtype_table_arr->len || - file_type_subtype_table[filetype].can_write_encap == NULL) + if (file_type_subtype < 0 || + file_type_subtype >= (int)file_type_subtype_table_arr->len || + file_type_subtype_table[file_type_subtype].can_write_encap == NULL) return FALSE; - result = (*file_type_subtype_table[filetype].can_write_encap)(encap); + result = (*file_type_subtype_table[file_type_subtype].can_write_encap)(encap); if (result != 0) { /* if the err said to check wslua's can_write_encap, try that */ if (result == WTAP_ERR_CHECK_WSLUA - && file_type_subtype_table[filetype].wslua_info != NULL - && file_type_subtype_table[filetype].wslua_info->wslua_can_write_encap != NULL) { + && file_type_subtype_table[file_type_subtype].wslua_info != NULL + && file_type_subtype_table[file_type_subtype].wslua_info->wslua_can_write_encap != NULL) { - result = (*file_type_subtype_table[filetype].wslua_info->wslua_can_write_encap)(encap, file_type_subtype_table[filetype].wslua_info->wslua_data); + result = (*file_type_subtype_table[file_type_subtype].wslua_info->wslua_can_write_encap)(encap, file_type_subtype_table[file_type_subtype].wslua_info->wslua_data); } diff --git a/wiretap/pcap-encap.h b/wiretap/pcap-encap.h index d72ede2928..5535e48a7d 100644 --- a/wiretap/pcap-encap.h +++ b/wiretap/pcap-encap.h @@ -21,7 +21,7 @@ extern "C" { #endif /* __cplusplus */ WS_DLL_PUBLIC int wtap_pcap_encap_to_wtap_encap(int encap); -WS_DLL_PUBLIC int wtap_wtap_encap_to_pcap_encap(int encap); +int wtap_wtap_encap_to_pcap_encap(int encap); WS_DLL_PUBLIC gboolean wtap_encap_requires_phdr(int encap); #ifdef __cplusplus diff --git a/wiretap/wtap.h b/wiretap/wtap.h index b4e85ed0f8..b500b6728d 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -1978,11 +1978,18 @@ WS_DLL_PUBLIC int wtap_dump_file_encap_type(const GArray *file_encaps); /** - * Return TRUE if we can write this capture file format out in + * Return TRUE if we can write this encapsulation type in this + * capture file type/subtype, FALSE if not. + */ +WS_DLL_PUBLIC +gboolean wtap_dump_can_write_encap(int file_type_subtype, int encap); + +/** + * Return TRUE if we can write this capture file type/subtype out in * compressed form, FALSE if not. */ WS_DLL_PUBLIC -gboolean wtap_dump_can_compress(int filetype); +gboolean wtap_dump_can_compress(int file_type_subtype); /** * Initialize the per-file information based on an existing file. Its