Wireshark 4.5.0
The Wireshark network protocol analyzer
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 <luis@ontanon.org>
7 * (c) 2007, Tamas Regos <tamas.regos@ericsson.com>
8 * (c) 2008, Balint Reczey <balint.reczey@ericsson.com>
9 * (c) 2025, Bartis Csaba <bracsek@bracsek.eu>
10 *
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
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/proto.h>
45#include <epan/epan_dissect.h>
46#include <epan/tap.h>
47#include <epan/column-utils.h>
48#include <wsutil/filesystem.h>
49#include <wsutil/wsgcrypt.h>
50#include <epan/funnel.h>
51#include <epan/tvbparse.h>
52#include <epan/epan.h>
53#include <epan/expert.h>
54#include <epan/exceptions.h>
55#include <epan/show_exception.h>
56#include <epan/conversation.h>
57
58#include <epan/wslua/declare_wslua.h>
59
64#define WSLUA_INIT_ROUTINES "init_routines"
65#define WSLUA_PREFS_CHANGED "prefs_changed"
66
67/* type conversion macros - lua_Number is a double, so casting isn't kosher; and
68 using Lua's already-available lua_tointeger() and luaL_checkinteger() might be
69 different on different machines; so use these instead please!
70
71 It can be important to choose the correct version of signed or unsigned
72 conversion macros; don't assume that you can freely convert to the signed
73 or unsigned integer of the same size later:
74
75 On 32-bit Windows x86, Lua 5.2 and earlier must use lua_tounsigned() and
76 luaL_checkunsigned() due to the use of float to integer inlined assembly.
77 (#18367)
78 On ARM, casting from a negative floating point number to an unsigned integer
79 type doesn't perform wraparound conversion in the same way as casting from
80 float to the same size signed integer then to unsigned does, unlike x86[-64].
81 (Commit 15392c324d5eaefcaa298cdee09cd5b40b12e09c)
82
83 On Lua 5.3 and later, numbers are stored as a kind of union between
84 Lua_Number and Lua_Integer. On 5.2 and earlier. all numbers are stored
85 as Lua_Number internally.
86
87 Be careful about using the 64-bit functions, as they convert from double
88 and lose precision at high values. See wslua_int64.c and the types there.
89 TODO: Check if Lua_Integer is 64 bit on Lua 5.3 and later.
90*/
91#define wslua_toint(L,i) (int) ( lua_tointeger(L,i) )
92#define wslua_toint32(L,i) (int32_t) ( lua_tointeger(L,i) )
93#define wslua_toint64(L,i) (int64_t) ( lua_tonumber(L,i) )
94#define wslua_touint64(L,i) (uint64_t) ( lua_tonumber(L,i) )
95
96#define wslua_checkint(L,i) (int) ( luaL_checkinteger(L,i) )
97#define wslua_checkint32(L,i) (int32_t) ( luaL_checkinteger(L,i) )
98#define wslua_checkint64(L,i) (int64_t) ( luaL_checknumber(L,i) )
99#define wslua_checkuint64(L,i) (uint64_t) ( luaL_checknumber(L,i) )
100
101#define wslua_optint(L,i,d) (int) ( luaL_optinteger(L,i,d) )
102#define wslua_optint32(L,i,d) (int32_t) ( luaL_optinteger(L,i,d) )
103#define wslua_optint64(L,i,d) (int64_t) ( luaL_optnumber(L,i,d) )
104#define wslua_optuint64(L,i,d) (uint64_t) ( luaL_optnumber(L,i,d) )
105
111#if LUA_VERSION_NUM < 503
112#define wslua_touint(L,i) (unsigned) ( lua_tounsigned(L,i) )
113#define wslua_touint32(L,i) (uint32_t) ( lua_tounsigned(L,i) )
114#define wslua_checkuint(L,i) (unsigned) ( luaL_checkunsigned(L,i) )
115#define wslua_checkuint32(L,i) (uint32_t) ( luaL_checkunsigned(L,i) )
116#define wslua_optuint(L,i,d) (unsigned) ( luaL_optunsigned(L,i,d) )
117#define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optunsigned(L,i,d) )
118#else
119#define wslua_touint(L,i) (unsigned) ( lua_tointeger(L,i) )
120#define wslua_touint32(L,i) (uint32_t) ( lua_tointeger(L,i) )
121#define wslua_checkuint(L,i) (unsigned) ( luaL_checkinteger(L,i) )
122#define wslua_checkuint32(L,i) (uint32_t) ( luaL_checkinteger(L,i) )
123#define wslua_optuint(L,i,d) (unsigned) ( luaL_optinteger(L,i,d) )
124#define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optinteger(L,i,d) )
125#endif
126
128 tvbuff_t* ws_tvb;
129 bool expired;
130 bool need_free;
131};
132
134 packet_info* ws_pinfo;
135 bool expired;
136};
137
139 struct _wslua_tvb* tvb;
140 int offset;
141 int len;
142};
143
144struct _wslua_tw {
146 bool expired;
147 void* close_cb_data;
148};
149
150typedef struct _wslua_field_t {
151 int hfid;
152 int ett;
153 char* name;
154 char* abbrev;
155 char* blob;
156 enum ftenum type;
157 unsigned base;
158 const void* vs;
159 int valuestring_ref;
160 uint64_t mask;
162
163typedef struct _wslua_expert_field_t {
164 expert_field ids;
165 const char *abbrev;
166 const char *text;
167 int group;
168 int severity;
170
175typedef enum {
176 PREF_UINT,
177 PREF_BOOL,
178 PREF_ENUM,
179 PREF_STRING,
180 PREF_RANGE,
181 PREF_STATIC_TEXT,
182 PREF_UAT,
183 PREF_OBSOLETE
185
186typedef struct _wslua_pref_t {
187 char* name;
188 char* label;
189 char* desc;
190 pref_type_t type;
191 union {
192 bool b;
193 unsigned u;
194 char* s;
195 int e;
196 range_t *r;
197 void* p;
198 } value;
199 union {
200 uint32_t max_value;
201 struct {
208 struct {
211 char* default_s;
214 struct _wslua_pref_t* next;
215 struct _wslua_proto_t* proto;
216 int ref; /* Reference to enable Proto to deregister prefs. */
218
219typedef struct _wslua_proto_t {
220 char* name;
221 char* loname;
222 char* desc;
223 int hfid;
224 int ett;
225 wslua_pref_t prefs;
226 int fields;
227 int expert_info_table_ref;
229 module_t *prefs_module;
230 dissector_handle_t handle;
231 GArray *hfa;
232 GArray *etta;
233 GArray *eia;
234 bool is_postdissector;
235 bool expired;
237
238typedef struct _wslua_conv_data_t {
239 conversation_t* conv;
240 int data_ref;
242
243/* a "DissectorTable" object can be different things under the hood,
244 * since its heuristic_new() can create a heur_dissector_list_t that
245 * needs to be deregistered. */
247 dissector_table_t table;
248 heur_dissector_list_t heur_list;
249 const char* name;
250 const char* ui_name;
251 bool created;
252 bool expired;
253};
254
256 column_info* cinfo;
257 int col;
258 bool expired;
259};
260
262 column_info* cinfo;
263 bool expired;
264};
265
267 GHashTable *table;
268 bool is_allocated;
269 bool expired;
270};
271
273 proto_item* item;
274 proto_tree* tree;
275 bool expired;
276};
277
278// Internal structure for wslua_field.c to track info about registered fields.
280 char *name;
282};
283
285 field_info *ws_fi;
286 bool expired;
287};
288
289typedef void (*tap_extractor_t)(lua_State*,const void*);
290
292 char* name;
293 char* filter;
294 tap_extractor_t extractor;
295 lua_State* L;
296 int packet_ref;
297 int draw_ref;
298 int reset_ref;
299 bool all_fields;
300};
301
302/* a "File" object can be different things under the hood. It can either
303 be a FILE_T from wtap struct, which it is during read operations, or it
304 can be a wtap_dumper struct during write operations. A wtap_dumper struct
305 has a FILE_T member, but we can't only store its pointer here because
306 dump operations need the whole thing to write out with. Ugh. */
308 FILE_T file;
309 wtap_dumper *wdh; /* will be NULL during read usage */
310 bool expired;
311};
312
313/* a "CaptureInfo" object can also be different things under the hood. */
315 wtap *wth; /* will be NULL during write usage */
316 wtap_dumper *wdh; /* will be NULL during read usage */
317 bool expired;
318};
319
321 wtap_rec *rec;
322 bool expired;
323};
324
326 const wtap_rec *rec;
327 const uint8_t *pd;
328 bool expired;
329};
330
332 struct file_type_subtype_info finfo;
333 bool is_reader;
334 bool is_writer;
335 char* internal_description; /* XXX - this is redundant; finfo.description should suffice */
336 char* type;
337 char* extensions;
338 lua_State* L;
339 int read_open_ref;
340 int read_ref;
341 int seek_read_ref;
342 int read_close_ref;
343 int seq_read_close_ref;
344 int can_write_encap_ref;
345 int write_open_ref;
346 int write_ref;
347 int write_close_ref;
348 int file_type;
349 bool registered;
350 bool removed; /* This is set during reload Lua plugins */
351};
352
354 GDir* dir;
355 char* ext;
356};
357
359 struct progdlg* pw;
360 char* title;
361 char* task;
362 bool stopped;
363};
364
365typedef struct { const char* name; tap_extractor_t extractor; } tappable_t;
366
367typedef struct {const char* str; enum ftenum id; } wslua_ft_types_t;
368typedef struct {const char* str; conversation_type id; } wslua_conv_types_t;
369
370typedef wslua_pref_t* Pref;
371typedef wslua_pref_t* Prefs;
372typedef struct _wslua_field_t* ProtoField;
373typedef struct _wslua_expert_field_t* ProtoExpert;
374typedef struct _wslua_proto_t* Proto;
375typedef struct _wslua_distbl_t* DissectorTable;
377typedef GByteArray* ByteArray;
378typedef gcry_cipher_hd_t* GcryptCipher;
379typedef struct _wslua_tvb* Tvb;
380typedef struct _wslua_tvbrange* TvbRange;
381typedef struct _wslua_col_info* Column;
382typedef struct _wslua_cols* Columns;
383typedef struct _wslua_pinfo* Pinfo;
384typedef struct _wslua_treeitem* TreeItem;
385typedef address* Address;
386typedef nstime_t* NSTime;
387typedef int64_t Int64;
388typedef uint64_t UInt64;
389typedef struct _wslua_header_field_info* Field;
390typedef struct _wslua_field_info* FieldInfo;
391typedef struct _wslua_tap* Listener;
392typedef struct _wslua_tw* TextWindow;
393typedef struct _wslua_progdlg* ProgDlg;
394typedef struct _wslua_file* File;
395typedef struct _wslua_captureinfo* CaptureInfo;
397typedef struct _wslua_rec* FrameInfo;
398typedef struct _wslua_const_rec* FrameInfoConst;
399typedef struct _wslua_filehandler* FileHandler;
400typedef wtap_dumper* Dumper;
401typedef struct lua_pseudo_header* PseudoHeader;
402typedef tvbparse_t* Parser;
403typedef tvbparse_wanted_t* Rule;
404typedef tvbparse_elem_t* Node;
405typedef tvbparse_action_t* Shortcut;
406typedef struct _wslua_dir* Dir;
407typedef struct _wslua_private_table* PrivateTable;
409typedef char* Struct;
410
411/*
412 * toXxx(L,idx) gets a Xxx from an index (Lua Error if fails)
413 * checkXxx(L,idx) gets a Xxx from an index after calling check_code (No Lua Error if it fails)
414 * pushXxx(L,xxx) pushes an Xxx into the stack
415 * isXxx(L,idx) tests whether we have an Xxx at idx
416 * shiftXxx(L,idx) removes and returns an Xxx from idx only if it has a type of Xxx, returns NULL otherwise
417 * WSLUA_CLASS_DEFINE must be used with a trailing ';'
418 * (a dummy typedef is used to be syntactically correct)
419 */
420#define WSLUA_CLASS_DEFINE(C,check_code) \
421 WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL)
422
423#define WSLUA_CLASS_DEFINE_BASE(C,check_code,retval) \
424C to##C(lua_State* L, int idx) { \
425 C* v = (C*)lua_touserdata (L, idx); \
426 if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)", idx, #C, lua_typename(L, lua_type(L, idx))); \
427 return v ? *v : retval; \
428} \
429C check##C(lua_State* L, int idx) { \
430 C* p; \
431 luaL_checktype(L,idx,LUA_TUSERDATA); \
432 p = (C*)luaL_checkudata(L, idx, #C); \
433 check_code; \
434 return p ? *p : retval; \
435} \
436C* push##C(lua_State* L, C v) { \
437 C* p; \
438 luaL_checkstack(L,2,"Unable to grow stack\n"); \
439 p = (C*)lua_newuserdata(L,sizeof(C)); *p = v; \
440 luaL_getmetatable(L, #C); lua_setmetatable(L, -2); \
441 return p; \
442}\
443bool is##C(lua_State* L,int i) { \
444 void *p; \
445 if(!lua_isuserdata(L,i)) return false; \
446 p = lua_touserdata(L, i); \
447 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
448 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
449 lua_pop(L, 2); \
450 return p ? true : false; \
451} \
452C shift##C(lua_State* L,int i) { \
453 C* p; \
454 if(!lua_isuserdata(L,i)) return retval; \
455 p = (C*)lua_touserdata(L, i); \
456 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
457 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
458 lua_pop(L, 2); \
459 if (p) { lua_remove(L,i); return *p; }\
460 else return retval;\
461} \
462typedef int dummy##C
463
465 const char *fieldname;
466 lua_CFunction getfunc;
467 lua_CFunction setfunc;
469extern int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, bool is_getter);
470
471#define WSLUA_TYPEOF_FIELD "__typeof"
472
473#ifdef HAVE_LUA
474
475/* temporary transition macro to reduce duplication in WSLUA_REGISTER_xxx. */
476#define WSLUA_REGISTER_GC(C) \
477 luaL_getmetatable(L, #C); \
478 /* add the '__gc' metamethod with a C-function named Class__gc */ \
479 /* this will force ALL wslua classes to have a Class__gc function defined, which is good */ \
480 lua_pushcfunction(L, C ## __gc); \
481 lua_setfield(L, -2, "__gc"); \
482 /* pop the metatable */ \
483 lua_pop(L, 1)
484
485#define __WSLUA_REGISTER_META(C, ATTRS) { \
486 const wslua_class C ## _class = { \
487 .name = #C, \
488 .instance_meta = C ## _meta, \
489 .attrs = ATTRS \
490 }; \
491 wslua_register_classinstance_meta(L, &C ## _class); \
492 WSLUA_REGISTER_GC(C); \
493}
494
495#define WSLUA_REGISTER_META(C) __WSLUA_REGISTER_META(C, NULL)
496#define WSLUA_REGISTER_META_WITH_ATTRS(C) \
497 __WSLUA_REGISTER_META(C, C ## _attributes)
498
499#define __WSLUA_REGISTER_CLASS(C, ATTRS) { \
500 const wslua_class C ## _class = { \
501 .name = #C, \
502 .class_methods = C ## _methods, \
503 .class_meta = C ## _meta, \
504 .instance_methods = C ## _methods, \
505 .instance_meta = C ## _meta, \
506 .attrs = ATTRS \
507 }; \
508 wslua_register_class(L, &C ## _class); \
509 WSLUA_REGISTER_GC(C); \
510}
511
512#define WSLUA_REGISTER_CLASS(C) __WSLUA_REGISTER_CLASS(C, NULL)
513#define WSLUA_REGISTER_CLASS_WITH_ATTRS(C) \
514 __WSLUA_REGISTER_CLASS(C, C ## _attributes)
515
516#define WSLUA_INIT(L) \
517 luaL_openlibs(L); \
518 wslua_register_classes(L); \
519 wslua_register_functions(L);
520
521#endif
522
523#define WSLUA_FUNCTION extern int
524/* This is for functions intended only to be used in init.lua */
525#define WSLUA_INTERNAL_FUNCTION extern int
526
527#define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }
528
529#define WSLUA_REGISTER extern int
530
531#define WSLUA_METHOD static int
532#define WSLUA_CONSTRUCTOR static int
533#define WSLUA_ATTR_SET static int
534#define WSLUA_ATTR_GET static int
535#define WSLUA_METAMETHOD static int
536
537#define WSLUA_METHODS static const luaL_Reg
538#define WSLUA_META static const luaL_Reg
539#define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name }
540#define WSLUA_CLASS_FNREG_ALIAS(class,aliasname,name) { #aliasname, class##_##name }
541#define WSLUA_CLASS_MTREG(class,name) { "__" #name, class##__##name }
542
543#define WSLUA_ATTRIBUTES static const wslua_attribute_table
544/* following are useful macros for the rows in the array created by above */
545#define WSLUA_ATTRIBUTE_RWREG(class,name) { #name, class##_get_##name, class##_set_##name }
546#define WSLUA_ATTRIBUTE_ROREG(class,name) { #name, class##_get_##name, NULL }
547#define WSLUA_ATTRIBUTE_WOREG(class,name) { #name, NULL, class##_set_##name }
548
549#define WSLUA_ATTRIBUTE_FUNC_SETTER(C,field) \
550 static int C##_set_##field (lua_State* L) { \
551 C obj = check##C (L,1); \
552 if (! lua_isfunction(L,-1) ) \
553 return luaL_error(L, "%s's attribute `%s' must be a function", #C , #field ); \
554 if (obj->field##_ref != LUA_NOREF) \
555 /* there was one registered before, remove it */ \
556 luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \
557 obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
558 return 0; \
559 } \
560 /* silly little trick so we can add a semicolon after this macro */ \
561 typedef void __dummy##C##_set_##field
562
563#define WSLUA_ATTRIBUTE_GET(C,name,block) \
564 static int C##_get_##name (lua_State* L) { \
565 C obj = check##C (L,1); \
566 block \
567 return 1; \
568 } \
569 /* silly little trick so we can add a semicolon after this macro */ \
570 typedef void __dummy##C##_get_##name
571
572#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
573 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
574
575#define WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,name,member) \
576 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushinteger(L,(lua_Integer)(obj->member));})
577
578#define WSLUA_ATTRIBUTE_INTEGER_GETTER(C,member) \
579 WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,member,member)
580
581#define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
582 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
583
584#define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
585 WSLUA_ATTRIBUTE_GET(C,name, { \
586 lua_pushstring(L,obj->member); /* this pushes nil if obj->member is null */ \
587 })
588
589#define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
590 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
591
592#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \
593 WSLUA_ATTRIBUTE_GET(C,name, { \
594 char* str; \
595 if ((obj->member) && (obj->member->len > 0)) { \
596 if (wtap_block_get_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, &str) == WTAP_OPTTYPE_SUCCESS) { \
597 lua_pushstring(L,str); \
598 } \
599 } \
600 })
601
602/*
603 * XXX - we need to support Lua programs getting instances of a "multiple
604 * allowed" option other than the first option.
605 */
606#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \
607 WSLUA_ATTRIBUTE_GET(C,name, { \
608 char* str; \
609 if ((obj->member) && (obj->member->len > 0)) { \
610 if (wtap_block_get_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, &str) == WTAP_OPTTYPE_SUCCESS) { \
611 lua_pushstring(L,str); \
612 } \
613 } \
614 })
615
616#define WSLUA_ATTRIBUTE_SET(C,name,block) \
617 static int C##_set_##name (lua_State* L) { \
618 C obj = check##C (L,1); \
619 block; \
620 return 0; \
621 } \
622 /* silly little trick so we can add a semicolon after this macro */ \
623 typedef void __dummy##C##_set_##name
624
625#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_SETTER(C,name,member) \
626 WSLUA_ATTRIBUTE_SET(C,name, { \
627 if (! lua_isboolean(L,-1) ) \
628 return luaL_error(L, "%s's attribute `%s' must be a boolean", #C , #name ); \
629 obj->member = lua_toboolean(L,-1); \
630 })
631
632/* to make this integral-safe, we treat it as int32 and then cast
633 Note: This will truncate 64-bit integers (but then Lua itself only has doubles */
634#define WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,name,member,cast) \
635 WSLUA_ATTRIBUTE_SET(C,name, { \
636 if (! lua_isinteger(L,-1) ) \
637 return luaL_error(L, "%s's attribute `%s' must be an integer", #C , #name ); \
638 obj->member = (cast) wslua_toint32(L,-1); \
639 })
640
641#define WSLUA_ATTRIBUTE_INTEGER_SETTER(C,member,cast) \
642 WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,member,member,cast)
643
644#define WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,member,need_free) \
645 static int C##_set_##field (lua_State* L) { \
646 C obj = check##C (L,1); \
647 char* s = NULL; \
648 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
649 s = g_strdup(lua_tostring(L,-1)); \
650 } else { \
651 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
652 } \
653 if (obj->member != NULL && need_free) \
654 g_free((void*) obj->member); \
655 obj->member = s; \
656 return 0; \
657 } \
658 /* silly little trick so we can add a semicolon after this macro */ \
659 typedef void __dummy##C##_set_##field
660
661#define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \
662 WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free)
663
664#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_SETTER(C,field,member,option) \
665 static int C##_set_##field (lua_State* L) { \
666 C obj = check##C (L,1); \
667 char* s = NULL; \
668 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
669 s = g_strdup(lua_tostring(L,-1)); \
670 } else { \
671 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
672 } \
673 if ((obj->member) && (obj->member->len > 0)) { \
674 wtap_block_set_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, s, strlen(s)); \
675 } \
676 g_free(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_NAMED_OPT_BLOCK_NTH_STRING_SETTER(C,field,member,option) \
683 static int C##_set_##field (lua_State* L) { \
684 C obj = check##C (L,1); \
685 char* s = NULL; \
686 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
687 s = g_strdup(lua_tostring(L,-1)); \
688 } else { \
689 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
690 } \
691 if ((obj->member) && (obj->member->len > 0)) { \
692 wtap_block_set_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, s, strlen(s)); \
693 } \
694 g_free(s); \
695 return 0; \
696 } \
697 /* silly little trick so we can add a semicolon after this macro */ \
698 typedef void __dummy##C##_set_##field
699
700#define WSLUA_ERROR(name,error) { luaL_error(L, "%s%s", #name ": ", error); }
701#define WSLUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_ARG_ ## name ## _ ## attr, #name ": " error); }
702#define WSLUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_OPTARG_##name##_ ##attr, #name ": " error); }
703
704#define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushboolean(L,v); lua_setglobal(L,n); }
705#define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,v); lua_setglobal(L,n); }
706#define WSLUA_REG_GLOBAL_INTEGER(L,n,v) { lua_pushinteger(L,v); lua_setglobal(L,n); }
707
708#define WSLUA_RETURN(i) return (i)
709
710#define WSLUA_API extern
711
712/* empty macro arguments trigger ISO C90 warnings, so do this */
713#define NOP (void)p
714
715#define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
716
717#define FAIL_ON_NULL_OR_EXPIRED(s) if (!*p) { \
718 luaL_argerror(L,idx,"null " s); \
719 } else if ((*p)->expired) { \
720 luaL_argerror(L,idx,"expired " s); \
721 }
722
723/* Clears or marks references that connects Lua to Wireshark structures */
724#define CLEAR_OUTSTANDING(C, marker, marker_val) void clear_outstanding_##C(void) { \
725 while (outstanding_##C->len) { \
726 C p = (C)g_ptr_array_remove_index_fast(outstanding_##C,0); \
727 if (p) { \
728 if (p->marker != marker_val) \
729 p->marker = marker_val; \
730 else \
731 g_free(p); \
732 } \
733 } \
734}
735
736#define WSLUA_CLASS_DECLARE(C) \
737extern C to##C(lua_State* L, int idx); \
738extern C check##C(lua_State* L, int idx); \
739extern C* push##C(lua_State* L, C v); \
740extern int C##_register(lua_State* L); \
741extern bool is##C(lua_State* L,int i); \
742extern C shift##C(lua_State* L,int i)
743
744
745/* Throws a Wireshark exception, catchable via normal exceptions.h routines. */
746#define THROW_LUA_ERROR(...) \
747 THROW_FORMATTED(DissectorError, __VA_ARGS__)
748
749/* Catches any Wireshark exceptions in code and convert it into a Lua error.
750 * Normal restrictions for TRY/CATCH apply, in particular, do not return!
751 *
752 * This means do not call lua[L]_error() inside code, as that longjmps out
753 * of the TRY block to the Lua pcall! Use THROW_LUA_ERROR, which is caught
754 * and then converted into a Lua error.
755 *
756 * XXX: We CATCH_ALL here, although there's little point in catching
757 * OutOfMemoryError here. (Is CATCH_BOUNDS_AND_DISSECTOR_ERRORS sufficient?)
758 * There are some Exceptions that we catch and show but don't want to add
759 * the Lua error malformed expert info to the tree: BoundsError,
760 * FragmentBoundsError, and ScsiBoundsError (show_exception doesn't consider
761 * those malformed). The traceback might (or might not) be useful for those.
762 * Putting an extra malformed expert info in the tree in the cases that are
763 * malformed seems not so bad, but we might want to reduce that. Perhaps
764 * at least we could have a separate LuaError type and not call show_exception
765 * for that (we still need to handle some Lua errors that don't use this in
766 * dissector_error_handler.)
767 */
768#define WRAP_NON_LUA_EXCEPTIONS(code) \
769{ \
770 volatile bool has_error = false; \
771 TRY { \
772 code \
773 } CATCH3(BoundsError, FragmentBoundsError, ScsiBoundsError) { \
774 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
775 } CATCH_ALL { \
776 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
777 lua_pushfstring(L, "%s: %s", __func__, GET_MESSAGE ? GET_MESSAGE : "Malformed packet"); \
778 has_error = true; \
779 } ENDTRY; \
780 if (has_error) { lua_error(L); } \
781}
782
783
784extern packet_info* lua_pinfo;
785extern TreeItem lua_tree;
786extern tvbuff_t* lua_tvb;
787extern bool lua_initialized;
788extern int lua_dissectors_table_ref;
789extern int lua_heur_dissectors_table_ref;
790
791WSLUA_DECLARE_CLASSES()
792WSLUA_DECLARE_FUNCTIONS()
793
794extern lua_State* wslua_state(void);
795
796
797/* wslua_internals.c */
804typedef struct _wslua_class {
805 const char *name;
806 const luaL_Reg *class_methods;
807 const luaL_Reg *class_meta;
808 const luaL_Reg *instance_methods;
809 const luaL_Reg *instance_meta;
812void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def);
813void wslua_register_class(lua_State *L, const wslua_class *cls_def);
814
815extern int wslua__concat(lua_State* L);
816extern bool wslua_toboolean(lua_State* L, int n);
817extern bool wslua_checkboolean(lua_State* L, int n);
818extern bool wslua_optbool(lua_State* L, int n, bool def);
819extern lua_Integer wslua_tointeger(lua_State* L, int n);
820extern int wslua_optboolint(lua_State* L, int n, int def);
821extern const char* wslua_checklstring_only(lua_State* L, int n, size_t *l);
822extern const char* wslua_checkstring_only(lua_State* L, int n);
823extern void wslua_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
824extern const char* wslua_typeof_unknown;
825extern const char* wslua_typeof(lua_State *L, int idx);
826extern bool wslua_get_table(lua_State *L, int idx, const char *name);
827extern bool wslua_get_field(lua_State *L, int idx, const char *name);
828extern int dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
829extern bool heur_dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
830extern expert_field* wslua_get_expert_field(const int group, const int severity);
831extern void wslua_prefs_changed(void);
832extern void proto_register_lua(void);
833extern GString* lua_register_all_taps(void);
834extern void wslua_prime_dfilter(epan_dissect_t *edt);
835extern bool wslua_has_field_extractors(void);
836extern void lua_prime_all_fields(proto_tree* tree);
837
838extern int Proto_commit(lua_State* L);
839
840extern TreeItem create_TreeItem(proto_tree* tree, proto_item* item);
841
842extern void clear_outstanding_FuncSavers(void);
843
844extern void Int64_pack(lua_State* L, luaL_Buffer *b, int idx, bool asLittleEndian);
845extern int Int64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
846extern void UInt64_pack(lua_State* L, luaL_Buffer *b, int idx, bool asLittleEndian);
847extern int UInt64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
848extern uint64_t getUInt64(lua_State *L, int i);
849
850extern Tvb* push_Tvb(lua_State* L, tvbuff_t* tvb);
851extern int push_wsluaTvb(lua_State* L, Tvb t);
852extern bool push_TvbRange(lua_State* L, tvbuff_t* tvb, int offset, int len);
853extern void clear_outstanding_Tvb(void);
854extern void clear_outstanding_TvbRange(void);
855
856extern Pinfo* push_Pinfo(lua_State* L, packet_info* p);
857extern void clear_outstanding_Pinfo(void);
858extern void clear_outstanding_Column(void);
859extern void clear_outstanding_Columns(void);
860extern void clear_outstanding_PrivateTable(void);
861
862extern int get_hf_wslua_text(void);
863extern TreeItem push_TreeItem(lua_State *L, proto_tree *tree, proto_item *item);
864extern void clear_outstanding_TreeItem(void);
865
866extern FieldInfo* push_FieldInfo(lua_State *L, field_info* f);
867extern void clear_outstanding_FieldInfo(void);
868
869extern void wslua_print_stack(char* s, lua_State* L);
870
871extern void wslua_init(register_cb cb, void *client_data);
872extern void wslua_early_cleanup(void);
873extern void wslua_cleanup(void);
874
875extern tap_extractor_t wslua_get_tap_extractor(const char* name);
876extern int wslua_set_tap_enums(lua_State* L);
877
878extern ProtoField wslua_is_field_available(lua_State* L, const char* field_abbr);
879
880extern char* wslua_get_actual_filename(const char* fname);
881
882extern int wslua_bin2hex(lua_State* L, const uint8_t* data, const unsigned len, const bool lowercase, const char* sep);
883extern int wslua_hex2bin(lua_State* L, const char* data, const unsigned len, const char* sep);
884extern int luaopen_rex_pcre2(lua_State *L);
885
886extern const char* get_current_plugin_version(void);
887extern void clear_current_plugin_version(void);
888
889extern int wslua_deregister_heur_dissectors(lua_State* L);
890extern int wslua_deregister_protocols(lua_State* L);
891extern int wslua_deregister_dissector_tables(lua_State* L);
892extern int wslua_deregister_listeners(lua_State* L);
893extern int wslua_deregister_fields(lua_State* L);
894extern int wslua_deregister_filehandlers(lua_State* L);
895extern void wslua_deregister_menus(void);
896
897extern void wslua_init_wtap_filetypes(lua_State* L);
898
899extern const wslua_conv_types_t* wslua_inspect_convtype_enum(void);
900
901#endif
902
903/*
904 * Editor modelines - https://www.wireshark.org/tools/modelines.html
905 *
906 * Local variables:
907 * c-basic-offset: 4
908 * tab-width: 8
909 * indent-tabs-mode: nil
910 * End:
911 *
912 * vi: set shiftwidth=4 tabstop=8 expandtab:
913 * :indentSize=4:tabSize=8:noTabs=true:
914 */
#define PREF_UINT
Definition prefs-int.h:90
Definition address.h:56
Definition tap-funnel.c:27
Definition proto.h:767
Definition packet_info.h:43
Definition proto.h:906
Definition tvbparse.h:142
Definition tvbparse.h:130
Definition tvbparse.h:89
Definition uat.h:234
Definition wslua.h:464
Definition wslua.h:314
Type for defining new classes.
Definition wslua.h:804
const wslua_attribute_table * attrs
Definition wslua.h:810
const luaL_Reg * class_meta
Definition wslua.h:807
const char * name
Definition wslua.h:805
const luaL_Reg * class_methods
Definition wslua.h:806
const luaL_Reg * instance_methods
Definition wslua.h:808
const luaL_Reg * instance_meta
Definition wslua.h:809
Definition wslua.h:255
Definition wslua.h:261
Definition wslua.h:325
Definition wslua.h:238
Definition wslua.h:353
Definition wslua.h:246
Definition wslua.h:163
Definition wslua.h:284
Definition wslua.h:150
Definition wslua.h:307
Definition wslua.h:331
Definition wslua.h:279
Definition wslua.h:133
Definition wslua.h:186
uat_field_t * uat_field_list
Definition wslua.h:209
bool radio_buttons
Definition wslua.h:203
struct _wslua_pref_t::@500::@501 enum_info
char * default_s
Definition wslua.h:211
struct _wslua_pref_t::@500::@502 uat_field_list_info
uint32_t max_value
Definition wslua.h:200
union _wslua_pref_t::@500 info
const enum_val_t * enumvals
Definition wslua.h:202
Definition wslua.h:266
Definition wslua.h:358
Definition wslua.h:219
Definition wslua.h:320
Definition wslua.h:291
Definition wslua.h:272
Definition wslua.h:127
Definition wslua.h:138
Definition wslua.h:144
Definition conversation.h:228
Definition packet.c:812
Definition packet.c:87
Definition params.h:23
Definition column-info.h:62
Definition epan_dissect.h:28
Definition range.h:41
Definition expert.h:39
Definition expert.c:48
Definition proto.h:816
Definition wtap.h:1801
Definition packet.c:162
Definition wslua_dumper.c:58
Definition nstime.h:26
Definition prefs-int.h:27
Definition progress_frame.h:31
Definition wslua.h:365
Definition tvbuff-int.h:35
Definition wslua.h:368
Definition wslua.h:367
Definition wtap-int.h:97
Definition file_wrappers.c:215
Definition wtap.h:1433
Definition wtap-int.h:37
void wslua_register_class(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:547
ProtoField wslua_is_field_available(lua_State *L, const char *field_abbr)
Definition wslua_proto.c:714
void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:470
struct _wslua_class wslua_class
Type for defining new classes.
pref_type_t
Definition wslua.h:175