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