From 6ecb37628fdfbe6fe6629fb783ce403ec9fa9a29 Mon Sep 17 00:00:00 2001 From: Lee Smet Date: Tue, 26 Mar 2024 11:48:36 +0100 Subject: [PATCH] Close #174: Reduce size field in datapacket header Reduce the size from 19 to 16 bits Signed-off-by: Lee Smet --- CHANGELOG.md | 4 ++++ docs/data_packet.md | 10 +++++----- src/packet/data.rs | 7 +++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d30626d..844509e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Size of data packets is limited to 65535 bytes. + ### Fixed - The feasibility distance of an existing source key is no longer incorrectly updated diff --git a/docs/data_packet.md b/docs/data_packet.md index 04393e9..c648e1c 100644 --- a/docs/data_packet.md +++ b/docs/data_packet.md @@ -13,7 +13,7 @@ The packet header has a fixed size of 36 bytes, with the following layout: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -|Reserved | Length | Hop Limit | +| Reserved | Length | Hop Limit | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | + + @@ -33,11 +33,11 @@ The packet header has a fixed size of 36 bytes, with the following layout: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ``` -The first 5 bits are reserved and must be set to 0. +The first 8 bits are reserved and must be set to 0. -The next 19 bits are used to specify the length of the body. It is expected that -the actual length of a packet does not exceed 256K right now, so the 19th bit is -only needed because we have to account for some overhead related to the encryption. +The next 16 bits are used to specify the length of the body. It is expected that +the actual length of a packet does not exceed 65K right now, and overhead related +to encryption should be handled by the client before sending the packet. The next byte is the hop-limit. Every node decrements this value by 1 before sending the packet. If a node decrements this value to 0, the packet is discarded. diff --git a/src/packet/data.rs b/src/packet/data.rs index 16edb55..5e14879 100644 --- a/src/packet/data.rs +++ b/src/packet/data.rs @@ -7,7 +7,7 @@ use tokio_util::codec::{Decoder, Encoder}; const DATA_PACKET_HEADER_SIZE: usize = 4; /// Mask to extract data length from -const DATA_PACKET_LEN_MASK: u32 = (1 << 19) - 1; +const DATA_PACKET_LEN_MASK: u32 = (1 << 16) - 1; #[derive(Debug, Clone)] pub struct DataPacket { @@ -27,8 +27,7 @@ pub struct Codec { /// Data from the DataPacket header. #[derive(Clone, Copy)] struct HeaderValues { - // At most 19 bits atm but that can't be properly expressed. - len: u32, + len: u16, hop_limit: u8, } @@ -59,7 +58,7 @@ impl Decoder for Codec { let raw_header = src.get_u32(); // Hop limit is the last 8 bits. let hop_limit = (raw_header & 0xFF) as u8; - let data_len = (raw_header >> 8) & DATA_PACKET_LEN_MASK; + let data_len = ((raw_header >> 8) & DATA_PACKET_LEN_MASK) as u16; let header_vals = HeaderValues { len: data_len, hop_limit,