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