From f9be95c4c8ab3484e6cf396e0cf4242b18ff8841 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Sun, 5 Jun 2016 18:04:23 -0700 Subject: [PATCH] Add a routine to get an array of all instances of a string option. Use it for OPT_COMMENT in the SHB, as there may be ore than one instance of OPT_COMMENT in an SHB. Also, use wtap_optionblock_get_option_string for OPT_SHB_HARDWARE, OPT_SHB_OS, and OPT_SHB_USERAPPL; they're specified as "only one instance allowed". Change-Id: I23ad87e41e40b7ae1155e96c0523a6f8caad5204 Reviewed-on: https://code.wireshark.org/review/15750 Reviewed-by: Guy Harris --- capinfos.c | 223 +++++++++++----------------------------- ui/gtk/summary_dlg.c | 27 +++-- wiretap/wtap_opttypes.c | 30 ++++++ wiretap/wtap_opttypes.h | 11 ++ 4 files changed, 118 insertions(+), 173 deletions(-) diff --git a/capinfos.c b/capinfos.c index d1cfa23837..55554ae8da 100644 --- a/capinfos.c +++ b/capinfos.c @@ -559,86 +559,15 @@ string_replace_newlines(gchar *str) } static void -show_comment(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_) +show_option_string(const char *prefix, const char *option_str) { - char *opt_str; + char *str; - switch(option_id) - { - case OPT_COMMENT: - if (option != NULL && option->stringval != NULL) { - opt_str = g_strdup(option->stringval); - string_replace_newlines(opt_str); - printf("Capture comment: %s\n", opt_str); - g_free(opt_str); - } - break; - default: - /* Don't show other options */ - break; - } -} - -static void -show_capture_hardware(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_) -{ - char *opt_str; - - switch(option_id) - { - case OPT_SHB_HARDWARE: - if (option != NULL && option->stringval != NULL) { - opt_str = g_strdup(option->stringval); - string_replace_newlines(opt_str); - printf("Capture hardware: %s\n", opt_str); - g_free(opt_str); - } - break; - default: - /* Don't show other options */ - break; - } -} - -static void -show_capture_os(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_) -{ - char *opt_str; - - switch(option_id) - { - case OPT_SHB_OS: - if (option != NULL && option->stringval != NULL) { - opt_str = g_strdup(option->stringval); - string_replace_newlines(opt_str); - printf("Capture oper-sys: %s\n", opt_str); - g_free(opt_str); - } - break; - default: - /* Don't show other options */ - break; - } -} - -static void -show_capture_userappl(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_) -{ - char *opt_str; - - switch(option_id) - { - case OPT_SHB_USERAPPL: - if (option != NULL && option->stringval != NULL) { - opt_str = g_strdup(option->stringval); - string_replace_newlines(opt_str); - printf("Capture application: %s\n", opt_str); - g_free(opt_str); - } - break; - default: - /* Don't show other options */ - break; + if (option_str != NULL && option_str[0] != '\0') { + str = g_strdup(option_str); + string_replace_newlines(str); + printf("%s%s\n", prefix, str); + g_free(str); } } @@ -766,12 +695,24 @@ print_stats(const gchar *filename, capture_info *cf_info) if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order)); if (cap_comment) { - wtap_optionblock_foreach_option(cf_info->shb, show_comment, NULL); + GArray *opts; + unsigned int i; + + wtap_optionblock_get_string_options(cf_info->shb, OPT_COMMENT, &opts); + for (i = 0; i < opts->len; i++) { + show_option_string("Capture comment: ", g_array_index(opts, char *, i)); + } + g_array_free(opts, TRUE); } if (cap_file_more_info) { - wtap_optionblock_foreach_option(cf_info->shb, show_capture_hardware, NULL); - wtap_optionblock_foreach_option(cf_info->shb, show_capture_os, NULL); - wtap_optionblock_foreach_option(cf_info->shb, show_capture_userappl, NULL); + char *str; + + wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_HARDWARE, &str); + show_option_string("Capture hardware: ", str); + wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_OS, &str); + show_option_string("Capture oper-sys: ", str); + wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_USERAPPL, &str); + show_option_string("Capture application: ", str); } if (cap_file_idb && cf_info->num_interfaces != 0) { @@ -849,82 +790,6 @@ print_stats_table_header(void) printf("\n"); } -static void -put_comment(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_) -{ - switch(option_id) - { - case OPT_COMMENT: - if (option != NULL && option->stringval != NULL) { - putsep(); - putquote(); - printf("%s", option->stringval); - putquote(); - } - break; - default: - /* Don't show other options */ - break; - } -} - -static void -put_capture_hardware(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_) -{ - switch(option_id) - { - case OPT_SHB_HARDWARE: - if (option != NULL && option->stringval != NULL) { - putsep(); - putquote(); - printf("%s", option->stringval); - putquote(); - } - break; - default: - /* Don't show other options */ - break; - } -} - -static void -put_capture_os(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_) -{ - switch(option_id) - { - case OPT_SHB_OS: - if (option != NULL && option->stringval != NULL) { - putsep(); - putquote(); - printf("%s", option->stringval); - putquote(); - } - break; - default: - /* Don't show other options */ - break; - } -} - -static void -put_capture_userappl(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_) -{ - switch(option_id) - { - case OPT_SHB_USERAPPL: - if (option != NULL && option->stringval != NULL) { - putsep(); - putquote(); - printf("%s", option->stringval); - putquote(); - } - break; - default: - /* Don't show other options */ - break; - } -} - static void print_stats_table(const gchar *filename, capture_info *cf_info) { @@ -1107,15 +972,47 @@ print_stats_table(const gchar *filename, capture_info *cf_info) * of options */ if (cap_comment) { - wtap_optionblock_foreach_option(cf_info->shb, put_comment, NULL); + GArray *opts; + unsigned int i; + + wtap_optionblock_get_string_options(cf_info->shb, OPT_COMMENT, &opts); + for (i = 0; i < opts->len; i++) { + const char *opt_comment = g_array_index(opts, char *, i); + + if (opt_comment != NULL) { + putsep(); + putquote(); + printf("%s", opt_comment); + putquote(); + } + } + g_array_free(opts, TRUE); } if (cap_file_more_info) { - wtap_optionblock_foreach_option(cf_info->shb, put_capture_hardware, NULL); + char *str; - wtap_optionblock_foreach_option(cf_info->shb, put_capture_os, NULL); - - wtap_optionblock_foreach_option(cf_info->shb, put_capture_userappl, NULL); + wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_HARDWARE, &str); + if (str != NULL) { + putsep(); + putquote(); + printf("%s", str); + putquote(); + } + wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_OS, &str); + if (str != NULL) { + putsep(); + putquote(); + printf("%s", str); + putquote(); + } + wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_USERAPPL, &str); + if (str != NULL) { + putsep(); + putquote(); + printf("%s", str); + putquote(); + } } printf("\n"); diff --git a/ui/gtk/summary_dlg.c b/ui/gtk/summary_dlg.c index 03b8d31d8c..8bb85774ab 100644 --- a/ui/gtk/summary_dlg.c +++ b/ui/gtk/summary_dlg.c @@ -190,6 +190,7 @@ summary_open_cb(GtkWidget *w _U_, gpointer d _U_) iface_options iface; wtap_optionblock_t shb_inf; unsigned int i; + GArray *opts; if (summary_dlg != NULL) { /* There's already a Summary dialog box; reactivate it. */ @@ -292,11 +293,14 @@ summary_open_cb(GtkWidget *w _U_, gpointer d _U_) buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (comment_view)); gtk_text_buffer_set_text (buffer, "", -1); if (shb_inf != NULL) { - char *str; - - wtap_optionblock_get_option_string(shb_inf, OPT_COMMENT, &str); - if (str != NULL && str[0] != '\0') - gtk_text_buffer_set_text (buffer, str, -1); + wtap_optionblock_get_string_options(shb_inf, OPT_COMMENT, &opts); + for (i = 0; i < opts->len; i++) { + /* XXX - this only shows the last comment */ + char *opt_comment = g_array_index(opts, char *, i); + if (opt_comment != NULL && opt_comment[0] != '\0') + gtk_text_buffer_set_text (buffer, opt_comment, -1); + } + g_array_free(opts, TRUE); } gtk_box_pack_start(GTK_BOX(comment_vbox), comment_view, TRUE, TRUE, 0); gtk_widget_show (comment_view); @@ -661,6 +665,7 @@ summary_to_texbuff(GtkTextBuffer *buffer) gchar string_buff[SUM_STR_MAX]; gchar tmp_buff[SUM_STR_MAX]; wtap_optionblock_t shb_inf; + GArray *opts; unsigned int i; unsigned int elapsed_time; iface_options iface; @@ -896,11 +901,13 @@ summary_to_texbuff(GtkTextBuffer *buffer) /* Trace file comments from SHB */ shb_inf = wtap_file_get_shb(cfile.wth); if (shb_inf != NULL) { - char *str; - - wtap_optionblock_get_option_string(shb_inf, OPT_COMMENT, &str); - if (str != NULL && str[0] != '\0') - gtk_text_buffer_insert_at_cursor(buffer, str, -1); + wtap_optionblock_get_string_options(shb_inf, OPT_COMMENT, &opts); + for (i = 0; i < opts->len; i++) { + /* XXX - separator between comments? */ + char *opt_comment = g_array_index(opts, char *, i); + if (opt_comment != NULL && opt_comment[0] != '\0') + gtk_text_buffer_insert_at_cursor(buffer, opt_comment, -1); + } } diff --git a/wiretap/wtap_opttypes.c b/wiretap/wtap_opttypes.c index d87fd3b805..296a6fb29d 100644 --- a/wiretap/wtap_opttypes.c +++ b/wiretap/wtap_opttypes.c @@ -374,6 +374,36 @@ wtap_opttype_return_val wtap_optionblock_get_option_string(wtap_optionblock_t bl return WTAP_OPTTYPE_SUCCESS; } +wtap_opttype_return_val wtap_optionblock_get_string_options(wtap_optionblock_t block, guint option_id, GArray **value) +{ + guint n_options; + guint i; + wtap_optblock_value_t* opt_value; + GArray *opt_values; + + n_options = 0; + for (i = 0; i < block->option_values->len; i++) + { + opt_value = g_array_index(block->option_values, wtap_optblock_value_t*, i); + if (opt_value->info->number == option_id) { + if (opt_value->info->type != WTAP_OPTTYPE_STRING) + return WTAP_OPTTYPE_TYPE_MISMATCH; + n_options++; + } + } + + opt_values = g_array_sized_new(FALSE, FALSE, sizeof (char *), n_options); + for (i = 0; i < block->option_values->len; i++) + { + opt_value = g_array_index(block->option_values, wtap_optblock_value_t*, i); + if (opt_value->info->number == option_id) + g_array_append_val(opt_values, opt_value->option.stringval); + } + + *value = opt_values; + return WTAP_OPTTYPE_SUCCESS; +} + wtap_opttype_return_val wtap_optionblock_set_option_uint64(wtap_optionblock_t block, guint option_id, guint64 value) { wtap_optblock_value_t* opt_value = wtap_optionblock_get_option(block, option_id); diff --git a/wiretap/wtap_opttypes.h b/wiretap/wtap_opttypes.h index 01df05b18b..92c9ff47cf 100644 --- a/wiretap/wtap_opttypes.h +++ b/wiretap/wtap_opttypes.h @@ -262,6 +262,17 @@ wtap_optionblock_set_option_string_format(wtap_optionblock_t block, guint option WS_DLL_PUBLIC wtap_opttype_return_val wtap_optionblock_get_option_string(wtap_optionblock_t block, guint option_id, char** value); +/** Get array of string option values from an option block + * + * @param[in] block Block from which to get option values + * @param[in] option_id Identifier value for option + * @param[out] value Returned GArray of option values + * @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful, + * error code otherwise + */ +WS_DLL_PUBLIC wtap_opttype_return_val +wtap_optionblock_get_string_options(wtap_optionblock_t block, guint option_id, GArray **value); + /** Set UINT64 option value in an option block * * @param[in] block Block in which to set the option value