Skip to content

Commit

Permalink
refactor: rename ServiceAddress to ServiceAddr to be inline with othe…
Browse files Browse the repository at this point in the history
…r names.
  • Loading branch information
jpcsmith committed Dec 7, 2023
1 parent 314d4e9 commit 6e49267
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 76 deletions.
2 changes: 1 addition & 1 deletion crates/scion-proto/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod ia;
pub use ia::IsdAsn;

mod service;
pub use service::ServiceAddress;
pub use service::ServiceAddr;

mod host;
pub use host::{HostAddr, HostType};
Expand Down
4 changes: 2 additions & 2 deletions crates/scion-proto/src/address/host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use super::ServiceAddress;
use super::ServiceAddr;

/// The AS-local host identifier of a SCION address.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand All @@ -10,7 +10,7 @@ pub enum HostAddr {
/// An IPv6 host address
V6(Ipv6Addr),
/// A SCION-service host address
Svc(ServiceAddress),
Svc(ServiceAddr),
}

impl From<Ipv4Addr> for HostAddr {
Expand Down
6 changes: 3 additions & 3 deletions crates/scion-proto/src/address/scion_address.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::net::{Ipv4Addr, Ipv6Addr};

use super::{error::AddressKind, AddressParseError, HostAddr, IsdAsn, ServiceAddress};
use super::{error::AddressKind, AddressParseError, HostAddr, IsdAsn, ServiceAddr};

/// A SCION network address.
///
/// SCION network addresses consist of an ISD-AS number, and either an [IPv4 address][`Ipv4Addr`],
/// an [IPv6 address][`Ipv6Addr`], or a [SCION service address][`ServiceAddress`].
/// an [IPv6 address][`Ipv6Addr`], or a [SCION service address][`ServiceAddr`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ScionAddr {
/// An IPv4 network address
Expand Down Expand Up @@ -197,5 +197,5 @@ scion_address! {
/// SCION service addresses consist of a SCION ISD-AS number and a service address.
///
/// See [`ScionAddr`] for a type encompassing SCION IPv6, IPv6, and Service addresses.
pub struct ScionAddrSvc {host: ServiceAddress, kind: AddressKind::ScionSvc};
pub struct ScionAddrSvc {host: ServiceAddr, kind: AddressKind::ScionSvc};
}
102 changes: 47 additions & 55 deletions crates/scion-proto/src/address/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use super::{error::AddressKind, AddressParseError, HostAddr, HostType};
/// # Textual Representation
///
/// Service addresses can also be represented as strings, for example CS and CS_A
/// both represent the anycast service address ServiceAddress::CONTROL. The
/// both represent the anycast service address ServiceAddr::CONTROL. The
/// corresponding multicast service address would be CS_M.
#[derive(Eq, PartialEq, Copy, Clone, Debug, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ServiceAddress(pub u16);
pub struct ServiceAddr(pub u16);

impl ServiceAddress {
impl ServiceAddr {
/// SCION daemon anycast service address (DS_A)
pub const DAEMON: Self = Self(0x0001);
/// SCION control-service anycast address (CS_A)
Expand All @@ -46,9 +46,9 @@ impl ServiceAddress {
/// # Examples
///
/// ```
/// # use scion_proto::address::ServiceAddress;
/// assert!(!ServiceAddress::DAEMON.is_multicast());
/// assert!(ServiceAddress::DAEMON.multicast().is_multicast());
/// # use scion_proto::address::ServiceAddr;
/// assert!(!ServiceAddr::DAEMON.is_multicast());
/// assert!(ServiceAddr::DAEMON.multicast().is_multicast());
/// ```
pub fn multicast(self) -> Self {
Self(self.0 | Self::MULTICAST_FLAG)
Expand All @@ -59,9 +59,9 @@ impl ServiceAddress {
/// # Examples
///
/// ```
/// # use scion_proto::address::ServiceAddress;
/// assert!(ServiceAddress::DAEMON.multicast().anycast().is_anycast());
/// assert!(!ServiceAddress::DAEMON.multicast().anycast().is_multicast());
/// # use scion_proto::address::ServiceAddr;
/// assert!(ServiceAddr::DAEMON.multicast().anycast().is_anycast());
/// assert!(!ServiceAddr::DAEMON.multicast().anycast().is_multicast());
/// ```
pub fn anycast(self) -> Self {
Self(self.0 & !Self::MULTICAST_FLAG)
Expand All @@ -72,25 +72,25 @@ impl ServiceAddress {
/// # Examples
///
/// ```
/// # use scion_proto::address::ServiceAddress;
/// assert!(ServiceAddress::DAEMON.is_anycast());
/// assert!(!ServiceAddress::DAEMON.multicast().is_anycast());
/// # use scion_proto::address::ServiceAddr;
/// assert!(ServiceAddr::DAEMON.is_anycast());
/// assert!(!ServiceAddr::DAEMON.multicast().is_anycast());
/// ```
pub fn is_anycast(&self) -> bool {
(self.0 & Self::MULTICAST_FLAG) == 0
}
}

impl FromStr for ServiceAddress {
impl FromStr for ServiceAddr {
type Err = AddressParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let (service, suffix) = s.split_once('_').unwrap_or((s, "A"));

let address = match service {
"CS" => ServiceAddress::CONTROL,
"DS" => ServiceAddress::DAEMON,
"Wildcard" => ServiceAddress::WILDCARD,
"CS" => ServiceAddr::CONTROL,
"DS" => ServiceAddr::DAEMON,
"Wildcard" => ServiceAddr::WILDCARD,
_ => return Err(AddressKind::Service.into()),
};
match suffix {
Expand All @@ -101,31 +101,31 @@ impl FromStr for ServiceAddress {
}
}

impl From<ServiceAddress> for u16 {
fn from(value: ServiceAddress) -> Self {
impl From<ServiceAddr> for u16 {
fn from(value: ServiceAddr) -> Self {
value.0
}
}

impl From<ServiceAddress> for HostAddr {
fn from(value: ServiceAddress) -> Self {
impl From<ServiceAddr> for HostAddr {
fn from(value: ServiceAddr) -> Self {
HostAddr::Svc(value)
}
}

impl From<ServiceAddress> for HostType {
fn from(_: ServiceAddress) -> Self {
impl From<ServiceAddr> for HostType {
fn from(_: ServiceAddr) -> Self {
HostType::Svc
}
}

impl Display for ServiceAddress {
impl Display for ServiceAddr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.anycast() {
ServiceAddress::DAEMON => write!(f, "DS")?,
ServiceAddress::CONTROL => write!(f, "CS")?,
ServiceAddress::WILDCARD => write!(f, "Wildcard")?,
ServiceAddress(value) => write!(f, "<SVC:{:#06x}>", value)?,
ServiceAddr::DAEMON => write!(f, "DS")?,
ServiceAddr::CONTROL => write!(f, "CS")?,
ServiceAddr::WILDCARD => write!(f, "Wildcard")?,
ServiceAddr(value) => write!(f, "<SVC:{:#06x}>", value)?,
}

if self.is_multicast() {
Expand All @@ -142,8 +142,8 @@ mod tests {

#[test]
fn set_multicast() {
assert!(!ServiceAddress::DAEMON.is_multicast());
assert!(ServiceAddress::DAEMON.multicast().is_multicast());
assert!(!ServiceAddr::DAEMON.is_multicast());
assert!(ServiceAddr::DAEMON.multicast().is_multicast());
}

mod parse {
Expand All @@ -153,35 +153,31 @@ mod tests {
($name:ident, $str:expr, $expected:expr) => {
#[test]
fn $name() {
assert_eq!(ServiceAddress::from_str($str).unwrap(), $expected);
assert_eq!(ServiceAddr::from_str($str).unwrap(), $expected);
}
};
}

test_success!(control, "CS_A", ServiceAddress::CONTROL);
test_success!(control_shorthand, "CS", ServiceAddress::CONTROL);
test_success!(daemon, "DS_A", ServiceAddress::DAEMON);
test_success!(daemon_shorthand, "DS", ServiceAddress::DAEMON);
test_success!(wildcard, "Wildcard_A", ServiceAddress::WILDCARD);
test_success!(wildcard_shorthand, "Wildcard", ServiceAddress::WILDCARD);
test_success!(
control_multicast,
"CS_M",
ServiceAddress::CONTROL.multicast()
);
test_success!(daemon_multicast, "DS_M", ServiceAddress::DAEMON.multicast());
test_success!(control, "CS_A", ServiceAddr::CONTROL);
test_success!(control_shorthand, "CS", ServiceAddr::CONTROL);
test_success!(daemon, "DS_A", ServiceAddr::DAEMON);
test_success!(daemon_shorthand, "DS", ServiceAddr::DAEMON);
test_success!(wildcard, "Wildcard_A", ServiceAddr::WILDCARD);
test_success!(wildcard_shorthand, "Wildcard", ServiceAddr::WILDCARD);
test_success!(control_multicast, "CS_M", ServiceAddr::CONTROL.multicast());
test_success!(daemon_multicast, "DS_M", ServiceAddr::DAEMON.multicast());
test_success!(
wildcard_multicast,
"Wildcard_M",
ServiceAddress::WILDCARD.multicast()
ServiceAddr::WILDCARD.multicast()
);

macro_rules! test_error {
($name:ident, $str:expr) => {
#[test]
fn $name() {
assert_eq!(
ServiceAddress::from_str($str).unwrap_err(),
ServiceAddr::from_str($str).unwrap_err(),
AddressParseError(AddressKind::Service)
);
}
Expand Down Expand Up @@ -209,19 +205,15 @@ mod tests {
};
}

test_display!(unknown, ServiceAddress(0xABC), "<SVC:0x0abc>");
test_display!(control, ServiceAddress::CONTROL, "CS");
test_display!(
control_multicast,
ServiceAddress::CONTROL.multicast(),
"CS_M"
);
test_display!(daemon, ServiceAddress::DAEMON, "DS");
test_display!(daemon_multicast, ServiceAddress::DAEMON.multicast(), "DS_M");
test_display!(wildcard, ServiceAddress::WILDCARD, "Wildcard");
test_display!(unknown, ServiceAddr(0xABC), "<SVC:0x0abc>");
test_display!(control, ServiceAddr::CONTROL, "CS");
test_display!(control_multicast, ServiceAddr::CONTROL.multicast(), "CS_M");
test_display!(daemon, ServiceAddr::DAEMON, "DS");
test_display!(daemon_multicast, ServiceAddr::DAEMON.multicast(), "DS_M");
test_display!(wildcard, ServiceAddr::WILDCARD, "Wildcard");
test_display!(
wildcard_multicast,
ServiceAddress::WILDCARD.multicast(),
ServiceAddr::WILDCARD.multicast(),
"Wildcard_M"
);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/scion-proto/src/address/socket_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use super::{
ScionAddrSvc,
ScionAddrV4,
ScionAddrV6,
ServiceAddress,
ServiceAddr,
};
use crate::packet::AddressInfo;

/// A SCION socket address.
///
/// SCION socket addresses consist of an ISD-AS number, a 16-bit port identifier, and either an
/// [IPv4 address][`Ipv4Addr`], an [IPv6 address][`Ipv6Addr`], or a
/// [SCION service address][`ServiceAddress`].
/// [SCION service address][`ServiceAddr`].
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum SocketAddr {
/// An IPv4 socket address.
Expand Down Expand Up @@ -306,7 +306,7 @@ socket_address! {
/// and a 16-bit port number.
///
/// See [`SocketAddr`] for a type encompassing IPv6, IPv6, and Service socket addresses.
pub struct SocketAddrSvc {scion_addr: ScionAddrSvc, host_addr: ServiceAddress, kind: AddressKind::SocketSvc};
pub struct SocketAddrSvc {scion_addr: ScionAddrSvc, host_addr: ServiceAddr, kind: AddressKind::SocketSvc};
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions crates/scion-proto/src/packet/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ mod tests {

use super::*;
use crate::{
address::{HostAddr, ServiceAddress},
address::{HostAddr, ServiceAddr},
packet::ByEndpoint,
wire_encoding::WireEncode,
};
Expand Down Expand Up @@ -362,7 +362,7 @@ mod tests {

test_checksum! {
name: ipv4_to_svc,
destination: {ia: "1-ff00:0:112", host: HostAddr::Svc(ServiceAddress::CONTROL)},
destination: {ia: "1-ff00:0:112", host: HostAddr::Svc(ServiceAddr::CONTROL)},
source: {ia: "1-ff00:0:110", host: HostAddr::V4("174.16.4.1".parse()?)},
data: b"\0\0\xaa\xbb\xcc\xdd",
protocol: 223u8,
Expand Down
6 changes: 3 additions & 3 deletions crates/scion-proto/src/packet/headers/address_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::{Buf, BufMut};

use super::AddressInfo;
use crate::{
address::{HostAddr, HostType, IsdAsn, ScionAddr, ServiceAddress, SocketAddr},
address::{HostAddr, HostType, IsdAsn, ScionAddr, ServiceAddr, SocketAddr},
packet::{ByEndpoint, DecodeError, InadequateBufferSize},
wire_encoding::{MaybeEncoded, WireDecodeWithContext, WireEncode},
};
Expand Down Expand Up @@ -152,7 +152,7 @@ where
HostType::Ipv4 => HostAddr::V4(data.get_u32().into()),
HostType::Ipv6 => HostAddr::V6(data.get_u128().into()),
HostType::Svc => {
let address = HostAddr::Svc(ServiceAddress(data.get_u16()));
let address = HostAddr::Svc(ServiceAddr(data.get_u16()));
// Remove service address's 2-byte padding
let _ = data.get_u16();

Expand Down Expand Up @@ -264,7 +264,7 @@ mod tests {

test_encode_decode! {
name: ipv6_to_service,
destination: {ia: "31-ff00:96:0", host: HostAddr::Svc(ServiceAddress::DAEMON.multicast())},
destination: {ia: "31-ff00:96:0", host: HostAddr::Svc(ServiceAddr::DAEMON.multicast())},
source: {ia: "47-ff13:0:cd", host: HostAddr::V6("2001:0db8:ac10:fe01::".parse()?)},
encoded: &[
0, 31, 0xff, 0, 0, 0x96, 0, 0,
Expand Down
10 changes: 5 additions & 5 deletions crates/scion-proto/src/reliable/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use bytes::{Buf, BufMut};

use super::wire_utils::LAYER4_PORT_OCTETS;
use crate::{
address::{HostType, IsdAsn, ServiceAddress, SocketAddr as ScionSocketAddr},
address::{HostType, IsdAsn, ServiceAddr, SocketAddr as ScionSocketAddr},
datagram::UdpDatagram,
reliable::{
wire_utils::{encoded_address_and_port_length, encoded_address_length},
Expand All @@ -46,7 +46,7 @@ pub(super) struct RegistrationRequest {
/// A separate "bind address", only used for logging by the dispatcher.
pub bind_address: Option<SocketAddr>,
/// The service to associate with
pub associated_service: Option<ServiceAddress>,
pub associated_service: Option<ServiceAddr>,
}

impl RegistrationRequest {
Expand All @@ -69,7 +69,7 @@ impl RegistrationRequest {

/// Add the provided associated service address to the request.
#[cfg(test)]
pub fn with_associated_service(mut self, address: ServiceAddress) -> Self {
pub fn with_associated_service(mut self, address: ServiceAddr) -> Self {
self.associated_service = Some(address);
self
}
Expand Down Expand Up @@ -410,15 +410,15 @@ mod tests {
test_successful!(
public_ipv4_with_service,
RegistrationRequest::new(parse!("1-ff00:0:1"), parse!("10.2.3.4:80"))
.with_associated_service(ServiceAddress::CONTROL),
.with_associated_service(ServiceAddr::CONTROL),
[0x03, 17, 0, 1, 0xff, 0, 0, 0, 0, 0x01, 0, 80, 1, 10, 2, 3, 4, 0x00, 0x02]
);

test_successful!(
with_bind_and_service,
RegistrationRequest::new(parse!("1-ff00:0:1"), parse!("10.2.3.4:80"))
.with_bind_address(parse!("10.5.6.7:81"))
.with_associated_service(ServiceAddress::CONTROL),
.with_associated_service(ServiceAddr::CONTROL),
[
0x07, 17, 0, 1, 0xff, 0, 0, 0, 0, 0x01, 0, 80, 1, 10, 2, 3, 4, 0, 81, 1, 10, 5, 6,
7, 0, 2
Expand Down
4 changes: 2 additions & 2 deletions crates/scion-proto/src/reliable/wire_utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::address::{HostType, ServiceAddress};
use crate::address::{HostType, ServiceAddr};

pub(super) const IPV4_OCTETS: usize = 4;
pub(super) const IPV6_OCTETS: usize = 16;
pub(super) const LAYER4_PORT_OCTETS: usize = 2;

pub(super) fn encoded_address_length(host_type: HostType) -> usize {
match host_type {
HostType::Svc => ServiceAddress::ENCODED_LENGTH,
HostType::Svc => ServiceAddr::ENCODED_LENGTH,
HostType::Ipv4 => IPV4_OCTETS,
HostType::Ipv6 => IPV6_OCTETS,
HostType::None => 0,
Expand Down

0 comments on commit 6e49267

Please sign in to comment.