Add command-line arg for input file format for tshark/wireshark

Now that we have the ability to choose input file format type
in the GUI, we might as well have it in the command-line too.
Plus it would help me in test-stuies if we had a commandline.
So I've added a '-X read_format:Foo' for this.  Using just
'-X read_format:', or with a bad name, will make it print out
the full list (in tshark); just like the '-F' does for output
file formats.

Note: I am *not* putting in code for Win32 GUI,
because I can't compile that and I wouldn't have even
done the GTK one if I could compile Qt originally. (I don't think we need
to add any more features to GTK or Win32, just Qt from now on,
right?)

Change-Id: I2fe6481d186f63bd2303b9e591edf397a2e14b64
Reviewed-on: https://code.wireshark.org/review/493
Reviewed-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
Tested-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
This commit is contained in:
Hadriel Kaplan 2014-03-04 06:19:01 -05:00 committed by Alexis La Goutte
parent f1f06014c4
commit 041f844d72
7 changed files with 79 additions and 9 deletions

View File

@ -244,6 +244,28 @@ list_capture_types(void) {
g_free(captypes);
}
static void
list_read_capture_types(void) {
int i;
struct string_elem *captypes;
GSList *list = NULL;
const char *magic = "Magic-value-based";
const char *heuristic = "Heuristics-based";
/* this is a hack, but WTAP_NUM_FILE_TYPES_SUBTYPES is always >= number of open routines so we're safe */
captypes = g_new(struct string_elem, WTAP_NUM_FILE_TYPES_SUBTYPES);
fprintf(stderr, "tshark: The available read file types for the \"-X read_format:\" option are:\n");
for (i = 0; open_routines[i].name != NULL; i++) {
captypes[i].sstr = open_routines[i].name;
captypes[i].lstr = (open_routines[i].type == OPEN_INFO_MAGIC) ? magic : heuristic;
list = g_slist_insert_sorted(list, &captypes[i], string_compare);
}
g_slist_foreach(list, string_elem_print, NULL);
g_slist_free(list);
g_free(captypes);
}
static void
print_usage(gboolean print_ver)
{
@ -930,6 +952,7 @@ main(int argc, char *argv[])
volatile int out_file_type = WTAP_FILE_TYPE_SUBTYPE_PCAP;
#endif
volatile gboolean out_file_name_res = FALSE;
volatile int in_file_type = WTAP_TYPE_AUTO;
gchar *volatile cf_name = NULL;
gchar *rfilter = NULL;
gchar *dfilter = NULL;
@ -1894,6 +1917,16 @@ main(int argc, char *argv[])
}
#endif
if (ex_opt_count("read_format") > 0) {
const gchar* name = ex_opt_get_next("read_format");
in_file_type = open_info_name_to_type(name);
if (in_file_type == WTAP_TYPE_AUTO) {
cmdarg_err("\"%s\" isn't a valid read file format type", name? name : "");
list_read_capture_types();
return 1;
}
}
/* disabled protocols as per configuration file */
if (gdp_path == NULL && dp_path == NULL) {
set_disabled_protos_list();
@ -1999,7 +2032,7 @@ main(int argc, char *argv[])
relinquish_special_privs_perm();
print_current_user();
if (cf_open(&cfile, cf_name, WTAP_TYPE_AUTO, FALSE, &err) != CF_OK) {
if (cf_open(&cfile, cf_name, in_file_type, FALSE, &err) != CF_OK) {
epan_cleanup();
return 2;
}

View File

@ -2149,6 +2149,7 @@ main(int argc, char *argv[])
search_direction jump_backwards = SD_FORWARD;
dfilter_t *jump_to_filter = NULL;
int optind_initial;
unsigned int in_file_type = WTAP_TYPE_AUTO;
#ifdef HAVE_GTKOSXAPPLICATION
GtkosxApplication *theApp;
#endif
@ -3087,8 +3088,11 @@ main(int argc, char *argv[])
rfilter_parse_failed = TRUE;
}
}
if (ex_opt_count("read_format") > 0) {
in_file_type = open_info_name_to_type(ex_opt_get_next("read_format"));
}
if (!rfilter_parse_failed) {
if (cf_open(&cfile, cf_name, WTAP_TYPE_AUTO, FALSE, &err) == CF_OK) {
if (cf_open(&cfile, cf_name, in_file_type, FALSE, &err) == CF_OK) {
/* "cf_open()" succeeded, so it closed the previous
capture file, and thus destroyed any previous read filter
attached to "cf". */

View File

@ -481,6 +481,8 @@ int main(int argc, char *argv[])
QString locale;
QString *cf_name = NULL;
QString *display_filter = NULL;
unsigned int in_file_type = WTAP_TYPE_AUTO;
// In Qt 5, C strings are treated always as UTF-8 when converted to
// QStrings; in Qt 4, the codec must be set to make that happen
@ -497,8 +499,8 @@ int main(int argc, char *argv[])
main_w->show();
// We may not need a queued connection here but it would seem to make sense
// to force the issue.
main_w->connect(&ws_app, SIGNAL(openCaptureFile(QString&)),
main_w, SLOT(openCaptureFile(QString&)));
main_w->connect(&ws_app, SIGNAL(openCaptureFile(QString&,QString&,unsigned int)),
main_w, SLOT(openCaptureFile(QString&,QString&,unsigned int)));
// XXX Should the remaining code be in WiresharkApplcation::WiresharkApplication?
#ifdef HAVE_LIBPCAP
@ -872,6 +874,10 @@ int main(int argc, char *argv[])
register_all_tap_listeners();
if (ex_opt_count("read_format") > 0) {
in_file_type = open_info_name_to_type(ex_opt_get_next("read_format"));
}
splash_update(RA_PREFERENCES, NULL, NULL);
prefs_p = ws_app.readConfigurationFiles (&gdp_path, &dp_path);
@ -990,8 +996,13 @@ int main(int argc, char *argv[])
wsApp->allSystemsGo();
g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "Wireshark is up and ready to go");
if (cf_name != NULL) {
main_w->openCaptureFile(*cf_name);
/* user could specify filename, or display filter, or both */
if (cf_name != NULL || display_filter != NULL) {
if (display_filter == NULL)
display_filter = new QString();
if (cf_name == NULL)
cf_name = new QString();
main_w->openCaptureFile(*cf_name, *display_filter, in_file_type);
}
g_main_loop_new(NULL, FALSE);

View File

@ -164,7 +164,7 @@ signals:
public slots:
// in main_window_slots.cpp
void openCaptureFile(QString& cf_path = *new QString(), QString &display_filter = *new QString());
void openCaptureFile(QString& cf_path = *new QString(), QString &display_filter = *new QString(), const unsigned int type = WTAP_TYPE_AUTO);
void filterPackets(QString& new_filter = *new QString(), bool force = false);
void updateForUnsavedChanges();
void layoutPanes();

View File

@ -95,12 +95,11 @@
const char *dfe_property_ = "display filter expression"; //TODO : Fix Translate
void MainWindow::openCaptureFile(QString &cf_path, QString &display_filter)
void MainWindow::openCaptureFile(QString &cf_path, QString &display_filter, unsigned int type)
{
QString file_name = "";
dfilter_t *rfcode = NULL;
int err;
unsigned int type = WTAP_TYPE_AUTO;
testCaptureFileClose(false);

View File

@ -412,6 +412,27 @@ void wtap_register_open_info(const struct open_info *oi) {
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
/* returns the 'type' number to use for wtap_open_offline based on the
passed-in name (the name in the open_info struct). It returns WTAP_TYPE_AUTO
on failure, which is the number 0. The 'type' number is the entry's index+1,
because that's what wtap_open_offline() expects it to be. */
unsigned int open_info_name_to_type(const char *name)
{
unsigned int i;
init_open_routines();
if (!name)
return WTAP_TYPE_AUTO;
for (i = 0; i < open_info_arr->len - 1; i++) {
if (open_routines[i].name != NULL &&
strcmp(name, open_routines[i].name) == 0)
return i+1;
}
return WTAP_TYPE_AUTO; /* no such file type */
}
static char *get_file_extension(const char *pathname)
{
gchar *filename;

View File

@ -1493,6 +1493,8 @@ void wtap_register_heuristic_open_info(const struct heuristic_open_info *oi);
WS_DLL_PUBLIC
void wtap_register_open_info(const struct open_info *oi);
WS_DLL_PUBLIC
unsigned int open_info_name_to_type(const char *name);
WS_DLL_PUBLIC
int wtap_register_file_type_subtypes(const struct file_type_subtype_info* fi);
WS_DLL_PUBLIC
int wtap_register_encap_type(const char* name, const char* short_name);