Replace g_log() calls with ws_log()
[metze/wireshark/wip.git] / epan / wslua / wslua.h
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 <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include <lua.h>
26 #include <lualib.h>
27 #include <lauxlib.h>
28
29 #include <wiretap/wtap.h>
30
31 #include <wsutil/report_message.h>
32 #include <wsutil/nstime.h>
33 #include <wsutil/ws_assert.h>
34 #include <wsutil/wslog.h>
35
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
38 #include <epan/to_str.h>
39 #include <epan/prefs.h>
40 #include <epan/proto.h>
41 #include <epan/epan_dissect.h>
42 #include <epan/tap.h>
43 #include <epan/column-utils.h>
44 #include <wsutil/filesystem.h>
45 #include <epan/funnel.h>
46 #include <epan/tvbparse.h>
47 #include <epan/epan.h>
48 #include <epan/expert.h>
49
50 #include <epan/wslua/declare_wslua.h>
51
52 /** @file
53  * @ingroup wslua_group
54  */
55
56 #define WSLUA_INIT_ROUTINES "init_routines"
57 #define WSLUA_PREFS_CHANGED "prefs_changed"
58
59 typedef void (*wslua_logger_t)(const gchar *, GLogLevelFlags, const gchar *, gpointer);
60 extern wslua_logger_t wslua_logger;
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 different
64    on different machines; so use these instead please! */
65 #define wslua_togint(L,i)       (gint)            ( lua_tointeger(L,i) )
66 #define wslua_togint32(L,i)     (gint32)          ( lua_tonumber(L,i) )
67 #define wslua_togint64(L,i)     (gint64)          ( lua_tonumber(L,i) )
68 #define wslua_toguint(L,i)      (guint)           ( lua_tointeger(L,i) )
69 #define wslua_toguint32(L,i)    (guint32)         ( lua_tonumber(L,i) )
70 #define wslua_toguint64(L,i)    (guint64)         ( lua_tonumber(L,i) )
71
72 #define wslua_checkgint(L,i)    (gint)            ( luaL_checkinteger(L,i) )
73 #define wslua_checkgint32(L,i)  (gint32)          ( luaL_checknumber(L,i) )
74 #define wslua_checkgint64(L,i)  (gint64)          ( luaL_checknumber(L,i) )
75 #define wslua_checkguint(L,i)   (guint)           ( luaL_checkinteger(L,i) )
76 #define wslua_checkguint32(L,i) (guint32)         ( luaL_checknumber(L,i) )
77 #define wslua_checkguint64(L,i) (guint64)         ( luaL_checknumber(L,i) )
78
79 #define wslua_optgint(L,i,d)    (gint)            ( luaL_optinteger(L,i,d) )
80 #define wslua_optgint32(L,i,d)  (gint32)          ( luaL_optnumber(L,i,d) )
81 #define wslua_optgint64(L,i,d)  (gint64)          ( luaL_optnumber(L,i,d) )
82 #define wslua_optguint(L,i,d)   (guint)           ( luaL_optinteger(L,i,d) )
83 #define wslua_optguint32(L,i,d) (guint32)         ( luaL_optnumber(L,i,d) )
84 #define wslua_optguint64(L,i,d) (guint64)         ( luaL_optnumber(L,i,d) )
85
86
87 struct _wslua_tvb {
88     tvbuff_t* ws_tvb;
89     gboolean expired;
90     gboolean need_free;
91 };
92
93 struct _wslua_pinfo {
94     packet_info* ws_pinfo;
95     gboolean expired;
96 };
97
98 struct _wslua_tvbrange {
99     struct _wslua_tvb* tvb;
100     int offset;
101     int len;
102 };
103
104 struct _wslua_tw {
105     funnel_text_window_t* ws_tw;
106     gboolean expired;
107     void* close_cb_data;
108 };
109
110 typedef struct _wslua_field_t {
111     int hfid;
112     int ett;
113     char* name;
114     char* abbrev;
115     char* blob;
116     enum ftenum type;
117     unsigned base;
118     const void* vs;
119     guint32 mask;
120 } wslua_field_t;
121
122 typedef struct _wslua_expert_field_t {
123     expert_field ids;
124     const gchar *abbrev;
125     const gchar *text;
126     int group;
127     int severity;
128 } wslua_expert_field_t;
129
130 /**
131  * PREF_OBSOLETE is used for preferences that a module used to support
132  * but no longer supports; we give different error messages for them.
133  */
134 typedef enum {
135     PREF_UINT,
136     PREF_BOOL,
137     PREF_ENUM,
138     PREF_STRING,
139     PREF_RANGE,
140     PREF_STATIC_TEXT,
141     PREF_OBSOLETE
142 } pref_type_t;
143
144 typedef struct _wslua_pref_t {
145     gchar* name;
146     gchar* label;
147     gchar* desc;
148     pref_type_t type;
149     union {
150         gboolean b;
151         guint u;
152         gchar* s;
153         gint e;
154         range_t *r;
155         void* p;
156     } value;
157     union {
158       guint32 max_value;         /**< maximum value of a range */
159       struct {
160           const enum_val_t *enumvals;    /**< list of name & values */
161           gboolean radio_buttons;    /**< TRUE if it should be shown as
162                          radio buttons rather than as an
163                          option menu or combo box in
164                          the preferences tab */
165       } enum_info;            /**< for PREF_ENUM */
166       gchar* default_s;       /**< default value for value.s */
167     } info;                    /**< display/text file information */
168
169     struct _wslua_pref_t* next;
170     struct _wslua_proto_t* proto;
171     int ref;            /* Reference to enable Proto to deregister prefs. */
172 } wslua_pref_t;
173
174 typedef struct _wslua_proto_t {
175     gchar* name;
176     gchar* loname;
177     gchar* desc;
178     int hfid;
179     int ett;
180     wslua_pref_t prefs;
181     int fields;
182     int expert_info_table_ref;
183     expert_module_t *expert_module;
184     module_t *prefs_module;
185     dissector_handle_t handle;
186     GArray *hfa;
187     GArray *etta;
188     GArray *eia;
189     gboolean is_postdissector;
190     gboolean expired;
191 } wslua_proto_t;
192
193 struct _wslua_distbl_t {
194     dissector_table_t table;
195     const gchar* name;
196     const gchar* ui_name;
197     gboolean created;
198     gboolean expired;
199 };
200
201 struct _wslua_col_info {
202     column_info* cinfo;
203     gint col;
204     gboolean expired;
205 };
206
207 struct _wslua_cols {
208     column_info* cinfo;
209     gboolean expired;
210 };
211
212 struct _wslua_private_table {
213     GHashTable *table;
214     gboolean is_allocated;
215     gboolean expired;
216 };
217
218 struct _wslua_treeitem {
219     proto_item* item;
220     proto_tree* tree;
221     gboolean expired;
222 };
223
224 // Internal structure for wslua_field.c to track info about registered fields.
225 struct _wslua_header_field_info {
226     char *name;
227     header_field_info *hfi;
228 };
229
230 struct _wslua_field_info {
231     field_info *ws_fi;
232     gboolean expired;
233 };
234
235 typedef void (*tap_extractor_t)(lua_State*,const void*);
236
237 struct _wslua_tap {
238     gchar* name;
239     gchar* filter;
240     tap_extractor_t extractor;
241     lua_State* L;
242     int packet_ref;
243     int draw_ref;
244     int reset_ref;
245     gboolean all_fields;
246 };
247
248 /* a "File" object can be different things under the hood. It can either
249    be a FILE_T from wtap struct, which it is during read operations, or it
250    can be a wtap_dumper struct during write operations. A wtap_dumper struct
251    has a FILE_T member, but we can't only store its pointer here because
252    dump operations need the whole thing to write out with. Ugh. */
253 struct _wslua_file {
254     FILE_T   file;
255     wtap_dumper *wdh;   /* will be NULL during read usage */
256     gboolean expired;
257 };
258
259 /* a "CaptureInfo" object can also be different things under the hood. */
260 struct _wslua_captureinfo {
261     wtap *wth;          /* will be NULL during write usage */
262     wtap_dumper *wdh;   /* will be NULL during read usage */
263     gboolean expired;
264 };
265
266 struct _wslua_phdr {
267     wtap_rec *rec;      /* this also exists in wtap struct, but is different for seek_read ops */
268     Buffer *buf;        /* can't use the one in wtap because it's different for seek_read ops */
269     gboolean expired;
270 };
271
272 struct _wslua_const_phdr {
273     const wtap_rec *rec;
274     const guint8 *pd;
275     gboolean expired;
276 };
277
278 struct _wslua_filehandler {
279     struct file_type_subtype_info finfo;
280     gboolean is_reader;
281     gboolean is_writer;
282     gchar* internal_description; /* XXX - this is redundant; finfo.description should suffice */
283     gchar* type;
284     gchar* extensions;
285     lua_State* L;
286     int read_open_ref;
287     int read_ref;
288     int seek_read_ref;
289     int read_close_ref;
290     int seq_read_close_ref;
291     int can_write_encap_ref;
292     int write_open_ref;
293     int write_ref;
294     int write_close_ref;
295     int file_type;
296     gboolean registered;
297 };
298
299 struct _wslua_dir {
300     GDir* dir;
301     char* ext;
302 };
303
304 struct _wslua_progdlg {
305     struct progdlg* pw;
306     char* title;
307     char* task;
308     gboolean stopped;
309 };
310
311 typedef struct { const char* name; tap_extractor_t extractor; } tappable_t;
312
313 typedef struct {const gchar* str; enum ftenum id; } wslua_ft_types_t;
314
315 typedef wslua_pref_t* Pref;
316 typedef wslua_pref_t* Prefs;
317 typedef struct _wslua_field_t* ProtoField;
318 typedef struct _wslua_expert_field_t* ProtoExpert;
319 typedef struct _wslua_proto_t* Proto;
320 typedef struct _wslua_distbl_t* DissectorTable;
321 typedef dissector_handle_t Dissector;
322 typedef GByteArray* ByteArray;
323 typedef struct _wslua_tvb* Tvb;
324 typedef struct _wslua_tvbrange* TvbRange;
325 typedef struct _wslua_col_info* Column;
326 typedef struct _wslua_cols* Columns;
327 typedef struct _wslua_pinfo* Pinfo;
328 typedef struct _wslua_treeitem* TreeItem;
329 typedef address* Address;
330 typedef nstime_t* NSTime;
331 typedef gint64 Int64;
332 typedef guint64 UInt64;
333 typedef struct _wslua_header_field_info* Field;
334 typedef struct _wslua_field_info* FieldInfo;
335 typedef struct _wslua_tap* Listener;
336 typedef struct _wslua_tw* TextWindow;
337 typedef struct _wslua_progdlg* ProgDlg;
338 typedef struct _wslua_file* File;
339 typedef struct _wslua_captureinfo* CaptureInfo;
340 typedef struct _wslua_captureinfo* CaptureInfoConst;
341 typedef struct _wslua_phdr* FrameInfo;
342 typedef struct _wslua_const_phdr* FrameInfoConst;
343 typedef struct _wslua_filehandler* FileHandler;
344 typedef wtap_dumper* Dumper;
345 typedef struct lua_pseudo_header* PseudoHeader;
346 typedef tvbparse_t* Parser;
347 typedef tvbparse_wanted_t* Rule;
348 typedef tvbparse_elem_t* Node;
349 typedef tvbparse_action_t* Shortcut;
350 typedef struct _wslua_dir* Dir;
351 typedef struct _wslua_private_table* PrivateTable;
352 typedef gchar* Struct;
353
354 /*
355  * toXxx(L,idx) gets a Xxx from an index (Lua Error if fails)
356  * checkXxx(L,idx) gets a Xxx from an index after calling check_code (No Lua Error if it fails)
357  * pushXxx(L,xxx) pushes an Xxx into the stack
358  * isXxx(L,idx) tests whether we have an Xxx at idx
359  * shiftXxx(L,idx) removes and returns an Xxx from idx only if it has a type of Xxx, returns NULL otherwise
360  * WSLUA_CLASS_DEFINE must be used with a trailing ';'
361  * (a dummy typedef is used to be syntactically correct)
362  */
363 #define WSLUA_CLASS_DEFINE(C,check_code) \
364     WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL)
365
366 #define WSLUA_CLASS_DEFINE_BASE(C,check_code,retval) \
367 C to##C(lua_State* L, int idx) { \
368     C* v = (C*)lua_touserdata (L, idx); \
369     if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)", idx, #C, lua_typename(L, lua_type(L, idx))); \
370     return v ? *v : retval; \
371 } \
372 C check##C(lua_State* L, int idx) { \
373     C* p; \
374     luaL_checktype(L,idx,LUA_TUSERDATA); \
375     p = (C*)luaL_checkudata(L, idx, #C); \
376     check_code; \
377     return p ? *p : retval; \
378 } \
379 C* push##C(lua_State* L, C v) { \
380     C* p; \
381     luaL_checkstack(L,2,"Unable to grow stack\n"); \
382     p = (C*)lua_newuserdata(L,sizeof(C)); *p = v; \
383     luaL_getmetatable(L, #C); lua_setmetatable(L, -2); \
384     return p; \
385 }\
386 gboolean is##C(lua_State* L,int i) { \
387     void *p; \
388     if(!lua_isuserdata(L,i)) return FALSE; \
389     p = lua_touserdata(L, i); \
390     lua_getfield(L, LUA_REGISTRYINDEX, #C); \
391     if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
392     lua_pop(L, 2); \
393     return p ? TRUE : FALSE; \
394 } \
395 C shift##C(lua_State* L,int i) { \
396     C* p; \
397     if(!lua_isuserdata(L,i)) return retval; \
398     p = (C*)lua_touserdata(L, i); \
399     lua_getfield(L, LUA_REGISTRYINDEX, #C); \
400     if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
401     lua_pop(L, 2); \
402     if (p) { lua_remove(L,i); return *p; }\
403     else return retval;\
404 } \
405 typedef int dummy##C
406
407 typedef struct _wslua_attribute_table {
408     const gchar  *fieldname;
409     lua_CFunction getfunc;
410     lua_CFunction setfunc;
411 } wslua_attribute_table;
412 extern int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, gboolean is_getter);
413
414 #define WSLUA_TYPEOF_FIELD "__typeof"
415
416 #ifdef HAVE_LUA
417
418 /* temporary transition macro to reduce duplication in WSLUA_REGISTER_xxx. */
419 #define WSLUA_REGISTER_GC(C) \
420     luaL_getmetatable(L, #C); \
421      /* add the '__gc' metamethod with a C-function named Class__gc */ \
422     /* this will force ALL wslua classes to have a Class__gc function defined, which is good */ \
423     lua_pushcfunction(L, C ## __gc); \
424     lua_setfield(L, -2, "__gc"); \
425     /* pop the metatable */ \
426     lua_pop(L, 1)
427
428 #define __WSLUA_REGISTER_META(C, ATTRS) { \
429     const wslua_class C ## _class = { \
430         .name               = #C, \
431         .instance_meta      = C ## _meta, \
432         .attrs              = ATTRS \
433     }; \
434     wslua_register_classinstance_meta(L, &C ## _class); \
435     WSLUA_REGISTER_GC(C); \
436 }
437
438 #define WSLUA_REGISTER_META(C)  __WSLUA_REGISTER_META(C, NULL)
439 #define WSLUA_REGISTER_META_WITH_ATTRS(C) \
440     __WSLUA_REGISTER_META(C, C ## _attributes)
441
442 #define __WSLUA_REGISTER_CLASS(C, ATTRS) { \
443     const wslua_class C ## _class = { \
444         .name               = #C, \
445         .class_methods      = C ## _methods, \
446         .class_meta         = C ## _meta, \
447         .instance_methods   = C ## _methods, \
448         .instance_meta      = C ## _meta, \
449         .attrs              = ATTRS \
450     }; \
451     wslua_register_class(L, &C ## _class); \
452     WSLUA_REGISTER_GC(C); \
453 }
454
455 #define WSLUA_REGISTER_CLASS(C)  __WSLUA_REGISTER_CLASS(C, NULL)
456 #define WSLUA_REGISTER_CLASS_WITH_ATTRS(C) \
457     __WSLUA_REGISTER_CLASS(C, C ## _attributes)
458
459 #define WSLUA_INIT(L) \
460     luaL_openlibs(L); \
461     wslua_register_classes(L); \
462     wslua_register_functions(L);
463
464 #endif
465
466 #define WSLUA_FUNCTION extern int
467 /* This is for functions intended only to be used in init.lua */
468 #define WSLUA_INTERNAL_FUNCTION extern int
469
470 #define WSLUA_REGISTER_FUNCTION(name)     { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }
471
472 #define WSLUA_REGISTER extern int
473
474 #define WSLUA_METHOD static int
475 #define WSLUA_CONSTRUCTOR static int
476 #define WSLUA_ATTR_SET static int
477 #define WSLUA_ATTR_GET static int
478 #define WSLUA_METAMETHOD static int
479
480 #define WSLUA_METHODS static const luaL_Reg
481 #define WSLUA_META static const luaL_Reg
482 #define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name }
483 #define WSLUA_CLASS_FNREG_ALIAS(class,aliasname,name) { #aliasname, class##_##name }
484 #define WSLUA_CLASS_MTREG(class,name) { "__" #name, class##__##name }
485
486 #define WSLUA_ATTRIBUTES static const wslua_attribute_table
487 /* following are useful macros for the rows in the array created by above */
488 #define WSLUA_ATTRIBUTE_RWREG(class,name) { #name, class##_get_##name, class##_set_##name }
489 #define WSLUA_ATTRIBUTE_ROREG(class,name) { #name, class##_get_##name, NULL }
490 #define WSLUA_ATTRIBUTE_WOREG(class,name) { #name, NULL, class##_set_##name }
491
492 #define WSLUA_ATTRIBUTE_FUNC_SETTER(C,field) \
493     static int C##_set_##field (lua_State* L) { \
494         C obj = check##C (L,1); \
495         if (! lua_isfunction(L,-1) ) \
496             return luaL_error(L, "%s's attribute `%s' must be a function", #C , #field ); \
497         if (obj->field##_ref != LUA_NOREF) \
498             /* there was one registered before, remove it */ \
499             luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \
500         obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
501         return 0; \
502     } \
503     /* silly little trick so we can add a semicolon after this macro */ \
504     typedef void __dummy##C##_set_##field
505
506 #define WSLUA_ATTRIBUTE_GET(C,name,block) \
507     static int C##_get_##name (lua_State* L) { \
508         C obj = check##C (L,1); \
509         block \
510         return 1; \
511     } \
512     /* silly little trick so we can add a semicolon after this macro */ \
513     typedef void __dummy##C##_get_##name
514
515 #define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
516     WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
517
518 #define WSLUA_ATTRIBUTE_NAMED_NUMBER_GETTER(C,name,member) \
519     WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(obj->member));})
520
521 #define WSLUA_ATTRIBUTE_NUMBER_GETTER(C,member) \
522     WSLUA_ATTRIBUTE_NAMED_NUMBER_GETTER(C,member,member)
523
524 #define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
525     WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
526
527 #define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
528     WSLUA_ATTRIBUTE_GET(C,name, { \
529         lua_pushstring(L,obj->member); /* this pushes nil if obj->member is null */ \
530     })
531
532 #define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
533     WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
534
535 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \
536     WSLUA_ATTRIBUTE_GET(C,name, { \
537         char* str;  \
538         if ((obj->member) && (obj->member->len > 0)) { \
539             if (wtap_block_get_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, &str) == WTAP_OPTTYPE_SUCCESS) { \
540                 lua_pushstring(L,str); \
541             } \
542         } \
543     })
544
545 /*
546  * XXX - we need to support Lua programs getting instances of a "multiple
547  * allowed" option other than the first option.
548  */
549 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \
550     WSLUA_ATTRIBUTE_GET(C,name, { \
551         char* str;  \
552         if ((obj->member) && (obj->member->len > 0)) { \
553             if (wtap_block_get_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, &str) == WTAP_OPTTYPE_SUCCESS) { \
554                 lua_pushstring(L,str); \
555             } \
556         } \
557     })
558
559 #define WSLUA_ATTRIBUTE_SET(C,name,block) \
560     static int C##_set_##name (lua_State* L) { \
561         C obj = check##C (L,1); \
562         block; \
563         return 0; \
564     } \
565     /* silly little trick so we can add a semicolon after this macro */ \
566     typedef void __dummy##C##_set_##name
567
568 #define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_SETTER(C,name,member) \
569     WSLUA_ATTRIBUTE_SET(C,name, { \
570         if (! lua_isboolean(L,-1) ) \
571             return luaL_error(L, "%s's attribute `%s' must be a boolean", #C , #name ); \
572         obj->member = lua_toboolean(L,-1); \
573     })
574
575 /* to make this integral-safe, we treat it as int32 and then cast
576    Note: This will truncate 64-bit integers (but then Lua itself only has doubles */
577 #define WSLUA_ATTRIBUTE_NAMED_NUMBER_SETTER(C,name,member,cast) \
578     WSLUA_ATTRIBUTE_SET(C,name, { \
579         if (! lua_isnumber(L,-1) ) \
580             return luaL_error(L, "%s's attribute `%s' must be a number", #C , #name ); \
581         obj->member = (cast) wslua_togint32(L,-1); \
582     })
583
584 #define WSLUA_ATTRIBUTE_NUMBER_SETTER(C,member,cast) \
585     WSLUA_ATTRIBUTE_NAMED_NUMBER_SETTER(C,member,member,cast)
586
587 #define WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,member,need_free) \
588     static int C##_set_##field (lua_State* L) { \
589         C obj = check##C (L,1); \
590         gchar* s = NULL; \
591         if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
592             s = g_strdup(lua_tostring(L,-1)); \
593         } else { \
594             return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
595         } \
596         if (obj->member != NULL && need_free) \
597             g_free((void*) obj->member); \
598         obj->member = s; \
599         return 0; \
600     } \
601     /* silly little trick so we can add a semicolon after this macro */ \
602     typedef void __dummy##C##_set_##field
603
604 #define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \
605     WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free)
606
607 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_SETTER(C,field,member,option) \
608     static int C##_set_##field (lua_State* L) { \
609         C obj = check##C (L,1); \
610         gchar* s = NULL; \
611         if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
612             s = g_strdup(lua_tostring(L,-1)); \
613         } else { \
614             return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
615         } \
616         if ((obj->member) && (obj->member->len > 0)) { \
617             wtap_block_set_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, s, strlen(s)); \
618         } \
619         g_free(s); \
620         return 0; \
621     } \
622     /* silly little trick so we can add a semicolon after this macro */ \
623     typedef void __dummy##C##_set_##field
624
625 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_SETTER(C,field,member,option) \
626     static int C##_set_##field (lua_State* L) { \
627         C obj = check##C (L,1); \
628         gchar* s = NULL; \
629         if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
630             s = g_strdup(lua_tostring(L,-1)); \
631         } else { \
632             return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
633         } \
634         if ((obj->member) && (obj->member->len > 0)) { \
635             wtap_block_set_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, s, strlen(s)); \
636         } \
637         g_free(s); \
638         return 0; \
639     } \
640     /* silly little trick so we can add a semicolon after this macro */ \
641     typedef void __dummy##C##_set_##field
642
643 #define WSLUA_ERROR(name,error) { luaL_error(L, "%s%s", #name ": " ,error); }
644 #define WSLUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_ARG_ ## name ## _ ## attr, #name  ": " error); }
645 #define WSLUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_OPTARG_##name##_ ##attr, #name  ": " error); }
646
647 #define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushboolean(L,v); lua_setglobal(L,n); }
648 #define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,v); lua_setglobal(L,n); }
649 #define WSLUA_REG_GLOBAL_NUMBER(L,n,v) { lua_pushnumber(L,v); lua_setglobal(L,n); }
650
651 #define WSLUA_RETURN(i) return (i)
652
653 #define WSLUA_API extern
654
655 /* empty macro arguments trigger ISO C90 warnings, so do this */
656 #define NOP (void)p
657
658 #define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
659
660 #define FAIL_ON_NULL_OR_EXPIRED(s) if (!*p) { \
661         luaL_argerror(L,idx,"null " s); \
662     } else if ((*p)->expired) { \
663         luaL_argerror(L,idx,"expired " s); \
664     }
665
666 /* Clears or marks references that connects Lua to Wireshark structures */
667 #define CLEAR_OUTSTANDING(C, marker, marker_val) void clear_outstanding_##C(void) { \
668     while (outstanding_##C->len) { \
669         C p = (C)g_ptr_array_remove_index_fast(outstanding_##C,0); \
670         if (p) { \
671             if (p->marker != marker_val) \
672                 p->marker = marker_val; \
673             else \
674                 g_free(p); \
675         } \
676     } \
677 }
678
679 #define WSLUA_CLASS_DECLARE(C) \
680 extern C to##C(lua_State* L, int idx); \
681 extern C check##C(lua_State* L, int idx); \
682 extern C* push##C(lua_State* L, C v); \
683 extern int C##_register(lua_State* L); \
684 extern gboolean is##C(lua_State* L,int i); \
685 extern C shift##C(lua_State* L,int i)
686
687
688 /* Throws a Wireshark exception, catchable via normal exceptions.h routines. */
689 #define THROW_LUA_ERROR(...) \
690     THROW_FORMATTED(DissectorError, __VA_ARGS__)
691
692 /* Catches any Wireshark exceptions in code and convert it into a LUA error.
693  * Normal restrictions for TRY/CATCH apply, in particular, do not return! */
694 #define WRAP_NON_LUA_EXCEPTIONS(code) \
695 { \
696     volatile gboolean has_error = FALSE; \
697     TRY { \
698         code \
699     } CATCH_ALL { \
700         lua_pushstring(L, GET_MESSAGE);  \
701         has_error = TRUE; \
702     } ENDTRY; \
703     if (has_error) { lua_error(L); } \
704 }
705
706
707 extern packet_info* lua_pinfo;
708 extern TreeItem lua_tree;
709 extern tvbuff_t* lua_tvb;
710 extern gboolean lua_initialized;
711 extern int lua_dissectors_table_ref;
712 extern int lua_heur_dissectors_table_ref;
713
714 WSLUA_DECLARE_CLASSES()
715 WSLUA_DECLARE_FUNCTIONS()
716
717 extern lua_State* wslua_state(void);
718
719
720 /* wslua_internals.c */
721 /**
722  * @brief Type for defining new classes.
723  *
724  * A new class is defined as a Lua table type. Instances of this class are
725  * created through pushXxx which sets the appropriate metatable.
726  */
727 typedef struct _wslua_class {
728     const char *name;                   /**< Class name that is exposed to Lua code. */
729     const luaL_Reg *class_methods;      /**< Methods for the static class (optional) */
730     const luaL_Reg *class_meta;         /**< Metatable for the static class (optional) */
731     const luaL_Reg *instance_methods;   /**< Methods for class instances. (optional) */
732     const luaL_Reg *instance_meta;      /**< Metatable for class instances (optional) */
733     const wslua_attribute_table *attrs; /**< Table of getters/setters for attributes on class instances (optional). */
734 } wslua_class;
735 void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def);
736 void wslua_register_class(lua_State *L, const wslua_class *cls_def);
737
738 extern int wslua__concat(lua_State* L);
739 extern gboolean wslua_toboolean(lua_State* L, int n);
740 extern gboolean wslua_checkboolean(lua_State* L, int n);
741 extern gboolean wslua_optbool(lua_State* L, int n, gboolean def);
742 extern lua_Integer wslua_tointeger(lua_State* L, int n);
743 extern int wslua_optboolint(lua_State* L, int n, int def);
744 extern const char* wslua_checklstring_only(lua_State* L, int n, size_t *l);
745 extern const char* wslua_checkstring_only(lua_State* L, int n);
746 extern void wslua_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
747 extern const gchar* wslua_typeof_unknown;
748 extern const gchar* wslua_typeof(lua_State *L, int idx);
749 extern gboolean wslua_get_table(lua_State *L, int idx, const gchar *name);
750 extern gboolean wslua_get_field(lua_State *L, int idx, const gchar *name);
751 extern int dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
752 extern int heur_dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
753 extern expert_field* wslua_get_expert_field(const int group, const int severity);
754 extern void wslua_prefs_changed(void);
755 extern void proto_register_lua(void);
756 extern GString* lua_register_all_taps(void);
757 extern void wslua_prime_dfilter(epan_dissect_t *edt);
758 extern gboolean wslua_has_field_extractors(void);
759 extern void lua_prime_all_fields(proto_tree* tree);
760
761 extern int Proto_commit(lua_State* L);
762
763 extern TreeItem create_TreeItem(proto_tree* tree, proto_item* item);
764
765 extern void clear_outstanding_FuncSavers(void);
766
767 extern void Int64_pack(lua_State* L, luaL_Buffer *b, gint idx, gboolean asLittleEndian);
768 extern int Int64_unpack(lua_State* L, const gchar *buff, gboolean asLittleEndian);
769 extern void UInt64_pack(lua_State* L, luaL_Buffer *b, gint idx, gboolean asLittleEndian);
770 extern int UInt64_unpack(lua_State* L, const gchar *buff, gboolean asLittleEndian);
771
772 extern Tvb* push_Tvb(lua_State* L, tvbuff_t* tvb);
773 extern int push_wsluaTvb(lua_State* L, Tvb t);
774 extern gboolean push_TvbRange(lua_State* L, tvbuff_t* tvb, int offset, int len);
775 extern void clear_outstanding_Tvb(void);
776 extern void clear_outstanding_TvbRange(void);
777
778 extern Pinfo* push_Pinfo(lua_State* L, packet_info* p);
779 extern void clear_outstanding_Pinfo(void);
780 extern void clear_outstanding_Column(void);
781 extern void clear_outstanding_Columns(void);
782 extern void clear_outstanding_PrivateTable(void);
783
784 extern int get_hf_wslua_text(void);
785 extern TreeItem push_TreeItem(lua_State *L, proto_tree *tree, proto_item *item);
786 extern void clear_outstanding_TreeItem(void);
787
788 extern FieldInfo* push_FieldInfo(lua_State *L, field_info* f);
789 extern void clear_outstanding_FieldInfo(void);
790
791 extern void wslua_print_stack(char* s, lua_State* L);
792
793 extern void wslua_init(register_cb cb, gpointer client_data);
794 extern void wslua_early_cleanup(void);
795 extern void wslua_cleanup(void);
796
797 extern tap_extractor_t wslua_get_tap_extractor(const gchar* name);
798 extern int wslua_set_tap_enums(lua_State* L);
799
800 extern ProtoField wslua_is_field_available(lua_State* L, const char* field_abbr);
801
802 extern char* wslua_get_actual_filename(const char* fname);
803
804 extern int wslua_bin2hex(lua_State* L, const guint8* data, const guint len, const gboolean lowercase, const gchar* sep);
805 extern int wslua_hex2bin(lua_State* L, const char* data, const guint len, const gchar* sep);
806 extern int luaopen_rex_glib(lua_State *L);
807
808 extern const gchar* get_current_plugin_version(void);
809 extern void clear_current_plugin_version(void);
810
811 extern int wslua_deregister_heur_dissectors(lua_State* L);
812 extern int wslua_deregister_protocols(lua_State* L);
813 extern int wslua_deregister_dissector_tables(lua_State* L);
814 extern int wslua_deregister_listeners(lua_State* L);
815 extern int wslua_deregister_fields(lua_State* L);
816 extern int wslua_deregister_filehandlers(lua_State* L);
817 extern void wslua_deregister_menus(void);
818
819 #endif
820
821 /*
822  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
823  *
824  * Local variables:
825  * c-basic-offset: 4
826  * tab-width: 8
827  * indent-tabs-mode: nil
828  * End:
829  *
830  * vi: set shiftwidth=4 tabstop=8 expandtab:
831  * :indentSize=4:tabSize=8:noTabs=true:
832  */