Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
wslua.h
Go to the documentation of this file.
1/*
2 * wslua.h
3 *
4 * Wireshark's interface to the Lua Programming Language
5 *
6 * (c) 2006, Luis E. Garcia Ontanon <[email protected]>
7 * (c) 2007, Tamas Regos <[email protected]>
8 * (c) 2008, Balint Reczey <[email protected]>
9 * (c) 2025, Bartis Csaba <[email protected]>
10 *
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <[email protected]>
13 * Copyright 1998 Gerald Combs
14 *
15 * SPDX-License-Identifier: GPL-2.0-or-later
16 */
17
18#ifndef _PACKET_LUA_H
19#define _PACKET_LUA_H
20
21#include <glib.h>
22#include <stdlib.h>
23#include <string.h>
24#include <math.h>
25#include <lua.h>
26#include <lualib.h>
27#include <lauxlib.h>
28
29#include <ws_log_defs.h>
30
31#include <wiretap/wtap.h>
32
34#include <wsutil/nstime.h>
35#include <wsutil/ws_assert.h>
36#include <wsutil/wslog.h>
37
38#include <epan/packet.h>
39#include <epan/strutil.h>
40#include <epan/to_str.h>
41#include <epan/uat-int.h>
42#include <epan/uat.h>
43#include <epan/prefs.h>
44#include <epan/prefs-int.h>
45#include <epan/proto.h>
46#include <epan/epan_dissect.h>
47#include <epan/tap.h>
48#include <epan/column-utils.h>
49#include <wsutil/filesystem.h>
50#include <wsutil/wsgcrypt.h>
51#include <epan/funnel.h>
52#include <epan/tvbparse.h>
53#include <epan/epan.h>
54#include <epan/expert.h>
55#include <epan/exceptions.h>
56#include <epan/show_exception.h>
57#include <epan/conversation.h>
58
59#include <epan/wslua/declare_wslua.h>
60
65#define WSLUA_INIT_ROUTINES "init_routines"
66#define WSLUA_PREFS_CHANGED "prefs_changed"
67
68/* type conversion macros - lua_Number is a double, so casting isn't kosher; and
69 using Lua's already-available lua_tointeger() and luaL_checkinteger() might be
70 different on different machines; so use these instead please!
71
72 It can be important to choose the correct version of signed or unsigned
73 conversion macros; don't assume that you can freely convert to the signed
74 or unsigned integer of the same size later:
75
76 On 32-bit Windows x86, Lua 5.2 and earlier must use lua_tounsigned() and
77 luaL_checkunsigned() due to the use of float to integer inlined assembly.
78 (#18367)
79 On ARM, casting from a negative floating point number to an unsigned integer
80 type doesn't perform wraparound conversion in the same way as casting from
81 float to the same size signed integer then to unsigned does, unlike x86[-64].
82 (Commit 15392c324d5eaefcaa298cdee09cd5b40b12e09c)
83
84 On Lua 5.3 and later, numbers are stored as a kind of union between
85 Lua_Number and Lua_Integer. On 5.2 and earlier. all numbers are stored
86 as Lua_Number internally.
87
88 Be careful about using the 64-bit functions, as they convert from double
89 and lose precision at high values. See wslua_int64.c and the types there.
90 TODO: Check if Lua_Integer is 64 bit on Lua 5.3 and later.
91*/
92#define wslua_toint(L,i) (int) ( lua_tointeger(L,i) )
93#define wslua_toint32(L,i) (int32_t) ( lua_tointeger(L,i) )
94#define wslua_toint64(L,i) (int64_t) ( lua_tonumber(L,i) )
95#define wslua_touint64(L,i) (uint64_t) ( lua_tonumber(L,i) )
96
97#define wslua_checkint(L,i) (int) ( luaL_checkinteger(L,i) )
98#define wslua_checkint32(L,i) (int32_t) ( luaL_checkinteger(L,i) )
99#define wslua_checkint64(L,i) (int64_t) ( luaL_checknumber(L,i) )
100#define wslua_checkuint64(L,i) (uint64_t) ( luaL_checknumber(L,i) )
101
102#define wslua_optint(L,i,d) (int) ( luaL_optinteger(L,i,d) )
103#define wslua_optint32(L,i,d) (int32_t) ( luaL_optinteger(L,i,d) )
104#define wslua_optint64(L,i,d) (int64_t) ( luaL_optnumber(L,i,d) )
105#define wslua_optuint64(L,i,d) (uint64_t) ( luaL_optnumber(L,i,d) )
106
112#if LUA_VERSION_NUM < 503
113#define wslua_touint(L,i) (unsigned) ( lua_tounsigned(L,i) )
114#define wslua_touint32(L,i) (uint32_t) ( lua_tounsigned(L,i) )
115#define wslua_checkuint(L,i) (unsigned) ( luaL_checkunsigned(L,i) )
116#define wslua_checkuint32(L,i) (uint32_t) ( luaL_checkunsigned(L,i) )
117#define wslua_optuint(L,i,d) (unsigned) ( luaL_optunsigned(L,i,d) )
118#define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optunsigned(L,i,d) )
119#else
120#define wslua_touint(L,i) (unsigned) ( lua_tointeger(L,i) )
121#define wslua_touint32(L,i) (uint32_t) ( lua_tointeger(L,i) )
122#define wslua_checkuint(L,i) (unsigned) ( luaL_checkinteger(L,i) )
123#define wslua_checkuint32(L,i) (uint32_t) ( luaL_checkinteger(L,i) )
124#define wslua_optuint(L,i,d) (unsigned) ( luaL_optinteger(L,i,d) )
125#define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optinteger(L,i,d) )
126#endif
127
129 tvbuff_t* ws_tvb;
130 bool expired;
131 bool need_free;
132};
133
135 packet_info* ws_pinfo;
136 bool expired;
137};
138
140 struct _wslua_tvb* tvb;
141 unsigned offset;
142 unsigned len;
143};
144
145struct _wslua_tw {
147 bool expired;
148 void* close_cb_data;
149 char* title;
150};
151
152typedef struct _wslua_field_t {
153 int hfid;
154 int ett;
155 char* name;
156 char* abbrev;
157 char* blob;
158 enum ftenum type;
159 unsigned base;
160 const void* vs;
161 int valuestring_ref;
162 uint64_t mask;
164
165typedef struct _wslua_expert_field_t {
166 expert_field ids;
167 const char *abbrev;
168 const char *text;
169 int group;
170 int severity;
172
173typedef struct _wslua_pref_t {
174 char* name;
175 char* label;
176 char* desc;
177 pref_type_e type;
178 union {
179 bool b;
180 unsigned u;
181 char* s;
182 int e;
183 range_t *r;
184 void* p;
185 } value;
186 union {
187 uint32_t max_value;
188 struct {
195 struct {
198 char* default_s;
201 struct _wslua_pref_t* next;
202 struct _wslua_proto_t* proto;
203 int ref; /* Reference to enable Proto to deregister prefs. */
205
206typedef struct _wslua_proto_t {
207 char* name;
208 char* loname;
209 char* desc;
210 int hfid;
211 int ett;
212 wslua_pref_t prefs;
213 int fields;
214 int expert_info_table_ref;
216 module_t *prefs_module;
217 dissector_handle_t handle;
218 GArray *hfa;
219 GArray *etta;
220 GArray *eia;
221 bool is_postdissector;
222 bool expired;
224
225typedef struct _wslua_conv_data_t {
226 conversation_t* conv;
227 int data_ref;
229
230/* a "DissectorTable" object can be different things under the hood,
231 * since its heuristic_new() can create a heur_dissector_list_t that
232 * needs to be deregistered. */
234 dissector_table_t table;
235 heur_dissector_list_t heur_list;
236 const char* name;
237 const char* ui_name;
238 bool created;
239 bool expired;
240};
241
243 column_info* cinfo;
244 int col;
245 bool expired;
246};
247
249 column_info* cinfo;
250 bool expired;
251};
252
254 GHashTable *table;
255 bool is_allocated;
256 bool expired;
257};
258
260 proto_item* item;
261 proto_tree* tree;
262 bool expired;
263};
264
265// Internal structure for wslua_field.c to track info about registered fields.
267 char *name;
269};
270
272 field_info *ws_fi;
273 bool expired;
274};
275
276/*
277 * _func_saver stores function refs so that Lua won't garbage collect them prematurely.
278 * It is only used by tcp_dissect_pdus right now.
279 */
281 lua_State* state;
282 int get_len_ref;
283 int dissect_ref;
284};
285
286typedef void (*tap_extractor_t)(lua_State*,const void*);
287
289 char* name;
290 char* filter;
291 tap_extractor_t extractor;
292 lua_State* L;
293 int packet_ref;
294 int draw_ref;
295 int reset_ref;
296 bool all_fields;
297};
298
299/* a "File" object can be different things under the hood. It can either
300 be a FILE_T from wtap struct, which it is during read operations, or it
301 can be a wtap_dumper struct during write operations. A wtap_dumper struct
302 has a FILE_T member, but we can't only store its pointer here because
303 dump operations need the whole thing to write out with. Ugh. */
305 FILE_T file;
306 wtap_dumper *wdh; /* will be NULL during read usage */
307 bool expired;
308};
309
310/* a "CaptureInfo" object can also be different things under the hood. */
312 wtap *wth; /* will be NULL during write usage */
313 wtap_dumper *wdh; /* will be NULL during read usage */
314 bool expired;
315};
316
318 wtap_rec *rec;
319 bool expired;
320};
321
323 const wtap_rec *rec;
324 const uint8_t *pd;
325 bool expired;
326};
327
329 struct file_type_subtype_info finfo;
330 bool is_reader;
331 bool is_writer;
332 char* internal_description; /* XXX - this is redundant; finfo.description should suffice */
333 char* type;
334 char* extensions;
335 lua_State* L;
336 int read_open_ref;
337 int read_ref;
338 int seek_read_ref;
339 int read_close_ref;
340 int seq_read_close_ref;
341 int can_write_encap_ref;
342 int write_open_ref;
343 int write_ref;
344 int write_close_ref;
345 int file_type;
346 bool registered;
347 bool removed; /* This is set during reload Lua plugins */
348};
349
351 GDir* dir;
352 char* ext;
353 char* path;
354};
355
357 struct progdlg* pw;
358 char* title;
359 char* task;
360 bool stopped;
361};
362
363typedef struct { const char* name; tap_extractor_t extractor; } tappable_t;
364
365typedef struct {const char* str; enum ftenum id; } wslua_ft_types_t;
366typedef struct {const char* str; conversation_type id; } wslua_conv_types_t;
367
368typedef wslua_pref_t* Pref;
369typedef wslua_pref_t* Prefs;
370typedef struct _wslua_field_t* ProtoField;
371typedef struct _wslua_expert_field_t* ProtoExpert;
372typedef struct _wslua_proto_t* Proto;
373typedef struct _wslua_distbl_t* DissectorTable;
375typedef GByteArray* ByteArray;
376typedef gcry_cipher_hd_t* GcryptCipher;
377typedef struct _wslua_tvb* Tvb;
378typedef struct _wslua_tvbrange* TvbRange;
379typedef struct _wslua_col_info* Column;
380typedef struct _wslua_cols* Columns;
381typedef struct _wslua_pinfo* Pinfo;
382typedef struct _wslua_treeitem* TreeItem;
383typedef address* Address;
384typedef nstime_t* NSTime;
385typedef int64_t Int64;
386typedef uint64_t UInt64;
387typedef struct _wslua_header_field_info* Field;
388typedef struct _wslua_field_info* FieldInfo;
389typedef struct _wslua_tap* Listener;
390typedef struct _wslua_tw* TextWindow;
391typedef struct _wslua_progdlg* ProgDlg;
392typedef struct _wslua_file* File;
393typedef struct _wslua_captureinfo* CaptureInfo;
395typedef struct _wslua_rec* FrameInfo;
396typedef struct _wslua_const_rec* FrameInfoConst;
397typedef struct _wslua_filehandler* FileHandler;
398typedef wtap_dumper* Dumper;
399typedef struct lua_pseudo_header* PseudoHeader;
400typedef tvbparse_t* Parser;
401typedef tvbparse_wanted_t* Rule;
402typedef tvbparse_elem_t* Node;
403typedef tvbparse_action_t* Shortcut;
404typedef struct _wslua_dir* Dir;
405typedef struct _wslua_private_table* PrivateTable;
407typedef char* Struct;
408
409/*
410 * toXxx(L,idx) gets a Xxx from an index (Lua Error if fails)
411 * checkXxx(L,idx) gets a Xxx from an index after calling check_code (No Lua Error if it fails)
412 * pushXxx(L,xxx) pushes an Xxx into the stack
413 * isXxx(L,idx) tests whether we have an Xxx at idx
414 * shiftXxx(L,idx) removes and returns an Xxx from idx only if it has a type of Xxx, returns NULL otherwise
415 * WSLUA_CLASS_DEFINE must be used with a trailing ';'
416 * (a dummy typedef is used to be syntactically correct)
417 */
418#define WSLUA_CLASS_DEFINE(C,check_code) \
419 WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL)
420
421#define WSLUA_CLASS_DEFINE_BASE(C,check_code,retval) \
422C to##C(lua_State* L, int idx) { \
423 C* v = (C*)lua_touserdata (L, idx); \
424 if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)", idx, #C, lua_typename(L, lua_type(L, idx))); \
425 return v ? *v : retval; \
426} \
427C check##C(lua_State* L, int idx) { \
428 C* p; \
429 luaL_checktype(L,idx,LUA_TUSERDATA); \
430 p = (C*)luaL_checkudata(L, idx, #C); \
431 check_code; \
432 return p ? *p : retval; \
433} \
434C* push##C(lua_State* L, C v) { \
435 C* p; \
436 luaL_checkstack(L,2,"Unable to grow stack\n"); \
437 p = (C*)lua_newuserdata(L,sizeof(C)); *p = v; \
438 luaL_getmetatable(L, #C); lua_setmetatable(L, -2); \
439 return p; \
440}\
441bool is##C(lua_State* L,int i) { \
442 void *p; \
443 if(!lua_isuserdata(L,i)) return false; \
444 p = lua_touserdata(L, i); \
445 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
446 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
447 lua_pop(L, 2); \
448 return p ? true : false; \
449} \
450C shift##C(lua_State* L,int i) { \
451 C* p; \
452 if(!lua_isuserdata(L,i)) return retval; \
453 p = (C*)lua_touserdata(L, i); \
454 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
455 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
456 lua_pop(L, 2); \
457 if (p) { lua_remove(L,i); return *p; }\
458 else return retval;\
459} \
460typedef int dummy##C
461
463 const char *fieldname;
464 lua_CFunction getfunc;
465 lua_CFunction setfunc;
467
478extern int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, bool is_getter);
479
480#define WSLUA_TYPEOF_FIELD "__typeof"
481
482#ifdef HAVE_LUA
483
484/* temporary transition macro to reduce duplication in WSLUA_REGISTER_xxx. */
485#define WSLUA_REGISTER_GC(C) \
486 luaL_getmetatable(L, #C); \
487 /* add the '__gc' metamethod with a C-function named Class__gc */ \
488 /* this will force ALL wslua classes to have a Class__gc function defined, which is good */ \
489 lua_pushcfunction(L, C ## __gc); \
490 lua_setfield(L, -2, "__gc"); \
491 /* pop the metatable */ \
492 lua_pop(L, 1)
493
494#define __WSLUA_REGISTER_META(C, ATTRS) { \
495 const wslua_class C ## _class = { \
496 .name = #C, \
497 .instance_meta = C ## _meta, \
498 .attrs = ATTRS \
499 }; \
500 wslua_register_classinstance_meta(L, &C ## _class); \
501 WSLUA_REGISTER_GC(C); \
502}
503
504#define WSLUA_REGISTER_META(C) __WSLUA_REGISTER_META(C, NULL)
505#define WSLUA_REGISTER_META_WITH_ATTRS(C) \
506 __WSLUA_REGISTER_META(C, C ## _attributes)
507
508#define __WSLUA_REGISTER_CLASS(C, ATTRS) { \
509 const wslua_class C ## _class = { \
510 .name = #C, \
511 .class_methods = C ## _methods, \
512 .class_meta = C ## _meta, \
513 .instance_methods = C ## _methods, \
514 .instance_meta = C ## _meta, \
515 .attrs = ATTRS \
516 }; \
517 wslua_register_class(L, &C ## _class); \
518 WSLUA_REGISTER_GC(C); \
519}
520
521#define WSLUA_REGISTER_CLASS(C) __WSLUA_REGISTER_CLASS(C, NULL)
522#define WSLUA_REGISTER_CLASS_WITH_ATTRS(C) \
523 __WSLUA_REGISTER_CLASS(C, C ## _attributes)
524
525#define WSLUA_INIT(L) \
526 luaL_openlibs(L); \
527 wslua_register_classes(L); \
528 wslua_register_functions(L);
529
530#endif
531
532#define WSLUA_FUNCTION extern int
533/* This is for functions intended only to be used in init.lua */
534#define WSLUA_INTERNAL_FUNCTION extern int
535
536#define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }
537
538#define WSLUA_REGISTER extern int
539
540#define WSLUA_METHOD static int
541#define WSLUA_CONSTRUCTOR static int
542#define WSLUA_ATTR_SET static int
543#define WSLUA_ATTR_GET static int
544#define WSLUA_METAMETHOD static int
545
546#define WSLUA_METHODS static const luaL_Reg
547#define WSLUA_META static const luaL_Reg
548#define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name }
549#define WSLUA_CLASS_FNREG_ALIAS(class,aliasname,name) { #aliasname, class##_##name }
550#define WSLUA_CLASS_MTREG(class,name) { "__" #name, class##__##name }
551
552#define WSLUA_ATTRIBUTES static const wslua_attribute_table
553/* following are useful macros for the rows in the array created by above */
554#define WSLUA_ATTRIBUTE_RWREG(class,name) { #name, class##_get_##name, class##_set_##name }
555#define WSLUA_ATTRIBUTE_ROREG(class,name) { #name, class##_get_##name, NULL }
556#define WSLUA_ATTRIBUTE_WOREG(class,name) { #name, NULL, class##_set_##name }
557
558/* Body of a __pairs metamethod that hands the generic-for protocol
559 * a stateless iterator `C##_pairs_iter`. The iterator must accept
560 * (self, prev_key_or_nil) and return the next (key, value) pair or a
561 * single nil when done. Use inside a WSLUA_METAMETHOD body so the
562 * caller retains control of any doc comments shown in the manual. */
563#define WSLUA_STATELESS_PAIRS_BODY(C) \
564 check##C(L, 1); \
565 lua_pushcfunction(L, C##_pairs_iter); \
566 lua_pushvalue(L, 1); \
567 lua_pushnil(L); \
568 return 3
569
570#define WSLUA_ATTRIBUTE_FUNC_SETTER(C,field) \
571 static int C##_set_##field (lua_State* L) { \
572 C obj = check##C (L,1); \
573 if (! lua_isfunction(L,-1) ) \
574 return luaL_error(L, "%s's attribute `%s' must be a function", #C , #field ); \
575 if (obj->field##_ref != LUA_NOREF) \
576 /* there was one registered before, remove it */ \
577 luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \
578 obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
579 return 0; \
580 } \
581 /* silly little trick so we can add a semicolon after this macro */ \
582 typedef void __dummy##C##_set_##field
583
584#define WSLUA_ATTRIBUTE_GET(C,name,block) \
585 static int C##_get_##name (lua_State* L) { \
586 C obj = check##C (L,1); \
587 block \
588 return 1; \
589 } \
590 /* silly little trick so we can add a semicolon after this macro */ \
591 typedef void __dummy##C##_get_##name
592
593#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
594 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
595
596#define WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,name,member) \
597 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushinteger(L,(lua_Integer)(obj->member));})
598
599#define WSLUA_ATTRIBUTE_INTEGER_GETTER(C,member) \
600 WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,member,member)
601
602#define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
603 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
604
605#define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
606 WSLUA_ATTRIBUTE_GET(C,name, { \
607 lua_pushstring(L,obj->member); /* this pushes nil if obj->member is null */ \
608 })
609
610#define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
611 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
612
613#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \
614 WSLUA_ATTRIBUTE_GET(C,name, { \
615 char* str; \
616 if ((obj->member) && (obj->member->len > 0)) { \
617 if (wtap_block_get_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, &str) == WTAP_OPTTYPE_SUCCESS) { \
618 lua_pushstring(L,str); \
619 } \
620 } \
621 })
622
623/*
624 * XXX - we need to support Lua programs getting instances of a "multiple
625 * allowed" option other than the first option.
626 */
627#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \
628 WSLUA_ATTRIBUTE_GET(C,name, { \
629 char* str; \
630 if ((obj->member) && (obj->member->len > 0)) { \
631 if (wtap_block_get_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, &str) == WTAP_OPTTYPE_SUCCESS) { \
632 lua_pushstring(L,str); \
633 } \
634 } \
635 })
636
637#define WSLUA_ATTRIBUTE_SET(C,name,block) \
638 static int C##_set_##name (lua_State* L) { \
639 C obj = check##C (L,1); \
640 block; \
641 return 0; \
642 } \
643 /* silly little trick so we can add a semicolon after this macro */ \
644 typedef void __dummy##C##_set_##name
645
646#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_SETTER(C,name,member) \
647 WSLUA_ATTRIBUTE_SET(C,name, { \
648 if (! lua_isboolean(L,-1) ) \
649 return luaL_error(L, "%s's attribute `%s' must be a boolean", #C , #name ); \
650 obj->member = lua_toboolean(L,-1); \
651 })
652
653/* to make this integral-safe, we treat it as int32 and then cast
654 Note: This will truncate 64-bit integers (but then Lua itself only has doubles */
655#define WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,name,member,cast) \
656 WSLUA_ATTRIBUTE_SET(C,name, { \
657 if (! lua_isinteger(L,-1) ) \
658 return luaL_error(L, "%s's attribute `%s' must be an integer", #C , #name ); \
659 obj->member = (cast) wslua_toint32(L,-1); \
660 })
661
662#define WSLUA_ATTRIBUTE_INTEGER_SETTER(C,member,cast) \
663 WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,member,member,cast)
664
665#define WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,member,need_free) \
666 static int C##_set_##field (lua_State* L) { \
667 C obj = check##C (L,1); \
668 char* s = NULL; \
669 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
670 s = g_strdup(lua_tostring(L,-1)); \
671 } else { \
672 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
673 } \
674 if (obj->member != NULL && need_free) \
675 g_free((void*) obj->member); \
676 obj->member = s; \
677 return 0; \
678 } \
679 /* silly little trick so we can add a semicolon after this macro */ \
680 typedef void __dummy##C##_set_##field
681
682#define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \
683 WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free)
684
685#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_SETTER(C,field,member,option) \
686 static int C##_set_##field (lua_State* L) { \
687 C obj = check##C (L,1); \
688 char* s = NULL; \
689 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
690 s = g_strdup(lua_tostring(L,-1)); \
691 } else { \
692 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
693 } \
694 if ((obj->member) && (obj->member->len > 0)) { \
695 wtap_block_set_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, s, strlen(s)); \
696 } \
697 g_free(s); \
698 return 0; \
699 } \
700 /* silly little trick so we can add a semicolon after this macro */ \
701 typedef void __dummy##C##_set_##field
702
703#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_SETTER(C,field,member,option) \
704 static int C##_set_##field (lua_State* L) { \
705 C obj = check##C (L,1); \
706 char* s = NULL; \
707 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
708 s = g_strdup(lua_tostring(L,-1)); \
709 } else { \
710 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
711 } \
712 if ((obj->member) && (obj->member->len > 0)) { \
713 wtap_block_set_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, s, strlen(s)); \
714 } \
715 g_free(s); \
716 return 0; \
717 } \
718 /* silly little trick so we can add a semicolon after this macro */ \
719 typedef void __dummy##C##_set_##field
720
721#define WSLUA_ERROR(name,error) { luaL_error(L, "%s%s", #name ": ", error); }
722#define WSLUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_ARG_ ## name ## _ ## attr, #name ": " error); }
723#define WSLUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_OPTARG_##name##_ ##attr, #name ": " error); }
724
725#define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushboolean(L,v); lua_setglobal(L,n); }
726#define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,v); lua_setglobal(L,n); }
727#define WSLUA_REG_GLOBAL_INTEGER(L,n,v) { lua_pushinteger(L,v); lua_setglobal(L,n); }
728
729#define WSLUA_RETURN(i) return (i)
730
731#define WSLUA_API extern
732
733/* empty macro arguments trigger ISO C90 warnings, so do this */
734#define NOP (void)p
735
736#define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
737
738#define FAIL_ON_NULL_OR_EXPIRED(s) if (!*p) { \
739 luaL_argerror(L,idx,"null " s); \
740 } else if ((*p)->expired) { \
741 luaL_argerror(L,idx,"expired " s); \
742 }
743
744/* Clears or marks references that connects Lua to Wireshark structures */
745#define CLEAR_OUTSTANDING(C, marker, marker_val) void clear_outstanding_##C(void) { \
746 while (outstanding_##C->len) { \
747 C p = (C)g_ptr_array_remove_index_fast(outstanding_##C,0); \
748 if (p) { \
749 if (p->marker != marker_val) \
750 p->marker = marker_val; \
751 else \
752 g_free(p); \
753 } \
754 } \
755}
756
757#define WSLUA_CLASS_DECLARE(C) \
758extern C to##C(lua_State* L, int idx); \
759extern C check##C(lua_State* L, int idx); \
760extern C* push##C(lua_State* L, C v); \
761extern int C##_register(lua_State* L); \
762extern bool is##C(lua_State* L,int i); \
763extern C shift##C(lua_State* L,int i)
764
765
766/* Throws a Wireshark exception, catchable via normal exceptions.h routines. */
767#define THROW_LUA_ERROR(...) \
768 THROW_FORMATTED(DissectorError, __VA_ARGS__)
769
770/* Catches any Wireshark exceptions in code and convert it into a Lua error.
771 * Normal restrictions for TRY/CATCH apply, in particular, do not return!
772 *
773 * This means do not call lua[L]_error() inside code, as that longjmps out
774 * of the TRY block to the Lua pcall! Use THROW_LUA_ERROR, which is caught
775 * and then converted into a Lua error.
776 *
777 * XXX: We CATCH_ALL here, although there's little point in catching
778 * OutOfMemoryError here. (Is CATCH_BOUNDS_AND_DISSECTOR_ERRORS sufficient?)
779 * There are some Exceptions that we catch and show but don't want to add
780 * the Lua error malformed expert info to the tree: BoundsError,
781 * FragmentBoundsError, and ScsiBoundsError (show_exception doesn't consider
782 * those malformed). The traceback might (or might not) be useful for those.
783 * Putting an extra malformed expert info in the tree in the cases that are
784 * malformed seems not so bad, but we might want to reduce that. Perhaps
785 * at least we could have a separate LuaError type and not call show_exception
786 * for that (we still need to handle some Lua errors that don't use this in
787 * dissector_error_handler.)
788 */
789#define WRAP_NON_LUA_EXCEPTIONS(code) \
790{ \
791 volatile bool has_error = false; \
792 TRY { \
793 code \
794 } CATCH3(BoundsError, FragmentBoundsError, ScsiBoundsError) { \
795 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
796 } CATCH_ALL { \
797 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
798 lua_pushfstring(L, "%s: %s", __func__, GET_MESSAGE ? GET_MESSAGE : "Malformed packet"); \
799 has_error = true; \
800 } ENDTRY; \
801 if (has_error) { lua_error(L); } \
802}
803
804
805extern packet_info* lua_pinfo;
806extern TreeItem lua_tree;
807extern tvbuff_t* lua_tvb;
808extern bool lua_initialized;
809extern int lua_dissectors_table_ref;
810extern int lua_heur_dissectors_table_ref;
811extern const char* lua_app_env_var_prefix;
812extern GPtrArray* lua_outstanding_FuncSavers;
813
814WSLUA_DECLARE_CLASSES()
815WSLUA_DECLARE_FUNCTIONS()
816
824extern lua_State* wslua_state(void);
825
826
827/* wslua_internals.c */
834typedef struct _wslua_class {
835 const char *name;
836 const luaL_Reg *class_methods;
837 const luaL_Reg *class_meta;
838 const luaL_Reg *instance_methods;
839 const luaL_Reg *instance_meta;
842void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def);
843void wslua_register_class(lua_State *L, const wslua_class *cls_def);
844
854extern int wslua__concat(lua_State* L);
855
867extern bool wslua_toboolean(lua_State* L, int n);
868
878extern bool wslua_checkboolean(lua_State* L, int n);
879
888extern bool wslua_optbool(lua_State* L, int n, bool def);
889
897extern lua_Integer wslua_tointeger(lua_State* L, int n);
898
907extern int wslua_optboolint(lua_State* L, int n, int def);
908
917extern const char* wslua_checklstring_only(lua_State* L, int n, size_t *l);
918
926extern const char* wslua_checkstring_only(lua_State* L, int n);
927
935extern void wslua_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
936extern const char* wslua_typeof_unknown;
937extern const char* wslua_typeof(lua_State *L, int idx);
938extern bool wslua_get_table(lua_State *L, int idx, const char *name);
939extern bool wslua_get_field(lua_State *L, int idx, const char *name);
940extern int dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
941extern bool heur_dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
942
950extern expert_field* wslua_get_expert_field(const int group, const int severity);
951
958extern void wslua_prefs_changed(void);
959
965extern void proto_register_lua(void);
966
974extern GString* lua_register_all_taps(void);
975
983extern void wslua_prime_dfilter(epan_dissect_t *edt);
984
990extern bool wslua_has_field_extractors(void);
991
1000
1009extern int Proto_commit(lua_State* L);
1010
1018extern TreeItem create_TreeItem(proto_tree* tree, proto_item* item);
1019
1025extern void clear_outstanding_FuncSavers(lua_State* L);
1026
1035extern void Int64_pack(lua_State* L, luaL_Buffer *b, int idx, bool asLittleEndian);
1036
1045extern int Int64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
1046
1055extern void UInt64_pack(lua_State* L, luaL_Buffer *b, int idx, bool asLittleEndian);
1056
1065extern int UInt64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
1066
1077extern uint64_t getUInt64(lua_State *L, int i);
1078
1086extern Tvb* push_Tvb(lua_State* L, tvbuff_t* tvb);
1087
1095extern int push_wsluaTvb(lua_State* L, Tvb t);
1096
1106extern bool push_TvbRange(lua_State* L, tvbuff_t* tvb, int offset, int len);
1107
1113extern void clear_outstanding_Tvb(void);
1114
1118extern void clear_outstanding_TvbRange(void);
1119
1127extern Pinfo* push_Pinfo(lua_State* L, packet_info* p);
1128
1132extern void clear_outstanding_Pinfo(void);
1133
1137extern void clear_outstanding_Column(void);
1138
1142extern void clear_outstanding_Columns(void);
1143
1147extern void clear_outstanding_PrivateTable(void);
1148
1154extern int get_hf_wslua_text(void);
1155
1164extern TreeItem push_TreeItem(lua_State *L, proto_tree *tree, proto_item *item);
1165
1169extern void clear_outstanding_TreeItem(void);
1170
1178extern FieldInfo* push_FieldInfo(lua_State *L, field_info* f);
1179
1183extern void clear_outstanding_FieldInfo(void);
1184
1191extern void wslua_print_stack(char* s, lua_State* L);
1192
1202extern void wslua_init(register_cb cb, void *client_data, const char* app_env_var_prefix);
1203extern void wslua_early_cleanup(void);
1204
1210extern void wslua_cleanup(void);
1211
1218extern tap_extractor_t wslua_get_tap_extractor(const char* name);
1219
1226extern int wslua_set_tap_enums(lua_State* L);
1227
1228extern ProtoField wslua_is_field_available(lua_State* L, const char* field_abbr);
1229
1236extern char* wslua_get_actual_filename(const char* fname);
1237
1250extern int wslua_bin2hex(lua_State* L, const uint8_t* data, const unsigned len, const bool lowercase, const char* sep);
1251
1261extern int wslua_hex2bin(lua_State* L, const char* data, const unsigned len, const char* sep);
1262
1269extern int luaopen_rex_pcre2(lua_State *L);
1270
1276extern const char* get_current_plugin_version(void);
1277
1281extern void clear_current_plugin_version(void);
1282
1291extern int wslua_deregister_heur_dissectors(lua_State* L);
1292
1301extern int wslua_deregister_protocols(lua_State* L);
1302
1311extern int wslua_deregister_dissector_tables(lua_State* L);
1312extern int wslua_deregister_listeners(lua_State* L);
1313
1320extern int wslua_deregister_fields(lua_State* L);
1321
1329extern int wslua_deregister_filehandlers(lua_State* L);
1330
1337extern void wslua_deregister_menus(void);
1338
1347extern void wslua_init_wtap_filetypes(lua_State* L);
1348
1355
1356#endif
1357
1358/*
1359 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1360 *
1361 * Local variables:
1362 * c-basic-offset: 4
1363 * tab-width: 8
1364 * indent-tabs-mode: nil
1365 * End:
1366 *
1367 * vi: set shiftwidth=4 tabstop=8 expandtab:
1368 * :indentSize=4:tabSize=8:noTabs=true:
1369 */
Definition address.h:55
Definition tap-funnel.c:21
Definition proto.h:762
Definition packet_info.h:40
Definition proto.h:902
Definition tvbparse.h:160
Represents an instance of a per-packet parser for tvbuff data.
Definition tvbparse.h:148
Describes a parsing rule or expectation for a tvbuff parser.
Definition tvbparse.h:93
Definition uat.h:232
Definition wslua.h:462
Definition wslua.h:311
Type for defining new classes.
Definition wslua.h:834
const wslua_attribute_table * attrs
Definition wslua.h:840
const luaL_Reg * class_meta
Definition wslua.h:837
const char * name
Definition wslua.h:835
const luaL_Reg * class_methods
Definition wslua.h:836
const luaL_Reg * instance_methods
Definition wslua.h:838
const luaL_Reg * instance_meta
Definition wslua.h:839
Definition wslua.h:242
Definition wslua.h:248
Definition wslua.h:322
Definition wslua.h:225
Definition wslua.h:350
Definition wslua.h:233
Definition wslua.h:165
Definition wslua.h:271
Definition wslua.h:152
Definition wslua.h:304
Definition wslua.h:328
Definition wslua.h:280
Definition wslua.h:266
Definition wslua.h:134
Definition wslua.h:173
uat_field_t * uat_field_list
Definition wslua.h:196
struct _wslua_pref_t::@509::@511 uat_field_list_info
bool radio_buttons
Definition wslua.h:190
struct _wslua_pref_t::@509::@510 enum_info
char * default_s
Definition wslua.h:198
uint32_t max_value
Definition wslua.h:187
const enum_val_t * enumvals
Definition wslua.h:189
union _wslua_pref_t::@509 info
Definition wslua.h:253
Definition wslua.h:356
Definition wslua.h:206
Definition wslua.h:317
Definition wslua.h:288
Definition wslua.h:259
Definition wslua.h:128
Definition wslua.h:139
Definition wslua.h:145
Definition packet-bt-dht.c:97
Definition conversation.h:227
Definition packet.c:852
Definition packet.c:97
Definition params.h:20
Definition column-info.h:59
Definition epan_dissect.h:25
Definition range.h:38
Definition expert.h:36
Definition expert.c:48
Definition proto.h:811
Definition wtap.h:1934
Definition packet.c:188
Definition wslua_dumper.c:58
Definition nstime.h:26
Definition prefs-int.h:24
Definition progress_frame.h:31
Definition wslua.h:363
Definition tvbuff-int.h:33
Definition wslua.h:366
Definition wslua.h:365
Wiretap dumper handle and associated state.
Definition wtap_module.h:163
Definition file_wrappers.c:96
Definition wtap.h:1512
Definition wtap_module.h:58
void clear_outstanding_TvbRange(void)
Clears all outstanding TvbRange objects.
Definition wslua_tvb.c:396
void wslua_init(register_cb cb, void *client_data, const char *app_env_var_prefix)
Initialize Wireshark Lua support.
Definition init_wslua.c:1639
tap_extractor_t wslua_get_tap_extractor(const char *name)
Retrieves a tap extractor by name.
void wslua_print_stack(char *s, lua_State *L)
Prints the stack of a Lua state with a given prefix.
Definition wslua_internals.c:167
int push_wsluaTvb(lua_State *L, Tvb t)
Pushes a Tvb object onto the Lua stack.
Definition wslua_tvb.c:77
const char * wslua_checklstring_only(lua_State *L, int n, size_t *l)
Checks if the value at the given index is a Lua string and returns it.
Definition wslua_internals.c:119
const char * wslua_checkstring_only(lua_State *L, int n)
Checks if a Lua value at a given index is a string.
Definition wslua_internals.c:129
void lua_prime_all_fields(proto_tree *tree)
Primes all fields in the protocol tree.
void clear_outstanding_Column(void)
Clears all outstanding Column objects.
Definition wslua_column.c:28
int UInt64_unpack(lua_State *L, const char *buff, bool asLittleEndian)
Unpacks a 64-bit unsigned integer from a buffer with specified endianness and pushes it onto the Lua ...
Definition wslua_int64.c:661
void wslua_cleanup(void)
Cleans up Lua resources.
Definition init_wslua.c:2086
int wslua_deregister_protocols(lua_State *L)
Deregisters all Lua-based protocol dissectors.
Definition wslua_proto.c:791
TreeItem create_TreeItem(proto_tree *tree, proto_item *item)
Creates a new TreeItem.
Definition wslua_tree.c:43
FieldInfo * push_FieldInfo(lua_State *L, field_info *f)
Pushes a field information object onto the Lua stack.
Definition wslua_field.c:36
void wslua_register_class(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:547
const char * get_current_plugin_version(void)
Get the current plugin version.
Definition wslua_utility.c:32
int wslua_deregister_dissector_tables(lua_State *L)
Deregisters all registered dissector tables.
Definition wslua_dissector.c:1111
int wslua_deregister_filehandlers(lua_State *L)
Deregisters file handlers and menus in Wireshark's Lua environment.
int luaopen_rex_pcre2(lua_State *L)
Open the Lua library for PCRE2 regular expressions.
Definition lpcre2.c:484
void wslua_prefs_changed(void)
Notify Lua scripts that preferences have changed.
Definition init_wslua.c:656
lua_State * wslua_state(void)
Retrieves the Lua state associated with Wireshark.
Definition init_wslua.c:2095
Pinfo * push_Pinfo(lua_State *L, packet_info *p)
Pushes a packet information structure onto the Lua stack.
Definition wslua_pinfo.c:39
void clear_outstanding_Pinfo(void)
Clears all outstanding Pinfo objects.
Definition wslua_pinfo.c:36
bool wslua_has_field_extractors(void)
Checks if there are any registered field extractors.
Definition wslua_field.c:521
int wslua_deregister_fields(lua_State *L)
Deregisters Lua fields.
int wslua_optboolint(lua_State *L, int n, int def)
Retrieves an optional boolean or integer value from the Lua stack.
Definition wslua_internals.c:102
int get_hf_wslua_text(void)
Retrieves the value of hf_wslua_text.
Definition init_wslua.c:187
bool wslua_optbool(lua_State *L, int n, bool def)
Checks if a Lua value at a given index is a boolean and returns its value, or a default value if not.
Definition wslua_internals.c:69
int wslua_deregister_heur_dissectors(lua_State *L)
Deregisters all Lua-based heuristics dissectors.
Definition wslua_proto.c:775
int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, bool is_getter)
Registers attributes for a Lua table.
void UInt64_pack(lua_State *L, luaL_Buffer *b, int idx, bool asLittleEndian)
Packs a 64-bit unsigned integer into a Lua string buffer with specified endianness.
Definition wslua_int64.c:620
void wslua_deregister_menus(void)
Deregisters all menus registered by Wireshark Lua.
Definition wslua_gui.c:103
void clear_current_plugin_version(void)
Clear the current plugin version.
Definition wslua_utility.c:36
void clear_outstanding_Columns(void)
Clears all outstanding Column objects.
Definition wslua_column.c:29
void clear_outstanding_FieldInfo(void)
Clears any outstanding FieldInfo structures.
Definition wslua_field.c:44
int wslua__concat(lua_State *L)
Concatenates two objects to a string.
Definition wslua_internals.c:27
bool push_TvbRange(lua_State *L, tvbuff_t *tvb, int offset, int len)
Pushes a TvbRange object onto the Lua stack.
Definition wslua_tvb.c:404
expert_field * wslua_get_expert_field(const int group, const int severity)
Retrieves an expert field based on group and severity.
Definition init_wslua.c:1170
bool wslua_toboolean(lua_State *L, int n)
Converts a Lua value to a boolean.
Definition wslua_internals.c:44
ProtoField wslua_is_field_available(lua_State *L, const char *field_abbr)
Definition wslua_proto.c:744
int Proto_commit(lua_State *L)
Commits protocol changes.
Definition wslua_proto.c:875
int Int64_unpack(lua_State *L, const char *buff, bool asLittleEndian)
Unpacks a 64-bit integer from a buffer with specified endianness and pushes it onto the Lua stack.
Definition wslua_int64.c:169
int wslua_bin2hex(lua_State *L, const uint8_t *data, const unsigned len, const bool lowercase, const char *sep)
Convert binary data to hexadecimal string.
Definition wslua_internals.c:318
void clear_outstanding_Tvb(void)
Clears all outstanding Tvb objects.
Definition wslua_tvb.c:98
void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:470
void wslua_init_wtap_filetypes(lua_State *L)
Initialize Wireshark Lua file types.
Definition wslua_wtap.c:111
struct _wslua_class wslua_class
Type for defining new classes.
Tvb * push_Tvb(lua_State *L, tvbuff_t *tvb)
Pushes a tvbuff_t to the Lua stack as a Tvb object.
Definition wslua_tvb.c:106
void proto_register_lua(void)
Registers the Lua protocol.
char * wslua_get_actual_filename(const char *fname)
Retrieves the actual filename with normalized path separators.
Definition wslua_utility.c:373
const wslua_conv_types_t * wslua_inspect_convtype_enum(void)
Retrieves the enumeration of conversation types for Lua inspection.
Definition wslua_conversation.c:77
bool wslua_checkboolean(lua_State *L, int n)
Checks if a Lua value at a given index is a boolean.
Definition wslua_internals.c:60
int wslua_set_tap_enums(lua_State *L)
Set tap enumerations in Lua.
void clear_outstanding_FuncSavers(lua_State *L)
Clears outstanding function savers associated with a Lua state.
Definition wslua_proto.c:49
void clear_outstanding_PrivateTable(void)
Clears any outstanding PrivateTable entries.
Definition wslua_pinfo.c:37
GString * lua_register_all_taps(void)
Registers all Lua taps.
uint64_t getUInt64(lua_State *L, int i)
Retrieves a 64-bit unsigned integer from the Lua stack.
Definition wslua_int64.c:599
lua_Integer wslua_tointeger(lua_State *L, int n)
Converts a Lua value to an integer.
Definition wslua_internals.c:85
void clear_outstanding_TreeItem(void)
Clears all outstanding TreeItem objects.
Definition wslua_tree.c:53
void wslua_prime_dfilter(epan_dissect_t *edt)
Prime the dissector filter with a protocol tree.
Definition wslua_field.c:514
TreeItem push_TreeItem(lua_State *L, proto_tree *tree, proto_item *item)
Pushes a TreeItem onto the Lua stack.
Definition wslua_tree.c:30
int wslua_hex2bin(lua_State *L, const char *data, const unsigned len, const char *sep)
Convert hexadecimal string to binary data.
Definition wslua_internals.c:375
void Int64_pack(lua_State *L, luaL_Buffer *b, int idx, bool asLittleEndian)
Packs a 64-bit integer into a Lua string using the specified endianness.
Definition wslua_int64.c:128
void wslua_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
Set functions in a Lua table.
Definition wslua_internals.c:134