28#include <ws_log_defs.h>
53#include <epan/wslua/declare_wslua.h>
59#define WSLUA_INIT_ROUTINES "init_routines"
60#define WSLUA_PREFS_CHANGED "prefs_changed"
86#define wslua_toint(L,i) (int) ( lua_tointeger(L,i) )
87#define wslua_toint32(L,i) (int32_t) ( lua_tointeger(L,i) )
88#define wslua_toint64(L,i) (int64_t) ( lua_tonumber(L,i) )
89#define wslua_touint64(L,i) (uint64_t) ( lua_tonumber(L,i) )
91#define wslua_checkint(L,i) (int) ( luaL_checkinteger(L,i) )
92#define wslua_checkint32(L,i) (int32_t) ( luaL_checkinteger(L,i) )
93#define wslua_checkint64(L,i) (int64_t) ( luaL_checknumber(L,i) )
94#define wslua_checkuint64(L,i) (uint64_t) ( luaL_checknumber(L,i) )
96#define wslua_optint(L,i,d) (int) ( luaL_optinteger(L,i,d) )
97#define wslua_optint32(L,i,d) (int32_t) ( luaL_optinteger(L,i,d) )
98#define wslua_optint64(L,i,d) (int64_t) ( luaL_optnumber(L,i,d) )
99#define wslua_optuint64(L,i,d) (uint64_t) ( luaL_optnumber(L,i,d) )
106#if LUA_VERSION_NUM < 503
107#define wslua_touint(L,i) (unsigned) ( lua_tounsigned(L,i) )
108#define wslua_touint32(L,i) (uint32_t) ( lua_tounsigned(L,i) )
109#define wslua_checkuint(L,i) (unsigned) ( luaL_checkunsigned(L,i) )
110#define wslua_checkuint32(L,i) (uint32_t) ( luaL_checkunsigned(L,i) )
111#define wslua_optuint(L,i,d) (unsigned) ( luaL_optunsigned(L,i,d) )
112#define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optunsigned(L,i,d) )
114#define wslua_touint(L,i) (unsigned) ( lua_tointeger(L,i) )
115#define wslua_touint32(L,i) (uint32_t) ( lua_tointeger(L,i) )
116#define wslua_checkuint(L,i) (unsigned) ( luaL_checkinteger(L,i) )
117#define wslua_checkuint32(L,i) (uint32_t) ( luaL_checkinteger(L,i) )
118#define wslua_optuint(L,i,d) (unsigned) ( luaL_optinteger(L,i,d) )
119#define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optinteger(L,i,d) )
218 int expert_info_table_ref;
225 bool is_postdissector;
275typedef void (*tap_extractor_t)(lua_State*,
const void*);
280 tap_extractor_t extractor;
321 char* internal_description;
329 int seq_read_close_ref;
330 int can_write_encap_ref;
351typedef struct {
const char* name; tap_extractor_t extractor; }
tappable_t;
362typedef GByteArray* ByteArray;
371typedef int64_t Int64;
372typedef uint64_t UInt64;
389typedef tvbparse_action_t* Shortcut;
403#define WSLUA_CLASS_DEFINE(C,check_code) \
404 WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL)
406#define WSLUA_CLASS_DEFINE_BASE(C,check_code,retval) \
407C to##C(lua_State* L, int idx) { \
408 C* v = (C*)lua_touserdata (L, idx); \
409 if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)", idx, #C, lua_typename(L, lua_type(L, idx))); \
410 return v ? *v : retval; \
412C check##C(lua_State* L, int idx) { \
414 luaL_checktype(L,idx,LUA_TUSERDATA); \
415 p = (C*)luaL_checkudata(L, idx, #C); \
417 return p ? *p : retval; \
419C* push##C(lua_State* L, C v) { \
421 luaL_checkstack(L,2,"Unable to grow stack\n"); \
422 p = (C*)lua_newuserdata(L,sizeof(C)); *p = v; \
423 luaL_getmetatable(L, #C); lua_setmetatable(L, -2); \
426bool is##C(lua_State* L,int i) { \
428 if(!lua_isuserdata(L,i)) return false; \
429 p = lua_touserdata(L, i); \
430 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
431 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
433 return p ? true : false; \
435C shift##C(lua_State* L,int i) { \
437 if(!lua_isuserdata(L,i)) return retval; \
438 p = (C*)lua_touserdata(L, i); \
439 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
440 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
442 if (p) { lua_remove(L,i); return *p; }\
448 const char *fieldname;
449 lua_CFunction getfunc;
450 lua_CFunction setfunc;
454#define WSLUA_TYPEOF_FIELD "__typeof"
459#define WSLUA_REGISTER_GC(C) \
460 luaL_getmetatable(L, #C); \
463 lua_pushcfunction(L, C ## __gc); \
464 lua_setfield(L, -2, "__gc"); \
468#define __WSLUA_REGISTER_META(C, ATTRS) { \
469 const wslua_class C ## _class = { \
471 .instance_meta = C ## _meta, \
474 wslua_register_classinstance_meta(L, &C ## _class); \
475 WSLUA_REGISTER_GC(C); \
478#define WSLUA_REGISTER_META(C) __WSLUA_REGISTER_META(C, NULL)
479#define WSLUA_REGISTER_META_WITH_ATTRS(C) \
480 __WSLUA_REGISTER_META(C, C ## _attributes)
482#define __WSLUA_REGISTER_CLASS(C, ATTRS) { \
483 const wslua_class C ## _class = { \
485 .class_methods = C ## _methods, \
486 .class_meta = C ## _meta, \
487 .instance_methods = C ## _methods, \
488 .instance_meta = C ## _meta, \
491 wslua_register_class(L, &C ## _class); \
492 WSLUA_REGISTER_GC(C); \
495#define WSLUA_REGISTER_CLASS(C) __WSLUA_REGISTER_CLASS(C, NULL)
496#define WSLUA_REGISTER_CLASS_WITH_ATTRS(C) \
497 __WSLUA_REGISTER_CLASS(C, C ## _attributes)
499#define WSLUA_INIT(L) \
501 wslua_register_classes(L); \
502 wslua_register_functions(L);
506#define WSLUA_FUNCTION extern int
508#define WSLUA_INTERNAL_FUNCTION extern int
510#define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }
512#define WSLUA_REGISTER extern int
514#define WSLUA_METHOD static int
515#define WSLUA_CONSTRUCTOR static int
516#define WSLUA_ATTR_SET static int
517#define WSLUA_ATTR_GET static int
518#define WSLUA_METAMETHOD static int
520#define WSLUA_METHODS static const luaL_Reg
521#define WSLUA_META static const luaL_Reg
522#define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name }
523#define WSLUA_CLASS_FNREG_ALIAS(class,aliasname,name) { #aliasname, class##_##name }
524#define WSLUA_CLASS_MTREG(class,name) { "__" #name, class##__##name }
526#define WSLUA_ATTRIBUTES static const wslua_attribute_table
528#define WSLUA_ATTRIBUTE_RWREG(class,name) { #name, class##_get_##name, class##_set_##name }
529#define WSLUA_ATTRIBUTE_ROREG(class,name) { #name, class##_get_##name, NULL }
530#define WSLUA_ATTRIBUTE_WOREG(class,name) { #name, NULL, class##_set_##name }
532#define WSLUA_ATTRIBUTE_FUNC_SETTER(C,field) \
533 static int C##_set_##field (lua_State* L) { \
534 C obj = check##C (L,1); \
535 if (! lua_isfunction(L,-1) ) \
536 return luaL_error(L, "%s's attribute `%s' must be a function", #C , #field ); \
537 if (obj->field##_ref != LUA_NOREF) \
539 luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \
540 obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
544 typedef void __dummy##C##_set_##field
546#define WSLUA_ATTRIBUTE_GET(C,name,block) \
547 static int C##_get_##name (lua_State* L) { \
548 C obj = check##C (L,1); \
553 typedef void __dummy##C##_get_##name
555#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
556 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
558#define WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,name,member) \
559 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushinteger(L,(lua_Integer)(obj->member));})
561#define WSLUA_ATTRIBUTE_INTEGER_GETTER(C,member) \
562 WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,member,member)
564#define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
565 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
567#define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
568 WSLUA_ATTRIBUTE_GET(C,name, { \
569 lua_pushstring(L,obj->member); \
572#define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
573 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
575#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \
576 WSLUA_ATTRIBUTE_GET(C,name, { \
578 if ((obj->member) && (obj->member->len > 0)) { \
579 if (wtap_block_get_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, &str) == WTAP_OPTTYPE_SUCCESS) { \
580 lua_pushstring(L,str); \
589#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \
590 WSLUA_ATTRIBUTE_GET(C,name, { \
592 if ((obj->member) && (obj->member->len > 0)) { \
593 if (wtap_block_get_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, &str) == WTAP_OPTTYPE_SUCCESS) { \
594 lua_pushstring(L,str); \
599#define WSLUA_ATTRIBUTE_SET(C,name,block) \
600 static int C##_set_##name (lua_State* L) { \
601 C obj = check##C (L,1); \
606 typedef void __dummy##C##_set_##name
608#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_SETTER(C,name,member) \
609 WSLUA_ATTRIBUTE_SET(C,name, { \
610 if (! lua_isboolean(L,-1) ) \
611 return luaL_error(L, "%s's attribute `%s' must be a boolean", #C , #name ); \
612 obj->member = lua_toboolean(L,-1); \
617#define WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,name,member,cast) \
618 WSLUA_ATTRIBUTE_SET(C,name, { \
619 if (! lua_isinteger(L,-1) ) \
620 return luaL_error(L, "%s's attribute `%s' must be an integer", #C , #name ); \
621 obj->member = (cast) wslua_toint32(L,-1); \
624#define WSLUA_ATTRIBUTE_INTEGER_SETTER(C,member,cast) \
625 WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,member,member,cast)
627#define WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,member,need_free) \
628 static int C##_set_##field (lua_State* L) { \
629 C obj = check##C (L,1); \
631 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
632 s = g_strdup(lua_tostring(L,-1)); \
634 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
636 if (obj->member != NULL && need_free) \
637 g_free((void*) obj->member); \
642 typedef void __dummy##C##_set_##field
644#define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \
645 WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free)
647#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_SETTER(C,field,member,option) \
648 static int C##_set_##field (lua_State* L) { \
649 C obj = check##C (L,1); \
651 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
652 s = g_strdup(lua_tostring(L,-1)); \
654 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
656 if ((obj->member) && (obj->member->len > 0)) { \
657 wtap_block_set_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, s, strlen(s)); \
663 typedef void __dummy##C##_set_##field
665#define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_SETTER(C,field,member,option) \
666 static int C##_set_##field (lua_State* L) { \
667 C obj = check##C (L,1); \
669 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
670 s = g_strdup(lua_tostring(L,-1)); \
672 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
674 if ((obj->member) && (obj->member->len > 0)) { \
675 wtap_block_set_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, s, strlen(s)); \
681 typedef void __dummy##C##_set_##field
683#define WSLUA_ERROR(name,error) { luaL_error(L, "%s%s", #name ": ", error); }
684#define WSLUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_ARG_ ## name ## _ ## attr, #name ": " error); }
685#define WSLUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_OPTARG_##name##_ ##attr, #name ": " error); }
687#define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushboolean(L,v); lua_setglobal(L,n); }
688#define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,v); lua_setglobal(L,n); }
689#define WSLUA_REG_GLOBAL_INTEGER(L,n,v) { lua_pushinteger(L,v); lua_setglobal(L,n); }
691#define WSLUA_RETURN(i) return (i)
693#define WSLUA_API extern
698#define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
700#define FAIL_ON_NULL_OR_EXPIRED(s) if (!*p) { \
701 luaL_argerror(L,idx,"null " s); \
702 } else if ((*p)->expired) { \
703 luaL_argerror(L,idx,"expired " s); \
707#define CLEAR_OUTSTANDING(C, marker, marker_val) void clear_outstanding_##C(void) { \
708 while (outstanding_##C->len) { \
709 C p = (C)g_ptr_array_remove_index_fast(outstanding_##C,0); \
711 if (p->marker != marker_val) \
712 p->marker = marker_val; \
719#define WSLUA_CLASS_DECLARE(C) \
720extern C to##C(lua_State* L, int idx); \
721extern C check##C(lua_State* L, int idx); \
722extern C* push##C(lua_State* L, C v); \
723extern int C##_register(lua_State* L); \
724extern bool is##C(lua_State* L,int i); \
725extern C shift##C(lua_State* L,int i)
729#define THROW_LUA_ERROR(...) \
730 THROW_FORMATTED(DissectorError, __VA_ARGS__)
751#define WRAP_NON_LUA_EXCEPTIONS(code) \
753 volatile bool has_error = false; \
756 } CATCH3(BoundsError, FragmentBoundsError, ScsiBoundsError) { \
757 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
759 show_exception(lua_tvb, lua_pinfo, lua_tree->tree, EXCEPT_CODE, GET_MESSAGE); \
760 lua_pushfstring(L, "%s: %s", __func__, GET_MESSAGE ? GET_MESSAGE : "Malformed packet"); \
763 if (has_error) { lua_error(L); } \
770extern bool lua_initialized;
771extern int lua_dissectors_table_ref;
772extern int lua_heur_dissectors_table_ref;
774WSLUA_DECLARE_CLASSES()
775WSLUA_DECLARE_FUNCTIONS()
777extern lua_State* wslua_state(
void);
798extern int wslua__concat(lua_State* L);
799extern bool wslua_toboolean(lua_State* L,
int n);
800extern bool wslua_checkboolean(lua_State* L,
int n);
801extern bool wslua_optbool(lua_State* L,
int n,
bool def);
802extern lua_Integer wslua_tointeger(lua_State* L,
int n);
803extern int wslua_optboolint(lua_State* L,
int n,
int def);
804extern const char* wslua_checklstring_only(lua_State* L,
int n,
size_t *l);
805extern const char* wslua_checkstring_only(lua_State* L,
int n);
806extern void wslua_setfuncs(lua_State *L,
const luaL_Reg *l,
int nup);
807extern const char* wslua_typeof_unknown;
808extern const char* wslua_typeof(lua_State *L,
int idx);
809extern bool wslua_get_table(lua_State *L,
int idx,
const char *name);
810extern bool wslua_get_field(lua_State *L,
int idx,
const char *name);
813extern expert_field* wslua_get_expert_field(
const int group,
const int severity);
814extern void wslua_prefs_changed(
void);
815extern void proto_register_lua(
void);
816extern GString* lua_register_all_taps(
void);
818extern bool wslua_has_field_extractors(
void);
819extern void lua_prime_all_fields(
proto_tree* tree);
821extern int Proto_commit(lua_State* L);
825extern void clear_outstanding_FuncSavers(
void);
827extern void Int64_pack(lua_State* L, luaL_Buffer *b,
int idx,
bool asLittleEndian);
828extern int Int64_unpack(lua_State* L,
const char *buff,
bool asLittleEndian);
829extern void UInt64_pack(lua_State* L, luaL_Buffer *b,
int idx,
bool asLittleEndian);
830extern int UInt64_unpack(lua_State* L,
const char *buff,
bool asLittleEndian);
831extern uint64_t getUInt64(lua_State *L,
int i);
834extern int push_wsluaTvb(lua_State* L,
Tvb t);
835extern bool push_TvbRange(lua_State* L,
tvbuff_t* tvb,
int offset,
int len);
836extern void clear_outstanding_Tvb(
void);
837extern void clear_outstanding_TvbRange(
void);
840extern void clear_outstanding_Pinfo(
void);
841extern void clear_outstanding_Column(
void);
842extern void clear_outstanding_Columns(
void);
843extern void clear_outstanding_PrivateTable(
void);
845extern int get_hf_wslua_text(
void);
847extern void clear_outstanding_TreeItem(
void);
850extern void clear_outstanding_FieldInfo(
void);
852extern void wslua_print_stack(
char* s, lua_State* L);
854extern void wslua_init(register_cb cb,
void *client_data);
855extern void wslua_early_cleanup(
void);
856extern void wslua_cleanup(
void);
858extern tap_extractor_t wslua_get_tap_extractor(
const char* name);
859extern int wslua_set_tap_enums(lua_State* L);
863extern char* wslua_get_actual_filename(
const char* fname);
865extern int wslua_bin2hex(lua_State* L,
const uint8_t* data,
const unsigned len,
const bool lowercase,
const char* sep);
866extern int wslua_hex2bin(lua_State* L,
const char* data,
const unsigned len,
const char* sep);
867extern int luaopen_rex_pcre2(lua_State *L);
869extern const char* get_current_plugin_version(
void);
870extern void clear_current_plugin_version(
void);
872extern int wslua_deregister_heur_dissectors(lua_State* L);
873extern int wslua_deregister_protocols(lua_State* L);
874extern int wslua_deregister_dissector_tables(lua_State* L);
875extern int wslua_deregister_listeners(lua_State* L);
876extern int wslua_deregister_fields(lua_State* L);
877extern int wslua_deregister_filehandlers(lua_State* L);
878extern void wslua_deregister_menus(
void);
880extern 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:787
const wslua_attribute_table * attrs
Definition wslua.h:793
const luaL_Reg * class_meta
Definition wslua.h:790
const char * name
Definition wslua.h:788
const luaL_Reg * class_methods
Definition wslua.h:789
const luaL_Reg * instance_methods
Definition wslua.h:791
const luaL_Reg * instance_meta
Definition wslua.h:792
struct _wslua_pref_t::@495::@496 enum_info
bool radio_buttons
Definition wslua.h:197
char * default_s
Definition wslua.h:202
uint32_t max_value
Definition wslua.h:194
const enum_val_t * enumvals
Definition wslua.h:196
union _wslua_pref_t::@495 info
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:230
void wslua_register_class(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:530
ProtoField wslua_is_field_available(lua_State *L, const char *field_abbr)
Definition wslua_proto.c:715
void wslua_register_classinstance_meta(lua_State *L, const wslua_class *cls_def)
Definition wslua_internals.c:453
struct _wslua_class wslua_class
Type for defining new classes.
pref_type_t
Definition wslua.h:170