-
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.
- Loading branch information
Showing
5 changed files
with
426 additions
and
215 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::net::{Ipv4Addr, Ipv6Addr}; | ||
|
||
use bytes::Buf; | ||
|
||
use super::{AddressInfo, ByEndpoint, DecodeError, MaybeEncoded, WireDecodeWithContext}; | ||
use crate::address::{Host, HostType, IsdAsn, ServiceAddress}; | ||
|
||
/// The bytes of an encoded host address. | ||
/// | ||
/// The length of the host address is stored in an associated [`AddressInfo`] instance. | ||
pub type RawHostAddress = [u8; AddressInfo::MAX_ADDRESS_BYTES]; | ||
|
||
/// The address header of a SCION packet. | ||
/// | ||
/// It contains the SCION ISD-AS number and host address of the destination and | ||
/// source endpoints. | ||
pub struct AddressHeader { | ||
/// The ISD-AS numbers of the source and destination hosts. | ||
pub ia: ByEndpoint<IsdAsn>, | ||
/// The host addresses of the source and destination. | ||
pub host: ByEndpoint<MaybeEncoded<Host, RawHostAddress>>, | ||
} | ||
|
||
impl<T> WireDecodeWithContext<T, ByEndpoint<AddressInfo>> for AddressHeader | ||
where | ||
T: Buf, | ||
{ | ||
type Error = DecodeError; | ||
|
||
fn decode_with_context( | ||
data: &mut T, | ||
context: ByEndpoint<AddressInfo>, | ||
) -> Result<Self, Self::Error> { | ||
const IA_BYTE_LENGTH: usize = 8; | ||
|
||
if data.remaining() < 2 * IA_BYTE_LENGTH { | ||
return Err(Self::Error::PacketTruncated); | ||
} | ||
|
||
Ok(Self { | ||
ia: ByEndpoint { | ||
destination: IsdAsn(data.get_u64()), | ||
source: IsdAsn(data.get_u64()), | ||
}, | ||
host: ByEndpoint { | ||
destination: maybe_decode_host(data, context.destination)?, | ||
source: maybe_decode_host(data, context.source)?, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
fn maybe_decode_host<T>( | ||
data: &mut T, | ||
info: AddressInfo, | ||
) -> Result<MaybeEncoded<Host, RawHostAddress>, DecodeError> | ||
where | ||
T: Buf, | ||
{ | ||
if data.remaining() < info.address_length() { | ||
return Err(DecodeError::PacketTruncated); | ||
} | ||
|
||
Ok(match info.host_type() { | ||
MaybeEncoded::Decoded(host_type) => MaybeEncoded::Decoded(match host_type { | ||
HostType::None => unreachable!("AddressInfo never returns None host type"), | ||
HostType::Ipv4 => Host::Ip(Ipv4Addr::from(data.get_u32()).into()), | ||
HostType::Ipv6 => Host::Ip(Ipv6Addr::from(data.get_u128()).into()), | ||
HostType::Svc => Host::Svc(ServiceAddress(data.get_u16())), | ||
}), | ||
MaybeEncoded::Encoded(_) => { | ||
let mut host_address: RawHostAddress = [0u8; AddressInfo::MAX_ADDRESS_BYTES]; | ||
|
||
assert!(info.address_length() <= AddressInfo::MAX_ADDRESS_BYTES); | ||
data.copy_to_slice(&mut host_address[..info.address_length()]); | ||
|
||
MaybeEncoded::Encoded(host_address) | ||
} | ||
}) | ||
} |
Oops, something went wrong.