4 * wireshark's interface to the Lua Programming Language
6 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
7 * (c) 2007, Tamas Regos <tamas.regos@ericsson.com>
8 * (c) 2014, Stig Bjorlykke <stig@bjorlykke.org>
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald@wireshark.org>
12 * Copyright 1998 Gerald Combs
14 * SPDX-License-Identifier: GPL-2.0-or-later
20 #include <epan/dissectors/packet-tcp.h>
21 #include <epan/exceptions.h>
23 /* WSLUA_MODULE Proto Functions For New Protocols And Dissectors
25 The classes and functions in this chapter allow Lua scripts to create new protocols for Wireshark.
26 <<lua_class_Proto,`Proto`>> protocol objects can have <<lua_class_Pref,`Pref`>> preferences, <<lua_class_ProtoField,`ProtoField`>> fields for filterable values that can be displayed in a details view tree, functions for dissecting the new protocol, and so on.
28 The dissection function can be hooked into existing protocol tables through <<lua_class_DissectorTable,`DissectorTable`>> so that the new protocol dissector function gets called by that protocol, and the new dissector can itself call on other, already existing protocol dissectors by retrieving and calling the <<lua_class_Dissector,`Dissector`>> object.
29 A <<lua_class_Proto,`Proto`>> dissector can also be used as a post-dissector, at the end of every frame's dissection, or as a heuristic dissector.
34 * _func_saver stores function refs so that Lua won't garbage collect them prematurely.
35 * It is only used by tcp_dissect_pdus right now.
37 typedef struct _func_saver
{
43 static GPtrArray
* outstanding_FuncSavers
;
45 void clear_outstanding_FuncSavers(void) {
46 while (outstanding_FuncSavers
->len
) {
47 func_saver_t
* fs
= (func_saver_t
*)g_ptr_array_remove_index_fast(outstanding_FuncSavers
,0);
49 lua_State
* L
= fs
->state
;
50 if (fs
->get_len_ref
!= LUA_NOREF
) {
51 luaL_unref(L
, LUA_REGISTRYINDEX
, fs
->get_len_ref
);
53 if (fs
->dissect_ref
!= LUA_NOREF
) {
54 luaL_unref(L
, LUA_REGISTRYINDEX
, fs
->dissect_ref
);
62 WSLUA_CLASS_DEFINE(Proto
,FAIL_ON_NULL("Proto"));
64 A new protocol in Wireshark.
65 Protocols have several uses.
66 The main one is to dissect a protocol, but they can also be dummies used to register preferences for other purposes.
69 static int protocols_table_ref
= LUA_NOREF
;
71 WSLUA_CONSTRUCTOR
Proto_new(lua_State
* L
) { /* Creates a new <<lua_class_Proto,`Proto`>> object. */
72 #define WSLUA_ARG_Proto_new_NAME 1 /* The name of the protocol. */
73 #define WSLUA_ARG_Proto_new_DESCRIPTION 2 /* A Long Text description of the protocol (usually lowercase). */
74 const char* name
= luaL_checkstring(L
,WSLUA_ARG_Proto_new_NAME
);
75 const char* desc
= luaL_checkstring(L
,WSLUA_ARG_Proto_new_DESCRIPTION
);
77 char *loname
, *hiname
;
79 /* TODO: should really make a common function for all of wslua that does checkstring and non-empty at same time */
81 WSLUA_ARG_ERROR(Proto_new
,NAME
,"must not be an empty string");
86 WSLUA_ARG_ERROR(Proto_new
,DESCRIPTION
,"must not be an empty string");
90 if (proto_name_already_registered(desc
)) {
91 WSLUA_ARG_ERROR(Proto_new
,DESCRIPTION
,"there cannot be two protocols with the same description");
95 loname
= g_ascii_strdown(name
, -1);
96 if (proto_check_field_name(loname
)) {
98 WSLUA_ARG_ERROR(Proto_new
,NAME
,"invalid character in name");
102 hiname
= g_ascii_strup(name
, -1);
103 if ((proto_get_id_by_short_name(hiname
) != -1) ||
104 (proto_get_id_by_filter_name(loname
) != -1))
108 WSLUA_ARG_ERROR(Proto_new
,NAME
,"there cannot be two protocols with the same name");
112 proto
= g_new0(wslua_proto_t
, 1);
114 proto
->name
= hiname
;
115 proto
->loname
= loname
;
116 proto
->desc
= g_strdup(desc
);
117 proto
->hfid
= proto_register_protocol(proto
->desc
,hiname
,loname
);
119 proto
->is_postdissector
= false;
120 proto
->expired
= false;
123 proto
->fields
= luaL_ref(L
, LUA_REGISTRYINDEX
);
126 proto
->expert_info_table_ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
127 proto
->expert_module
= expert_register_protocol(proto
->hfid
);
129 proto
->prefs
.name
= NULL
;
130 proto
->prefs
.label
= NULL
;
131 proto
->prefs
.desc
= NULL
;
132 proto
->prefs
.value
.u
= 0;
133 proto
->prefs
.next
= NULL
;
134 proto
->prefs
.proto
= proto
;
136 proto
->prefs_module
= NULL
;
137 proto
->handle
= NULL
;
139 lua_rawgeti(L
, LUA_REGISTRYINDEX
, protocols_table_ref
);
141 lua_pushstring(L
,loname
);
148 WSLUA_RETURN(1); /* The newly created <<lua_class_Proto,`Proto`>> object. */
151 WSLUA_METAMETHOD
Proto__call(lua_State
* L
) { /* Creates a <<lua_class_Proto,`Proto`>> object. */
152 #define WSLUA_ARG_Proto__call_NAME 1 /* The name of the protocol. */
153 #define WSLUA_ARG_Proto__call_DESCRIPTION 2 /* A Long Text description of the protocol (usually lowercase). */
154 lua_remove(L
,1); /* remove the table */
155 WSLUA_RETURN(Proto_new(L
)); /* The new <<lua_class_Proto,`Proto`>> object. */
158 static int Proto__tostring(lua_State
* L
) {
159 Proto proto
= checkProto(L
,1);
161 lua_pushfstring(L
, "Proto: %s", proto
->name
);
166 WSLUA_FUNCTION
wslua_register_postdissector(lua_State
* L
) {
167 /* Make a <<lua_class_Proto,`Proto`>> protocol (with a dissector function) a post-dissector.
168 It will be called for every frame after dissection. */
169 #define WSLUA_ARG_register_postdissector_PROTO 1 /* The protocol to be used as post-dissector. */
170 #define WSLUA_OPTARG_register_postdissector_ALLFIELDS 2 /* Whether to generate all fields.
171 Note: This impacts performance (default=false). */
173 Proto proto
= checkProto(L
,WSLUA_ARG_register_postdissector_PROTO
);
174 const bool all_fields
= wslua_optbool(L
, WSLUA_OPTARG_register_postdissector_ALLFIELDS
, false);
176 if(!proto
->is_postdissector
) {
177 if (! proto
->handle
) {
178 proto
->handle
= register_dissector(proto
->loname
, dissect_lua
, proto
->hfid
);
181 register_postdissector(proto
->handle
);
182 proto
->is_postdissector
= true;
184 luaL_argerror(L
,1,"this protocol is already registered as postdissector");
189 * XXX - are there any Lua postdissectors that need "all fields",
190 * i.e. the entire protocol tree, or do they just look for
191 * *particular* fields, with field extractors?
193 * And do all of them require the actual *displayed* format of
194 * the fields they need?
196 * If not, this is overkill.
198 epan_set_always_visible(true);
204 WSLUA_METHOD
Proto_register_heuristic(lua_State
* L
) {
205 /* Registers a heuristic dissector function for this <<lua_class_Proto,`Proto`>> protocol,
206 for the given heuristic list name.
208 When later called, the passed-in function will be given:
209 1. A <<lua_class_Tvb,`Tvb`>> object
210 2. A <<lua_class_Pinfo,`Pinfo`>> object
211 3. A <<lua_class_TreeItem,`TreeItem`>> object
213 The function must return `true` if the payload is for it, else `false`.
215 The function should perform as much verification as possible to ensure the payload is for it,
216 and dissect the packet (including setting TreeItem info and such) only if the payload is for it,
217 before returning true or false.
219 Since version 1.99.1, this function also accepts a Dissector object as the second argument,
220 to allow re-using the same Lua code as the `function proto.dissector(...)`. In this case,
221 the Dissector must return a Lua number of the number of bytes consumed/parsed: if 0 is returned,
222 it will be treated the same as a `false` return for the heuristic; if a positive or negative
223 number is returned, then the it will be treated the same as a `true` return for the heuristic,
224 meaning the packet is for this protocol and no other heuristic will be tried.
226 #define WSLUA_ARG_Proto_register_heuristic_LISTNAME 2 /* The heuristic list name this function
227 is a heuristic for (e.g., "udp" or
228 "infiniband.payload"). */
229 #define WSLUA_ARG_Proto_register_heuristic_FUNC 3 /* A Lua function that will be invoked for
230 heuristic dissection. */
231 Proto proto
= checkProto(L
,1);
232 const char *listname
= luaL_checkstring(L
, WSLUA_ARG_Proto_register_heuristic_LISTNAME
);
233 const char *proto_name
= proto
->name
;
234 const int top _U_
= lua_gettop(L
);
238 if (!proto_name
|| proto
->hfid
== -1) {
239 /* this shouldn't happen - internal bug if it does */
240 luaL_error(L
,"Proto_register_heuristic: got NULL proto name or invalid hfid");
244 /* verify listname has a heuristic list */
245 if (!has_heur_dissector_list(listname
)) {
246 luaL_error(L
, "there is no heuristic list for '%s'", listname
);
250 short_name
= wmem_strconcat(NULL
, proto
->loname
, "_", listname
, NULL
);
252 /* verify that this is not already registered */
253 if (find_heur_dissector_by_unique_short_name(short_name
)) {
254 wmem_free(NULL
, short_name
);
255 luaL_error(L
, "'%s' is already registered as heuristic", proto
->loname
);
258 wmem_free(NULL
, short_name
);
260 /* we'll check if the second form of this function was called: when the second arg is
261 a Dissector obejct. The truth is we don't need the Dissector object to do this
262 form of registration, but someday we might... so we're using it as a boolean arg
263 right now and in the future might use it for other things in this registration.
265 if (isDissector(L
, WSLUA_ARG_Proto_register_heuristic_FUNC
)) {
266 /* retrieve the Dissector's Lua function... first get the table of all dissector funcs */
267 lua_rawgeti(L
, LUA_REGISTRYINDEX
, lua_dissectors_table_ref
);
268 /* then get the one for this Proto */
269 lua_getfield(L
, -1, proto_name
);
271 if (!lua_isfunction(L
,-1)) {
272 /* this shouldn't be possible */
273 luaL_error(L
,"Proto_register_heuristic: could not get lua function from lua_dissectors_table");
276 /* replace the Dissector with the function */
277 lua_replace(L
, WSLUA_ARG_Proto_register_heuristic_FUNC
);
278 /* pop the lua_dissectors_table */
280 ws_assert(top
== lua_gettop(L
));
283 /* heuristic functions are stored in a table in the registry; the registry has a
284 * table at reference lua_heur_dissectors_table_ref, and that table has keys for
285 * the heuristic listname (e.g., "udp", "tcp", etc.), and that key's value is a
286 * table of keys of the Proto->name, and their value is the function.
287 * So it's like registry[table_ref][heur_list_name][proto_name] = func
289 if (lua_isfunction(L
,WSLUA_ARG_Proto_register_heuristic_FUNC
)) {
290 /* insert the heur dissector into the heur dissectors table */
291 lua_rawgeti(L
, LUA_REGISTRYINDEX
, lua_heur_dissectors_table_ref
);
292 /* the heuristic lists table is now at -1 */
293 if (!lua_istable(L
,-1)) {
294 /* this shouldn't be possible */
295 luaL_error(L
,"Proto_register_heuristic: could not get lua_heur_dissectors table from registry");
299 if (!wslua_get_table(L
,-1,listname
)) {
300 /* no one's registered a lua heuristic for this list, so make a new list table */
302 lua_pushvalue(L
,-1); /* duplicate the table so we can set it as a field */
303 lua_setfield(L
,-3,listname
); /* sets this new list table into the lists table */
305 else if (wslua_get_field(L
,-1,proto_name
)) {
306 luaL_error(L
,"A heuristic dissector for Proto '%s' is already registered for the '%s' list", proto_name
, listname
);
310 /* copy the func, set it as the value for key proto_name in listname's table */
311 lua_pushvalue(L
,WSLUA_ARG_Proto_register_heuristic_FUNC
);
312 lua_setfield(L
,-2,proto_name
);
314 /* ok, we're done with lua stuff, pop what we added to the stack */
315 lua_pop(L
,2); /* pop the lists table and the listname table */
316 ws_assert(top
== lua_gettop(L
));
318 short_name
= wmem_strconcat(NULL
, proto
->loname
, "_", listname
, NULL
);
320 /* now register the single/common heur_dissect_lua function */
321 /* XXX - ADD PARAMETERS FOR NEW heur_dissector_add PARAMETERS!!! */
322 heur_dissector_add(listname
, heur_dissect_lua
, proto_name
, short_name
, proto
->hfid
, HEURISTIC_ENABLE
);
324 wmem_free(NULL
, short_name
);
326 luaL_argerror(L
,3,"The heuristic dissector must be a function");
331 /* WSLUA_ATTRIBUTE Proto_dissector RW The protocol's dissector, a function you define.
333 When later called, the function will be given:
334 1. A <<lua_class_Tvb,`Tvb`>> object
335 2. A <<lua_class_Pinfo,`Pinfo`>> object
336 3. A <<lua_class_TreeItem,`TreeItem`>> object
338 static int Proto_get_dissector(lua_State
* L
) {
339 Proto proto
= checkProto(L
,1);
342 pushDissector(L
,proto
->handle
);
345 luaL_error(L
,"The protocol hasn't been registered yet");
350 static int Proto_set_dissector(lua_State
* L
) {
351 Proto proto
= checkProto(L
,1);
353 if (lua_isfunction(L
,2)) {
354 /* insert the dissector into the dissectors table */
355 lua_rawgeti(L
, LUA_REGISTRYINDEX
, lua_dissectors_table_ref
);
357 lua_pushstring(L
,proto
->name
);
358 lua_insert(L
, 2); /* function is now at 3 */
361 if (! proto
->handle
) {
362 proto
->handle
= register_dissector(proto
->loname
, dissect_lua
, proto
->hfid
);
365 luaL_argerror(L
,2,"The dissector of a protocol must be a function");
370 /* WSLUA_ATTRIBUTE Proto_prefs RO The preferences of this dissector. */
371 static int Proto_get_prefs(lua_State
* L
) {
372 Proto proto
= checkProto(L
,1);
373 pushPrefs(L
,&proto
->prefs
);
377 /* WSLUA_ATTRIBUTE Proto_prefs_changed WO The preferences changed routine of this dissector,
378 a Lua function you define.
380 The function is called when the protocol's preferences are changed.
381 It is passed no arguments.
383 static int Proto_set_prefs_changed(lua_State
* L
) {
384 Proto proto
= checkProto(L
,1);
386 if (lua_isfunction(L
,2)) {
387 /* insert the prefs changed callback into the prefs_changed table */
388 lua_getglobal(L
, WSLUA_PREFS_CHANGED
);
390 lua_pushstring(L
,proto
->name
);
391 lua_insert(L
, 2); /* function is now at 3 */
394 luaL_argerror(L
,2,"The prefs of a protocol must be a function");
399 /* WSLUA_ATTRIBUTE Proto_init WO The init routine of this dissector, a function you define.
401 The init function is called when the a new capture file is opened or when
402 the open capture file is closed. It is passed no arguments.
404 static int Proto_set_init(lua_State
* L
) {
405 Proto proto
= checkProto(L
,1);
407 if (lua_isfunction(L
,2)) {
408 /* insert the init routine into the init_routines table */
409 lua_getglobal(L
, WSLUA_INIT_ROUTINES
);
411 lua_pushstring(L
,proto
->name
);
412 lua_insert(L
, 2); /* function is now at 3 */
415 luaL_argerror(L
,2,"The initializer of a protocol must be a function");
420 /* WSLUA_ATTRIBUTE Proto_name RO The name given to this dissector. */
421 WSLUA_ATTRIBUTE_STRING_GETTER(Proto
,name
);
423 /* WSLUA_ATTRIBUTE Proto_description RO The description given to this dissector. */
424 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(Proto
,description
,desc
);
426 /* WSLUA_ATTRIBUTE Proto_fields RW The Lua table of this dissector's ``ProtoField``s.
427 ``ProtoField``s added to this table are registered to the `Proto` (and any
428 removed are deregistered if previously registered.) */
429 static int Proto_get_fields(lua_State
* L
) {
430 Proto proto
= checkProto(L
,1);
431 lua_rawgeti(L
, LUA_REGISTRYINDEX
, proto
->fields
);
435 static bool Proto_append_ProtoField(Proto proto
, ProtoField f
) {
438 // Already registered
441 hf_register_info hfri
= { NULL
, { NULL
, NULL
, FT_NONE
, 0, NULL
, 0, NULL
, HFILL
} };
445 hfri
.p_id
= &(f
->hfid
);
446 hfri
.hfinfo
.name
= f
->name
;
447 hfri
.hfinfo
.abbrev
= f
->abbrev
;
448 hfri
.hfinfo
.type
= f
->type
;
449 hfri
.hfinfo
.display
= f
->base
;
450 hfri
.hfinfo
.strings
= VALS(f
->vs
);
451 hfri
.hfinfo
.bitmask
= f
->mask
;
452 hfri
.hfinfo
.blurb
= f
->blob
;
455 g_array_append_val(proto
->hfa
,hfri
);
456 g_array_append_val(proto
->etta
,ettp
);
461 static int Proto_set_fields(lua_State
* L
) {
462 Proto proto
= checkProto(L
,1);
463 #define FIELDS_TABLE 2
468 * XXX - This is a "setter", but it really appends any ProtoFields to
469 * the Lua Table without removing any existing ones.
473 /* This Proto's ProtoFields were already registered in Proto_commit.
474 * Deregister the existing array with epan so we can add new ones.
475 * (Appending to the GArray and registering only the new ones would
476 * have a use-after-free, for reasons mentioned in proto.c )
477 * XXX - What is the reason for waiting and registering all
478 * at once in Proto_commit instead of doing it here every time?
480 if (proto
->hfa
->len
) {
481 proto_add_deregistered_data(g_array_free(proto
->hfa
,false));
483 g_array_free(proto
->hfa
,true);
485 /* No need for deferred deletion of subtree indexes */
486 g_array_free(proto
->etta
,true);
487 proto
->hfa
= g_array_new(true,true,sizeof(hf_register_info
));
488 proto
->etta
= g_array_new(true,true,sizeof(int*));
491 lua_rawgeti(L
, LUA_REGISTRYINDEX
, proto
->fields
);
492 lua_insert(L
,FIELDS_TABLE
);
494 if( lua_istable(L
,NEW_TABLE
)) {
495 for (lua_pushnil(L
); lua_next(L
, NEW_TABLE
); ) {
496 if (isProtoField(L
,5)) {
498 ProtoField f
= toProtoField(L
,5);
499 // XXX this will leak resources on error
500 // If this continued and registered the field array, it
501 // wouldn't leak. We could perhaps print a warning or even
502 // err after registration.
503 if (!Proto_append_ProtoField(proto
, f
)) {
504 return luaL_error(L
,"%s is already registered; fields can be registered only once", f
->abbrev
);
507 /* luaL_ref returns a reference. lua_next will return not
508 * just occupied entries in the table, but also references
509 * used to store unused/deleted entries in the hash table
510 * so that they can be reused without reallocation. Those
511 * will have a lua_Number as their value. The values form
512 * a linked list of available indicies, starting with the
513 * head at index 3 (LUA_RIDX_LAST + 1) in Lua 5.4 and index
514 * 0 in earlier versions. (Since arrays are 1-indexed, this
515 * is mostly invisible in Lua 5.3 and earlier so long as
516 * nothing has been deleted.)
518 * Perhaps the assumption is that no one wants to use a
519 * hash table to store numbers anyway? This also means
520 * that for any table with 2 or more real entries, the
521 * length operator # *includes* the freelist and cannot
524 * If we wanted to only check entries we knew were valid,
525 * we could save this reference.
527 * This also means that our checks below on registration
528 * and deregistration that the table entries are ProtoFields
529 * are less useful, because we do now expect some numbers
530 * in the table. Hopefully the check on insert here obviates
531 * needing to check there.
533 /* int ref = */ luaL_ref(L
,FIELDS_TABLE
);
534 } else if (! lua_isnil(L
,5) ) {
535 return luaL_error(L
,"only ProtoFields should be in the table");
538 } else if (isProtoField(L
,NEW_FIELD
)){
540 ProtoField f
= toProtoField(L
,NEW_FIELD
);
541 // XXX this will leak resources on error
542 // If this continued and registered the field array, it wouldn't
543 // leak. We could perhaps print a warning or even err after
545 if (!Proto_append_ProtoField(proto
, f
)) {
546 return luaL_error(L
,"%s is already registered; fields can be registered only once", f
->abbrev
);
549 lua_pushvalue(L
, NEW_FIELD
);
550 luaL_ref(L
,FIELDS_TABLE
);
553 return luaL_error(L
,"either a ProtoField or an array of protofields");
556 if (proto
->hfa
&& proto
->hfa
->len
) {
557 /* register the proto fields */
558 proto_register_field_array(proto
->hfid
,&g_array_index(proto
->hfa
, hf_register_info
, 0),proto
->hfa
->len
);
559 proto_register_subtree_array(&g_array_index(proto
->etta
, int*, 0),proto
->etta
->len
);
567 /* WSLUA_ATTRIBUTE Proto_experts RW The expert info Lua table of this `Proto`. */
568 static int Proto_get_experts(lua_State
* L
) {
569 Proto proto
= checkProto(L
,1);
570 lua_rawgeti(L
, LUA_REGISTRYINDEX
, proto
->expert_info_table_ref
);
574 static bool Proto_append_ProtoExpert(Proto proto
, ProtoExpert e
) {
576 if (e
->ids
.ei
!= EI_INIT_EI
|| e
->ids
.hf
!= -2) {
579 ei_register_info eiri
= { NULL
, { NULL
, 0, 0, NULL
, EXPFILL
} };
581 eiri
.ids
= &(e
->ids
);
582 eiri
.eiinfo
.name
= e
->abbrev
;
583 eiri
.eiinfo
.group
= e
->group
;
584 eiri
.eiinfo
.severity
= e
->severity
;
585 eiri
.eiinfo
.summary
= e
->text
;
588 g_array_append_val(proto
->eia
,eiri
);
593 static int Proto_set_experts(lua_State
* L
) {
594 Proto proto
= checkProto(L
,1);
600 * XXX - This is a "setter", but it really appends any ProtoExperts to
601 * the Lua Table without removing any existing ones.
605 /* This Proto's ProtoExperts were already registered in Proto_commit.
606 * Deregister the existing array with epan so we can add new ones.
607 * XXX - What is the reason for waiting and registering all at
608 * at once in Proto_commit instead of doing it here every time?
610 if (proto
->eia
&& proto
->eia
->len
) {
611 proto_add_deregistered_data(g_array_free(proto
->eia
,false));
613 g_array_free(proto
->eia
,true);
615 proto
->eia
= g_array_new(true,true,sizeof(ei_register_info
));
618 lua_rawgeti(L
, LUA_REGISTRYINDEX
, proto
->expert_info_table_ref
);
619 lua_insert(L
,EI_TABLE
);
621 if( lua_istable(L
,NEW_TABLE
)) {
622 for (lua_pushnil(L
); lua_next(L
, NEW_TABLE
); ) {
623 if (isProtoExpert(L
,5)) {
625 ProtoExpert e
= toProtoExpert(L
, NEW_FIELD
);
627 if (!Proto_append_ProtoExpert(proto
, e
)) {
628 return luaL_error(L
,"%s is already registered; expert fields can be registered only once", e
->abbrev
);
631 luaL_ref(L
,EI_TABLE
);
632 } else if (! lua_isnil(L
,5) ) {
633 return luaL_error(L
,"only ProtoExperts should be in the table");
636 } else if (isProtoExpert(L
,NEW_FIELD
)){
638 ProtoExpert e
= toProtoExpert(L
, NEW_FIELD
);
640 if (!Proto_append_ProtoExpert(proto
, e
)) {
641 return luaL_error(L
,"%s is already registered; expert fields can be registered only once", e
->abbrev
);
644 lua_pushvalue(L
, NEW_FIELD
);
645 luaL_ref(L
,EI_TABLE
);
648 return luaL_error(L
,"either a ProtoExpert or an array of ProtoExperts");
656 /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
657 static int Proto__gc(lua_State
* L
) {
658 /* Proto is registered twice, once in protocols_table_ref and once returned from Proto_new.
659 * It will not be freed unless deregistered.
661 Proto proto
= toProto(L
,1);
663 if (!proto
->expired
) {
664 proto
->expired
= true;
665 } else if (proto
->hfid
== -2) {
666 /* Only free deregistered Proto */
673 /* This table is ultimately registered as a sub-table of the class' metatable,
674 * and if __index/__newindex is invoked then it calls the appropriate function
675 * from this table for getting/setting the members.
677 WSLUA_ATTRIBUTES Proto_attributes
[] = {
678 WSLUA_ATTRIBUTE_RWREG(Proto
,dissector
),
679 WSLUA_ATTRIBUTE_RWREG(Proto
,fields
),
680 WSLUA_ATTRIBUTE_RWREG(Proto
,experts
),
681 WSLUA_ATTRIBUTE_ROREG(Proto
,prefs
),
682 WSLUA_ATTRIBUTE_WOREG(Proto
,prefs_changed
),
683 WSLUA_ATTRIBUTE_WOREG(Proto
,init
),
684 WSLUA_ATTRIBUTE_ROREG(Proto
,name
),
685 WSLUA_ATTRIBUTE_ROREG(Proto
,description
),
689 WSLUA_METHODS Proto_methods
[] = {
690 WSLUA_CLASS_FNREG(Proto
,new),
691 WSLUA_CLASS_FNREG(Proto
,register_heuristic
),
695 WSLUA_META Proto_meta
[] = {
696 WSLUA_CLASS_MTREG(Proto
,tostring
),
697 WSLUA_CLASS_MTREG(Proto
,call
),
701 int Proto_register(lua_State
* L
) {
702 WSLUA_REGISTER_CLASS_WITH_ATTRS(Proto
);
704 if (outstanding_FuncSavers
!= NULL
) {
705 g_ptr_array_unref(outstanding_FuncSavers
);
707 outstanding_FuncSavers
= g_ptr_array_new();
710 protocols_table_ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
716 * Query field abbr that is defined and bound to a Proto in lua.
717 * They are not registered until the end of the initialization.
719 ProtoField
wslua_is_field_available(lua_State
* L
, const char* field_abbr
) {
720 lua_rawgeti(L
, LUA_REGISTRYINDEX
, protocols_table_ref
);
722 while (lua_next(L
, -2)) {
724 proto
= checkProto(L
, -1);
726 lua_rawgeti(L
, LUA_REGISTRYINDEX
, proto
->fields
);
729 while (lua_next(L
, -2)) {
730 if (lua_type(L
, -1) == LUA_TNUMBER
) {
731 /* part of free reference linked list, ignore */
732 lua_pop(L
, 1); /* table value */
735 ProtoField f
= checkProtoField(L
, -1);
736 if (strcmp(field_abbr
, f
->abbrev
) == 0) {
741 lua_pop(L
, 1); /* table value */
743 lua_pop(L
, 2); /* proto->fields and table value */
745 lua_pop(L
, 1); /* protocols_table_ref */
750 int wslua_deregister_heur_dissectors(lua_State
* L
) {
751 /* for each registered heur dissector do... */
752 lua_rawgeti(L
, LUA_REGISTRYINDEX
, lua_heur_dissectors_table_ref
);
753 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1)) {
754 const char *listname
= luaL_checkstring(L
, -2);
755 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1)) {
756 const char *proto_name
= luaL_checkstring(L
, -2);
757 int proto_id
= proto_get_id_by_short_name(proto_name
);
758 heur_dissector_delete(listname
, heur_dissect_lua
, proto_id
);
761 lua_pop(L
, 1); /* lua_heur_dissectors_table_ref */
766 int wslua_deregister_protocols(lua_State
* L
) {
767 /* for each registered Proto protocol do... */
768 lua_rawgeti(L
, LUA_REGISTRYINDEX
, protocols_table_ref
);
769 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1)) {
771 proto
= checkProto(L
, -1);
774 deregister_dissector(proto
->loname
);
776 if (proto
->prefs_module
) {
778 prefs_deregister_protocol(proto
->hfid
);
779 /* Preferences are unregistered, now free its memory via Pref__gc */
780 for (pref
= proto
->prefs
.next
; pref
; pref
= pref
->next
) {
781 int pref_ref
= pref
->ref
;
782 pref
->ref
= LUA_NOREF
;
783 luaL_unref(L
, LUA_REGISTRYINDEX
, pref_ref
);
786 if (proto
->expert_module
) {
787 expert_deregister_protocol(proto
->expert_module
);
789 proto_deregister_protocol(proto
->name
);
791 /* for each registered ProtoField do... */
792 lua_rawgeti(L
, LUA_REGISTRYINDEX
, proto
->fields
);
793 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1)) {
794 if (lua_type(L
, -1) == LUA_TNUMBER
) {
795 /* part of free reference linked list, ignore */
798 ProtoField f
= checkProtoField(L
, -1);
800 /* Memory ownership was previously transferred to epan in Proto_commit */
806 f
->hfid
= -2; /* Deregister ProtoField, freed in ProtoField__gc */
810 /* for each registered ProtoExpert do... */
811 lua_rawgeti(L
, LUA_REGISTRYINDEX
, proto
->expert_info_table_ref
);
812 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1)) {
813 if (lua_type(L
, -1) == LUA_TNUMBER
) {
814 /* part of free reference linked list, ignore */
817 ProtoExpert pe
= checkProtoExpert(L
,-1);
819 /* Memory ownership was previously transferred to epan in Proto_commit */
823 pe
->ids
.hf
= -2; /* Deregister ProtoExpert, freed in ProtoExpert__gc */
827 if (proto
->hfa
&& proto
->hfa
->len
) {
828 proto_add_deregistered_data(g_array_free(proto
->hfa
,false));
830 g_array_free(proto
->hfa
,true);
833 /* No need for deferred deletion of subtree indexes */
834 g_array_free(proto
->etta
,true);
836 if (proto
->eia
&& proto
->eia
->len
) {
837 proto_add_deregistered_data(g_array_free(proto
->eia
,false));
839 g_array_free(proto
->eia
,true);
842 proto
->hfid
= -2; /* Deregister Proto, freed in Proto__gc */
845 lua_pop(L
, 1); /* protocols_table_ref */
850 int Proto_commit(lua_State
* L
) {
852 /* the following gets the table of registered Proto protocols and puts it on the stack (index=1) */
853 lua_rawgeti(L
, LUA_REGISTRYINDEX
, protocols_table_ref
);
855 /* for each registered Proto protocol do... */
856 for (lua_pushnil(L
); lua_next(L
, 1); lua_pop(L
, 1)) {
857 /* lua_next() pop'ed the nil, pushed a table entry key at index=2, with value at index=3.
858 In our case, the key is the Proto's name, and the value is the Proto object.
859 At next iteration, the value (Proto object) will be pop'ed due
860 to lua_pop(L, 1), and when lua_next() returns 0 (no more table entries), it will have
861 pop'ed the final key itself, leaving just the protocols_table_ref table on the stack.
863 Proto proto
= checkProto(L
,3);
867 /* This proto's ProtoFields were already registered. */
871 proto
->hfa
= g_array_new(true,true,sizeof(hf_register_info
));
872 proto
->etta
= g_array_new(true,true,sizeof(int*));
873 proto
->eia
= g_array_new(true,true,sizeof(ei_register_info
));
875 ettp
= &(proto
->ett
);
876 g_array_append_val(proto
->etta
,ettp
);
878 /* get the Lua table of ProtoFields, push it on the stack (index=3) */
879 lua_rawgeti(L
, LUA_REGISTRYINDEX
, proto
->fields
);
881 /* for each ProtoField in the Lua table do... */
882 for (lua_pushnil(L
); lua_next(L
, 4); lua_pop(L
, 1)) {
883 if (lua_type(L
, -1) == LUA_TNUMBER
) {
884 /* part of free reference linked list, ignore */
887 ProtoField f
= checkProtoField(L
,6);
889 // XXX this will leak resources on error
890 // If this continued and registered the field array, it wouldn't
891 // leak. We could perhaps print a warning or even err after
893 if (!Proto_append_ProtoField(proto
, f
)) {
894 return luaL_error(L
,"%s is already registered; fields can be registered only once", f
->abbrev
);
898 /* register the proto fields */
899 proto_register_field_array(proto
->hfid
,(hf_register_info
*)(void*)proto
->hfa
->data
,proto
->hfa
->len
);
900 proto_register_subtree_array((int**)(void*)proto
->etta
->data
,proto
->etta
->len
);
902 lua_pop(L
,1); /* pop the table of ProtoFields */
904 /* now do the same thing for expert fields */
906 /* get the Lua table of ProtoExperts, push it on the stack (index=2) */
907 lua_rawgeti(L
, LUA_REGISTRYINDEX
, proto
->expert_info_table_ref
);
909 /* for each ProtoExpert in the Lua table do... */
910 for (lua_pushnil(L
); lua_next(L
, 4); lua_pop(L
, 1)) {
911 if (lua_type(L
, -1) == LUA_TNUMBER
) {
912 /* part of free reference linked list, ignore */
915 ProtoExpert e
= checkProtoExpert(L
,6);
916 if (!Proto_append_ProtoExpert(proto
, e
)) {
917 return luaL_error(L
,"%s is already registered; expert fields can be registered only once", e
->abbrev
);
921 expert_register_field_array(proto
->expert_module
, (ei_register_info
*)(void*)proto
->eia
->data
, proto
->eia
->len
);
923 lua_pop(L
,1); /* pop the table of ProtoExperts */
925 /* Proto object will be pop'ed by lua_pop(L, 1) in for statement */
928 lua_pop(L
,1); /* pop the protocols_table_ref */
934 wslua_dissect_tcp_get_pdu_len(packet_info
*pinfo
, tvbuff_t
*tvb
,
935 int offset
, void *data
)
937 /* WARNING: called from a TRY block, do not call luaL_error! */
938 func_saver_t
* fs
= (func_saver_t
*)data
;
939 lua_State
* L
= fs
->state
;
943 lua_rawgeti(L
, LUA_REGISTRYINDEX
, fs
->get_len_ref
);
945 if (lua_isfunction(L
,1)) {
949 lua_pushinteger(L
,offset
);
951 if ( lua_pcall(L
,3,1,0) ) {
952 THROW_LUA_ERROR("Lua Error in dissect_tcp_pdus get_len_func: %s", lua_tostring(L
,-1));
954 /* if the Lua dissector reported the consumed bytes, pass it to our caller */
955 if (lua_isnumber(L
, -1)) {
956 /* we got the pdu_len */
957 pdu_len
= wslua_toint(L
, -1);
960 THROW_LUA_ERROR("Lua Error dissect_tcp_pdus: get_len_func did not return a Lua number of the PDU length");
965 REPORT_DISSECTOR_BUG("Lua Error in dissect_tcp_pdus: did not find the get_len_func dissector");
972 wslua_dissect_tcp_dissector(tvbuff_t
*tvb
, packet_info
*pinfo
,
973 proto_tree
*tree
, void *data
)
975 /* WARNING: called from a TRY block, do not call luaL_error! */
976 func_saver_t
* fs
= (func_saver_t
*)data
;
977 lua_State
* L
= fs
->state
;
978 int consumed_bytes
= 0;
981 lua_rawgeti(L
, LUA_REGISTRYINDEX
, fs
->dissect_ref
);
983 if (lua_isfunction(L
,1)) {
987 /* XXX: not sure if it's kosher to just use the tree as the item */
988 push_TreeItem(L
, tree
, (proto_item
*)tree
);
990 if ( lua_pcall(L
,3,1,0) ) {
991 THROW_LUA_ERROR("dissect_tcp_pdus dissect_func: %s", lua_tostring(L
, -1));
993 /* if the Lua dissector reported the consumed bytes, pass it to our caller */
994 if (lua_isnumber(L
, -1)) {
995 /* we got the consumed bytes or the missing bytes as a negative number */
996 consumed_bytes
= wslua_toint(L
, -1);
1002 REPORT_DISSECTOR_BUG("dissect_tcp_pdus: did not find the dissect_func dissector");
1005 return consumed_bytes
;
1009 WSLUA_FUNCTION
wslua_dissect_tcp_pdus(lua_State
* L
) {
1010 /* Make the TCP-layer invoke the given Lua dissection function for each
1011 PDU in the TCP segment, of the length returned by the given get_len_func
1014 This function is useful for protocols that run over TCP and that are
1015 either a fixed length always, or have a minimum size and have a length
1016 field encoded within that minimum portion that identifies their full
1017 length. For such protocols, their protocol dissector function can invoke
1018 this `dissect_tcp_pdus()` function to make it easier to handle dissecting
1019 their protocol's messages (i.e., their protocol data unit (PDU)). This
1020 function shouild not be used for protocols whose PDU length cannot be
1021 determined from a fixed minimum portion, such as HTTP or Telnet.
1023 #define WSLUA_ARG_dissect_tcp_pdus_TVB 1 /* The Tvb buffer to dissect PDUs from. */
1024 #define WSLUA_ARG_dissect_tcp_pdus_TREE 2 /* `TreeItem` object passed to the `dissect_func`. */
1025 #define WSLUA_ARG_dissect_tcp_pdus_MIN_HEADER_SIZE 3 /* The number of bytes
1026 in the fixed-length part of the PDU. */
1027 #define WSLUA_ARG_dissect_tcp_pdus_GET_LEN_FUNC 4 /* A Lua function that will be
1028 called for each PDU, to determine the full length of the
1029 PDU. The called function will be given (1) the `Tvb` object
1030 of the whole `Tvb` (possibly reassembled), (2) the `Pinfo` object,
1031 and (3) an offset number of the index of the first byte
1032 of the PDU (i.e., its first header byte). The Lua function
1033 must return a Lua number of the full length of the PDU. */
1034 #define WSLUA_ARG_dissect_tcp_pdus_DISSECT_FUNC 5 /* A Lua function that will be
1035 called for each PDU, to dissect the PDU. The called
1036 function will be given (1) the `Tvb` object of the PDU's
1037 `Tvb` (possibly reassembled), (2) the `Pinfo` object,
1038 and (3) the `TreeItem` object. The Lua function must
1039 return a Lua number of the number of bytes read/handled,
1040 which would typically be the `Tvb:len()`.*/
1041 #define WSLUA_OPTARG_dissect_tcp_pdus_DESEGMENT 6 /* Whether to reassemble PDUs
1042 crossing TCP segment boundaries or not. (default=true) */
1043 Tvb tvb
= checkTvb(L
,WSLUA_ARG_dissect_tcp_pdus_TVB
);
1044 TreeItem ti
= checkTreeItem(L
,WSLUA_ARG_dissect_tcp_pdus_TREE
);
1045 unsigned fixed_len
= (unsigned)luaL_checkinteger(L
,WSLUA_ARG_dissect_tcp_pdus_MIN_HEADER_SIZE
);
1046 bool proto_desegment
= wslua_optbool(L
, WSLUA_OPTARG_dissect_tcp_pdus_DESEGMENT
, true);
1049 luaL_error(L
,"dissect_tcp_pdus can only be invoked while in a dissect function");
1053 if (lua_isfunction(L
,WSLUA_ARG_dissect_tcp_pdus_GET_LEN_FUNC
) &&
1054 lua_isfunction(L
,WSLUA_ARG_dissect_tcp_pdus_DISSECT_FUNC
))
1056 /* save the Lua functions so that we can call them later */
1057 func_saver_t
* fs
= g_new(func_saver_t
, 1);
1059 lua_settop(L
, WSLUA_ARG_dissect_tcp_pdus_DISSECT_FUNC
);
1062 /* the following pops the top function and sets a ref to it in the registry */
1063 fs
->dissect_ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
1064 fs
->get_len_ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
1066 /* save the passed-in function refs, so Lua's garbage collector won't
1067 destroy them before they get invoked */
1068 g_ptr_array_add(outstanding_FuncSavers
, fs
);
1070 WRAP_NON_LUA_EXCEPTIONS(
1071 tcp_dissect_pdus(tvb
->ws_tvb
, lua_pinfo
, ti
->tree
, proto_desegment
,
1072 fixed_len
, wslua_dissect_tcp_get_pdu_len
,
1073 wslua_dissect_tcp_dissector
, (void*)fs
);
1076 luaL_error(L
,"The third and fourth arguments need to be Lua functions");
1083 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1088 * indent-tabs-mode: nil
1091 * vi: set shiftwidth=4 tabstop=8 expandtab:
1092 * :indentSize=4:tabSize=8:noTabs=true: