From fe96182c27fc479f38c8cf5b9d3284b50002dc51 Mon Sep 17 00:00:00 2001 From: Erik Rainey Date: Wed, 7 Jun 2023 17:51:31 -0500 Subject: [PATCH 1/2] Add CRC32 at the end of a transfer and demark the payload as a field Fixes #5 Added Payload size too and fixed names for CRC. --- cyphal_udp.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cyphal_udp.lua b/cyphal_udp.lua index d49e3d7..4db4e23 100644 --- a/cyphal_udp.lua +++ b/cyphal_udp.lua @@ -2,7 +2,7 @@ -- Protocol constants local CYPHAL_UDP_PORT = 9382 -- The port number used by Cyphal/UDP - +local CYPHAL_UDP_HEADER_SIZE = 24 -- The sizeof the Cyphal/UDP Header -- Custom protocol dissector local cyphal_udp = Proto("cyphaludp", "Cyphal/UDP Protocol 1.0 beta") @@ -23,7 +23,10 @@ frame_index_eot = ProtoField.uint32("cyphal_udp.frame_index_eot", "frame_index_e frame_index = ProtoField.uint32("cyphal_udp.frame_index", "frame_index", base.DEC) end_of_transfer = ProtoField.bool("cyphal_udp.end_of_transfer", "end_of_transfer", base.NONE) user_data = ProtoField.uint16("cyphal_udp.user_data", "user_data", base.HEX) -crc16_ccitt_false = ProtoField.uint16("cyphal_udp.crc16_ccitt_false", "crc16_ccitt_false (BE)", base.HEX) +crc16_ccitt_false = ProtoField.uint16("cyphal_udp.crc16_ccitt_false", "CRC16-CCITT-FALSE (BE)", base.HEX) +serialized_payload = ProtoField.bytes("cyphal_udp.serialized_payload", "serialized_payload", base.SPACE) +serialized_payload_size = ProtoField.uint32("cyphal_udp.serialized_payload_size", "serialized_payload_size", base.DEC) +crc32 = ProtoField.uint32("cyphal_udp.crc32", "CRC32-C (LE)", base.HEX) -- Protocol fields cyphal_udp.fields = { @@ -41,7 +44,10 @@ cyphal_udp.fields = { frame_index, end_of_transfer, user_data, - crc16_ccitt_false + crc16_ccitt_false, + serialized_payload, + serialized_payload_size, + crc32 -- Add more fields as needed } @@ -93,6 +99,13 @@ local function dissect_cyphal_udp(buffer, pinfo, tree) cyphal_udp_tree:add_le(end_of_transfer, eot) cyphal_udp_tree:add_le(user_data, buffer(20, 2)) cyphal_udp_tree:add(crc16_ccitt_false, buffer(22, 2)) + local len = buffer:len() + local rem = len - CYPHAL_UDP_HEADER_SIZE - 4 -- the remaining bytes minus CRC32C + if rem > 0 then + cyphal_udp_tree:add_le(serialized_payload_size, rem) + cyphal_udp_tree:add_le(serialized_payload, buffer(24, rem)) + end + cyphal_udp_tree:add_le(crc32, buffer(len-4, 4)) -- Add more field dissectors as needed end From 85b4cb758f46d16017f76c8abcbc7aa2fea7894a Mon Sep 17 00:00:00 2001 From: Erik Rainey Date: Wed, 7 Jun 2023 19:50:47 -0500 Subject: [PATCH 2/2] Adding separate sections for header/payload/footer --- cyphal_udp.lua | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/cyphal_udp.lua b/cyphal_udp.lua index 4db4e23..5b4c517 100644 --- a/cyphal_udp.lua +++ b/cyphal_udp.lua @@ -57,22 +57,25 @@ ipv4_destination_address_field = Field.new("ip.dst") -- Function to dissect the custom protocol local function dissect_cyphal_udp(buffer, pinfo, tree) -- Create a subtree for the custom protocol - local cyphal_udp_tree = tree:add(cyphal_udp, buffer(), "Cyphal/UDP Header") + local metadata_tree = tree:add(cyphal_udp, buffer(), "Cyphal/UDP Metadata") + local header_tree = tree:add(cyphal_udp, buffer(), "Cyphal/UDP Header") + local payload_tree = tree:add(cyphal_udp, buffer(), "Cyphal/UDP Payload") + local footer_tree = tree:add(cyphal_udp, buffer(), "Cyphal/UDP Footer") if (ipv4_source_address_field()) then local ip_src = ipv4_source_address_field().value - cyphal_udp_tree:add(ipv4_src, ip_src) + metadata_tree:add(ipv4_src, ip_src) end if (ipv4_destination_address_field()) then local ip_dst = ipv4_destination_address_field().value - cyphal_udp_tree:add(ipv4_dst, ip_dst) + metadata_tree:add(ipv4_dst, ip_dst) end -- Add fields to the subtree - cyphal_udp_tree:add_le(version, buffer(0, 1)) - cyphal_udp_tree:add_le(priority, buffer(1, 1)) - cyphal_udp_tree:add_le(source_node_id, buffer(2, 2)) - cyphal_udp_tree:add_le(destination_node_id, buffer(4, 2)) + header_tree:add_le(version, buffer(0, 1)) + header_tree:add_le(priority, buffer(1, 1)) + header_tree:add_le(source_node_id, buffer(2, 2)) + header_tree:add_le(destination_node_id, buffer(4, 2)) local ds = buffer(6, 2):le_uint() local port_id = bit.band(ds, 0x7FFF) local snm = bit.rshift(bit.band(ds, 0x8000), 15) @@ -82,30 +85,30 @@ local function dissect_cyphal_udp(buffer, pinfo, tree) port_id = port_id - 16384 rnr = true end - cyphal_udp_tree:add_le(request_not_response, rnr) - cyphal_udp_tree:add_le(service_id, port_id) + header_tree:add_le(request_not_response, rnr) + header_tree:add_le(service_id, port_id) else - cyphal_udp_tree:add_le(subject_id, port_id) + header_tree:add_le(subject_id, port_id) end - cyphal_udp_tree:add_le(service_not_message, snm) - cyphal_udp_tree:add_le(data_specifier, buffer(6, 2)) - cyphal_udp_tree:add_le(transfer_id, buffer(8, 8)) + header_tree:add_le(service_not_message, snm) + header_tree:add_le(data_specifier, buffer(6, 2)) + header_tree:add_le(transfer_id, buffer(8, 8)) -- process the number as BE - cyphal_udp_tree:add_le(frame_index_eot, buffer(16, 4)) + header_tree:add_le(frame_index_eot, buffer(16, 4)) local fi = buffer(16, 4):le_uint() local fidx = bit.band(fi, 0x7FFFFFFF) local eot = bit.rshift(bit.band(fi, 0x80000000), 31) - cyphal_udp_tree:add_le(frame_index, fidx) - cyphal_udp_tree:add_le(end_of_transfer, eot) - cyphal_udp_tree:add_le(user_data, buffer(20, 2)) - cyphal_udp_tree:add(crc16_ccitt_false, buffer(22, 2)) + header_tree:add_le(frame_index, fidx) + header_tree:add_le(end_of_transfer, eot) + header_tree:add_le(user_data, buffer(20, 2)) + header_tree:add(crc16_ccitt_false, buffer(22, 2)) local len = buffer:len() local rem = len - CYPHAL_UDP_HEADER_SIZE - 4 -- the remaining bytes minus CRC32C if rem > 0 then - cyphal_udp_tree:add_le(serialized_payload_size, rem) - cyphal_udp_tree:add_le(serialized_payload, buffer(24, rem)) + payload_tree:add_le(serialized_payload_size, rem) + payload_tree:add_le(serialized_payload, buffer(24, rem)) end - cyphal_udp_tree:add_le(crc32, buffer(len-4, 4)) + footer_tree:add_le(crc32, buffer(len-4, 4)) -- Add more field dissectors as needed end