2006-09-25 01:09:00 +00:00
/*
2007-01-05 15:09:33 +00:00
* wslua_dumper . c
2006-09-25 01:09:00 +00:00
*
* 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 >
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"
2021-04-29 11:23:21 +00:00
# include <wiretap/wtap_opttypes.h>
2021-07-26 00:31:17 +00:00
# include <epan/wmem_scopes.h>
2013-09-13 08:49:38 +00:00
2020-03-30 15:57:16 +00:00
/* WSLUA_MODULE Dumper Saving Capture Files
2014-03-23 15:01:12 +00:00
The classes / functions defined in this module are for using a ` Dumper ` object to
make Wireshark save a capture file to disk . ` Dumper ` represents Wireshark ' s built - in
2021-02-14 06:12:23 +00:00
file format writers ( see the ` wtap_name_to_file_type_subtype ` function ) .
( The ` wtap_filetypes ` table in ` init . lua ` is deprecated , and should
only be used in code that must run on Wireshark 3.4 .3 and earlier 3.4
releases or in Wireshark 3.2 .11 and earlier 3.2 . x releases . )
2014-03-23 15:01:12 +00:00
To have a Lua script create its own file format writer , see the chapter titled
" Custom file format reading/writing " .
*/
2006-10-18 18:45:24 +00:00
2006-09-25 01:09:00 +00:00
# include "wslua.h"
# include <math.h>
2016-04-03 08:58:44 +00:00
WSLUA_CLASS_DEFINE ( PseudoHeader , NOP ) ;
2006-09-25 01:09:00 +00:00
/*
2010-10-21 12:41:15 +00:00
A pseudoheader to be used to save captured frames .
2010-10-21 00:54:58 +00:00
*/
2006-09-25 01:09:00 +00:00
enum lua_pseudoheader_type {
PHDR_NONE ,
PHDR_ETH ,
PHDR_X25 ,
PHDR_ISDN ,
PHDR_ATM ,
PHDR_ASCEND ,
PHDR_P2P ,
PHDR_WIFI ,
PHDR_COSINE ,
PHDR_IRDA ,
PHDR_NETTL ,
PHDR_MTP2 ,
PHDR_K12
} ;
struct lua_pseudo_header {
enum lua_pseudoheader_type type ;
2010-10-21 12:41:15 +00:00
union wtap_pseudo_header * wph ;
2006-09-25 01:09:00 +00:00
} ;
2010-10-21 12:41:15 +00:00
WSLUA_CONSTRUCTOR PseudoHeader_none ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
/*
Creates a " no " pseudoheader .
2010-10-21 00:54:58 +00:00
2012-06-17 16:32:04 +00:00
*/
2013-03-16 08:12:29 +00:00
PseudoHeader ph = ( PseudoHeader ) g_malloc ( sizeof ( struct lua_pseudo_header ) ) ;
2006-09-25 01:09:00 +00:00
ph - > type = PHDR_NONE ;
ph - > wph = NULL ;
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
pushPseudoHeader ( L , ph ) ;
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ;
2012-06-17 16:32:04 +00:00
/* A null pseudoheader */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_CONSTRUCTOR PseudoHeader_eth ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
/*
2014-03-23 15:01:12 +00:00
Creates an ethernet pseudoheader .
2012-06-17 16:32:04 +00:00
*/
2006-09-25 01:09:00 +00:00
2009-05-08 16:40:38 +00:00
# define WSLUA_OPTARG_PseudoHeader_eth_FCSLEN 1 /* The fcs length */
2010-10-21 00:54:58 +00:00
2013-03-16 08:12:29 +00:00
PseudoHeader ph = ( PseudoHeader ) g_malloc ( sizeof ( struct lua_pseudo_header ) ) ;
2006-09-25 01:09:00 +00:00
ph - > type = PHDR_ETH ;
2013-03-16 08:12:29 +00:00
ph - > wph = ( union wtap_pseudo_header * ) g_malloc ( sizeof ( union wtap_pseudo_header ) ) ;
2015-01-22 21:04:15 +00:00
ph - > wph - > eth . fcs_len = ( gint ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_eth_FCSLEN , - 1 ) ;
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
pushPseudoHeader ( L , ph ) ;
2010-10-21 00:54:58 +00:00
2012-06-17 16:32:04 +00:00
WSLUA_RETURN ( 1 ) ; /* The ethernet pseudoheader */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_CONSTRUCTOR PseudoHeader_atm ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
/*
2014-03-23 15:01:12 +00:00
Creates an ATM pseudoheader .
2012-06-17 16:32:04 +00:00
*/
2006-09-25 01:09:00 +00:00
# define WSLUA_OPTARG_PseudoHeader_atm_AAL 1 /* AAL number */
# define WSLUA_OPTARG_PseudoHeader_atm_VPI 2 /* VPI */
# define WSLUA_OPTARG_PseudoHeader_atm_VCI 3 /* VCI */
# define WSLUA_OPTARG_PseudoHeader_atm_CHANNEL 4 /* Channel */
# define WSLUA_OPTARG_PseudoHeader_atm_CELLS 5 /* Number of cells in the PDU */
# define WSLUA_OPTARG_PseudoHeader_atm_AAL5U2U 6 /* AAL5 User to User indicator */
# define WSLUA_OPTARG_PseudoHeader_atm_AAL5LEN 7 /* AAL5 Len */
2013-03-16 08:12:29 +00:00
PseudoHeader ph = ( PseudoHeader ) g_malloc ( sizeof ( struct lua_pseudo_header ) ) ;
2006-09-25 01:09:00 +00:00
ph - > type = PHDR_ATM ;
2013-03-16 08:12:29 +00:00
ph - > wph = ( union wtap_pseudo_header * ) g_malloc ( sizeof ( union wtap_pseudo_header ) ) ;
2015-01-22 21:04:15 +00:00
ph - > wph - > atm . aal = ( guint8 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_atm_AAL , 5 ) ;
ph - > wph - > atm . vpi = ( guint16 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_atm_VPI , 1 ) ;
ph - > wph - > atm . vci = ( guint16 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_atm_VCI , 1 ) ;
ph - > wph - > atm . channel = ( guint16 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_atm_CHANNEL , 0 ) ;
ph - > wph - > atm . cells = ( guint16 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_atm_CELLS , 1 ) ;
ph - > wph - > atm . aal5t_u2u = ( guint16 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_atm_AAL5U2U , 1 ) ;
ph - > wph - > atm . aal5t_len = ( guint16 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_atm_AAL5LEN , 0 ) ;
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
pushPseudoHeader ( L , ph ) ;
2012-06-17 16:32:04 +00:00
WSLUA_RETURN ( 1 ) ;
/* The ATM pseudoheader */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_CONSTRUCTOR PseudoHeader_mtp2 ( lua_State * L ) {
2014-03-23 15:01:12 +00:00
/* Creates an MTP2 PseudoHeader. */
2006-09-25 01:09:00 +00:00
# define WSLUA_OPTARG_PseudoHeader_mtp2_SENT 1 /* True if the packet is sent, False if received. */
2014-03-23 15:01:12 +00:00
# define WSLUA_OPTARG_PseudoHeader_mtp2_ANNEXA 2 /* True if annex A is used. */
# define WSLUA_OPTARG_PseudoHeader_mtp2_LINKNUM 3 /* Link Number. */
2013-03-16 08:12:29 +00:00
PseudoHeader ph = ( PseudoHeader ) g_malloc ( sizeof ( struct lua_pseudo_header ) ) ;
2006-09-25 01:09:00 +00:00
ph - > type = PHDR_MTP2 ;
2013-03-16 08:12:29 +00:00
ph - > wph = ( union wtap_pseudo_header * ) g_malloc ( sizeof ( union wtap_pseudo_header ) ) ;
2015-01-22 21:04:15 +00:00
ph - > wph - > mtp2 . sent = ( guint8 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_mtp2_SENT , 0 ) ;
ph - > wph - > mtp2 . annex_a_used = ( guint8 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_mtp2_ANNEXA , 0 ) ;
ph - > wph - > mtp2 . link_number = ( guint16 ) luaL_optinteger ( L , WSLUA_OPTARG_PseudoHeader_mtp2_LINKNUM , 0 ) ;
2006-09-25 01:09:00 +00:00
pushPseudoHeader ( L , ph ) ;
2012-06-17 16:32:04 +00:00
WSLUA_RETURN ( 1 ) ; /* The MTP2 pseudoheader */
2006-09-25 01:09:00 +00:00
}
#if 0
2010-10-21 12:41:15 +00:00
static int PseudoHeader_x25 ( lua_State * L ) { luaL_error ( L , " not implemented " ) ; return 0 ; }
static int PseudoHeader_isdn ( lua_State * L ) { luaL_error ( L , " not implemented " ) ; return 0 ; }
static int PseudoHeader_ascend ( lua_State * L ) { luaL_error ( L , " not implemented " ) ; return 0 ; }
static int PseudoHeader_wifi ( lua_State * L ) { luaL_error ( L , " not implemented " ) ; return 0 ; }
static int PseudoHeader_cosine ( lua_State * L ) { luaL_error ( L , " not implemented " ) ; return 0 ; }
static int PseudoHeader_irda ( lua_State * L ) { luaL_error ( L , " not implemented " ) ; return 0 ; }
static int PseudoHeader_nettl ( lua_State * L ) { luaL_error ( L , " not implemented " ) ; return 0 ; }
static int PseudoHeader_k12 ( lua_State * L ) { luaL_error ( L , " not implemented " ) ; return 0 ; }
2006-09-25 01:09:00 +00:00
# endif
2013-02-25 22:05:28 +00:00
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int PseudoHeader__gc ( lua_State * L _U_ ) {
/* do NOT free PseudoHeader */
return 0 ;
}
2006-09-25 01:09:00 +00:00
WSLUA_METHODS PseudoHeader_methods [ ] = {
2012-06-17 16:32:04 +00:00
WSLUA_CLASS_FNREG ( PseudoHeader , mtp2 ) ,
WSLUA_CLASS_FNREG ( PseudoHeader , atm ) ,
WSLUA_CLASS_FNREG ( PseudoHeader , eth ) ,
WSLUA_CLASS_FNREG ( PseudoHeader , none ) ,
{ 0 , 0 }
2006-09-25 01:09:00 +00:00
} ;
WSLUA_META PseudoHeader_meta [ ] = {
2012-06-17 16:32:04 +00:00
{ 0 , 0 }
2006-09-25 01:09:00 +00:00
} ;
2010-10-21 12:41:15 +00:00
int PseudoHeader_register ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
WSLUA_REGISTER_CLASS ( PseudoHeader )
2006-09-25 01:09:00 +00:00
return 0 ;
}
2016-04-03 08:58:44 +00:00
WSLUA_CLASS_DEFINE ( Dumper , FAIL_ON_NULL ( " Dumper already closed " ) ) ;
2006-09-25 01:09:00 +00:00
2010-10-21 12:41:15 +00:00
static GHashTable * dumper_encaps = NULL ;
2006-09-25 01:09:00 +00:00
# define DUMPER_ENCAP(d) GPOINTER_TO_INT(g_hash_table_lookup(dumper_encaps,d))
2010-10-21 12:41:15 +00:00
static const char * cross_plat_fname ( const char * fname ) {
2012-06-17 16:32:04 +00:00
static char fname_clean [ 256 ] ;
char * f ;
2021-04-30 10:18:25 +00:00
( void ) g_strlcpy ( fname_clean , fname , 255 ) ;
2012-06-17 16:32:04 +00:00
fname_clean [ 255 ] = ' \0 ' ;
for ( f = fname_clean ; * f ; f + + ) {
switch ( * f ) {
case ' / ' : case ' \\ ' :
* f = * ( G_DIR_SEPARATOR_S ) ;
break ;
default :
break ;
}
}
return fname_clean ;
2007-01-05 15:09:33 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_CONSTRUCTOR Dumper_new ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
/*
Creates a file to write packets .
2014-03-23 15:01:12 +00:00
` Dumper : new_for_current ( ) ` will probably be a better choice .
2012-06-17 16:32:04 +00:00
*/
2014-03-23 15:01:12 +00:00
# define WSLUA_ARG_Dumper_new_FILENAME 1 /* The name of the capture file to be created. */
2021-02-14 06:12:23 +00:00
# define WSLUA_OPTARG_Dumper_new_FILETYPE 2 / * The type of the file to be created - a number returned by `wtap_name_to_file_type_subtype()`.
( The ` wtap_filetypes ` table in ` init . lua `
is deprecated , and should only be used
in code that must run on Wireshark 3.4 .3 and earlier 3.4 releases
or in Wireshark 3.2 .11 and earlier
3.2 . x releases . ) */
2014-03-23 15:01:12 +00:00
# define WSLUA_OPTARG_Dumper_new_ENCAP 3 /* The encapsulation to be used in the file to be created - a number entry from the `wtap_encaps` table in `init.lua`. */
2006-09-25 01:09:00 +00:00
Dumper d ;
2010-10-21 12:41:15 +00:00
const char * fname = luaL_checkstring ( L , WSLUA_ARG_Dumper_new_FILENAME ) ;
2021-02-23 09:18:31 +00:00
int filetype = ( int ) luaL_optinteger ( L , WSLUA_OPTARG_Dumper_new_FILETYPE , wtap_pcap_file_type_subtype ( ) ) ;
2015-01-22 21:04:15 +00:00
int encap = ( int ) luaL_optinteger ( L , WSLUA_OPTARG_Dumper_new_ENCAP , WTAP_ENCAP_ETHERNET ) ;
2006-09-25 01:09:00 +00:00
int err = 0 ;
2020-10-14 01:48:46 +00:00
gchar * err_info = NULL ;
2014-10-12 20:27:54 +00:00
const char * filename = cross_plat_fname ( fname ) ;
2018-11-14 00:10:53 +00:00
wtap_dump_params params = WTAP_DUMP_PARAMS_INIT ;
2010-10-21 00:54:58 +00:00
2018-11-14 00:10:53 +00:00
params . encap = encap ;
2020-10-14 01:48:46 +00:00
d = wtap_dump_open ( filename , filetype , WTAP_UNCOMPRESSED , & params , & err ,
& err_info ) ;
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
if ( ! d ) {
2012-06-17 16:32:04 +00:00
/* WSLUA_ERROR("Error while opening file for writing"); */
2013-04-01 21:39:28 +00:00
switch ( err ) {
2020-08-04 09:18:22 +00:00
case WTAP_ERR_NOT_REGULAR_FILE :
luaL_error ( L , " The file \" %s \" is a \" special file \" or socket or other non-regular file " ,
filename ) ;
break ;
case WTAP_ERR_CANT_WRITE_TO_PIPE :
luaL_error ( L , " The file \" %s \" is a pipe, and %s capture files can't be written to a pipe " ,
2021-02-13 08:03:51 +00:00
filename , wtap_file_type_subtype_description ( filetype ) ) ;
2020-08-04 09:18:22 +00:00
break ;
2014-12-17 08:29:31 +00:00
case WTAP_ERR_UNWRITABLE_FILE_TYPE :
2013-04-01 21:39:28 +00:00
luaL_error ( L , " Files of file type %s cannot be written " ,
2021-02-13 08:03:51 +00:00
wtap_file_type_subtype_description ( filetype ) ) ;
2013-04-01 21:39:28 +00:00
break ;
2014-12-17 06:40:45 +00:00
case WTAP_ERR_UNWRITABLE_ENCAP :
2013-04-01 21:39:28 +00:00
luaL_error ( L , " Files of file type %s don't support encapsulation %s " ,
2021-02-13 08:03:51 +00:00
wtap_file_type_subtype_description ( filetype ) ,
2019-01-09 21:21:10 +00:00
wtap_encap_name ( encap ) ) ;
2013-04-01 21:39:28 +00:00
break ;
2020-08-04 09:18:22 +00:00
case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED :
luaL_error ( L , " Files of file type %s don't support per-packet encapsulation " ,
2021-02-13 08:03:51 +00:00
wtap_file_type_subtype_description ( filetype ) ) ;
2020-08-04 09:18:22 +00:00
break ;
case WTAP_ERR_CANT_OPEN :
luaL_error ( L , " The file \" %s \" could not be created for some unknown reason " ,
filename ) ;
break ;
case WTAP_ERR_SHORT_WRITE :
luaL_error ( L , " A full header couldn't be written to the file \" %s \" . " ,
filename ) ;
break ;
case WTAP_ERR_COMPRESSION_NOT_SUPPORTED :
luaL_error ( L , " Files of file type %s cannot be written as a compressed file " ,
2021-02-13 08:03:51 +00:00
wtap_file_type_subtype_description ( filetype ) ) ;
2020-08-04 09:18:22 +00:00
break ;
2020-10-14 01:48:46 +00:00
case WTAP_ERR_INTERNAL :
luaL_error ( L , " An internal error occurred creating the file \" %s \" (%s) " ,
filename ,
err_info ! = NULL ? err_info : " no information supplied " ) ;
g_free ( err_info ) ;
break ;
2013-04-01 21:39:28 +00:00
default :
2020-08-04 09:18:22 +00:00
luaL_error ( L , " error while opening \" %s \" : %s " ,
2013-04-01 21:39:28 +00:00
filename ,
wtap_strerror ( err ) ) ;
break ;
}
2006-09-25 01:09:00 +00:00
return 0 ;
}
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
g_hash_table_insert ( dumper_encaps , d , GINT_TO_POINTER ( encap ) ) ;
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
pushDumper ( L , d ) ;
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ;
2012-06-17 16:32:04 +00:00
/* The newly created Dumper object */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD Dumper_close ( lua_State * L ) {
2014-03-23 15:01:12 +00:00
/* Closes a dumper. */
2012-06-17 16:32:04 +00:00
Dumper * dp = ( Dumper * ) luaL_checkudata ( L , 1 , " Dumper " ) ;
2006-09-25 01:09:00 +00:00
int err ;
2020-10-14 01:48:46 +00:00
gchar * err_info ;
2010-10-21 00:54:58 +00:00
2014-02-27 04:42:15 +00:00
if ( ! * dp ) {
2012-06-17 16:32:04 +00:00
WSLUA_ERROR ( Dumper_close , " Cannot operate on a closed dumper " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2006-09-25 01:09:00 +00:00
2010-10-21 12:41:15 +00:00
g_hash_table_remove ( dumper_encaps , * dp ) ;
2006-09-25 01:09:00 +00:00
2020-10-14 01:48:46 +00:00
if ( ! wtap_dump_close ( * dp , & err , & err_info ) ) {
if ( err_info ! = NULL ) {
luaL_error ( L , " error closing: %s (%s) " ,
wtap_strerror ( err ) , err_info ) ;
g_free ( err_info ) ;
} else {
luaL_error ( L , " error closing: %s " ,
wtap_strerror ( err ) ) ;
}
2006-09-25 01:09:00 +00:00
}
2012-06-17 16:32:04 +00:00
/* this way if we close a dumper any attempt to use it (for everything but GC) will yield an error */
* dp = NULL ;
2006-09-25 01:09:00 +00:00
return 0 ;
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD Dumper_flush ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
/*
Writes all unsaved data of a dumper to the disk .
*/
2006-09-25 01:09:00 +00:00
Dumper d = checkDumper ( L , 1 ) ;
2020-08-09 21:41:11 +00:00
int err ;
2006-09-25 01:09:00 +00:00
if ( ! d ) return 0 ;
2010-10-21 00:54:58 +00:00
2020-08-09 21:41:11 +00:00
if ( ! wtap_dump_flush ( d , & err ) ) {
luaL_error ( L , " error while dumping: %s " ,
wtap_strerror ( err ) ) ;
}
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
return 0 ;
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD Dumper_dump ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
/*
Dumps an arbitrary packet .
Note : Dumper : dump_current ( ) will fit best in most cases .
*/
2014-03-23 15:01:12 +00:00
# define WSLUA_ARG_Dumper_dump_TIMESTAMP 2 /* The absolute timestamp the packet will have. */
# define WSLUA_ARG_Dumper_dump_PSEUDOHEADER 3 /* The `PseudoHeader` to use. */
2006-09-25 01:09:00 +00:00
# define WSLUA_ARG_Dumper_dump_BYTEARRAY 4 /* the data to be saved */
2012-06-17 16:32:04 +00:00
Dumper d = checkDumper ( L , 1 ) ;
2006-09-25 01:09:00 +00:00
PseudoHeader ph ;
ByteArray ba ;
2018-02-09 00:19:12 +00:00
wtap_rec rec ;
2006-09-25 01:09:00 +00:00
double ts ;
int err ;
2014-12-18 00:02:50 +00:00
gchar * err_info ;
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
if ( ! d ) return 0 ;
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
ts = luaL_checknumber ( L , WSLUA_ARG_Dumper_dump_TIMESTAMP ) ;
ph = checkPseudoHeader ( L , WSLUA_ARG_Dumper_dump_PSEUDOHEADER ) ;
2010-10-21 00:54:58 +00:00
2014-02-27 04:42:15 +00:00
if ( ! ph ) {
2017-08-05 10:29:33 +00:00
WSLUA_ARG_ERROR ( Dumper_dump , PSEUDOHEADER , " need a PseudoHeader " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
ba = checkByteArray ( L , WSLUA_ARG_Dumper_dump_BYTEARRAY ) ;
2010-10-21 00:54:58 +00:00
2014-02-27 04:42:15 +00:00
if ( ! ba ) {
WSLUA_ARG_ERROR ( Dumper_dump , BYTEARRAY , " must be a ByteArray " ) ;
return 0 ;
}
2010-10-21 00:54:58 +00:00
2018-02-09 00:19:12 +00:00
memset ( & rec , 0 , sizeof rec ) ;
2012-07-28 15:51:37 +00:00
2018-02-09 00:19:12 +00:00
rec . rec_type = REC_TYPE_PACKET ;
2014-05-24 18:28:30 +00:00
2018-02-09 00:19:12 +00:00
rec . presence_flags = WTAP_HAS_TS ;
rec . ts . secs = ( unsigned int ) ( floor ( ts ) ) ;
rec . ts . nsecs = ( unsigned int ) ( floor ( ( ts - ( double ) rec . ts . secs ) * 1000000000 ) ) ;
2007-03-21 00:48:46 +00:00
2018-02-09 00:19:12 +00:00
rec . rec_header . packet_header . len = ba - > len ;
rec . rec_header . packet_header . caplen = ba - > len ;
rec . rec_header . packet_header . pkt_encap = DUMPER_ENCAP ( d ) ;
2017-08-05 10:19:02 +00:00
if ( ph - > wph ) {
2018-02-09 00:19:12 +00:00
rec . rec_header . packet_header . pseudo_header = * ph - > wph ;
2017-08-05 10:19:02 +00:00
}
2010-10-21 00:54:58 +00:00
2021-04-29 11:23:21 +00:00
/* TODO: Can we get access to pinfo->rec->block here somehow? We
* should be copying it to pkthdr . pkt_block if we can . */
2012-07-28 15:51:37 +00:00
2018-02-09 00:19:12 +00:00
if ( ! wtap_dump ( d , & rec , ba - > data , & err , & err_info ) ) {
2014-12-18 00:02:50 +00:00
switch ( err ) {
case WTAP_ERR_UNWRITABLE_REC_DATA :
luaL_error ( L , " error while dumping: %s (%s) " ,
wtap_strerror ( err ) , err_info ) ;
g_free ( err_info ) ;
break ;
default :
luaL_error ( L , " error while dumping: %s " ,
wtap_strerror ( err ) ) ;
break ;
}
2006-09-25 01:09:00 +00:00
}
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
return 0 ;
2010-10-21 12:41:15 +00:00
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD Dumper_new_for_current ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
/*
2014-03-23 15:01:12 +00:00
Creates a capture file using the same encapsulation as the one of the current packet .
2012-06-17 16:32:04 +00:00
*/
2006-09-25 01:09:00 +00:00
# define WSLUA_OPTARG_Dumper_new_for_current_FILETYPE 2 /* The file type. Defaults to pcap. */
2012-06-17 16:32:04 +00:00
Dumper d ;
2010-10-21 12:41:15 +00:00
const char * fname = luaL_checkstring ( L , 1 ) ;
2021-02-23 09:18:31 +00:00
int filetype = ( int ) luaL_optinteger ( L , WSLUA_OPTARG_Dumper_new_for_current_FILETYPE , wtap_pcap_file_type_subtype ( ) ) ;
2006-09-25 01:09:00 +00:00
int encap ;
int err = 0 ;
2020-10-14 01:48:46 +00:00
gchar * err_info = NULL ;
2014-10-12 20:27:54 +00:00
const char * filename = cross_plat_fname ( fname ) ;
2018-11-14 00:10:53 +00:00
wtap_dump_params params = WTAP_DUMP_PARAMS_INIT ;
2010-10-21 00:54:58 +00:00
2014-02-27 04:42:15 +00:00
if ( ! lua_pinfo ) {
2012-06-17 16:32:04 +00:00
WSLUA_ERROR ( Dumper_new_for_current , " Cannot be used outside a tap or a dissector " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2010-10-21 00:54:58 +00:00
2018-02-09 00:19:12 +00:00
if ( lua_pinfo - > rec - > rec_type ! = REC_TYPE_PACKET ) {
return 0 ;
}
encap = lua_pinfo - > rec - > rec_header . packet_header . pkt_encap ;
2018-11-14 00:10:53 +00:00
params . encap = encap ;
2020-10-14 01:48:46 +00:00
d = wtap_dump_open ( filename , filetype , WTAP_UNCOMPRESSED , & params , & err ,
& err_info ) ;
2006-09-25 01:09:00 +00:00
2010-10-21 12:41:15 +00:00
if ( ! d ) {
2013-04-01 21:39:28 +00:00
switch ( err ) {
2020-08-04 09:18:22 +00:00
case WTAP_ERR_NOT_REGULAR_FILE :
luaL_error ( L , " The file \" %s \" is a \" special file \" or socket or other non-regular file " ,
filename ) ;
break ;
case WTAP_ERR_CANT_WRITE_TO_PIPE :
luaL_error ( L , " The file \" %s \" is a pipe, and %s capture files can't be written to a pipe " ,
2021-02-13 08:03:51 +00:00
filename , wtap_file_type_subtype_description ( filetype ) ) ;
2020-08-04 09:18:22 +00:00
break ;
2014-12-17 08:29:31 +00:00
case WTAP_ERR_UNWRITABLE_FILE_TYPE :
2013-04-01 21:39:28 +00:00
luaL_error ( L , " Files of file type %s cannot be written " ,
2021-02-13 08:03:51 +00:00
wtap_file_type_subtype_description ( filetype ) ) ;
2013-04-01 21:39:28 +00:00
break ;
2014-12-17 06:40:45 +00:00
case WTAP_ERR_UNWRITABLE_ENCAP :
2013-04-01 21:39:28 +00:00
luaL_error ( L , " Files of file type %s don't support encapsulation %s " ,
2021-02-13 08:03:51 +00:00
wtap_file_type_subtype_description ( filetype ) ,
2019-01-09 21:21:10 +00:00
wtap_encap_name ( encap ) ) ;
2013-04-01 21:39:28 +00:00
break ;
2020-08-04 09:18:22 +00:00
case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED :
luaL_error ( L , " Files of file type %s don't support per-packet encapsulation " ,
2021-02-13 08:03:51 +00:00
wtap_file_type_subtype_description ( filetype ) ) ;
2020-08-04 09:18:22 +00:00
break ;
case WTAP_ERR_CANT_OPEN :
luaL_error ( L , " The file \" %s \" could not be created for some unknown reason " ,
filename ) ;
break ;
case WTAP_ERR_SHORT_WRITE :
luaL_error ( L , " A full header couldn't be written to the file \" %s \" . " ,
filename ) ;
break ;
case WTAP_ERR_COMPRESSION_NOT_SUPPORTED :
luaL_error ( L , " Files of file type %s cannot be written as a compressed file " ,
2021-02-13 08:03:51 +00:00
wtap_file_type_subtype_description ( filetype ) ) ;
2020-08-04 09:18:22 +00:00
break ;
2020-10-14 01:48:46 +00:00
case WTAP_ERR_INTERNAL :
luaL_error ( L , " An internal error occurred creating the file \" %s \" (%s) " ,
filename ,
err_info ! = NULL ? err_info : " no information supplied " ) ;
g_free ( err_info ) ;
break ;
2013-04-01 21:39:28 +00:00
default :
2020-08-04 09:18:22 +00:00
luaL_error ( L , " error while opening \" %s \" : %s " ,
2013-04-01 21:39:28 +00:00
filename ,
wtap_strerror ( err ) ) ;
break ;
}
2006-09-25 01:09:00 +00:00
return 0 ;
}
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
pushDumper ( L , d ) ;
WSLUA_RETURN ( 1 ) ; /* The newly created Dumper Object */
2010-10-21 12:41:15 +00:00
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD Dumper_dump_current ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
/*
Dumps the current packet as it is .
*/
2006-09-25 01:09:00 +00:00
Dumper d = checkDumper ( L , 1 ) ;
2018-02-09 00:19:12 +00:00
wtap_rec rec ;
2010-10-21 12:41:15 +00:00
const guchar * data ;
tvbuff_t * tvb ;
2012-10-20 19:54:56 +00:00
struct data_source * data_src ;
2006-09-25 01:09:00 +00:00
int err = 0 ;
2014-12-18 00:02:50 +00:00
gchar * err_info ;
2010-10-21 00:54:58 +00:00
2006-09-25 01:09:00 +00:00
if ( ! d ) return 0 ;
2010-10-21 00:54:58 +00:00
2014-02-27 04:42:15 +00:00
if ( ! lua_pinfo ) {
WSLUA_ERROR ( Dumper_new_for_current , " Cannot be used outside a tap or a dissector " ) ;
return 0 ;
}
2006-09-25 01:09:00 +00:00
2018-02-09 00:19:12 +00:00
if ( lua_pinfo - > rec - > rec_type ! = REC_TYPE_PACKET ) {
return 0 ;
}
2012-10-20 19:54:56 +00:00
data_src = ( struct data_source * ) ( lua_pinfo - > data_src - > data ) ;
2009-08-15 06:38:10 +00:00
if ( ! data_src )
return 0 ;
2012-10-20 19:54:56 +00:00
tvb = get_data_source_tvb ( data_src ) ;
2006-09-25 01:09:00 +00:00
2018-02-09 00:19:12 +00:00
memset ( & rec , 0 , sizeof rec ) ;
2012-07-28 15:51:37 +00:00
2018-02-09 00:19:12 +00:00
rec . rec_type = REC_TYPE_PACKET ;
rec . presence_flags = WTAP_HAS_TS | WTAP_HAS_CAP_LEN ;
rec . ts = lua_pinfo - > abs_ts ;
rec . rec_header . packet_header . len = tvb_reported_length ( tvb ) ;
rec . rec_header . packet_header . caplen = tvb_captured_length ( tvb ) ;
rec . rec_header . packet_header . pkt_encap = lua_pinfo - > rec - > rec_header . packet_header . pkt_encap ;
rec . rec_header . packet_header . pseudo_header = * lua_pinfo - > pseudo_header ;
2006-09-25 01:09:00 +00:00
2019-01-24 11:08:57 +00:00
/*
2021-04-29 11:23:21 +00:00
* wtap_dump does not modify rec . block , so it should be possible to
2021-07-08 05:43:29 +00:00
* pass epan_get_modified_block ( ) or lua_pinfo - > rec - > block directly .
2019-01-24 11:08:57 +00:00
* Temporarily duplicating the memory should not hurt though .
*/
2021-07-08 05:43:29 +00:00
if ( lua_pinfo - > fd - > has_modified_block ) {
rec . block = epan_get_modified_block ( lua_pinfo - > epan , lua_pinfo - > fd ) ;
rec . block_was_modified = TRUE ;
2021-08-30 02:12:13 +00:00
} else {
2021-04-29 11:23:21 +00:00
rec . block = lua_pinfo - > rec - > block ;
2017-06-01 08:34:25 +00:00
}
2012-07-28 15:51:37 +00:00
2021-09-14 14:19:10 +00:00
data = ( const guchar * ) tvb_memdup ( lua_pinfo - > pool , tvb , 0 , rec . rec_header . packet_header . caplen ) ;
2010-10-21 00:54:58 +00:00
2018-02-09 00:19:12 +00:00
if ( ! wtap_dump ( d , & rec , data , & err , & err_info ) ) {
2014-12-18 00:02:50 +00:00
switch ( err ) {
case WTAP_ERR_UNWRITABLE_REC_DATA :
luaL_error ( L , " error while dumping: %s (%s) " ,
wtap_strerror ( err ) , err_info ) ;
g_free ( err_info ) ;
break ;
default :
luaL_error ( L , " error while dumping: %s " ,
wtap_strerror ( err ) ) ;
break ;
}
2006-09-25 01:09:00 +00:00
}
return 0 ;
}
2013-02-25 22:05:28 +00:00
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
2010-10-21 12:41:15 +00:00
static int Dumper__gc ( lua_State * L ) {
2012-06-17 16:32:04 +00:00
Dumper * dp = ( Dumper * ) luaL_checkudata ( L , 1 , " Dumper " ) ;
int err ;
2020-10-14 01:48:46 +00:00
gchar * err_info ;
2006-09-25 01:09:00 +00:00
2012-06-17 16:32:04 +00:00
/* If we are Garbage Collected it means the Dumper is no longer usable. Close it */
2010-10-21 00:54:58 +00:00
2012-06-17 16:32:04 +00:00
if ( ! * dp )
return 0 ; /* already closed, nothing to do! */
2006-09-25 01:09:00 +00:00
2012-06-17 16:32:04 +00:00
g_hash_table_remove ( dumper_encaps , * dp ) ;
2006-09-25 01:09:00 +00:00
2020-10-14 01:48:46 +00:00
if ( ! wtap_dump_close ( * dp , & err , & err_info ) ) {
if ( err_info ! = NULL ) {
luaL_error ( L , " error closing: %s (%s) " ,
wtap_strerror ( err ) , err_info ) ;
g_free ( err_info ) ;
} else {
luaL_error ( L , " error closing: %s " ,
wtap_strerror ( err ) ) ;
}
2012-06-17 16:32:04 +00:00
}
2006-09-25 01:09:00 +00:00
2012-06-17 16:32:04 +00:00
return 0 ;
2006-09-25 01:09:00 +00:00
}
WSLUA_METHODS Dumper_methods [ ] = {
2013-02-15 01:38:45 +00:00
WSLUA_CLASS_FNREG ( Dumper , new ) ,
WSLUA_CLASS_FNREG ( Dumper , new_for_current ) ,
WSLUA_CLASS_FNREG ( Dumper , close ) ,
WSLUA_CLASS_FNREG ( Dumper , flush ) ,
WSLUA_CLASS_FNREG ( Dumper , dump ) ,
WSLUA_CLASS_FNREG ( Dumper , dump_current ) ,
2014-02-19 08:22:55 +00:00
{ NULL , NULL }
2006-09-25 01:09:00 +00:00
} ;
WSLUA_META Dumper_meta [ ] = {
2014-02-19 08:22:55 +00:00
{ NULL , NULL }
2006-09-25 01:09:00 +00:00
} ;
2010-10-21 12:41:15 +00:00
int Dumper_register ( lua_State * L ) {
2006-09-25 01:09:00 +00:00
dumper_encaps = g_hash_table_new ( g_direct_hash , g_direct_equal ) ;
WSLUA_REGISTER_CLASS ( Dumper ) ;
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 :
*/