Skip to main content

mcrx_core/raw/
packet.rs

1use crate::packet::ReceiveMetadata;
2use crate::subscription::SubscriptionId;
3use bytes::Bytes;
4use std::net::IpAddr;
5
6/// A received raw multicast IP datagram.
7#[derive(Debug, Clone)]
8pub struct RawPacket {
9    /// The subscription through which this datagram was received.
10    pub subscription_id: SubscriptionId,
11    /// The complete received IP datagram bytes, including the IP header.
12    pub datagram: Bytes,
13    /// The parsed source IP address, if it was cheaply available.
14    pub source_ip: Option<IpAddr>,
15    /// The parsed destination/group IP address, if it was cheaply available.
16    pub group: Option<IpAddr>,
17    /// The parsed IP protocol / next-header value, if it was cheaply available.
18    pub ip_protocol: Option<u8>,
19    /// Additional receive context supplied by the platform layer.
20    pub metadata: ReceiveMetadata,
21}
22
23impl RawPacket {
24    /// Returns the length of the complete datagram in bytes.
25    pub fn datagram_len(&self) -> usize {
26        self.datagram.len()
27    }
28
29    /// Returns the received datagram bytes.
30    pub fn datagram(&self) -> &[u8] {
31        &self.datagram
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn raw_packet_datagram_len_returns_correct_length() {
41        let packet = RawPacket {
42            subscription_id: SubscriptionId(1),
43            datagram: Bytes::from_static(&[0x45, 0, 0, 1]),
44            source_ip: None,
45            group: None,
46            ip_protocol: Some(17),
47            metadata: ReceiveMetadata::empty(),
48        };
49
50        assert_eq!(packet.datagram_len(), 4);
51    }
52}