3 This is a HOWTO for adding support for new Lua hooks/functions/abilities in
4 Wireshark. If you see any errors or have any improvements, submit patches -
5 free software is a community effort....
7 This is NOT a guide for how to write Lua plugins - that's documented already
8 on the Wireshark webpages.
10 Contributors to this README:
11 Hadriel Kaplan <hadrielk[AT]yahoo.com>
13 ==============================================================================
17 The way Wireshark exposes functions for Lua is generally based on a
18 callback/event model, letting Lua plugins register their custom Lua functions
19 into event callbacks. C-based "objects" are exposed as Lua tables with
20 typical Lua USERDATA pointer dispatching, plain C-functions are registered as
21 such in Lua, and C-based enums/variables are registered into Lua as table
22 key=value (usually... though rarely they're registered as array indexed
23 values). All of that is very typical for applications that expose things
24 into a Lua scripting environment.
26 The details that make it a little different are (1) the process by which the
27 code is bound/registered into Lua, and (2) the API documentation generator.
28 Wireshark uses C-macros liberally, both for the usual reasons as well as for
29 the binding generator and documentation generator scripts. The macros are
30 described within this document.
32 The API documentation is auto-generated from a Python script called 'make-
33 wsluarm.py', which searches C-files for the known macros and generates
34 appropriate AsciiDoc documentation from them. This includes using the C
35 comments after the macros for the API document info.
37 Likewise, another script called 'make-reg.py' generates the C-files
38 'register_wslua.c' and 'declare_wslua.h', based on the C-macros it searches
39 for in existing source files. The code this script auto-generates is
40 what actually registers some classes/functions into Lua - you don't have to
41 write your own registration functions to get your new functions/classes into
42 Lua tables. (you can do so, but it's not advisable)
44 Both of the scripts above are given the C-source files to search through
45 by the make process, generated from the lists in epan/wslua/CMakeLists.txt.
46 Naturally if you add new source files, you need to add them to the list in
47 epan/wslua/CMakeLists.txt. You also have to add the source files into
48 doc/CMakeLists.txt, to get it to be generated in the user guide.
50 Due to those documentation and registration scripts, you MUST follow some very
51 specific conventions in the functions you write to expose C-side code to Lua,
52 as described in this document.
54 Naming conventions/rules:
56 Class/object names must be UpperCamelCase, no numbers/underscores.
57 Function and method names must be lower_underscore_case, no numbers.
58 Constants/enums must be ALLCAPS, and can have numbers.
60 The above rules are more than merely conventions - the scripts which
61 auto-generate stuff use regex patterns that require the naming syntax to be
64 ==============================================================================
66 Documenting things for the API docs:
68 As explained previously, the API documentation is auto-generated from a
69 Python script called 'make-wsluarm.py', which searches C-files for the known
70 macros and generates appropriate HTML documentation from them. This includes
71 using the C-comments after the macros for the API document info. The comments
72 are extremely important, because the API documentation is what most Lua script
73 authors will see - do *not* expect them to go looking through the C-source code
76 Please make sure to at least use the '@since' version notification markup
77 in your comments, to let users know when the new class/function/etc. you
78 created became available.
80 Because documentation is so important, the make-wsluarm.py script supports
81 specific markup syntax in comments, and converts them to XML and ultimately
82 into the various documentation formats. The markup syntax is documented in
83 the top comments in make-wsluarm.py, but are repeated here as well:
84 - two (or more) line breaks in comments result in separate paragraphs
85 - all '&' are converted into their entity names, except inside urls
86 - all '<', and '>' are converted into their entity names everywhere
87 - any word(s) wrapped in one star, e.g., *foo bar*, become italics
88 - any word(s) wrapped in two stars, e.g., **foo bar**, become bold
89 - any word(s) wrapped in backticks, e.g., `foo bar`, become bold (for now)
90 - any word(s) wrapped in two backticks, e.g., ``foo bar``, become one backtick
91 - any "[[url]]" becomes an XML ulink with the url as both the url and text
92 - any "[[url|text]]" becomes an XML ulink with the url as the url and text as text
93 - any indent with a single leading star '*' followed by space is a bulleted list item
94 reducing indent or having an extra linebreak stops the list
95 - any indent with a leading digits-dot followed by space, i.e. "1. ", is a numbered list item
96 reducing indent or having an extra linebreak stops the list
97 - supports meta-tagged info inside comment descriptions as follows:
98 * a line starting with "@note" or "Note:" becomes an XML note line
99 * a line starting with "@warning" or "Warning:" becomes an XML warning line
100 * a line starting with "@version" or "@since" becomes a "Since:" line
101 * a line starting with "@code" and ending with "@endcode" becomes an
102 XML programlisting block, with no indenting/parsing within the block
103 The above '@' commands are based on Doxygen commands
106 ==============================================================================
108 Some implementation details:
110 Creating new C-classes for Lua:
112 Explaining the Lua class/object model and how it's bound to C-code functions
113 and data types is beyond the scope of this document; if you don't already know
114 how that works, I suggest you start reading lua-users.org's wiki, and
115 lua.org's free reference manual.
117 Wireshark generally uses a model close to the typical binding
118 model: 'registering' class methods and metamethods, pushing objects into Lua
119 by applying the class' metatable to the USERDATA, etc. This latter part is
120 mostly handled for you by the C-macro's created by WSLUA_CLASS_DEFINE, such as
121 push/check, described later in this document.
123 The actual way methods are dispatched is a little different from normal Lua
124 bindings, because attributes are supported as well (see next section). The
125 details won't be covered in this document - they're documented in the code
126 itself in: wslua_internals.c above the wslua_reg_attributes function.
128 Registering a class requires you to write some code: a WSLUA_METHODS table,
129 a WSLUA_META table, and a registration function. The WSLUA_METHODS table is an
130 array of luaL_Reg structs, which map a string name that will be the function's
131 name in Lua, to a C-function pointer which is the C-function to be invoked by
132 Lua when the user calls the name. Instead of defining this array of structs
133 explicitly using strings and function names, you should use the WSLUA_METHODS
134 macro name for the array, and use WSLUA_CLASS_FNREG macro for each entry.
135 The WSLUA_META table follows the same behavior, with the WSLUA_CLASS_MTREG
136 macro for each entry. Make sure your C-function names use two underscores
137 instead of one (for instance, ClassName__tostring).
139 Once you've created the appropriate array tables, define a registration
140 function named 'ClassName_register', where 'ClassName'is your class name, the
141 same one used in WSLUA_CLASS_DEFINE. The make-reg.py script will search
142 your file for WSLUA_CLASS_DEFINE, and it generates a register_wslua.c which
143 will call your ClassName_register function during Wireshark initialization.
144 Define a wslua_class structure which describes the class and register this in
145 your ClassName_register function using one of:
146 - wslua_register_classinstance_meta to create a metatable which allows
147 instances of a class to have methods and attributes. C code can create such
148 instances by setting the metatable on userdata, the class itself is not
149 directly visible in the Lua scope.
150 - wslua_register_class to additionally expose a class with static functions
151 that is also directly visible in the Lua global scope.
152 Also, you should read the 'Memory management model' section later in this
155 Class member variable attributes (getters/setters):
157 The current implementation does not follow a single/common class-variable
158 attribute accessor model for the Lua API: some class member values are
159 populated/retrieved when a table field attribute is used that triggers the
160 __index or __newindex metamethods, and others are accessed through explicit
161 getter/setter method functions. In other words from a Lua code perspective
162 some class object variables are retrieves as 'foo = myObj.var', while others
163 are done as 'foo = myObj.getVar()'.
165 From the C-side code perspective, some classes register no real method
166 functions but just have attributes (and use the WSLUA_ATTRIBUTE documentation
167 model for them). For example the FieldInfo class in wslua_field.c does this.
168 Other classes provide access to member variable through getter/setter method
169 functions (and thus use the WSLUA_METHOD documentation model). For example
170 the TvbRange class in wslua_tvb.c does this. Using the latter model of having
171 a getter/setter method function allows one to pass multiple arguments, whereas
172 the former __index/__newindex metamethod model does not. Both models are
173 fairly common in Lua APIs, although having a mixture of both in the same API
174 probably isn't. There is even a third model in use: pre-loading the member
175 fields of the class table with the values, instead of waiting for the Lua
176 script to access a particular one to retrieve it; for example the Listener tap
177 extractors table is pre-populated (see files 'wslua_listener.c' and 'taps'
178 which through the make-taps.py Python3 script creates 'taps_wslua.c'). The
179 downside of that approach is the performance impact, filling fields the Lua
180 script may never access. Lastly, the Field, FieldInfo, and Tvb's ByteArray
181 type each provide a __call metamethod as an accessor - I strongly suggest you
182 do NOT do that, as it's not a common model and will confuse people since it
183 doesn't follow the model of the other classes in Wireshark.
185 Attributes are handled internally like this:
187 -- invoked on myObj.myAttribute
188 function myObj.__metatable:__index(key)
189 if "getter for key exists" then
191 elseif "method for key exists" then
192 -- ensures that myObj.myMethod() works
195 error("no such property error message")
198 -- invoked on myObj.myAttribute = 1
199 function myObj.__metatable:__newindex(key, value)
200 if "setter for key exists" then
201 return setter(self, value)
203 error("no such property error message")
207 To add getters/setters in C, initialize the "attrs" member of the wslua_class
208 structure. This should contain an array table similar to the WSLUA_METHODS and
209 WSLUA_META tables, except using the macro name WSLUA_ATTRIBUTES. Inside this
210 array, each entry should use one of the following macros: WSLUA_ATTRIBUTE_ROREG,
211 WSLUA_ATTRIBUTE_WOREG, or WSLUA_ATTRIBUTE_RWREG. Those provide the hooks for
212 a getter-only, setter-only, or both getter and setter function. The functions
213 themselves need to follow a naming scheme of ClassName_get_attributename(),
214 or ClassName_set_attributename(), for the respective getter vs. setter function.
215 Trivial getters/setters have macros provided to make this automatic, for things
216 such as getting numbers, strings, etc. The macros are in wslua.h. For example,
217 the WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(Foo,bar,choo) macro creates a getter
218 function to get the boolean value of the Class Foo's choo member variable, as
219 the Lua attribute named 'bar'.
221 Callback function registration:
223 For some callbacks, there are register_* Lua global functions, which take a
224 user-defined Lua function name as the argument - the one to be hooked into
225 that event. Unlike in most Lua APIs, there's a unique register_foo() function
226 for each event type, instead of a single register() with the event as an
227 argument. For example there's a register_postdissector() function. In some
228 cases the Lua functions are invoked based on a pre-defined function-name model
229 instead of explicit register_foo(), whereby a C-object looks for a defined
230 member variable in the Registry that represents a Lua function created by the
231 plugin. This would be the case if the Lua plugin had defined a pre-defined
232 member key of its object's table in Lua, for that purpose. For example if the
233 Lua plugin sets the 'reset' member of the Listener object table to a function,
234 then Wireshark creates a Registry entry for that Lua function, and executes
235 that Lua function when the Listener resets. (see the example Listener Lua
236 script in the online docs) That model is only useful if the object can only be
237 owned by one plugin so only one function is ever hooked, obviously, and thus
238 only if it's created by the Lua plugin (e.g., Listener.new()).
240 Creating new Listener tap types:
242 The Listener object is one of the more complicated ones. When the Lua script
243 creates a Listener (using Listener.new()), the code creates and returns a tap
244 object. The type of tap is based on the passed-in argument to Listener.new(),
245 and it creates a Lua table of the tap member variables. That happens in
246 taps_wslua.c, which is an auto-generated file from make-taps.py. That Python3
247 script reads from a file called 'taps.ini', which identifies every struct name
248 (and associated enum name) that should be exposed as a tap type. The Python3
249 script then generates the taps_wslua.c to push those whenever the Listener
250 calls for a tap; and it also generates a taps.tx file documenting them all.
251 So to add a new type, add the info to the taps file (or uncomment an existing
252 one), and make sure every member of the tap struct you're exposing is of a
253 type that make-taps.py has in its Python "types" and "comments" dictionaries.
255 Note on Lua versions:
257 Wireshark supports both Lua 5.3 and 5.4, which are defined as LUA_VERSION_NUM
258 values 503 and 504 respectively. When exposing things into Lua, make sure to
259 use ifdef wrappers for things which changed between the versions of Lua. See
260 this for details: http://www.lua.org/manual/5.3/manual.html#8.3
262 ==============================================================================
264 Defined Macros for Lua-API C-files:
266 WSLUA_MODULE - this isn't actually used in real C-code, but rather only
267 appears in C-comments at the top of .c files. That's because it's purely used
268 for documentation, and it makes a new section in the API documentation.
270 For example, this appears near the top of the wslua_gui.c file:
272 /* WSLUA_MODULE Gui GUI support */
274 That makes the API documentation have a section titled 'GUI support' (it's
275 currently section 11.7 in the API docs). It does NOT mean there's any Lua
276 table named 'Gui' (in fact there isn't). It's just for documentation.
277 If you look at the documentation, you'll see there is 'ProgDlg', 'TextWindow',
278 etc. in that 'GUI support' section. That's because both ProgDlg and
279 TextWindow are defined in that same wslua_gui.c file using the
280 'WSLUA_CLASS_DEFINE' macro. (see description of that later) make-wsluarm.py
281 created those in the same documentation section because they're in the same c
282 file as that WSLUA_MODULE comment. You'll also note the documentation
283 includes a sub-section for 'Non Method Functions', which it auto-generated
284 from anything with a 'WSLUA_FUNCTION' macro (as opposed to class member
285 functions, which use the 'WSLUA_METHOD' and 'WSLUA_CONSTRUCTOR' macros). Also,
286 to make new wslua files generate documentation, add this macro to a new file
287 and add the file to the doc/CMakeLists.txt.
290 WSLUA_CONTINUE_MODULE - like WSLUA_MODULE, except used at the top of a .c file
291 to continue defining classes/functions/etc. within a previously declared module
292 in a previous file (i.e., one that used WSLUA_MODULE). The module name must match
293 the original one, and the .c file must be listed after the original one in the
294 CMakeLists.txt lists in the doc directory.
297 WSLUA_ATTRIBUTE - this is another documentation-only "macro", only used within
298 comments. It makes the API docs generate documentation for a member variable
299 of a class, i.e. a key of a Lua table that is not called as a function in Lua,
300 but rather just retrieved or set. The 'WSLUA_ATTRIBUTE' token is followed by
301 a 'RO', 'WO', or 'RW' token, for Read-Only, Write-Only, or Read-Write. (ie,
302 whether the variable can be retrieved, written to, or both) This read/write
303 mode indication gets put into the API documentation. After that comes the name
304 of the attribute, which must be the class name followed by the specific
309 /* WSLUA_ATTRIBUTE Pinfo_rel_ts RO Number of seconds passed since beginning of capture */
313 WSLUA_FUNCTION - this is used for any global Lua function (functions put into
314 the global table) you want to expose, but not for object-style methods (that's
315 the 'WSLUA_METHOD' macro), nor static functions within an object (that's
316 WSLUA_CONSTRUCTOR). Unlike many of the macros here, the function name must
317 begin with 'wslua_'. Everything after that prefix will be the name of the
318 function in Lua. You can ONLY use lower-case letters and the underscore
319 character in this function name. For example 'WSLUA_FUNCTION
320 wslua_get_foo(lua_State* L)' will become a Lua function named 'get_foo'.
321 Documentation for it will also be automatically generated, as it is for the
322 other macros. Although from a Lua perspective it is a global function (not in
323 any class' table), the documentation will append it to the documentation page
324 of the module/file its source code is in, in a "Non Method Functions" section.
325 Descriptive text about the function must be located after the '{' and optional
326 whitespace, within a '\*' '*\' comment block on one line.
330 WSLUA_FUNCTION wslua_gui_enabled(lua_State* L) { /* Checks whether the GUI facility is enabled. */
331 lua_pushboolean(L,GPOINTER_TO_INT(ops && ops->add_button));
332 WSLUA_RETURN(1); /* A boolean: true if it is enabled, false if it isn't. */
336 WSLUA_CLASS_DEFINE - this is used to define/create a new Lua class type (i.e.,
337 table with methods). A Class name must begin with an uppercase letter,
338 followed by any upper or lower case letters but not underscores; in other
339 words, UpperCamelCase without numbers. The macro is expanded to create a
340 bunch of helper functions - see wslua.h. Documentation for it will also be
341 automatically generated, as it is for the other macros.
345 WSLUA_CLASS_DEFINE(ProgDlg,NOP,NOP); /* Manages a progress bar dialog. */
348 WSLUA_CONSTRUCTOR - this is used to define a function of a class that is a
349 static function rather than a per-object method; i.e., from a Lua perspective
350 the function is called as 'myObj.func()' instead of 'myObj:func()'. From a
351 C-code perspective the code generated by make-reg.py does not treat this
352 differently than a WSLUA_METHOD, the only real difference being that the code
353 you write within the function won't be checking the object instance as the
354 first passed-in argument on the Lua-API stack. But from a documentation
355 perspective this macro correctly documents the usage using a '.' period rather
356 than ':' colon. This can also be used within comments, but then it's
357 '_WSLUA_CONSTRUCTOR_'. The name of the function must use the Class name
358 first, followed by underscore, and then the specific lower_underscore name
359 that will end up being the name of the function in Lua.
363 WSLUA_CONSTRUCTOR Dissector_get (lua_State *L) {
364 /* Obtains a dissector reference by name */
365 #define WSLUA_ARG_Dissector_get_NAME 1 /* The name of the dissector */
366 const char* name = luaL_checkstring(L,WSLUA_ARG_Dissector_get_NAME);
370 WSLUA_ARG_ERROR(Dissector_get,NAME,"must be a string");
372 if ((d = find_dissector(name))) {
374 WSLUA_RETURN(1); /* The Dissector reference */
376 WSLUA_ARG_ERROR(Dissector_get,NAME,"No such dissector");
380 WSLUA_METHOD - this is used for object-style class method definitions. The
381 documentation will use the colon syntax, and it will be called as
382 'myObj:func()' in Lua, so your function needs to check the first argument of
383 the stack for the object pointer. Two helper functions are automatically made
384 for this purpose, from the macro expansion of WSLUA_CLASS_DEFINE, of the
385 signatures 'MyObj toMyObj(lua_State* L, int idx)' and 'MyObj
386 checkMyObj(lua_State* L, int idx)'. They do the same thing, but the former
387 generates a Lua Error on failure, while the latter does not.
391 WSLUA_METHOD Listener_remove(lua_State* L) {
392 /* Removes a tap listener */
393 Listener tap = checkListener(L,1);
395 remove_tap_listener(tap);
400 WSLUA_METAMETHOD - this is used for defining object metamethods (ie, Lua
401 metatable functions). The documentation will describe these as well, although
402 currently it doesn't specify they're metamethods but rather makes them appear
403 as regular object methods. The name of it must be the class name followed by
404 *two* underscores, or else it will not appear in the documentation.
408 WSLUA_METAMETHOD NSTime__eq(lua_State* L) { /* Compares two NSTimes */
409 NSTime time1 = checkNSTime(L,1);
410 NSTime time2 = checkNSTime(L,2);
413 if (!time1 || !time2)
414 WSLUA_ERROR(FieldInfo__eq,"Data source must be the same for both fields");
416 if (nstime_cmp(time1, time2) == 0)
419 lua_pushboolean(L,result);
425 WSLUA_ARG_ - the prefix used in a #define statement, for a required
426 function/method argument (ie, one without a default value). It is defined to
427 an integer representing the index slot number of the Lua stack it will be at,
428 when calling the appropriate lua_check/lua_opt routine to get it from the
429 stack. The make_wsluarm.py Python script will generate API documentation with
430 this argument name for the function/method, removing the 'WSLUA_ARG_' prefix.
431 The name following the 'WSLUA_ARG_' prefix must be the same name as the
432 function it's an argument for, followed by an underscore and then an ALLCAPS
433 argument name (including numbers is ok). Although this last part is in
434 ALLCAPS, it is documented in lowercase. The argument name itself is
435 meaningless since it does not exist in Lua or C code.
437 Example: see the example in WSLUA_CONSTRUCTOR above, where
438 WSLUA_ARG_Dissector_get_NAME is used.
441 WSLUA_OPTARG_ - the prefix used in a #define statement, for an optional
442 function/method argument (ie, one with a default value). It is defined to an
443 integer representing the index slot number of the Lua stack it will be at,
444 when calling the appropriate lua_check/lua_opt routine to get it from the
445 stack. The make_wsluarm.py Python script will generate API documentation with
446 this argument name for the function/method, removing the 'WSLUA_OPTARG_'
447 prefix. The rules for the name of the argument after the prefix are the same
448 as for 'WSLUA_ARG_' above.
452 #define WSLUA_OPTARG_Dumper_new_FILETYPE 2 /* The type of the file to be created */
455 WSLUA_MOREARGS - a documentation-only macro used to document that more
456 arguments are expected/supported. This is useful when the number of
457 arguments is not fixed, i.e., a vararg model. The macro is followed by the
458 name of the function it's an argument for (without the 'wslua_' prefix if the
459 function is a WSLUA_FUNCTION type), and then followed by descriptive text.
463 WSLUA_FUNCTION wslua_critical( lua_State* L ) { /* Will add a log entry with critical severity*/
464 /* WSLUA_MOREARGS critical objects to be printed */
465 wslua_log(L,G_LOG_LEVEL_CRITICAL);
470 WSLUA_RETURN - a macro with parentheses containing the number of return
471 values, meaning the number of items pushed back to Lua. Lua supports multiple
472 return values, although Wireshark usually just returns 0 or 1 value. The
473 argument can be an integer or a variable of the integer, and is not actually
474 documented. The API documentation will use the comments after this macro for
475 the return description. This macro can also be within comments, but is then
480 WSLUA_RETURN(1); /* The ethernet pseudoheader */
483 WSLUA_ERROR - this C macro takes arguments, and expands to call luaL_error()
484 using them, and returns 0. The arguments it takes is the full function name
485 and a string describing the error. For documentation, it uses the string
486 argument and displays it with the function it's associated to.
489 if (!wtap_dump_can_write_encap(filetype, encap))
490 WSLUA_ERROR(Dumper_new,"Not every filetype handles every encap");
493 WSLUA_ARG_ERROR - this is a pure C macro and does not generate any
494 documentation. It is used for errors in type/value of function/method
497 Example: see the example in thr WSLUA_CONSTRUCTOR above.
500 ==============================================================================
502 Memory management model:
504 Lua uses a garbage collection model, which for all intents and purposes can
505 collect garbage at any time once an item is no longer referenced by something
506 in Lua. When C-malloc'ed values are pushed into Lua, the Lua library has to
507 let you decide whether to try to free them or not. This is done through the
508 '__gc' metamethod, so every Wireshark class created by WSLUA_CLASS_DEFINE must
509 implement a metamethod function to handle this. The name of the function must
510 be 'ClassName__gc', where 'ClassName' is the same name as the class. Even if
511 you decide to do nothing, you still have to define the function or it will
512 fail to compile - as of this writing, which changed it to do so, in order to
513 make the programmer think about it and not forget.
515 The thing to think about is the lifetime of the object/value. If C-code
516 controls/manages the object after pushing it into Lua, then C-code MUST NOT
517 free it until it knows Lua has garbage collected it, which is only known by
518 the __gc metamethod being invoked. Otherwise you run the risk of the Lua
519 script trying to use it later, which will dereference a pointer to something
520 that has been free'd, and crash. There are known ways to avoid this, but
521 those ways are not currently used in Wireshark's Lua API implementation;
522 except Tvb and TvbRange do implement a simple model of reference counting to
523 protect against this.
525 If possible/reasonable, the best model is to malloc the object when you push
526 it into Lua, usually in a class function (not method) named 'new', and then
527 free it in the __gc metamethod. But if that's not reasonable, then the next
528 best model is to have a boolean member of the class called something like
529 'expired', which is set to true if the C-code decides it is dead/no-longer-
530 useful, and then have every Lua-to-C accessor method for that class type check
531 that boolean before trying to use it, and have the __gc metamethod set
532 expired=true or free it if it's already expired by C-side code; and vice-versa
535 In some cases the class is exposed with a specific method to free/remove it,
536 typically called 'remove'; the Listener class does this, for example. When
537 the Lua script calls myListener:remove(), the C-code for that class method
538 free's the Listener that was malloc'ed previously in Listener.new(). The
539 Listener__gc() metamethod does not do anything, since it's hopefully already
540 been free'd. The downside with this approach is if the script never calls
541 remove(), then it leaks memory; and if the script ever tries to use the
542 Listener userdata object after it called remove(), then Wireshark crashes. Of
543 course either case would be a Lua script programming error, and easily
544 fixable, so it's not a huge deal.
546 ==============================================================================