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