28 #include <ws_log_defs.h>
51 #include <epan/wslua/declare_wslua.h>
57 #define WSLUA_INIT_ROUTINES "init_routines"
58 #define WSLUA_PREFS_CHANGED "prefs_changed"
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) )
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) )
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) )
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) )
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) )
216 int expert_info_table_ref;
223 bool is_postdissector;
269 typedef void (*tap_extractor_t)(lua_State*,
const void*);
274 tap_extractor_t extractor;
316 char* internal_description;
324 int seq_read_close_ref;
325 int can_write_encap_ref;
346 typedef struct {
const char* name; tap_extractor_t extractor; }
tappable_t;
357 typedef GByteArray* ByteArray;
366 typedef int64_t Int64;
367 typedef uint64_t UInt64;
384 typedef tvbparse_action_t* Shortcut;
387 typedef char* Struct;
398 #define WSLUA_CLASS_DEFINE(C,check_code) \
399 WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL)
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; \
407 C check##C(lua_State* L, int idx) { \
409 luaL_checktype(L,idx,LUA_TUSERDATA); \
410 p = (C*)luaL_checkudata(L, idx, #C); \
412 return p ? *p : retval; \
414 C* push##C(lua_State* L, C v) { \
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); \
421 bool is##C(lua_State* L,int i) { \
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; \
428 return p ? true : false; \
430 C shift##C(lua_State* L,int i) { \
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; \
437 if (p) { lua_remove(L,i); return *p; }\
443 const char *fieldname;
444 lua_CFunction getfunc;
445 lua_CFunction setfunc;
449 #define WSLUA_TYPEOF_FIELD "__typeof"
454 #define WSLUA_REGISTER_GC(C) \
455 luaL_getmetatable(L, #C); \
458 lua_pushcfunction(L, C ## __gc); \
459 lua_setfield(L, -2, "__gc"); \
463 #define __WSLUA_REGISTER_META(C, ATTRS) { \
464 const wslua_class C ## _class = { \
466 .instance_meta = C ## _meta, \
469 wslua_register_classinstance_meta(L, &C ## _class); \
470 WSLUA_REGISTER_GC(C); \
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)
477 #define __WSLUA_REGISTER_CLASS(C, ATTRS) { \
478 const wslua_class C ## _class = { \
480 .class_methods = C ## _methods, \
481 .class_meta = C ## _meta, \
482 .instance_methods = C ## _methods, \
483 .instance_meta = C ## _meta, \
486 wslua_register_class(L, &C ## _class); \
487 WSLUA_REGISTER_GC(C); \
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)
494 #define WSLUA_INIT(L) \
496 wslua_register_classes(L); \
497 wslua_register_functions(L);
501 #define WSLUA_FUNCTION extern int
503 #define WSLUA_INTERNAL_FUNCTION extern int
505 #define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }
507 #define WSLUA_REGISTER extern int
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
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 }
521 #define WSLUA_ATTRIBUTES static const wslua_attribute_table
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 }
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) \
534 luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \
535 obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
539 typedef void __dummy##C##_set_##field
541 #define WSLUA_ATTRIBUTE_GET(C,name,block) \
542 static int C##_get_##name (lua_State* L) { \
543 C obj = check##C (L,1); \
548 typedef void __dummy##C##_get_##name
550 #define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
551 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
553 #define WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,name,member) \
554 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushinteger(L,(lua_Integer)(obj->member));})
556 #define WSLUA_ATTRIBUTE_INTEGER_GETTER(C,member) \
557 WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,member,member)
559 #define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
560 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
562 #define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
563 WSLUA_ATTRIBUTE_GET(C,name, { \
564 lua_pushstring(L,obj->member); \
567 #define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
568 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
570 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \
571 WSLUA_ATTRIBUTE_GET(C,name, { \
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); \
584 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \
585 WSLUA_ATTRIBUTE_GET(C,name, { \
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); \
594 #define WSLUA_ATTRIBUTE_SET(C,name,block) \
595 static int C##_set_##name (lua_State* L) { \
596 C obj = check##C (L,1); \
601 typedef void __dummy##C##_set_##name
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); \
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); \
619 #define WSLUA_ATTRIBUTE_INTEGER_SETTER(C,member,cast) \
620 WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,member,member,cast)
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); \
626 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
627 s = g_strdup(lua_tostring(L,-1)); \
629 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
631 if (obj->member != NULL && need_free) \
632 g_free((void*) obj->member); \
637 typedef void __dummy##C##_set_##field
639 #define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \
640 WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free)
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); \
646 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
647 s = g_strdup(lua_tostring(L,-1)); \
649 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
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)); \
658 typedef void __dummy##C##_set_##field
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); \
664 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
665 s = g_strdup(lua_tostring(L,-1)); \
667 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
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)); \
676 typedef void __dummy##C##_set_##field
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); }
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); }
686 #define WSLUA_RETURN(i) return (i)
688 #define WSLUA_API extern
693 #define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
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); \
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); \
706 if (p->marker != marker_val) \
707 p->marker = marker_val; \
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)
724 #define THROW_LUA_ERROR(...) \
725 THROW_FORMATTED(DissectorError, __VA_ARGS__)
729 #define WRAP_NON_LUA_EXCEPTIONS(code) \
731 volatile bool has_error = false; \
735 lua_pushstring(L, GET_MESSAGE); \
738 if (has_error) { lua_error(L); } \
745 extern bool lua_initialized;
746 extern int lua_dissectors_table_ref;
747 extern int lua_heur_dissectors_table_ref;
749 WSLUA_DECLARE_CLASSES()
750 WSLUA_DECLARE_FUNCTIONS()
752 extern lua_State* wslua_state(
void);
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);
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);
793 extern bool wslua_has_field_extractors(
void);
794 extern void lua_prime_all_fields(
proto_tree* tree);
796 extern int Proto_commit(lua_State* L);
800 extern void clear_outstanding_FuncSavers(
void);
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);
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);
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);
820 extern int get_hf_wslua_text(
void);
822 extern void clear_outstanding_TreeItem(
void);
825 extern void clear_outstanding_FieldInfo(
void);
827 extern void wslua_print_stack(
char* s, lua_State* L);
829 extern void wslua_init(register_cb cb,
void *client_data);
830 extern void wslua_early_cleanup(
void);
831 extern void wslua_cleanup(
void);
833 extern tap_extractor_t wslua_get_tap_extractor(
const char* name);
834 extern int wslua_set_tap_enums(lua_State* L);
838 extern char* wslua_get_actual_filename(
const char* fname);
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);
844 extern const char* get_current_plugin_version(
void);
845 extern void clear_current_plugin_version(
void);
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);
855 extern void wslua_init_wtap_filetypes(lua_State* L);
#define PREF_UINT
Definition: prefs-int.h:89
Definition: tap-funnel.c:27
Definition: packet_info.h:44
Definition: tvbparse.h:143
Definition: tvbparse.h:131
Definition: tvbparse.h:90
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
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: column-info.h:63
Definition: epan_dissect.h:28
Definition: prefs-int.h:27
Definition: progress_frame.h:31
Definition: tvbuff-int.h:35
Definition: wtap-int.h:96
Definition: file_wrappers.c:168
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