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"
63 #define wslua_toint(L,i) (int) ( lua_tointeger(L,i) )
64 #define wslua_toint32(L,i) (int32_t) ( lua_tonumber(L,i) )
65 #define wslua_toint64(L,i) (int64_t) ( lua_tonumber(L,i) )
66 #define wslua_touint(L,i) (unsigned) ( lua_tointeger(L,i) )
67 #define wslua_touint32(L,i) (uint32_t) ( lua_tonumber(L,i) )
68 #define wslua_touint64(L,i) (uint64_t) ( lua_tonumber(L,i) )
70 #define wslua_checkint(L,i) (int) ( luaL_checkinteger(L,i) )
71 #define wslua_checkint32(L,i) (int32_t) ( luaL_checknumber(L,i) )
72 #define wslua_checkint64(L,i) (int64_t) ( luaL_checknumber(L,i) )
73 #define wslua_checkuint(L,i) (unsigned) ( luaL_checkinteger(L,i) )
74 #define wslua_checkuint32(L,i) (uint32_t) ( luaL_checknumber(L,i) )
75 #define wslua_checkuint64(L,i) (uint64_t) ( luaL_checknumber(L,i) )
77 #define wslua_optint(L,i,d) (int) ( luaL_optinteger(L,i,d) )
78 #define wslua_optint32(L,i,d) (int32_t) ( luaL_optnumber(L,i,d) )
79 #define wslua_optint64(L,i,d) (int64_t) ( luaL_optnumber(L,i,d) )
80 #define wslua_optuint(L,i,d) (unsigned) ( luaL_optinteger(L,i,d) )
81 #define wslua_optuint32(L,i,d) (uint32_t) ( luaL_optnumber(L,i,d) )
82 #define wslua_optuint64(L,i,d) (uint64_t) ( luaL_optnumber(L,i,d) )
181 int expert_info_table_ref;
188 bool is_postdissector;
234 typedef void (*tap_extractor_t)(lua_State*,
const void*);
239 tap_extractor_t extractor;
281 char* internal_description;
289 int seq_read_close_ref;
290 int can_write_encap_ref;
311 typedef struct {
const char* name; tap_extractor_t extractor; }
tappable_t;
322 typedef GByteArray* ByteArray;
331 typedef int64_t Int64;
332 typedef uint64_t UInt64;
349 typedef tvbparse_action_t* Shortcut;
352 typedef char* Struct;
363 #define WSLUA_CLASS_DEFINE(C,check_code) \
364 WSLUA_CLASS_DEFINE_BASE(C,check_code,NULL)
366 #define WSLUA_CLASS_DEFINE_BASE(C,check_code,retval) \
367 C to##C(lua_State* L, int idx) { \
368 C* v = (C*)lua_touserdata (L, idx); \
369 if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)", idx, #C, lua_typename(L, lua_type(L, idx))); \
370 return v ? *v : retval; \
372 C check##C(lua_State* L, int idx) { \
374 luaL_checktype(L,idx,LUA_TUSERDATA); \
375 p = (C*)luaL_checkudata(L, idx, #C); \
377 return p ? *p : retval; \
379 C* push##C(lua_State* L, C v) { \
381 luaL_checkstack(L,2,"Unable to grow stack\n"); \
382 p = (C*)lua_newuserdata(L,sizeof(C)); *p = v; \
383 luaL_getmetatable(L, #C); lua_setmetatable(L, -2); \
386 bool is##C(lua_State* L,int i) { \
388 if(!lua_isuserdata(L,i)) return false; \
389 p = lua_touserdata(L, i); \
390 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
391 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
393 return p ? true : false; \
395 C shift##C(lua_State* L,int i) { \
397 if(!lua_isuserdata(L,i)) return retval; \
398 p = (C*)lua_touserdata(L, i); \
399 lua_getfield(L, LUA_REGISTRYINDEX, #C); \
400 if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
402 if (p) { lua_remove(L,i); return *p; }\
408 const char *fieldname;
409 lua_CFunction getfunc;
410 lua_CFunction setfunc;
414 #define WSLUA_TYPEOF_FIELD "__typeof"
419 #define WSLUA_REGISTER_GC(C) \
420 luaL_getmetatable(L, #C); \
423 lua_pushcfunction(L, C ## __gc); \
424 lua_setfield(L, -2, "__gc"); \
428 #define __WSLUA_REGISTER_META(C, ATTRS) { \
429 const wslua_class C ## _class = { \
431 .instance_meta = C ## _meta, \
434 wslua_register_classinstance_meta(L, &C ## _class); \
435 WSLUA_REGISTER_GC(C); \
438 #define WSLUA_REGISTER_META(C) __WSLUA_REGISTER_META(C, NULL)
439 #define WSLUA_REGISTER_META_WITH_ATTRS(C) \
440 __WSLUA_REGISTER_META(C, C ## _attributes)
442 #define __WSLUA_REGISTER_CLASS(C, ATTRS) { \
443 const wslua_class C ## _class = { \
445 .class_methods = C ## _methods, \
446 .class_meta = C ## _meta, \
447 .instance_methods = C ## _methods, \
448 .instance_meta = C ## _meta, \
451 wslua_register_class(L, &C ## _class); \
452 WSLUA_REGISTER_GC(C); \
455 #define WSLUA_REGISTER_CLASS(C) __WSLUA_REGISTER_CLASS(C, NULL)
456 #define WSLUA_REGISTER_CLASS_WITH_ATTRS(C) \
457 __WSLUA_REGISTER_CLASS(C, C ## _attributes)
459 #define WSLUA_INIT(L) \
461 wslua_register_classes(L); \
462 wslua_register_functions(L);
466 #define WSLUA_FUNCTION extern int
468 #define WSLUA_INTERNAL_FUNCTION extern int
470 #define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); }
472 #define WSLUA_REGISTER extern int
474 #define WSLUA_METHOD static int
475 #define WSLUA_CONSTRUCTOR static int
476 #define WSLUA_ATTR_SET static int
477 #define WSLUA_ATTR_GET static int
478 #define WSLUA_METAMETHOD static int
480 #define WSLUA_METHODS static const luaL_Reg
481 #define WSLUA_META static const luaL_Reg
482 #define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name }
483 #define WSLUA_CLASS_FNREG_ALIAS(class,aliasname,name) { #aliasname, class##_##name }
484 #define WSLUA_CLASS_MTREG(class,name) { "__" #name, class##__##name }
486 #define WSLUA_ATTRIBUTES static const wslua_attribute_table
488 #define WSLUA_ATTRIBUTE_RWREG(class,name) { #name, class##_get_##name, class##_set_##name }
489 #define WSLUA_ATTRIBUTE_ROREG(class,name) { #name, class##_get_##name, NULL }
490 #define WSLUA_ATTRIBUTE_WOREG(class,name) { #name, NULL, class##_set_##name }
492 #define WSLUA_ATTRIBUTE_FUNC_SETTER(C,field) \
493 static int C##_set_##field (lua_State* L) { \
494 C obj = check##C (L,1); \
495 if (! lua_isfunction(L,-1) ) \
496 return luaL_error(L, "%s's attribute `%s' must be a function", #C , #field ); \
497 if (obj->field##_ref != LUA_NOREF) \
499 luaL_unref(L, LUA_REGISTRYINDEX, obj->field##_ref); \
500 obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
504 typedef void __dummy##C##_set_##field
506 #define WSLUA_ATTRIBUTE_GET(C,name,block) \
507 static int C##_get_##name (lua_State* L) { \
508 C obj = check##C (L,1); \
513 typedef void __dummy##C##_get_##name
515 #define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
516 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
518 #define WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,name,member) \
519 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushinteger(L,(lua_Integer)(obj->member));})
521 #define WSLUA_ATTRIBUTE_INTEGER_GETTER(C,member) \
522 WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(C,member,member)
524 #define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
525 WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
527 #define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
528 WSLUA_ATTRIBUTE_GET(C,name, { \
529 lua_pushstring(L,obj->member); \
532 #define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
533 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
535 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_GETTER(C,name,member,option) \
536 WSLUA_ATTRIBUTE_GET(C,name, { \
538 if ((obj->member) && (obj->member->len > 0)) { \
539 if (wtap_block_get_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, &str) == WTAP_OPTTYPE_SUCCESS) { \
540 lua_pushstring(L,str); \
549 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_GETTER(C,name,member,option) \
550 WSLUA_ATTRIBUTE_GET(C,name, { \
552 if ((obj->member) && (obj->member->len > 0)) { \
553 if (wtap_block_get_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, &str) == WTAP_OPTTYPE_SUCCESS) { \
554 lua_pushstring(L,str); \
559 #define WSLUA_ATTRIBUTE_SET(C,name,block) \
560 static int C##_set_##name (lua_State* L) { \
561 C obj = check##C (L,1); \
566 typedef void __dummy##C##_set_##name
568 #define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_SETTER(C,name,member) \
569 WSLUA_ATTRIBUTE_SET(C,name, { \
570 if (! lua_isboolean(L,-1) ) \
571 return luaL_error(L, "%s's attribute `%s' must be a boolean", #C , #name ); \
572 obj->member = lua_toboolean(L,-1); \
577 #define WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,name,member,cast) \
578 WSLUA_ATTRIBUTE_SET(C,name, { \
579 if (! lua_isinteger(L,-1) ) \
580 return luaL_error(L, "%s's attribute `%s' must be an integer", #C , #name ); \
581 obj->member = (cast) wslua_toint32(L,-1); \
584 #define WSLUA_ATTRIBUTE_INTEGER_SETTER(C,member,cast) \
585 WSLUA_ATTRIBUTE_NAMED_INTEGER_SETTER(C,member,member,cast)
587 #define WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,member,need_free) \
588 static int C##_set_##field (lua_State* L) { \
589 C obj = check##C (L,1); \
591 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
592 s = g_strdup(lua_tostring(L,-1)); \
594 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
596 if (obj->member != NULL && need_free) \
597 g_free((void*) obj->member); \
602 typedef void __dummy##C##_set_##field
604 #define WSLUA_ATTRIBUTE_STRING_SETTER(C,field,need_free) \
605 WSLUA_ATTRIBUTE_NAMED_STRING_SETTER(C,field,field,need_free)
607 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_STRING_SETTER(C,field,member,option) \
608 static int C##_set_##field (lua_State* L) { \
609 C obj = check##C (L,1); \
611 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
612 s = g_strdup(lua_tostring(L,-1)); \
614 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
616 if ((obj->member) && (obj->member->len > 0)) { \
617 wtap_block_set_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, s, strlen(s)); \
623 typedef void __dummy##C##_set_##field
625 #define WSLUA_ATTRIBUTE_NAMED_OPT_BLOCK_NTH_STRING_SETTER(C,field,member,option) \
626 static int C##_set_##field (lua_State* L) { \
627 C obj = check##C (L,1); \
629 if (lua_isstring(L,-1) || lua_isnil(L,-1)) { \
630 s = g_strdup(lua_tostring(L,-1)); \
632 return luaL_error(L, "%s's attribute `%s' must be a string or nil", #C , #field ); \
634 if ((obj->member) && (obj->member->len > 0)) { \
635 wtap_block_set_nth_string_option_value(g_array_index(obj->member, wtap_block_t, 0), option, 0, s, strlen(s)); \
641 typedef void __dummy##C##_set_##field
643 #define WSLUA_ERROR(name,error) { luaL_error(L, "%s%s", #name ": " ,error); }
644 #define WSLUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_ARG_ ## name ## _ ## attr, #name ": " error); }
645 #define WSLUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_OPTARG_##name##_ ##attr, #name ": " error); }
647 #define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushboolean(L,v); lua_setglobal(L,n); }
648 #define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,v); lua_setglobal(L,n); }
649 #define WSLUA_REG_GLOBAL_INTEGER(L,n,v) { lua_pushinteger(L,v); lua_setglobal(L,n); }
651 #define WSLUA_RETURN(i) return (i)
653 #define WSLUA_API extern
658 #define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
660 #define FAIL_ON_NULL_OR_EXPIRED(s) if (!*p) { \
661 luaL_argerror(L,idx,"null " s); \
662 } else if ((*p)->expired) { \
663 luaL_argerror(L,idx,"expired " s); \
667 #define CLEAR_OUTSTANDING(C, marker, marker_val) void clear_outstanding_##C(void) { \
668 while (outstanding_##C->len) { \
669 C p = (C)g_ptr_array_remove_index_fast(outstanding_##C,0); \
671 if (p->marker != marker_val) \
672 p->marker = marker_val; \
679 #define WSLUA_CLASS_DECLARE(C) \
680 extern C to##C(lua_State* L, int idx); \
681 extern C check##C(lua_State* L, int idx); \
682 extern C* push##C(lua_State* L, C v); \
683 extern int C##_register(lua_State* L); \
684 extern bool is##C(lua_State* L,int i); \
685 extern C shift##C(lua_State* L,int i)
689 #define THROW_LUA_ERROR(...) \
690 THROW_FORMATTED(DissectorError, __VA_ARGS__)
694 #define WRAP_NON_LUA_EXCEPTIONS(code) \
696 volatile bool has_error = false; \
700 lua_pushstring(L, GET_MESSAGE); \
703 if (has_error) { lua_error(L); } \
710 extern bool lua_initialized;
711 extern int lua_dissectors_table_ref;
712 extern int lua_heur_dissectors_table_ref;
714 WSLUA_DECLARE_CLASSES()
715 WSLUA_DECLARE_FUNCTIONS()
717 extern lua_State* wslua_state(
void);
738 extern int wslua__concat(lua_State* L);
739 extern bool wslua_toboolean(lua_State* L,
int n);
740 extern bool wslua_checkboolean(lua_State* L,
int n);
741 extern bool wslua_optbool(lua_State* L,
int n,
bool def);
742 extern lua_Integer wslua_tointeger(lua_State* L,
int n);
743 extern int wslua_optboolint(lua_State* L,
int n,
int def);
744 extern const char* wslua_checklstring_only(lua_State* L,
int n,
size_t *l);
745 extern const char* wslua_checkstring_only(lua_State* L,
int n);
746 extern void wslua_setfuncs(lua_State *L,
const luaL_Reg *l,
int nup);
747 extern const char* wslua_typeof_unknown;
748 extern const char* wslua_typeof(lua_State *L,
int idx);
749 extern bool wslua_get_table(lua_State *L,
int idx,
const char *name);
750 extern bool wslua_get_field(lua_State *L,
int idx,
const char *name);
753 extern expert_field* wslua_get_expert_field(
const int group,
const int severity);
754 extern void wslua_prefs_changed(
void);
755 extern void proto_register_lua(
void);
756 extern GString* lua_register_all_taps(
void);
758 extern bool wslua_has_field_extractors(
void);
759 extern void lua_prime_all_fields(
proto_tree* tree);
761 extern int Proto_commit(lua_State* L);
765 extern void clear_outstanding_FuncSavers(
void);
767 extern void Int64_pack(lua_State* L, luaL_Buffer *b,
int idx,
bool asLittleEndian);
768 extern int Int64_unpack(lua_State* L,
const char *buff,
bool asLittleEndian);
769 extern void UInt64_pack(lua_State* L, luaL_Buffer *b,
int idx,
bool asLittleEndian);
770 extern int UInt64_unpack(lua_State* L,
const char *buff,
bool asLittleEndian);
771 extern uint64_t getUInt64(lua_State *L,
int i);
774 extern int push_wsluaTvb(lua_State* L,
Tvb t);
775 extern bool push_TvbRange(lua_State* L,
tvbuff_t* tvb,
int offset,
int len);
776 extern void clear_outstanding_Tvb(
void);
777 extern void clear_outstanding_TvbRange(
void);
780 extern void clear_outstanding_Pinfo(
void);
781 extern void clear_outstanding_Column(
void);
782 extern void clear_outstanding_Columns(
void);
783 extern void clear_outstanding_PrivateTable(
void);
785 extern int get_hf_wslua_text(
void);
787 extern void clear_outstanding_TreeItem(
void);
790 extern void clear_outstanding_FieldInfo(
void);
792 extern void wslua_print_stack(
char* s, lua_State* L);
794 extern void wslua_init(register_cb cb,
void *client_data);
795 extern void wslua_early_cleanup(
void);
796 extern void wslua_cleanup(
void);
798 extern tap_extractor_t wslua_get_tap_extractor(
const char* name);
799 extern int wslua_set_tap_enums(lua_State* L);
803 extern char* wslua_get_actual_filename(
const char* fname);
805 extern int wslua_bin2hex(lua_State* L,
const uint8_t* data,
const unsigned len,
const bool lowercase,
const char* sep);
806 extern int wslua_hex2bin(lua_State* L,
const char* data,
const unsigned len,
const char* sep);
807 extern int luaopen_rex_pcre2(lua_State *L);
809 extern const char* get_current_plugin_version(
void);
810 extern const char* get_current_plugin_description(
void);
811 extern const char* get_current_plugin_repository(
void);
812 extern const char* get_current_plugin_spdx_id(
void);
813 extern void clear_current_plugin_info(
void);
815 extern int wslua_deregister_heur_dissectors(lua_State* L);
816 extern int wslua_deregister_protocols(lua_State* L);
817 extern int wslua_deregister_dissector_tables(lua_State* L);
818 extern int wslua_deregister_listeners(lua_State* L);
819 extern int wslua_deregister_fields(lua_State* L);
820 extern int wslua_deregister_filehandlers(lua_State* L);
821 extern void wslua_deregister_menus(
void);
823 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:145
Definition: tvbparse.h:133
Definition: tvbparse.h:90
Type for defining new classes.
Definition: wslua.h:727
const wslua_attribute_table * attrs
Definition: wslua.h:733
const luaL_Reg * class_meta
Definition: wslua.h:730
const char * name
Definition: wslua.h:728
const luaL_Reg * class_methods
Definition: wslua.h:729
const luaL_Reg * instance_methods
Definition: wslua.h:731
const luaL_Reg * instance_meta
Definition: wslua.h:732
struct _wslua_pref_t::@487::@488 enum_info
bool radio_buttons
Definition: wslua.h:160
char * default_s
Definition: wslua.h:165
uint32_t max_value
Definition: wslua.h:157
const enum_val_t * enumvals
Definition: wslua.h:159
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:133