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