29 #include <wiretap/wtap.h> 31 #include <wsutil/report_message.h> 34 #include <epan/packet.h> 36 #include <epan/to_str.h> 37 #include <epan/prefs.h> 39 #include <epan/epan_dissect.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> 48 #include <epan/wslua/declare_wslua.h> 54 #define WSLUA_INIT_ROUTINES "init_routines" 55 #define WSLUA_PREFS_CHANGED "prefs_changed" 56 #define LOG_DOMAIN_LUA "wslua" 58 typedef void (*wslua_logger_t)(
const gchar *, GLogLevelFlags,
const gchar *, gpointer);
59 extern wslua_logger_t wslua_logger;
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) ) 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) ) 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) ) 179 int expert_info_table_ref;
186 gboolean is_postdissector;
193 const gchar* ui_name;
211 gboolean is_allocated;
226 typedef void (*tap_extractor_t)(lua_State*,
const void*);
231 tap_extractor_t extractor;
281 int seq_read_close_ref;
282 int can_write_encap_ref;
302 typedef struct {
const char* name; tap_extractor_t extractor; }
tappable_t;
313 typedef GByteArray* ByteArray;
322 typedef gint64 Int64;
323 typedef guint64 UInt64;
340 typedef tvbparse_action_t* Shortcut;
341 typedef struct _wslua_main* WireShark;
344 typedef gchar* Struct;
355 #define WSLUA_CLASS_DEFINE(C,check_code) \ 356 WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL) 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; \ 364 C check##C(lua_State* L, int idx) { \ 366 luaL_checktype(L,idx,LUA_TUSERDATA); \ 367 p = (C*)luaL_checkudata(L, idx, #C); \ 369 return p ? *p : retval; \ 371 C* push##C(lua_State* L, C v) { \ 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); \ 378 gboolean is##C(lua_State* L,int i) { \ 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; \ 385 return p ? TRUE : FALSE; \ 387 C shift##C(lua_State* L,int i) { \ 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; \ 394 if (p) { lua_remove(L,i); return *p; }\ 400 const gchar *fieldname;
401 lua_CFunction getfunc;
402 lua_CFunction setfunc;
406 #define WSLUA_TYPEOF_FIELD "__typeof" 411 #define WSLUA_REGISTER_GC(C) \ 412 luaL_getmetatable(L, #C); \ 415 lua_pushcfunction(L, C ## __gc); \ 416 lua_setfield(L, -2, "__gc"); \ 420 #define WSLUA_REGISTER_META(C) { \ 421 const wslua_class C ## _class = { \ 423 .instance_meta = C ## _meta, \ 425 wslua_register_classinstance_meta(L, &C ## _class); \ 426 WSLUA_REGISTER_GC(C); \ 429 #define WSLUA_REGISTER_CLASS(C) { \ 430 const wslua_class C ## _class = { \ 432 .class_methods = C ## _methods, \ 433 .class_meta = C ## _meta, \ 434 .instance_methods = C ## _methods, \ 435 .instance_meta = C ## _meta, \ 437 wslua_register_class(L, &C ## _class); \ 438 WSLUA_REGISTER_GC(C); \ 442 #define WSLUA_REGISTER_ATTRIBUTES(C) { \ 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); \ 449 wslua_reg_attributes(L, C##_attributes, TRUE); \ 450 wslua_reg_attributes(L, C##_attributes, FALSE); \ 454 #define WSLUA_INIT(L) \ 456 wslua_register_classes(L); \ 457 wslua_register_functions(L); 461 #define WSLUA_FUNCTION extern int 463 #define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); } 465 #define WSLUA_REGISTER extern int 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 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 } 479 #define WSLUA_ATTRIBUTES static const wslua_attribute_table 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 } 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) \ 492 luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \ 493 obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \ 497 typedef void __dummy##C##_set_##field 499 #define WSLUA_ATTRIBUTE_GET(C,name,block) \ 500 static int C##_get_##name (lua_State* L) { \ 501 C obj = check##C (L,1); \ 506 typedef void __dummy##C##_get_##name 508 #define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \ 509 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );}) 511 #define WSLUA_ATTRIBUTE_NAMED_NUMBER_GETTER(C,name,member) \ 512 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(obj->member));}) 514 #define WSLUA_ATTRIBUTE_NUMBER_GETTER(C,member) \ 515 WSLUA_ATTRIBUTE_NAMED_NUMBER_GETTER(C,member,member) 517 #define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \ 518 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));}) 520 #define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \ 521 WSLUA_ATTRIBUTE_GET(C,name, { \ 522 lua_pushstring(L,obj->member); \ 525 #define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \ 526 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member) 528 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \ 529 WSLUA_ATTRIBUTE_GET(C,name, { \ 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); \ 542 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \ 543 WSLUA_ATTRIBUTE_GET(C,name, { \ 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); \ 552 #define WSLUA_ATTRIBUTE_SET(C,name,block) \ 553 static int C##_set_##name (lua_State* L) { \ 554 C obj = check##C (L,1); \ 559 typedef void __dummy##C##_set_##name 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); \ 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); \ 577 #define WSLUA_ATTRIBUTE_NUMBER_SETTER(C,member,cast) \ 578 WSLUA_ATTRIBUTE_NAMED_NUMBER_SETTER(C,member,member,cast) 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); \ 584 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \ 585 s = g_strdup(lua_tostring(L,-1)); \ 587 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \ 589 if (obj->member != NULL && need_free) \ 590 g_free((void*) obj->member); \ 595 typedef void __dummy##C##_set_##field 597 #define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \ 598 WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free) 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); \ 604 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \ 605 s = g_strdup(lua_tostring(L,-1)); \ 607 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \ 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)); \ 616 typedef void __dummy##C##_set_##field 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); \ 622 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \ 623 s = g_strdup(lua_tostring(L,-1)); \ 625 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \ 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)); \ 634 typedef void __dummy##C##_set_##field 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); } 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); } 644 #define WSLUA_RETURN(i) return (i); 646 #define WSLUA_API extern 651 #define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s) 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); \ 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); \ 664 if (p->marker != marker_val) \ 665 p->marker = marker_val; \ 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) 682 #define THROW_LUA_ERROR(...) \ 683 THROW_FORMATTED(DissectorError, __VA_ARGS__) 687 #define WRAP_NON_LUA_EXCEPTIONS(code) \ 689 volatile gboolean has_error = FALSE; \ 693 lua_pushstring(L, GET_MESSAGE); \ 696 if (has_error) { lua_error(L); } \ 701 extern TreeItem lua_tree;
704 extern gboolean lua_initialized;
705 extern int lua_dissectors_table_ref;
706 extern int lua_heur_dissectors_table_ref;
708 WSLUA_DECLARE_CLASSES()
709 WSLUA_DECLARE_FUNCTIONS()
711 extern lua_State* wslua_state(
void);
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);
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);
752 extern gboolean wslua_has_field_extractors(
void);
753 extern void lua_prime_all_fields(
proto_tree* tree);
755 extern int Proto_commit(lua_State* L);
759 extern void clear_outstanding_FuncSavers(
void);
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);
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);
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);
778 extern int get_hf_wslua_text(
void);
780 extern void clear_outstanding_TreeItem(
void);
782 extern FieldInfo* push_FieldInfo(lua_State *L,
field_info* f);
783 extern void clear_outstanding_FieldInfo(
void);
785 extern void wslua_print_stack(
char* s, lua_State* L);
787 extern void wslua_init(register_cb cb, gpointer client_data);
788 extern void wslua_cleanup(
void);
790 extern tap_extractor_t wslua_get_tap_extractor(
const gchar* name);
791 extern int wslua_set_tap_enums(lua_State* L);
795 extern char* wslua_get_actual_filename(
const char* fname);
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);
801 extern const gchar* get_current_plugin_version(
void);
802 extern void clear_current_plugin_version(
void);
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);
Definition: tvbparse.h:144
const enum_val_t * enumvals
Definition: wslua.h:158
pref_type_t
Definition: wslua.h:132
const luaL_Reg * instance_meta
Definition: wslua.h:726
Definition: packet_info.h:44
Definition: wtap-int.h:34
Definition: prefs-int.h:27
Definition: tvbparse.h:90
Definition: file-pcapng.c:177
Definition: column-info.h:51
const luaL_Reg * class_methods
Definition: wslua.h:723
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: progress_frame.h:33
const luaL_Reg * class_meta
Definition: wslua.h:724
gchar * default_s
Definition: wslua.h:164
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: sttype-range.c:22
const wslua_attribute_table * attrs
Definition: wslua.h:727
Definition: tap-funnel.c:25
guint32 max_value
Definition: wslua.h:156
struct _wslua_class wslua_class
Type for defining new classes.
Definition: epan_dissect.h:28
#define PREF_UINT
Definition: prefs-int.h:89
const luaL_Reg * instance_methods
Definition: wslua.h:725
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: wtap-int.h:86
const char * name
Definition: wslua.h:722