From 6adfeab21aaa8fedf7e8ad4d6e0a25e40b4d1cc1 Mon Sep 17 00:00:00 2001 From: daxpedda <1645124+daxpedda@users.noreply.github.com> Date: Tue, 19 Mar 2019 20:39:46 +0100 Subject: [PATCH] Change CRC32 to CRC16 in `protocol_version`. (#155) --- benches/packet_processing.rs | 2 +- docs/md_book/src/packet_header.md | 4 ++-- src/net/constants.rs | 4 ++-- src/packet/header/heart_beat_header.rs | 2 +- src/packet/header/standard_header.rs | 10 +++++----- src/protocol_version.rs | 26 +++++++++++++------------- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/benches/packet_processing.rs b/benches/packet_processing.rs index fdc8c0c1..b3ac86de 100644 --- a/benches/packet_processing.rs +++ b/benches/packet_processing.rs @@ -29,7 +29,7 @@ fn process_packet_when_received(connection: &mut VirtualConnection, data: &[u8]) /// visible externally fn standard_header_bytes(delivery_method: DeliveryMethod) -> Vec { let mut buffer = Vec::new(); - buffer.write_u32::(ProtocolVersion::get_crc32()); + buffer.write_u16::(ProtocolVersion::get_crc16()); // Represents a standard `Packet` buffer.write_u8(0); buffer.write_u8(delivery_method as u8); diff --git a/docs/md_book/src/packet_header.md b/docs/md_book/src/packet_header.md index feaa7f42..ae241acd 100644 --- a/docs/md_book/src/packet_header.md +++ b/docs/md_book/src/packet_header.md @@ -5,8 +5,8 @@ In this topic we'll discuss the different headers we are pre-pending to the data Will be included in each packet. ```rust pub struct StandardHeader { - /// crc32 of the protocol version. - pub protocol_version: u32, + /// crc16 of the protocol version. + pub protocol_version: u16, /// specifies the packet type. pub packet_type_id: PacketTypeId, /// specifies how this packet should be processed. diff --git a/src/net/constants.rs b/src/net/constants.rs index ba911175..aedc38f4 100644 --- a/src/net/constants.rs +++ b/src/net/constants.rs @@ -3,7 +3,7 @@ pub const FRAGMENT_HEADER_SIZE: u8 = 4 + STANDARD_HEADER_SIZE; /// Acked packet header size pub const ACKED_PACKET_HEADER: u8 = 8 + STANDARD_HEADER_SIZE; /// Standard header size -pub const STANDARD_HEADER_SIZE: u8 = 6; +pub const STANDARD_HEADER_SIZE: u8 = 4; /// Heartbeat header size pub const HEART_BEAT_HEADER_SIZE: u8 = 5; /// Default max number of fragments to size @@ -22,6 +22,6 @@ pub const DEFAULT_MTU: u16 = 1452; /// This is the current protocol version. /// /// It is used for: -/// - Generating crc32 for the packet header. +/// - Generating crc16 for the packet header. /// - Validating if arriving packets have the same protocol version. pub const PROTOCOL_VERSION: &str = "laminar-0.1.0"; diff --git a/src/packet/header/heart_beat_header.rs b/src/packet/header/heart_beat_header.rs index 9fd9e844..4518b106 100644 --- a/src/packet/header/heart_beat_header.rs +++ b/src/packet/header/heart_beat_header.rs @@ -32,7 +32,7 @@ impl HeaderWriter for HeartBeatHeader { type Output = Result<()>; fn parse(&self, buffer: &mut Vec) -> Self::Output { - buffer.write_u32::(ProtocolVersion::get_crc32())?; + buffer.write_u16::(ProtocolVersion::get_crc16())?; buffer.write_u8(PacketTypeId::get_id(self.packet_type_id))?; Ok(()) diff --git a/src/packet/header/standard_header.rs b/src/packet/header/standard_header.rs index f265c039..b373edb4 100644 --- a/src/packet/header/standard_header.rs +++ b/src/packet/header/standard_header.rs @@ -10,8 +10,8 @@ use std::io::Cursor; #[derive(Copy, Clone, Debug)] /// This header will be included in each packet, and contains some basic information. pub struct StandardHeader { - /// crc32 of the protocol version. - pub protocol_version: u32, + /// crc16 of the protocol version. + pub protocol_version: u16, /// specifies the packet type. pub packet_type_id: PacketTypeId, /// specifies how this packet should be processed. @@ -22,7 +22,7 @@ impl StandardHeader { /// Create new heartbeat header. pub fn new(delivery_method: DeliveryMethod, packet_type_id: PacketTypeId) -> Self { StandardHeader { - protocol_version: ProtocolVersion::get_crc32(), + protocol_version: ProtocolVersion::get_crc16(), packet_type_id, delivery_method, } @@ -39,7 +39,7 @@ impl HeaderWriter for StandardHeader { type Output = Result<()>; fn parse(&self, buffer: &mut Vec) -> Self::Output { - buffer.write_u32::(self.protocol_version)?; + buffer.write_u16::(self.protocol_version)?; buffer.write_u8(PacketTypeId::get_id(self.packet_type_id))?; buffer.write_u8(DeliveryMethod::get_delivery_method_id(self.delivery_method))?; @@ -51,7 +51,7 @@ impl HeaderReader for StandardHeader { type Header = Result; fn read(rdr: &mut Cursor<&[u8]>) -> Self::Header { - let protocol_version = rdr.read_u32::()?; /* protocol id */ + let protocol_version = rdr.read_u16::()?; /* protocol id */ let packet_id = rdr.read_u8()?; let delivery_method_id = rdr.read_u8()?; diff --git a/src/protocol_version.rs b/src/protocol_version.rs index 51ba2827..a17ce803 100644 --- a/src/protocol_version.rs +++ b/src/protocol_version.rs @@ -1,11 +1,11 @@ -use crc::crc32; +use crc::crc16; use lazy_static::lazy_static; pub use crate::net::constants::PROTOCOL_VERSION; lazy_static! { - // The CRC32 of the current protocol version. - static ref VERSION_CRC32: u32 = crc32::checksum_ieee(PROTOCOL_VERSION.as_bytes()); + // The CRC16 of the current protocol version. + static ref VERSION_CRC16: u16 = crc16::checksum_x25(PROTOCOL_VERSION.as_bytes()); } /// Wrapper to provide some functions to perform with the current protocol version. @@ -18,16 +18,16 @@ impl ProtocolVersion { PROTOCOL_VERSION } - /// This will return the crc32 from the current protocol version. + /// This will return the crc16 from the current protocol version. #[inline] - pub fn get_crc32() -> u32 { - *VERSION_CRC32 + pub fn get_crc16() -> u16 { + *VERSION_CRC16 } - /// Validate a crc32 with the current protocol version and return the results. + /// Validate a crc16 with the current protocol version and return the results. #[inline] - pub fn valid_version(protocol_version_crc32: u32) -> bool { - protocol_version_crc32 == ProtocolVersion::get_crc32() + pub fn valid_version(protocol_version_crc16: u16) -> bool { + protocol_version_crc16 == ProtocolVersion::get_crc16() } } @@ -38,19 +38,19 @@ mod test { #[test] fn valid_version() { - let protocol_id = crc32::checksum_ieee(PROTOCOL_VERSION.as_bytes()); + let protocol_id = crc16::checksum_x25(PROTOCOL_VERSION.as_bytes()); assert!(ProtocolVersion::valid_version(protocol_id)); } #[test] fn not_valid_version() { - let protocol_id = crc32::checksum_ieee("not-laminar".as_bytes()); + let protocol_id = crc16::checksum_x25("not-laminar".as_bytes()); assert!(!ProtocolVersion::valid_version(protocol_id)); } #[test] - fn get_crc32() { - assert_eq!(ProtocolVersion::get_crc32(), *VERSION_CRC32); + fn get_crc16() { + assert_eq!(ProtocolVersion::get_crc16(), *VERSION_CRC16); } #[test]