From 9b797e97a2107cd0d5364634d517390e7d6910af Mon Sep 17 00:00:00 2001 From: David Perry Date: Fri, 27 Jan 2023 10:41:07 -0500 Subject: [PATCH] generate-dissector.py: allow creating plugin Tweak the script used for creating a new skeleton dissector, to allow it to create the dissector in `plugins/epan/PROTOSHORTNAME` instead of in `epan/dissectors`. Handles modifying the appropriate CMake file in the appropriate way, and generates the plugin's `CMakeLists.txt` if needed. --- doc/CMakeLists-PROTOABBREV.txt | 71 ++++++++++++++++++++++++++++++++++ tools/generate-dissector.py | 58 +++++++++++++++++---------- 2 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 doc/CMakeLists-PROTOABBREV.txt diff --git a/doc/CMakeLists-PROTOABBREV.txt b/doc/CMakeLists-PROTOABBREV.txt new file mode 100644 index 0000000000..f8a62b9eda --- /dev/null +++ b/doc/CMakeLists-PROTOABBREV.txt @@ -0,0 +1,71 @@ +# CMakeLists.txt +# +# Wireshark - Network traffic analyzer +# Copyright YEARS, YOUR_NAME +# +# SPDX-License-Identifier: LICENSE +# + +include(WiresharkPlugin) + +# Plugin name and version info (major minor micro extra) +set_module_info(PROTOABBREV 0 0 1 0) + +set(DISSECTOR_SRC + # Source files that directly dissect data + packet-PROTOABBREV.c +) + +set(DISSECTOR_SUPPORT_SRC + # Source files that provide additional routines +) + +set(PLUGIN_FILES + plugin.c + ${DISSECTOR_SRC} + ${DISSECTOR_SUPPORT_SRC} +) + +set_source_files_properties( + ${PLUGIN_FILES} + PROPERTIES + COMPILE_FLAGS "${WERROR_COMMON_FLAGS}" +) + +register_plugin_files(plugin.c + plugin + ${DISSECTOR_SRC} + ${DISSECTOR_SUPPORT_SRC} +) + +add_wireshark_plugin_library(PROTOABBREV epan) + +target_link_libraries(PROTOABBREV epan) + +install_plugin(PROTOABBREV epan) + +file(GLOB DISSECTOR_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.h") +CHECKAPI( + NAME + PROTOABBREV + SWITCHES + --group dissectors-prohibited + --group dissectors-restricted + SOURCES + ${DISSECTOR_SRC} + ${DISSECTOR_SUPPORT_SRC} + ${DISSECTOR_HEADERS} +) + +# +# Editor modelines - https://www.wireshark.org/tools/modelines.html +# +# 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: +# diff --git a/tools/generate-dissector.py b/tools/generate-dissector.py index f8b722488a..4d8ab37dde 100755 --- a/tools/generate-dissector.py +++ b/tools/generate-dissector.py @@ -27,21 +27,29 @@ parser.add_argument("--email", help="The email address of the author", required= parser.add_argument("--protoname", help="The name of the protocol", required=True) parser.add_argument("--protoshortname", help="The protocol short name", required=True) parser.add_argument("--protoabbrev", help="The protocol abbreviation", required=True) -parser.add_argument("--license", help="The license for this dissector (please use a SPDX-License-Identifier). If omitted, GPL-2.0-or-later will be used") -parser.add_argument("--years", help="Years of validity for the license. If omitted, the current year will be used") +parser.add_argument("--license", help="The license for this dissector (please use a SPDX-License-Identifier). If omitted, %(default)s will be used", default="GPL-2.0-or-later") +parser.add_argument("--years", help="Years of validity for the license. If omitted, the current year will be used", default=str(datetime.now().year)) parser.add_argument("-f", "--force", action='store_true', help="Force overwriting the dissector file if it already exists") +parser.add_argument("-p", "--plugin", action='store_true', help="Create as a plugin. Default is to create in epan") def wsdir(): return os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) +def output_dir(args): + if args.plugin: + os.makedirs(os.path.join(wsdir(), "plugins/epan/" + args.protoabbrev), exist_ok=True) + return os.path.join(wsdir(), "plugins/epan/" + args.protoabbrev) + return os.path.join(wsdir(), "epan/dissectors") + + def output_file(args): - return os.path.join(wsdir(), "epan/dissectors/packet-" + args.protoabbrev + ".c") + return os.path.join(output_dir(args), "packet-" + args.protoabbrev + ".c") -def read_skeleton(): - skeletonfile = os.path.join(wsdir(), "doc/packet-PROTOABBREV.c") +def read_skeleton(filename): + skeletonfile = os.path.join(wsdir(), "doc/" + filename) print("Reading skeleton file: " + skeletonfile) return open(skeletonfile).read() @@ -68,17 +76,9 @@ def replace_fields(buffer, args): .replace("PI_GROUP", "PI_PROTOCOL")\ .replace("PI_SEVERITY", "PI_ERROR")\ .replace("TEST_EXPERT_condition", "0")\ - .replace("const char *subtree", "\"\"") - - if args.license: - output = output.replace("LICENSE", args.license) - else: - output = output.replace("LICENSE", "GPL-2.0-or-later") - - if args.years: - output = output.replace("YEARS", args.years) - else: - output = output.replace("YEARS", str(datetime.now().year)) + .replace("const char *subtree", "\"\"")\ + .replace("LICENSE", args.license)\ + .replace("YEARS", args.years) return output @@ -92,10 +92,16 @@ def write_dissector(buffer, args): def patch_makefile(args): - cmakefile = os.path.join(wsdir(), "epan/dissectors/CMakeLists.txt") + if args.plugin: + cmakefile = os.path.join(wsdir(), "CMakeLists.txt") + patchline = "\t\tplugins/epan/" + args.protoabbrev + groupstart = "set(PLUGIN_SRC_DIRS" + else: + cmakefile = os.path.join(wsdir(), "epan/dissectors/CMakeLists.txt") + patchline = "\t${CMAKE_CURRENT_SOURCE_DIR}/packet-" + args.protoabbrev + ".c" + groupstart = "set(DISSECTOR_SRC" print("Patching makefile: " + cmakefile) output = "" - patchline = "${CMAKE_CURRENT_SOURCE_DIR}/packet-" + args.protoabbrev + ".c" in_group = False patched = False for line in open(cmakefile): @@ -103,15 +109,24 @@ def patch_makefile(args): if in_group and line_strip == ")": in_group = False if in_group and not patched and line_strip > patchline: - output += "\t" + patchline + "\n" + output += patchline + "\n" patched = True - if line_strip == "set(DISSECTOR_SRC": + if line_strip == groupstart: in_group = True if line_strip != patchline: output += line open(cmakefile, "w").write(output) +def write_plugin_makefile(args): + if not args.plugin: + return True + buffer = replace_fields(read_skeleton("CMakeLists-PROTOABBREV.txt"), args) + ofile = os.path.join(output_dir(args), "CMakeLists.txt") + print("Writing output file: " + ofile) + return open(ofile, "w").write(buffer) + + def print_header(): print("") print("**************************************************") @@ -136,7 +151,8 @@ def print_trailer(args): if __name__ == '__main__': print_header() args = parser.parse_args() - buffer = replace_fields(read_skeleton(), args) + buffer = replace_fields(read_skeleton("packet-PROTOABBREV.c"), args) write_dissector(buffer, args) patch_makefile(args) + write_plugin_makefile(args) print_trailer(args)