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