diff options
-rw-r--r-- | plugins/lua/lua_tap.c | 94 | ||||
-rw-r--r-- | plugins/lua/packet-lua.c | 54 | ||||
-rw-r--r-- | plugins/lua/packet-lua.h | 3 | ||||
-rw-r--r-- | plugins/lua/plugin.c | 4 |
4 files changed, 109 insertions, 46 deletions
diff --git a/plugins/lua/lua_tap.c b/plugins/lua/lua_tap.c index fa3c895cb0..4586d7f1cb 100644 --- a/plugins/lua/lua_tap.c +++ b/plugins/lua/lua_tap.c @@ -31,6 +31,57 @@ LUA_CLASS_DEFINE(Tap,TAP,NOP); LUA_CLASS_DEFINE(Field,FIELD,NOP); +static GPtrArray* wanted_fields = NULL; +static GPtrArray* lua_taps = NULL; +static gboolean taps_registered = FALSE; + +/* XXX this will be used in the future, called from somewhere in packet.c */ +#if 0 +void lua_prime_all_fields(proto_tree* tree) { + guint i; + + for(i=0; i < wanted_fields->len; i++) { + Field f = g_ptr_array_index(wanted_fields,i); + for (;f;f = f->same_name_next) { + proto_tree_prime_hfid(tree,f->id); + } + } +} +#else +/* XXX - this will be used while we are a plugin */ + +void lua_prime_all_fields(proto_tree* tree _U_) { + GString* fake_tap_filter = g_string_new("frame"); + guint i; + static gboolean fake_tap; + + + if ( !wanted_fields || fake_tap ) return; + + fake_tap = FALSE; + + for(i=0; i < wanted_fields->len; i++) { + Field f = g_ptr_array_index(wanted_fields,i); + g_string_sprintfa(fake_tap_filter," || %s",f->abbrev); + fake_tap = TRUE; + } + + if (fake_tap) { + /* a boring tap :-) */ + GString* error = register_tap_listener("frame", + &fake_tap, + fake_tap_filter->str, + NULL, NULL, NULL); + + if (error) { + report_failure("while regitering lua_fake_tap:\n%s",error->str); + g_string_free(error,TRUE); + } + } + +} +#endif + static int Field_get (lua_State *L) { const gchar* name = luaL_checkstring(L,1); Field f; @@ -44,6 +95,11 @@ static int Field_get (lua_State *L) { return 0; } + if (!wanted_fields) + wanted_fields = g_ptr_array_new(); + + g_ptr_array_add(wanted_fields,f); + pushField(L,f); return 1; } @@ -159,17 +215,6 @@ static int Tap_new(lua_State* L) { return 1; } -static int Tap_add(lua_State* L) { - Tap tap = checkTap(L,1); - Field in = checkField(L,2); - - if (!(tap && in)) return 0; - - g_ptr_array_add(tap->interesting_fields,in); - - return 0; -} - static int Tap_tostring(lua_State* L) { Tap tap = checkTap(L,1); gchar* str; @@ -199,8 +244,6 @@ static int Tap_set_filter(lua_State* L) { return 0; } -GPtrArray* lua_taps = NULL; -gboolean taps_registered = FALSE; GString* register_all_lua_taps(void) { @@ -223,34 +266,12 @@ GString* register_all_lua_taps(void) { return NULL; } + static int Tap_register_to_ethereal(lua_State*L) { Tap tap = checkTap(L,1); - GString* filter_s; - GPtrArray* ins; - guint i; if (!tap) return 0; - filter_s = g_string_new(""); - - if (tap->filter) - g_string_sprintfa(filter_s,"( %s ) && frame ",tap->filter); - else - g_string_sprintfa(filter_s,"frame "); - - g_free(tap->filter); - - ins = tap->interesting_fields; - - for (i=0; i < ins->len; i++) { - Field in = g_ptr_array_index(ins,i); - g_string_sprintfa(filter_s," ||%s",in->abbrev); - } - - tap->filter = filter_s->str; - - g_string_free(filter_s,FALSE); - if (!lua_taps) lua_taps = g_ptr_array_new(); @@ -261,7 +282,6 @@ static int Tap_register_to_ethereal(lua_State*L) { static const luaL_reg Tap_methods[] = { {"new", Tap_new}, - {"add", Tap_add}, {"set_filter", Tap_set_filter}, {"register", Tap_register_to_ethereal}, {0,0} diff --git a/plugins/lua/packet-lua.c b/plugins/lua/packet-lua.c index e7a0b25c6c..ed38f7985a 100644 --- a/plugins/lua/packet-lua.c +++ b/plugins/lua/packet-lua.c @@ -151,13 +151,12 @@ void dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) { /* OK */ break; case LUA_ERRRUN: - g_warning("Runtime error while calling dissectors.%s() ",pinfo->current_proto); + g_warning("Runtime error while calling " LUA_DISSECTORS_TABLE ".%s() ",pinfo->current_proto); break; case LUA_ERRMEM: - g_warning("Memory alloc error while calling dissectors.%s() ",pinfo->current_proto); + g_warning("Memory alloc error while calling " LUA_DISSECTORS_TABLE ".%s() ",pinfo->current_proto); break; default: - g_warning("-X2-"); g_assert_not_reached(); break; } @@ -172,11 +171,52 @@ static void init_lua(void) { if ( tap_error ) { report_failure("lua tap registration problem: %s",tap_error->str); + g_string_free(tap_error,TRUE); } - /* XXX in C */ - if (L) - lua_dostring(L, "for k in init_routines do init_routines[k]() end;"); + + /* this should be called in a more appropriate place */ + lua_prime_all_fields(NULL); + + if (L) { + lua_getglobal(L, LUA_INIT_ROUTINES); + + if (!lua_istable(L, -1)) { + g_warning("either `" LUA_INIT_ROUTINES "' does not exist or it is not a table!"); + return; + } + + lua_pushnil(L); + + while (lua_next(L, -2) != 0) { + const gchar* name = lua_tostring(L,-2); + if (!lua_isfunction(L,-1)) { + g_warning("`" LUA_INIT_ROUTINES ".%s' is not a function, is a %s", + name,lua_typename(L,lua_type(L,-1))); + return; + } + + switch ( lua_pcall(L,-1,0,0) ) { + case 0: + /* OK */ + break; + case LUA_ERRRUN: + g_warning("Runtime error while calling " LUA_INIT_ROUTINES ".%s() ",name); + break; + case LUA_ERRMEM: + g_warning("Memory alloc error while calling " LUA_INIT_ROUTINES ".%s() ",name); + break; + default: + g_assert_not_reached(); + break; + } + + lua_pop(L, 1); + } + + lua_pop(L, 1); + + } } void proto_reg_handoff_lua(void) { @@ -255,7 +295,7 @@ void proto_register_lua(void) lua_newtable (L); lua_settable(L, LUA_GLOBALSINDEX); - lua_pushstring(L, "init_routines"); + lua_pushstring(L, LUA_INIT_ROUTINES); lua_newtable (L); lua_settable(L, LUA_GLOBALSINDEX); diff --git a/plugins/lua/packet-lua.h b/plugins/lua/packet-lua.h index c29c049171..5bc1775669 100644 --- a/plugins/lua/packet-lua.h +++ b/plugins/lua/packet-lua.h @@ -49,6 +49,8 @@ #include <epan/emem.h> #define LUA_DISSECTORS_TABLE "dissectors" +#define LUA_INIT_ROUTINES "init_routines" + typedef struct _eth_field_t { int hfid; @@ -210,6 +212,7 @@ extern void lua_tap_reset(void *tapdata); extern void lua_tap_draw(void *tapdata); extern GString* register_all_lua_taps(void); +extern void lua_prime_all_fields(proto_tree* tree); #define WARNSTACK(s) {int i; for (i = 1; i <= lua_gettop(L); i++) g_warning("-%s-> %i %s",s,i , lua_typename(L,lua_type(L,i))); } diff --git a/plugins/lua/plugin.c b/plugins/lua/plugin.c index e782b9686b..5d9c55e075 100644 --- a/plugins/lua/plugin.c +++ b/plugins/lua/plugin.c @@ -36,14 +36,14 @@ #endif /* Name of package */ -#define PACKAGE "lua_plugin" +#define PACKAGE "lua_plugin_5.0.2" #ifdef VERSION #undef VERSION #endif /* Version number of package */ -#define VERSION "0.0.0" +#define VERSION "0.0" #include <gmodule.h> |