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_toint(L,i) (int) ( lua_tointeger(L,i) )
64 #define wslua_toint32(L,i) (int32_t) ( lua_tonumber(L,i) )
65 #define wslua_toint64(L,i) (int64_t) ( lua_tonumber(L,i) )
66 #define wslua_touint(L,i) (unsigned) ( lua_tointeger(L,i) )
67 #define wslua_touint32(L,i) (uint32_t) ( lua_tonumber(L,i) )
68 #define wslua_touint64(L,i) (uint64_t) ( lua_tonumber(L,i) )
69 
70 #define wslua_checkint(L,i) (int) ( luaL_checkinteger(L,i) )
71 #define wslua_checkint32(L,i) (int32_t) ( luaL_checknumber(L,i) )
72 #define wslua_checkint64(L,i) (int64_t) ( luaL_checknumber(L,i) )
73 #define wslua_checkuint(L,i) (unsigned) ( luaL_checkinteger(L,i) )
74 #define wslua_checkuint32(L,i) (uint32_t) ( luaL_checknumber(L,i) )
75 #define wslua_checkuint64(L,i) (uint64_t) ( luaL_checknumber(L,i) )
76 
77 #define wslua_optint(L,i,d) (int) ( luaL_optinteger(L,i,d) )
78 #define wslua_optint32(L,i,d) (int32_t) ( luaL_optnumber(L,i,d) )
79 #define wslua_optint64(L,i,d) (int64_t) ( luaL_optnumber(L,i,d) )
80 #define wslua_optuint(L,i,d) (unsigned) ( luaL_optinteger(L,i,d) )
81 #define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optnumber(L,i,d) )
82 #define wslua_optuint64(L,i,d) (uint64_t) ( luaL_optnumber(L,i,d) )
83 
84 
85 struct _wslua_tvb {
86  tvbuff_t* ws_tvb;
87  bool expired;
88  bool need_free;
89 };
90 
91 struct _wslua_pinfo {
92  packet_info* ws_pinfo;
93  bool 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  bool 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  int valuestring_ref;
118  uint64_t mask;
119 } wslua_field_t;
120 
121 typedef struct _wslua_expert_field_t {
122  expert_field ids;
123  const char *abbrev;
124  const char *text;
125  int group;
126  int severity;
128 
133 typedef enum {
134  PREF_UINT,
135  PREF_BOOL,
136  PREF_ENUM,
137  PREF_STRING,
138  PREF_RANGE,
139  PREF_STATIC_TEXT,
140  PREF_OBSOLETE
141 } pref_type_t;
142 
143 typedef struct _wslua_pref_t {
144  char* name;
145  char* label;
146  char* desc;
147  pref_type_t type;
148  union {
149  bool b;
150  unsigned u;
151  char* s;
152  int e;
153  range_t *r;
154  void* p;
155  } value;
156  union {
157  uint32_t max_value;
158  struct {
165  char* default_s;
166  } info;
168  struct _wslua_pref_t* next;
169  struct _wslua_proto_t* proto;
170  int ref; /* Reference to enable Proto to deregister prefs. */
171 } wslua_pref_t;
172 
173 typedef struct _wslua_proto_t {
174  char* name;
175  char* loname;
176  char* desc;
177  int hfid;
178  int ett;
179  wslua_pref_t prefs;
180  int fields;
181  int expert_info_table_ref;
183  module_t *prefs_module;
184  dissector_handle_t handle;
185  GArray *hfa;
186  GArray *etta;
187  GArray *eia;
188  bool is_postdissector;
189  bool expired;
190 } wslua_proto_t;
191 
193  dissector_table_t table;
194  const char* name;
195  const char* ui_name;
196  bool created;
197  bool expired;
198 };
199 
201  column_info* cinfo;
202  int col;
203  bool expired;
204 };
205 
206 struct _wslua_cols {
207  column_info* cinfo;
208  bool expired;
209 };
210 
212  GHashTable *table;
213  bool is_allocated;
214  bool expired;
215 };
216 
218  proto_item* item;
219  proto_tree* tree;
220  bool expired;
221 };
222 
223 // Internal structure for wslua_field.c to track info about registered fields.
225  char *name;
226  header_field_info *hfi;
227 };
228 
230  field_info *ws_fi;
231  bool expired;
232 };
233 
234 typedef void (*tap_extractor_t)(lua_State*,const void*);
235 
236 struct _wslua_tap {
237  char* name;
238  char* filter;
239  tap_extractor_t extractor;
240  lua_State* L;
241  int packet_ref;
242  int draw_ref;
243  int reset_ref;
244  bool all_fields;
245 };
246 
247 /* a "File" object can be different things under the hood. It can either
248  be a FILE_T from wtap struct, which it is during read operations, or it
249  can be a wtap_dumper struct during write operations. A wtap_dumper struct
250  has a FILE_T member, but we can't only store its pointer here because
251  dump operations need the whole thing to write out with. Ugh. */
252 struct _wslua_file {
253  FILE_T file;
254  wtap_dumper *wdh; /* will be NULL during read usage */
255  bool expired;
256 };
257 
258 /* a "CaptureInfo" object can also be different things under the hood. */
260  wtap *wth; /* will be NULL during write usage */
261  wtap_dumper *wdh; /* will be NULL during read usage */
262  bool expired;
263 };
264 
265 struct _wslua_phdr {
266  wtap_rec *rec; /* this also exists in wtap struct, but is different for seek_read ops */
267  Buffer *buf; /* can't use the one in wtap because it's different for seek_read ops */
268  bool expired;
269 };
270 
272  const wtap_rec *rec;
273  const uint8_t *pd;
274  bool expired;
275 };
276 
278  struct file_type_subtype_info finfo;
279  bool is_reader;
280  bool is_writer;
281  char* internal_description; /* XXX - this is redundant; finfo.description should suffice */
282  char* type;
283  char* extensions;
284  lua_State* L;
285  int read_open_ref;
286  int read_ref;
287  int seek_read_ref;
288  int read_close_ref;
289  int seq_read_close_ref;
290  int can_write_encap_ref;
291  int write_open_ref;
292  int write_ref;
293  int write_close_ref;
294  int file_type;
295  bool registered;
296  bool removed; /* This is set during reload Lua plugins */
297 };
298 
299 struct _wslua_dir {
300  GDir* dir;
301  char* ext;
302 };
303 
305  struct progdlg* pw;
306  char* title;
307  char* task;
308  bool stopped;
309 };
310 
311 typedef struct { const char* name; tap_extractor_t extractor; } tappable_t;
312 
313 typedef struct {const char* 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;
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 int64_t Int64;
332 typedef uint64_t 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 char* 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 bool 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 char *fieldname;
409  lua_CFunction getfunc;
410  lua_CFunction setfunc;
412 extern int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, bool 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_INTEGER_GETTER(C,name,member) \
519  WSLUA_ATTRIBUTE_GET(C,name,{lua_pushinteger(L,(lua_Integer)(obj->member));})
520 
521 #define WSLUA_ATTRIBUTE_INTEGER_GETTER(C,member) \
522  WSLUA_ATTRIBUTE_NAMED_INTEGER_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_INTEGER_SETTER(C,name,member,cast) \
578  WSLUA_ATTRIBUTE_SET(C,name, { \
579  if (! lua_isinteger(L,-1) ) \
580  return luaL_error(L, "%s's attribute `%s' must be an integer", #C , #name ); \
581  obj->member = (cast) wslua_toint32(L,-1); \
582  })
583 
584 #define WSLUA_ATTRIBUTE_INTEGER_SETTER(C,member,cast) \
585  WSLUA_ATTRIBUTE_NAMED_INTEGER_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  char* 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  char* 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  char* 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_INTEGER(L,n,v) { lua_pushinteger(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 bool 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 bool 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 bool 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 */
727 typedef struct _wslua_class {
728  const char *name;
729  const luaL_Reg *class_methods;
730  const luaL_Reg *class_meta;
731  const luaL_Reg *instance_methods;
732  const luaL_Reg *instance_meta;
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 bool wslua_toboolean(lua_State* L, int n);
740 extern bool wslua_checkboolean(lua_State* L, int n);
741 extern bool wslua_optbool(lua_State* L, int n, bool 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 char* wslua_typeof_unknown;
748 extern const char* wslua_typeof(lua_State *L, int idx);
749 extern bool wslua_get_table(lua_State *L, int idx, const char *name);
750 extern bool wslua_get_field(lua_State *L, int idx, const char *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 bool 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, int idx, bool asLittleEndian);
768 extern int Int64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
769 extern void UInt64_pack(lua_State* L, luaL_Buffer *b, int idx, bool asLittleEndian);
770 extern int UInt64_unpack(lua_State* L, const char *buff, bool asLittleEndian);
771 extern uint64_t getUInt64(lua_State *L, int i);
772 
773 extern Tvb* push_Tvb(lua_State* L, tvbuff_t* tvb);
774 extern int push_wsluaTvb(lua_State* L, Tvb t);
775 extern bool push_TvbRange(lua_State* L, tvbuff_t* tvb, int offset, int len);
776 extern void clear_outstanding_Tvb(void);
777 extern void clear_outstanding_TvbRange(void);
778 
779 extern Pinfo* push_Pinfo(lua_State* L, packet_info* p);
780 extern void clear_outstanding_Pinfo(void);
781 extern void clear_outstanding_Column(void);
782 extern void clear_outstanding_Columns(void);
783 extern void clear_outstanding_PrivateTable(void);
784 
785 extern int get_hf_wslua_text(void);
786 extern TreeItem push_TreeItem(lua_State *L, proto_tree *tree, proto_item *item);
787 extern void clear_outstanding_TreeItem(void);
788 
789 extern FieldInfo* push_FieldInfo(lua_State *L, field_info* f);
790 extern void clear_outstanding_FieldInfo(void);
791 
792 extern void wslua_print_stack(char* s, lua_State* L);
793 
794 extern void wslua_init(register_cb cb, void *client_data);
795 extern void wslua_early_cleanup(void);
796 extern void wslua_cleanup(void);
797 
798 extern tap_extractor_t wslua_get_tap_extractor(const char* name);
799 extern int wslua_set_tap_enums(lua_State* L);
800 
801 extern ProtoField wslua_is_field_available(lua_State* L, const char* field_abbr);
802 
803 extern char* wslua_get_actual_filename(const char* fname);
804 
805 extern int wslua_bin2hex(lua_State* L, const uint8_t* data, const unsigned len, const bool lowercase, const char* sep);
806 extern int wslua_hex2bin(lua_State* L, const char* data, const unsigned len, const char* sep);
807 extern int luaopen_rex_pcre2(lua_State *L);
808 
809 extern const char* get_current_plugin_version(void);
810 extern void clear_current_plugin_version(void);
811 
812 extern int wslua_deregister_heur_dissectors(lua_State* L);
813 extern int wslua_deregister_protocols(lua_State* L);
814 extern int wslua_deregister_dissector_tables(lua_State* L);
815 extern int wslua_deregister_listeners(lua_State* L);
816 extern int wslua_deregister_fields(lua_State* L);
817 extern int wslua_deregister_filehandlers(lua_State* L);
818 extern void wslua_deregister_menus(void);
819 
820 extern void wslua_init_wtap_filetypes(lua_State* L);
821 
822 #endif
823 
824 /*
825  * Editor modelines - https://www.wireshark.org/tools/modelines.html
826  *
827  * Local variables:
828  * c-basic-offset: 4
829  * tab-width: 8
830  * indent-tabs-mode: nil
831  * End:
832  *
833  * vi: set shiftwidth=4 tabstop=8 expandtab:
834  * :indentSize=4:tabSize=8:noTabs=true:
835  */
#define PREF_UINT
Definition: prefs-int.h:89
Definition: address.h:56
Definition: tap-funnel.c:27
Definition: proto.h:763
Definition: packet_info.h:44
Definition: proto.h:898
Definition: tvbparse.h:143
Definition: tvbparse.h:131
Definition: tvbparse.h:90
Definition: wslua.h:407
Definition: wslua.h:259
Type for defining new classes.
Definition: wslua.h:727
const wslua_attribute_table * attrs
Definition: wslua.h:733
const luaL_Reg * class_meta
Definition: wslua.h:730
const char * name
Definition: wslua.h:728
const luaL_Reg * class_methods
Definition: wslua.h:729
const luaL_Reg * instance_methods
Definition: wslua.h:731
const luaL_Reg * instance_meta
Definition: wslua.h:732
Definition: wslua.h:200
Definition: wslua.h:206
Definition: wslua.h:271
Definition: wslua.h:299
Definition: wslua.h:192
Definition: wslua.h:121
Definition: wslua.h:229
Definition: wslua.h:108
Definition: wslua.h:252
Definition: wslua.h:277
Definition: wslua.h:224
Definition: wslua.h:265
Definition: wslua.h:91
Definition: wslua.h:143
struct _wslua_pref_t::@487::@488 enum_info
bool radio_buttons
Definition: wslua.h:160
char * default_s
Definition: wslua.h:165
uint32_t max_value
Definition: wslua.h:157
const enum_val_t * enumvals
Definition: wslua.h:159
union _wslua_pref_t::@487 info
Definition: wslua.h:211
Definition: wslua.h:304
Definition: wslua.h:173
Definition: wslua.h:236
Definition: wslua.h:217
Definition: wslua.h:85
Definition: wslua.h:96
Definition: wslua.h:102
Definition: buffer.h:22
Definition: packet.c:763
Definition: packet.c:86
Definition: params.h:23
Definition: column-info.h:63
Definition: epan_dissect.h:28
Definition: range.h:42
Definition: expert.h:39
Definition: expert.c:48
Definition: proto.h:810
Definition: wtap.h:1740
Definition: wslua_dumper.c:58
Definition: nstime.h:26
Definition: prefs-int.h:27
Definition: progress_frame.h:31
Definition: wslua.h:311
Definition: tvbuff-int.h:35
Definition: wslua.h:313
Definition: wtap-int.h:96
Definition: file_wrappers.c:168
Definition: wtap.h:1395
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:583
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:133