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