WSLUA_METHOD Tvb_raw(lua_State* L) { /* Obtain a Lua string of the binary bytes in a Tvb. */ #define WSLUA_OPTARG_Tvb_raw_OFFSET 2 /* The position of the first byte (default=0/first). */ #define WSLUA_OPTARG_Tvb_raw_LENGTH 3 /* The length of the segment to get (default=all). */ Tvb tvb = checkTvb(L,1); int offset = luaL_optint(L,WSLUA_OPTARG_Tvb_raw_OFFSET,0); int len = luaL_optint(L,WSLUA_OPTARG_Tvb_raw_LENGTH,-1); if (!tvb) return 0; if (tvb->expired) { luaL_error(L,"expired tvb"); return 0; } if ((guint)offset > tvb_length(tvb->ws_tvb)) { WSLUA_OPTARG_ERROR(Tvb_raw,OFFSET,"offset beyond end of Tvb"); return 0; } if (len == -1) { len = tvb_length_remaining(tvb->ws_tvb,offset); if (len < 0) { luaL_error(L,"out of bounds"); return FALSE; } } else if ( (guint)(len + offset) > tvb_length(tvb->ws_tvb)) { luaL_error(L,"Range is out of bounds"); return FALSE; } lua_pushlstring(L, tvb_get_ptr(tvb->ws_tvb, offset, len), len); WSLUA_RETURN(1); /* A Lua string of the binary bytes in the Tvb. */ }
WSLUA_FUNCTION wslua_register_menu(lua_State* L) { /* Register a menu item in one of the main menus. */ #define WSLUA_ARG_register_menu_NAME 1 /* The name of the menu item. The submenus are to be separated by '/'s. (string) */ #define WSLUA_ARG_register_menu_ACTION 2 /* The function to be called when the menu item is invoked. (function taking no arguments and returning nothing) */ #define WSLUA_OPTARG_register_menu_GROUP 3 /* The menu group into which the menu item is to be inserted. If omitted, defaults to MENU_STAT_GENERIC. One of MENU_STAT_UNSORTED (Statistics), MENU_STAT_GENERIC (Statistics, first section), MENU_STAT_CONVERSATION (Statistics/Conversation List), MENU_STAT_ENDPOINT (Statistics/Endpoint List), MENU_STAT_RESPONSE (Statistics/Service Response Time), MENU_STAT_TELEPHONY (Telephony), MENU_ANALYZE (Analyze), MENU_ANALYZE_CONVERSATION (Analyze/Conversation Filter), MENU_TOOLS_UNSORTED (Tools). (number) */ const gchar* name = luaL_checkstring(L,WSLUA_ARG_register_menu_NAME); struct _lua_menu_data* md; gboolean retap = FALSE; register_stat_group_t group = (int)luaL_optnumber(L,WSLUA_OPTARG_register_menu_GROUP,REGISTER_STAT_GROUP_GENERIC); if ( group > REGISTER_TOOLS_GROUP_UNSORTED) WSLUA_OPTARG_ERROR(register_menu,GROUP,"Must be a defined MENU_* (see init.lua)"); if(!name) WSLUA_ARG_ERROR(register_menu,NAME,"Must be a string"); if (!lua_isfunction(L,WSLUA_ARG_register_menu_ACTION)) WSLUA_ARG_ERROR(register_menu,ACTION,"Must be a function"); md = g_malloc(sizeof(struct _lua_menu_data)); md->L = L; lua_pushvalue(L, 2); md->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); lua_remove(L,2); funnel_register_menu(name, group, lua_menu_callback, md, retap); WSLUA_RETURN(0); }
WSLUA_METHOD TvbRange_bytes(lua_State* L) { /* Obtain a `ByteArray` from a `TvbRange`. Starting in 1.11.4, this function also takes an optional `encoding` argument, which can be set to `ENC_STR_HEX` to decode a hex-string from the `TvbRange` into the returned `ByteArray`. The `encoding` can be bitwise-or'ed with one or more separator encodings, such as `ENC_SEP_COLON`, to allow separators to occur between each pair of hex characters. The return value also now returns the number of bytes used as a second return value. On failure or error, nil is returned for both return values. @note The encoding type of the hex string should also be set, for example `ENC_ASCII` or `ENC_UTF_8`, along with `ENC_STR_HEX`. */ #define WSLUA_OPTARG_TvbRange_bytes_ENCODING 2 /* An optional ENC_* encoding value to use */ TvbRange tvbr = checkTvbRange(L,1); GByteArray* ba; const guint encoding = (guint)luaL_optinteger(L, WSLUA_OPTARG_TvbRange_bytes_ENCODING, 0); if ( !(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { luaL_error(L,"expired tvb"); return 0; } if (encoding == 0) { ba = g_byte_array_new(); g_byte_array_append(ba,(const guint8 *)tvb_memdup(wmem_packet_scope(),tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len),tvbr->len); pushByteArray(L,ba); lua_pushinteger(L, tvbr->len); } else if ((encoding & ENC_STR_HEX) == 0) { WSLUA_OPTARG_ERROR(TvbRange_nstime, ENCODING, "invalid encoding value"); } else { gint endoff = 0; GByteArray* retval; ba = g_byte_array_new(); retval = tvb_get_string_bytes(tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len, encoding, ba, &endoff); if (!retval || endoff == 0) { g_byte_array_free(ba, TRUE); /* push nil nstime and offset */ lua_pushnil(L); lua_pushnil(L); } else { pushByteArray(L,ba); lua_pushinteger(L, endoff); } } WSLUA_RETURN(2); /* The `ByteArray` object or nil, and number of bytes consumed or nil. */ }
WSLUA_METHOD TvbRange_nstime(lua_State* L) { /* Obtain a time_t structure from a `TvbRange`, as an `NSTime` object. */ #define WSLUA_OPTARG_TvbRange_nstime_ENCODING 2 /* An optional ENC_* encoding value to use */ TvbRange tvbr = checkTvbRange(L,1); NSTime nstime; const guint encoding = (guint) luaL_optinteger(L, WSLUA_OPTARG_TvbRange_nstime_ENCODING, 0); if ( !(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { luaL_error(L,"expired tvb"); return 0; } nstime = g_new(nstime_t,1); if (encoding == 0) { if (tvbr->len == 4) { nstime->secs = tvb_get_ntohl(tvbr->tvb->ws_tvb, tvbr->offset); nstime->nsecs = 0; } else if (tvbr->len == 8) { nstime->secs = tvb_get_ntohl(tvbr->tvb->ws_tvb, tvbr->offset); nstime->nsecs = tvb_get_ntohl(tvbr->tvb->ws_tvb, tvbr->offset + 4); } else { g_free(nstime); WSLUA_ERROR(TvbRange_nstime,"The range must be 4 or 8 bytes long"); return 0; } pushNSTime(L, nstime); lua_pushinteger(L, tvbr->len); } else if (encoding & ~ENC_STR_TIME_MASK) { WSLUA_OPTARG_ERROR(TvbRange_nstime, ENCODING, "invalid encoding value"); } else { gint endoff = 0; nstime_t *retval = tvb_get_string_time(tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len, encoding, nstime, &endoff); if (!retval || endoff == 0) { g_free(nstime); /* push nil nstime and offset */ lua_pushnil(L); lua_pushnil(L); } else { pushNSTime(L, nstime); lua_pushinteger(L, endoff); } } WSLUA_RETURN(2); /* The `NSTime` object and number of bytes used, or nil on failure. */ }
WSLUA_CONSTRUCTOR ByteArray_new(lua_State* L) { /* Creates a ByteArray Object */ #define WSLUA_OPTARG_ByteArray_new_HEXBYTES 1 /* A string consisting of hexadecimal bytes like "00 B1 A2" or "1a2b3c4d" */ GByteArray* ba = g_byte_array_new(); const gchar* s; int nibble[2]; int i = 0; gchar c; if (lua_gettop(L) == 1) { s = luaL_checkstring(L,WSLUA_OPTARG_ByteArray_new_HEXBYTES); if (!s) WSLUA_OPTARG_ERROR(ByteArray_new,HEXBYTES,"must be a string"); /* XXX: slow! */ for (; (c = *s); s++) { switch(c) { case '0': case '1': case '2': case '3': case '4': case '5' : case '6' : case '7': case '8' : case '9' : nibble[(i++)%2] = c - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f' : nibble[(i++)%2] = c - 'a' + 0xa; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F' : nibble[(i++)%2] = c - 'A' + 0xa; break; default: break; } if ( i == 2 ) { guint8 b = (guint8)(nibble[0] * 16 + nibble[1]); g_byte_array_append(ba,&b,1); i = 0; } } } pushByteArray(L,ba); WSLUA_RETURN(1); /* The new ByteArray object. */ }
WSLUA_CONSTRUCTOR ByteArray_new(lua_State* L) { /* Creates a ByteArray Object */ #define WSLUA_OPTARG_ByteArray_new_HEXBYTES 1 /* A string consisting of hexadecimal bytes like "00 B1 A2" or "1a2b3c4d" */ #define WSLUA_OPTARG_ByteArray_new_SEPARATOR 2 /* A string separator between hex bytes/words (default=" "); or if the boolean value 'true' is used, then the first arg is treated as raw binary data */ GByteArray* ba = g_byte_array_new(); const gchar* s; size_t len = 0; const gchar* sep = " "; gboolean ishex = TRUE; if (lua_gettop(L) >= 1) { s = luaL_checklstring(L,WSLUA_OPTARG_ByteArray_new_HEXBYTES,&len); if (!s) { WSLUA_OPTARG_ERROR(ByteArray_new,HEXBYTES,"must be a string"); return 0; } if (lua_gettop(L) >= 2) { if (lua_type(L,2) == LUA_TBOOLEAN && lua_toboolean(L,2)) { ishex = FALSE; } else { sep = luaL_optstring(L,WSLUA_OPTARG_ByteArray_new_SEPARATOR," "); } } if (ishex) { wslua_hex2bin(L, s, (guint)len, sep); /* this pushes a new string on top of stack */ s = luaL_checklstring(L, -1, &len); /* get the new binary string */ g_byte_array_append(ba,s,(guint)len); /* copy it into ByteArray */ lua_pop(L,1); /* pop the newly created string */ } else { g_byte_array_append(ba,s,(guint)len); } } pushByteArray(L,ba); WSLUA_RETURN(1); /* The new ByteArray object. */ }
WSLUA_METHOD ByteArray_raw(lua_State* L) { /* Obtain a Lua string of the binary bytes in a ByteArray. */ #define WSLUA_OPTARG_ByteArray_raw_OFFSET 2 /* The position of the first byte (default=0/first). */ #define WSLUA_OPTARG_ByteArray_raw_LENGTH 3 /* The length of the segment to get (default=all). */ ByteArray ba = checkByteArray(L,1); guint offset = (guint) luaL_optint(L,WSLUA_OPTARG_ByteArray_raw_OFFSET,0); int len; if (!ba) return 0; if (offset > ba->len) { WSLUA_OPTARG_ERROR(ByteArray_raw,OFFSET,"offset beyond end of byte array"); return 0; } len = luaL_optint(L,WSLUA_OPTARG_ByteArray_raw_LENGTH, ba->len - offset); if ((len < 0) || ((guint)len > (ba->len - offset))) len = ba->len - offset; lua_pushlstring(L, &(ba->data[offset]), len); WSLUA_RETURN(1); /* A Lua string of the binary bytes in the ByteArray. */ }