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