Skip to content

Commit

Permalink
Add more error-reporting routines that call through a function pointer.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guyharris committed Mar 15, 2021
1 parent 89ae76d commit c33e2f7
Show file tree
Hide file tree
Showing 23 changed files with 522 additions and 422 deletions.
28 changes: 19 additions & 9 deletions capinfos.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
26 changes: 18 additions & 8 deletions captype.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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 */
}
}
Expand Down
6 changes: 6 additions & 0 deletions debian/libwsutil0.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
61 changes: 25 additions & 36 deletions dftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,34 @@
#include <wiretap/wtap.h>

#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.
*/
Expand All @@ -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);
Expand Down Expand Up @@ -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
*
Expand Down
Loading

0 comments on commit c33e2f7

Please sign in to comment.