Ask Your Question
0

Lua Script (if condition judgement) is invalid

asked 2023-01-29 10:54:52 +0000

navahoo gravatar image

updated 2023-01-30 15:05:43 +0000

cmaynard gravatar image

I just have next script in next,

tox_flag_frag = 0
tox_flag_reliablesn = 1
tox_flag_life = 0

I expect cmdindex = 14, but finally cmdindex is 26 , not sure why (tox_flag_frag > 0) judgement is invalid?

cmdindex = 10

if (tox_flag_frag > 0) then
    cmdindex = cmdindex + 12
end
if (tox_flag_reliablesn > 0) then
    cmdindex = cmdindex + 4
end   
if (tox_flag_life > 0) then
    cmdindex = cmdindex + 4
end
edit retag flag offensive close merge delete

Comments

Can you update the question with output of wireshark -v or Help->About Wireshark:Wireshark.

Output below from Tools->Lua->Evaluate with version 4.0.3.

1/29/2023 7:27:17 AM Console cleared
1/29/2023 7:27:19 AM tox_flag_frag =    0
1/29/2023 7:27:19 AM tox_flag_reliablesn =  1
1/29/2023 7:27:19 AM tox_flag_life =    0
1/29/2023 7:27:19 AM cmdindex =     10
1/29/2023 7:27:19 AM cmdindex =     14
Chuckc gravatar imageChuckc ( 2023-01-29 13:30:14 +0000 )edit

My labtop is Mac,

next is output of Help->About Wireshark:Wireshark.

==============================================

Version 3.6.7 (v3.6.7-0-g4a304d7ec222) Copyright 1998-2022 Gerald Combs [email protected] and contributors. License GPLv2+: GNU GPL version 2 or later https://www.gnu.org/licenses/gpl-2.0.htmlThis is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Compiled (64-bit) using Clang 13.0.0 (clang-1300.0.29.30), with Qt 5.15.3, with libpcap, without POSIX capabilities, with GLib 2.68.4, with zlib 1.2.11, with Lua 5.2.4, with GnuTLS 3.6.15 and PKCS #11 support, with Gcrypt 1.8.7, with MIT Kerberos, with MaxMind DB resolver, with nghttp2 1.46.0, with brotli, with LZ4, with Zstandard, with Snappy, with libxml2 2.9.9, with libsmi 0.4.8, with QtMultimedia, with ...(more)

navahoo gravatar imagenavahoo ( 2023-01-30 01:35:18 +0000 )edit

Is it possible the flags are not being reinitialized on successive passes through the code?

Chuckc gravatar imageChuckc ( 2023-01-30 01:58:23 +0000 )edit

here is more code with flag variable initialization, and also I use debug_func to dump flag variable and cmdindex,

the flag variable is expected as next :

tox_flag_frag = 0

tox_flag_reliablesn = 1

tox_flag_life = 0

but the cmdindex is 22 when debug_func(299, tox_flag_reliablesn, tox_flag_life, tox_flag_frag, cmdindex), this is unexpected...

Don't understand why this happen...

================================================

    tox_flag = tvb(curindex,1):uint() 

    tox_flag_reliablesn = (tox_flag/64)

    tox_flag_reliablesn = tox_flag_reliablesn%2

    tox_flag_frag = (tox_flag/128)

    tox_flag_life =  (tox_flag/32)

    tox_flag_life = (tox_flag_life%2)

    if pinfo.number == 31 then
        debug_func(290, tox_flag_reliablesn, tox_flag_life, tox_flag_frag, cmdindex)
    end

    if (tox_flag_frag > 0) then
        cmdindex = cmdindex + 12
    end


    if pinfo.number == 31 then
        debug_func(299, tox_flag_reliablesn, tox_flag_life, tox_flag_frag, cmdindex)
    end


    if (tox_flag_reliablesn > 0) then
        cmdindex = cmdindex + 4
    end
    if pinfo.number == 31 then
        debug_func(307, tox_flag_reliablesn, tox_flag_life, tox_flag_frag, cmdindex)
    end

    if (tox_flag_life > 0) then
        cmdindex = cmdindex + 4
    end

    if pinfo.number == 31 then
        debug_func(315, tox_flag_reliablesn, tox_flag_life, tox_flag_frag, cmdindex)
    end

local function ...
(more)
navahoo gravatar imagenavahoo ( 2023-01-30 07:42:21 +0000 )edit

What is the value of curindex and what is the value of tox_flag after this call:

tox_flag = tvb(curindex,1):uint()

The tvb()offset field is 0-based, so perhaps you're off by 1? From https://www.wireshark.org/docs/wsdg_h...:

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:captured_len().

cmaynard gravatar imagecmaynard ( 2023-01-30 15:15:05 +0000 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-02-01 04:00:33 +0000

Chuckc gravatar image

updated 2023-02-01 16:54:44 +0000

Lua treats all number variables as floats. Forcing a cast to integer with math.floor gave better results.
Modulo (%) returns a non-integer if passed a non-integer so may need wrapped also.
Maybe better to rework the math logic if bit positions are the target?

1/31/2023 9:48:19 PM line = 290 : value1 = 1  value2 = 0 value3 = 0 value4 = 10
1/31/2023 9:48:19 PM line = 299 : value1 = 1  value2 = 0 value3 = 0 value4 = 10
1/31/2023 9:48:19 PM line = 307 : value1 = 1  value2 = 0 value3 = 0 value4 = 14
1/31/2023 9:48:19 PM line = 315 : value1 = 1  value2 = 0 value3 = 0 value4 = 14

2.1 โ€“ Values and Types

There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.

Number represents real (double-precision floating-point) numbers.

   cmdindex = 10
   tox_flag = 65

    tox_flag_reliablesn = math.floor(tox_flag/64)

    tox_flag_reliablesn = tox_flag_reliablesn%2

    tox_flag_frag = math.floor(tox_flag/128)
    print("tox_flag_frag =", tox_flag_frag)

    tox_flag_life =  math.floor(tox_flag/32)

    tox_flag_life = (tox_flag_life%2

3.4.1 โ€“ Arithmetic Operators

Modulo is defined as

 a % b == a - math.floor(a/b)*b

That is, it is the remainder of a division that rounds the quotient towards minus infinity.

20 โ€“ The String Library

The function string.format is a powerful tool when formatting strings, ...

Changing the debug line from integer (%d) to floats (%f) :

    local message = string.format("line = %f : value1 = %f  value2 = %f value3 = %f value4 = %f", lineno, value1, value2, value3, value4)

shows that the variables are non-zero values.

2/1/2023 10:50:31 AM line = 290.000000 : value1 = 1.015625  value2 = 0.031250 value3 = 0.507813 value4 = 10.000000
2/1/2023 10:50:31 AM line = 299.000000 : value1 = 1.015625  value2 = 0.031250 value3 = 0.507813 value4 = 22.000000
2/1/2023 10:50:31 AM line = 307.000000 : value1 = 1.015625  value2 = 0.031250 value3 = 0.507813 value4 = 26.000000
2/1/2023 10:50:31 AM line = 315.000000 : value1 = 1.015625  value2 = 0.031250 value3 = 0.507813 value4 = 30.000000
edit flag offensive delete link more

Comments

An alternative, and IMO, a simpler solution:

tox_flag_reliablesn = bit.band(bit.rshift(tox_flag, 6), 1)
tox_flag_frag = bit.rshift(tox_flag, 7)
tox_flag_life = bit.band(bit.rshift(tox_flag, 5), 1)

... and if you just need to test if the bit is set or not, which seems to be the case, then you really don't even have to bother with the shifting operation, just do this instead:

tox_flag_reliablesn = bit.band(tox_flag, 0x40)
tox_flag_frag = bit.band(tox_flag, 0x80)
tox_flag_life = bit.band(tox_flag, 0x20)
cmaynard gravatar imagecmaynard ( 2023-02-01 17:39:58 +0000 )edit

this bit operation is a good solution :) Thanks

navahoo gravatar imagenavahoo ( 2023-02-04 14:44:33 +0000 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-01-29 10:54:52 +0000

Seen: 401 times

Last updated: Feb 01 '23