-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add types and de/encoding for SCMP messages
- Loading branch information
Showing
6 changed files
with
1,222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! Types and conversion for the SCION Control Message Protocol. | ||
//! | ||
//! This implements the specification at the [SCION documentation page][scion-doc-scmp] but currently | ||
//! does not cover DRKey-based authentication. | ||
//! | ||
//! [scion-doc-scmp]: https://docs.scion.org/en/latest/protocols/scmp.html | ||
mod error; | ||
pub use error::ScmpDecodeError; | ||
|
||
mod messages; | ||
pub use messages::*; | ||
|
||
mod raw; | ||
pub use raw::ScmpMessageRaw; | ||
|
||
use crate::packet::AddressHeader; | ||
|
||
/// Trait implemented by all SCMP messages. | ||
pub trait ScmpMessageBase { | ||
/// Returns the SCMP type of this message. | ||
fn get_type(&self) -> ScmpType; | ||
|
||
/// Returns the additional SCMP code of this message. | ||
fn code(&self) -> u8 { | ||
0 | ||
} | ||
} | ||
|
||
/// Trait implemented by all SCMP messages to handle checksums. | ||
pub trait ScmpMessageChecksum: ScmpMessageBase { | ||
/// Returns the currently stored checksum of the message. | ||
fn checksum(&self) -> u16; | ||
|
||
/// Clears then sets the checksum to the value returned by [`Self::calculate_checksum()`]. | ||
fn set_checksum(&mut self, address_header: &AddressHeader); | ||
|
||
/// Compute the checksum for this SCMP message using the provided address header. | ||
fn calculate_checksum(&self, address_header: &AddressHeader) -> u16; | ||
|
||
/// Returns true if the checksum successfully verifies, otherwise false. | ||
fn verify_checksum(&self, address_header: &AddressHeader) -> bool { | ||
self.calculate_checksum(address_header) == 0 | ||
} | ||
} | ||
|
||
/// SCION protocol number for SCMP. | ||
/// | ||
/// See the [IETF SCION-dataplane RFC draft][rfc] for possible values. | ||
/// | ||
///[rfc]: https://www.ietf.org/archive/id/draft-dekater-scion-dataplane-00.html#protnum | ||
pub const SCMP_PROTOCOL_NUMBER: u8 = 202; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//! Errors encountered when handling SCMP messages. | ||
/// Error encountered when attempting to decode an SCMP message. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum ScmpDecodeError { | ||
/// The data is shorter than the minimum length of the corresponding SCMP message. | ||
#[error("message is empty or was truncated")] | ||
MessageEmptyOrTruncated, | ||
/// When attempting to decode a specific message type and the data contains a different message | ||
/// type. | ||
#[error("the type of the message does not match the type being decoded")] | ||
MessageTypeMismatch, | ||
/// Informational messages of unknown types need to be dropped. | ||
#[error("unknown info message type {0}")] | ||
UnknownInfoMessage(u8), | ||
/// Depending on the type of SCMP message, only specific values of the `code` field are allowed. | ||
#[error("invalid code for this message type")] | ||
InvalidCode, | ||
/// When decoding a SCION packet presumably containing an SCMP message but the next-header value | ||
/// of the SCION header doesn't match [`SCMP_PROTOCOL_NUMBER`][super::SCMP_PROTOCOL_NUMBER]. | ||
#[error("next-header value of SCION header is not correct")] | ||
WrongProtocolNumber(u8), | ||
} |
Oops, something went wrong.