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.
This commit is contained in:
Guy Harris 2021-03-14 14:22:16 -07:00
parent 7dc17158db
commit 03d6f39a99
5 changed files with 32 additions and 14 deletions

View File

@ -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

View File

@ -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)))

View File

@ -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);
}

View File

@ -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

View File

@ -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