4 * Ethereal's interface to the Lua Programming Language
6 * (c) 2006, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
10 * Ethereal - Network traffic analyzer
11 * By Gerald Combs <gerald@ethereal.com>
12 * Copyright 1998 Gerald Combs
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "packet-lua.h"
30 #include <epan/expert.h>
32 LUA_CLASS_DEFINE(ProtoTree,PROTO_TREE,NOP);
33 LUA_CLASS_DEFINE(ProtoItem,ITEM,NOP);
37 static int ProtoTree_add_item_any(lua_State *L, gboolean little_endian) {
40 tree,field,tvb,offset,len,datum
41 tree,field,tvb,offset,len
42 tree,tvb,offset,len,text
45 ProtoTree tree = checkProtoTree(L,1);
47 ProtoItem item = NULL;
51 pushProtoItem(L,NULL);
55 if (( luaL_checkudata (L, 2, TVB) )) {
63 if (lua_isnumber(L,3)) {
64 offset = luaL_checkint(L,3);
65 len = luaL_checkint(L,4);
66 str = lua_tostring(L,5);
67 } else if (lua_isstring(L,3)) {
70 str = lua_tostring(L,3);
72 luaL_error(L,"First arg must be either TVB or ProtoField");
76 item = proto_tree_add_text(tree,tvb,offset,len,"%s",str);
77 } CATCH(ReportedBoundsError) {
78 proto_tree_add_protocol_format(lua_tree, lua_malformed, lua_tvb, 0, 0, "[Malformed Frame: Packet Length]" );
79 luaL_error(L,"Malformed Frame");
83 } else if (( luaL_checkudata (L, 2, PROTO_FIELD) )) {
84 field = checkProtoField(L,2);
88 int offset = luaL_checkint(L,4);
89 int len = luaL_checkint(L,5);
91 if ( lua_gettop(L) == 6 ) {
98 item = proto_tree_add_uint(tree,field->hfid,tvb,offset,len,(guint32)luaL_checknumber(L,6));
104 item = proto_tree_add_int(tree,field->hfid,tvb,offset,len,(gint32)luaL_checknumber(L,6));
107 item = proto_tree_add_float(tree,field->hfid,tvb,offset,len,(float)luaL_checknumber(L,6));
110 item = proto_tree_add_double(tree,field->hfid,tvb,offset,len,(double)luaL_checknumber(L,6));
114 item = proto_tree_add_string(tree,field->hfid,tvb,offset,len,luaL_checkstring(L,6));
127 luaL_error(L,"FT_ not yet supported");
131 item = proto_tree_add_item(tree,field->hfid,tvb,offset,len,little_endian);
133 } CATCH(ReportedBoundsError) {
134 proto_tree_add_protocol_format(lua_tree, lua_malformed, lua_tvb, 0, 0, "[Malformed Frame: Packet Length]" );
135 luaL_error(L,"Malformed Frame");
140 luaL_error(L,"First arg must be either TVB or ProtoField");
144 pushProtoItem(L,item);
148 static int ProtoTree_add_item(lua_State *L) { return ProtoTree_add_item_any(L,FALSE); }
149 static int ProtoTree_add_item_le(lua_State *L) { return ProtoTree_add_item_any(L,TRUE); }
151 static int ProtoTree_tostring(lua_State *L) {
152 ProtoTree tree = checkProtoTree(L,1);
153 lua_pushstring(L,ep_strdup_printf("ProtoTree %p",tree));
158 static int ProtoTree_get_parent(lua_State *L) {
159 ProtoTree tree = checkProtoTree(L,1);
160 proto_item* item = NULL;
163 item = proto_tree_get_parent(tree);
166 pushProtoItem(L,item);
171 static const luaL_reg ProtoTree_methods[] = {
172 {"add_item", ProtoTree_add_item},
173 {"add_item_le", ProtoTree_add_item_le},
174 {"get_parent", ProtoTree_get_parent},
178 static const luaL_reg ProtoTree_meta[] = {
179 {"__tostring", ProtoTree_tostring},
183 int ProtoTree_register(lua_State* L) {
184 luaL_openlib(L, PROTO_TREE, ProtoTree_methods, 0);
185 luaL_newmetatable(L, PROTO_TREE);
186 luaL_openlib(L, 0, ProtoTree_meta, 0);
187 lua_pushliteral(L, "__index");
188 lua_pushvalue(L, -3);
190 lua_pushliteral(L, "__metatable");
191 lua_pushvalue(L, -3);
198 /* ProtoItem class */
199 static int ProtoItem_tostring(lua_State *L) {
200 ProtoItem item = checkProtoItem(L,1);
201 lua_pushstring(L,ep_strdup_printf("ProtoItem %p",item));
205 static int ProtoItem_add_subtree(lua_State *L) {
206 ProtoItem item = checkProtoItem(L,1);
208 ProtoTree tree = NULL;
211 ett = checkSubTreeType(L,2);
213 if (ett && *ett >= 0) {
214 tree = proto_item_add_subtree(item,*ett);
216 luaL_argerror(L,2,"bad ett");
220 pushProtoTree(L,tree);
224 static int ProtoItem_set_text(lua_State *L) {
225 ProtoItem item = checkProtoItem(L,1);
228 const gchar* s = luaL_checkstring(L,2);
229 proto_item_set_text(item,"%s",s);
235 static int ProtoItem_append_text(lua_State *L) {
236 ProtoItem item = checkProtoItem(L,1);
240 s = luaL_checkstring(L,2);
241 proto_item_append_text(item,"%s",s);
246 static int ProtoItem_set_len(lua_State *L) {
247 ProtoItem item = checkProtoItem(L,1);
251 len = luaL_checkint(L,2);
252 proto_item_set_len(item,len);
258 struct _expert_severity {
263 static const struct _expert_severity severities[] = {
267 {"PI_ERROR",PI_ERROR},
268 {"PI_CHECKSUM",PI_CHECKSUM},
269 {"PI_SEQUENCE",PI_SEQUENCE},
270 {"PI_RESPONSE_CODE",PI_RESPONSE_CODE},
271 {"PI_UNDECODED",PI_UNDECODED},
272 {"PI_REASSEMBLE",PI_REASSEMBLE},
273 {"PI_MALFORMED",PI_MALFORMED},
274 {"PI_DEBUG",PI_DEBUG},
278 static int str_to_expert(const gchar* str) {
279 const struct _expert_severity* s;
283 for(s = severities; s->str; s++) {
284 if (g_str_equal(str,s->str)) {
292 static const gchar* expert_to_str(int val) {
293 const struct _expert_severity* s;
294 for(s = severities; s->str; s++) {
303 static int ProtoItem_set_expert_flags(lua_State *L) {
304 ProtoItem item = checkProtoItem(L,1);
309 group = str_to_expert(luaL_checkstring(L,2));
310 severity = str_to_expert(luaL_checkstring(L,3));
312 if (group && severity) {
313 proto_item_set_expert_flags(item,group,severity);
320 static int ProtoItem_add_expert_info(lua_State *L) {
321 ProtoItem item = checkProtoItem(L,1);
324 int group = str_to_expert(luaL_checkstring(L,2));
325 int severity = str_to_expert(luaL_checkstring(L,3));
326 const gchar* str = luaL_optstring(L,4,"Expert Info");
328 expert_add_info_format(lua_pinfo, item, group, severity, "%s", str);
334 static int ProtoItem_set_generated(lua_State *L) {
335 ProtoItem item = checkProtoItem(L,1);
337 PROTO_ITEM_SET_GENERATED(item);
343 static int ProtoItem_set_hidden(lua_State *L) {
344 ProtoItem item = checkProtoItem(L,1);
346 PROTO_ITEM_SET_HIDDEN(item);
351 static const luaL_reg ProtoItem_methods[] = {
352 {"add_subtree", ProtoItem_add_subtree},
353 {"set_text", ProtoItem_set_text},
354 {"append_text", ProtoItem_append_text},
355 {"set_len", ProtoItem_set_len},
356 {"set_expert_flags", ProtoItem_set_expert_flags},
357 {"add_expert_info", ProtoItem_add_expert_info},
358 {"set_generated", ProtoItem_set_generated},
359 {"set_hidden", ProtoItem_set_hidden},
363 static const luaL_reg ProtoItem_meta[] = {
364 {"__tostring", ProtoItem_tostring},
370 int ProtoItem_register(lua_State *L) {
371 const struct _expert_severity* s;
373 luaL_openlib(L, ITEM, ProtoItem_methods, 0);
374 luaL_newmetatable(L, ITEM);
375 luaL_openlib(L, 0, ProtoItem_meta, 0);
376 lua_pushliteral(L, "__index");
377 lua_pushvalue(L, -3);
379 lua_pushliteral(L, "__metatable");
380 lua_pushvalue(L, -3);
384 for(s = severities; s->str; s++) {
385 lua_pushstring(L, s->str);
386 lua_pushstring(L, s->str);
387 lua_settable(L, LUA_GLOBALSINDEX);