2009-02-15 21:47:57 +00:00
|
|
|
/* dftest.c
|
|
|
|
* Shows display filter byte-code, for debugging dfilter routines.
|
2001-02-01 20:21:25 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2001-02-01 20:21:25 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-07 11:26:45 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2001-02-01 20:21:25 +00:00
|
|
|
*/
|
|
|
|
|
2014-08-22 21:13:05 +00:00
|
|
|
#include <config.h>
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2013-12-02 08:30:29 +00:00
|
|
|
#include <epan/epan.h>
|
2002-01-21 07:37:49 +00:00
|
|
|
#include <epan/timestamp.h>
|
2013-12-02 08:30:29 +00:00
|
|
|
#include <epan/prefs.h>
|
|
|
|
#include <epan/dfilter/dfilter.h>
|
|
|
|
|
2014-12-02 04:08:50 +00:00
|
|
|
#ifdef HAVE_PLUGINS
|
2013-12-02 08:30:29 +00:00
|
|
|
#include <wsutil/plugins.h>
|
2014-12-02 04:08:50 +00:00
|
|
|
#endif
|
2013-11-17 02:55:14 +00:00
|
|
|
#include <wsutil/filesystem.h>
|
2008-06-30 17:16:29 +00:00
|
|
|
#include <wsutil/privileges.h>
|
2017-04-08 19:45:19 +00:00
|
|
|
#include <wsutil/report_message.h>
|
2021-06-19 18:44:58 +00:00
|
|
|
#include <wsutil/wslog.h>
|
2013-12-02 08:30:29 +00:00
|
|
|
|
2017-02-09 03:31:11 +00:00
|
|
|
#include <wiretap/wtap.h>
|
|
|
|
|
2012-02-17 12:30:27 +00:00
|
|
|
#include "ui/util.h"
|
2021-03-15 18:29:43 +00:00
|
|
|
#include "ui/cmdarg_err.h"
|
|
|
|
#include "ui/failure_message.h"
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2021-03-15 18:29:43 +00:00
|
|
|
static void dftest_cmdarg_err(const char *fmt, va_list ap);
|
|
|
|
static void dftest_cmdarg_err_cont(const char *fmt, va_list ap);
|
2004-03-23 21:19:58 +00:00
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2007-05-25 20:03:26 +00:00
|
|
|
char *init_progfile_dir_error;
|
2021-03-15 18:29:43 +00:00
|
|
|
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
|
|
|
|
};
|
2001-02-01 20:21:25 +00:00
|
|
|
char *text;
|
|
|
|
dfilter_t *df;
|
2015-01-18 10:22:19 +00:00
|
|
|
gchar *err_msg;
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2021-03-15 18:29:43 +00:00
|
|
|
cmdarg_err_init(dftest_cmdarg_err, dftest_cmdarg_err_cont);
|
|
|
|
|
2021-06-24 20:20:25 +00:00
|
|
|
/* Initialize log handler early so we can have proper logging during startup. */
|
|
|
|
ws_log_init("dftest", vcmdarg_err);
|
|
|
|
|
2021-06-19 18:44:58 +00:00
|
|
|
/* Early logging command-line initialization. */
|
|
|
|
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);
|
|
|
|
|
Add a WIRESHARK_RUN_FROM_BUILD_DIRECTORY environment variable on UN*X;
if set, and if the program isn't running with additional privileges,
it'll treat the directory in which the program is found as the data
directory.
If, on Windows, the version-number subdirectory of {data
directory}\plugins doesn't exist (which is assumed to mean that the
program is being run from the build directory), or if, on UN*X,
WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set, the plugin directory is the
"plugins" subdirectory of the data directory, and all subdirectories of
that directory are scanned for plugins, as the "plugins" subdirectory of
the build directory contains subdirectories for the plugins; this means
that if we're running from the build directory, we'll find the plugins
we built in the build tree.
When generating the wireshark-filter man page, run tshark with
WIRESHARK_RUN_FROM_BUILD_DIRECTORY set, so it uses the plugins from the
build to generate the list of filters.
svn path=/trunk/; revision=20261
2007-01-02 06:49:40 +00:00
|
|
|
/*
|
2007-05-21 20:31:45 +00:00
|
|
|
* Get credential information for later use.
|
Add a WIRESHARK_RUN_FROM_BUILD_DIRECTORY environment variable on UN*X;
if set, and if the program isn't running with additional privileges,
it'll treat the directory in which the program is found as the data
directory.
If, on Windows, the version-number subdirectory of {data
directory}\plugins doesn't exist (which is assumed to mean that the
program is being run from the build directory), or if, on UN*X,
WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set, the plugin directory is the
"plugins" subdirectory of the data directory, and all subdirectories of
that directory are scanned for plugins, as the "plugins" subdirectory of
the build directory contains subdirectories for the plugins; this means
that if we're running from the build directory, we'll find the plugins
we built in the build tree.
When generating the wireshark-filter man page, run tshark with
WIRESHARK_RUN_FROM_BUILD_DIRECTORY set, so it uses the plugins from the
build to generate the list of filters.
svn path=/trunk/; revision=20261
2007-01-02 06:49:40 +00:00
|
|
|
*/
|
2010-09-16 19:20:06 +00:00
|
|
|
init_process_policies();
|
Add a WIRESHARK_RUN_FROM_BUILD_DIRECTORY environment variable on UN*X;
if set, and if the program isn't running with additional privileges,
it'll treat the directory in which the program is found as the data
directory.
If, on Windows, the version-number subdirectory of {data
directory}\plugins doesn't exist (which is assumed to mean that the
program is being run from the build directory), or if, on UN*X,
WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set, the plugin directory is the
"plugins" subdirectory of the data directory, and all subdirectories of
that directory are scanned for plugins, as the "plugins" subdirectory of
the build directory contains subdirectories for the plugins; this means
that if we're running from the build directory, we'll find the plugins
we built in the build tree.
When generating the wireshark-filter man page, run tshark with
WIRESHARK_RUN_FROM_BUILD_DIRECTORY set, so it uses the plugins from the
build to generate the list of filters.
svn path=/trunk/; revision=20261
2007-01-02 06:49:40 +00:00
|
|
|
|
2006-02-08 10:22:20 +00:00
|
|
|
/*
|
2016-12-05 04:25:51 +00:00
|
|
|
* Attempt to get the pathname of the directory containing the
|
|
|
|
* executable file.
|
2006-02-08 10:22:20 +00:00
|
|
|
*/
|
2018-05-05 07:19:09 +00:00
|
|
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
2007-05-25 20:03:26 +00:00
|
|
|
if (init_progfile_dir_error != NULL) {
|
2016-12-05 04:25:51 +00:00
|
|
|
fprintf(stderr, "dftest: Can't get pathname of directory containing the dftest program: %s.\n",
|
2009-09-09 16:38:09 +00:00
|
|
|
init_progfile_dir_error);
|
2016-12-05 04:25:51 +00:00
|
|
|
g_free(init_progfile_dir_error);
|
2007-05-25 20:03:26 +00:00
|
|
|
}
|
Add a WIRESHARK_RUN_FROM_BUILD_DIRECTORY environment variable on UN*X;
if set, and if the program isn't running with additional privileges,
it'll treat the directory in which the program is found as the data
directory.
If, on Windows, the version-number subdirectory of {data
directory}\plugins doesn't exist (which is assumed to mean that the
program is being run from the build directory), or if, on UN*X,
WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set, the plugin directory is the
"plugins" subdirectory of the data directory, and all subdirectories of
that directory are scanned for plugins, as the "plugins" subdirectory of
the build directory contains subdirectories for the plugins; this means
that if we're running from the build directory, we'll find the plugins
we built in the build tree.
When generating the wireshark-filter man page, run tshark with
WIRESHARK_RUN_FROM_BUILD_DIRECTORY set, so it uses the plugins from the
build to generate the list of filters.
svn path=/trunk/; revision=20261
2007-01-02 06:49:40 +00:00
|
|
|
|
2021-03-15 18:29:43 +00:00
|
|
|
init_report_message("dftest", &dftest_report_routines);
|
2013-12-05 09:30:22 +00:00
|
|
|
|
2005-08-25 21:29:54 +00:00
|
|
|
timestamp_set_type(TS_RELATIVE);
|
2010-05-07 06:35:17 +00:00
|
|
|
timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
|
2004-03-18 19:04:33 +00:00
|
|
|
|
2021-02-19 22:46:42 +00:00
|
|
|
/*
|
|
|
|
* Libwiretap must be initialized before libwireshark is, so that
|
|
|
|
* dissection-time handlers for file-type-dependent blocks can
|
|
|
|
* register using the file type/subtype value for the file type.
|
|
|
|
*/
|
2018-01-09 08:55:37 +00:00
|
|
|
wtap_init(TRUE);
|
2017-02-09 03:31:11 +00:00
|
|
|
|
Add a WIRESHARK_RUN_FROM_BUILD_DIRECTORY environment variable on UN*X;
if set, and if the program isn't running with additional privileges,
it'll treat the directory in which the program is found as the data
directory.
If, on Windows, the version-number subdirectory of {data
directory}\plugins doesn't exist (which is assumed to mean that the
program is being run from the build directory), or if, on UN*X,
WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set, the plugin directory is the
"plugins" subdirectory of the data directory, and all subdirectories of
that directory are scanned for plugins, as the "plugins" subdirectory of
the build directory contains subdirectories for the plugins; this means
that if we're running from the build directory, we'll find the plugins
we built in the build tree.
When generating the wireshark-filter man page, run tshark with
WIRESHARK_RUN_FROM_BUILD_DIRECTORY set, so it uses the plugins from the
build to generate the list of filters.
svn path=/trunk/; revision=20261
2007-01-02 06:49:40 +00:00
|
|
|
/* Register all dissectors; we must do this before checking for the
|
|
|
|
"-g" flag, as the "-g" flag dumps a list of fields registered
|
|
|
|
by the dissectors, and we must do it before we read the preferences,
|
|
|
|
in case any dissectors register preferences. */
|
2018-09-08 15:02:06 +00:00
|
|
|
if (!epan_init(NULL, NULL, FALSE))
|
2015-10-28 00:41:23 +00:00
|
|
|
return 2;
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2020-06-24 23:02:39 +00:00
|
|
|
/*
|
|
|
|
* Set the C-language locale to the native environment and set the
|
|
|
|
* code page to UTF-8 on Windows.
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
|
|
|
setlocale(LC_ALL, ".UTF-8");
|
|
|
|
#else
|
2001-02-01 20:21:25 +00:00
|
|
|
setlocale(LC_ALL, "");
|
2020-06-24 23:02:39 +00:00
|
|
|
#endif
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2017-04-09 19:27:57 +00:00
|
|
|
/* Load libwireshark settings from the current profile. */
|
|
|
|
epan_load_settings();
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
/* notify all registered modules that have had any of their preferences
|
|
|
|
changed either from one of the preferences file or from the command
|
|
|
|
line that its preferences have changed. */
|
|
|
|
prefs_apply_all();
|
|
|
|
|
|
|
|
/* Check for filter on command line */
|
|
|
|
if (argc <= 1) {
|
2006-11-21 00:36:35 +00:00
|
|
|
fprintf(stderr, "Usage: dftest <filter>\n");
|
2001-02-01 20:21:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get filter text */
|
|
|
|
text = get_args_as_string(argc, argv, 1);
|
|
|
|
|
|
|
|
printf("Filter: \"%s\"\n", text);
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
/* Compile it */
|
2015-01-18 10:22:19 +00:00
|
|
|
if (!dfilter_compile(text, &df, &err_msg)) {
|
|
|
|
fprintf(stderr, "dftest: %s\n", err_msg);
|
|
|
|
g_free(err_msg);
|
2001-02-01 20:21:25 +00:00
|
|
|
epan_cleanup();
|
2019-01-30 21:41:26 +00:00
|
|
|
g_free(text);
|
2001-02-01 20:21:25 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2012-07-23 17:10:13 +00:00
|
|
|
printf("\n");
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2006-04-06 20:07:16 +00:00
|
|
|
if (df == NULL)
|
|
|
|
printf("Filter is empty\n");
|
|
|
|
else
|
|
|
|
dfilter_dump(df);
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2009-09-09 16:38:09 +00:00
|
|
|
dfilter_free(df);
|
2001-02-01 20:21:25 +00:00
|
|
|
epan_cleanup();
|
2019-01-30 21:41:26 +00:00
|
|
|
g_free(text);
|
2001-02-01 20:21:25 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2004-02-21 22:00:46 +00:00
|
|
|
|
2004-04-17 01:05:00 +00:00
|
|
|
/*
|
2021-03-15 18:29:43 +00:00
|
|
|
* Report an error in command-line arguments.
|
2004-04-17 01:05:00 +00:00
|
|
|
*/
|
|
|
|
static void
|
2021-03-15 18:29:43 +00:00
|
|
|
dftest_cmdarg_err(const char *fmt, va_list ap)
|
2004-04-17 01:05:00 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "dftest: ");
|
2021-03-15 18:29:43 +00:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2004-04-17 01:05:00 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
2004-02-21 22:00:46 +00:00
|
|
|
/*
|
2021-03-15 18:29:43 +00:00
|
|
|
* Report additional information for an error in command-line arguments.
|
2004-02-21 22:00:46 +00:00
|
|
|
*/
|
2004-03-23 21:19:58 +00:00
|
|
|
static void
|
2021-03-15 18:29:43 +00:00
|
|
|
dftest_cmdarg_err_cont(const char *fmt, va_list ap)
|
2004-02-21 22:00:46 +00:00
|
|
|
{
|
2021-03-15 18:29:43 +00:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2004-04-17 01:05:00 +00:00
|
|
|
fprintf(stderr, "\n");
|
2004-02-21 22:00:46 +00:00
|
|
|
}
|
|
|
|
|
2014-10-12 18:56:12 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2014-10-12 18:56:12 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
|
|
|
|
* :indentSize=8:tabSize=8:noTabs=false:
|
|
|
|
*/
|