| 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 <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
33#include <wsutil/report_message.h>
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
128 struct _wslua_tvb {
129 tvbuff_t* ws_tvb;
130 bool expired;
131 bool need_free;
132};
133
134 struct _wslua_pinfo {
135 packet_info* ws_pinfo;
136 bool expired;
137};
138
139 struct _wslua_tvbrange {
140 struct _wslua_tvb* tvb;
141 int offset;
142 int len;
143};
144
145 struct _wslua_tw {
146 funnel_text_window_t* ws_tw;
147 bool expired;
148 void* close_cb_data;
149};
150
151 typedef 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;
162} wslua_field_t;
163
164 typedef struct _wslua_expert_field_t {
165 expert_field ids;
166 const char *abbrev;
167 const char *text;
168 int group;
169 int severity;
170} wslua_expert_field_t;
171
172 typedef 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 {
188 const enum_val_t *enumvals;
189 bool radio_buttons;
193 } enum_info;
194 struct {
195 uat_field_t *uat_field_list;
196 } uat_field_list_info;
197 char* default_s;
198 } info;
200 struct _wslua_pref_t* next;
201 struct _wslua_proto_t* proto;
202 int ref; /* Reference to enable Proto to deregister prefs. */
203} wslua_pref_t;
204
205 typedef 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;
214 expert_module_t *expert_module;
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;
222} wslua_proto_t;
223
224 typedef struct _wslua_conv_data_t {
225 conversation_t* conv;
226 int data_ref;
227} wslua_conv_data_t;
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. */
232 struct _wslua_distbl_t {
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
241 struct _wslua_col_info {
242 column_info* cinfo;
243 int col;
244 bool expired;
245};
246
247 struct _wslua_cols {
248 column_info* cinfo;
249 bool expired;
250};
251
252 struct _wslua_private_table {
253 GHashTable *table;
254 bool is_allocated;
255 bool expired;
256};
257
258 struct _wslua_treeitem {
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.
265 struct _wslua_header_field_info {
266 char *name;
267 header_field_info *hfi;
268};
269
270 struct _wslua_field_info {
271 field_info *ws_fi;
272 bool expired;
273};
274
275/*
276 * _func_saver stores function refs so that Lua won't garbage collect them prematurely.
277 * It is only used by tcp_dissect_pdus right now.
278 */
279 struct _wslua_func_saver {
280 lua_State* state;
281 int get_len_ref;
282 int dissect_ref;
283};
284
285 typedef void (*tap_extractor_t)(lua_State*,const void*);
286
287 struct _wslua_tap {
288 char* name;
289 char* filter;
290 tap_extractor_t extractor;
291 lua_State* L;
292 int packet_ref;
293 int draw_ref;
294 int reset_ref;
295 bool all_fields;
296};
297
298/* a "File" object can be different things under the hood. It can either
299 be a FILE_T from wtap struct, which it is during read operations, or it
300 can be a wtap_dumper struct during write operations. A wtap_dumper struct
301 has a FILE_T member, but we can't only store its pointer here because
302 dump operations need the whole thing to write out with. Ugh. */
303 struct _wslua_file {
304 FILE_T file;
305 wtap_dumper *wdh; /* will be NULL during read usage */
306 bool expired;
307};
308
309/* a "CaptureInfo" object can also be different things under the hood. */
310 struct _wslua_captureinfo {
311 wtap *wth; /* will be NULL during write usage */
312 wtap_dumper *wdh; /* will be NULL during read usage */
313 bool expired;
314};
315
316 struct _wslua_rec {
317 wtap_rec *rec;
318 bool expired;
319};
320
321 struct _wslua_const_rec {
322 const wtap_rec *rec;
323 const uint8_t *pd;
324 bool expired;
325};
326
327 struct _wslua_filehandler {
328 struct file_type_subtype_info finfo;
329 bool is_reader;
330 bool is_writer;
331 char* internal_description; /* XXX - this is redundant; finfo.description should suffice */
332 char* type;
333 char* extensions;
334 lua_State* L;
335 int read_open_ref;
336 int read_ref;
337 int seek_read_ref;
338 int read_close_ref;
339 int seq_read_close_ref;
340 int can_write_encap_ref;
341 int write_open_ref;
342 int write_ref;
343 int write_close_ref;
344 int file_type;
345 bool registered;
346 bool removed; /* This is set during reload Lua plugins */
347};
348
349 struct _wslua_dir {
350 GDir* dir;
351 char* ext;
352};
353
354 struct _wslua_progdlg {
355 struct progdlg* pw;
356 char* title;
357 char* task;
358 bool stopped;
359};
360
361 typedef struct { const char* name; tap_extractor_t extractor; } tappable_t;
362
363 typedef struct {const char* str; enum ftenum id; } wslua_ft_types_t;
364 typedef struct {const char* str; conversation_type id; } wslua_conv_types_t;
365
366 typedef wslua_pref_t* Pref;
367 typedef wslua_pref_t* Prefs;
368 typedef struct _wslua_field_t* ProtoField;
369 typedef struct _wslua_expert_field_t* ProtoExpert;
370 typedef struct _wslua_proto_t* Proto;
371 typedef struct _wslua_distbl_t* DissectorTable;
372 typedef dissector_handle_t Dissector;
373 typedef GByteArray* ByteArray;
374 typedef gcry_cipher_hd_t* GcryptCipher;
375 typedef struct _wslua_tvb* Tvb;
376 typedef struct _wslua_tvbrange* TvbRange;
377 typedef struct _wslua_col_info* Column;
378 typedef struct _wslua_cols* Columns;
379 typedef struct _wslua_pinfo* Pinfo;
380 typedef struct _wslua_treeitem* TreeItem;
381 typedef address* Address;
382 typedef nstime_t* NSTime;
383 typedef int64_t Int64;
384 typedef uint64_t UInt64;
385 typedef struct _wslua_header_field_info* Field;
386 typedef struct _wslua_field_info* FieldInfo;
387 typedef struct _wslua_tap* Listener;
388 typedef struct _wslua_tw* TextWindow;
389 typedef struct _wslua_progdlg* ProgDlg;
390 typedef struct _wslua_file* File;
391 typedef struct _wslua_captureinfo* CaptureInfo;
392 typedef struct _wslua_captureinfo* CaptureInfoConst;
393 typedef struct _wslua_rec* FrameInfo;
394 typedef struct _wslua_const_rec* FrameInfoConst;
395 typedef struct _wslua_filehandler* FileHandler;
396 typedef wtap_dumper* Dumper;
397 typedef struct lua_pseudo_header* PseudoHeader;
398 typedef tvbparse_t* Parser;
399 typedef tvbparse_wanted_t* Rule;
400 typedef tvbparse_elem_t* Node;
401 typedef tvbparse_action_t* Shortcut;
402 typedef struct _wslua_dir* Dir;
403 typedef struct _wslua_private_table* PrivateTable;
404 typedef conversation_t* Conversation;
405 typedef char* Struct;
406
407/*
408 * toXxx(L,idx) gets a Xxx from an index (Lua Error if fails)
409 * checkXxx(L,idx) gets a Xxx from an index after calling check_code (No Lua Error if it fails)
410 * pushXxx(L,xxx) pushes an Xxx into the stack
411 * isXxx(L,idx) tests whether we have an Xxx at idx
412 * shiftXxx(L,idx) removes and returns an Xxx from idx only if it has a type of Xxx, returns NULL otherwise
413 * WSLUA_CLASS_DEFINE must be used with a trailing ';'
414 * (a dummy typedef is used to be syntactically correct)
415 */
416#define WSLUA_CLASS_DEFINE(C,check_code) \
417 WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL)
418
419#define WSLUA_CLASS_DEFINE_BASE(C,check_code,retval) \
420 C to##C(lua_State* L, int idx) { \
421 C* v = (C*)lua_touserdata (L, idx); \
422 if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)", idx, #C, lua_typename(L, lua_type(L, idx))); \
423 return v ? *v : retval; \
424} \
425 C check##C(lua_State* L, int idx) { \
426 C* p; \
427 luaL_checktype(L,idx,LUA_TUSERDATA); \
428 p = (C*)luaL_checkudata(L, idx, #C); \
429 check_code; \
430 return p ? *p : retval; \
431} \
432 C* push##C(lua_State* L, C v) { \
433 C* p; \
434 luaL_checkstack(L,2,"Unable to grow stack\n"); \
435 p = (C*)lua_newuserdata(L,sizeof(C)); *p = v; \
436 luaL_getmetatable(L, #C); lua_setmetatable(L, -2); \
437 return p; \
438}\
439 bool is##C(lua_State* L,int i) { \
440 void *p; \
441 if(!lua_isuserdata(L,i)) return false; \
442 p = lua_touserdata(L, i); \
443 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
444 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
445 lua_pop(L, 2); \
446 return p ? true : false; \
447} \
448 C shift##C(lua_State* L,int i) { \
449 C* p; \
450 if(!lua_isuserdata(L,i)) return retval; \
451 p = (C*)lua_touserdata(L, i); \
452 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
453 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
454 lua_pop(L, 2); \
455 if (p) { lua_remove(L,i); return *p; }\
456 else return retval;\
457} \
458 typedef int dummy##C
459
460 typedef struct _wslua_attribute_table {
461 const char *fieldname;
462 lua_CFunction getfunc;
463 lua_CFunction setfunc;
464} wslua_attribute_table;
465 extern int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, bool is_getter);
466
467#define WSLUA_TYPEOF_FIELD "__typeof"
468
469#ifdef HAVE_LUA
470
471/* temporary transition macro to reduce duplication in WSLUA_REGISTER_xxx. */
472#define WSLUA_REGISTER_GC(C) \
473 luaL_getmetatable(L, #C); \
474 /* add the '__gc' metamethod with a C-function named Class__gc */ \
475 /* this will force ALL wslua classes to have a Class__gc function defined, which is good */ \
476 lua_pushcfunction(L, C ## __gc); \
477 lua_setfield(L, -2, "__gc"); \
478 /* pop the metatable */ \
479 lua_pop(L, 1)
480
481#define __WSLUA_REGISTER_META(C, ATTRS) { \
482 const wslua_class C ## _class = { \
483 .name = #C, \
484 .instance_meta = C ## _meta, \
485 .attrs = ATTRS \
486 }; \
487 wslua_register_classinstance_meta(L, &C ## _class); \
488 WSLUA_REGISTER_GC(C); \
489}
490
491#define WSLUA_REGISTER_META(C) __WSLUA_REGISTER_META(C, NULL)
492#define WSLUA_REGISTER_META_WITH_ATTRS(C) \
493 __WSLUA_REGISTER_META(C, C ## _attributes)
494
495#define __WSLUA_REGISTER_CLASS(C, ATTRS) { \
496 const wslua_class C ## _class = { \
497 .name = #C, \
498 .class_methods = C ## _methods, \
499 .class_meta = C ## _meta, \
500 .instance_methods = C ## _methods, \
501 .instance_meta = C ## _meta, \
502 .attrs = ATTRS \
503 }; \
504 wslua_register_class(L, &C ## _class); \
505 WSLUA_REGISTER_GC(C); \
506}
507
508#define WSLUA_REGISTER_CLASS(C) __WSLUA_REGISTER_CLASS(C, NULL)
509#define WSLUA_REGISTER_CLASS_WITH_ATTRS(C) \
510 __WSLUA_REGISTER_CLASS(C, C ## _attributes)
511
512#define WSLUA_INIT(L) \
513 luaL_openlibs(L); \
514 wslua_register_classes(L); \
515 wslua_register_functions(L);
516
517#endif
518
519#define WSLUA_FUNCTION extern int
520/* This is for functions intended only to be used in init.lua */
521#define WSLUA_INTERNAL_FUNCTION extern int
522
523#define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }
524
525#define WSLUA_REGISTER extern int
526
527#define WSLUA_METHOD static int
528#define WSLUA_CONSTRUCTOR static int
529#define WSLUA_ATTR_SET static int
530#define WSLUA_ATTR_GET static int
531#define WSLUA_METAMETHOD static int
532
533#define WSLUA_METHODS static const luaL_Reg
534#define WSLUA_META static const luaL_Reg
535#define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name }
536#define WSLUA_CLASS_FNREG_ALIAS(class,aliasname,name) { #aliasname, class##_##name }
537#define WSLUA_CLASS_MTREG(class,name) { "__" #name, class##__##name }
538
539#define WSLUA_ATTRIBUTES static const wslua_attribute_table
540/* following are useful macros for the rows in the array created by above */
541#define WSLUA_ATTRIBUTE_RWREG(class,name) { #name, class##_get_##name, class##_set_##name }
542#define WSLUA_ATTRIBUTE_ROREG(class,name) { #name, class##_get_##name, NULL }
543#define WSLUA_ATTRIBUTE_WOREG(class,name) { #name, NULL, class##_set_##name }
544
545#define WSLUA_ATTRIBUTE_FUNC_SETTER(C,field) \
546 static int C##_set_##field (lua_State* L) { \
547 C obj = check##C (L,1); \
548 if (! lua_isfunction(L,-1) ) \
549 return luaL_error(L, "%s's attribute `%s' must be a function", #C , #field ); \
550 if (obj->field##_ref != LUA_NOREF) \
551 /* there was one registered before, remove it */ \
552 luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \
553 obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
554 return 0; \
555 } \
556 /* silly little trick so we can add a semicolon after this macro */ \
557 typedef void __dummy##C##_set_##field
558
559#define WSLUA_ATTRIBUTE_GET(C,name,block) \
560 static int C##_get_##name (lua_State* L) { \
561 C obj = check##C (L,1); \
562 block \
563 return 1; \
564 } \
565 /* silly little trick so we can add a semicolon after this macro */ \
566 typedef void __dummy##C##_get_##name
567
568#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
569 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
570
571#define WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,name,member) \
572 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushinteger(L,(lua_Integer)(obj->member));})
573
574#define WSLUA_ATTRIBUTE_INTEGER_GETTER(C,member) \
575 WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,member,member)
576
577#define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
578 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
579
580#define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
581 WSLUA_ATTRIBUTE_GET(C,name, { \
582 lua_pushstring(L,obj->member); /* this pushes nil if obj->member is null */ \
583 })
584
585#define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
586 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
587
588#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \
589 WSLUA_ATTRIBUTE_GET(C,name, { \
590 char* str; \
591 if ((obj->member) && (obj->member->len > 0)) { \
592 if (wtap_block_get_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, &str) == WTAP_OPTTYPE_SUCCESS) { \
593 lua_pushstring(L,str); \
594 } \
595 } \
596 })
597
598/*
599 * XXX - we need to support Lua programs getting instances of a "multiple
600 * allowed" option other than the first option.
601 */
602#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \
603 WSLUA_ATTRIBUTE_GET(C,name, { \
604 char* str; \
605 if ((obj->member) && (obj->member->len > 0)) { \
606 if (wtap_block_get_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, &str) == WTAP_OPTTYPE_SUCCESS) { \
607 lua_pushstring(L,str); \
608 } \
609 } \
610 })
611
612#define WSLUA_ATTRIBUTE_SET(C,name,block) \
613 static int C##_set_##name (lua_State* L) { \
614 C obj = check##C (L,1); \
615 block; \
616 return 0; \
617 } \
618 /* silly little trick so we can add a semicolon after this macro */ \
619 typedef void __dummy##C##_set_##name
620
621#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_SETTER(C,name,member) \
622 WSLUA_ATTRIBUTE_SET(C,name, { \
623 if (! lua_isboolean(L,-1) ) \
624 return luaL_error(L, "%s's attribute `%s' must be a boolean", #C , #name ); \
625 obj->member = lua_toboolean(L,-1); \
626 })
627
628/* to make this integral-safe, we treat it as int32 and then cast
629 Note: This will truncate 64-bit integers (but then Lua itself only has doubles */
630#define WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,name,member,cast) \
631 WSLUA_ATTRIBUTE_SET(C,name, { \
632 if (! lua_isinteger(L,-1) ) \
633 return luaL_error(L, "%s's attribute `%s' must be an integer", #C , #name ); \
634 obj->member = (cast) wslua_toint32(L,-1); \
635 })
636
637#define WSLUA_ATTRIBUTE_INTEGER_SETTER(C,member,cast) \
638 WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,member,member,cast)
639
640#define WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,member,need_free) \
641 static int C##_set_##field (lua_State* L) { \
642 C obj = check##C (L,1); \
643 char* s = NULL; \
644 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
645 s = g_strdup(lua_tostring(L,-1)); \
646 } else { \
647 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
648 } \
649 if (obj->member != NULL && need_free) \
650 g_free((void*) obj->member); \
651 obj->member = s; \
652 return 0; \
653 } \
654 /* silly little trick so we can add a semicolon after this macro */ \
655 typedef void __dummy##C##_set_##field
656
657#define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \
658 WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free)
659
660#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_SETTER(C,field,member,option) \
661 static int C##_set_##field (lua_State* L) { \
662 C obj = check##C (L,1); \
663 char* s = NULL; \
664 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
665 s = g_strdup(lua_tostring(L,-1)); \
666 } else { \
667 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
668 } \
669 if ((obj->member) && (obj->member->len > 0)) { \
670 wtap_block_set_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, s, strlen(s)); \
671 } \
672 g_free(s); \
673 return 0; \
674 } \
675 /* silly little trick so we can add a semicolon after this macro */ \
676 typedef void __dummy##C##_set_##field
677
678#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_SETTER(C,field,member,option) \
679 static int C##_set_##field (lua_State* L) { \
680 C obj = check##C (L,1); \
681 char* s = NULL; \
682 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
683 s = g_strdup(lua_tostring(L,-1)); \
684 } else { \
685 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
686 } \
687 if ((obj->member) && (obj->member->len > 0)) { \
688 wtap_block_set_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, s, strlen(s)); \
689 } \
690 g_free(s); \
691 return 0; \
692 } \
693 /* silly little trick so we can add a semicolon after this macro */ \
694 typedef void __dummy##C##_set_##field
695
696#define WSLUA_ERROR(name,error) { luaL_error(L, "%s%s", #name ": ", error); }
697#define WSLUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_ARG_ ## name ## _ ## attr, #name ": " error); }
698#define WSLUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_OPTARG_##name##_ ##attr, #name ": " error); }
699
700#define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushboolean(L,v); lua_setglobal(L,n); }
701#define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,v); lua_setglobal(L,n); }
702#define WSLUA_REG_GLOBAL_INTEGER(L,n,v) { lua_pushinteger(L,v); lua_setglobal(L,n); }
703
704#define WSLUA_RETURN(i) return (i)
705
706#define WSLUA_API extern
707
708/* empty macro arguments trigger ISO C90 warnings, so do this */
709#define NOP (void)p
710
711#define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
712
713#define FAIL_ON_NULL_OR_EXPIRED(s) if (!*p) { \
714 luaL_argerror(L,idx,"null " s); \
715 } else if ((*p)->expired) { \
716 luaL_argerror(L,idx,"expired " s); \
717 }
718
719/* Clears or marks references that connects Lua to Wireshark structures */
720#define CLEAR_OUTSTANDING(C, marker, marker_val) void clear_outstanding_##C(void) { \
721 while (outstanding_##C->len) { \
722 C p = (C)g_ptr_array_remove_index_fast(outstanding_##C,0); \
723 if (p) { \
724 if (p->marker != marker_val) \
725 p->marker = marker_val; \
726 else \
727 g_free(p); \
728 } \
729 } \
730}
731
732#define WSLUA_CLASS_DECLARE(C) \
733 extern C to##C(lua_State* L, int idx); \
734 extern C check##C(lua_State* L, int idx); \
735 extern C* push##C(lua_State* L, C v); \
736 extern int C##_register(lua_State* L); \
737 extern bool is##C(lua_State* L,int i); \
738 extern C shift##C(lua_State* L,int i)
739
740
741/* Throws a Wireshark exception, catchable via normal exceptions.h routines. */
742#define THROW_LUA_ERROR(...) \
743 THROW_FORMATTED(DissectorError, __VA_ARGS__)
744
745/* Catches any Wireshark exceptions in code and convert it into a Lua error.
746 * Normal restrictions for TRY/CATCH apply, in particular, do not return!
747 *
748 * This means do not call lua[L]_error() inside code, as that longjmps out
749 * of the TRY block to the Lua pcall! Use THROW_LUA_ERROR, which is caught
750 * and then converted into a Lua error.
751 *
752 * XXX: We CATCH_ALL here, although there's little point in catching
753 * OutOfMemoryError here. (Is CATCH_BOUNDS_AND_DISSECTOR_ERRORS sufficient?)
754 * There are some Exceptions that we catch and show but don't want to add
755 * the Lua error malformed expert info to the tree: BoundsError,
756 * FragmentBoundsError, and ScsiBoundsError (show_exception doesn't consider
757 * those malformed). The traceback might (or might not) be useful for those.
758 * Putting an extra malformed expert info in the tree in the cases that are
759 * malformed seems not so bad, but we might want to reduce that. Perhaps
760 * at least we could have a separate LuaError type and not call show_exception
761 * for that (we still need to handle some Lua errors that don't use this in
762 * dissector_error_handler.)
763 */
764#define WRAP_NON_LUA_EXCEPTIONS(code) \
765{ \
766 volatile bool has_error = false; \
767 TRY { \
768 code \
769 } CATCH3(BoundsError, FragmentBoundsError, ScsiBoundsError) { \
770 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
771 } CATCH_ALL { \
772 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
773 lua_pushfstring(L, "%s: %s", __func__, GET_MESSAGE ? GET_MESSAGE : "Malformed packet"); \
774 has_error = true; \
775 } ENDTRY; \
776 if (has_error) { lua_error(L); } \
777}
778
779
780 extern packet_info* lua_pinfo;
781 extern TreeItem lua_tree;
782 extern tvbuff_t* lua_tvb;
783 extern bool lua_initialized;
784 extern int lua_dissectors_table_ref;
785 extern int lua_heur_dissectors_table_ref;
786 extern const char* lua_app_env_var_prefix;
787 extern GPtrArray* lua_outstanding_FuncSavers;
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;
808 const wslua_attribute_table *attrs;
809} wslua_class;
810 void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def);
811 void wslua_register_class(lua_State *L, const wslua_class *cls_def);
812
813 extern int wslua__concat(lua_State* L);
814 extern bool wslua_toboolean(lua_State* L, int n);
815 extern bool wslua_checkboolean(lua_State* L, int n);
816 extern bool wslua_optbool(lua_State* L, int n, bool def);
817 extern lua_Integer wslua_tointeger(lua_State* L, int n);
818 extern int wslua_optboolint(lua_State* L, int n, int def);
819 extern const char* wslua_checklstring_only(lua_State* L, int n, size_t *l);
820 extern const char* wslua_checkstring_only(lua_State* L, int n);
821 extern void wslua_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
822 extern const char* wslua_typeof_unknown;
823 extern const char* wslua_typeof(lua_State *L, int idx);
824 extern bool wslua_get_table(lua_State *L, int idx, const char *name);
825 extern bool wslua_get_field(lua_State *L, int idx, const char *name);
826 extern int dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
827 extern bool heur_dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
828 extern expert_field* wslua_get_expert_field(const int group, const int severity);
829 extern void wslua_prefs_changed(void);
830 extern void proto_register_lua(void);
831 extern GString* lua_register_all_taps(void);
832 extern void wslua_prime_dfilter(epan_dissect_t *edt);
833 extern bool wslua_has_field_extractors(void);
834 extern void lua_prime_all_fields(proto_tree* tree);
835
836 extern int Proto_commit(lua_State* L);
837
838 extern TreeItem create_TreeItem(proto_tree* tree, proto_item* item);
839
840 extern void clear_outstanding_FuncSavers(void);
841
842 extern void Int64_pack(lua_State* L, luaL_Buffer *b, int idx, bool asLittleEndian);
843 extern int Int64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
844 extern void UInt64_pack(lua_State* L, luaL_Buffer *b, int idx, bool asLittleEndian);
845 extern int UInt64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
846 extern uint64_t getUInt64(lua_State *L, int i);
847
848 extern Tvb* push_Tvb(lua_State* L, tvbuff_t* tvb);
849 extern int push_wsluaTvb(lua_State* L, Tvb t);
850 extern bool push_TvbRange(lua_State* L, tvbuff_t* tvb, int offset, int len);
851 extern void clear_outstanding_Tvb(void);
852 extern void clear_outstanding_TvbRange(void);
853
854 extern Pinfo* push_Pinfo(lua_State* L, packet_info* p);
855 extern void clear_outstanding_Pinfo(void);
856 extern void clear_outstanding_Column(void);
857 extern void clear_outstanding_Columns(void);
858 extern void clear_outstanding_PrivateTable(void);
859
860 extern int get_hf_wslua_text(void);
861 extern TreeItem push_TreeItem(lua_State *L, proto_tree *tree, proto_item *item);
862 extern void clear_outstanding_TreeItem(void);
863
864 extern FieldInfo* push_FieldInfo(lua_State *L, field_info* f);
865 extern void clear_outstanding_FieldInfo(void);
866
867 extern void wslua_print_stack(char* s, lua_State* L);
868
869 extern void wslua_init(register_cb cb, void *client_data, const char* app_env_var_prefix);
870 extern void wslua_early_cleanup(void);
871 extern void wslua_cleanup(void);
872
873 extern tap_extractor_t wslua_get_tap_extractor(const char* name);
874 extern int wslua_set_tap_enums(lua_State* L);
875
876 extern ProtoField wslua_is_field_available(lua_State* L, const char* field_abbr);
877
878 extern char* wslua_get_actual_filename(const char* fname);
879
880 extern int wslua_bin2hex(lua_State* L, const uint8_t* data, const unsigned len, const bool lowercase, const char* sep);
881 extern int wslua_hex2bin(lua_State* L, const char* data, const unsigned len, const char* sep);
882 extern int luaopen_rex_pcre2(lua_State *L);
883
884 extern const char* get_current_plugin_version(void);
885 extern void clear_current_plugin_version(void);
886
887 extern int wslua_deregister_heur_dissectors(lua_State* L);
888 extern int wslua_deregister_protocols(lua_State* L);
889 extern int wslua_deregister_dissector_tables(lua_State* L);
890 extern int wslua_deregister_listeners(lua_State* L);
891 extern int wslua_deregister_fields(lua_State* L);
892 extern int wslua_deregister_filehandlers(lua_State* L);
893 extern void wslua_deregister_menus(void);
894
895 extern void wslua_init_wtap_filetypes(lua_State* L);
896
897 extern 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 */
column-utils.h
conversation.h
to_str.h
epan.h
epan_dissect.h
exceptions.h
expert.h
filesystem.h
funnel.h
nstime.h
packet.h
prefs-int.h
prefs.h
proto.h
report_message.h
show_exception.h
_address
Definition address.h:58
_funnel_text_window_t
Definition tap-funnel.c:21
_header_field_info
Definition proto.h:767
_packet_info
Definition packet_info.h:43
_proto_node
Definition proto.h:907
_tvbparse_elem_t
Definition tvbparse.h:163
_tvbparse_t
Represents an instance of a per-packet parser for tvbuff data.
Definition tvbparse.h:151
_tvbparse_wanted_t
Describes a parsing rule or expectation for a tvbuff parser.
Definition tvbparse.h:96
_uat_field_t
Definition uat.h:235
_wslua_attribute_table
Definition wslua.h:460
_wslua_captureinfo
Definition wslua.h:310
_wslua_class
Type for defining new classes.
Definition wslua.h:802
_wslua_class::attrs
const wslua_attribute_table * attrs
Definition wslua.h:808
_wslua_class::class_meta
const luaL_Reg * class_meta
Definition wslua.h:805
_wslua_class::name
const char * name
Definition wslua.h:803
_wslua_class::class_methods
const luaL_Reg * class_methods
Definition wslua.h:804
_wslua_class::instance_methods
const luaL_Reg * instance_methods
Definition wslua.h:806
_wslua_class::instance_meta
const luaL_Reg * instance_meta
Definition wslua.h:807
_wslua_col_info
Definition wslua.h:241
_wslua_cols
Definition wslua.h:247
_wslua_const_rec
Definition wslua.h:321
_wslua_conv_data_t
Definition wslua.h:224
_wslua_dir
Definition wslua.h:349
_wslua_distbl_t
Definition wslua.h:232
_wslua_expert_field_t
Definition wslua.h:164
_wslua_field_info
Definition wslua.h:270
_wslua_field_t
Definition wslua.h:151
_wslua_file
Definition wslua.h:303
_wslua_filehandler
Definition wslua.h:327
_wslua_func_saver
Definition wslua.h:279
_wslua_header_field_info
Definition wslua.h:265
_wslua_pinfo
Definition wslua.h:134
_wslua_pref_t
Definition wslua.h:172
_wslua_pref_t::uat_field_list
uat_field_t * uat_field_list
Definition wslua.h:195
_wslua_pref_t::uat_field_list_info
struct _wslua_pref_t::@509::@511 uat_field_list_info
_wslua_pref_t::radio_buttons
bool radio_buttons
Definition wslua.h:189
_wslua_pref_t::enum_info
struct _wslua_pref_t::@509::@510 enum_info
_wslua_pref_t::default_s
char * default_s
Definition wslua.h:197
_wslua_pref_t::max_value
uint32_t max_value
Definition wslua.h:186
_wslua_pref_t::enumvals
const enum_val_t * enumvals
Definition wslua.h:188
_wslua_pref_t::info
union _wslua_pref_t::@509 info
_wslua_private_table
Definition wslua.h:252
_wslua_progdlg
Definition wslua.h:354
_wslua_proto_t
Definition wslua.h:205
_wslua_rec
Definition wslua.h:316
_wslua_tap
Definition wslua.h:287
_wslua_treeitem
Definition wslua.h:258
_wslua_tvb
Definition wslua.h:128
_wslua_tvbrange
Definition wslua.h:139
_wslua_tw
Definition wslua.h:145
conversation
Definition conversation.h:230
dissector_handle
Definition packet.c:857
dissector_table
Definition packet.c:97
enum_val_t
Definition params.h:23
epan_column_info
Definition column-info.h:62
epan_dissect
Definition epan_dissect.h:28
epan_range
Definition range.h:41
expert_field
Definition expert.h:39
expert_module
Definition expert.c:48
field_info
Definition proto.h:816
file_type_subtype_info
Definition wtap.h:1927
heur_dissector_list
Definition packet.c:188
lua_pseudo_header
Definition wslua_dumper.c:58
nstime_t
Definition nstime.h:26
pref_module
Definition prefs-int.h:27
progdlg
Definition progress_frame.h:31
tappable_t
Definition wslua.h:361
tvbuff
Definition tvbuff-int.h:35
wslua_conv_types_t
Definition wslua.h:364
wslua_ft_types_t
Definition wslua.h:363
wtap_dumper
Wiretap dumper handle and associated state.
Definition wtap_module.h:163
wtap_reader
Definition file_wrappers.c:97
wtap_rec
Definition wtap.h:1512
wtap
Definition wtap_module.h:58
strutil.h
tap.h
tvbparse.h
uat-int.h
uat.h
ws_assert.h
wsgcrypt.h
wslog.h
wslua_register_class
void wslua_register_class(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:547
wslua_is_field_available
ProtoField wslua_is_field_available(lua_State *L, const char *field_abbr)
Definition wslua_proto.c:702
wslua_register_classinstance_meta
void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:470
wslua_class
struct _wslua_class wslua_class
Type for defining new classes.
wtap.h