Packed_struct and accessing [u8] payloads/fields


I am working on a project where I am decoding TCP and UDP network packets from a network based radio using VITA-49. The packed_struct crate is able to handle everything I have thrown at it for decoding network packets. The final challenge I have is a payload field in several of the network packets. It is towards the end in most packet definitions, but not the last field. The size of the payload will vary based on the packet type and the status string being returned. Is there an idiomatic way to handle this in packed_struct?

I have tried using String (requires Copy for packed_struct) and &[u8] slices (packed_struct doesn't know about lifetimes). The best I have been able to do to have the code compile is to set the type to [u8; 1] and hope I can later extract the payload with unsafe code. The problem with this, is that there is a trailer struct at the very end of the packet.

#[derive(PackedStruct, Clone, Copy, Debug, Default, PartialEq)]
#[packed_struct(endian="msb", bit_numbering="msb0")]
pub struct VitaDiscoveryPacket {
/* 4B B0-4      */  header          : Header,

/* 2B B5-6      */  stream_id       : u32,

/* 8B B7-14     */  class_id        : VitaClassID,

/* 2B B11-12    */  timestamp_int   : u32,
/* 2B B13-16    */  timestamp_frac  : u64,

/* ** B17...    */  payload         : [u8; 1], // String of status key=value pairs

/* 4B B*+4      */  trailer         : Trailer,

Any ideas would be appreciated.


You could try studying the pnet library. It is sort of the de-facto standard when it comes to networking data-structures implementations. It doesn't have VITA-49 though. But you can get good ideas.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.