2006-09-25 01:09:00 +00:00
/*
* wslua_tree . c
*
* Wireshark ' s interface to the Lua Programming Language
*
2008-08-05 21:03:46 +00:00
* ( c ) 2006 , Luis E . Garcia Ontanon < luis @ ontanon . org >
2008-07-26 23:41:31 +00:00
* ( c ) 2008 , Balint Reczey < balint . reczey @ ericsson . com >
2006-09-25 01:09:00 +00:00
*
* Wireshark - Network traffic analyzer
* By Gerald Combs < gerald @ wireshark . org >
* Copyright 1998 Gerald Combs
*
2018-02-08 16:33:09 +00:00
* SPDX - License - Identifier : GPL - 2.0 - or - later
2006-09-25 01:09:00 +00:00
*/
2011-04-21 13:13:39 +00:00
# include "config.h"
2020-03-30 15:57:16 +00:00
/* WSLUA_MODULE Tree Adding Information To The Dissection Tree */
2006-10-18 18:45:24 +00:00
2006-09-25 01:09:00 +00:00
# include "wslua.h"
2014-04-12 04:32:20 +00:00
# include <epan/exceptions.h>
# include <epan/show_exception.h>
2006-09-25 01:09:00 +00:00
static gint wslua_ett = - 1 ;
2008-07-26 23:41:31 +00:00
static GPtrArray * outstanding_TreeItem = NULL ;
2006-09-25 01:09:00 +00:00
2015-07-08 19:20:50 +00:00
/* pushing a TreeItem with a NULL item or subtree is completely valid for this function */
TreeItem push_TreeItem ( lua_State * L , proto_tree * tree , proto_item * item ) {
2020-12-21 02:30:28 +00:00
TreeItem ti = g_new ( struct _wslua_treeitem , 1 ) ;
2015-07-08 19:20:50 +00:00
ti - > tree = tree ;
ti - > item = item ;
ti - > expired = FALSE ;
g_ptr_array_add ( outstanding_TreeItem , ti ) ;
return * ( pushTreeItem ( L , ti ) ) ;
2006-09-25 01:09:00 +00:00
}
2015-07-08 19:20:50 +00:00
/* creates the TreeItem but does NOT push it into Lua */
2015-01-25 19:30:13 +00:00
TreeItem create_TreeItem ( proto_tree * tree , proto_item * item )
{
TreeItem tree_item = ( TreeItem ) g_malloc ( sizeof ( struct _wslua_treeitem ) ) ;
tree_item - > tree = tree ;
tree_item - > item = item ;
tree_item - > expired = FALSE ;
return tree_item ;
}
2008-07-26 23:41:31 +00:00
CLEAR_OUTSTANDING ( TreeItem , expired , TRUE )
2006-09-25 01:09:00 +00:00
2016-04-03 08:58:44 +00:00
WSLUA_CLASS_DEFINE ( TreeItem , FAIL_ON_NULL_OR_EXPIRED ( " TreeItem " ) ) ;
2020-03-29 23:15:47 +00:00
/* <lua_class_TreeItem,`TreeItem`>>s represent information in the https://www.wireshark.org/docs/wsug_html_chunked/ChUsePacketDetailsPaneSection.html[packet details] pane of Wireshark, and the packet details view of Tshark.
A < < lua_class_TreeItem , ` TreeItem ` > > represents a node in the tree , which might also be a subtree and have a list of children .
The children of a subtree have zero or more siblings which are other children of the same < < lua_class_TreeItem , ` TreeItem ` > > subtree .
During dissection , heuristic - dissection , and post - dissection , a root < lua_class_TreeItem , ` TreeItem ` > > is passed to dissectors as the third argument of the function
2015-07-08 19:20:50 +00:00
callback ( e . g . , ` myproto . dissector ( tvbuf , pktinfo , root ) ` ) .
In some cases the tree is not truly added to , in order to improve performance .
For example for packets not currently displayed / selected in Wireshark ' s visible
window pane , or if Tshark isn ' t invoked with the ` - V ` switch . However the
2020-03-29 23:15:47 +00:00
" add " type < < lua_class_TreeItem , ` TreeItem ` > > functions can still be called , and still return < < lua_class_TreeItem , ` TreeItem ` > >
2015-07-08 19:20:50 +00:00
objects - but the info isn ' t really added to the tree . Therefore you do not
typically need to worry about whether there ' s a real tree or not . If , for some
2020-03-29 23:15:47 +00:00
reason , you need to know it , you can use the < < lua_class_attrib_treeitem_visible , ` TreeItem . visible ` > > attribute getter
2015-07-08 19:20:50 +00:00
to retrieve the state .
*/
2006-09-25 01:09:00 +00:00
2014-04-12 04:32:20 +00:00
/* the following is used by TreeItem_add_packet_field() - this can THROW errors */
static proto_item *
try_add_packet_field ( lua_State * L , TreeItem tree_item , TvbRange tvbr , const int hfid ,
const ftenum_t type , const guint encoding , gint * ret_err )
{
gint err = 0 ;
proto_item * item = NULL ;
gint endoff = 0 ;
switch ( type ) {
2014-04-13 03:20:15 +00:00
/* these all generate ByteArrays */
case FT_BYTES :
case FT_UINT_BYTES :
case FT_OID :
case FT_REL_OID :
case FT_SYSTEM_ID :
{
/* GByteArray and its data will be g_free'd by Lua */
GByteArray * gba = g_byte_array_new ( ) ;
item = proto_tree_add_bytes_item ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb ,
tvbr - > offset , tvbr - > len , encoding ,
gba , & endoff , & err ) ;
if ( err = = 0 ) {
pushByteArray ( L , gba ) ;
lua_pushinteger ( L , endoff ) ;
}
}
break ;
2014-04-12 04:32:20 +00:00
case FT_ABSOLUTE_TIME :
case FT_RELATIVE_TIME :
{
/* nstime_t will be g_free'd by Lua */
2020-12-21 02:30:28 +00:00
nstime_t * nstime = g_new0 ( nstime_t , 1 ) ;
2014-04-12 04:32:20 +00:00
item = proto_tree_add_time_item ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb ,
tvbr - > offset , tvbr - > len , encoding ,
nstime , & endoff , & err ) ;
if ( err = = 0 ) {
pushNSTime ( L , nstime ) ;
lua_pushinteger ( L , endoff ) ;
}
}
break ;
2014-04-13 03:20:15 +00:00
/* XXX: what about these? */
case FT_NONE :
case FT_PROTOCOL :
2014-04-12 04:32:20 +00:00
/* anything else just needs to be done the old fashioned way */
default :
item = proto_tree_add_item ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , encoding ) ;
lua_pushnil ( L ) ;
lua_pushnil ( L ) ;
break ;
}
if ( ret_err ) * ret_err = err ;
return item ;
}
2012-05-14 00:49:05 +00:00
WSLUA_METHOD TreeItem_add_packet_field ( lua_State * L ) {
/*
2020-03-29 23:15:47 +00:00
Adds a new child tree for the given < < lua_class_ProtoField , ` ProtoField ` > > object to this tree item ,
returning the new child < < lua_class_TreeItem , ` TreeItem ` > > .
2014-04-12 04:32:20 +00:00
2020-03-29 23:15:47 +00:00
Unlike ` TreeItem : add ( ) ` and ` TreeItem : add_le ( ) ` , the < < lua_class_ProtoField , ` ProtoField ` > > argument
2014-04-12 04:32:20 +00:00
is not optional , and cannot be a ` Proto ` object . Instead , this function always
2020-03-29 23:15:47 +00:00
uses the < < lua_class_ProtoField , ` ProtoField ` > > to determine the type of field to extract from the
2014-04-12 04:32:20 +00:00
passed - in ` TvbRange ` , highlighting the relevant bytes in the Packet Bytes pane
2020-03-29 23:15:47 +00:00
of the GUI ( if there is a GUI ) , etc . If no < < lua_class_TvbRange , ` TvbRange ` > > is given , no bytes are
highlighted and the field ' s value cannot be determined ; the < < lua_class_ProtoField , ` ProtoField ` > > must
2014-04-12 04:32:20 +00:00
have been defined / created not to have a length in such a case , or an error will
occur . For backwards - compatibility reasons the ` encoding ` argument , however ,
must still be given .
Unlike ` TreeItem : add ( ) ` and ` TreeItem : add_le ( ) ` , this function performs both
big - endian and little - endian decoding , by setting the ` encoding ` argument to
be ` ENC_BIG_ENDIAN ` or ` ENC_LITTLE_ENDIAN ` .
The signature of this function :
2018-02-09 20:56:58 +00:00
[ source , lua ]
- - - -
2014-04-12 04:32:20 +00:00
tree_item : add_packet_field ( proto_field [ , tvbrange ] , encoding , . . . )
2018-02-09 20:56:58 +00:00
- - - -
2014-04-12 04:32:20 +00:00
In Wireshark version 1.11 .3 , this function was changed to return more than
2020-03-29 23:15:47 +00:00
just the new child < < lua_class_TreeItem , ` TreeItem ` > > . The child is the first return value , so that
2014-04-12 04:32:20 +00:00
function chaining will still work as before ; but it now also returns the value
of the extracted field ( i . e . , a number , ` UInt64 ` , ` Address ` , etc . ) . If the
2020-03-29 23:15:47 +00:00
value could not be extracted from the ` TvbRange ` , the child < < lua_class_TreeItem , ` TreeItem ` > > is still
2014-04-12 04:32:20 +00:00
returned , but the second returned value is ` nil ` .
Another new feature added to this function in Wireshark version 1.11 .3 is the
ability to extract native number ` ProtoField ` s from string encoding in the
` TvbRange ` , for ASCII - based and similar string encodings . For example , a
2020-03-29 23:15:47 +00:00
< < lua_class_ProtoField , ` ProtoField ` > > of as ` ftypes . UINT32 ` type can be extracted from a ` TvbRange `
2014-04-12 04:32:20 +00:00
containing the ASCII string " 123 " , and it will correctly decode the ASCII to
the number ` 123 ` , both in the tree as well as for the second return value of
this function . To do so , you must set the ` encoding ` argument of this function
to the appropriate string ` ENC_ * ` value , bitwise - or ' d with the ` ENC_STRING `
value ( see ` init . lua ` ) . ` ENC_STRING ` is guaranteed to be a unique bit flag , and
thus it can added instead of bitwise - or ' ed as well . Only single - byte ASCII digit
string encoding types can be used for this , such as ` ENC_ASCII ` and ` ENC_UTF_8 ` .
2020-03-29 23:15:47 +00:00
For example , assuming the < < lua_class_Tvb , ` Tvb ` > > named " `tvb` " contains the string " 123 " :
2018-02-09 20:56:58 +00:00
[ source , lua ]
- - - -
2014-04-12 04:32:20 +00:00
- - this is done earlier in the script
local myfield = ProtoField . new ( " Transaction ID " , " myproto.trans_id " , ftypes . UINT16 )
- - this is done inside a dissector , post - dissector , or heuristic function
- - child will be the created child tree , and value will be the number 123 or nil on failure
local child , value = tree : add_packet_field ( myfield , tvb : range ( 0 , 3 ) , ENC_UTF_8 + ENC_STRING )
2018-02-09 20:56:58 +00:00
- - - -
2014-04-12 04:32:20 +00:00
2012-05-14 00:49:05 +00:00
*/
2014-04-12 04:32:20 +00:00
# define WSLUA_ARG_TreeItem_add_packet_field_PROTOFIELD 2 /* The ProtoField field object to add to the tree. */
2020-03-29 23:15:47 +00:00
# define WSLUA_OPTARG_TreeItem_add_packet_field_TVBRANGE 3 /* The <<lua_class_TvbRange,`TvbRange`>> of bytes in the packet this tree item covers/represents. */
2014-04-12 04:32:20 +00:00
# define WSLUA_ARG_TreeItem_add_packet_field_ENCODING 4 /* The field's encoding in the `TvbRange`. */
2020-03-29 23:15:47 +00:00
# define WSLUA_OPTARG_TreeItem_add_packet_field_LABEL 5 /* One or more strings to append to the created <<lua_class_TreeItem,`TreeItem`>>. */
2014-04-14 12:34:37 +00:00
volatile TvbRange tvbr ;
2012-05-14 00:49:05 +00:00
ProtoField field ;
int hfid ;
2014-04-14 12:34:37 +00:00
volatile int ett ;
2012-05-14 00:49:05 +00:00
ftenum_t type ;
2014-04-12 04:32:20 +00:00
TreeItem tree_item = shiftTreeItem ( L , 1 ) ;
2012-05-14 00:49:05 +00:00
guint encoding ;
proto_item * item = NULL ;
2014-04-14 12:34:37 +00:00
volatile int nargs ;
2014-04-16 02:34:07 +00:00
volatile gint err = 0 ;
2014-04-12 04:32:20 +00:00
const char * volatile error = NULL ;
2012-05-14 00:49:05 +00:00
if ( ! tree_item ) {
return luaL_error ( L , " not a TreeItem! " ) ;
}
if ( tree_item - > expired ) {
luaL_error ( L , " expired TreeItem " ) ;
return 0 ;
}
if ( ! ( field = shiftProtoField ( L , 1 ) ) ) {
luaL_error ( L , " TreeField:add_packet_field not passed a ProtoField " ) ;
return 0 ;
}
hfid = field - > hfid ;
type = field - > type ;
ett = field - > ett ;
tvbr = shiftTvbRange ( L , 1 ) ;
if ( ! tvbr ) {
/* No TvbRange specified */
2014-12-29 05:21:15 +00:00
tvbr = wmem_new ( wmem_packet_scope ( ) , struct _wslua_tvbrange ) ;
tvbr - > tvb = wmem_new ( wmem_packet_scope ( ) , struct _wslua_tvb ) ;
2012-05-14 00:49:05 +00:00
tvbr - > tvb - > ws_tvb = lua_tvb ;
tvbr - > offset = 0 ;
tvbr - > len = 0 ;
}
2014-02-27 04:42:15 +00:00
encoding = wslua_checkguint ( L , 1 ) ;
2012-05-14 00:49:05 +00:00
lua_remove ( L , 1 ) ;
2014-04-12 04:32:20 +00:00
/* get the number of additional args before we add more to the stack */
nargs = lua_gettop ( L ) ;
/* XXX: why is this being done? If the length was -1, FT_STRINGZ figures out
* the right length in tvb_get_stringz_enc ( ) ; if it was 0 , it should remain zero ;
* if it was greater than zero , then it ' s the length the caller wanted .
*/
2012-05-14 00:49:05 +00:00
if ( type = = FT_STRINGZ ) {
switch ( encoding & ENC_CHARENCODING_MASK ) {
case ENC_UTF_16 :
case ENC_UCS_2 :
2012-05-14 06:32:16 +00:00
tvbr - > len = tvb_unicode_strsize ( tvbr - > tvb - > ws_tvb , tvbr - > offset ) ;
2012-05-14 00:49:05 +00:00
break ;
default :
2016-02-08 23:02:33 +00:00
if ( tvb_find_guint8 ( tvbr - > tvb - > ws_tvb , tvbr - > offset , - 1 , 0 ) = = - 1 ) {
luaL_error ( L , " out of bounds " ) ;
return 0 ;
}
2012-05-14 00:49:05 +00:00
tvbr - > len = tvb_strsize ( tvbr - > tvb - > ws_tvb , tvbr - > offset ) ;
break ;
}
}
2014-04-12 04:32:20 +00:00
TRY {
2014-04-16 02:34:07 +00:00
gint errx = 0 ;
item = try_add_packet_field ( L , tree_item , tvbr , hfid , type , encoding , & errx ) ;
err = errx ;
2014-04-12 04:32:20 +00:00
} CATCH_ALL {
show_exception ( tvbr - > tvb - > ws_tvb , lua_pinfo , tree_item - > tree , EXCEPT_CODE , GET_MESSAGE ) ;
error = " Lua programming error " ;
} ENDTRY ;
if ( error ) { WSLUA_ERROR ( TreeItem_add_packet_field , error ) ; }
if ( err ! = 0 ) {
lua_pushnil ( L ) ;
lua_pushnil ( L ) ;
}
while ( nargs ) {
2012-05-14 00:49:05 +00:00
const gchar * s ;
s = lua_tostring ( L , 1 ) ;
if ( s ) proto_item_append_text ( item , " %s " , s ) ;
lua_remove ( L , 1 ) ;
2014-04-12 04:32:20 +00:00
nargs - - ;
2012-05-14 00:49:05 +00:00
}
2015-07-10 12:38:50 +00:00
push_TreeItem ( L , proto_item_add_subtree ( item , ett > 0 ? ett : wslua_ett ) , item ) ;
2012-05-14 00:49:05 +00:00
2014-04-12 04:32:20 +00:00
/* move the tree object before the field value */
lua_insert ( L , 1 ) ;
2020-03-29 23:15:47 +00:00
WSLUA_RETURN ( 3 ) ; /* The new child <<lua_class_TreeItem,`TreeItem`>>, the field's extracted value or nil, and offset or nil. */
2012-05-14 00:49:05 +00:00
}
2010-10-21 12:41:15 +00:00
static int TreeItem_add_item_any ( lua_State * L , gboolean little_endian ) {
2006-09-25 01:09:00 +00:00
TvbRange tvbr ;
Proto proto ;
ProtoField field ;
int hfid = - 1 ;
int ett = - 1 ;
ftenum_t type = FT_NONE ;
2009-06-08 19:45:53 +00:00
TreeItem tree_item = shiftTreeItem ( L , 1 ) ;
proto_item * item = NULL ;
2006-09-25 01:09:00 +00:00
if ( ! tree_item ) {
return luaL_error ( L , " not a TreeItem! " ) ;
}
2008-07-26 23:41:31 +00:00
if ( tree_item - > expired ) {
luaL_error ( L , " expired TreeItem " ) ;
return 0 ;
}
2006-09-25 01:09:00 +00:00
if ( ! ( field = shiftProtoField ( L , 1 ) ) ) {
if ( ( proto = shiftProto ( L , 1 ) ) ) {
hfid = proto - > hfid ;
type = FT_PROTOCOL ;
2009-06-08 19:45:53 +00:00
ett = proto - > ett ;
2016-06-27 22:15:33 +00:00
} else if ( lua_isnil ( L , 1 ) ) {
return luaL_error ( L , " first argument to TreeItem:add is nil! " ) ;
2006-09-25 01:09:00 +00:00
}
} else {
hfid = field - > hfid ;
type = field - > type ;
ett = field - > ett ;
}
tvbr = shiftTvbRange ( L , 1 ) ;
if ( ! tvbr ) {
2014-12-29 05:21:15 +00:00
tvbr = wmem_new ( wmem_packet_scope ( ) , struct _wslua_tvbrange ) ;
tvbr - > tvb = wmem_new ( wmem_packet_scope ( ) , struct _wslua_tvb ) ;
2008-07-26 23:41:31 +00:00
tvbr - > tvb - > ws_tvb = lua_tvb ;
2006-09-25 01:09:00 +00:00
tvbr - > offset = 0 ;
tvbr - > len = 0 ;
}
2010-05-12 08:08:01 +00:00
2006-09-25 01:09:00 +00:00
if ( hfid > 0 ) {
2015-07-08 19:20:50 +00:00
/* hfid is > 0 when the first arg was a ProtoField or Proto */
2016-02-08 23:02:33 +00:00
if ( type = = FT_STRINGZ ) {
if ( tvb_find_guint8 ( tvbr - > tvb - > ws_tvb , tvbr - > offset , - 1 , 0 ) = = - 1 ) {
luaL_error ( L , " out of bounds " ) ;
return 0 ;
}
tvbr - > len = tvb_strsize ( tvbr - > tvb - > ws_tvb , tvbr - > offset ) ;
}
2006-09-25 01:09:00 +00:00
if ( lua_gettop ( L ) ) {
2015-07-08 19:20:50 +00:00
/* if we got here, the (L,1) index is the value to add, instead of decoding from the Tvb */
2006-09-25 01:09:00 +00:00
switch ( type ) {
case FT_PROTOCOL :
2012-05-14 00:49:05 +00:00
item = proto_tree_add_item ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , ENC_NA ) ;
2006-09-25 01:09:00 +00:00
lua_pushnumber ( L , 0 ) ;
lua_insert ( L , 1 ) ;
break ;
2009-10-02 16:51:31 +00:00
case FT_BOOLEAN :
2014-03-27 20:35:48 +00:00
{
/* this needs to use checkinteger so that it can accept a Lua boolean and coerce it to an int */
guint32 val = ( guint32 ) ( wslua_tointeger ( L , 1 ) ) ;
item = proto_tree_add_boolean ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , val ) ;
}
2009-10-02 16:51:31 +00:00
break ;
2006-09-25 01:09:00 +00:00
case FT_UINT8 :
case FT_UINT16 :
case FT_UINT24 :
case FT_UINT32 :
case FT_FRAMENUM :
2014-02-27 04:42:15 +00:00
item = proto_tree_add_uint ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , wslua_checkguint32 ( L , 1 ) ) ;
2006-09-25 01:09:00 +00:00
break ;
case FT_INT8 :
case FT_INT16 :
case FT_INT24 :
case FT_INT32 :
2014-02-27 04:42:15 +00:00
item = proto_tree_add_int ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , wslua_checkguint32 ( L , 1 ) ) ;
2006-09-25 01:09:00 +00:00
break ;
case FT_FLOAT :
2008-07-26 23:41:31 +00:00
item = proto_tree_add_float ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , ( float ) luaL_checknumber ( L , 1 ) ) ;
2006-09-25 01:09:00 +00:00
break ;
case FT_DOUBLE :
2008-07-26 23:41:31 +00:00
item = proto_tree_add_double ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , ( double ) luaL_checknumber ( L , 1 ) ) ;
2006-09-25 01:09:00 +00:00
break ;
2011-08-19 08:36:02 +00:00
case FT_ABSOLUTE_TIME :
case FT_RELATIVE_TIME :
item = proto_tree_add_time ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , checkNSTime ( L , 1 ) ) ;
break ;
2006-09-25 01:09:00 +00:00
case FT_STRING :
2010-01-01 18:49:38 +00:00
case FT_STRINGZ :
2016-02-08 23:02:33 +00:00
item = proto_tree_add_string ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , luaL_checkstring ( L , 1 ) ) ;
2006-09-25 01:09:00 +00:00
break ;
2007-12-17 09:00:45 +00:00
case FT_BYTES :
2008-07-26 23:41:31 +00:00
item = proto_tree_add_bytes ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , ( const guint8 * ) luaL_checkstring ( L , 1 ) ) ;
2007-12-17 09:00:45 +00:00
break ;
2006-09-25 01:09:00 +00:00
case FT_UINT64 :
2014-02-03 03:49:30 +00:00
item = proto_tree_add_uint64 ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , checkUInt64 ( L , 1 ) ) ;
2008-02-21 19:58:09 +00:00
break ;
2006-09-25 01:09:00 +00:00
case FT_INT64 :
2014-02-03 03:49:30 +00:00
item = proto_tree_add_int64 ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , checkInt64 ( L , 1 ) ) ;
2008-02-21 19:58:09 +00:00
break ;
case FT_IPv4 :
2016-10-24 22:56:37 +00:00
{
Address addr = checkAddress ( L , 1 ) ;
2018-02-03 21:15:30 +00:00
guint32 addr_value ;
2016-10-24 22:56:37 +00:00
if ( addr - > type ! = AT_IPv4 ) {
luaL_error ( L , " Expected IPv4 address for FT_IPv4 field " ) ;
return 0 ;
}
2019-10-15 07:57:10 +00:00
/*
* The address is not guaranteed to be aligned on a
* 32 - bit boundary , so we can ' t safely dereference
* the pointer as if it were so aligned .
*/
memcpy ( & addr_value , addr - > data , sizeof addr_value ) ;
2018-02-03 21:15:30 +00:00
item = proto_tree_add_ipv4 ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , addr_value ) ;
2016-10-24 22:56:37 +00:00
}
break ;
case FT_IPv6 :
{
Address addr = checkAddress ( L , 1 ) ;
if ( addr - > type ! = AT_IPv6 ) {
luaL_error ( L , " Expected IPv6 address for FT_IPv6 field " ) ;
return 0 ;
}
2018-02-03 21:15:30 +00:00
item = proto_tree_add_ipv6 ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , ( const ws_in6_addr * ) addr - > data ) ;
2016-10-24 22:56:37 +00:00
}
2008-02-21 19:58:09 +00:00
break ;
2006-09-25 01:09:00 +00:00
case FT_ETHER :
2016-11-21 18:20:41 +00:00
{
Address addr = checkAddress ( L , 1 ) ;
if ( addr - > type ! = AT_ETHER ) {
luaL_error ( L , " Expected MAC address for FT_ETHER field " ) ;
return 0 ;
}
item = proto_tree_add_ether ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , ( const guint8 * ) addr - > data ) ;
}
break ;
2006-09-25 01:09:00 +00:00
case FT_UINT_BYTES :
case FT_IPXNET :
case FT_GUID :
case FT_OID :
2013-10-06 02:31:10 +00:00
case FT_REL_OID :
2013-12-23 15:18:48 +00:00
case FT_SYSTEM_ID :
2014-12-24 05:06:35 +00:00
case FT_VINES :
case FT_FCWWN :
2006-09-25 01:09:00 +00:00
default :
luaL_error ( L , " FT_ not yet supported " ) ;
return 0 ;
}
2010-05-12 08:08:01 +00:00
2006-09-25 01:09:00 +00:00
lua_remove ( L , 1 ) ;
} else {
2015-11-14 21:15:21 +00:00
if ( type = = FT_FRAMENUM ) {
luaL_error ( L , " ProtoField FRAMENUM cannot fetch value from Tvb " ) ;
return 0 ;
}
2015-07-08 19:20:50 +00:00
/* the Lua stack is empty - no value was given - so decode the value from the tvb */
2012-05-14 00:49:05 +00:00
item = proto_tree_add_item ( tree_item - > tree , hfid , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , little_endian ? ENC_LITTLE_ENDIAN : ENC_BIG_ENDIAN ) ;
2006-09-25 01:09:00 +00:00
}
2010-05-12 08:08:01 +00:00
2006-09-25 01:09:00 +00:00
if ( lua_gettop ( L ) ) {
2015-07-08 19:20:50 +00:00
/* if there was a value, it was removed earlier, so what's left is the display string to set */
2006-09-25 01:09:00 +00:00
const gchar * s = lua_tostring ( L , 1 ) ;
if ( s ) proto_item_set_text ( item , " %s " , s ) ;
lua_remove ( L , 1 ) ;
2010-05-12 08:08:01 +00:00
}
2006-09-25 01:09:00 +00:00
} else {
2015-07-08 19:20:50 +00:00
/* no ProtoField or Proto was given */
2006-09-25 01:09:00 +00:00
if ( lua_gettop ( L ) ) {
const gchar * s = lua_tostring ( L , 1 ) ;
2015-01-04 00:27:59 +00:00
const int hf = get_hf_wslua_text ( ) ;
if ( hf > - 1 ) {
/* use proto_tree_add_none_format() instead? */
item = proto_tree_add_item ( tree_item - > tree , hf , tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len , ENC_NA ) ;
proto_item_set_text ( item , " %s " , s ) ;
} else {
luaL_error ( L , " Internal error: hf_wslua_text not registered " ) ;
}
2006-09-25 01:09:00 +00:00
lua_remove ( L , 1 ) ;
2014-02-06 06:33:12 +00:00
} else {
luaL_error ( L , " Tree item ProtoField/Protocol handle is invalid (ProtoField/Proto not registered?) " ) ;
2006-09-25 01:09:00 +00:00
}
}
while ( lua_gettop ( L ) ) {
2015-07-08 19:20:50 +00:00
/* keep appending more text */
2006-09-25 01:09:00 +00:00
const gchar * s = lua_tostring ( L , 1 ) ;
if ( s ) proto_item_append_text ( item , " %s " , s ) ;
lua_remove ( L , 1 ) ;
}
2015-07-10 12:38:50 +00:00
push_TreeItem ( L , proto_item_add_subtree ( item , ett > 0 ? ett : wslua_ett ) , item ) ;
2010-05-12 08:08:01 +00:00
2006-09-25 01:09:00 +00:00
return 1 ;
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TreeItem_add ( lua_State * L ) {
/*
2020-03-29 23:15:47 +00:00
Adds a child item to this tree item , returning the new child < < lua_class_TreeItem , ` TreeItem ` > > .
2014-03-23 15:01:12 +00:00
2020-03-29 23:15:47 +00:00
If the < < lua_class_ProtoField , ` ProtoField ` > > represents a numeric value ( int , uint or float ) , then it ' s treated as a Big Endian ( network order ) value .
2014-03-23 15:01:12 +00:00
2020-03-29 23:15:47 +00:00
This function has a complicated form : ' treeitem : add ( [ protofield , ] [ tvbrange , ] [ [ value ] , label ] ] ) ' , such that if the first
argument is a < < lua_class_ProtoField , ` ProtoField ` > > or a < < lua_class_Proto , ` Proto ` > > , the second argument is a < < lua_class_TvbRange , ` TvbRange ` > > , and a third argument is given , it ' s a value ;
but if the second argument is a non - < < lua_class_TvbRange , ` TvbRange ` > > , then it ' s the value ( as opposed to filling that argument with ' nil ' ,
which is invalid for this function ) . If the first argument is a non - < < lua_class_ProtoField , ` ProtoField ` > > and a non - < < lua_class_Proto , ` Proto ` > > then this argument can
be either a < < lua_class_TvbRange , ` TvbRange ` > > or a label , and the value is not in use .
= = = = Example
[ source , lua ]
- - - -
local proto_foo = Proto ( " foo " , " Foo Protocol " )
proto_foo . fields . bytes = ProtoField . bytes ( " foo.bytes " , " Byte array " )
proto_foo . fields . u16 = ProtoField . uint16 ( " foo.u16 " , " Unsigned short " , base . HEX )
function proto_foo . dissector ( buf , pinfo , tree )
- - ignore packets less than 4 bytes long
if buf : len ( ) < 4 then return end
- - # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
- - # Assume buf ( 0 , 4 ) = = { 0x00 , 0x01 , 0x00 , 0x02 }
- - # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
local t = tree : add ( proto_foo , buf ( ) )
- - Adds a byte array that shows as : " Byte array: 00010002 "
t : add ( proto_foo . fields . bytes , buf ( 0 , 4 ) )
- - Adds a byte array that shows as " Byte array: 313233 "
- - ( the ASCII char code of each character in " 123 " )
t : add ( proto_foo . fields . bytes , buf ( 0 , 4 ) , " 123 " )
- - Adds a tree item that shows as : " Unsigned short: 0x0001 "
t : add ( proto_foo . fields . u16 , buf ( 0 , 2 ) )
- - Adds a tree item that shows as : " Unsigned short: 0x0064 "
t : add ( proto_foo . fields . u16 , buf ( 0 , 2 ) , 100 )
- - Adds a tree item that shows as : " Unsigned short: 0x0064 ( big endian ) "
t : add ( proto_foo . fields . u16 , buf ( 1 , 2 ) , 100 , nil , " ( " , nil , " big " , 999 , nil , " endian " , nil , " ) " )
- - LITTLE ENDIAN : Adds a tree item that shows as : " Unsigned short: 0x0100 "
t : add_le ( proto_foo . fields . u16 , buf ( 0 , 2 ) )
- - LITTLE ENDIAN : Adds a tree item that shows as : " Unsigned short: 0x6400 "
t : add_le ( proto_foo . fields . u16 , buf ( 0 , 2 ) , 100 )
- - LITTLE ENDIAN : Adds a tree item that shows as : " Unsigned short: 0x6400 ( little endian ) "
t : add_le ( proto_foo . fields . u16 , buf ( 1 , 2 ) , 100 , nil , " ( " , nil , " little " , 999 , nil , " endian " , nil , " ) " )
end
udp_table = DissectorTable . get ( " udp.port " )
udp_table : add ( 7777 , proto_foo )
- - - -
*/
# define WSLUA_OPTARG_TreeItem_add_PROTOFIELD 2 /* The <<lua_class_ProtoField,`ProtoField`>> field or <<lua_class_Proto,`Proto`>> protocol object to add to the tree. */
# define WSLUA_OPTARG_TreeItem_add_TVBRANGE 3 /* The <<lua_class_TvbRange,`TvbRange`>> of bytes in the packet this tree item covers/represents. */
2014-02-06 06:33:12 +00:00
# define WSLUA_OPTARG_TreeItem_add_VALUE 4 /* The field's value, instead of the ProtoField/Proto one. */
# define WSLUA_OPTARG_TreeItem_add_LABEL 5 /* One or more strings to use for the tree item label, instead of the ProtoField/Proto one. */
2014-03-27 20:35:48 +00:00
WSLUA_RETURN ( TreeItem_add_item_any ( L , FALSE ) ) ; /* The new child TreeItem. */
2006-10-24 13:16:57 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TreeItem_add_le ( lua_State * L ) {
/*
2020-03-29 23:15:47 +00:00
Adds a child item to this tree item , returning the new child < < lua_class_TreeItem , ` TreeItem ` > > .
2014-03-23 15:01:12 +00:00
2020-03-29 23:15:47 +00:00
If the < < lua_class_ProtoField , ` ProtoField ` > > represents a numeric value ( int , uint or float ) , then it ' s treated as a Little Endian value .
2014-03-23 15:01:12 +00:00
2014-10-08 17:32:55 +00:00
This function has a complicated form : ' treeitem : add_le ( [ protofield , ] [ tvbrange , ] [ [ value ] , label ] ] ) ' , such that if the first
2020-03-29 23:15:47 +00:00
argument is a < < lua_class_ProtoField , ` ProtoField ` > > or a < < lua_class_Proto , ` Proto ` > > , the second argument is a < < lua_class_TvbRange , ` TvbRange ` > > , and a third argument is given , it ' s a value ;
but if the second argument is a non - < < lua_class_TvbRange , ` TvbRange ` > > , then it ' s the value ( as opposed to filling that argument with ' nil ' ,
which is invalid for this function ) . If the first argument is a non - < < lua_class_ProtoField , ` ProtoField ` > > and a non - < < lua_class_Proto , ` Proto ` > > then this argument can
be either a < < lua_class_TvbRange , ` TvbRange ` > > or a label , and the value is not in use .
2010-10-21 12:41:15 +00:00
*/
2014-10-08 17:32:55 +00:00
# define WSLUA_OPTARG_TreeItem_add_le_PROTOFIELD 2 /* The ProtoField field or Proto protocol object to add to the tree. */
2014-02-06 06:33:12 +00:00
# define WSLUA_OPTARG_TreeItem_add_le_TVBRANGE 3 /* The TvbRange of bytes in the packet this tree item covers/represents. */
# define WSLUA_OPTARG_TreeItem_add_le_VALUE 4 /* The field's value, instead of the ProtoField/Proto one. */
# define WSLUA_OPTARG_TreeItem_add_le_LABEL 5 /* One or more strings to use for the tree item label, instead of the ProtoField/Proto one. */
2014-03-27 20:35:48 +00:00
WSLUA_RETURN ( TreeItem_add_item_any ( L , TRUE ) ) ; /* The new child TreeItem. */
2006-10-24 13:16:57 +00:00
}
2006-09-25 01:09:00 +00:00
2020-03-29 23:15:47 +00:00
/* WSLUA_ATTRIBUTE TreeItem_text RW Set/get the <<lua_class_TreeItem,`TreeItem`>>'s display string (string).
2015-07-08 19:20:50 +00:00
For the getter , if the TreeItem has no display string , then nil is returned .
@ since 1.99 .3
*/
static int TreeItem_get_text ( lua_State * L ) {
TreeItem ti = checkTreeItem ( L , 1 ) ;
gchar label_str [ ITEM_LABEL_LENGTH + 1 ] ;
gchar * label_ptr ;
2016-10-17 15:06:08 +00:00
if ( ti - > item ) {
field_info * fi = PITEM_FINFO ( ti - > item ) ;
2015-07-08 19:20:50 +00:00
if ( ! fi - > rep ) {
label_ptr = label_str ;
proto_item_fill_label ( fi , label_str ) ;
} else
label_ptr = fi - > rep - > representation ;
if ( label_ptr ) {
lua_pushstring ( L , label_ptr ) ;
} else {
lua_pushnil ( L ) ;
}
} else {
lua_pushnil ( L ) ;
}
return 1 ;
}
/* the following is used as both a method and attribute */
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TreeItem_set_text ( lua_State * L ) {
2014-03-27 20:35:48 +00:00
/* Sets the text of the label.
This used to return nothing , but as of 1.11 .3 it returns the same tree item to allow chained calls .
*/
2006-10-24 13:16:57 +00:00
# define WSLUA_ARG_TreeItem_set_text_TEXT 2 /* The text to be used. */
2006-09-25 01:09:00 +00:00
TreeItem ti = checkTreeItem ( L , 1 ) ;
2014-02-19 08:22:55 +00:00
const gchar * s = luaL_checkstring ( L , WSLUA_ARG_TreeItem_set_text_TEXT ) ;
2010-05-12 08:08:01 +00:00
2014-02-19 08:22:55 +00:00
proto_item_set_text ( ti - > item , " %s " , s ) ;
2010-05-12 08:08:01 +00:00
2014-03-27 20:35:48 +00:00
/* copy the TreeItem userdata so we give it back */
lua_pushvalue ( L , 1 ) ;
WSLUA_RETURN ( 1 ) ; /* The same TreeItem. */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TreeItem_append_text ( lua_State * L ) {
2014-03-27 20:35:48 +00:00
/* Appends text to the label.
This used to return nothing , but as of 1.11 .3 it returns the same tree item to allow chained calls .
*/
2006-10-24 13:16:57 +00:00
# define WSLUA_ARG_TreeItem_append_text_TEXT 2 /* The text to be appended. */
2006-09-25 01:09:00 +00:00
TreeItem ti = checkTreeItem ( L , 1 ) ;
2014-02-19 08:22:55 +00:00
const gchar * s = luaL_checkstring ( L , WSLUA_ARG_TreeItem_append_text_TEXT ) ;
2014-02-25 20:42:35 +00:00
2014-02-19 08:22:55 +00:00
proto_item_append_text ( ti - > item , " %s " , s ) ;
2010-05-12 08:08:01 +00:00
2014-03-27 20:35:48 +00:00
/* copy the TreeItem userdata so we give it back */
lua_pushvalue ( L , 1 ) ;
WSLUA_RETURN ( 1 ) ; /* The same TreeItem. */
2006-09-25 01:09:00 +00:00
}
2013-09-10 10:35:30 +00:00
WSLUA_METHOD TreeItem_prepend_text ( lua_State * L ) {
2014-03-27 20:35:48 +00:00
/* Prepends text to the label.
This used to return nothing , but as of 1.11 .3 it returns the same tree item to allow chained calls .
*/
2014-03-23 15:01:12 +00:00
# define WSLUA_ARG_TreeItem_prepend_text_TEXT 2 /* The text to be prepended. */
2013-09-10 10:35:30 +00:00
TreeItem ti = checkTreeItem ( L , 1 ) ;
2014-02-19 08:22:55 +00:00
const gchar * s = luaL_checkstring ( L , WSLUA_ARG_TreeItem_prepend_text_TEXT ) ;
2013-09-10 10:35:30 +00:00
2014-02-19 08:22:55 +00:00
proto_item_prepend_text ( ti - > item , " %s " , s ) ;
2013-09-10 10:35:30 +00:00
2014-03-27 20:35:48 +00:00
/* copy the TreeItem userdata so we give it back */
lua_pushvalue ( L , 1 ) ;
WSLUA_RETURN ( 1 ) ; /* The same TreeItem. */
2013-09-10 10:35:30 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TreeItem_add_expert_info ( lua_State * L ) {
2014-03-25 22:11:05 +00:00
/* Sets the expert flags of the item and adds expert info to the packet.
This function does * not * create a truly filterable expert info for a protocol .
Instead you should use ` TreeItem . add_proto_expert_info ( ) ` .
Note : This function is provided for backwards compatibility only , and should not
be used in new Lua code . It may be removed in the future . You should only
use ` TreeItem . add_proto_expert_info ( ) ` .
*/
2014-03-23 15:01:12 +00:00
# define WSLUA_OPTARG_TreeItem_add_expert_info_GROUP 2 / * One of `PI_CHECKSUM`, `PI_SEQUENCE`,
` PI_RESPONSE_CODE ` , ` PI_REQUEST_CODE ` ,
` PI_UNDECODED ` , ` PI_REASSEMBLE ` ,
` PI_MALFORMED ` or ` PI_DEBUG ` . */
# define WSLUA_OPTARG_TreeItem_add_expert_info_SEVERITY 3 / * One of `PI_CHAT`, `PI_NOTE`,
` PI_WARN ` , or ` PI_ERROR ` . */
# define WSLUA_OPTARG_TreeItem_add_expert_info_TEXT 4 /* The text for the expert info display. */
2014-03-25 22:11:05 +00:00
TreeItem ti = checkTreeItem ( L , 1 ) ;
2015-01-22 21:04:15 +00:00
int group = ( int ) luaL_optinteger ( L , WSLUA_OPTARG_TreeItem_add_expert_info_GROUP , PI_DEBUG ) ;
int severity = ( int ) luaL_optinteger ( L , WSLUA_OPTARG_TreeItem_add_expert_info_SEVERITY , PI_CHAT ) ;
2014-03-25 22:11:05 +00:00
expert_field * ei_info = wslua_get_expert_field ( group , severity ) ;
const gchar * str ;
if ( lua_gettop ( L ) > = WSLUA_OPTARG_TreeItem_add_expert_info_TEXT ) {
str = wslua_checkstring_only ( L , WSLUA_OPTARG_TreeItem_add_expert_info_TEXT ) ;
expert_add_info_format ( lua_pinfo , ti - > item , ei_info , " %s " , str ) ;
} else {
expert_add_info ( lua_pinfo , ti - > item , ei_info ) ;
}
2014-03-27 20:35:48 +00:00
/* copy the TreeItem userdata so we give it back */
lua_pushvalue ( L , 1 ) ;
WSLUA_RETURN ( 1 ) ; /* The same TreeItem. */
2014-03-25 22:11:05 +00:00
}
WSLUA_METHOD TreeItem_add_proto_expert_info ( lua_State * L ) {
/* Sets the expert flags of the tree item and adds expert info to the packet.
@ since 1.11 .3
*/
2020-03-29 23:15:47 +00:00
# define WSLUA_ARG_TreeItem_add_proto_expert_info_EXPERT 2 /* The <<lua_class_ProtoExpert,`ProtoExpert`>> object to add to the tree. */
2014-03-25 22:11:05 +00:00
# define WSLUA_OPTARG_TreeItem_add_proto_expert_info_TEXT 3 / * Text for the expert info display
( default is to use the registered
text ) . */
2006-09-25 01:09:00 +00:00
TreeItem ti = checkTreeItem ( L , 1 ) ;
2014-03-25 22:11:05 +00:00
ProtoExpert expert = checkProtoExpert ( L , WSLUA_ARG_TreeItem_add_proto_expert_info_EXPERT ) ;
const gchar * str ;
if ( expert - > ids . ei = = EI_INIT_EI | | expert - > ids . hf = = EI_INIT_HF ) {
luaL_error ( L , " ProtoExpert is not registered " ) ;
return 0 ;
}
if ( lua_gettop ( L ) > = WSLUA_OPTARG_TreeItem_add_proto_expert_info_TEXT ) {
str = wslua_checkstring_only ( L , WSLUA_OPTARG_TreeItem_add_proto_expert_info_TEXT ) ;
expert_add_info_format ( lua_pinfo , ti - > item , & expert - > ids , " %s " , str ) ;
} else {
expert_add_info ( lua_pinfo , ti - > item , & expert - > ids ) ;
}
2014-03-27 20:35:48 +00:00
/* copy the TreeItem userdata so we give it back */
lua_pushvalue ( L , 1 ) ;
WSLUA_RETURN ( 1 ) ; /* The same TreeItem. */
2014-03-25 22:11:05 +00:00
}
2009-06-08 19:45:53 +00:00
2014-03-25 22:11:05 +00:00
WSLUA_METHOD TreeItem_add_tvb_expert_info ( lua_State * L ) {
/* Sets the expert flags of the tree item and adds expert info to the packet
2020-03-29 23:15:47 +00:00
associated with the < < lua_class_Tvb , ` Tvb ` > > or < < lua_class_TvbRange , ` TvbRange ` > > bytes in the packet .
2014-03-25 22:11:05 +00:00
@ since 1.11 .3
*/
2020-03-29 23:15:47 +00:00
# define WSLUA_ARG_TreeItem_add_tvb_expert_info_EXPERT 2 /* The <<lua_class_ProtoExpert,`ProtoExpert`>> object to add to the tree. */
# define WSLUA_ARG_TreeItem_add_tvb_expert_info_TVB 3 / * The <<lua_class_Tvb,`Tvb`>> or <<lua_class_TvbRange,`TvbRange`>> object bytes to associate
2014-03-25 22:11:05 +00:00
the expert info with . */
# define WSLUA_OPTARG_TreeItem_add_tvb_expert_info_TEXT 4 / * Text for the expert info display
( default is to use the registered
text ) . */
TreeItem ti = checkTreeItem ( L , 1 ) ;
ProtoExpert expert = checkProtoExpert ( L , WSLUA_ARG_TreeItem_add_proto_expert_info_EXPERT ) ;
TvbRange tvbr ;
const gchar * str ;
if ( expert - > ids . ei = = EI_INIT_EI | | expert - > ids . hf = = EI_INIT_HF ) {
luaL_error ( L , " ProtoExpert is not registered " ) ;
return 0 ;
}
tvbr = shiftTvbRange ( L , WSLUA_ARG_TreeItem_add_tvb_expert_info_TVB ) ;
if ( ! tvbr ) {
2014-12-29 05:21:15 +00:00
tvbr = wmem_new ( wmem_packet_scope ( ) , struct _wslua_tvbrange ) ;
2014-03-25 22:11:05 +00:00
tvbr - > tvb = shiftTvb ( L , WSLUA_ARG_TreeItem_add_tvb_expert_info_TVB ) ;
if ( ! tvbr - > tvb ) {
2014-12-29 05:21:15 +00:00
tvbr - > tvb = wmem_new ( wmem_packet_scope ( ) , struct _wslua_tvb ) ;
2014-03-25 22:11:05 +00:00
}
tvbr - > tvb - > ws_tvb = lua_tvb ;
tvbr - > offset = 0 ;
tvbr - > len = 0 ;
}
if ( lua_gettop ( L ) > = WSLUA_OPTARG_TreeItem_add_proto_expert_info_TEXT ) {
str = wslua_checkstring_only ( L , WSLUA_OPTARG_TreeItem_add_proto_expert_info_TEXT ) ;
proto_tree_add_expert_format ( ti - > tree , lua_pinfo , & expert - > ids ,
tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len ,
" %s " , str ) ;
} else {
proto_tree_add_expert ( ti - > tree , lua_pinfo , & expert - > ids ,
tvbr - > tvb - > ws_tvb , tvbr - > offset , tvbr - > len ) ;
}
2010-05-12 08:08:01 +00:00
2014-03-27 20:35:48 +00:00
/* copy the TreeItem userdata so we give it back */
lua_pushvalue ( L , 1 ) ;
WSLUA_RETURN ( 1 ) ; /* The same TreeItem. */
2006-09-25 01:09:00 +00:00
}
2015-07-08 19:20:50 +00:00
2020-03-29 23:15:47 +00:00
/* WSLUA_ATTRIBUTE TreeItem_visible RO Get the <<lua_class_TreeItem,`TreeItem`>>'s subtree visibility status (boolean).
2015-07-08 19:20:50 +00:00
2015-07-24 00:27:26 +00:00
@ since 1.99 .8
2015-07-08 19:20:50 +00:00
*/
static int TreeItem_get_visible ( lua_State * L ) {
TreeItem ti = checkTreeItem ( L , 1 ) ;
if ( ti - > tree ) {
lua_pushboolean ( L , PTREE_DATA ( ti - > tree ) - > visible ) ;
}
else {
lua_pushboolean ( L , FALSE ) ;
}
return 1 ;
}
2020-03-29 23:15:47 +00:00
/* WSLUA_ATTRIBUTE TreeItem_generated RW Set/get the <<lua_class_TreeItem,`TreeItem`>>'s generated state (boolean).
2015-07-08 19:20:50 +00:00
2015-07-24 00:27:26 +00:00
@ since 1.99 .8
2015-07-08 19:20:50 +00:00
*/
static int TreeItem_get_generated ( lua_State * L ) {
TreeItem ti = checkTreeItem ( L , 1 ) ;
2019-04-03 21:32:30 +00:00
lua_pushboolean ( L , proto_item_is_generated ( ti - > item ) ) ;
2015-07-08 19:20:50 +00:00
return 1 ;
}
/* the following is used as both a method and attribute. As a method it defaults
to setting the value , because that ' s what it used to do before . */
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TreeItem_set_generated ( lua_State * L ) {
2020-03-29 23:15:47 +00:00
/* Marks the <<lua_class_TreeItem,`TreeItem`>> as a generated field (with data inferred but not contained in the packet).
2014-03-27 20:35:48 +00:00
This used to return nothing , but as of 1.11 .3 it returns the same tree item to allow chained calls .
*/
2020-03-29 23:15:47 +00:00
# define WSLUA_OPTARG_TreeItem_set_generated_BOOL 2 / * A Lua boolean, which if `true` sets the <<lua_class_TreeItem,`TreeItem`>>
2015-07-08 19:20:50 +00:00
generated flag , else clears it ( default = true ) */
2006-09-25 01:09:00 +00:00
TreeItem ti = checkTreeItem ( L , 1 ) ;
2015-07-08 19:20:50 +00:00
gboolean set = wslua_optbool ( L , WSLUA_OPTARG_TreeItem_set_generated_BOOL , TRUE ) ;
2014-02-19 08:22:55 +00:00
2015-07-08 19:20:50 +00:00
if ( set ) {
2019-04-03 21:32:30 +00:00
proto_item_set_generated ( ti - > item ) ;
2015-07-08 19:20:50 +00:00
} else {
if ( ti - > item )
FI_RESET_FLAG ( PITEM_FINFO ( ti - > item ) , FI_GENERATED ) ;
}
2014-02-19 08:22:55 +00:00
2014-03-27 20:35:48 +00:00
/* copy the TreeItem userdata so we give it back */
lua_pushvalue ( L , 1 ) ;
WSLUA_RETURN ( 1 ) ; /* The same TreeItem. */
2006-09-25 01:09:00 +00:00
}
2020-03-29 23:15:47 +00:00
/* WSLUA_ATTRIBUTE TreeItem_hidden RW Set/get <<lua_class_TreeItem,`TreeItem`>>'s hidden state (boolean).
2006-09-25 01:09:00 +00:00
2015-07-24 00:27:26 +00:00
@ since 1.99 .8
2015-07-08 19:20:50 +00:00
*/
static int TreeItem_get_hidden ( lua_State * L ) {
TreeItem ti = checkTreeItem ( L , 1 ) ;
2019-04-03 21:32:30 +00:00
lua_pushboolean ( L , proto_item_is_hidden ( ti - > item ) ) ;
2015-07-08 19:20:50 +00:00
return 1 ;
}
/* the following is used as both a method and attribute. As a method it defaults
to setting the value , because that ' s what it used to do before . */
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TreeItem_set_hidden ( lua_State * L ) {
2020-03-29 23:15:47 +00:00
/*
Marks the < < lua_class_TreeItem , ` TreeItem ` > > as a hidden field ( neither displayed nor used in filters ) .
Deprecated
2015-07-08 19:20:50 +00:00
2020-03-29 23:15:47 +00:00
This used to return nothing , but as of 1.11 .3 it returns the same tree item to allow chained calls .
2015-07-08 19:20:50 +00:00
*/
2020-03-29 23:15:47 +00:00
# define WSLUA_OPTARG_TreeItem_set_hidden_BOOL 2 / * A Lua boolean, which if `true` sets the <<lua_class_TreeItem,`TreeItem`>>
hidden flag , else clears it . Default is ` true ` . */
2006-09-25 01:09:00 +00:00
TreeItem ti = checkTreeItem ( L , 1 ) ;
2015-07-08 19:20:50 +00:00
gboolean set = wslua_optbool ( L , WSLUA_OPTARG_TreeItem_set_hidden_BOOL , TRUE ) ;
2014-02-19 08:22:55 +00:00
2015-07-08 19:20:50 +00:00
if ( set ) {
2019-04-03 21:32:30 +00:00
proto_item_set_hidden ( ti - > item ) ;
2015-07-08 19:20:50 +00:00
} else {
2019-04-03 21:32:30 +00:00
proto_item_set_visible ( ti - > item ) ;
2015-07-08 19:20:50 +00:00
}
2014-02-19 08:22:55 +00:00
2014-03-27 20:35:48 +00:00
/* copy the TreeItem userdata so we give it back */
lua_pushvalue ( L , 1 ) ;
WSLUA_RETURN ( 1 ) ; /* The same TreeItem. */
2006-09-25 01:09:00 +00:00
}
2020-03-29 23:15:47 +00:00
/* WSLUA_ATTRIBUTE TreeItem_len RW Set/get <<lua_class_TreeItem,`TreeItem`>>'s length inside tvb, after it has already been created.
2015-07-08 19:20:50 +00:00
2015-07-24 00:27:26 +00:00
@ since 1.99 .8
2015-07-08 19:20:50 +00:00
*/
static int TreeItem_get_len ( lua_State * L ) {
TreeItem ti = checkTreeItem ( L , 1 ) ;
int len = 0 ;
2016-01-09 02:25:26 +00:00
/* XXX - this is *NOT* guaranteed to return a correct value! */
2015-07-08 19:20:50 +00:00
len = proto_item_get_len ( ti - > item ) ;
lua_pushinteger ( L , len > 0 ? len : 0 ) ;
return 1 ;
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TreeItem_set_len ( lua_State * L ) {
2020-03-29 23:15:47 +00:00
/* Set <<lua_class_TreeItem,`TreeItem`>>'s length inside tvb, after it has already been created.
2014-03-27 20:35:48 +00:00
This used to return nothing , but as of 1.11 .3 it returns the same tree item to allow chained calls .
*/
2009-09-10 06:37:04 +00:00
# define WSLUA_ARG_TreeItem_set_len_LEN 2 /* The length to be used. */
TreeItem ti = checkTreeItem ( L , 1 ) ;
2015-01-22 21:04:15 +00:00
gint len = ( int ) luaL_checkinteger ( L , WSLUA_ARG_TreeItem_set_len_LEN ) ;
2010-05-12 08:08:01 +00:00
2014-02-19 08:22:55 +00:00
proto_item_set_len ( ti - > item , len ) ;
2010-05-12 08:08:01 +00:00
2014-03-27 20:35:48 +00:00
/* copy the TreeItem userdata so we give it back */
lua_pushvalue ( L , 1 ) ;
WSLUA_RETURN ( 1 ) ; /* The same TreeItem. */
2009-09-10 06:37:04 +00:00
}
2017-04-27 17:06:24 +00:00
WSLUA_METHOD TreeItem_referenced ( lua_State * L ) {
2020-03-29 23:15:47 +00:00
/* Checks if a <<lua_class_ProtoField,`ProtoField`>> or <<lua_class_Dissector,`Dissector`>> is referenced by a filter/tap/UI.
2017-04-27 17:06:24 +00:00
2020-03-29 23:15:47 +00:00
If this function returns ` false ` , it means that the field ( or dissector ) does not need to be dissected
2017-04-27 17:06:24 +00:00
and can be safely skipped . By skipping a field rather than dissecting it , the dissector will
usually run faster since Wireshark will not do extra dissection work when it doesn ' t need the field .
You can use this in conjunction with the TreeItem . visible attribute . This function will always return
TRUE when the TreeItem is visible . When it is not visible and the field is not referenced , you can
speed up the dissection by not dissecting the field as it is not needed for display or filtering .
2020-03-29 23:15:47 +00:00
This function takes one parameter that can be a < < lua_class_ProtoField , ` ProtoField ` > > or < < lua_class_Dissector , ` Dissector ` > > .
The < < lua_class_Dissector , ` Dissector ` > > form is useful when you need to decide whether to call a sub - dissector .
2017-04-27 17:06:24 +00:00
2018-02-09 20:56:58 +00:00
@ since 2.4 .0
2017-04-27 17:06:24 +00:00
*/
2020-03-29 23:15:47 +00:00
# define WSLUA_ARG_TreeItem_referenced_PROTOFIELD 2 /* The <<lua_class_ProtoField,`ProtoField`>> or <<lua_class_Dissector,`Dissector`>> to check if referenced. */
2017-04-27 17:06:24 +00:00
TreeItem ti = checkTreeItem ( L , 1 ) ;
if ( ! ti ) return 0 ;
ProtoField f = shiftProtoField ( L , WSLUA_ARG_TreeItem_referenced_PROTOFIELD ) ;
if ( f ) {
lua_pushboolean ( L , proto_field_is_referenced ( ti - > tree , f - > hfid ) ) ;
}
else {
Dissector d = checkDissector ( L , WSLUA_ARG_TreeItem_referenced_PROTOFIELD ) ;
if ( ! d ) return 0 ;
lua_pushboolean ( L , proto_field_is_referenced ( ti - > tree , dissector_handle_get_protocol_index ( d ) ) ) ;
}
WSLUA_RETURN ( 1 ) ; /* A boolean indicating if the ProtoField/Dissector is referenced */
}
2015-07-08 19:20:50 +00:00
WSLUA_METAMETHOD TreeItem__tostring ( lua_State * L ) {
2020-03-29 23:15:47 +00:00
/* Returns string debug information about the <<lua_class_TreeItem,`TreeItem`>>.
2015-07-08 19:20:50 +00:00
2015-07-24 00:27:26 +00:00
@ since 1.99 .8
2015-07-08 19:20:50 +00:00
*/
TreeItem ti = toTreeItem ( L , 1 ) ;
if ( ti ) {
lua_pushfstring ( L ,
" TreeItem: expired=%s, has item=%s, has subtree=%s, they are %sthe same " ,
ti - > expired ? " true " : " false " ,
ti - > item ? " true " : " false " ,
ti - > tree ? " true " : " false " ,
( ti - > tree = = ti - > item ) ? " " : " not " ) ;
}
else {
lua_pushstring ( L , " No TreeItem object! " ) ;
}
return 1 ;
}
2013-02-25 22:05:28 +00:00
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int TreeItem__gc ( lua_State * L ) {
2014-02-19 08:22:55 +00:00
TreeItem ti = toTreeItem ( L , 1 ) ;
2008-07-26 23:41:31 +00:00
if ( ! ti ) return 0 ;
if ( ! ti - > expired )
ti - > expired = TRUE ;
else
g_free ( ti ) ;
return 0 ;
}
2015-07-08 19:20:50 +00:00
WSLUA_ATTRIBUTES TreeItem_attributes [ ] = {
WSLUA_ATTRIBUTE_RWREG ( TreeItem , generated ) ,
WSLUA_ATTRIBUTE_RWREG ( TreeItem , hidden ) ,
WSLUA_ATTRIBUTE_RWREG ( TreeItem , len ) ,
WSLUA_ATTRIBUTE_RWREG ( TreeItem , text ) ,
WSLUA_ATTRIBUTE_ROREG ( TreeItem , visible ) ,
{ NULL , NULL , NULL }
} ;
2014-02-19 08:22:55 +00:00
WSLUA_METHODS TreeItem_methods [ ] = {
WSLUA_CLASS_FNREG ( TreeItem , add_packet_field ) ,
WSLUA_CLASS_FNREG ( TreeItem , add ) ,
WSLUA_CLASS_FNREG ( TreeItem , add_le ) ,
WSLUA_CLASS_FNREG ( TreeItem , set_text ) ,
WSLUA_CLASS_FNREG ( TreeItem , append_text ) ,
WSLUA_CLASS_FNREG ( TreeItem , prepend_text ) ,
WSLUA_CLASS_FNREG ( TreeItem , add_expert_info ) ,
2014-03-25 22:11:05 +00:00
WSLUA_CLASS_FNREG ( TreeItem , add_proto_expert_info ) ,
WSLUA_CLASS_FNREG ( TreeItem , add_tvb_expert_info ) ,
2014-02-19 08:22:55 +00:00
WSLUA_CLASS_FNREG ( TreeItem , set_generated ) ,
WSLUA_CLASS_FNREG ( TreeItem , set_hidden ) ,
WSLUA_CLASS_FNREG ( TreeItem , set_len ) ,
2017-04-27 17:06:24 +00:00
WSLUA_CLASS_FNREG ( TreeItem , referenced ) ,
2008-04-25 18:59:20 +00:00
{ NULL , NULL }
2006-09-25 01:09:00 +00:00
} ;
2010-05-12 08:08:01 +00:00
2014-02-19 08:22:55 +00:00
WSLUA_META TreeItem_meta [ ] = {
2015-07-08 19:20:50 +00:00
WSLUA_CLASS_MTREG ( TreeItem , tostring ) ,
2008-04-25 18:59:20 +00:00
{ NULL , NULL }
2006-09-25 01:09:00 +00:00
} ;
2010-10-21 12:41:15 +00:00
int TreeItem_register ( lua_State * L ) {
2009-06-08 19:45:53 +00:00
gint * etts [ ] = { & wslua_ett } ;
2015-08-11 12:08:08 +00:00
wslua_ett = - 1 ; /* Reset to support reload Lua plugins */
2019-01-06 14:03:01 +00:00
WSLUA_REGISTER_CLASS_WITH_ATTRS ( TreeItem ) ;
2009-06-08 19:45:53 +00:00
outstanding_TreeItem = g_ptr_array_new ( ) ;
proto_register_subtree_array ( etts , 1 ) ;
2014-01-31 07:25:42 +00:00
return 0 ;
2006-09-25 01:09:00 +00:00
}
2015-02-13 18:25:19 +00:00
/*
2019-07-26 18:43:17 +00:00
* Editor modelines - https : //www.wireshark.org/tools/modelines.html
2015-02-13 18:25:19 +00:00
*
* Local variables :
* c - basic - offset : 4
* tab - width : 8
* indent - tabs - mode : nil
* End :
*
* vi : set shiftwidth = 4 tabstop = 8 expandtab :
* : indentSize = 4 : tabSize = 8 : noTabs = true :
*/