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