wslua: Use introspection API to generate constants

This commit is contained in:
João Valverde 2022-10-01 23:49:48 +01:00
parent ab96dffa3f
commit 9faaf0ecff
5 changed files with 161 additions and 186 deletions

View File

@ -363,9 +363,7 @@ void proto_report_dissector_bug(const char *format, ...)
* values are encoded in all but the top bit (which is the byte-order
* bit, required for FT_UINT_STRING and for UCS-2 and UTF-16 strings)
* and the bottom bit (which we ignore for now so that programs that
* pass TRUE for the encoding just do ASCII). (The encodings are given
* directly as even numbers in hex, so that make-init-lua.py can just
* turn them into numbers for use in init.lua.)
* pass TRUE for the encoding just do ASCII).
*
* We don't yet process ASCII and UTF-8 differently. Ultimately, for
* ASCII, all bytes with the 8th bit set should be mapped to some "this
@ -588,10 +586,6 @@ void proto_report_dissector_bug(const char *format, ...)
*
* ENC_TIME_CLASSIC_MAC_OS_SECS - 4-8 bytes, representing a count of seconds
* since January 1, 1904, 00:00:00 UTC.
*
* The backwards-compatibility names are defined as hex numbers so that
* the script to generate init.lua will add them as global variables,
* along with the new names.
*/
#define ENC_TIME_SECS_NSECS 0x00000000
#define ENC_TIME_TIMESPEC 0x00000000 /* for backwards source compatibility */
@ -656,49 +650,44 @@ void proto_report_dissector_bug(const char *format, ...)
/** FIELD_DISPLAY_E_MASK selects the field_display_e value. */
#define FIELD_DISPLAY_E_MASK 0xFF
/*
* Note that this enum values are parsed in make-init-lua.py so make sure
* any changes here still makes valid entries in init.lua.
* XXX The script requires the equals sign.
*/
typedef enum {
BASE_NONE = 0, /**< none */
/* Integral and float types */
BASE_DEC = 1, /**< decimal [integer, float] */
BASE_HEX = 2, /**< hexadecimal [integer, float] */
BASE_OCT = 3, /**< octal [integer] */
BASE_DEC_HEX = 4, /**< decimal (hexadecimal) [integer] */
BASE_HEX_DEC = 5, /**< hexadecimal (decimal) [integer] */
BASE_CUSTOM = 6, /**< call custom routine to format [integer, float] */
BASE_EXP = 7, /**< exponential [float] */
BASE_DEC, /**< decimal [integer, float] */
BASE_HEX, /**< hexadecimal [integer, float] */
BASE_OCT, /**< octal [integer] */
BASE_DEC_HEX, /**< decimal (hexadecimal) [integer] */
BASE_HEX_DEC, /**< hexadecimal (decimal) [integer] */
BASE_CUSTOM, /**< call custom routine to format [integer, float] */
BASE_EXP, /**< exponential [float] */
/* Byte separators */
SEP_DOT = 8, /**< hexadecimal bytes with a period (.) between each byte */
SEP_DASH = 9, /**< hexadecimal bytes with a dash (-) between each byte */
SEP_COLON = 10, /**< hexadecimal bytes with a colon (:) between each byte */
SEP_SPACE = 11, /**< hexadecimal bytes with a space between each byte */
SEP_DOT, /**< hexadecimal bytes with a period (.) between each byte */
SEP_DASH, /**< hexadecimal bytes with a dash (-) between each byte */
SEP_COLON, /**< hexadecimal bytes with a colon (:) between each byte */
SEP_SPACE, /**< hexadecimal bytes with a space between each byte */
/* Address types */
BASE_NETMASK = 12, /**< Used for IPv4 address that shouldn't be resolved (like for netmasks) */
BASE_NETMASK, /**< Used for IPv4 address that shouldn't be resolved (like for netmasks) */
/* Port types */
BASE_PT_UDP = 13, /**< UDP port */
BASE_PT_TCP = 14, /**< TCP port */
BASE_PT_DCCP = 15, /**< DCCP port */
BASE_PT_SCTP = 16, /**< SCTP port */
BASE_PT_UDP, /**< UDP port */
BASE_PT_TCP, /**< TCP port */
BASE_PT_DCCP, /**< DCCP port */
BASE_PT_SCTP, /**< SCTP port */
/* OUI types */
BASE_OUI = 17, /**< OUI resolution */
BASE_OUI, /**< OUI resolution */
/* Time types */
ABSOLUTE_TIME_LOCAL = 18, /**< local time in our time zone, with month and day */
ABSOLUTE_TIME_UTC = 19, /**< UTC, with month and day */
ABSOLUTE_TIME_DOY_UTC = 20, /**< UTC, with 1-origin day-of-year */
ABSOLUTE_TIME_NTP_UTC = 21, /**< UTC, with "NULL" when timestamp is all zeros */
ABSOLUTE_TIME_LOCAL, /**< local time in our time zone, with month and day */
ABSOLUTE_TIME_UTC, /**< UTC, with month and day */
ABSOLUTE_TIME_DOY_UTC, /**< UTC, with 1-origin day-of-year */
ABSOLUTE_TIME_NTP_UTC, /**< UTC, with "NULL" when timestamp is all zeros */
/* String types */
BASE_STR_WSP = 22, /**< Replace all whitespace characters (newline, formfeed, etc) with "space". */
BASE_STR_WSP, /**< Replace all whitespace characters (newline, formfeed, etc) with "space". */
} field_display_e;
#define FIELD_DISPLAY(d) ((d) & FIELD_DISPLAY_E_MASK)
@ -899,7 +888,6 @@ typedef proto_node proto_item;
* the bottom up.
*/
/* do not modify the PI_SEVERITY_MASK name - it's used by make-init-lua.py */
/* expert severities */
#define PI_SEVERITY_MASK 0x00F00000 /**< mask usually for internal use only! */
/** Packet comment */
@ -913,7 +901,6 @@ typedef proto_node proto_item;
/** Serious problems, e.g. a malformed packet */
#define PI_ERROR 0x00800000
/* do not modify the PI_GROUP_MASK name - it's used by make-init-lua.py */
/* expert "event groups" */
#define PI_GROUP_MASK 0xFF000000 /**< mask usually for internal use only! */
/** The protocol field has a bad checksum, usually uses PI_WARN severity */

View File

@ -25,24 +25,9 @@ extern "C" {
*
* XXX - stats should be able to register additional menu groups, although
* the question then would be "in what order should they appear in the menu?"
*
* NOTE: the enum below is parsed by epan/wslua/make-init-lua.py in order
* to generate usable values for Lua scripts to use, so they can add to
* the menus in the GUI. The perl script's regex is such that the following
* prefixes must only appear once in this list:
* REGISTER_ANALYZE_GROUP_CONVERSATION
* REGISTER_STAT_GROUP_CONVERSATION
* REGISTER_STAT_GROUP_RESPONSE
* REGISTER_STAT_GROUP_ENDPOINT
* In other words, because there is a REGISTER_STAT_GROUP_RESPONSE_TIME, you cannot
* add a REGISTER_STAT_GROUP_RESPONSE nor a REGISTER_STAT_GROUP_RESPONSE_FOOBAR
* because they use the same "REGISTER_STAT_GROUP_RESPONSE" prefix.
* Also, do NOT change the names in the enum - you can add, but not remove.
* If you do, legacy scripts will break. (which is why the perl script regex isn't better)
*/
/*! Statistics groups. Used for UI menu layout. */
/* This is parsed by make-init-lua.py, so we can't do anything fancy here. */
typedef enum register_stat_group_e {
REGISTER_PACKET_ANALYZE_GROUP_UNSORTED, /*!< Unsorted packet analysis */
REGISTER_ANALYZE_GROUP_CONVERSATION_FILTER, /*!< Conversation filters. Unused? */

View File

@ -21,6 +21,7 @@
#include <stdio.h>
#include <epan/expert.h>
#include <epan/ex-opt.h>
#include <epan/introspection.h>
#include <wsutil/privileges.h>
#include <wsutil/file_util.h>
#include <wsutil/wslog.h>
@ -828,6 +829,134 @@ wslua_allocf(void *ud _U_, void *ptr, size_t osize _U_, size_t nsize)
return g_realloc(ptr, nsize);
}
#define WSLUA_BASE_TABLE "base"
#define WSLUA_FTYPE_TABLE "ftypes"
#define WSLUA_FRAMETYPE_TABLE "frametype"
#define WSLUA_EXPERT_TABLE "expert"
#define WSLUA_EXPERT_GROUP_TABLE "group"
#define WSLUA_EXPERT_SEVERITY_TABLE "severity"
static void
add_table_symbol(const char *table, const char *name, int value)
{
/* Get table from the global environment. */
lua_getglobal(L, table);
/* Set symbol in table. */
lua_pushstring(L, name);
lua_pushnumber(L, value);
lua_settable(L, -3);
/* Pop table from stack. */
lua_pop(L, 1);
}
static void
add_global_symbol(const char *name, int value)
{
/* Set symbol in global environment. */
lua_pushnumber(L, value);
lua_setglobal(L, name);
}
static void
add_pi_severity_symbol(const char *name, int value)
{
lua_getglobal(L, WSLUA_EXPERT_TABLE);
lua_getfield(L, -1, WSLUA_EXPERT_SEVERITY_TABLE);
lua_pushnumber(L, value);
lua_setfield(L, -2, name);
lua_pop(L, 2);
}
static void
add_pi_group_symbol(const char *name, int value)
{
lua_getglobal(L, WSLUA_EXPERT_TABLE);
lua_getfield(L, -1, WSLUA_EXPERT_GROUP_TABLE);
lua_pushnumber(L, value);
lua_setfield(L, -2, name);
lua_pop(L, 2);
}
static void
add_menu_group_symbol(const char *name, int value)
{
/* Set symbol in global environment. */
lua_pushnumber(L, value);
char *str = g_strdup(name);
char *s = strstr(str, "_GROUP_");
if (s == NULL)
return;
*s = '\0';
s += strlen("_GROUP_");
char *str2 = ws_strdup_printf("MENU_%s_%s", str, s);
lua_setglobal(L, str2);
g_free(str);
g_free(str2);
}
/*
* Read introspection constants and add them according to the historical
* (sometimes arbitrary) rules of make-init-lua.py. For efficiency reasons
* we only loop the enums array once.
*/
static void
wslua_add_introspection(void)
{
const ws_enum_t *ep;
/* Add empty tables to be populated. */
lua_newtable(L);
lua_setglobal(L, WSLUA_BASE_TABLE);
lua_newtable(L);
lua_setglobal(L, WSLUA_FTYPE_TABLE);
lua_newtable(L);
lua_setglobal(L, WSLUA_FRAMETYPE_TABLE);
lua_newtable(L);
lua_pushstring(L, WSLUA_EXPERT_GROUP_TABLE);
lua_newtable(L);
lua_settable(L, -3);
lua_pushstring(L, WSLUA_EXPERT_SEVERITY_TABLE);
lua_newtable(L);
lua_settable(L, -3);
lua_setglobal(L, WSLUA_EXPERT_TABLE);
for (ep = epan_inspect_enums(); ep->symbol != NULL; ep++) {
if (g_str_has_prefix(ep->symbol, "BASE_")) {
add_table_symbol(WSLUA_BASE_TABLE, ep->symbol + strlen("BASE_"), ep->value);
}
else if (g_str_has_prefix(ep->symbol, "SEP_")) {
add_table_symbol(WSLUA_BASE_TABLE, ep->symbol + strlen("SEP_"), ep->value);
}
else if (g_str_has_prefix(ep->symbol, "ABSOLUTE_TIME_")) {
add_table_symbol(WSLUA_BASE_TABLE, ep->symbol + strlen("ABSOLUTE_TIME_"), ep->value);
}
else if (g_str_has_prefix(ep->symbol, "ENC_")) {
add_global_symbol(ep->symbol, ep->value);
}
else if (g_str_has_prefix(ep->symbol, "FT_FRAMENUM_")) {
add_table_symbol(WSLUA_FRAMETYPE_TABLE, ep->symbol + strlen("FT_FRAMENUM_"), ep->value);
}
else if (g_str_has_prefix(ep->symbol, "FT_")) {
add_table_symbol(WSLUA_FTYPE_TABLE, ep->symbol + strlen("FT_"), ep->value);
}
else if (g_str_has_prefix(ep->symbol, "PI_")) {
if (ep->value & PI_SEVERITY_MASK) {
add_pi_severity_symbol(ep->symbol + strlen("PI_"), ep->value);
}
else {
add_pi_group_symbol(ep->symbol + strlen("PI_"), ep->value);
}
/* For backward compatibility. */
add_global_symbol(ep->symbol, ep->value);
}
else if (g_str_has_prefix(ep->symbol, "REGISTER_")) {
add_menu_group_symbol(ep->symbol + strlen("REGISTER_"), ep->value);
}
}
}
void wslua_init(register_cb cb, gpointer client_data) {
gchar* filename;
const funnel_ops_t* ops = funnel_get_funnel_ops();
@ -1021,6 +1150,8 @@ void wslua_init(register_cb cb, gpointer client_data) {
/* see dissect_lua() for notes */
WSLUA_REG_GLOBAL_NUMBER(L,"DESEGMENT_ONE_MORE_SEGMENT",DESEGMENT_ONE_MORE_SEGMENT);
wslua_add_introspection();
/* load system's init.lua */
filename = get_datafile_path("init.lua");
if (( file_exists(filename))) {

View File

@ -18,11 +18,6 @@ import re
from enum import Enum
from string import Template
class ExpertState(Enum):
NONE = 0
IN_GROUP = 1
IN_SEVERITY = 2
def main():
parser = argparse.ArgumentParser(description="Generate the registration macros for Lua code.")
parser.add_argument("template", metavar='template', help="template file")
@ -36,15 +31,8 @@ def main():
'WTAP_ENCAPS': '-- Wiretap encapsulations XXX\nwtap_encaps = {',
'WTAP_TSPRECS': '-- Wiretap timestamp precision types\nwtap_tsprecs = {',
'WTAP_COMMENT_TYPES': '-- Wiretap file comment types\nwtap_comments = {',
'FT_TYPES': '-- Field Types\nftypes = {',
'FT_FRAME_TYPES': '-- Field Type FRAMENUM Types\nframetype = {',
'WTAP_REC_TYPES': '-- Wiretap record_types\nwtap_rec_types = {',
'WTAP_PRESENCE_FLAGS': '-- Wiretap presence flags\nwtap_presence_flags = {',
'BASES': '-- Display Bases\nbase = {',
'ENCODINGS': '-- Encodings',
'EXPERT': '-- Expert flags and facilities (deprecated - see \'expert\' table below)',
'EXPERT_TABLE': '-- Expert flags and facilities\nexpert = {',
'MENU_GROUPS': '-- menu groups for register_menu',
}
with open(args.template, encoding='utf-8') as tmpl_f:
@ -83,114 +71,6 @@ def main():
replacements['WTAP_REC_TYPES'] += ''.join(wtap_rec_types) + '\n}\n'
replacements['WTAP_PRESENCE_FLAGS'] += ''.join(wtap_presence_flags) + '\n}\n'
frametypes = []
ftypes = []
with open(os.path.join(src_root, 'epan', 'ftypes', 'ftypes.h'), encoding='utf-8') as ftypes_f:
for line in ftypes_f:
m = re.match(r'\s+FT_FRAMENUM_([A-Z0-9a-z_]+)\s*,', line)
if m:
idx = len(frametypes)
frametypes.append(f'\n\t["{m.group(1)}"] = {idx}');
continue
m = re.match(r'\s+FT_([A-Z0-9a-z_]+)\s*,', line)
if m:
idx = len(ftypes)
ftypes.append(f'\n\t["{m.group(1)}"] = {idx}');
replacements['FT_TYPES'] += ','.join(ftypes) + '\n}\n'
replacements['FT_FRAME_TYPES'] += ','.join(frametypes) + '\n}\n'
bases = []
encodings = []
expert = []
expert_group = []
expert_severity = []
expert_state = ExpertState.NONE
prev_comment = ''
with open(os.path.join(src_root, 'epan', 'proto.h'), encoding='utf-8') as proto_f:
for line in proto_f:
skip_this = False
m = re.match(r'\s+(?:BASE|SEP|ABSOLUTE_TIME)_([A-Z_]+)[ ]*=[ ]*([0-9]+)[,\s]+(?:\/\*\*< (.*?) \*\/)?', line)
if m:
bases.append(f'\n\t["{m.group(1)}"] = {m.group(2)}, -- {m.group(3)}')
m = re.match(r'#define\s+BASE_(RANGE_STRING)[ ]*((?:0x)?[0-9]+)[ ]+(?:\/\*\*< (.*?) \*\/)?', line)
if m:
# Handle BASE_RANGE_STRING
bases.append(f'\n\t["{m.group(1)}"] = {int(m.group(2), 16)}, -- {m.group(3)}')
m = re.match(r'^#define\s+BASE_(UNIT_STRING)[ ]*((?:0x)?[0-9]+)[ ]+(?:\/\*\*< (.*?) \*\/)?', line)
if m:
# Handle BASE_UNIT_STRING as a valid base value in Lua
bases.append(f'\n\t["{m.group(1)}"] = {int(m.group(2), 16)}, -- {m.group(3)}')
if re.match(r'#define\s+PI_GROUP_MASK ', line):
expert_state = ExpertState.IN_GROUP
skip_this = True
if re.match(r'.define\s+PI_SEVERITY_MASK ', line):
expert_state = ExpertState.IN_SEVERITY
skip_this = True
m = re.search(r'/\*\* (.*?) \*\/', line)
if m:
prev_comment = m.group(1)
m = re.match(r'#define\s+(PI_([A-Z_]+))\s+((0x)?[0-9A-Fa-f]+)', line)
if m:
name = m.group(1)
abbr = m.group(2)
value = int(m.group(3), 16)
# I'm keeping this here for backwards-compatibility
expert.append(f'\n{name} = {value}')
if not skip_this and expert_state == ExpertState.IN_GROUP:
expert_group.append(f'\n\t\t-- {prev_comment}\n\t\t["{abbr}"] = {value},')
elif not skip_this and expert_state == ExpertState.IN_SEVERITY:
expert_severity.append(f'\n\t\t-- {prev_comment}\n\t\t["{abbr}"] = {value},')
m = re.match(r'^.define\s+(ENC_[A-Z0-9_]+)\s+((0x)?[0-9A-Fa-f]+)', line)
if m:
encodings.append(f'\n{m.group(1)} = {int(m.group(2), 16)}')
menu_groups = []
in_stat_group_enum = False
with open(os.path.join(src_root, 'epan', 'stat_groups.h'), encoding='utf-8') as stat_groups_f:
for line in stat_groups_f:
# need to skip matching words in comments, and get to the enum
if re.match(r'^typedef enum register_stat_group_e \{', line):
in_stat_group_enum = True
elif re.match('^\} register_stat_group_t\;/', line):
in_stat_group_enum = False
# the problem here is we need to pick carefully, so we don't break existing scripts
if in_stat_group_enum:
m = re.search('REGISTER_([A-Z0-9_]+)_GROUP_([A-Z0-9_]+),? ', line)
if m:
idx = len(menu_groups)
menu_groups.append(f'\nMENU_{m.group(1)}_{m.group(2)} = {idx}')
replacements['BASES'] += ''.join(bases) + '\n}\n'
replacements['ENCODINGS'] += ''.join(encodings) + '\n\n'
replacements['EXPERT'] += ''.join(expert) + '\n\n'
replacements['EXPERT_TABLE'] += '\n\t-- Expert event groups\n\tgroup = {'
replacements['EXPERT_TABLE'] += ''.join(expert_group) + '\n\t},'
replacements['EXPERT_TABLE'] += '\n\t-- Expert severity levels\n\tseverity = {'
replacements['EXPERT_TABLE'] += ''.join(expert_severity) + '\n\t},\n}\n'
replacements['MENU_GROUPS'] += ''.join(menu_groups) + '\n\n'
replacements['MENU_GROUPS'] += '''\
-- 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
MENU_STAT_CONVERSATION = MENU_STAT_CONVERSATION_LIST
MENU_STAT_ENDPOINT = MENU_STAT_ENDPOINT_LIST
MENU_STAT_RESPONSE = MENU_STAT_RESPONSE_TIME
MENU_STAT_UNSORTED = MENU_PACKET_STAT_UNSORTED
'''
with open(args.output, mode='w', encoding='utf-8') as out_f:
out_f.write(template.substitute(replacements))

View File

@ -94,27 +94,19 @@ ${WTAP_TSPRECS}
${WTAP_COMMENT_TYPES}
${FT_TYPES}
-- the following table is since 2.0
${FT_FRAME_TYPES}
-- the following table is since 1.12
${WTAP_REC_TYPES}
-- the following table is since 1.11.3
${WTAP_PRESENCE_FLAGS}
${BASES}
${ENCODINGS}
${EXPERT}
-- the following table is since 1.11.3
${EXPERT_TABLE}
${MENU_GROUPS}
-- 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
MENU_STAT_CONVERSATION = MENU_STAT_CONVERSATION_LIST
MENU_STAT_ENDPOINT = MENU_STAT_ENDPOINT_LIST
MENU_STAT_RESPONSE = MENU_STAT_RESPONSE_TIME
MENU_STAT_UNSORTED = MENU_PACKET_STAT_UNSORTED
-- the possible values for Pinfo's p2p_dir attribute
P2P_DIR_UNKNOWN = -1