diff --git a/crates/net/src/connection/delivery/mod.rs b/crates/net/src/connection/delivery/mod.rs index 0c7526626..71fc34f2e 100644 --- a/crates/net/src/connection/delivery/mod.rs +++ b/crates/net/src/connection/delivery/mod.rs @@ -13,7 +13,7 @@ use self::{ received::{IdContinuity, Received}, }; use super::book::{Connection, ConnectionBook}; -use crate::{record::DeliveryRecord, tasks::OutDatagram, Reliability}; +use crate::{record::DeliveryRecord, tasks::OutDatagram, Reliability, MAX_PACKAGE_SIZE}; mod confirms; mod deliveries; @@ -129,14 +129,18 @@ impl ConnDeliveryHandler { /// /// # Panics /// - /// Panics if `buf` len is smaller than length of any of the drained - /// buffered pending package. + /// * If `buf` len is smaller than length of any of the drained buffered + /// pending package. + /// + /// * If `data` is longer than [`MAX_PACKAGE_SIZE`]. fn push<'b>( &mut self, record: DeliveryRecord, data: Vec, buf: &'b mut [u8], ) -> Result, ReceivedIdError> { + assert!(data.len() <= MAX_PACKAGE_SIZE); + let result = self.received.process(record.header().id()); if let Ok(_) | Err(ReceivedIdError::Duplicate) = result { // Push to the buffer even duplicate packages, because the reason diff --git a/crates/net/src/connection/delivery/pending.rs b/crates/net/src/connection/delivery/pending.rs index 3865f5cd4..20739bec8 100644 --- a/crates/net/src/connection/delivery/pending.rs +++ b/crates/net/src/connection/delivery/pending.rs @@ -1,6 +1,8 @@ use std::collections::BTreeMap; -use crate::{connection::databuf::DataBuf, header::PackageId, record::DeliveryRecord}; +use crate::{ + connection::databuf::DataBuf, header::PackageId, record::DeliveryRecord, MAX_PACKAGE_SIZE, +}; /// Buffer for packages received out-of-order and thus awaiting the right /// moment to be delivered. @@ -24,8 +26,11 @@ impl Pending { /// * When there already is a pending package with the given `id`. /// /// * It is not a (semi-)ordered package. + /// + /// * If the data is longer than [`MAX_PACKAGE_SIZE`]. pub(super) fn store(&mut self, record: DeliveryRecord, data: &[u8]) { assert!(record.header().reliability().is_ordered()); + assert!(data.len() <= MAX_PACKAGE_SIZE); let id = record.header().id(); let result = self.ids.insert(id, record); assert!(result.is_none()); diff --git a/crates/net/src/connection/dispatch/mod.rs b/crates/net/src/connection/dispatch/mod.rs index 881dd194f..29bfe5077 100644 --- a/crates/net/src/connection/dispatch/mod.rs +++ b/crates/net/src/connection/dispatch/mod.rs @@ -13,6 +13,7 @@ use super::book::{Connection, ConnectionBook}; use crate::{ header::{DatagramHeader, PackageHeader, PackageId, PackageIdRange}, tasks::OutDatagram, + MAX_PACKAGE_SIZE, }; mod resends; @@ -39,6 +40,11 @@ impl DispatchHandler { handler.next_package_id() } + /// # Panics + /// + /// * If the package is already registered as sent. + /// + /// * If the data are longer than [`MAX_PACKAGE_SIZE`]. pub(crate) async fn sent( &mut self, time: Instant, @@ -46,6 +52,7 @@ impl DispatchHandler { header: PackageHeader, data: &[u8], ) { + assert!(data.len() <= MAX_PACKAGE_SIZE); let mut book = self.book.lock().await; let handler = book.update(time, addr, ConnDispatchHandler::new); handler.resends.push(header, data, time); diff --git a/crates/net/src/connection/dispatch/resends.rs b/crates/net/src/connection/dispatch/resends.rs index a9efd1b86..99f6d0337 100644 --- a/crates/net/src/connection/dispatch/resends.rs +++ b/crates/net/src/connection/dispatch/resends.rs @@ -9,6 +9,7 @@ use priority_queue::PriorityQueue; use crate::{ connection::{book::MAX_CONN_AGE, databuf::DataBuf}, header::{PackageHeader, PackageId}, + MAX_PACKAGE_SIZE, }; pub(super) const START_BACKOFF_MS: u64 = 220; @@ -43,8 +44,16 @@ impl Resends { } /// Registers new package for re-sending until it is resolved. + /// + /// # Panics + /// + /// * If the package (ID) is already stored. + /// + /// * If data is longer than [`MAX_PACKAGE_SIZE`]. pub(super) fn push(&mut self, header: PackageHeader, data: &[u8], now: Instant) { - self.queue.push(header.id(), Timing::new(now)); + assert!(data.len() <= MAX_PACKAGE_SIZE); + let result = self.queue.push(header.id(), Timing::new(now)); + assert!(result.is_none()); self.headers.insert(header.id(), header); self.data.push(header.id(), data); } diff --git a/crates/net/src/tasks/dreceiver.rs b/crates/net/src/tasks/dreceiver.rs index 4f38fc2bc..a56aaae87 100644 --- a/crates/net/src/tasks/dreceiver.rs +++ b/crates/net/src/tasks/dreceiver.rs @@ -6,7 +6,7 @@ use tracing::{error, info, warn}; use crate::{ header::{DatagramHeader, PackageHeader}, protocol::{MsgRecvError, ProtocolSocket}, - MAX_DATAGRAM_SIZE, + MAX_DATAGRAM_SIZE, MAX_PACKAGE_SIZE, }; pub(super) struct InSystemDatagram { @@ -54,6 +54,8 @@ pub(super) async fn run( } }; + assert!(data.len() <= MAX_PACKAGE_SIZE); + // Closed channel(s) are handled at the top part of the loop, // therefore errors from .send() are not treated below. match header {