Wireshark 4.5.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
wslua.h
Go to the documentation of this file.
1/*
2 * wslua.h
3 *
4 * Wireshark's interface to the Lua Programming Language
5 *
6 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
7 * (c) 2007, Tamas Regos <tamas.regos@ericsson.com>
8 * (c) 2008, Balint Reczey <balint.reczey@ericsson.com>
9 *
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
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;
308 bool expired;
309};
310
312 const wtap_rec *rec;
313 const uint8_t *pd;
314 bool expired;
315};
316
318 struct file_type_subtype_info finfo;
319 bool is_reader;
320 bool is_writer;
321 char* internal_description; /* XXX - this is redundant; finfo.description should suffice */
322 char* type;
323 char* extensions;
324 lua_State* L;
325 int read_open_ref;
326 int read_ref;
327 int seek_read_ref;
328 int read_close_ref;
329 int seq_read_close_ref;
330 int can_write_encap_ref;
331 int write_open_ref;
332 int write_ref;
333 int write_close_ref;
334 int file_type;
335 bool registered;
336 bool removed; /* This is set during reload Lua plugins */
337};
338
340 GDir* dir;
341 char* ext;
342};
343
345 struct progdlg* pw;
346 char* title;
347 char* task;
348 bool stopped;
349};
350
351typedef struct { const char* name; tap_extractor_t extractor; } tappable_t;
352
353typedef struct {const char* str; enum ftenum id; } wslua_ft_types_t;
354
355typedef wslua_pref_t* Pref;
356typedef wslua_pref_t* Prefs;
357typedef struct _wslua_field_t* ProtoField;
358typedef struct _wslua_expert_field_t* ProtoExpert;
359typedef struct _wslua_proto_t* Proto;
360typedef struct _wslua_distbl_t* DissectorTable;
362typedef GByteArray* ByteArray;
363typedef struct _wslua_tvb* Tvb;
364typedef struct _wslua_tvbrange* TvbRange;
365typedef struct _wslua_col_info* Column;
366typedef struct _wslua_cols* Columns;
367typedef struct _wslua_pinfo* Pinfo;
368typedef struct _wslua_treeitem* TreeItem;
369typedef address* Address;
370typedef nstime_t* NSTime;
371typedef int64_t Int64;
372typedef uint64_t UInt64;
373typedef struct _wslua_header_field_info* Field;
374typedef struct _wslua_field_info* FieldInfo;
375typedef struct _wslua_tap* Listener;
376typedef struct _wslua_tw* TextWindow;
377typedef struct _wslua_progdlg* ProgDlg;
378typedef struct _wslua_file* File;
379typedef struct _wslua_captureinfo* CaptureInfo;
381typedef struct _wslua_rec* FrameInfo;
382typedef struct _wslua_const_rec* FrameInfoConst;
383typedef struct _wslua_filehandler* FileHandler;
384typedef wtap_dumper* Dumper;
385typedef struct lua_pseudo_header* PseudoHeader;
386typedef tvbparse_t* Parser;
387typedef tvbparse_wanted_t* Rule;
388typedef tvbparse_elem_t* Node;
389typedef tvbparse_action_t* Shortcut;
390typedef struct _wslua_dir* Dir;
391typedef struct _wslua_private_table* PrivateTable;
392typedef char* Struct;
393
394/*
395 * toXxx(L,idx) gets a Xxx from an index (Lua Error if fails)
396 * checkXxx(L,idx) gets a Xxx from an index after calling check_code (No Lua Error if it fails)
397 * pushXxx(L,xxx) pushes an Xxx into the stack
398 * isXxx(L,idx) tests whether we have an Xxx at idx
399 * shiftXxx(L,idx) removes and returns an Xxx from idx only if it has a type of Xxx, returns NULL otherwise
400 * WSLUA_CLASS_DEFINE must be used with a trailing ';'
401 * (a dummy typedef is used to be syntactically correct)
402 */
403#define WSLUA_CLASS_DEFINE(C,check_code) \
404 WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL)
405
406#define WSLUA_CLASS_DEFINE_BASE(C,check_code,retval) \
407C to##C(lua_State* L, int idx) { \
408 C* v = (C*)lua_touserdata (L, idx); \
409 if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)", idx, #C, lua_typename(L, lua_type(L, idx))); \
410 return v ? *v : retval; \
411} \
412C check##C(lua_State* L, int idx) { \
413 C* p; \
414 luaL_checktype(L,idx,LUA_TUSERDATA); \
415 p = (C*)luaL_checkudata(L, idx, #C); \
416 check_code; \
417 return p ? *p : retval; \
418} \
419C* push##C(lua_State* L, C v) { \
420 C* p; \
421 luaL_checkstack(L,2,"Unable to grow stack\n"); \
422 p = (C*)lua_newuserdata(L,sizeof(C)); *p = v; \
423 luaL_getmetatable(L, #C); lua_setmetatable(L, -2); \
424 return p; \
425}\
426bool is##C(lua_State* L,int i) { \
427 void *p; \
428 if(!lua_isuserdata(L,i)) return false; \
429 p = lua_touserdata(L, i); \
430 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
431 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
432 lua_pop(L, 2); \
433 return p ? true : false; \
434} \
435C shift##C(lua_State* L,int i) { \
436 C* p; \
437 if(!lua_isuserdata(L,i)) return retval; \
438 p = (C*)lua_touserdata(L, i); \
439 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
440 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
441 lua_pop(L, 2); \
442 if (p) { lua_remove(L,i); return *p; }\
443 else return retval;\
444} \
445typedef int dummy##C
446
448 const char *fieldname;
449 lua_CFunction getfunc;
450 lua_CFunction setfunc;
452extern int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, bool is_getter);
453
454#define WSLUA_TYPEOF_FIELD "__typeof"
455
456#ifdef HAVE_LUA
457
458/* temporary transition macro to reduce duplication in WSLUA_REGISTER_xxx. */
459#define WSLUA_REGISTER_GC(C) \
460 luaL_getmetatable(L, #C); \
461 /* add the '__gc' metamethod with a C-function named Class__gc */ \
462 /* this will force ALL wslua classes to have a Class__gc function defined, which is good */ \
463 lua_pushcfunction(L, C ## __gc); \
464 lua_setfield(L, -2, "__gc"); \
465 /* pop the metatable */ \
466 lua_pop(L, 1)
467
468#define __WSLUA_REGISTER_META(C, ATTRS) { \
469 const wslua_class C ## _class = { \
470 .name = #C, \
471 .instance_meta = C ## _meta, \
472 .attrs = ATTRS \
473 }; \
474 wslua_register_classinstance_meta(L, &C ## _class); \
475 WSLUA_REGISTER_GC(C); \
476}
477
478#define WSLUA_REGISTER_META(C) __WSLUA_REGISTER_META(C, NULL)
479#define WSLUA_REGISTER_META_WITH_ATTRS(C) \
480 __WSLUA_REGISTER_META(C, C ## _attributes)
481
482#define __WSLUA_REGISTER_CLASS(C, ATTRS) { \
483 const wslua_class C ## _class = { \
484 .name = #C, \
485 .class_methods = C ## _methods, \
486 .class_meta = C ## _meta, \
487 .instance_methods = C ## _methods, \
488 .instance_meta = C ## _meta, \
489 .attrs = ATTRS \
490 }; \
491 wslua_register_class(L, &C ## _class); \
492 WSLUA_REGISTER_GC(C); \
493}
494
495#define WSLUA_REGISTER_CLASS(C) __WSLUA_REGISTER_CLASS(C, NULL)
496#define WSLUA_REGISTER_CLASS_WITH_ATTRS(C) \
497 __WSLUA_REGISTER_CLASS(C, C ## _attributes)
498
499#define WSLUA_INIT(L) \
500 luaL_openlibs(L); \
501 wslua_register_classes(L); \
502 wslua_register_functions(L);
503
504#endif
505
506#define WSLUA_FUNCTION extern int
507/* This is for functions intended only to be used in init.lua */
508#define WSLUA_INTERNAL_FUNCTION extern int
509
510#define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }
511
512#define WSLUA_REGISTER extern int
513
514#define WSLUA_METHOD static int
515#define WSLUA_CONSTRUCTOR static int
516#define WSLUA_ATTR_SET static int
517#define WSLUA_ATTR_GET static int
518#define WSLUA_METAMETHOD static int
519
520#define WSLUA_METHODS static const luaL_Reg
521#define WSLUA_META static const luaL_Reg
522#define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name }
523#define WSLUA_CLASS_FNREG_ALIAS(class,aliasname,name) { #aliasname, class##_##name }
524#define WSLUA_CLASS_MTREG(class,name) { "__" #name, class##__##name }
525
526#define WSLUA_ATTRIBUTES static const wslua_attribute_table
527/* following are useful macros for the rows in the array created by above */
528#define WSLUA_ATTRIBUTE_RWREG(class,name) { #name, class##_get_##name, class##_set_##name }
529#define WSLUA_ATTRIBUTE_ROREG(class,name) { #name, class##_get_##name, NULL }
530#define WSLUA_ATTRIBUTE_WOREG(class,name) { #name, NULL, class##_set_##name }
531
532#define WSLUA_ATTRIBUTE_FUNC_SETTER(C,field) \
533 static int C##_set_##field (lua_State* L) { \
534 C obj = check##C (L,1); \
535 if (! lua_isfunction(L,-1) ) \
536 return luaL_error(L, "%s's attribute `%s' must be a function", #C , #field ); \
537 if (obj->field##_ref != LUA_NOREF) \
538 /* there was one registered before, remove it */ \
539 luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \
540 obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
541 return 0; \
542 } \
543 /* silly little trick so we can add a semicolon after this macro */ \
544 typedef void __dummy##C##_set_##field
545
546#define WSLUA_ATTRIBUTE_GET(C,name,block) \
547 static int C##_get_##name (lua_State* L) { \
548 C obj = check##C (L,1); \
549 block \
550 return 1; \
551 } \
552 /* silly little trick so we can add a semicolon after this macro */ \
553 typedef void __dummy##C##_get_##name
554
555#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
556 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
557
558#define WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,name,member) \
559 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushinteger(L,(lua_Integer)(obj->member));})
560
561#define WSLUA_ATTRIBUTE_INTEGER_GETTER(C,member) \
562 WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,member,member)
563
564#define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
565 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
566
567#define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
568 WSLUA_ATTRIBUTE_GET(C,name, { \
569 lua_pushstring(L,obj->member); /* this pushes nil if obj->member is null */ \
570 })
571
572#define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
573 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
574
575#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \
576 WSLUA_ATTRIBUTE_GET(C,name, { \
577 char* str; \
578 if ((obj->member) && (obj->member->len > 0)) { \
579 if (wtap_block_get_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, &str) == WTAP_OPTTYPE_SUCCESS) { \
580 lua_pushstring(L,str); \
581 } \
582 } \
583 })
584
585/*
586 * XXX - we need to support Lua programs getting instances of a "multiple
587 * allowed" option other than the first option.
588 */
589#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \
590 WSLUA_ATTRIBUTE_GET(C,name, { \
591 char* str; \
592 if ((obj->member) && (obj->member->len > 0)) { \
593 if (wtap_block_get_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, &str) == WTAP_OPTTYPE_SUCCESS) { \
594 lua_pushstring(L,str); \
595 } \
596 } \
597 })
598
599#define WSLUA_ATTRIBUTE_SET(C,name,block) \
600 static int C##_set_##name (lua_State* L) { \
601 C obj = check##C (L,1); \
602 block; \
603 return 0; \
604 } \
605 /* silly little trick so we can add a semicolon after this macro */ \
606 typedef void __dummy##C##_set_##name
607
608#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_SETTER(C,name,member) \
609 WSLUA_ATTRIBUTE_SET(C,name, { \
610 if (! lua_isboolean(L,-1) ) \
611 return luaL_error(L, "%s's attribute `%s' must be a boolean", #C , #name ); \
612 obj->member = lua_toboolean(L,-1); \
613 })
614
615/* to make this integral-safe, we treat it as int32 and then cast
616 Note: This will truncate 64-bit integers (but then Lua itself only has doubles */
617#define WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,name,member,cast) \
618 WSLUA_ATTRIBUTE_SET(C,name, { \
619 if (! lua_isinteger(L,-1) ) \
620 return luaL_error(L, "%s's attribute `%s' must be an integer", #C , #name ); \
621 obj->member = (cast) wslua_toint32(L,-1); \
622 })
623
624#define WSLUA_ATTRIBUTE_INTEGER_SETTER(C,member,cast) \
625 WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,member,member,cast)
626
627#define WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,member,need_free) \
628 static int C##_set_##field (lua_State* L) { \
629 C obj = check##C (L,1); \
630 char* s = NULL; \
631 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
632 s = g_strdup(lua_tostring(L,-1)); \
633 } else { \
634 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
635 } \
636 if (obj->member != NULL && need_free) \
637 g_free((void*) obj->member); \
638 obj->member = s; \
639 return 0; \
640 } \
641 /* silly little trick so we can add a semicolon after this macro */ \
642 typedef void __dummy##C##_set_##field
643
644#define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \
645 WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free)
646
647#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_SETTER(C,field,member,option) \
648 static int C##_set_##field (lua_State* L) { \
649 C obj = check##C (L,1); \
650 char* s = NULL; \
651 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
652 s = g_strdup(lua_tostring(L,-1)); \
653 } else { \
654 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
655 } \
656 if ((obj->member) && (obj->member->len > 0)) { \
657 wtap_block_set_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, s, strlen(s)); \
658 } \
659 g_free(s); \
660 return 0; \
661 } \
662 /* silly little trick so we can add a semicolon after this macro */ \
663 typedef void __dummy##C##_set_##field
664
665#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_SETTER(C,field,member,option) \
666 static int C##_set_##field (lua_State* L) { \
667 C obj = check##C (L,1); \
668 char* s = NULL; \
669 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
670 s = g_strdup(lua_tostring(L,-1)); \
671 } else { \
672 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
673 } \
674 if ((obj->member) && (obj->member->len > 0)) { \
675 wtap_block_set_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, s, strlen(s)); \
676 } \
677 g_free(s); \
678 return 0; \
679 } \
680 /* silly little trick so we can add a semicolon after this macro */ \
681 typedef void __dummy##C##_set_##field
682
683#define WSLUA_ERROR(name,error) { luaL_error(L, "%s%s", #name ": ", error); }
684#define WSLUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_ARG_ ## name ## _ ## attr, #name ": " error); }
685#define WSLUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_OPTARG_##name##_ ##attr, #name ": " error); }
686
687#define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushboolean(L,v); lua_setglobal(L,n); }
688#define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,v); lua_setglobal(L,n); }
689#define WSLUA_REG_GLOBAL_INTEGER(L,n,v) { lua_pushinteger(L,v); lua_setglobal(L,n); }
690
691#define WSLUA_RETURN(i) return (i)
692
693#define WSLUA_API extern
694
695/* empty macro arguments trigger ISO C90 warnings, so do this */
696#define NOP (void)p
697
698#define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
699
700#define FAIL_ON_NULL_OR_EXPIRED(s) if (!*p) { \
701 luaL_argerror(L,idx,"null " s); \
702 } else if ((*p)->expired) { \
703 luaL_argerror(L,idx,"expired " s); \
704 }
705
706/* Clears or marks references that connects Lua to Wireshark structures */
707#define CLEAR_OUTSTANDING(C, marker, marker_val) void clear_outstanding_##C(void) { \
708 while (outstanding_##C->len) { \
709 C p = (C)g_ptr_array_remove_index_fast(outstanding_##C,0); \
710 if (p) { \
711 if (p->marker != marker_val) \
712 p->marker = marker_val; \
713 else \
714 g_free(p); \
715 } \
716 } \
717}
718
719#define WSLUA_CLASS_DECLARE(C) \
720extern C to##C(lua_State* L, int idx); \
721extern C check##C(lua_State* L, int idx); \
722extern C* push##C(lua_State* L, C v); \
723extern int C##_register(lua_State* L); \
724extern bool is##C(lua_State* L,int i); \
725extern C shift##C(lua_State* L,int i)
726
727
728/* Throws a Wireshark exception, catchable via normal exceptions.h routines. */
729#define THROW_LUA_ERROR(...) \
730 THROW_FORMATTED(DissectorError, __VA_ARGS__)
731
732/* Catches any Wireshark exceptions in code and convert it into a Lua error.
733 * Normal restrictions for TRY/CATCH apply, in particular, do not return!
734 *
735 * This means do not call lua[L]_error() inside code, as that longjmps out
736 * of the TRY block to the Lua pcall! Use THROW_LUA_ERROR, which is caught
737 * and then converted into a Lua error.
738 *
739 * XXX: We CATCH_ALL here, although there's little point in catching
740 * OutOfMemoryError here. (Is CATCH_BOUNDS_AND_DISSECTOR_ERRORS sufficient?)
741 * There are some Exceptions that we catch and show but don't want to add
742 * the Lua error malformed expert info to the tree: BoundsError,
743 * FragmentBoundsError, and ScsiBoundsError (show_exception doesn't consider
744 * those malformed). The traceback might (or might not) be useful for those.
745 * Putting an extra malformed expert info in the tree in the cases that are
746 * malformed seems not so bad, but we might want to reduce that. Perhaps
747 * at least we could have a separate LuaError type and not call show_exception
748 * for that (we still need to handle some Lua errors that don't use this in
749 * dissector_error_handler.)
750 */
751#define WRAP_NON_LUA_EXCEPTIONS(code) \
752{ \
753 volatile bool has_error = false; \
754 TRY { \
755 code \
756 } CATCH3(BoundsError, FragmentBoundsError, ScsiBoundsError) { \
757 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
758 } CATCH_ALL { \
759 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
760 lua_pushfstring(L, "%s: %s", __func__, GET_MESSAGE ? GET_MESSAGE : "Malformed packet"); \
761 has_error = true; \
762 } ENDTRY; \
763 if (has_error) { lua_error(L); } \
764}
765
766
767extern packet_info* lua_pinfo;
768extern TreeItem lua_tree;
769extern tvbuff_t* lua_tvb;
770extern bool lua_initialized;
771extern int lua_dissectors_table_ref;
772extern int lua_heur_dissectors_table_ref;
773
774WSLUA_DECLARE_CLASSES()
775WSLUA_DECLARE_FUNCTIONS()
776
777extern lua_State* wslua_state(void);
778
779
780/* wslua_internals.c */
787typedef struct _wslua_class {
788 const char *name;
789 const luaL_Reg *class_methods;
790 const luaL_Reg *class_meta;
791 const luaL_Reg *instance_methods;
792 const luaL_Reg *instance_meta;
795void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def);
796void wslua_register_class(lua_State *L, const wslua_class *cls_def);
797
798extern int wslua__concat(lua_State* L);
799extern bool wslua_toboolean(lua_State* L, int n);
800extern bool wslua_checkboolean(lua_State* L, int n);
801extern bool wslua_optbool(lua_State* L, int n, bool def);
802extern lua_Integer wslua_tointeger(lua_State* L, int n);
803extern int wslua_optboolint(lua_State* L, int n, int def);
804extern const char* wslua_checklstring_only(lua_State* L, int n, size_t *l);
805extern const char* wslua_checkstring_only(lua_State* L, int n);
806extern void wslua_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
807extern const char* wslua_typeof_unknown;
808extern const char* wslua_typeof(lua_State *L, int idx);
809extern bool wslua_get_table(lua_State *L, int idx, const char *name);
810extern bool wslua_get_field(lua_State *L, int idx, const char *name);
811extern int dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
812extern bool heur_dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
813extern expert_field* wslua_get_expert_field(const int group, const int severity);
814extern void wslua_prefs_changed(void);
815extern void proto_register_lua(void);
816extern GString* lua_register_all_taps(void);
817extern void wslua_prime_dfilter(epan_dissect_t *edt);
818extern bool wslua_has_field_extractors(void);
819extern void lua_prime_all_fields(proto_tree* tree);
820
821extern int Proto_commit(lua_State* L);
822
823extern TreeItem create_TreeItem(proto_tree* tree, proto_item* item);
824
825extern void clear_outstanding_FuncSavers(void);
826
827extern void Int64_pack(lua_State* L, luaL_Buffer *b, int idx, bool asLittleEndian);
828extern int Int64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
829extern void UInt64_pack(lua_State* L, luaL_Buffer *b, int idx, bool asLittleEndian);
830extern int UInt64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
831extern uint64_t getUInt64(lua_State *L, int i);
832
833extern Tvb* push_Tvb(lua_State* L, tvbuff_t* tvb);
834extern int push_wsluaTvb(lua_State* L, Tvb t);
835extern bool push_TvbRange(lua_State* L, tvbuff_t* tvb, int offset, int len);
836extern void clear_outstanding_Tvb(void);
837extern void clear_outstanding_TvbRange(void);
838
839extern Pinfo* push_Pinfo(lua_State* L, packet_info* p);
840extern void clear_outstanding_Pinfo(void);
841extern void clear_outstanding_Column(void);
842extern void clear_outstanding_Columns(void);
843extern void clear_outstanding_PrivateTable(void);
844
845extern int get_hf_wslua_text(void);
846extern TreeItem push_TreeItem(lua_State *L, proto_tree *tree, proto_item *item);
847extern void clear_outstanding_TreeItem(void);
848
849extern FieldInfo* push_FieldInfo(lua_State *L, field_info* f);
850extern void clear_outstanding_FieldInfo(void);
851
852extern void wslua_print_stack(char* s, lua_State* L);
853
854extern void wslua_init(register_cb cb, void *client_data);
855extern void wslua_early_cleanup(void);
856extern void wslua_cleanup(void);
857
858extern tap_extractor_t wslua_get_tap_extractor(const char* name);
859extern int wslua_set_tap_enums(lua_State* L);
860
861extern ProtoField wslua_is_field_available(lua_State* L, const char* field_abbr);
862
863extern char* wslua_get_actual_filename(const char* fname);
864
865extern int wslua_bin2hex(lua_State* L, const uint8_t* data, const unsigned len, const bool lowercase, const char* sep);
866extern int wslua_hex2bin(lua_State* L, const char* data, const unsigned len, const char* sep);
867extern int luaopen_rex_pcre2(lua_State *L);
868
869extern const char* get_current_plugin_version(void);
870extern void clear_current_plugin_version(void);
871
872extern int wslua_deregister_heur_dissectors(lua_State* L);
873extern int wslua_deregister_protocols(lua_State* L);
874extern int wslua_deregister_dissector_tables(lua_State* L);
875extern int wslua_deregister_listeners(lua_State* L);
876extern int wslua_deregister_fields(lua_State* L);
877extern int wslua_deregister_filehandlers(lua_State* L);
878extern void wslua_deregister_menus(void);
879
880extern void wslua_init_wtap_filetypes(lua_State* L);
881
882#endif
883
884/*
885 * Editor modelines - https://www.wireshark.org/tools/modelines.html
886 *
887 * Local variables:
888 * c-basic-offset: 4
889 * tab-width: 8
890 * indent-tabs-mode: nil
891 * End:
892 *
893 * vi: set shiftwidth=4 tabstop=8 expandtab:
894 * :indentSize=4:tabSize=8:noTabs=true:
895 */
#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:447
Definition wslua.h:300
Type for defining new classes.
Definition wslua.h:787
const wslua_attribute_table * attrs
Definition wslua.h:793
const luaL_Reg * class_meta
Definition wslua.h:790
const char * name
Definition wslua.h:788
const luaL_Reg * class_methods
Definition wslua.h:789
const luaL_Reg * instance_methods
Definition wslua.h:791
const luaL_Reg * instance_meta
Definition wslua.h:792
Definition wslua.h:241
Definition wslua.h:247
Definition wslua.h:311
Definition wslua.h:339
Definition wslua.h:232
Definition wslua.h:158
Definition wslua.h:270
Definition wslua.h:145
Definition wslua.h:293
Definition wslua.h:317
Definition wslua.h:265
Definition wslua.h:128
Definition wslua.h:180
struct _wslua_pref_t::@495::@496 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::@495 info
Definition wslua.h:252
Definition wslua.h:344
Definition wslua.h:210
Definition wslua.h:306
Definition wslua.h:277
Definition wslua.h:258
Definition wslua.h:122
Definition wslua.h:133
Definition wslua.h:139
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:351
Definition tvbuff-int.h:35
Definition wslua.h:353
Definition wtap-int.h:98
Definition file_wrappers.c:230
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