29#include <ws_log_defs.h>
57#include <epan/wslua/declare_wslua.h>
63#define WSLUA_INIT_ROUTINES "init_routines"
64#define WSLUA_PREFS_CHANGED "prefs_changed"
90#define wslua_toint(L,i) (int) ( lua_tointeger(L,i) )
91#define wslua_toint32(L,i) (int32_t) ( lua_tointeger(L,i) )
92#define wslua_toint64(L,i) (int64_t) ( lua_tonumber(L,i) )
93#define wslua_touint64(L,i) (uint64_t) ( lua_tonumber(L,i) )
95#define wslua_checkint(L,i) (int) ( luaL_checkinteger(L,i) )
96#define wslua_checkint32(L,i) (int32_t) ( luaL_checkinteger(L,i) )
97#define wslua_checkint64(L,i) (int64_t) ( luaL_checknumber(L,i) )
98#define wslua_checkuint64(L,i) (uint64_t) ( luaL_checknumber(L,i) )
100#define wslua_optint(L,i,d) (int) ( luaL_optinteger(L,i,d) )
101#define wslua_optint32(L,i,d) (int32_t) ( luaL_optinteger(L,i,d) )
102#define wslua_optint64(L,i,d) (int64_t) ( luaL_optnumber(L,i,d) )
103#define wslua_optuint64(L,i,d) (uint64_t) ( luaL_optnumber(L,i,d) )
110#if LUA_VERSION_NUM < 503
111#define wslua_touint(L,i) (unsigned) ( lua_tounsigned(L,i) )
112#define wslua_touint32(L,i) (uint32_t) ( lua_tounsigned(L,i) )
113#define wslua_checkuint(L,i) (unsigned) ( luaL_checkunsigned(L,i) )
114#define wslua_checkuint32(L,i) (uint32_t) ( luaL_checkunsigned(L,i) )
115#define wslua_optuint(L,i,d) (unsigned) ( luaL_optunsigned(L,i,d) )
116#define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optunsigned(L,i,d) )
118#define wslua_touint(L,i) (unsigned) ( lua_tointeger(L,i) )
119#define wslua_touint32(L,i) (uint32_t) ( lua_tointeger(L,i) )
120#define wslua_checkuint(L,i) (unsigned) ( luaL_checkinteger(L,i) )
121#define wslua_checkuint32(L,i) (uint32_t) ( luaL_checkinteger(L,i) )
122#define wslua_optuint(L,i,d) (unsigned) ( luaL_optinteger(L,i,d) )
123#define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optinteger(L,i,d) )
226 int expert_info_table_ref;
233 bool is_postdissector;
288typedef void (*tap_extractor_t)(lua_State*,
const void*);
293 tap_extractor_t extractor;
334 char* internal_description;
342 int seq_read_close_ref;
343 int can_write_encap_ref;
364typedef struct {
const char* name; tap_extractor_t extractor; }
tappable_t;
376typedef GByteArray* ByteArray;
385typedef int64_t Int64;
386typedef uint64_t UInt64;
403typedef tvbparse_action_t* Shortcut;
418#define WSLUA_CLASS_DEFINE(C,check_code) \
419 WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL)
421#define WSLUA_CLASS_DEFINE_BASE(C,check_code,retval) \
422C to##C(lua_State* L, int idx) { \
423 C* v = (C*)lua_touserdata (L, idx); \
424 if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)", idx, #C, lua_typename(L, lua_type(L, idx))); \
425 return v ? *v : retval; \
427C check##C(lua_State* L, int idx) { \
429 luaL_checktype(L,idx,LUA_TUSERDATA); \
430 p = (C*)luaL_checkudata(L, idx, #C); \
432 return p ? *p : retval; \
434C* push##C(lua_State* L, C v) { \
436 luaL_checkstack(L,2,"Unable to grow stack\n"); \
437 p = (C*)lua_newuserdata(L,sizeof(C)); *p = v; \
438 luaL_getmetatable(L, #C); lua_setmetatable(L, -2); \
441bool is##C(lua_State* L,int i) { \
443 if(!lua_isuserdata(L,i)) return false; \
444 p = lua_touserdata(L, i); \
445 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
446 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
448 return p ? true : false; \
450C shift##C(lua_State* L,int i) { \
452 if(!lua_isuserdata(L,i)) return retval; \
453 p = (C*)lua_touserdata(L, i); \
454 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
455 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
457 if (p) { lua_remove(L,i); return *p; }\
463 const char *fieldname;
464 lua_CFunction getfunc;
465 lua_CFunction setfunc;
469#define WSLUA_TYPEOF_FIELD "__typeof"
474#define WSLUA_REGISTER_GC(C) \
475 luaL_getmetatable(L, #C); \
478 lua_pushcfunction(L, C ## __gc); \
479 lua_setfield(L, -2, "__gc"); \
483#define __WSLUA_REGISTER_META(C, ATTRS) { \
484 const wslua_class C ## _class = { \
486 .instance_meta = C ## _meta, \
489 wslua_register_classinstance_meta(L, &C ## _class); \
490 WSLUA_REGISTER_GC(C); \
493#define WSLUA_REGISTER_META(C) __WSLUA_REGISTER_META(C, NULL)
494#define WSLUA_REGISTER_META_WITH_ATTRS(C) \
495 __WSLUA_REGISTER_META(C, C ## _attributes)
497#define __WSLUA_REGISTER_CLASS(C, ATTRS) { \
498 const wslua_class C ## _class = { \
500 .class_methods = C ## _methods, \
501 .class_meta = C ## _meta, \
502 .instance_methods = C ## _methods, \
503 .instance_meta = C ## _meta, \
506 wslua_register_class(L, &C ## _class); \
507 WSLUA_REGISTER_GC(C); \
510#define WSLUA_REGISTER_CLASS(C) __WSLUA_REGISTER_CLASS(C, NULL)
511#define WSLUA_REGISTER_CLASS_WITH_ATTRS(C) \
512 __WSLUA_REGISTER_CLASS(C, C ## _attributes)
514#define WSLUA_INIT(L) \
516 wslua_register_classes(L); \
517 wslua_register_functions(L);
521#define WSLUA_FUNCTION extern int
523#define WSLUA_INTERNAL_FUNCTION extern int
525#define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }
527#define WSLUA_REGISTER extern int
529#define WSLUA_METHOD static int
530#define WSLUA_CONSTRUCTOR static int
531#define WSLUA_ATTR_SET static int
532#define WSLUA_ATTR_GET static int
533#define WSLUA_METAMETHOD static int
535#define WSLUA_METHODS static const luaL_Reg
536#define WSLUA_META static const luaL_Reg
537#define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name }
538#define WSLUA_CLASS_FNREG_ALIAS(class,aliasname,name) { #aliasname, class##_##name }
539#define WSLUA_CLASS_MTREG(class,name) { "__" #name, class##__##name }
541#define WSLUA_ATTRIBUTES static const wslua_attribute_table
543#define WSLUA_ATTRIBUTE_RWREG(class,name) { #name, class##_get_##name, class##_set_##name }
544#define WSLUA_ATTRIBUTE_ROREG(class,name) { #name, class##_get_##name, NULL }
545#define WSLUA_ATTRIBUTE_WOREG(class,name) { #name, NULL, class##_set_##name }
547#define WSLUA_ATTRIBUTE_FUNC_SETTER(C,field) \
548 static int C##_set_##field (lua_State* L) { \
549 C obj = check##C (L,1); \
550 if (! lua_isfunction(L,-1) ) \
551 return luaL_error(L, "%s's attribute `%s' must be a function", #C , #field ); \
552 if (obj->field##_ref != LUA_NOREF) \
554 luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \
555 obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
559 typedef void __dummy##C##_set_##field
561#define WSLUA_ATTRIBUTE_GET(C,name,block) \
562 static int C##_get_##name (lua_State* L) { \
563 C obj = check##C (L,1); \
568 typedef void __dummy##C##_get_##name
570#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
571 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
573#define WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,name,member) \
574 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushinteger(L,(lua_Integer)(obj->member));})
576#define WSLUA_ATTRIBUTE_INTEGER_GETTER(C,member) \
577 WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,member,member)
579#define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
580 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
582#define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
583 WSLUA_ATTRIBUTE_GET(C,name, { \
584 lua_pushstring(L,obj->member); \
587#define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
588 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
590#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \
591 WSLUA_ATTRIBUTE_GET(C,name, { \
593 if ((obj->member) && (obj->member->len > 0)) { \
594 if (wtap_block_get_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, &str) == WTAP_OPTTYPE_SUCCESS) { \
595 lua_pushstring(L,str); \
604#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \
605 WSLUA_ATTRIBUTE_GET(C,name, { \
607 if ((obj->member) && (obj->member->len > 0)) { \
608 if (wtap_block_get_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, &str) == WTAP_OPTTYPE_SUCCESS) { \
609 lua_pushstring(L,str); \
614#define WSLUA_ATTRIBUTE_SET(C,name,block) \
615 static int C##_set_##name (lua_State* L) { \
616 C obj = check##C (L,1); \
621 typedef void __dummy##C##_set_##name
623#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_SETTER(C,name,member) \
624 WSLUA_ATTRIBUTE_SET(C,name, { \
625 if (! lua_isboolean(L,-1) ) \
626 return luaL_error(L, "%s's attribute `%s' must be a boolean", #C , #name ); \
627 obj->member = lua_toboolean(L,-1); \
632#define WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,name,member,cast) \
633 WSLUA_ATTRIBUTE_SET(C,name, { \
634 if (! lua_isinteger(L,-1) ) \
635 return luaL_error(L, "%s's attribute `%s' must be an integer", #C , #name ); \
636 obj->member = (cast) wslua_toint32(L,-1); \
639#define WSLUA_ATTRIBUTE_INTEGER_SETTER(C,member,cast) \
640 WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,member,member,cast)
642#define WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,member,need_free) \
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 != NULL && need_free) \
652 g_free((void*) obj->member); \
657 typedef void __dummy##C##_set_##field
659#define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \
660 WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free)
662#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_SETTER(C,field,member,option) \
663 static int C##_set_##field (lua_State* L) { \
664 C obj = check##C (L,1); \
666 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
667 s = g_strdup(lua_tostring(L,-1)); \
669 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
671 if ((obj->member) && (obj->member->len > 0)) { \
672 wtap_block_set_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, s, strlen(s)); \
678 typedef void __dummy##C##_set_##field
680#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_SETTER(C,field,member,option) \
681 static int C##_set_##field (lua_State* L) { \
682 C obj = check##C (L,1); \
684 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
685 s = g_strdup(lua_tostring(L,-1)); \
687 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
689 if ((obj->member) && (obj->member->len > 0)) { \
690 wtap_block_set_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, s, strlen(s)); \
696 typedef void __dummy##C##_set_##field
698#define WSLUA_ERROR(name,error) { luaL_error(L, "%s%s", #name ": ", error); }
699#define WSLUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_ARG_ ## name ## _ ## attr, #name ": " error); }
700#define WSLUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_OPTARG_##name##_ ##attr, #name ": " error); }
702#define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushboolean(L,v); lua_setglobal(L,n); }
703#define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,v); lua_setglobal(L,n); }
704#define WSLUA_REG_GLOBAL_INTEGER(L,n,v) { lua_pushinteger(L,v); lua_setglobal(L,n); }
706#define WSLUA_RETURN(i) return (i)
708#define WSLUA_API extern
713#define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
715#define FAIL_ON_NULL_OR_EXPIRED(s) if (!*p) { \
716 luaL_argerror(L,idx,"null " s); \
717 } else if ((*p)->expired) { \
718 luaL_argerror(L,idx,"expired " s); \
722#define CLEAR_OUTSTANDING(C, marker, marker_val) void clear_outstanding_##C(void) { \
723 while (outstanding_##C->len) { \
724 C p = (C)g_ptr_array_remove_index_fast(outstanding_##C,0); \
726 if (p->marker != marker_val) \
727 p->marker = marker_val; \
734#define WSLUA_CLASS_DECLARE(C) \
735extern C to##C(lua_State* L, int idx); \
736extern C check##C(lua_State* L, int idx); \
737extern C* push##C(lua_State* L, C v); \
738extern int C##_register(lua_State* L); \
739extern bool is##C(lua_State* L,int i); \
740extern C shift##C(lua_State* L,int i)
744#define THROW_LUA_ERROR(...) \
745 THROW_FORMATTED(DissectorError, __VA_ARGS__)
766#define WRAP_NON_LUA_EXCEPTIONS(code) \
768 volatile bool has_error = false; \
771 } CATCH3(BoundsError, FragmentBoundsError, ScsiBoundsError) { \
772 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
774 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
775 lua_pushfstring(L, "%s: %s", __func__, GET_MESSAGE ? GET_MESSAGE : "Malformed packet"); \
778 if (has_error) { lua_error(L); } \
785extern bool lua_initialized;
786extern int lua_dissectors_table_ref;
787extern int lua_heur_dissectors_table_ref;
789WSLUA_DECLARE_CLASSES()
790WSLUA_DECLARE_FUNCTIONS()
792extern lua_State* wslua_state(
void);
813extern int wslua__concat(lua_State* L);
814extern bool wslua_toboolean(lua_State* L,
int n);
815extern bool wslua_checkboolean(lua_State* L,
int n);
816extern bool wslua_optbool(lua_State* L,
int n,
bool def);
817extern lua_Integer wslua_tointeger(lua_State* L,
int n);
818extern int wslua_optboolint(lua_State* L,
int n,
int def);
819extern const char* wslua_checklstring_only(lua_State* L,
int n,
size_t *l);
820extern const char* wslua_checkstring_only(lua_State* L,
int n);
821extern void wslua_setfuncs(lua_State *L,
const luaL_Reg *l,
int nup);
822extern const char* wslua_typeof_unknown;
823extern const char* wslua_typeof(lua_State *L,
int idx);
824extern bool wslua_get_table(lua_State *L,
int idx,
const char *name);
825extern bool wslua_get_field(lua_State *L,
int idx,
const char *name);
828extern expert_field* wslua_get_expert_field(
const int group,
const int severity);
829extern void wslua_prefs_changed(
void);
830extern void proto_register_lua(
void);
831extern GString* lua_register_all_taps(
void);
833extern bool wslua_has_field_extractors(
void);
834extern void lua_prime_all_fields(
proto_tree* tree);
836extern int Proto_commit(lua_State* L);
840extern void clear_outstanding_FuncSavers(
void);
842extern void Int64_pack(lua_State* L, luaL_Buffer *b,
int idx,
bool asLittleEndian);
843extern int Int64_unpack(lua_State* L,
const char *buff,
bool asLittleEndian);
844extern void UInt64_pack(lua_State* L, luaL_Buffer *b,
int idx,
bool asLittleEndian);
845extern int UInt64_unpack(lua_State* L,
const char *buff,
bool asLittleEndian);
846extern uint64_t getUInt64(lua_State *L,
int i);
849extern int push_wsluaTvb(lua_State* L,
Tvb t);
850extern bool push_TvbRange(lua_State* L,
tvbuff_t* tvb,
int offset,
int len);
851extern void clear_outstanding_Tvb(
void);
852extern void clear_outstanding_TvbRange(
void);
855extern void clear_outstanding_Pinfo(
void);
856extern void clear_outstanding_Column(
void);
857extern void clear_outstanding_Columns(
void);
858extern void clear_outstanding_PrivateTable(
void);
860extern int get_hf_wslua_text(
void);
862extern void clear_outstanding_TreeItem(
void);
865extern void clear_outstanding_FieldInfo(
void);
867extern void wslua_print_stack(
char* s, lua_State* L);
869extern void wslua_init(register_cb cb,
void *client_data);
870extern void wslua_early_cleanup(
void);
871extern void wslua_cleanup(
void);
873extern tap_extractor_t wslua_get_tap_extractor(
const char* name);
874extern int wslua_set_tap_enums(lua_State* L);
878extern char* wslua_get_actual_filename(
const char* fname);
880extern int wslua_bin2hex(lua_State* L,
const uint8_t* data,
const unsigned len,
const bool lowercase,
const char* sep);
881extern int wslua_hex2bin(lua_State* L,
const char* data,
const unsigned len,
const char* sep);
882extern int luaopen_rex_pcre2(lua_State *L);
884extern const char* get_current_plugin_version(
void);
885extern void clear_current_plugin_version(
void);
887extern int wslua_deregister_heur_dissectors(lua_State* L);
888extern int wslua_deregister_protocols(lua_State* L);
889extern int wslua_deregister_dissector_tables(lua_State* L);
890extern int wslua_deregister_listeners(lua_State* L);
891extern int wslua_deregister_fields(lua_State* L);
892extern int wslua_deregister_filehandlers(lua_State* L);
893extern void wslua_deregister_menus(
void);
895extern void wslua_init_wtap_filetypes(lua_State* L);
#define PREF_UINT
Definition prefs-int.h:90
Definition tap-funnel.c:27
Definition packet_info.h:43
Definition tvbparse.h:142
Definition tvbparse.h:130
Type for defining new classes.
Definition wslua.h:802
const wslua_attribute_table * attrs
Definition wslua.h:808
const luaL_Reg * class_meta
Definition wslua.h:805
const char * name
Definition wslua.h:803
const luaL_Reg * class_methods
Definition wslua.h:804
const luaL_Reg * instance_methods
Definition wslua.h:806
const luaL_Reg * instance_meta
Definition wslua.h:807
uat_field_t * uat_field_list
Definition wslua.h:208
bool radio_buttons
Definition wslua.h:202
struct _wslua_pref_t::@500::@501 enum_info
char * default_s
Definition wslua.h:210
struct _wslua_pref_t::@500::@502 uat_field_list_info
uint32_t max_value
Definition wslua.h:199
union _wslua_pref_t::@500 info
const enum_val_t * enumvals
Definition wslua.h:201
Definition conversation.h:228
Definition column-info.h:62
Definition epan_dissect.h:28
Definition prefs-int.h:27
Definition progress_frame.h:31
Definition tvbuff-int.h:35
Definition file_wrappers.c:215
void wslua_register_class(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:547
ProtoField wslua_is_field_available(lua_State *L, const char *field_abbr)
Definition wslua_proto.c:714
void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:470
struct _wslua_class wslua_class
Type for defining new classes.
pref_type_t
Definition wslua.h:174