From c33e2f7b51f9e14f99f6b6d9018ac41b7e97d5a6 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Mon, 15 Mar 2021 11:29:43 -0700 Subject: [PATCH] Add more error-reporting routines that call through a function pointer. Have routines to report capture-file errors, using libwireshark error codes and strings, that call through a pointer, so they can pop up dialogs in GUI apps, print a message to the standard error on command-line apps, and possibly do something different on server programs. Have init_report_message() take a pointer to structure containing those function pointers, rather than the function pointers themselves, as arguments. Make other API changes to make that work. --- capinfos.c | 28 ++++++++---- captype.c | 26 +++++++---- debian/libwsutil0.symbols | 6 +++ dftest.c | 61 +++++++++++-------------- editcap.c | 44 ++++++++++-------- extcap/androiddump.c | 27 ++++++++--- extcap/randpktdump.c | 4 +- fuzz/fuzzshark.c | 57 +++++++++-------------- mergecap.c | 41 ++++++++--------- randpkt.c | 25 ++++++++--- randpkt_core/randpkt_core.c | 6 +-- rawshark.c | 66 ++++++--------------------- reordercap.c | 32 ++++++++----- sharkd.c | 68 +++++++++------------------- tfshark.c | 65 +++++++++------------------ tshark.c | 84 ++++++++++++---------------------- ui/cmdarg_err.c | 6 +++ ui/cmdarg_err.h | 4 ++ ui/failure_message.c | 80 +++++++++++++++++++++++++-------- ui/failure_message.h | 48 +++++++++++++++----- ui/qt/main.cpp | 16 +++++-- wsutil/report_message.c | 90 ++++++++++++++++++++++++++++--------- wsutil/report_message.h | 58 +++++++++++++++++++++--- 23 files changed, 521 insertions(+), 421 deletions(-) diff --git a/capinfos.c b/capinfos.c index ddc224d2e9..00b2e74c5d 100644 --- a/capinfos.c +++ b/capinfos.c @@ -1193,7 +1193,7 @@ process_cap_file(const char *filename, gboolean need_separator) cf_info.wth = wtap_open_offline(filename, WTAP_TYPE_AUTO, &err, &err_info, FALSE); if (!cf_info.wth) { - cfile_open_failure_message("capinfos", filename, err, err_info); + cfile_open_failure_message(filename, err, err_info); return 2; } @@ -1352,7 +1352,7 @@ process_cap_file(const char *filename, gboolean need_separator) fprintf(stderr, "capinfos: An error occurred after reading %u packets from \"%s\".\n", packet, filename); - cfile_read_failure_message("capinfos", filename, err, err_info); + cfile_read_failure_message(filename, err, err_info); if (err == WTAP_ERR_SHORT_READ) { /* Don't give up completely with this one. */ status = 1; @@ -1511,11 +1511,10 @@ print_usage(FILE *output) } /* - * General errors and warnings are reported with an console message - * in capinfos. + * Report an error in command-line arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +capinfos_cmdarg_err(const char *msg_format, va_list ap) { fprintf(stderr, "capinfos: "); vfprintf(stderr, msg_format, ap); @@ -1526,7 +1525,7 @@ failure_warning_message(const char *msg_format, va_list ap) * Report additional information for an error in command-line arguments. */ static void -failure_message_cont(const char *msg_format, va_list ap) +capinfos_cmdarg_err_cont(const char *msg_format, va_list ap) { vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); @@ -1545,6 +1544,18 @@ int main(int argc, char *argv[]) { char *init_progfile_dir_error; + static const struct report_message_routines capinfos_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; gboolean need_separator = FALSE; int opt; int overall_error_status = EXIT_SUCCESS; @@ -1570,7 +1581,7 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif - cmdarg_err_init(failure_warning_message, failure_message_cont); + cmdarg_err_init(capinfos_cmdarg_err, capinfos_cmdarg_err_cont); /* Get the decimal point. */ decimal_point = g_strdup(localeconv()->decimal_point); @@ -1599,8 +1610,7 @@ main(int argc, char *argv[]) g_free(init_progfile_dir_error); } - init_report_message(failure_warning_message, failure_warning_message, - NULL, NULL, NULL); + init_report_message("capinfos", &capinfos_report_routines); wtap_init(TRUE); diff --git a/captype.c b/captype.c index 0a620af0c4..37f81ff443 100644 --- a/captype.c +++ b/captype.c @@ -61,11 +61,10 @@ print_usage(FILE *output) } /* - * General errors and warnings are reported with an console message - * in captype. + * Report an error in command-line arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +captype_cmdarg_err(const char *msg_format, va_list ap) { fprintf(stderr, "captype: "); vfprintf(stderr, msg_format, ap); @@ -76,7 +75,7 @@ failure_warning_message(const char *msg_format, va_list ap) * Report additional information for an error in command-line arguments. */ static void -failure_message_cont(const char *msg_format, va_list ap) +captype_cmdarg_err_cont(const char *msg_format, va_list ap) { vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); @@ -86,6 +85,18 @@ int main(int argc, char *argv[]) { char *init_progfile_dir_error; + static const struct report_message_routines captype_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; wtap *wth; int err; gchar *err_info; @@ -108,7 +119,7 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif - cmdarg_err_init(failure_warning_message, failure_message_cont); + cmdarg_err_init(captype_cmdarg_err, captype_cmdarg_err_cont); /* Initialize the version information. */ ws_init_version_info("Captype (Wireshark)", NULL, NULL, NULL); @@ -134,8 +145,7 @@ main(int argc, char *argv[]) g_free(init_progfile_dir_error); } - init_report_message(failure_warning_message, failure_warning_message, - NULL, NULL, NULL); + init_report_message("captype", &captype_report_routines); wtap_init(TRUE); @@ -179,7 +189,7 @@ main(int argc, char *argv[]) if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT) printf("%s: unknown\n", argv[i]); else { - cfile_open_failure_message("captype", argv[i], err, err_info); + cfile_open_failure_message(argv[i], err, err_info); overall_error_status = 2; /* remember that an error has occurred */ } } diff --git a/debian/libwsutil0.symbols b/debian/libwsutil0.symbols index df377b8bee..4c3135dbf4 100644 --- a/debian/libwsutil0.symbols +++ b/debian/libwsutil0.symbols @@ -80,6 +80,7 @@ libwsutil.so.0 libwsutil0 #MINVER# get_datafile_path@Base 1.12.0~rc1 get_dirname@Base 1.12.0~rc1 get_extcap_dir@Base 1.99.0 + get_friendly_program_name@Base 3.5.0 get_global_profiles_dir@Base 1.12.0~rc1 get_os_version_info@Base 1.99.0 get_persconffile_path@Base 1.12.0~rc1 @@ -160,6 +161,11 @@ libwsutil.so.0 libwsutil0 #MINVER# register_codec@Base 3.1.0 relinquish_special_privs_perm@Base 1.10.0 rename_persconffile_profile@Base 1.12.0~rc1 + report_cfile_close_failure@Base 3.5.0 + report_cfile_dump_open_failure@Base 3.5.0 + report_cfile_open_failure@Base 3.5.0 + report_cfile_read_failure@Base 3.5.0 + report_cfile_write_failure@Base 3.5.0 report_failure@Base 1.12.0~rc1 report_open_failure@Base 1.12.0~rc1 report_read_failure@Base 1.12.0~rc1 diff --git a/dftest.c b/dftest.c index 5ccd16ea08..e248b56183 100644 --- a/dftest.c +++ b/dftest.c @@ -33,21 +33,34 @@ #include #include "ui/util.h" +#include "ui/cmdarg_err.h" +#include "ui/failure_message.h" -static void failure_warning_message(const char *msg_format, va_list ap); -static void open_failure_message(const char *filename, int err, - gboolean for_writing); -static void read_failure_message(const char *filename, int err); -static void write_failure_message(const char *filename, int err); +static void dftest_cmdarg_err(const char *fmt, va_list ap); +static void dftest_cmdarg_err_cont(const char *fmt, va_list ap); int main(int argc, char **argv) { char *init_progfile_dir_error; + static const struct report_message_routines dftest_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; char *text; dfilter_t *df; gchar *err_msg; + cmdarg_err_init(dftest_cmdarg_err, dftest_cmdarg_err_cont); + /* * Get credential information for later use. */ @@ -64,9 +77,7 @@ main(int argc, char **argv) g_free(init_progfile_dir_error); } - init_report_message(failure_warning_message, failure_warning_message, - open_failure_message, read_failure_message, - write_failure_message); + init_report_message("dftest", &dftest_report_routines); timestamp_set_type(TS_RELATIVE); timestamp_set_seconds_type(TS_SECONDS_DEFAULT); @@ -137,48 +148,26 @@ main(int argc, char **argv) } /* - * General errors and warnings are reported with an console message - * in "dftest". + * Report an error in command-line arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +dftest_cmdarg_err(const char *fmt, va_list ap) { fprintf(stderr, "dftest: "); - vfprintf(stderr, msg_format, ap); + vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); } /* - * Open/create errors are reported with an console message in "dftest". + * Report additional information for an error in command-line arguments. */ static void -open_failure_message(const char *filename, int err, gboolean for_writing) +dftest_cmdarg_err_cont(const char *fmt, va_list ap) { - fprintf(stderr, "dftest: "); - fprintf(stderr, file_open_error_message(err, for_writing), filename); + vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); } -/* - * Read errors are reported with an console message in "dftest". - */ -static void -read_failure_message(const char *filename, int err) -{ - fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n", - filename, g_strerror(err)); -} - -/* - * Write errors are reported with an console message in "dftest". - */ -static void -write_failure_message(const char *filename, int err) -{ - fprintf(stderr, "dftest: An error occurred while writing to the file \"%s\": %s.\n", - filename, g_strerror(err)); -} - /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/editcap.c b/editcap.c index 8f02b084b6..87a6b6157c 100644 --- a/editcap.c +++ b/editcap.c @@ -970,11 +970,10 @@ framenum_compare(gconstpointer a, gconstpointer b, gpointer user_data _U_) } /* - * General errors and warnings are reported with an console message - * in editcap. + * Report an error in command-line arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +editcap_cmdarg_err(const char *msg_format, va_list ap) { fprintf(stderr, "editcap: "); vfprintf(stderr, msg_format, ap); @@ -985,7 +984,7 @@ failure_warning_message(const char *msg_format, va_list ap) * Report additional information for an error in command-line arguments. */ static void -failure_message_cont(const char *msg_format, va_list ap) +editcap_cmdarg_err_cont(const char *msg_format, va_list ap) { vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); @@ -1078,6 +1077,18 @@ int main(int argc, char *argv[]) { char *init_progfile_dir_error; + static const struct report_message_routines editcap_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; wtap *wth = NULL; int i, j, read_err, write_err; gchar *read_err_info, *write_err_info; @@ -1141,7 +1152,7 @@ main(int argc, char *argv[]) gboolean valid_seed = FALSE; unsigned int seed = 0; - cmdarg_err_init(failure_warning_message, failure_message_cont); + cmdarg_err_init(editcap_cmdarg_err, editcap_cmdarg_err_cont); #ifdef _WIN32 create_app_running_mutex(); @@ -1167,8 +1178,7 @@ main(int argc, char *argv[]) g_free(init_progfile_dir_error); } - init_report_message(failure_warning_message, failure_warning_message, - NULL, NULL, NULL); + init_report_message("editcap", &editcap_report_routines); wtap_init(TRUE); @@ -1546,8 +1556,7 @@ main(int argc, char *argv[]) wth = wtap_open_offline(argv[optind], WTAP_TYPE_AUTO, &read_err, &read_err_info, FALSE); if (!wth) { - cfile_open_failure_message("editcap", argv[optind], read_err, - read_err_info); + cfile_open_failure_message(argv[optind], read_err, read_err_info); ret = INVALID_FILE; goto clean_exit; } @@ -1733,7 +1742,7 @@ main(int argc, char *argv[]) &write_err_info); if (pdh == NULL) { - cfile_dump_open_failure_message("editcap", filename, + cfile_dump_open_failure_message(filename, write_err, write_err_info, out_file_type_subtype); ret = INVALID_FILE; @@ -1745,8 +1754,7 @@ main(int argc, char *argv[]) * Process whatever IDBs we haven't seen yet. */ if (!process_new_idbs(wth, pdh, idbs_seen, &write_err, &write_err_info)) { - cfile_write_failure_message("editcap", argv[optind], - filename, + cfile_write_failure_message(argv[optind], filename, write_err, write_err_info, read_count, out_file_type_subtype); @@ -1786,7 +1794,7 @@ main(int argc, char *argv[]) &write_err, &write_err_info); if (pdh == NULL) { - cfile_dump_open_failure_message("editcap", filename, + cfile_dump_open_failure_message(filename, write_err, write_err_info, out_file_type_subtype); @@ -1817,7 +1825,7 @@ main(int argc, char *argv[]) pdh = editcap_dump_open(filename, ¶ms, idbs_seen, &write_err, &write_err_info); if (pdh == NULL) { - cfile_dump_open_failure_message("editcap", filename, + cfile_dump_open_failure_message(filename, write_err, write_err_info, out_file_type_subtype); ret = INVALID_FILE; @@ -2191,8 +2199,7 @@ main(int argc, char *argv[]) /* Attempt to dump out current frame to the output file */ if (!wtap_dump(pdh, rec, buf, &write_err, &write_err_info)) { - cfile_write_failure_message("editcap", argv[optind], - filename, + cfile_write_failure_message(argv[optind], filename, write_err, write_err_info, read_count, out_file_type_subtype); @@ -2212,8 +2219,7 @@ main(int argc, char *argv[]) if (read_err != 0) { /* Print a message noting that the read failed somewhere along the * line. */ - cfile_read_failure_message("editcap", argv[optind], read_err, - read_err_info); + cfile_read_failure_message(argv[optind], read_err, read_err_info); } if (!pdh) { @@ -2225,7 +2231,7 @@ main(int argc, char *argv[]) pdh = editcap_dump_open(filename, ¶ms, idbs_seen, &write_err, &write_err_info); if (pdh == NULL) { - cfile_dump_open_failure_message("editcap", filename, + cfile_dump_open_failure_message(filename, write_err, write_err_info, out_file_type_subtype); ret = INVALID_FILE; diff --git a/extcap/androiddump.c b/extcap/androiddump.c index f999161aef..6011345385 100644 --- a/extcap/androiddump.c +++ b/extcap/androiddump.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -359,7 +360,7 @@ static const char* interface_to_logbuf(char* interface) * g_logv() with the appropriate arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +androiddump_cmdarg_err(const char *msg_format, va_list ap) { g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, msg_format, ap); } @@ -459,12 +460,12 @@ static struct extcap_dumper extcap_dumper_open(char *fifo, int encap) { file_type_subtype = wtap_pcap_nsec_file_type_subtype(); extcap_dumper.dumper.wtap = wtap_dump_open(fifo, file_type_subtype, WTAP_UNCOMPRESSED, ¶ms, &err, &err_info); if (!extcap_dumper.dumper.wtap) { - cfile_dump_open_failure_message("androiddump", fifo, err, err_info, file_type_subtype); + cfile_dump_open_failure_message(fifo, err, err_info, file_type_subtype); exit(EXIT_CODE_CANNOT_SAVE_WIRETAP_DUMP); } extcap_dumper.encap = encap; if (!wtap_dump_flush(extcap_dumper.dumper.wtap, &err)) { - cfile_dump_open_failure_message("androiddump", fifo, err, NULL, file_type_subtype); + cfile_dump_open_failure_message(fifo, err, NULL, file_type_subtype); exit(EXIT_CODE_CANNOT_SAVE_WIRETAP_DUMP); } #endif @@ -522,13 +523,13 @@ static gboolean extcap_dumper_dump(struct extcap_dumper extcap_dumper, rec.rec_header.packet_header.pkt_encap = extcap_dumper.encap; if (!wtap_dump(extcap_dumper.dumper.wtap, &rec, (const guint8 *) buffer, &err, &err_info)) { - cfile_write_failure_message("androiddump", NULL, fifo, err, err_info, 0, + cfile_write_failure_message(NULL, fifo, err, err_info, 0, wtap_dump_file_type_subtype(extcap_dumper.dumper.wtap)); return FALSE; } if (!wtap_dump_flush(extcap_dumper.dumper.wtap, &err)) { - cfile_write_failure_message("androiddump", NULL, fifo, err, NULL, 0, + cfile_write_failure_message(NULL, fifo, err, NULL, 0, wtap_dump_file_type_subtype(extcap_dumper.dumper.wtap)); return FALSE; } @@ -2490,6 +2491,18 @@ static int capture_android_tcpdump(char *interface, char *fifo, int main(int argc, char *argv[]) { char *err_msg; + static const struct report_message_routines androiddummp_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; int ret = EXIT_CODE_GENERIC; int option_idx = 0; int result; @@ -2514,7 +2527,7 @@ int main(int argc, char *argv[]) { char *help_url; char *help_header = NULL; - cmdarg_err_init(failure_warning_message, failure_warning_message); + cmdarg_err_init(androiddump_cmdarg_err, androiddump_cmdarg_err); /* * Get credential information for later use. @@ -2532,6 +2545,8 @@ int main(int argc, char *argv[]) { g_free(err_msg); } + init_report_message("androiddump", &androiddummp_report_routines); + extcap_conf = g_new0(extcap_parameters, 1); help_url = data_file_url("androiddump.html"); diff --git a/extcap/randpktdump.c b/extcap/randpktdump.c index 50ef15fc56..be7dc52810 100644 --- a/extcap/randpktdump.c +++ b/extcap/randpktdump.c @@ -125,7 +125,7 @@ static int list_config(char *interface) return EXIT_SUCCESS; } -static void failure_warning_message(const char *msg_format, va_list ap) +static void randpktdump_cmdarg_err(const char *msg_format, va_list ap) { g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, msg_format, ap); } @@ -150,7 +150,7 @@ int main(int argc, char *argv[]) char* help_url; char* help_header = NULL; - cmdarg_err_init(failure_warning_message, failure_warning_message); + cmdarg_err_init(randpktdump_cmdarg_err, randpktdump_cmdarg_err); /* * Get credential information for later use. diff --git a/fuzz/fuzzshark.c b/fuzz/fuzzshark.c index 3bcfca451a..f7cefd98d0 100644 --- a/fuzz/fuzzshark.c +++ b/fuzz/fuzzshark.c @@ -1,4 +1,4 @@ -/* oss-fuzzshark.c +/* fuzzshark.c * * Fuzzer variant of Wireshark for oss-fuzz * @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -49,51 +50,21 @@ static epan_t *fuzz_epan; static epan_dissect_t *fuzz_edt; /* - * General errors and warnings are reported with an console message - * in oss-fuzzshark. + * Report an error in command-line arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +fuzzshark_cmdarg_err(const char *msg_format, va_list ap) { fprintf(stderr, "oss-fuzzshark: "); vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); } -/* - * Open/create errors are reported with an console message in oss-fuzzshark. - */ -static void -open_failure_message(const char *filename, int err, gboolean for_writing) -{ - fprintf(stderr, "oss-fuzzshark: "); - fprintf(stderr, file_open_error_message(err, for_writing), filename); - fprintf(stderr, "\n"); -} - -/* - * Read errors are reported with an console message in oss-fuzzshark. - */ -static void -read_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while reading from the file \"%s\": %s.", filename, g_strerror(err)); -} - -/* - * Write errors are reported with an console message in oss-fuzzshark. - */ -static void -write_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while writing to the file \"%s\": %s.", filename, g_strerror(err)); -} - /* * Report additional information for an error in command-line arguments. */ static void -failure_message_cont(const char *msg_format, va_list ap) +fuzzshark_cmdarg_err_cont(const char *msg_format, va_list ap) { vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); @@ -181,6 +152,19 @@ fuzz_init(int argc _U_, char **argv) { char *init_progfile_dir_error; + static const struct report_message_routines fuzzshark_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; + char *err_msg = NULL; e_prefs *prefs_p; int ret = EXIT_SUCCESS; @@ -250,7 +234,7 @@ fuzz_init(int argc _U_, char **argv) g_setenv("WIRESHARK_DEBUG_WMEM_OVERRIDE", "simple", 0); g_setenv("G_SLICE", "always-malloc", 0); - cmdarg_err_init(failure_warning_message, failure_message_cont); + cmdarg_err_init(fuzzshark_cmdarg_err, fuzzshark_cmdarg_err_cont); /* * Get credential information for later use, and drop privileges @@ -273,8 +257,7 @@ fuzz_init(int argc _U_, char **argv) ws_init_version_info("OSS Fuzzshark (Wireshark)", NULL, epan_get_compiled_version_info, epan_get_runtime_version_info); - init_report_message(failure_warning_message, failure_warning_message, - open_failure_message, read_failure_message, write_failure_message); + init_report_message("fuzzshark", &fuzzshark_report_routines); timestamp_set_type(TS_RELATIVE); timestamp_set_precision(TS_PREC_AUTO); diff --git a/mergecap.c b/mergecap.c index 085bc1f021..fc21531d5a 100644 --- a/mergecap.c +++ b/mergecap.c @@ -101,18 +101,6 @@ mergecap_cmdarg_err_cont(const char *fmt, va_list ap) fprintf(stderr, "\n"); } -/* - * General errors and warnings are reported with an console message - * in mergecap. - */ -static void -failure_warning_message(const char *msg_format, va_list ap) -{ - fprintf(stderr, "mergecap: "); - vfprintf(stderr, msg_format, ap); - fprintf(stderr, "\n"); -} - static void list_capture_types(void) { GArray *writable_type_subtypes; @@ -206,6 +194,18 @@ int main(int argc, char *argv[]) { char *init_progfile_dir_error; + static const struct report_message_routines mergecap_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; int opt; static const struct option long_options[] = { {"help", no_argument, NULL, 'h'}, @@ -252,8 +252,7 @@ main(int argc, char *argv[]) g_free(init_progfile_dir_error); } - init_report_message(failure_warning_message, failure_warning_message, - NULL, NULL, NULL); + init_report_message("mergecap", &mergecap_report_routines); wtap_init(TRUE); @@ -393,18 +392,15 @@ main(int argc, char *argv[]) break; case MERGE_ERR_CANT_OPEN_INFILE: - cfile_open_failure_message("mergecap", argv[optind + err_fileno], - err, err_info); + cfile_open_failure_message(argv[optind + err_fileno], err, err_info); break; case MERGE_ERR_CANT_OPEN_OUTFILE: - cfile_dump_open_failure_message("mergecap", out_filename, err, err_info, - file_type); + cfile_dump_open_failure_message(out_filename, err, err_info, file_type); break; case MERGE_ERR_CANT_READ_INFILE: - cfile_read_failure_message("mergecap", argv[optind + err_fileno], - err, err_info); + cfile_read_failure_message(argv[optind + err_fileno], err, err_info); break; case MERGE_ERR_BAD_PHDR_INTERFACE_ID: @@ -413,9 +409,8 @@ main(int argc, char *argv[]) break; case MERGE_ERR_CANT_WRITE_OUTFILE: - cfile_write_failure_message("mergecap", argv[optind + err_fileno], - out_filename, err, err_info, err_framenum, - file_type); + cfile_write_failure_message(argv[optind + err_fileno], out_filename, + err, err_info, err_framenum, file_type); break; case MERGE_ERR_CANT_CLOSE_OUTFILE: diff --git a/randpkt.c b/randpkt.c index e4c05fe14a..4604803b3e 100644 --- a/randpkt.c +++ b/randpkt.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -48,11 +49,10 @@ #define CLOSE_ERROR 2 /* - * General errors and warnings are reported with an console message - * in randpkt. + * Report an error in command-line arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +randpkt_cmdarg_err(const char *msg_format, va_list ap) { fprintf(stderr, "randpkt: "); vfprintf(stderr, msg_format, ap); @@ -63,7 +63,7 @@ failure_warning_message(const char *msg_format, va_list ap) * Report additional information for an error in command-line arguments. */ static void -failure_message_cont(const char *msg_format, va_list ap) +randpkt_cmdarg_err_cont(const char *msg_format, va_list ap) { vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); @@ -109,6 +109,18 @@ int main(int argc, char *argv[]) { char *init_progfile_dir_error; + static const struct report_message_routines randpkt_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; int opt; int produce_type = -1; char *produce_filename = NULL; @@ -141,12 +153,11 @@ main(int argc, char *argv[]) g_free(init_progfile_dir_error); } - init_report_message(failure_warning_message, failure_warning_message, - NULL, NULL, NULL); + init_report_message("randpkt", &randpkt_report_routines); wtap_init(TRUE); - cmdarg_err_init(failure_warning_message, failure_message_cont); + cmdarg_err_init(randpkt_cmdarg_err, randpkt_cmdarg_err_cont); #ifdef _WIN32 create_app_running_mutex(); diff --git a/randpkt_core/randpkt_core.c b/randpkt_core/randpkt_core.c index ea8c74b40d..5ecdac437c 100644 --- a/randpkt_core/randpkt_core.c +++ b/randpkt_core/randpkt_core.c @@ -620,14 +620,14 @@ void randpkt_loop(randpkt_example* example, guint64 produce_count, guint64 packe } if (!wtap_dump(example->dump, rec, buffer, &err, &err_info)) { - cfile_write_failure_message("randpkt", NULL, + cfile_write_failure_message(NULL, example->filename, err, err_info, 0, wtap_dump_file_type_subtype(example->dump)); } if (packet_delay_ms) { g_usleep(1000 * (gulong)packet_delay_ms); if (!wtap_dump_flush(example->dump, &err)) { - cfile_write_failure_message("randpkt", NULL, + cfile_write_failure_message(NULL, example->filename, err, NULL, 0, wtap_dump_file_type_subtype(example->dump)); } @@ -683,7 +683,7 @@ int randpkt_example_init(randpkt_example* example, char* produce_filename, int p example->filename = produce_filename; } if (!example->dump) { - cfile_dump_open_failure_message("randpkt", produce_filename, + cfile_dump_open_failure_message(produce_filename, err, err_info, file_type_subtype); return WRITE_ERROR; } diff --git a/rawshark.c b/rawshark.c index 23fc53bcb7..8e3d2fa33d 100644 --- a/rawshark.c +++ b/rawshark.c @@ -144,11 +144,6 @@ static gboolean process_packet(capture_file *cf, epan_dissect_t *edt, gint64 off wtap_rec *rec, Buffer *buf); static void show_print_file_io_error(int err); -static void failure_warning_message(const char *msg_format, va_list ap); -static void open_failure_message(const char *filename, int err, - gboolean for_writing); -static void read_failure_message(const char *filename, int err); -static void write_failure_message(const char *filename, int err); static void rawshark_cmdarg_err(const char *fmt, va_list ap); static void rawshark_cmdarg_err_cont(const char *fmt, va_list ap); static void protocolinfo_init(char *field); @@ -437,6 +432,18 @@ main(int argc, char *argv[]) #define OPTSTRING_INIT "d:F:hlm:nN:o:pr:R:sS:t:v" static const char optstring[] = OPTSTRING_INIT; + static const struct report_message_routines rawshark_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; /* * Set the C-language locale to the native environment and set the @@ -501,9 +508,7 @@ main(int argc, char *argv[]) (GLogLevelFlags)log_flags, log_func_ignore, NULL /* user_data */); - init_report_message(failure_warning_message, failure_warning_message, - open_failure_message, read_failure_message, - write_failure_message); + init_report_message("rawshark", &rawshark_report_routines); timestamp_set_type(TS_RELATIVE); timestamp_set_precision(TS_PREC_AUTO); @@ -966,7 +971,7 @@ load_cap_file(capture_file *cf) ws_buffer_free(&buf); if (err != 0) { /* Print a message noting that the read failed somewhere along the line. */ - cfile_read_failure_message("Rawshark", cf->filename, err, err_info); + cfile_read_failure_message(cf->filename, err, err_info); return FALSE; } @@ -1431,29 +1436,6 @@ show_print_file_io_error(int err) } } -/* - * General errors and warnings are reported with an console message - * in Rawshark. - */ -static void -failure_warning_message(const char *msg_format, va_list ap) -{ - fprintf(stderr, "rawshark: "); - vfprintf(stderr, msg_format, ap); - fprintf(stderr, "\n"); -} - -/* - * Open/create errors are reported with an console message in Rawshark. - */ -static void -open_failure_message(const char *filename, int err, gboolean for_writing) -{ - fprintf(stderr, "rawshark: "); - fprintf(stderr, file_open_error_message(err, for_writing), filename); - fprintf(stderr, "\n"); -} - static const nstime_t * raw_get_frame_ts(struct packet_provider_data *prov, guint32 frame_num) { @@ -1522,26 +1504,6 @@ raw_cf_open(capture_file *cf, const char *fname) return CF_OK; } -/* - * Read errors are reported with an console message in Rawshark. - */ -static void -read_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while reading from the file \"%s\": %s.", - filename, g_strerror(err)); -} - -/* - * Write errors are reported with an console message in Rawshark. - */ -static void -write_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while writing to the file \"%s\": %s.", - filename, g_strerror(err)); -} - /* * Report an error in command-line arguments. */ diff --git a/reordercap.c b/reordercap.c index 17408a848b..7d91c24338 100644 --- a/reordercap.c +++ b/reordercap.c @@ -105,7 +105,7 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, fprintf(stderr, "reordercap: An error occurred while re-reading \"%s\".\n", infile); - cfile_read_failure_message("reordercap", infile, err, err_info); + cfile_read_failure_message(infile, err, err_info); exit(1); } } @@ -117,8 +117,7 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, /* Dump frame to outfile */ if (!wtap_dump(pdh, rec, ws_buffer_start_ptr(buf), &err, &err_info)) { - cfile_write_failure_message("reordercap", infile, outfile, err, - err_info, frame->num, + cfile_write_failure_message(infile, outfile, err, err_info, frame->num, wtap_file_type_subtype(wth)); exit(1); } @@ -146,7 +145,7 @@ frames_compare(gconstpointer a, gconstpointer b) * in reordercap. */ static void -failure_warning_message(const char *msg_format, va_list ap) +reordercap_cmdarg_err(const char *msg_format, va_list ap) { fprintf(stderr, "reordercap: "); vfprintf(stderr, msg_format, ap); @@ -157,7 +156,7 @@ failure_warning_message(const char *msg_format, va_list ap) * Report additional information for an error in command-line arguments. */ static void -failure_message_cont(const char *msg_format, va_list ap) +reordercap_cmdarg_err_cont(const char *msg_format, va_list ap) { vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); @@ -170,6 +169,18 @@ int main(int argc, char *argv[]) { char *init_progfile_dir_error; + static const struct report_message_routines reordercap_message_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; wtap *wth = NULL; wtap_dumper *pdh = NULL; wtap_rec rec; @@ -196,7 +207,7 @@ main(int argc, char *argv[]) char *infile; const char *outfile; - cmdarg_err_init(failure_warning_message, failure_message_cont); + cmdarg_err_init(reordercap_cmdarg_err, reordercap_cmdarg_err_cont); /* Initialize the version information. */ ws_init_version_info("Reordercap (Wireshark)", NULL, NULL, NULL); @@ -218,8 +229,7 @@ main(int argc, char *argv[]) g_free(init_progfile_dir_error); } - init_report_message(failure_warning_message, failure_warning_message, - NULL, NULL, NULL); + init_report_message("reordercap", &reordercap_message_routines); wtap_init(TRUE); @@ -260,7 +270,7 @@ main(int argc, char *argv[]) open_routine reader to use, then the following needs to change. */ wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE); if (wth == NULL) { - cfile_open_failure_message("reordercap", infile, err, err_info); + cfile_open_failure_message(infile, err, err_info); ret = OPEN_ERROR; goto clean_exit; } @@ -280,7 +290,7 @@ main(int argc, char *argv[]) params.idb_inf = NULL; if (pdh == NULL) { - cfile_dump_open_failure_message("reordercap", outfile, err, err_info, + cfile_dump_open_failure_message(outfile, err, err_info, wtap_file_type_subtype(wth)); wtap_dump_params_cleanup(¶ms); ret = OUTPUT_FILE_ERROR; @@ -316,7 +326,7 @@ main(int argc, char *argv[]) ws_buffer_free(&buf); if (err != 0) { /* Print a message noting that the read failed somewhere along the line. */ - cfile_read_failure_message("reordercap", infile, err, err_info); + cfile_read_failure_message(infile, err, err_info); } printf("%u frames, %u out of order\n", frames->len, wrong_order_count); diff --git a/sharkd.c b/sharkd.c index 7e0aff4f56..b9561fffb9 100644 --- a/sharkd.c +++ b/sharkd.c @@ -74,12 +74,8 @@ capture_file cfile; static guint32 cum_bytes; static frame_data ref_frame; -static void failure_warning_message(const char *msg_format, va_list ap); -static void open_failure_message(const char *filename, int err, - gboolean for_writing); -static void read_failure_message(const char *filename, int err); -static void write_failure_message(const char *filename, int err); -static void failure_message_cont(const char *msg_format, va_list ap); +static void sharkd_cmdarg_err(const char *msg_format, va_list ap); +static void sharkd_cmdarg_err_cont(const char *msg_format, va_list ap); static void print_current_user(void) { @@ -107,8 +103,20 @@ main(int argc, char *argv[]) char *err_msg = NULL; e_prefs *prefs_p; int ret = EXIT_SUCCESS; + static const struct report_message_routines sharkd_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; - cmdarg_err_init(failure_warning_message, failure_message_cont); + cmdarg_err_init(sharkd_cmdarg_err, sharkd_cmdarg_err_cont); /* * Get credential information for later use, and drop privileges @@ -140,9 +148,7 @@ main(int argc, char *argv[]) goto clean_exit; } - init_report_message(failure_warning_message, failure_warning_message, - open_failure_message, read_failure_message, - write_failure_message); + init_report_message("sharkd", &sharkd_report_routines); timestamp_set_type(TS_RELATIVE); timestamp_set_precision(TS_PREC_AUTO); @@ -392,7 +398,7 @@ load_cap_file(capture_file *cf, int max_packet_count, gint64 max_byte_count) } if (err != 0) { - cfile_read_failure_message("sharkd", cf->filename, err, err_info); + cfile_read_failure_message(cf->filename, err, err_info); } return err; @@ -448,58 +454,26 @@ cf_open(capture_file *cf, const char *fname, unsigned int type, gboolean is_temp return CF_OK; fail: - cfile_open_failure_message("sharkd", fname, *err, err_info); + cfile_open_failure_message(fname, *err, err_info); return CF_ERROR; } /* - * General errors and warnings are reported with an console message - * in sharkd. + * Report an error in command-line arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +sharkd_cmdarg_err(const char *msg_format, va_list ap) { fprintf(stderr, "sharkd: "); vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); } -/* - * Open/create errors are reported with an console message in sharkd. - */ -static void -open_failure_message(const char *filename, int err, gboolean for_writing) -{ - fprintf(stderr, "sharkd: "); - fprintf(stderr, file_open_error_message(err, for_writing), filename); - fprintf(stderr, "\n"); -} - -/* - * Read errors are reported with an console message in sharkd. - */ -static void -read_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while reading from the file \"%s\": %s.", - filename, g_strerror(err)); -} - -/* - * Write errors are reported with an console message in sharkd. - */ -static void -write_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while writing to the file \"%s\": %s.", - filename, g_strerror(err)); -} - /* * Report additional information for an error in command-line arguments. */ static void -failure_message_cont(const char *msg_format, va_list ap) +sharkd_cmdarg_err_cont(const char *msg_format, va_list ap) { vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); diff --git a/tfshark.c b/tfshark.c index 504251c2fe..9aae70005c 100644 --- a/tfshark.c +++ b/tfshark.c @@ -63,6 +63,7 @@ #include "ui/util.h" #include "ui/decode_as_utils.h" #include "ui/dissect_opts.h" +#include "ui/failure_message.h" #include #include #include @@ -134,12 +135,8 @@ static gboolean write_finale(void); static const char *cf_open_error_message(int err, gchar *err_info, gboolean for_writing, int file_type); -static void failure_warning_message(const char *msg_format, va_list ap); -static void open_failure_message(const char *filename, int err, - gboolean for_writing); -static void read_failure_message(const char *filename, int err); -static void write_failure_message(const char *filename, int err); -static void failure_message_cont(const char *msg_format, va_list ap); +static void tfshark_cmdarg_err(const char *msg_format, va_list ap); +static void tfshark_cmdarg_err_cont(const char *msg_format, va_list ap); static GHashTable *output_only_tables = NULL; @@ -353,6 +350,18 @@ main(int argc, char *argv[]) #define OPTSTRING "+2C:d:e:E:hK:lo:O:qQr:R:S:t:T:u:vVxX:Y:z:" static const char optstring[] = OPTSTRING; + static const struct report_message_routines tfshark_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; /* * Set the C-language locale to the native environment and set the @@ -364,7 +373,7 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif - cmdarg_err_init(failure_warning_message, failure_message_cont); + cmdarg_err_init(tfshark_cmdarg_err, tfshark_cmdarg_err_cont); #ifdef _WIN32 create_app_running_mutex(); @@ -471,9 +480,7 @@ main(int argc, char *argv[]) (GLogLevelFlags)log_flags, tfshark_log_handler, NULL /* user_data */); - init_report_message(failure_warning_message, failure_warning_message, - open_failure_message, read_failure_message, - write_failure_message); + init_report_message("tfshark", &tfshark_report_routines); timestamp_set_type(TS_RELATIVE); timestamp_set_precision(TS_PREC_AUTO); @@ -2236,53 +2243,21 @@ cf_open_error_message(int err, gchar *err_info _U_, gboolean for_writing, } /* - * General errors and warnings are reported with an console message - * in TFShark. + * Report an error in command-line arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +tfshark_cmdarg_err(const char *msg_format, va_list ap) { fprintf(stderr, "tfshark: "); vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); } -/* - * Open/create errors are reported with an console message in TFShark. - */ -static void -open_failure_message(const char *filename, int err, gboolean for_writing) -{ - fprintf(stderr, "tfshark: "); - fprintf(stderr, file_open_error_message(err, for_writing), filename); - fprintf(stderr, "\n"); -} - -/* - * Read errors are reported with an console message in TFShark. - */ -static void -read_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while reading from the file \"%s\": %s.", - filename, g_strerror(err)); -} - -/* - * Write errors are reported with an console message in TFShark. - */ -static void -write_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while writing to the file \"%s\": %s.", - filename, g_strerror(err)); -} - /* * Report additional information for an error in command-line arguments. */ static void -failure_message_cont(const char *msg_format, va_list ap) +tfshark_cmdarg_err_cont(const char *msg_format, va_list ap) { vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); diff --git a/tshark.c b/tshark.c index 6b9ac73382..10bf2249e7 100644 --- a/tshark.c +++ b/tshark.c @@ -266,12 +266,8 @@ static gboolean write_preamble(capture_file *cf); static gboolean print_packet(capture_file *cf, epan_dissect_t *edt); static gboolean write_finale(void); -static void failure_warning_message(const char *msg_format, va_list ap); -static void open_failure_message(const char *filename, int err, - gboolean for_writing); -static void read_failure_message(const char *filename, int err); -static void write_failure_message(const char *filename, int err); -static void failure_message_cont(const char *msg_format, va_list ap); +static void tshark_cmdarg_err(const char *msg_format, va_list ap); +static void tshark_cmdarg_err_cont(const char *msg_format, va_list ap); static GHashTable *output_only_tables = NULL; @@ -714,6 +710,18 @@ int main(int argc, char *argv[]) { char *err_msg; + static const struct report_message_routines tshark_report_routines = { + failure_message, + failure_message, + open_failure_message, + read_failure_message, + write_failure_message, + cfile_open_failure_message, + cfile_dump_open_failure_message, + cfile_read_failure_message, + cfile_write_failure_message, + cfile_close_failure_message + }; int opt; static const struct option long_options[] = { {"help", no_argument, NULL, 'h'}, @@ -797,7 +805,7 @@ main(int argc, char *argv[]) tshark_debug("tshark started with %d args", argc); - cmdarg_err_init(failure_warning_message, failure_message_cont); + cmdarg_err_init(tshark_cmdarg_err, tshark_cmdarg_err_cont); #ifdef _WIN32 create_app_running_mutex(); @@ -946,9 +954,7 @@ main(int argc, char *argv[]) tshark_log_handler, NULL /* user_data */); #endif - init_report_message(failure_warning_message, failure_warning_message, - open_failure_message, read_failure_message, - write_failure_message); + init_report_message("TShark", &tshark_report_routines); #ifdef HAVE_LIBPCAP capture_opts_init(&global_capture_opts); @@ -2073,8 +2079,7 @@ main(int argc, char *argv[]) &err, &err_info); g_free(comment); if (!exp_pdu_status) { - cfile_dump_open_failure_message("TShark", exp_pdu_filename, - err, err_info, + cfile_dump_open_failure_message(exp_pdu_filename, err, err_info, out_file_type); exit_status = INVALID_EXPORT; goto clean_exit; @@ -3601,7 +3606,7 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, if (pdh == NULL) { /* We couldn't set up to write to the capture file. */ - cfile_dump_open_failure_message("TShark", save_file, err, err_info, + cfile_dump_open_failure_message(save_file, err, err_info, out_file_type); status = PROCESS_FILE_NO_FILE_PROCESSED; goto out; @@ -3724,8 +3729,7 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, case PASS_READ_ERROR: /* Read error. */ - cfile_read_failure_message("TShark", cf->filename, err_pass1, - err_info_pass1); + cfile_read_failure_message(cf->filename, err_pass1, err_info_pass1); status = PROCESS_FILE_ERROR; break; @@ -3749,7 +3753,7 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, case PASS_READ_ERROR: /* Read error. */ - cfile_read_failure_message("TShark", cf->filename, err, err_info); + cfile_read_failure_message(cf->filename, err, err_info); status = PROCESS_FILE_ERROR; break; @@ -3757,8 +3761,8 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, /* Write error. XXX - framenum is not necessarily the frame number in the input file if there was a read filter. */ - cfile_write_failure_message("TShark", cf->filename, save_file, - err, err_info, err_framenum, out_file_type); + cfile_write_failure_message(cf->filename, save_file, err, err_info, + err_framenum, out_file_type); status = PROCESS_FILE_ERROR; break; @@ -4433,7 +4437,7 @@ cf_open(capture_file *cf, const char *fname, unsigned int type, gboolean is_temp return CF_OK; fail: - cfile_open_failure_message("TShark", fname, *err, err_info); + cfile_open_failure_message(fname, *err, err_info); return CF_ERROR; } @@ -4512,11 +4516,10 @@ show_print_file_io_error(void) } /* - * General errors and warnings are reported with an console message - * in TShark. + * Report an error in command-line arguments. */ static void -failure_warning_message(const char *msg_format, va_list ap) +tshark_cmdarg_err(const char *msg_format, va_list ap) { fprintf(stderr, "tshark: "); vfprintf(stderr, msg_format, ap); @@ -4524,36 +4527,15 @@ failure_warning_message(const char *msg_format, va_list ap) } /* - * Open/create errors are reported with an console message in TShark. + * Report additional information for an error in command-line arguments. */ static void -open_failure_message(const char *filename, int err, gboolean for_writing) +tshark_cmdarg_err_cont(const char *msg_format, va_list ap) { - fprintf(stderr, "tshark: "); - fprintf(stderr, file_open_error_message(err, for_writing), filename); + vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); } -/* - * Read errors are reported with an console message in TShark. - */ -static void -read_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while reading from the file \"%s\": %s.", - filename, g_strerror(err)); -} - -/* - * Write errors are reported with an console message in TShark. - */ -static void -write_failure_message(const char *filename, int err) -{ - cmdarg_err("An error occurred while writing to the file \"%s\": %s.", - filename, g_strerror(err)); -} - static void reset_epan_mem(capture_file *cf,epan_dissect_t *edt, gboolean tree, gboolean visual) { if (!epan_auto_reset || (cf->count < epan_auto_reset_count)) @@ -4569,16 +4551,6 @@ static void reset_epan_mem(capture_file *cf,epan_dissect_t *edt, gboolean tree, cf->count = 0; } -/* - * Report additional information for an error in command-line arguments. - */ -static void -failure_message_cont(const char *msg_format, va_list ap) -{ - vfprintf(stderr, msg_format, ap); - fprintf(stderr, "\n"); -} - /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/ui/cmdarg_err.c b/ui/cmdarg_err.c index 4829b3c64f..1f9fc97c2b 100644 --- a/ui/cmdarg_err.c +++ b/ui/cmdarg_err.c @@ -29,6 +29,12 @@ cmdarg_err_init(void (*err)(const char *, va_list), /* * Report an error in command-line arguments. */ +void +vcmdarg_err(const char *fmt, va_list ap) +{ + print_err(fmt, ap); +} + void cmdarg_err(const char *fmt, ...) { diff --git a/ui/cmdarg_err.h b/ui/cmdarg_err.h index 96328a0b66..fce7e51182 100644 --- a/ui/cmdarg_err.h +++ b/ui/cmdarg_err.h @@ -30,6 +30,10 @@ cmdarg_err_init(void (*err)(const char *, va_list), * Report an error in command-line arguments. */ extern void +vcmdarg_err(const char *fmt, va_list ap) + G_GNUC_PRINTF(1, 0); + +extern void cmdarg_err(const char *fmt, ...) G_GNUC_PRINTF(1, 2); diff --git a/ui/failure_message.c b/ui/failure_message.c index f5660122e2..760e75124e 100644 --- a/ui/failure_message.c +++ b/ui/failure_message.c @@ -16,10 +16,59 @@ #include #include +#include #include #include "ui/failure_message.h" +/* + * Generic error message. + */ +void +failure_message(const char *msg_format, va_list ap) +{ + vcmdarg_err(msg_format, ap); +} + +/* + * Error message for a failed attempt to open or create a file + * other than a capture file. + * "filename" is the name of the file being opened; "err" is assumed + * to be a UNIX-style errno; "for_writing" is TRUE if we're opening + * the file for writing and FALSE if we're opening it for reading. + */ +void +open_failure_message(const char *filename, int err, gboolean for_writing) +{ + cmdarg_err(file_open_error_message(err, for_writing), filename); +} + +/* + * Error message for a failed attempt to read from a file other than + * a capture file. + * "filename" is the name of the file being read from; "err" is assumed + * to be a UNIX-style errno. + */ +void +read_failure_message(const char *filename, int err) +{ + cmdarg_err("An error occurred while reading from the file \"%s\": %s.", + filename, g_strerror(err)); +} + +/* + * Error message for a failed attempt to write to a file other than + * a capture file. + * "filename" is the name of the file being written to; "err" is assumed + * to be a UNIX-style errno. + */ +void +write_failure_message(const char *filename, int err) +{ + cmdarg_err("An error occurred while writing to the file \"%s\": %s.", + filename, g_strerror(err)); +} + static char * input_file_description(const char *fname) { @@ -52,14 +101,12 @@ output_file_description(const char *fname) /* * Error message for a failed attempt to open a capture file for reading. - * "progname" is the name of the program trying to open the file; * "filename" is the name of the file being opened; "err" is assumed * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed * to be a string giving further information for some WTAP_ERR_ values. */ void -cfile_open_failure_message(const char *progname, const char *filename, - int err, gchar *err_info) +cfile_open_failure_message(const char *filename, int err, gchar *err_info) { if (err < 0) { /* @@ -77,25 +124,25 @@ cfile_open_failure_message(const char *progname, const char *filename, case WTAP_ERR_RANDOM_OPEN_PIPE: cmdarg_err("The %s is a pipe or FIFO; %s can't read pipe or FIFO files in two-pass mode.", - file_description, progname); + file_description, get_friendly_program_name()); break; case WTAP_ERR_FILE_UNKNOWN_FORMAT: cmdarg_err("The %s isn't a capture file in a format %s understands.", - file_description, progname); + file_description, get_friendly_program_name()); break; case WTAP_ERR_UNSUPPORTED: cmdarg_err("The %s contains record data that %s doesn't support.\n" "(%s)", - file_description, progname, + file_description, get_friendly_program_name(), err_info != NULL ? err_info : "no information supplied"); g_free(err_info); break; case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: cmdarg_err("The %s is a capture for a network type that %s doesn't support.", - file_description, progname); + file_description, get_friendly_program_name()); break; case WTAP_ERR_BAD_FILE: @@ -153,7 +200,6 @@ cfile_open_failure_message(const char *progname, const char *filename, /* * Error message for a failed attempt to open a capture file for writing. - * "progname" is the name of the program trying to open the file; * "filename" is the name of the file being opened; "err" is assumed * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed * to be a string giving further information for some WTAP_ERR_ values; @@ -161,8 +207,7 @@ cfile_open_failure_message(const char *progname, const char *filename, * and subtype of file being opened. */ void -cfile_dump_open_failure_message(const char *progname, const char *filename, - int err, gchar *err_info, +cfile_dump_open_failure_message(const char *filename, int err, gchar *err_info, int file_type_subtype) { if (err < 0) { @@ -187,7 +232,7 @@ cfile_dump_open_failure_message(const char *progname, const char *filename, case WTAP_ERR_UNWRITABLE_FILE_TYPE: cmdarg_err("%s doesn't support writing capture files in that format.", - progname); + get_friendly_program_name()); break; case WTAP_ERR_UNWRITABLE_ENCAP: @@ -235,14 +280,12 @@ cfile_dump_open_failure_message(const char *progname, const char *filename, /* * Error message for a failed attempt to read from a capture file. - * "progname" is the name of the program trying to open the file; * "filename" is the name of the file being opened; "err" is assumed * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed * to be a string giving further information for some WTAP_ERR_ values. */ void -cfile_read_failure_message(const char *progname, const char *filename, - int err, gchar *err_info) +cfile_read_failure_message(const char *filename, int err, gchar *err_info) { char *file_string; @@ -254,7 +297,7 @@ cfile_read_failure_message(const char *progname, const char *filename, case WTAP_ERR_UNSUPPORTED: cmdarg_err("The %s contains record data that %s doesn't support.\n" "(%s)", - file_string, progname, + file_string, get_friendly_program_name(), err_info != NULL ? err_info : "no information supplied"); g_free(err_info); break; @@ -305,7 +348,6 @@ cfile_read_failure_message(const char *progname, const char *filename, /* * Error message for a failed attempt to write to a capture file. - * "progname" is the name of the program trying to open the file; * "in_filename" is the name of the file from which the record * being written came; "out_filename" is the name of the file to * which we're writing; "err" is assumed "err" is assumed to be a @@ -316,8 +358,8 @@ cfile_read_failure_message(const char *progname, const char *filename, * for the type and subtype of file being written. */ void -cfile_write_failure_message(const char *progname, const char *in_filename, - const char *out_filename, int err, gchar *err_info, +cfile_write_failure_message(const char *in_filename, const char *out_filename, + int err, gchar *err_info, guint32 framenum, int file_type_subtype) { char *in_file_string; @@ -368,7 +410,7 @@ cfile_write_failure_message(const char *progname, const char *in_filename, * and report the frame number and file type/subtype. */ cmdarg_err("Frame%s is larger than %s supports in a \"%s\" file.", - in_frame_string, progname, + in_frame_string, get_friendly_program_name(), wtap_file_type_subtype_name(file_type_subtype)); break; diff --git a/ui/failure_message.h b/ui/failure_message.h index d0a7bee14e..70971f9e4b 100644 --- a/ui/failure_message.h +++ b/ui/failure_message.h @@ -16,44 +16,69 @@ extern "C" { #endif /* __cplusplus */ +/* + * Generic error message. + */ +extern void failure_message(const char *msg_format, va_list ap); + +/* + * Error message for a failed attempt to open or create a file + * other than a capture file. + * "filename" is the name of the file being opened; "err" is assumed + * to be a UNIX-style errno; "for_writing" is TRUE if we're opening + * the file for writing and FALSE if we're opening it for reading. + */ +extern void open_failure_message(const char *filename, int err, + gboolean for_writing); + +/* + * Error message for a failed attempt to read from a file other than + * a capture file. + * "filename" is the name of the file being opened; "err" is assumed + * to be a UNIX-style errno. + */ +extern void read_failure_message(const char *filename, int err); + +/* + * Error message for a failed attempt to write to a file other than + * a capture file. + * "filename" is the name of the file being written to; "err" is assumed + * to be a UNIX-style errno. + */ +extern void write_failure_message(const char *filename, int err); + /* * Error message for a failed attempt to open a capture file for input. - * "progname" is the name of the program trying to open the file; * "filename" is the name of the file being opened; "err" is assumed * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed * to be a string giving further information for some WTAP_ERR_ values. */ -extern void cfile_open_failure_message(const char *progname, - const char *filename, int err, +extern void cfile_open_failure_message(const char *filename, int err, gchar *err_info); /* - * "progname" is the name of the program trying to open the file; + * Error message for a failed attempt to open a capture file for output. * "filename" is the name of the file being opened; "err" is assumed * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed * to be a string giving further information for some WTAP_ERR_ values; * "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value for the type * and subtype of file being opened. */ -extern void cfile_dump_open_failure_message(const char *progname, - const char *filename, int err, +extern void cfile_dump_open_failure_message(const char *filename, int err, gchar *err_info, int file_type_subtype); /* * Error message for a failed attempt to read from a capture file. - * "progname" is the name of the program trying to open the file; * "filename" is the name of the file being opened; "err" is assumed * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed * to be a string giving further information for some WTAP_ERR_ values. */ -extern void cfile_read_failure_message(const char *progname, - const char *filename, int err, +extern void cfile_read_failure_message(const char *filename, int err, gchar *err_info); /* * Error message for a failed attempt to write to a capture file. - * "progname" is the name of the program trying to open the file; * "in_filename" is the name of the file from which the record * being written came; "out_filename" is the name of the file to * which we're writing; "err" is assumed "err" is assumed to be a @@ -63,8 +88,7 @@ extern void cfile_read_failure_message(const char *progname, * occurred; "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value * for the type and subtype of file being written. */ -extern void cfile_write_failure_message(const char *progname, - const char *in_filename, +extern void cfile_write_failure_message(const char *in_filename, const char *out_filename, int err, gchar *err_info, guint32 framenum, diff --git a/ui/qt/main.cpp b/ui/qt/main.cpp index fd20563d60..62ae492991 100644 --- a/ui/qt/main.cpp +++ b/ui/qt/main.cpp @@ -465,6 +465,18 @@ int main(int argc, char *qt_argv[]) #endif /* Start time in microseconds */ guint64 start_time = g_get_monotonic_time(); + static const struct report_message_routines wireshark_report_routines = { + vfailure_alert_box, + vwarning_alert_box, + open_failure_alert_box, + read_failure_alert_box, + write_failure_alert_box, + cfile_open_failure_alert_box, + cfile_dump_open_failure_alert_box, + cfile_read_failure_alert_box, + cfile_write_failure_alert_box, + cfile_close_failure_alert_box + }; #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) /* @@ -728,9 +740,7 @@ int main(int argc, char *qt_argv[]) capture_opts_init(&global_capture_opts); #endif - init_report_message(vfailure_alert_box, vwarning_alert_box, - open_failure_alert_box, read_failure_alert_box, - write_failure_alert_box); + init_report_message("Wireshark", &wireshark_report_routines); /* * Libwiretap must be initialized before libwireshark is, so that diff --git a/wsutil/report_message.c b/wsutil/report_message.c index a1b29cadab..4f0d5c1b95 100644 --- a/wsutil/report_message.c +++ b/wsutil/report_message.c @@ -21,23 +21,15 @@ #include #include "report_message.h" -static void (*vreport_failure_func)(const char *, va_list); -static void (*vreport_warning_func)(const char *, va_list); -static void (*report_open_failure_func)(const char *, int, gboolean); -static void (*report_read_failure_func)(const char *, int); -static void (*report_write_failure_func)(const char *, int); +static const char *friendly_program_name; +static const struct report_message_routines *routines; -void init_report_message(void (*vreport_failure_fcn_p)(const char *, va_list), - void (*vreport_warning_fcn_p)(const char *, va_list), - void (*report_open_failure_fcn_p)(const char *, int, gboolean), - void (*report_read_failure_fcn_p)(const char *, int), - void (*report_write_failure_fcn_p)(const char *, int)) +void +init_report_message(const char *friendly_program_name_arg, + const struct report_message_routines *routines_arg) { - vreport_failure_func = vreport_failure_fcn_p; - vreport_warning_func = vreport_warning_fcn_p; - report_open_failure_func = report_open_failure_fcn_p; - report_read_failure_func = report_read_failure_fcn_p; - report_write_failure_func = report_write_failure_fcn_p; + friendly_program_name = friendly_program_name_arg; + routines = routines_arg; } /* @@ -49,7 +41,7 @@ report_failure(const char *msg_format, ...) va_list ap; va_start(ap, msg_format); - (*vreport_failure_func)(msg_format, ap); + (*routines->vreport_failure)(msg_format, ap); va_end(ap); } @@ -62,7 +54,7 @@ report_warning(const char *msg_format, ...) va_list ap; va_start(ap, msg_format); - (*vreport_warning_func)(msg_format, ap); + (*routines->vreport_warning)(msg_format, ap); va_end(ap); } @@ -76,7 +68,7 @@ void report_open_failure(const char *filename, int err, gboolean for_writing) { - (*report_open_failure_func)(filename, err, for_writing); + (*routines->report_open_failure)(filename, err, for_writing); } /* @@ -86,7 +78,7 @@ report_open_failure(const char *filename, int err, void report_read_failure(const char *filename, int err) { - (*report_read_failure_func)(filename, err); + (*routines->report_read_failure)(filename, err); } /* @@ -96,7 +88,65 @@ report_read_failure(const char *filename, int err) void report_write_failure(const char *filename, int err) { - (*report_write_failure_func)(filename, err); + (*routines->report_write_failure)(filename, err); +} + +/* + * Report an error from opening a capture file for reading. + */ +void +report_cfile_open_failure(const char *filename, int err, gchar *err_info) +{ + (*routines->report_cfile_open_failure)(filename, err, err_info); +} + +/* + * Report an error from opening a capture file for writing. + */ +void +report_cfile_dump_open_failure(const char *filename, + int err, gchar *err_info, int file_type_subtype) +{ + (*routines->report_cfile_dump_open_failure)(filename, + err, err_info, file_type_subtype); +} + +/* + * Report an error from attempting to read from a capture file. + */ +void +report_cfile_read_failure(const char *filename, int err, gchar *err_info) +{ + (*routines->report_cfile_read_failure)(filename, err, err_info); +} + +/* + * Report an error from attempting to write to a capture file. + */ +void +report_cfile_write_failure(const char *in_filename, const char *out_filename, + int err, gchar *err_info, guint32 framenum, int file_type_subtype) +{ + (*routines->report_cfile_write_failure)(in_filename, out_filename, + err, err_info, framenum, file_type_subtype); +} + +/* + * Report an error from closing a capture file open for writing. + */ +void +report_cfile_close_failure(const char *filename, int err, gchar *err_info) +{ + (*routines->report_cfile_close_failure)(filename, err, err_info); +} + +/* + * Return the "friendly" program name. + */ +const char * +get_friendly_program_name(void) +{ + return friendly_program_name; } /* diff --git a/wsutil/report_message.h b/wsutil/report_message.h index d0d6b2fc28..76078d3879 100644 --- a/wsutil/report_message.h +++ b/wsutil/report_message.h @@ -29,12 +29,22 @@ extern "C" { /* * Initialize the report message routines */ -WS_DLL_PUBLIC void init_report_message( - void (*vreport_failure)(const char *, va_list), - void (*vreport_warning)(const char *, va_list), - void (*report_open_failure)(const char *, int, gboolean), - void (*report_read_failure)(const char *, int), - void (*report_write_failure)(const char *, int)); +struct report_message_routines { + void (*vreport_failure)(const char *, va_list); + void (*vreport_warning)(const char *, va_list); + void (*report_open_failure)(const char *, int, gboolean); + void (*report_read_failure)(const char *, int); + void (*report_write_failure)(const char *, int); + void (*report_cfile_open_failure)(const char *, int, gchar *); + void (*report_cfile_dump_open_failure)(const char *, int, gchar *, int); + void (*report_cfile_read_failure)(const char *, int, gchar *); + void (*report_cfile_write_failure)(const char *, const char *, + int, gchar *, guint32, int); + void (*report_cfile_close_failure)(const char *, int, gchar *); +}; + +WS_DLL_PUBLIC void init_report_message(const char *friendly_program_name, + const struct report_message_routines *routines); /* * Report a general error. @@ -67,6 +77,42 @@ WS_DLL_PUBLIC void report_read_failure(const char *filename, int err); */ WS_DLL_PUBLIC void report_write_failure(const char *filename, int err); +/* + * Report an error from opening a capture file for reading. + */ +WS_DLL_PUBLIC void report_cfile_open_failure(const char *filename, + int err, gchar *err_info); + +/* + * Report an error from opening a capture file for writing. + */ +WS_DLL_PUBLIC void report_cfile_dump_open_failure(const char *filename, + int err, gchar *err_info, int file_type_subtype); + +/* + * Report an error from attempting to read from a capture file. + */ +WS_DLL_PUBLIC void report_cfile_read_failure(const char *filename, + int err, gchar *err_info); + +/* + * Report an error from attempting to write to a capture file. + */ +WS_DLL_PUBLIC void report_cfile_write_failure(const char *in_filename, + const char *out_filename, int err, gchar *err_info, guint32 framenum, + int file_type_subtype); + +/* + * Report an error from closing a capture file open for writing. + */ +WS_DLL_PUBLIC void report_cfile_close_failure(const char *filename, + int err, gchar *err_info); + +/* + * Return the "friendly" program name. + */ +WS_DLL_PUBLIC const char *get_friendly_program_name(void); + #ifdef __cplusplus } #endif /* __cplusplus */