aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/lua
diff options
context:
space:
mode:
authorLuis Ontanon <luis.ontanon@gmail.com>2006-03-07 01:25:14 +0000
committerLuis Ontanon <luis.ontanon@gmail.com>2006-03-07 01:25:14 +0000
commit6e04b0aadbd2c731eb9a2abf7cac6fffae2b4c34 (patch)
tree53af875c08eb8d88e82bc9018ed49a40f376aca3 /plugins/lua
parent8dbad83e5ca2173a1c4c7aa89ad890e7209348a3 (diff)
- merge SubTree, ProtoTree and ProtoItem into a single TreeItem class.
- By the same token get rid of ProtoFieldArray having Proto.fields attribute been a lua "Array" containing fields. (still untested, and probably broken) svn path=/trunk/; revision=17486
Diffstat (limited to 'plugins/lua')
-rw-r--r--plugins/lua/elua.c12
-rw-r--r--plugins/lua/elua.h35
-rw-r--r--plugins/lua/elua_gui.c16
-rw-r--r--plugins/lua/elua_pinfo.c3
-rw-r--r--plugins/lua/elua_proto.c313
-rw-r--r--plugins/lua/elua_tap.c6
-rw-r--r--plugins/lua/elua_tree.c274
-rw-r--r--plugins/lua/elua_tvb.c3
-rw-r--r--plugins/lua/elua_util.c30
9 files changed, 278 insertions, 414 deletions
diff --git a/plugins/lua/elua.c b/plugins/lua/elua.c
index f1673a0ac6..dd3bbf6516 100644
--- a/plugins/lua/elua.c
+++ b/plugins/lua/elua.c
@@ -36,7 +36,7 @@
static lua_State* L = NULL;
packet_info* lua_pinfo;
-proto_tree* lua_tree;
+struct _eth_treeitem* lua_tree;
tvbuff_t* lua_tvb;
int lua_malformed;
int lua_dissectors_table_ref;
@@ -57,9 +57,13 @@ static int elua_not_print(lua_State* L) {
void dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) {
lua_pinfo = pinfo;
- lua_tree = tree;
lua_tvb = tvb;
+ lua_tree = ep_alloc(sizeof(struct _eth_treeitem));
+ lua_tree->tree = tree;
+ lua_tree->item = proto_tree_add_text(tree,tvb,0,0,"lua fake item");
+ PROTO_ITEM_SET_HIDDEN(lua_tree->item);
+
/*
* almost equivalent to Lua:
* dissectors[current_proto](tvb,pinfo,tree)
@@ -79,7 +83,7 @@ void dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) {
push_Tvb(L,tvb);
push_Pinfo(L,pinfo);
- push_ProtoTree(L,tree);
+ push_TreeItem(L,lua_tree);
if ( lua_pcall(L,3,0,0) ) {
const gchar* error = lua_tostring(L,-1);
@@ -154,8 +158,6 @@ static void init_lua(void) {
lua_prime_all_fields(NULL);
- lua_register_subtrees();
-
lua_initialized = TRUE;
}
diff --git a/plugins/lua/elua.h b/plugins/lua/elua.h
index 23b215bcac..fa755d67c3 100644
--- a/plugins/lua/elua.h
+++ b/plugins/lua/elua.h
@@ -63,6 +63,7 @@ struct _eth_tvbrange {
typedef struct _eth_field_t {
int hfid;
+ int ett;
char* name;
char* abbr;
char* blob;
@@ -83,6 +84,7 @@ typedef struct _eth_pref_t {
gboolean b;
guint u;
const gchar* s;
+ void* p;
} value;
struct _eth_pref_t* next;
@@ -90,15 +92,15 @@ typedef struct _eth_pref_t {
} eth_pref_t;
typedef struct _eth_proto_t {
- int hfid;
- char* name;
- char* desc;
- hf_register_info* hfarray;
- gboolean hf_registered;
- module_t *prefs_module;
+ gchar* name;
+ gchar* desc;
+ int hfid;
+ int ett;
eth_pref_t prefs;
+ int fields;
+ module_t *prefs_module;
dissector_handle_t handle;
- gboolean is_postdissector;
+ gboolean is_postdissector;
} eth_proto_t;
struct _eth_distbl_t {
@@ -111,13 +113,16 @@ struct _eth_col_info {
gint col;
};
+struct _eth_treeitem {
+ proto_item* item;
+ proto_tree* tree;
+};
+
typedef struct {const gchar* str; enum ftenum id; } eth_ft_types_t;
typedef eth_pref_t* Pref;
typedef eth_pref_t* Prefs;
typedef struct _eth_field_t* ProtoField;
-typedef GArray* ProtoFieldArray;
-typedef int* SubTree;
typedef struct _eth_proto_t* Proto;
typedef struct _eth_distbl_t* DissectorTable;
typedef dissector_handle_t Dissector;
@@ -127,8 +132,7 @@ typedef struct _eth_tvbrange* TvbRange;
typedef struct _eth_col_info* Column;
typedef column_info* Columns;
typedef packet_info* Pinfo;
-typedef proto_tree* ProtoTree;
-typedef proto_item* ProtoItem;
+typedef struct _eth_treeitem* TreeItem;
typedef address* Address;
typedef header_field_info** Field;
typedef struct _eth_tap* Tap;
@@ -275,7 +279,7 @@ extern C shift##C(lua_State* L,int i)
extern packet_info* lua_pinfo;
-extern proto_tree* lua_tree;
+extern TreeItem lua_tree;
extern tvbuff_t* lua_tvb;
extern int lua_malformed;
extern dissector_handle_t lua_data_handle;
@@ -298,15 +302,14 @@ extern int lua_gui_enabled(lua_State* L);
extern void proto_register_lua(void);
extern GString* lua_register_all_taps(void);
extern void lua_prime_all_fields(proto_tree* tree);
-extern void lua_register_subtrees(void);
-extern void push_Tvb(lua_State* L, Tvb tvb);
+extern void* push_Tvb(lua_State* L, Tvb tvb);
extern void clear_outstanding_tvbs(void);
-extern void push_Pinfo(lua_State* L, Pinfo p);
+extern void* push_Pinfo(lua_State* L, Pinfo p);
extern void clear_outstanding_pinfos(void);
-extern void push_ProtoTree(lua_State* L, ProtoTree t);
+extern void* push_TreeItem(lua_State* L, TreeItem ti);
extern void clear_outstanding_trees(void);
#endif
diff --git a/plugins/lua/elua_gui.c b/plugins/lua/elua_gui.c
index 7ca6158fe3..3eed688b25 100644
--- a/plugins/lua/elua_gui.c
+++ b/plugins/lua/elua_gui.c
@@ -60,8 +60,7 @@ void lua_menu_callback(gpointer data) {
ELUA_FUNCTION elua_register_menu(lua_State* L) { /* Register a menu item in the Statistics menu. */
#define ELUA_ARG_register_menu_NAME 1 /* The name of the menu item. */
#define ELUA_ARG_register_menu_ACTION 2 /* The function to be called when the menu item is invoked. */
-#define ELUA_OPTARG_register_menu_RETAP 3 /* Whether to rerun the packet list after the menu is invoked. */
-#define ELUA_OPTARG_register_menu_USERDATA 4 /* To be passed to the action. */
+#define ELUA_OPTARG_register_menu_USERDATA 3 /* To be passed to the action. */
const gchar* name = luaL_checkstring(L,ELUA_ARG_register_menu_NAME);
struct _lua_menu_data* md;
@@ -80,10 +79,6 @@ ELUA_FUNCTION elua_register_menu(lua_State* L) { /* Register a menu item in the
md->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
if ( lua_gettop(L) > 2) {
- retap = lua_toboolean(L,ELUA_OPTARG_register_menu_RETAP);
- }
-
- if ( lua_gettop(L) > 3) {
lua_pushvalue(L, ELUA_OPTARG_register_menu_USERDATA);
md->data_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
@@ -387,8 +382,15 @@ int TextWindow_register(lua_State* L) {
ELUA_FUNCTION elua_retap_packets(lua_State* L) {
- if ( ops->retap_packets )
+ /*
+ Rescan all packets and just run taps - don't reconstruct the display.
+ */
+ if ( ops->retap_packets ) {
ops->retap_packets();
+ } else {
+ ELUA_ERROR(elua_retap_packets, "does not work on tethereal");
+ }
+
return 0;
}
diff --git a/plugins/lua/elua_pinfo.c b/plugins/lua/elua_pinfo.c
index 73c3a43941..87fd12344f 100644
--- a/plugins/lua/elua_pinfo.c
+++ b/plugins/lua/elua_pinfo.c
@@ -48,9 +48,10 @@ void clear_outstanding_pinfos(void) {
}
}
-void push_Pinfo(lua_State* L, Pinfo pinfo) {
+void* push_Pinfo(lua_State* L, Pinfo pinfo) {
void** p = (void**)pushPinfo(L,pinfo);
g_ptr_array_add(outstanding_stuff,p);
+ return p;
}
#define PUSH_COLUMN(L,c) g_ptr_array_add(outstanding_stuff,pushColumn(L,c))
diff --git a/plugins/lua/elua_proto.c b/plugins/lua/elua_proto.c
index fb778fc951..41b84b7661 100644
--- a/plugins/lua/elua_proto.c
+++ b/plugins/lua/elua_proto.c
@@ -287,6 +287,7 @@ static const eth_ft_types_t ftenums[] = {
{NULL,FT_NONE}
};
+#if 0
static enum ftenum get_ftenum(const gchar* type) {
const eth_ft_types_t* ts;
for (ts = ftenums; ts->str; ts++) {
@@ -297,6 +298,7 @@ static enum ftenum get_ftenum(const gchar* type) {
return FT_NONE;
}
+#endif
static const gchar* ftenum_to_string(enum ftenum ft) {
const eth_ft_types_t* ts;
@@ -392,24 +394,26 @@ static value_string* value_string_from_table(lua_State* L, int idx) {
}
ELUA_CONSTRUCTOR ProtoField_new(lua_State* L) { /* Creates a new field to be used in a protocol. */
-#define ELUA_ARG_ProtoField_new_ABBR 1 /* abbreviated name of the field (the string used in filters). */
-#define ELUA_ARG_ProtoField_new_NAME 2 /* Actual name of the field (the string that appears in the tree). */
+#define ELUA_ARG_ProtoField_new_NAME 1 /* Actual name of the field (the string that appears in the tree). */
+#define ELUA_ARG_ProtoField_new_ABBR 2 /* Filter name of the field (the string that is used in filters). */
#define ELUA_ARG_ProtoField_new_TYPE 3 /* Field Type (FT_*). */
-#define ELUA_OPTARG_ProtoField_new_VALUESTRING 5 /* a ValueString object. */
-#define ELUA_OPTARG_ProtoField_new_BASE 5 /* The representation BASE_*. */
-#define ELUA_OPTARG_ProtoField_new_MASK 6 /* the bitmask to be used. */
-#define ELUA_OPTARG_ProtoField_new_DESCR 7 /* The description of the field. */
+#define ELUA_OPTARG_ProtoField_new_VALUESTRING 3 /* a ValueString object. */
+#define ELUA_OPTARG_ProtoField_new_BASE 4 /* The representation BASE_*. */
+#define ELUA_OPTARG_ProtoField_new_MASK 5 /* the bitmask to be used. */
+#define ELUA_OPTARG_ProtoField_new_DESCR 6 /* The description of the field. */
ProtoField f = g_malloc(sizeof(eth_field_t));
value_string* vs;
/* will be using -2 as far as the field has not been added to an array then it will turn -1 */
f->hfid = -2;
- f->name = g_strdup(luaL_checkstring(L,ELUA_ARG_ProtoField_new_ABBR));
- f->abbr = g_strdup(luaL_checkstring(L,ELUA_ARG_ProtoField_new_NAME));
- f->type = get_ftenum(luaL_checkstring(L,ELUA_ARG_ProtoField_new_TYPE));
+ f->ett = -1;
+ f->name = g_strdup(luaL_checkstring(L,ELUA_ARG_ProtoField_new_NAME));
+ f->abbr = g_strdup(luaL_checkstring(L,ELUA_ARG_ProtoField_new_ABBR));
+ f->type = luaL_checkint(L,ELUA_ARG_ProtoField_new_TYPE);
- if (f->type == FT_NONE) {
+ /*XXX do it better*/
+ if (f->type == FT_NONE) {
ELUA_ARG_ERROR(ProtoField_new,TYPE,"invalid FT_type");
return 0;
}
@@ -442,7 +446,7 @@ ELUA_CONSTRUCTOR ProtoField_new(lua_State* L) { /* Creates a new field to be use
static int ProtoField_integer(lua_State* L, enum ftenum type) {
ProtoField f = g_malloc(sizeof(eth_field_t));
- const gchar* abbr = luaL_checkstring(L,1);
+ const gchar* abbr = luaL_checkstring(L,1);
const gchar* name = luaL_optstring(L,2,abbr);
base_display_e base = luaL_optint(L, 3, BASE_DEC);
value_string* vs = (lua_gettop(L) > 3) ? value_string_from_table(L,4) : NULL;
@@ -450,14 +454,15 @@ static int ProtoField_integer(lua_State* L, enum ftenum type) {
const gchar* blob = luaL_optstring(L,6,"");
if (base < BASE_DEC || base > BASE_HEX_DEC) {
- luaL_argerror(L,3,"Base must be either BASE_DEC, BASE_HEX, BASE_OCT,"
+ luaL_argerror(L,2,"Base must be either BASE_DEC, BASE_HEX, BASE_OCT,"
" BASE_DEC_HEX, BASE_DEC_HEX or BASE_HEX_DEC");
return 0;
}
f->hfid = -2;
+ f->ett = -1;
f->name = g_strdup(name);
- f->abbr = g_strdup(abbr);
+ f->abbr = NULL;
f->type = type;
f->vs = vs;
f->base = base;
@@ -502,13 +507,14 @@ PROTOFIELD_INTEGER(framenum,FT_FRAMENUM)
static int ProtoField_other(lua_State* L,enum ftenum type) {
ProtoField f = g_malloc(sizeof(eth_field_t));
- const gchar* abbr = luaL_checkstring(L,1);
+ const gchar* abbr = luaL_checkstring(L,1);
const gchar* name = luaL_optstring(L,2,abbr);
const gchar* blob = luaL_optstring(L,3,"");
f->hfid = -2;
+ f->ett = -1;
f->name = g_strdup(name);
- f->abbr = g_strdup(abbr);
+ f->abbr = NULL;
f->type = type;
f->vs = NULL;
f->base = ( type == FT_FLOAT || type == FT_DOUBLE) ? BASE_DEC : BASE_NONE;
@@ -633,131 +639,15 @@ int ProtoField_register(lua_State* L) {
return 1;
}
-
-ELUA_CLASS_DEFINE(ProtoFieldArray,FAIL_ON_NULL("null ProtoFieldArray"))
-/*
- A ProtoField group to be assigned to
- (only) one Proto using proto.fields = FieldArray
-*/
-
-ELUA_CONSTRUCTOR ProtoFieldArray_new(lua_State* L) {
- /*
- Creates a new protocol field array using
- */
-/* ELUA_MOREARGS zero or more ProtoFields to be added to the new ProtoFieldArray*/
- ProtoFieldArray fa;
- guint i;
- guint num_args = lua_gettop(L);
-
- fa = g_array_new(TRUE,TRUE,sizeof(hf_register_info));
-
- for ( i = 1; i <= num_args; i++) {
- ProtoField f = checkProtoField(L,i);
- hf_register_info hfri = { &(f->hfid), {f->name,f->abbr,f->type,f->base,VALS(f->vs),f->mask,f->blob,HFILL}};
-
- if (f->hfid != -2) {
- /* ELUA_ERROR(ProtoFieldArray_new,"ProtoFields can be assigned only to one ProtoFieldArray")*/
- luaL_argerror(L, i, "ProtoFields can be assigned only to one ProtoFieldArray");
- return 0;
- }
-
- f->hfid = -1;
-
- g_array_append_val(fa,hfri);
- }
-
- pushProtoFieldArray(L,fa);
- ELUA_RETURN(1);
- /* The newly created ProtoFieldArray */
-}
-
-
-ELUA_METHOD ProtoFieldArray_add(lua_State* L) {
-/* ELUA_MOREARGS zero or more ProtoFields to be added to the ProtoFieldArray*/
- ProtoFieldArray fa = checkProtoFieldArray(L,1);
- guint i;
- guint num_args = lua_gettop(L);
-
- for ( i = 2; i <= num_args; i++) {
- ProtoField f = checkProtoField(L,i);
- hf_register_info hfri = { &(f->hfid), {f->name,f->abbr,f->type,f->base,VALS(f->vs),f->mask,f->blob,HFILL}};
-
- if (f->hfid != -2) {
- /* ELUA_ERROR(ProtoFieldArray_new,"ProtoFields can be assigned only to one ProtoFieldArray")*/
- luaL_argerror(L, i, "ProtoFields can be assigned only to one ProtoFieldArray");
- return 0;
- }
-
- f->hfid = -1;
-
- g_array_append_val(fa,hfri);
- }
-
- return 0;
-}
-
-static int ProtoFieldArray_tostring(lua_State* L) {
- GString* s = g_string_new("ProtoFieldArray:\n");
- hf_register_info* f;
- ProtoFieldArray fa = checkProtoFieldArray(L,1);
- unsigned i;
-
- for(i = 0; i< fa->len; i++) {
- f = &(((hf_register_info*)(fa->data))[i]);
- g_string_sprintfa(s,"%i %s %s %s %u %p %.8x %s\n",*(f->p_id),f->hfinfo.name,f->hfinfo.abbrev,ftenum_to_string(f->hfinfo.type),f->hfinfo.display,f->hfinfo.strings,f->hfinfo.bitmask,f->hfinfo.blurb);
- };
-
- lua_pushstring(L,s->str);
- g_string_free(s,TRUE);
-
- return 1;
-}
-
-static int ProtoFieldArray_gc(lua_State* L) {
- ProtoFieldArray fa = checkProtoFieldArray(L,1);
- gboolean free_it = FALSE;
-
- /* we'll keep the data if the array was registered to a protocol */
- if (fa->len) {
- hf_register_info* f = (hf_register_info*)fa->data;
-
- if ( *(f->p_id) == -1)
- free_it = TRUE;
- } else {
- free_it = TRUE;
- }
-
- g_array_free(fa,free_it);
-
- return 0;
-}
-
-
-static const luaL_reg ProtoFieldArray_methods[] = {
- {"new", ProtoFieldArray_new},
- {"add", ProtoFieldArray_add},
- {0,0}
-};
-
-static const luaL_reg ProtoFieldArray_meta[] = {
- {"__gc", ProtoFieldArray_gc},
- {"__tostring", ProtoFieldArray_tostring},
- {0, 0}
-};
-
-int ProtoFieldArray_register(lua_State* L) {
- ELUA_REGISTER_CLASS(ProtoFieldArray);
- return 1;
-}
-
-
-
ELUA_CLASS_DEFINE(Proto,NOP)
/*
A new protocol in ethereal. Protocols have more uses, the main one is to dissect
a protocol. But they can be just dummies used to register preferences for
other purposes.
*/
+
+static int protocols_table_ref = 0;
+
ELUA_CONSTRUCTOR Proto_new(lua_State* L) {
#define ELUA_ARG_Proto_new_NAME 1 /* The name of the protocol */
#define ELUA_ARG_Proto_new_DESC 1 /* A Long Text description of the protocol (usually lowercase) */
@@ -779,20 +669,35 @@ ELUA_CONSTRUCTOR Proto_new(lua_State* L) {
proto->name = loname;
proto->desc = g_strdup(desc);
- proto->hfarray = NULL;
- proto->prefs_module = NULL;
+ proto->hfid = proto_register_protocol(proto->desc,loname,hiname);
+ proto->ett = -1;
+
+ lua_newtable (L);
+ proto->fields = luaL_ref(L, LUA_REGISTRYINDEX);
+
proto->prefs.name = NULL;
proto->prefs.label = NULL;
proto->prefs.desc = NULL;
proto->prefs.value.u = 0;
proto->prefs.next = NULL;
proto->prefs.proto = proto;
- proto->is_postdissector = FALSE;
- proto->hfid = proto_register_protocol(proto->desc,loname,hiname);
+
+ proto->prefs_module = NULL;
proto->handle = NULL;
- pushProto(L,proto);
- ELUA_RETURN(1); /* The newly created protocol */
+ if (! protocols_table_ref) {
+ lua_newtable(L);
+ protocols_table_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+ }
+
+ lua_rawgeti(L, LUA_REGISTRYINDEX, protocols_table_ref);
+ pushProto(L,proto);
+ lua_pushstring(L,loname);
+ lua_settable(L, -3);
+
+ pushProto(L,proto);
+
+ ELUA_RETURN(1); /* The newly created protocol */
}
} else {
ELUA_ARG_ERROR(Proto_new,NAME,"must be a string");
@@ -803,36 +708,6 @@ ELUA_CONSTRUCTOR Proto_new(lua_State* L) {
-static int Proto_register_field_array(lua_State* L) {
- Proto proto = toProto(L,1);
- ProtoFieldArray fa = checkProtoFieldArray(L,2);
-
- if (! fa) {
- luaL_argerror(L,2,"not a good field_array");
- return 0;
- }
-
- if( ! fa->len ) {
- luaL_argerror(L,2,"empty field_array");
- return 0;
- }
-
- if (proto->hfarray) {
- luaL_argerror(L,1,"field_array already registered for this protocol");
- }
-
- if ( *(((hf_register_info*)(fa->data))->p_id) != -1 ) {
- luaL_argerror(L,1,"this field_array has been already registered to another protocol");
- }
-
- proto->hfarray = (hf_register_info*)(fa->data);
- proto_register_field_array(proto->hfid,proto->hfarray,fa->len);
-
- return 0;
-}
-
-
-
static int Proto_tostring(lua_State* L) {
Proto proto = checkProto(L,1);
gchar* s;
@@ -922,7 +797,6 @@ static int Proto_set_init(lua_State* L) {
luaL_argerror(L,3,"The initializer of a protocol must be a function");
return 0;
}
-
}
static int Proto_get_name(lua_State* L) {
@@ -932,6 +806,44 @@ static int Proto_get_name(lua_State* L) {
return 1;
}
+
+static int Proto_get_fields(lua_State* L) {
+ Proto proto = toProto(L,1);
+ lua_rawgeti(L, LUA_REGISTRYINDEX, proto->fields);
+ return 1;
+}
+
+static int Proto_set_fields(lua_State* L) {
+ int i = 0;
+ Proto proto = toProto(L,1);
+ /* "fields" string in pos 2 */
+ /* assigned value in pos 3 */
+ lua_rawgeti(L, LUA_REGISTRYINDEX, proto->fields); /* fields table in pos 4 */
+ lua_replace(L,2); /* fields table now in pos 2 */
+
+ for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) { i++; } /* find last index of table */
+
+ if( lua_istable(L,3)) {
+
+ for (lua_pushnil(L); lua_next(L, 3); lua_pop(L, 1)) {
+ if (! isProtoField(L,-1)) {
+ return luaL_error(L,"only ProtoFields should be in the table");
+ }
+ lua_rawseti(L,4,i++);
+ }
+ } else if (isProtoField(L,3)){
+ lua_pushvalue(L, 3);
+ lua_rawseti(L,4,i++);
+ } else {
+ return luaL_error(L,"either a ProtoField or an array of protofields");
+ }
+
+ lua_pushvalue(L, 3);
+ return 1;
+}
+
+
+
typedef struct {
gchar* name;
lua_CFunction get;
@@ -942,8 +854,8 @@ static const proto_actions_t proto_actions[] = {
/* ELUA_ATTRIBUTE Pinfo_dissector RW the protocol's dissector, a function you define */
{"dissector",Proto_get_dissector, Proto_set_dissector},
- /* ELUA_ATTRIBUTE Pinfo_fields WO the ProtoFieldArray of this dissector */
- {"fields",NULL,Proto_register_field_array},
+ /* ELUA_ATTRIBUTE Pinfo_fields RO the Fields Table of this dissector */
+ {"fields" ,Proto_get_fields, Proto_set_fields},
/* ELUA_ATTRIBUTE Proto_get_prefs RO the preferences of this dissector */
{"prefs",Proto_get_prefs,NULL},
@@ -1022,6 +934,43 @@ int Proto_register(lua_State* L) {
return 1;
}
+int Proto_commit(lua_State* L) {
+ lua_settop(L,0);
+ lua_rawgeti(L, LUA_REGISTRYINDEX, protocols_table_ref);
+
+ for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
+ GArray* hfa = g_array_new(TRUE,TRUE,sizeof(hf_register_info));
+ GArray* etta = g_array_new(TRUE,TRUE,sizeof(gint*));
+ Proto proto = checkProto(L,-1);
+
+ lua_rawgeti(L, LUA_REGISTRYINDEX, proto->fields);
+
+ for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
+ ProtoField f = checkProtoField(L,-1);
+ hf_register_info hfri = { &(f->hfid), {f->name,f->abbr,f->type,f->base,VALS(f->vs),f->mask,f->blob,HFILL}};
+
+ if (f->hfid != -2) {
+ return luaL_error(L,"fields can be registered only once");
+ }
+
+ f->hfid = -1;
+ g_array_append_val(hfa,hfri);
+
+ g_array_set_size(etta,etta->len+1);
+ g_array_index(etta,gint*,etta->len) = &(f->ett);
+ }
+
+ proto_register_field_array(proto->hfid,(hf_register_info*)hfa->data,hfa->len);
+ proto_register_subtree_array((gint**)etta->data,etta->len);
+
+ g_array_free(hfa,FALSE);
+ g_array_free(etta,FALSE);
+ }
+
+ return 0;
+}
+
+
ELUA_CLASS_DEFINE(Dissector,NOP)
/*
A refererence to a dissector, used to call a dissector against a packet or a part of it.
@@ -1057,15 +1006,15 @@ ELUA_METHOD Dissector_call(lua_State* L) {
Dissector d = checkDissector(L,1);
Tvb tvb = checkTvb(L,ELUA_ARG_Dissector_call_TVB);
Pinfo pinfo = checkPinfo(L,ELUA_ARG_Dissector_call_PINFO);
- ProtoTree tree = checkProtoTree(L,ELUA_ARG_Dissector_call_TREE);
+ TreeItem ti = checkTreeItem(L,ELUA_ARG_Dissector_call_TREE);
if (! ( d && tvb && pinfo) ) return 0;
TRY {
- call_dissector(d, tvb, pinfo, tree);
+ call_dissector(d, tvb, pinfo, ti->tree);
/* XXX Are we sure about this??? is this the right/only thing to catch */
} CATCH(ReportedBoundsError) {
- proto_tree_add_protocol_format(lua_tree, lua_malformed, lua_tvb, 0, 0, "[Malformed Frame: Packet Length]" );
+ proto_tree_add_protocol_format(lua_tree->tree, lua_malformed, lua_tvb, 0, 0, "[Malformed Frame: Packet Length]" );
ELUA_ERROR(Dissector_call,"malformed frame");
return 0;
} ENDTRY;
@@ -1256,10 +1205,10 @@ ELUA_METHOD DissectorTable_try (lua_State *L) {
DissectorTable dt = checkDissectorTable(L,1);
Tvb tvb = checkTvb(L,3);
Pinfo pinfo = checkPinfo(L,4);
- ProtoTree tree = checkProtoTree(L,5);
+ TreeItem ti = checkTreeItem(L,5);
ftenum_t type;
- if (! (dt && tvb && pinfo && tree) ) return 0;
+ if (! (dt && tvb && pinfo && ti) ) return 0;
type = get_dissector_table_selector_type(dt->name);
@@ -1270,24 +1219,24 @@ ELUA_METHOD DissectorTable_try (lua_State *L) {
if (!pattern) return 0;
- if (dissector_try_string(dt->table,pattern,tvb,pinfo,tree))
+ if (dissector_try_string(dt->table,pattern,tvb,pinfo,ti->tree))
return 0;
} else if ( type == FT_UINT32 || type == FT_UINT16 || type == FT_UINT8 || type == FT_UINT24 ) {
int port = luaL_checkint(L, 2);
- if (dissector_try_port(dt->table,port,tvb,pinfo,tree))
+ if (dissector_try_port(dt->table,port,tvb,pinfo,ti->tree))
return 0;
} else {
luaL_error(L,"No such type of dissector_table");
}
- call_dissector(lua_data_handle,tvb,pinfo,tree);
+ call_dissector(lua_data_handle,tvb,pinfo,ti->tree);
/* XXX Are we sure about this??? is this the right/only thing to catch */
} CATCH(ReportedBoundsError) {
- proto_tree_add_protocol_format(lua_tree, lua_malformed, lua_tvb, 0, 0, "[Malformed Frame: Packet Length]" );
+ proto_tree_add_protocol_format(lua_tree->tree, lua_malformed, lua_tvb, 0, 0, "[Malformed Frame: Packet Length]" );
ELUA_ERROR(DissectorTable_try,"malformed frame");
} ENDTRY;
diff --git a/plugins/lua/elua_tap.c b/plugins/lua/elua_tap.c
index 4d5fb0f5e1..6181d88ee2 100644
--- a/plugins/lua/elua_tap.c
+++ b/plugins/lua/elua_tap.c
@@ -129,7 +129,7 @@ ELUA_METAMETHOD Field__call (lua_State* L) {
}
for (;in;in = in->same_name_next) {
- GPtrArray* found = proto_find_finfo(lua_tree, in->id);
+ GPtrArray* found = proto_find_finfo(lua_tree->tree, in->id);
guint i;
if (found) {
for (i=0; i<found->len; i++) {
@@ -291,7 +291,9 @@ int lua_tap_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const
lua_pinfo = pinfo;
lua_tvb = edt->tvb;
- lua_tree = edt->tree;
+ lua_tree = ep_alloc(sizeof(struct _eth_treeitem));
+ lua_tree->tree = edt->tree;
+ lua_tree->item = NULL;
switch ( lua_pcall(tap->L,3,1,1) ) {
case 0:
diff --git a/plugins/lua/elua_tree.c b/plugins/lua/elua_tree.c
index 37aaf45562..2a570629e2 100644
--- a/plugins/lua/elua_tree.c
+++ b/plugins/lua/elua_tree.c
@@ -1,5 +1,5 @@
/*
- * lua_tree.c
+ * elua_tree.c
*
* Ethereal's interface to the Lua Programming Language
*
@@ -31,12 +31,12 @@
static GPtrArray* outstanding_stuff = NULL;
-#define PUSH_PROTOITEM(L,i) g_ptr_array_add(outstanding_stuff,pushProtoItem(L,i))
-#define PUSH_PROTOTREE(L,t) g_ptr_array_add(outstanding_stuff,pushProtoTree(L,t))
+#define PUSH_TREEITEM(L,i) g_ptr_array_add(outstanding_stuff,push_TreeItem(L,i))
-void push_ProtoTree(lua_State*L, ProtoTree t) {
- void** p = (void**)pushProtoTree(L,t);
+void* push_TreeItem(lua_State*L, TreeItem t) {
+ void** p = (void**)pushTreeItem(L,t);
g_ptr_array_add(outstanding_stuff,p);
+ return p;
}
void clear_outstanding_trees(void) {
@@ -46,95 +46,33 @@ void clear_outstanding_trees(void) {
}
}
-ELUA_CLASS_DEFINE(SubTree,NOP)
-
-static GArray* lua_etts = NULL;
-static gint lua_ett = -1;
-
-void lua_register_subtrees(void) {
- gint* ettp = &lua_ett;
-
- if (!lua_etts)
- lua_etts = g_array_new(FALSE,FALSE,sizeof(gint*));
-
- g_array_append_val(lua_etts,ettp);
-
- proto_register_subtree_array((gint**)lua_etts->data,lua_etts->len);
-}
-
-static int SubTree_new(lua_State* L) {
- SubTree e;
-
- if (lua_initialized)
- luaL_error(L,"a SubTree can be created only before initialization");
-
- e = g_malloc(sizeof(gint));
- *e = -1;
-
- if (!lua_etts)
- lua_etts = g_array_new(FALSE,FALSE,sizeof(gint*));
-
- g_array_append_val(lua_etts,e);
-
- pushSubTree(L,e);
-
- return 1;
-}
-
-static int SubTree_tostring(lua_State* L) {
- SubTree e = checkSubTree(L,1);
- gchar* s = g_strdup_printf("SubTree: %i",*e);
-
- lua_pushstring(L,s);
- g_free(s);
-
- return 1;
-}
-
-
-static const luaL_reg SubTree_methods[] = {
- {"new", SubTree_new},
- {0,0}
-};
-
-static const luaL_reg SubTree_meta[] = {
- {"__tostring", SubTree_tostring},
- {0, 0}
-};
-
-int SubTree_register(lua_State* L) {
- ELUA_REGISTER_CLASS(SubTree);
- return 1;
-}
-
-ELUA_CLASS_DEFINE(ProtoTree,NOP)
+ELUA_CLASS_DEFINE(TreeItem,NOP)
/* ProtoTree class */
-static int ProtoTree_add_item_any(lua_State *L, gboolean little_endian) {
- ProtoTree tree;
+static int TreeItem_add_item_any(lua_State *L, gboolean little_endian) {
TvbRange tvbr;
Proto proto;
ProtoField field;
- ProtoItem item = NULL;
int hfid = -1;
+ int ett = -1;
ftenum_t type = FT_NONE;
-
- tree = shiftProtoTree(L,1);
-
- if (!tree) {
- pushProtoItem(L,NULL);
- return 1;
+ TreeItem tree_item = shiftTreeItem(L,1);
+ proto_item* item = NULL;
+
+ if (!tree_item) {
+ return luaL_error(L,"not a TreeItem!");
}
-
+
if (! ( field = shiftProtoField(L,1) ) ) {
if (( proto = shiftProto(L,1) )) {
hfid = proto->hfid;
type = FT_PROTOCOL;
+ ett = proto->ett;
}
} else {
hfid = field->hfid;
type = field->type;
-
+ ett = field->ett;
}
tvbr = shiftTvbRange(L,1);
@@ -150,7 +88,7 @@ static int ProtoTree_add_item_any(lua_State *L, gboolean little_endian) {
if (lua_gettop(L)) {
switch(type) {
case FT_PROTOCOL:
- item = proto_tree_add_item(tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,FALSE);
+ item = proto_tree_add_item(tree_item->tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,FALSE);
lua_pushnumber(L,0);
lua_insert(L,1);
break;
@@ -159,23 +97,23 @@ static int ProtoTree_add_item_any(lua_State *L, gboolean little_endian) {
case FT_UINT24:
case FT_UINT32:
case FT_FRAMENUM:
- item = proto_tree_add_uint(tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,(guint32)luaL_checknumber(L,1));
+ item = proto_tree_add_uint(tree_item->tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,(guint32)luaL_checknumber(L,1));
break;
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
- item = proto_tree_add_int(tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,(gint32)luaL_checknumber(L,1));
+ item = proto_tree_add_int(tree_item->tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,(gint32)luaL_checknumber(L,1));
break;
case FT_FLOAT:
- item = proto_tree_add_float(tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,(float)luaL_checknumber(L,1));
+ item = proto_tree_add_float(tree_item->tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,(float)luaL_checknumber(L,1));
break;
case FT_DOUBLE:
- item = proto_tree_add_double(tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,(double)luaL_checknumber(L,1));
+ item = proto_tree_add_double(tree_item->tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,(double)luaL_checknumber(L,1));
break;
case FT_STRING:
case FT_STRINGZ:
- item = proto_tree_add_string(tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,luaL_checkstring(L,1));
+ item = proto_tree_add_string(tree_item->tree,hfid,tvbr->tvb,tvbr->offset,tvbr->len,luaL_checkstring(L,1));
break;
case FT_UINT64:
case FT_INT64:
@@ -195,7 +133,7 @@ static int ProtoTree_add_item_any(lua_State *L, gboolean little_endian) {
lua_remove(L,1);
} else {
- item = proto_tree_add_item(tree, hfid, tvbr->tvb, tvbr->offset, tvbr->len, little_endian);
+ item = proto_tree_add_item(tree_item->tree, hfid, tvbr->tvb, tvbr->offset, tvbr->len, little_endian);
}
if ( lua_gettop(L) ) {
@@ -211,13 +149,13 @@ static int ProtoTree_add_item_any(lua_State *L, gboolean little_endian) {
if (lua_gettop(L)) {
const gchar* s = lua_tostring(L,1);
- item = proto_tree_add_text(tree, tvbr->tvb, tvbr->offset, tvbr->len,"%s",s);
+ item = proto_tree_add_text(tree_item->tree, tvbr->tvb, tvbr->offset, tvbr->len,"%s",s);
lua_remove(L,1);
}
} else {
if (lua_gettop(L)) {
const gchar* s = lua_tostring(L,1);
- item = proto_tree_add_text(tree, lua_tvb, 0, 0,"%s",s);
+ item = proto_tree_add_text(tree_item->tree, lua_tvb, 0, 0,"%s",s);
lua_remove(L,1);
}
}
@@ -231,117 +169,54 @@ static int ProtoTree_add_item_any(lua_State *L, gboolean little_endian) {
}
- PUSH_PROTOITEM(L,item);
-
- return 1;
-}
-
+ tree_item = ep_alloc(sizeof(struct _eth_treeitem));
+ tree_item->item = item;
+ tree_item->tree = proto_item_add_subtree(item,ett);;
-static int ProtoTree_add_item(lua_State *L) { return ProtoTree_add_item_any(L,FALSE); }
-static int ProtoTree_add_item_le(lua_State *L) { return ProtoTree_add_item_any(L,TRUE); }
-
-static int ProtoTree_tostring(lua_State *L) {
- ProtoTree tree = checkProtoTree(L,1);
- lua_pushstring(L,ep_strdup_printf("ProtoTree %p",tree));
- return 1;
-}
-
-
-static int ProtoTree_get_parent(lua_State *L) {
- ProtoTree tree = checkProtoTree(L,1);
- proto_item* item = NULL;
-
- if (tree) {
- item = proto_tree_get_parent(tree);
- }
-
- PUSH_PROTOITEM(L,item);
+ PUSH_TREEITEM(L,tree_item);
return 1;
}
-static const luaL_reg ProtoTree_methods[] = {
- {"add_item", ProtoTree_add_item},
- {"add_item_le", ProtoTree_add_item_le},
- {"get_parent", ProtoTree_get_parent},
- {0, 0}
-};
-
-static const luaL_reg ProtoTree_meta[] = {
- {"__tostring", ProtoTree_tostring},
- {0, 0}
-};
-
-int ProtoTree_register(lua_State* L) {
- ELUA_REGISTER_CLASS(ProtoTree);
- return 1;
-}
-
-ELUA_CLASS_DEFINE(ProtoItem,NOP)
-
-static int ProtoItem_tostring(lua_State *L) {
- ProtoItem item = checkProtoItem(L,1);
- lua_pushstring(L,ep_strdup_printf("ProtoItem %p",item));
- return 1;
-}
-static int ProtoItem_add_subtree(lua_State *L) {
- ProtoItem item = checkProtoItem(L,1);
-
- if (item) {
- SubTree* ett;
- ProtoTree tree;
-
- if (isSubTree(L,2)) {
- ett = luaL_checkudata(L,2,"SubTree");
- tree = proto_item_add_subtree(item,**ett);
- } else {
- tree = proto_item_add_subtree(item,lua_ett);
- }
-
- PUSH_PROTOTREE(L,tree);
- } else {
- pushProtoTree(L,NULL);
- }
-
- return 1;
-}
+ELUA_METHOD TreeItem_add(lua_State *L) { return TreeItem_add_item_any(L,FALSE); }
+ELUA_METHOD TreeItem_add_le(lua_State *L) { return TreeItem_add_item_any(L,TRUE); }
-static int ProtoItem_set_text(lua_State *L) {
- ProtoItem item = checkProtoItem(L,1);
+ELUA_METHOD TreeItem_set_text(lua_State *L) {
+ TreeItem ti = checkTreeItem(L,1);
- if (!item) {
+ if (ti) {
const gchar* s = luaL_checkstring(L,2);
- proto_item_set_text(item,"%s",s);
+ proto_item_set_text(ti->item,"%s",s);
}
return 0;
}
-static int ProtoItem_append_text(lua_State *L) {
- ProtoItem item = checkProtoItem(L,1);
+ELUA_METHOD TreeItem_append_text(lua_State *L) {
+ TreeItem ti = checkTreeItem(L,1);
const gchar* s;
- if (item) {
+ if (ti) {
s = luaL_checkstring(L,2);
- proto_item_append_text(item,"%s",s);
+ proto_item_append_text(ti->item,"%s",s);
}
return 0;
}
-static int ProtoItem_set_len(lua_State *L) {
- ProtoItem item = checkProtoItem(L,1);
+ELUA_METHOD TreeItem_set_len(lua_State *L) {
+ TreeItem ti = checkTreeItem(L,1);
int len;
- if (item) {
+ if (ti) {
len = luaL_checkint(L,2);
- proto_item_set_len(item,len);
+ proto_item_set_len(ti->item,len);
}
return 0;
}
-/* XXX: expensive use of strings should think in lpp */
+/* XXX: expensive use of strings or variables should think in lpp */
struct _expert_severity {
const gchar* str;
int val;
@@ -387,77 +262,76 @@ static const gchar* expert_to_str(int val) {
}
#endif
-static int ProtoItem_set_expert_flags(lua_State *L) {
- ProtoItem item = checkProtoItem(L,1);
+ELUA_METHOD TreeItem_set_expert_flags(lua_State *L) {
+ TreeItem ti = checkTreeItem(L,1);
int group;
int severity;
- if (item) {
+ if (ti) {
group = str_to_expert(luaL_checkstring(L,2));
severity = str_to_expert(luaL_checkstring(L,3));
if (group && severity) {
- proto_item_set_expert_flags(item,group,severity);
+ proto_item_set_expert_flags(ti->item,group,severity);
}
}
return 0;
}
-static int ProtoItem_add_expert_info(lua_State *L) {
- ProtoItem item = checkProtoItem(L,1);
-
- if (item) {
+ELUA_METHOD TreeItem_add_expert_info(lua_State *L) {
+ TreeItem ti = checkTreeItem(L,1);
+
+ if (ti) {
int group = str_to_expert(luaL_checkstring(L,2));
int severity = str_to_expert(luaL_checkstring(L,3));
const gchar* str = luaL_optstring(L,4,"Expert Info");
- expert_add_info_format(lua_pinfo, item, group, severity, "%s", str);
+ expert_add_info_format(lua_pinfo, ti->item, group, severity, "%s", str);
}
return 0;
}
-static int ProtoItem_set_generated(lua_State *L) {
- ProtoItem item = checkProtoItem(L,1);
- if (item) {
- PROTO_ITEM_SET_GENERATED(item);
+ELUA_METHOD TreeItem_set_generated(lua_State *L) {
+ TreeItem ti = checkTreeItem(L,1);
+ if (ti) {
+ PROTO_ITEM_SET_GENERATED(ti->item);
}
return 0;
}
-static int ProtoItem_set_hidden(lua_State *L) {
- ProtoItem item = checkProtoItem(L,1);
- if (item) {
- PROTO_ITEM_SET_HIDDEN(item);
+ELUA_METHOD TreeItem_set_hidden(lua_State *L) {
+ TreeItem ti = checkTreeItem(L,1);
+ if (ti) {
+ PROTO_ITEM_SET_HIDDEN(ti->item);
}
return 0;
}
-static const luaL_reg ProtoItem_methods[] = {
- {"add_subtree", ProtoItem_add_subtree},
- {"set_text", ProtoItem_set_text},
- {"append_text", ProtoItem_append_text},
- {"set_len", ProtoItem_set_len},
- {"set_expert_flags", ProtoItem_set_expert_flags},
- {"add_expert_info", ProtoItem_add_expert_info},
- {"set_generated", ProtoItem_set_generated},
- {"set_hidden", ProtoItem_set_hidden},
+static const luaL_reg TreeItem_methods[] = {
+ {"add", TreeItem_add},
+ {"add_le", TreeItem_add_le},
+ {"set_text", TreeItem_set_text},
+ {"append_text", TreeItem_append_text},
+ {"set_len", TreeItem_set_len},
+ {"set_expert_flags", TreeItem_set_expert_flags},
+ {"add_expert_info", TreeItem_add_expert_info},
+ {"set_generated", TreeItem_set_generated},
+ {"set_hidden", TreeItem_set_hidden},
{0, 0}
};
-
-static const luaL_reg ProtoItem_meta[] = {
- {"__tostring", ProtoItem_tostring},
+static const luaL_reg TreeItem_meta[] = {
{0, 0}
};
-int ProtoItem_register(lua_State *L) {
+int TreeItem_register(lua_State *L) {
const struct _expert_severity* s;
- ELUA_REGISTER_CLASS(ProtoItem);
+ ELUA_REGISTER_CLASS(TreeItem);
outstanding_stuff = g_ptr_array_new();
for(s = severities; s->str; s++) {
diff --git a/plugins/lua/elua_tvb.c b/plugins/lua/elua_tvb.c
index 00ed8144d7..5e867a90e9 100644
--- a/plugins/lua/elua_tvb.c
+++ b/plugins/lua/elua_tvb.c
@@ -315,9 +315,10 @@ void clear_outstanding_tvbs(void) {
}
}
-void push_Tvb(lua_State* L, Tvb tvb) {
+void* push_Tvb(lua_State* L, Tvb tvb) {
void** p = (void**)pushTvb(L,tvb);
g_ptr_array_add(outstanding_stuff,p);
+ return p;
}
diff --git a/plugins/lua/elua_util.c b/plugins/lua/elua_util.c
index b805f43ed1..4591db4d5b 100644
--- a/plugins/lua/elua_util.c
+++ b/plugins/lua/elua_util.c
@@ -126,3 +126,33 @@ ELUA_FUNCTION elua_debug( lua_State* L ) { /* Will add a log entry with debug se
return 0;
}
+
+ELUA_FUNCTION elua_test_strhash(lua_State* L) {
+ se_string_hash_t* hash = se_string_hash_new();
+
+ se_string_hash_insert(hash,"tttttttt1","tt");
+ se_string_hash_insert(hash,"t2","t2");
+ se_string_hash_insert(hash,"t3","t3");
+ se_string_hash_insert(hash,"tttttttt2","tt2");
+ se_string_hash_insert(hash,"tttttttt3","tt3");
+ se_string_hash_insert(hash,"tttttttt4","tt4");
+ se_string_hash_insert(hash,"aaaa","aaa");
+ se_string_hash_insert(hash,"ccc","c");
+ se_string_hash_insert(hash,"cccdddd","cd");
+ se_string_hash_insert(hash,"cccdd","cc");
+
+ printf("%s\n",(char*)se_string_hash_lookup(hash,"tttttttt1"));
+ printf("%s\n",(char*)se_string_hash_lookup(hash,"tttttttt2"));
+ printf("%s\n",(char*)se_string_hash_lookup(hash,"tttttttt3"));
+ printf("%s\n",(char*)se_string_hash_lookup(hash,"aaaa"));
+ printf("%s\n",(char*)se_string_hash_lookup(hash,"ccc"));
+ printf("%s\n",(char*)se_string_hash_lookup(hash,"cccdddd"));
+ printf("%s\n",(char*)se_string_hash_lookup(hash,"cccdd"));
+ printf("%s\n",(char*)se_string_hash_lookup(hash,"x"));
+
+ se_string_hash_insert(hash,"ccc","X");
+ printf("%s\n",(char*)se_string_hash_lookup(hash,"ccc"));
+
+ return 0;
+}
+