dlms-cosem-kit 0.4.0

DLMS/COSEM client and server for Rust with a no_std protocol core, selective profile reads, block transfer and AES-GCM protection
Documentation
//! The wrapper — DLMS over TCP and UDP.
//!
//! Eight bytes in front of the APDU: a version, the sending and receiving service
//! access points, and a length. Over UDP one datagram is one wrapper PDU; over TCP the
//! layer must reassemble, because an APDU may arrive in as many segments as the network
//! feels like.

use crate::codec::{Decode, Encode, Error, ErrorKind, Reader, Result, Writer};

/// The only wrapper version defined.
pub const VERSION: u16 = 0x0001;

/// The port IANA assigned to DLMS.
pub const DEFAULT_PORT: u16 = 4059;

/// The eight-byte wrapper header.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Header {
    /// Always [`VERSION`].
    pub version: u16,
    /// The sender's service access point — the client SAP in a request.
    pub source: u16,
    /// The receiver's service access point — the logical device in a request.
    pub destination: u16,
    /// How many bytes of APDU follow.
    pub length: u16,
}

impl Header {
    /// A header for an APDU of `length` bytes.
    #[must_use]
    pub const fn new(source: u16, destination: u16, length: u16) -> Self {
        Self { version: VERSION, source, destination, length }
    }

    /// The header size, which is fixed.
    pub const LEN: usize = 8;
}

impl Encode for Header {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        w.write_u16(self.version)?;
        w.write_u16(self.source)?;
        w.write_u16(self.destination)?;
        w.write_u16(self.length)
    }

    fn encoded_len(&self) -> usize {
        Self::LEN
    }
}

impl<'a> Decode<'a> for Header {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        let version = r.u16()?;
        if version != VERSION {
            return Err(r.err_back(ErrorKind::InvalidValue, 2));
        }
        Ok(Self { version, source: r.u16()?, destination: r.u16()?, length: r.u16()? })
    }
}

/// A wrapper PDU: a header and the APDU it carries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Wpdu<'a> {
    /// The header.
    pub header: Header,
    /// The APDU.
    pub apdu: &'a [u8],
}

impl<'a> Wpdu<'a> {
    /// A PDU carrying `apdu` between two service access points.
    ///
    /// # Errors
    /// When the APDU is longer than the sixteen-bit length field can describe.
    pub fn new(source: u16, destination: u16, apdu: &'a [u8]) -> Result<Self> {
        let length = u16::try_from(apdu.len()).map_err(|_| Error::new(ErrorKind::InvalidLength, 0))?;
        Ok(Self { header: Header::new(source, destination, length), apdu })
    }
}

impl Encode for Wpdu<'_> {
    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
        self.header.encode(w)?;
        w.write_bytes(self.apdu)
    }

    fn encoded_len(&self) -> usize {
        Header::LEN + self.apdu.len()
    }
}

impl<'a> Decode<'a> for Wpdu<'a> {
    fn decode(r: &mut Reader<'a>) -> Result<Self> {
        let header = Header::decode(r)?;
        let apdu = r.take(usize::from(header.length))?;
        Ok(Self { header, apdu })
    }
}

/// Reassembles wrapper PDUs from a TCP stream.
///
/// Holds no buffer of its own: the caller keeps the bytes and is told how many were
/// consumed, which is what lets the same code serve a `Vec` on a head-end and a fixed
/// array in firmware. Everything a header announces is checked on its eight bytes,
/// before the caller waits for or buffers the body.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StreamReassembler {
    max_apdu: u16,
    peer: Option<(u16, u16)>,
}

impl StreamReassembler {
    /// A reassembler that refuses any PDU claiming more than `max_apdu` bytes.
    ///
    /// The length field is attacker-controlled: a peer that announces 65 535 bytes must
    /// not make the caller hold that much before anything is authenticated. 0 admits
    /// only empty APDUs.
    #[must_use]
    pub const fn new(max_apdu: u16) -> Self {
        Self { max_apdu, peer: None }
    }

    /// Also refuse a PDU whose service access points are not `source` → `destination`.
    ///
    /// A client expects the meter's replies: `source` is the logical device's port and
    /// `destination` its own.
    ///
    /// ```
    /// # use dlms_cosem_kit::transport::wrapper::StreamReassembler;
    /// let r = StreamReassembler::new(1024).with_peer(0x0001, 0x0010);
    /// // A reply from logical device 2: refused on the header alone.
    /// assert!(r.next_pdu(&[0x00, 0x01, 0x00, 0x02, 0x00, 0x10, 0x00, 0x05]).is_err());
    /// ```
    #[must_use]
    pub const fn with_peer(self, source: u16, destination: u16) -> Self {
        Self { peer: Some((source, destination)), ..self }
    }

    /// The header at the front of `buf`, once its eight bytes are there and pass every
    /// check. The body need not have arrived.
    ///
    /// ```
    /// # use dlms_cosem_kit::transport::wrapper::StreamReassembler;
    /// let r = StreamReassembler::new(256);
    /// let header = r.header(&[0x00, 0x01, 0x00, 0x01, 0x00, 0x10, 0x00, 0x20]).unwrap().unwrap();
    /// assert_eq!(header.length, 32); // read exactly 32 more bytes
    /// assert!(r.header(&[0x00, 0x01, 0x00, 0x01, 0x00, 0x10, 0x01, 0x01]).is_err()); // 257
    /// ```
    ///
    /// # Errors
    /// As [`StreamReassembler::next_pdu`].
    pub fn header(&self, buf: &[u8]) -> Result<Option<Header>> {
        if buf.len() < Header::LEN {
            return Ok(None);
        }
        let header = Header::decode(&mut Reader::new(buf))?;
        if let Some((source, destination)) = self.peer {
            if header.source != source {
                return Err(Error::new(ErrorKind::InvalidValue, 2));
            }
            if header.destination != destination {
                return Err(Error::new(ErrorKind::InvalidValue, 4));
            }
        }
        if header.length > self.max_apdu {
            return Err(Error::new(ErrorKind::InvalidLength, 6));
        }
        Ok(Some(header))
    }

    /// The next complete PDU in `buf`, and how many bytes it used.
    ///
    /// # Errors
    /// [`ErrorKind::InvalidValue`] for a bad version (offset 0) or, with
    /// [`with_peer`](Self::with_peer), the wrong source (offset 2) or destination (offset
    /// 4); [`ErrorKind::InvalidLength`] when the announced length exceeds the maximum.
    pub fn next_pdu<'b>(&self, buf: &'b [u8]) -> Result<Option<(Wpdu<'b>, usize)>> {
        let Some(header) = self.header(buf)? else {
            return Ok(None);
        };
        let total = Header::LEN + usize::from(header.length);
        match buf.get(Header::LEN..total) {
            Some(apdu) => Ok(Some((Wpdu { header, apdu }, total))),
            None => Ok(None),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codec::SliceWriter;

    #[test]
    fn the_header_is_eight_bytes_big_endian() {
        let w = Wpdu::new(0x0010, 0x0001, &[0xC0, 0x01, 0x81]).unwrap();
        let mut buf = [0u8; 32];
        let mut sw = SliceWriter::new(&mut buf);
        w.encode(&mut sw).unwrap();
        assert_eq!(sw.as_slice(), [0x00, 0x01, 0x00, 0x10, 0x00, 0x01, 0x00, 0x03, 0xC0, 0x01, 0x81]);
        assert_eq!(Wpdu::from_bytes(sw.as_slice()).unwrap(), w);
    }

    #[test]
    fn a_wrong_version_is_refused() {
        assert_eq!(
            Header::from_bytes(&[0x00, 0x02, 0, 0x10, 0, 1, 0, 0]).unwrap_err().kind,
            ErrorKind::InvalidValue
        );
    }

    #[test]
    fn the_reassembler_waits_for_every_byte() {
        let w = Wpdu::new(0x0010, 0x0001, &[0xAA; 20]).unwrap();
        let mut buf = [0u8; 64];
        let mut sw = SliceWriter::new(&mut buf);
        w.encode(&mut sw).unwrap();
        let n = sw.written();
        let r = StreamReassembler::new(1024);
        for split in 0..n {
            assert!(r.next_pdu(&buf[..split]).unwrap().is_none(), "cut at {split}");
        }
        let (pdu, used) = r.next_pdu(&buf[..n]).unwrap().unwrap();
        assert_eq!(used, n);
        assert_eq!(pdu.apdu.len(), 20);
    }

    #[test]
    fn an_oversized_announcement_is_refused_before_it_is_buffered() {
        let r = StreamReassembler::new(256);
        let header = [0x00, 0x01, 0x00, 0x10, 0x00, 0x01, 0xFF, 0xFF];
        assert_eq!(r.next_pdu(&header).unwrap_err().kind, ErrorKind::InvalidLength);
    }

    /// 003-checked-clock-and-links/SR-001
    #[test]
    fn a_reply_from_the_wrong_peer_is_refused_on_its_header() {
        let r = StreamReassembler::new(1024).with_peer(0x0001, 0x0010);
        let good = [0x00, 0x01, 0x00, 0x01, 0x00, 0x10, 0x00, 0x03];
        assert_eq!(r.header(&good).unwrap(), Some(Header::new(0x0001, 0x0010, 3)));
        assert_eq!(r.next_pdu(&good).unwrap(), None, "the body is still missing");
        let wrong_source = [0x00, 0x01, 0x00, 0x02, 0x00, 0x10, 0x00, 0x03];
        let e = r.next_pdu(&wrong_source).unwrap_err();
        assert_eq!((e.kind, e.offset), (ErrorKind::InvalidValue, 2));
        let wrong_destination = [0x00, 0x01, 0x00, 0x01, 0x00, 0x11, 0x00, 0x03];
        let e = r.next_pdu(&wrong_destination).unwrap_err();
        assert_eq!((e.kind, e.offset), (ErrorKind::InvalidValue, 4));
        // Without a peer any addresses pass.
        assert!(StreamReassembler::new(1024).header(&wrong_source).unwrap().is_some());
    }

    /// 003-checked-clock-and-links/SR-002
    #[test]
    fn a_zero_maximum_admits_only_empty_apdus() {
        let r = StreamReassembler::new(0);
        assert!(r.next_pdu(&[0x00, 0x01, 0, 0x10, 0, 1, 0, 0]).unwrap().is_some());
        assert_eq!(
            r.next_pdu(&[0x00, 0x01, 0, 0x10, 0, 1, 0, 1]).unwrap_err().kind,
            ErrorKind::InvalidLength
        );
    }

    #[test]
    fn two_pdus_back_to_back_are_returned_one_at_a_time() {
        let mut stream = alloc::vec::Vec::new();
        for i in 0..2u8 {
            let body = [i, i, i];
            let w = Wpdu::new(0x0010, 0x0001, &body).unwrap();
            w.encode(&mut stream).unwrap();
        }
        let r = StreamReassembler::new(1024);
        let (first, used) = r.next_pdu(&stream).unwrap().unwrap();
        assert_eq!(first.apdu, &[0, 0, 0]);
        let (second, _) = r.next_pdu(&stream[used..]).unwrap().unwrap();
        assert_eq!(second.apdu, &[1, 1, 1]);
    }
}