kaitai-io / kaitai_struct

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby
https://kaitai.io
4.04k stars 199 forks source link

Question: How can i determine which packet type I recieved? #1002

Closed iboofkratom closed 1 year ago

iboofkratom commented 1 year ago

Hi, I have a problem: I want to use Kaitai for communication between 2 devices. There are multiple types of packets i can receive. What would be the best way to test which packet i received? Obvious answer is to use contents to check for signature. But I have hundreds of types of packets I can receive. Calling the parsing function on every one of them and checking if it fails would be just too slow. So i wanted to ask If this is even possible to do in Kaitai and if yes, what is the best way to do it. I figured that i could put all the packets in one Kaitai file as substructs and then put switch in seq to choose the packet type. But I am just starting with Kaitai, so I'm not sure If this is a supported way of doing this or a hacky workaround. Thanks!!

KOLANICH commented 1 year ago

I figured that i could put all the packets in one Kaitai file as substructs and then put switch in seq to choose the packet type.

Yes, you also will need a container packet type. Sometimes they are already provided for you, for example protocol and next_header_type in IP and ether_type in Ethernet. TCP and UDP have no built-in field for a protocol, so protocols are usually just prior knowledge, and are usually derived from server ports. But nothing prevents you from adding an own container as simple as a seq of 2 fields.

iboofkratom commented 1 year ago

Great, thanks!! :)