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