Skip to content

Commit

Permalink
feat: add reliable protocol implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcsmith committed Oct 9, 2023
1 parent 8342c13 commit 16efb30
Show file tree
Hide file tree
Showing 7 changed files with 1,109 additions and 31 deletions.
2 changes: 1 addition & 1 deletion crates/scion/src/address/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ServiceAddress {

#[allow(unused)]
/// Special none service address value.
const NONE: Self = Self(0xffff);
pub(crate) const NONE: Self = Self(0xffff);
/// Flag bit indicating whether the address includes multicast
const MULTICAST_FLAG: u16 = 0x8000;

Expand Down
14 changes: 14 additions & 0 deletions crates/scion/src/reliable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
use std::net::SocketAddr;

use bytes::Bytes;

mod common_header;
pub use common_header::DecodeError;

mod relay_protocol;
pub use relay_protocol::{ReceiveError, ReliableRelayProtocol, SendError};

mod parser;
mod registration;
mod wire_utils;

const ADDRESS_TYPE_OCTETS: usize = 1;

#[derive(Debug)]
pub struct Packet {
pub last_hop: Option<SocketAddr>,
pub content: Vec<Bytes>,
}
30 changes: 6 additions & 24 deletions crates/scion/src/reliable/common_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ use std::net::{IpAddr, SocketAddr};
use bytes::{Buf, BufMut};
use thiserror::Error;

use super::{
wire_utils::{encoded_address_and_port_length, IPV6_OCTETS, LAYER4_PORT_OCTETS},
ADDRESS_TYPE_OCTETS,
};
use super::{wire_utils::encoded_address_and_port_length, ADDRESS_TYPE_OCTETS};
use crate::address::{HostAddress, HostType};

/// Errors occurring during decoding of packets received over the reliable-relay protocol.
#[derive(Error, Debug, Eq, PartialEq)]
#[derive(Error, Debug, Eq, PartialEq, Clone, Copy)]
pub enum DecodeError {
/// The decoded packet started with an incorrect token. This indicates a
/// synchronisation issue with the relay.
Expand All @@ -26,20 +23,14 @@ pub enum DecodeError {
}

/// Partial or fully decoded commonHeader
#[derive(Debug)]
pub(super) enum DecodedHeader {
Partial(PartialHeader),
Full(CommonHeader),
}

impl DecodedHeader {
#[allow(dead_code)]
pub fn is_fully_decoded(&self) -> bool {
matches!(self, DecodedHeader::Full(..))
}
}

/// A partially decoded common header
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
pub(super) struct PartialHeader {
pub host_type: HostType,
pub payload_length: u32,
Expand Down Expand Up @@ -114,9 +105,8 @@ impl PartialHeader {
/// The header for packets exchange between the client and relay.
#[derive(Default, Debug, Copy, Clone)]
pub(super) struct CommonHeader {
/// The destination to which to relay the packet (when sent), or the client's address
/// The destination to which to relay the packet (when sent), or the last hop
/// when receiving.
// TODO(jsmith): Need to check if this is always the client's address
pub destination: Option<SocketAddr>,
/// The length of the associated payload.
pub payload_length: u32,
Expand All @@ -126,21 +116,12 @@ impl CommonHeader {
/// The minimum length of a common header.
pub const MIN_LENGTH: usize =
Self::COOKIE_LENGTH + ADDRESS_TYPE_OCTETS + Self::PAYLOAD_SIZE_LENGTH;
#[allow(dead_code)]
/// The maximum length of the common header.
pub const MAX_LENGTH: usize = Self::MIN_LENGTH + IPV6_OCTETS + LAYER4_PORT_OCTETS;

const COOKIE: u64 = 0xde00ad01be02ef03;
const COOKIE_LENGTH: usize = 8;
const PAYLOAD_SIZE_LENGTH: usize = 4;

#[allow(dead_code)]
pub fn new() -> Self {
Self::default()
}

/// The size of the payload as a usize.
#[allow(dead_code)]
#[inline]
pub fn payload_size(&self) -> usize {
self.payload_length
Expand Down Expand Up @@ -210,6 +191,7 @@ impl CommonHeader {
/// Panics if there is insufficient data in the buffer to decode the entire header.
/// To avoid the panic, ensure there is [`Self::MAX_LENGTH`] bytes available, or
/// use [`Self::partial_decode()`] instead.
#[cfg(test)]
pub fn decode(buffer: &mut impl Buf) -> Result<Self, DecodeError> {
PartialHeader::decode(buffer).map(|header| header.finish_decoding(buffer))
}
Expand Down
Loading

0 comments on commit 16efb30

Please sign in to comment.