6
6
* (c) 2006, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
8
* $Id: wslua_tvb.c 19676 2006-10-24 22:21:04Z gerald $
8
* $Id: wslua_tvb.c 20628 2007-01-30 19:19:21Z gerald $
10
10
* Wireshark - Network traffic analyzer
11
11
* By Gerald Combs <gerald@wireshark.org>
43
43
if (lua_gettop(L) == 1) {
44
44
s = luaL_checkstring(L,WSLUA_OPTARG_ByteArray_new_HEXBYTES);
47
47
WSLUA_OPTARG_ERROR(ByteArray_new,HEXBYTES,"must be a string");
52
52
for (; (c = *s); s++) {
81
81
ByteArray ba = checkByteArray(L,1);
85
85
g_byte_array_free(ba,TRUE);
89
89
WSLUA_METAMETHOD ByteArray__concat(lua_State* L) {
90
90
/* concatenate two ByteArrays */
91
#define WSLUA_ARG_ByteArray__cat_FIRST 1
92
#define WSLUA_ARG_ByteArray__cat_SECOND 1
91
#define WSLUA_ARG_ByteArray__cat_FIRST 1 /* first array */
92
#define WSLUA_ARG_ByteArray__cat_SECOND 1 /* second array */
94
94
ByteArray ba = checkByteArray(L,1);
95
95
ByteArray ba2 = checkByteArray(L,2);
97
97
if (! (ba && ba2) )
98
98
WSLUA_ERROR(ByteArray__cat,"both arguments must be ByteArrays");
100
100
g_byte_array_append(ba,ba2->data,ba2->len);
102
102
pushByteArray(L,ba);
106
106
WSLUA_METHOD ByteArray_prepend(lua_State* L) {
107
107
/* prepend a ByteArray to this ByteArray */
108
#define WSLUA_ARG_ByteArray_prepend_PREPENDED 2
108
#define WSLUA_ARG_ByteArray_prepend_PREPENDED 2 /* array to be prepended */
109
109
ByteArray ba = checkByteArray(L,1);
110
110
ByteArray ba2 = checkByteArray(L,2);
112
112
if (! (ba && ba2) )
113
113
WSLUA_ERROR(ByteArray_prepend,"both arguments must be ByteArrays");
115
115
g_byte_array_prepend(ba,ba2->data,ba2->len);
117
117
pushByteArray(L,ba);
121
121
WSLUA_METHOD ByteArray_append(lua_State* L) {
122
122
/* append a ByteArray to this ByteArray */
123
#define WSLUA_ARG_ByteArray_append_APPENDED 2
123
#define WSLUA_ARG_ByteArray_append_APPENDED 2 /* array to be appended */
124
124
ByteArray ba = checkByteArray(L,1);
125
125
ByteArray ba2 = checkByteArray(L,2);
127
127
if (! (ba && ba2) )
128
128
WSLUA_ERROR(ByteArray_prepend,"both arguments must be ByteArrays");
130
130
g_byte_array_prepend(ba,ba2->data,ba2->len);
132
132
pushByteArray(L,ba);
136
136
WSLUA_METHOD ByteArray_set_size(lua_State* L) {
137
137
/* Sets the size of a ByteArray, either truncating it or filling it with zeros. */
138
#define WSLUA_ARG_ByteArray_set_size_SIZE 2
138
#define WSLUA_ARG_ByteArray_set_size_SIZE 2 /* new size of the array*/
140
140
ByteArray ba = checkByteArray(L,1);
141
141
int siz = luaL_checkint(L,2);
153
153
ByteArray ba = checkByteArray(L,1);
154
154
int idx = luaL_checkint(L,2);
155
155
int v = luaL_checkint(L,3);
157
157
if (!ba) return 0;
159
159
if (idx == 0 && ! g_str_equal(luaL_optstring(L,2,""),"0") ) {
160
160
luaL_argerror(L,2,"bad index");
164
164
if (idx < 0 || (guint)idx >= ba->len) {
165
165
luaL_argerror(L,2,"index out of range");
182
182
#define WSLUA_ARG_ByteArray_set_index_INDEX 2 /* the position of the byte to be set */
183
183
ByteArray ba = checkByteArray(L,1);
184
184
int idx = luaL_checkint(L,2);
186
186
if (!ba) return 0;
188
188
if (idx == 0 && ! g_str_equal(luaL_optstring(L,2,""),"0") ) {
189
189
luaL_argerror(L,2,"bad index");
193
193
if (idx < 0 || (guint)idx >= ba->len) {
194
194
luaL_argerror(L,2,"index out of range");
197
197
lua_pushnumber(L,ba->data[idx]);
199
199
WSLUA_RETURN(1); /* The value [0-255] of the byte. */
202
202
WSLUA_METHOD ByteArray_len(lua_State* L) {
203
203
/* obtain the length of a ByteArray */
204
204
ByteArray ba = checkByteArray(L,1);
206
206
if (!ba) return 0;
208
208
lua_pushnumber(L,(lua_Number)ba->len);
210
210
WSLUA_RETURN(1); /* The length of the ByteArray. */
257
257
ByteArray ba = checkByteArray(L,1);
261
261
if (!ba) return 0;
263
263
s = g_string_new("");
265
265
for (i = 0; i < (int)ba->len; i++) {
266
266
g_string_append(s,byte_to_str[(ba->data)[i]]);
269
269
lua_pushstring(L,s->str);
270
270
g_string_free(s,TRUE);
272
272
WSLUA_RETURN(1); /* a string contaning a representaion of the ByteArray. */
307
* a Tvb represents a tvbuff_t in Lua.
308
* a TvbRange represents a range in a tvb (tvb,offset,lenght) it's main purpose is to do bounds checking,
307
* a Tvb represents a tvbuff_t in Lua.
308
* a TvbRange represents a range in a tvb (tvb,offset,lenght) it's main purpose is to do bounds checking,
309
309
* it helps too simplifing argument passing to Tree. In wireshark terms this is worthless nothing
310
310
* not already done by the TVB itself. In lua's terms is necessary to avoid abusing TRY{}CATCH(){}
311
* via preemptive bounds checking.
311
* via preemptive bounds checking.
313
313
* These lua objects have to be "NULLified after use", that is, we cannot leave pointers in the
314
* lua machine to a tvb or a tvbr that might exist anymore.
314
* lua machine to a tvb or a tvbr that might exist anymore.
316
* To do so we are going to keep a pointer to every "box" in which lua has placed a pointer to our object
316
* To do so we are going to keep a pointer to every "box" in which lua has placed a pointer to our object
317
317
* and then NULLify the object lua points to.
319
319
* Other than that we are going to check every instance of a potentialy NULLified object before using it
324
324
/* a Tvb represents the packet's buffer. It is passed as an argument to listeners and dissectors,
325
325
and can be used to extract information (via TvbRange) from the packet's data. Beware that Tvbs are usable only by the current
326
326
listener or dissector call and are destroyed as soon as the listener/dissector returns, so references
327
to them are unusable once the function has returned.
327
to them are unusable once the function has returned.
328
328
To create a tvbrange the tvb must be called with offset and length as optional arguments ( the offset defaults to 0 and the length to tvb:len() )*/
330
330
static GPtrArray* outstanding_stuff = NULL;
358
358
const gchar* name = luaL_optstring(L,2,"Unnamed") ;
362
362
if (!ba) return 0;
365
365
luaL_error(L,"Tvbs can only be created and used in dissectors");
369
369
data = g_memdup(ba->data, ba->len);
371
371
tvb = tvb_new_real_data(data, ba->len,ba->len);
372
372
tvb_set_free_cb(tvb, g_free);
374
374
add_new_data_source(lua_pinfo, tvb, name);
376
376
WSLUA_RETURN(1); /* the created Tvb. */
381
381
#define WSLUA_ARG_Tvb_new_subset_RANGE 2 /* the TvbRange from which to create the new Tvb. */
383
383
TvbRange tvbr = checkTvbRange(L,1);
385
385
if (! tvbr) return 0;
387
387
if (tvb_offset_exists(tvbr->tvb, tvbr->offset + tvbr->len -1 )) {
388
388
PUSH_TVB(L, tvb_new_subset(tvbr->tvb,tvbr->offset,tvbr->len, tvbr->len) );
411
411
WSLUA_METHOD Tvb_len(lua_State* L) {
412
412
/* obtain the length of a TVB */
413
413
Tvb tvb = checkTvb(L,1);
415
415
if (!tvb) return 0;
417
417
lua_pushnumber(L,tvb_length(tvb));
418
418
WSLUA_RETURN(1); /* the lenght of the Tvb. */
421
421
WSLUA_METHOD Tvb_offset(lua_State* L) {
422
422
/* returns the raw offset (from the beginning of the source Tvb) of a sub Tvb. */
423
423
Tvb tvb = checkTvb(L,1);
425
425
if (!tvb) return 0;
427
427
lua_pushnumber(L,TVB_RAW_OFFSET(tvb));
428
428
WSLUA_RETURN(1); /* the raw offset of the Tvb. */
432
static const luaL_reg Tvb_methods[] = {
434
{"offset", Tvb_offset},
438
static int Tvb_range(lua_State* L);
440
static const luaL_reg Tvb_meta[] = {
441
{"__call", Tvb_range},
442
{"__tostring", Tvb__tostring},
446
int Tvb_register(lua_State* L) {
447
WSLUA_REGISTER_CLASS(Tvb);
432
#if USED_FOR_DOC_PURPOSES
433
WSLUA_METAMETHOD Tvb__call(lua_State* L) {
434
/* equivalent to tvb:range(...) */
451
439
WSLUA_CLASS_DEFINE(TvbRange,FAIL_ON_NULL("expired tvbrange"),NOP);
457
445
TvbRange new_TvbRange(lua_State* L, tvbuff_t* tvb, int offset, int len) {
461
449
len = tvb_length_remaining(tvb,offset);
463
451
luaL_error(L,"out of bounds");
466
454
} else if ( (guint)(len + offset) > tvb_length(tvb)) {
467
455
luaL_error(L,"Range is out of bounds");
471
459
tvbr = ep_alloc(sizeof(struct _wslua_tvbrange));
473
461
tvbr->offset = offset;
479
468
WSLUA_METHOD Tvb_range(lua_State* L) {
480
469
/* creates a tvbr from this Tvb. This is used also as the Tvb:__call() metamethod. */
481
470
#define WSLUA_OPTARG_Tvb_range_OFFSET 2 /* The offset (in octets) from the begining of the Tvb. Defaults to 0. */
482
471
#define WSLUA_OPTARG_Tvb_range_LENGTH 2 /* The length (in octets) of the range. Defaults to until the end of the Tvb. */
484
473
Tvb tvb = checkTvb(L,1);
485
474
int offset = luaL_optint(L,2,0);
486
475
int len = luaL_optint(L,3,-1);
492
481
PUSH_TVBRANGE(L,tvbr);
493
482
WSLUA_RETURN(1); /* the TvbRange */
488
static const luaL_reg Tvb_methods[] = {
489
{"range", Tvb_range},
491
{"offset", Tvb_offset},
495
static int Tvb_range(lua_State* L);
497
static const luaL_reg Tvb_meta[] = {
498
{"__call", Tvb_range},
499
{"__tostring", Tvb__tostring},
503
int Tvb_register(lua_State* L) {
504
WSLUA_REGISTER_CLASS(Tvb);
501
509
* read access to tvbr's data
503
511
static int TvbRange_get_index(lua_State* L) {
504
/* WSLUA_ATTRIBUTE TvbRange_tvb RO The Tvb from which this TvbRange was generated */
505
/* WSLUA_ATTRIBUTE TvbRange_len RW The lenght (in octets) of this TvbRange */
512
/* WSLUA_ATTRIBUTE TvbRange_tvb RO The Tvb from which this TvbRange was generated */
513
/* WSLUA_ATTRIBUTE TvbRange_len RW The lenght (in octets) of this TvbRange */
506
514
/* WSLUA_ATTRIBUTE TvbRange_offset RW The offset (in octets) of this TvbRange */
508
516
TvbRange tvbr = checkTvbRange(L,1);
509
517
const gchar* index = luaL_checkstring(L,2);
511
519
if (!(tvbr && index)) return 0;
513
521
if (g_str_equal(index,"offset")) {
514
522
lua_pushnumber(L,(lua_Number)tvbr->offset);
534
542
const gchar* index = luaL_checkstring(L,2);
536
544
if (!tvbr) return 0;
538
546
if (g_str_equal(index,"offset")) {
539
547
int offset = (int)lua_tonumber(L,3);
541
549
if ( (guint)(tvbr->len + offset) > tvb_length(tvbr->tvb)) {
542
550
luaL_error(L,"out of bounds");
549
557
} else if (g_str_equal(index,"len")) {
550
558
int len = (int)lua_tonumber(L,3);
552
560
if ( (guint)(tvbr->offset + len) > tvb_length(tvbr->tvb)) {
553
561
luaL_error(L,"out of bounds");
573
581
There's no support yet for 64 bit integers*/
574
582
TvbRange tvbr = checkTvbRange(L,1);
575
583
if (!tvbr) return 0;
577
585
switch (tvbr->len) {
579
587
lua_pushnumber(L,tvb_get_guint8(tvbr->tvb,tvbr->offset));
636
644
/* get a Big Endian (network order) floating point number from a TvbRange. The range must be 4 or 8 octets long. */
637
645
TvbRange tvbr = checkTvbRange(L,1);
638
646
if (!tvbr) return 0;
640
648
switch (tvbr->len) {
642
650
lua_pushnumber(L,(double)tvb_get_ntohieee_float(tvbr->tvb,tvbr->offset));
657
665
/* get a Little Endian floating point number from a TvbRange. The range must be 4 or 8 octets long. */
658
666
TvbRange tvbr = checkTvbRange(L,1);
659
667
if (!tvbr) return 0;
661
669
switch (tvbr->len) {
663
671
lua_pushnumber(L,tvb_get_letohieee_float(tvbr->tvb,tvbr->offset));
677
685
TvbRange tvbr = checkTvbRange(L,1);
679
687
guint32* ip_addr;
681
689
if ( !tvbr ) return 0;
684
692
WSLUA_ERROR(TvbRange_get_ipv4,"The range must be 4 octets long");
686
694
addr = g_malloc(sizeof(address));
688
696
ip_addr = g_malloc(sizeof(guint32));
689
697
*ip_addr = tvb_get_ntohl(tvbr->tvb,tvbr->offset);
691
SET_ADDRESS(addr, AT_IPv4, 4, ip_addr);
699
SET_ADDRESS(addr, AT_IPv4, 4, ip_addr);
692
700
pushAddress(L,addr);
694
702
WSLUA_RETURN(1); /* the IPv4 Address */
699
707
TvbRange tvbr = checkTvbRange(L,1);
703
711
if ( !tvbr ) return 0;
705
713
addr = g_malloc(sizeof(address));
707
715
if (tvbr->len != 6)
708
716
WSLUA_ERROR(TvbRange_get_ether,"The range must be 6 bytes long");
710
718
buff = tvb_memdup(tvbr->tvb,tvbr->offset,tvbr->len);
712
SET_ADDRESS(addr, AT_ETHER, 6, buff);
720
SET_ADDRESS(addr, AT_ETHER, 6, buff);
713
721
pushAddress(L,addr);
715
723
WSLUA_RETURN(1); /* the Ethernet Address */
719
727
WSLUA_METHOD TvbRange_get_string(lua_State* L) {
720
728
/* obtain a string from a TvbRange */
721
729
TvbRange tvbr = checkTvbRange(L,1);
723
731
if ( !tvbr ) return 0;
725
733
lua_pushstring(L, (gchar*)tvb_get_ephemeral_string(tvbr->tvb,tvbr->offset,tvbr->len) );
727
735
WSLUA_RETURN(1); /* the string */
731
739
/* obtain a ByteArray */
732
740
TvbRange tvbr = checkTvbRange(L,1);
735
743
if ( !tvbr ) return 0;
737
745
ba = g_byte_array_new();
738
746
g_byte_array_append(ba,ep_tvb_memdup(tvbr->tvb,tvbr->offset,tvbr->len),tvbr->len);
740
748
pushByteArray(L,ba);
742
750
WSLUA_RETURN(1); /* the ByteArray */