wslua: Use wiretap introspection

This commit is contained in:
João Valverde 2022-10-02 11:03:23 +01:00
parent 3c99478cef
commit 91f7762fad
6 changed files with 40 additions and 130 deletions

View File

@ -2299,7 +2299,7 @@ endif()
# epan/wslua/CMakeLists.txt
if(LUA_FOUND AND ENABLE_LUA)
set(_lua_files
"${CMAKE_BINARY_DIR}/epan/wslua/init.lua"
"${CMAKE_SOURCE_DIR}/epan/wslua/init.lua"
"${CMAKE_SOURCE_DIR}/epan/wslua/console.lua"
"${CMAKE_SOURCE_DIR}/epan/wslua/dtd_gen.lua"
)
@ -2312,7 +2312,6 @@ if(LUA_FOUND AND ENABLE_LUA)
"${_lua_file}"
"${DATAFILE_DIR}/${_lua_filename}"
DEPENDS
wsluaauxiliary
"${_lua_file}"
)
endforeach()

View File

@ -48,20 +48,6 @@ epan/wslua/CMakeLists.txt. You also have to add the module name into
docbook/user-guide.xml and docbook/wsluarm.xml, and the source files into
docbook/CMakeLists.txt, to get it to be generated in the user guide.
Another Python3 script is used as well, called 'make-init-lua.py', which
generates the init.lua script. A large part of it deals with exposing #define
values into the Lua global table, or sub-tables. Unfortunately not all of
them are put in sub-tables, which means the global Lua table is quite polluted
now. If you add new ones in here, please think of putting them in a subtable,
as they are for wtap, ftypes, and base. For example, there are several put in
as 'PI_' prefixed names, such as 'PI_SEVERITY_MASK = 15728640'. The fact they
all have a common 'PI_' prefix should be an indicator they can be put in a
table named PI, or PacketInfo. Just because C-code doesn't have namespaces,
doesn't mean Lua can't. This has now been fixed, and the PI_* names are now in
two separate subtables of a table named 'expert', as 'expert.group' and
'expert.severity' subtables. Follow that model in 'make-init-lua.py'.
Due to those documentation and registration scripts, you MUST follow some very
specific conventions in the functions you write to expose C-side code to Lua,
as described in this document.

View File

@ -109,34 +109,11 @@ add_custom_target(
set_target_properties(register_wslua PROPERTIES FOLDER "Libs/epan/wslua")
add_custom_command(
OUTPUT init.lua
COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/make-init-lua.py
${CMAKE_CURRENT_SOURCE_DIR}/template-init.lua
init.lua
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/make-init-lua.py
${CMAKE_CURRENT_SOURCE_DIR}/template-init.lua
${CMAKE_SOURCE_DIR}/epan/ftypes/ftypes.h
${CMAKE_SOURCE_DIR}/wiretap/wtap.h
${CMAKE_SOURCE_DIR}/epan/epan.h
${CMAKE_SOURCE_DIR}/epan/stat_groups.h
)
add_custom_target(
wsluaauxiliary ALL
DEPENDS
init.lua
)
set_target_properties(wsluaauxiliary PROPERTIES FOLDER "Auxiliary")
install(
FILES
init.lua
console.lua
dtd_gen.lua
${CMAKE_CURRENT_BINARY_DIR}/init.lua
DESTINATION
${CMAKE_INSTALL_DATADIR}
)

View File

@ -76,7 +76,8 @@ function package.prepend_path(name)
package.path = name .. sep .. "?.lua;" .. package.path
end
${WTAP_ENCAPS}
-- for backward compatibility
wtap = wtap_encaps
--
-- Generate the wtap_filetypes items for file types, for backwards
@ -90,16 +91,6 @@ ${WTAP_ENCAPS}
--
wtap_filetypes = get_wtap_filetypes()
${WTAP_TSPRECS}
${WTAP_COMMENT_TYPES}
-- the following table is since 1.12
${WTAP_REC_TYPES}
-- the following table is since 1.11.3
${WTAP_PRESENCE_FLAGS}
-- Old / deprecated menu groups. These shoudn't be used in new code.
MENU_ANALYZE_UNSORTED = MENU_PACKET_ANALYZE_UNSORTED
MENU_ANALYZE_CONVERSATION = MENU_ANALYZE_CONVERSATION_FILTER

View File

@ -22,6 +22,7 @@
#include <epan/expert.h>
#include <epan/ex-opt.h>
#include <epan/introspection.h>
#include <wiretap/introspection.h>
#include <wsutil/privileges.h>
#include <wsutil/file_util.h>
#include <wsutil/wslog.h>
@ -835,6 +836,11 @@ wslua_allocf(void *ud _U_, void *ptr, size_t osize _U_, size_t nsize)
#define WSLUA_EXPERT_TABLE "expert"
#define WSLUA_EXPERT_GROUP_TABLE "group"
#define WSLUA_EXPERT_SEVERITY_TABLE "severity"
#define WSLUA_WTAP_ENCAPS_TABLE "wtap_encaps"
#define WSLUA_WTAP_TSPREC_TABLE "wtap_tsprecs"
#define WSLUA_WTAP_COMMENTS_TABLE "wtap_comments"
#define WSLUA_WTAP_RECTYPES_TABLE "wtap_rec_types"
#define WSLUA_WTAP_PRESENCE_FLAGS_TABLE "wtap_presence_flags"
static void
add_table_symbol(const char *table, const char *name, int value)
@ -953,7 +959,37 @@ wslua_add_introspection(void)
else if (g_str_has_prefix(ep->symbol, "REGISTER_")) {
add_menu_group_symbol(ep->symbol + strlen("REGISTER_"), ep->value);
}
}
/* Add empty tables to be populated. */
lua_newtable(L);
lua_setglobal(L, WSLUA_WTAP_ENCAPS_TABLE);
lua_newtable(L);
lua_setglobal(L, WSLUA_WTAP_TSPREC_TABLE);
lua_newtable(L);
lua_setglobal(L, WSLUA_WTAP_COMMENTS_TABLE);
lua_newtable(L);
lua_setglobal(L, WSLUA_WTAP_RECTYPES_TABLE);
lua_newtable(L);
lua_setglobal(L, WSLUA_WTAP_PRESENCE_FLAGS_TABLE);
for (ep = wtap_inspect_enums(); ep->symbol != NULL; ep++) {
if (g_str_has_prefix(ep->symbol, "WTAP_ENCAP_")) {
add_table_symbol(WSLUA_WTAP_ENCAPS_TABLE, ep->symbol + strlen("WTAP_ENCAP_"), ep->value);
}
else if (g_str_has_prefix(ep->symbol, "WTAP_TSPREC_")) {
add_table_symbol(WSLUA_WTAP_TSPREC_TABLE, ep->symbol + strlen("WTAP_TSPREC_"), ep->value);
}
else if (g_str_has_prefix(ep->symbol, "WTAP_COMMENT_")) {
add_table_symbol(WSLUA_WTAP_COMMENTS_TABLE, ep->symbol + strlen("WTAP_COMMENT_"), ep->value);
}
else if (g_str_has_prefix(ep->symbol, "REC_TYPE_")) {
add_table_symbol(WSLUA_WTAP_RECTYPES_TABLE, ep->symbol + strlen("REC_TYPE_"), ep->value);
}
else if (g_str_has_prefix(ep->symbol, "WTAP_HAS_")) {
add_table_symbol(WSLUA_WTAP_PRESENCE_FLAGS_TABLE, ep->symbol + strlen("WTAP_HAS_"), ep->value);
}
}
}

View File

@ -1,79 +0,0 @@
#!/usr/bin/env python3
#
# make-init-lua.py
#
# By Gerald Combs <gerald@wireshark.org>
# Based on make-init-lua.pl by Luis E. Garcia Onatnon <luis.ontanon@gmail.com>
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
import argparse
import os
import re
from enum import Enum
from string import Template
def main():
parser = argparse.ArgumentParser(description="Generate the registration macros for Lua code.")
parser.add_argument("template", metavar='template', help="template file")
parser.add_argument("output", metavar='output', help="output file")
args = parser.parse_args()
this_dir = os.path.dirname(__file__)
src_root = os.path.join(this_dir, '..', '..')
replacements = {
'WTAP_ENCAPS': '-- Wiretap encapsulations XXX\nwtap_encaps = {',
'WTAP_TSPRECS': '-- Wiretap timestamp precision types\nwtap_tsprecs = {',
'WTAP_COMMENT_TYPES': '-- Wiretap file comment types\nwtap_comments = {',
'WTAP_REC_TYPES': '-- Wiretap record_types\nwtap_rec_types = {',
'WTAP_PRESENCE_FLAGS': '-- Wiretap presence flags\nwtap_presence_flags = {',
}
with open(args.template, encoding='utf-8') as tmpl_f:
template = Template(tmpl_f.read())
wtap_encaps = []
wtap_tsprecs = []
wtap_comment_types = []
wtap_rec_types = []
wtap_presence_flags = []
with open(os.path.join(src_root, 'wiretap', 'wtap.h'), encoding='utf-8') as wtap_f:
for line in wtap_f:
m = re.search(r'#define WTAP_ENCAP_([A-Z0-9_]+)\s+(-?\d+)', line)
if m:
wtap_encaps.append(f'\n\t["{m.group(1)}"] = {m.group(2)}')
m = re.search(r'#define WTAP_TSPREC_([A-Z0-9_]+)\s+(\d+)', line)
if m:
wtap_tsprecs.append(f'\n\t["{m.group(1)}"] = {m.group(2)}')
m = re.search(r'#define WTAP_COMMENT_([A-Z0-9_]+)\s+(0x\d+)', line)
if m:
wtap_comment_types.append(f'\n\t["{m.group(1)}"] = {m.group(2)}')
m = re.search(r'#define REC_TYPE_([A-Z0-9_]+)\s+(\d+)\s+\/\*\*<([^\*]+)\*\/', line)
if m:
wtap_rec_types.append(f'\n\t["{m.group(1)}"] = {m.group(2)}, --{m.group(3)}')
m = re.search(r'#define WTAP_HAS_([A-Z0-9_]+)\s+(0x\d+)\s+\/\*\*<([^\*]+)\*\/', line)
if m:
wtap_presence_flags.append(f'\n\t["{m.group(1)}"] = {int(m.group(2), 16)}, --{m.group(3)}')
replacements['WTAP_ENCAPS'] += ','.join(wtap_encaps) + '\n}\nwtap = wtap_encaps -- for bw compatibility\n'
replacements['WTAP_TSPRECS'] += ','.join(wtap_tsprecs) + '\n}\n'
replacements['WTAP_COMMENT_TYPES'] += ','.join(wtap_comment_types) + '\n}\n'
replacements['WTAP_REC_TYPES'] += ''.join(wtap_rec_types) + '\n}\n'
replacements['WTAP_PRESENCE_FLAGS'] += ''.join(wtap_presence_flags) + '\n}\n'
with open(args.output, mode='w', encoding='utf-8') as out_f:
out_f.write(template.substitute(replacements))
if __name__ == '__main__':
main()