Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CRC32 at the end of a transfer and demark the payload as a field #6

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions cyphal_udp.lua
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version is only 4 bits. The following 4 bits are reserved.

Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's all upper-case now. Okay. Am I supposed to be impressed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the reference online are in SHOUTY CASE. So I thought I'd SHOUT too.

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 = {
Expand All @@ -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
}

Expand All @@ -51,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)
Expand All @@ -76,23 +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
payload_tree:add_le(serialized_payload_size, rem)
payload_tree:add_le(serialized_payload, buffer(24, rem))
end
footer_tree:add_le(crc32, buffer(len-4, 4))
-- Add more field dissectors as needed
end

Expand Down