Skip to content

Commit

Permalink
Change CRC32 to CRC16 in protocol_version. (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored and TimonPost committed Mar 19, 2019
1 parent 162cf8e commit 6adfeab
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion benches/packet_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn process_packet_when_received(connection: &mut VirtualConnection, data: &[u8])
/// visible externally
fn standard_header_bytes(delivery_method: DeliveryMethod) -> Vec<u8> {
let mut buffer = Vec::new();
buffer.write_u32::<BigEndian>(ProtocolVersion::get_crc32());
buffer.write_u16::<BigEndian>(ProtocolVersion::get_crc16());
// Represents a standard `Packet`
buffer.write_u8(0);
buffer.write_u8(delivery_method as u8);
Expand Down
4 changes: 2 additions & 2 deletions docs/md_book/src/packet_header.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/net/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
2 changes: 1 addition & 1 deletion src/packet/header/heart_beat_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl HeaderWriter for HeartBeatHeader {
type Output = Result<()>;

fn parse(&self, buffer: &mut Vec<u8>) -> Self::Output {
buffer.write_u32::<BigEndian>(ProtocolVersion::get_crc32())?;
buffer.write_u16::<BigEndian>(ProtocolVersion::get_crc16())?;
buffer.write_u8(PacketTypeId::get_id(self.packet_type_id))?;

Ok(())
Expand Down
10 changes: 5 additions & 5 deletions src/packet/header/standard_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
}
Expand All @@ -39,7 +39,7 @@ impl HeaderWriter for StandardHeader {
type Output = Result<()>;

fn parse(&self, buffer: &mut Vec<u8>) -> Self::Output {
buffer.write_u32::<BigEndian>(self.protocol_version)?;
buffer.write_u16::<BigEndian>(self.protocol_version)?;
buffer.write_u8(PacketTypeId::get_id(self.packet_type_id))?;
buffer.write_u8(DeliveryMethod::get_delivery_method_id(self.delivery_method))?;

Expand All @@ -51,7 +51,7 @@ impl HeaderReader for StandardHeader {
type Header = Result<StandardHeader>;

fn read(rdr: &mut Cursor<&[u8]>) -> Self::Header {
let protocol_version = rdr.read_u32::<BigEndian>()?; /* protocol id */
let protocol_version = rdr.read_u16::<BigEndian>()?; /* protocol id */
let packet_id = rdr.read_u8()?;
let delivery_method_id = rdr.read_u8()?;

Expand Down
26 changes: 13 additions & 13 deletions src/protocol_version.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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()
}
}

Expand All @@ -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]
Expand Down

0 comments on commit 6adfeab

Please sign in to comment.