Skip to content

Commit

Permalink
rename Scope to MulticastScope
Browse files Browse the repository at this point in the history
It was not clear what scope meant. Renaming it to MulticastScope makes
it clear that it is only used for multicast addresses.
  • Loading branch information
thvdveld committed Jan 15, 2024
1 parent 67f0528 commit c7de00a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
15 changes: 10 additions & 5 deletions src/iface/interface/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ impl InterfaceInner {
}

if dst_addr.is_multicast()
&& matches!(dst_addr.scope(), Ipv6AddressScope::LinkLocal)
&& matches!(dst_addr.multicast_scope(), Ipv6MulticastScope::LinkLocal)
&& src_addr.is_multicast()
&& !matches!(src_addr.scope(), Ipv6AddressScope::LinkLocal)
&& !matches!(src_addr.multicast_scope(), Ipv6MulticastScope::LinkLocal)
{
return false;
}
Expand Down Expand Up @@ -96,11 +96,16 @@ impl InterfaceInner {
}

// Rule 2: prefer appropriate scope.
if (candidate.address().scope() as u8) < (addr.address().scope() as u8) {
if (candidate.address().scope() as u8) < (dst_addr.scope() as u8) {
if (candidate.address().multicast_scope() as u8)
< (addr.address().multicast_scope() as u8)
{
if (candidate.address().multicast_scope() as u8)
< (dst_addr.multicast_scope() as u8)
{
candidate = addr;
}
} else if (addr.address().scope() as u8) > (dst_addr.scope() as u8) {
} else if (addr.address().multicast_scope() as u8) > (dst_addr.multicast_scope() as u8)
{
candidate = addr;
}

Expand Down
56 changes: 31 additions & 25 deletions src/wire/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub const IPV4_MAPPED_PREFIX_SIZE: usize = ADDR_SIZE - 4; // 4 == ipv4::ADDR_SIZ
/// [scope]: https://www.rfc-editor.org/rfc/rfc4291#section-2.7
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Scope {
pub(crate) enum MulticastScope {
/// Interface Local scope
InterfaceLocal = 0x1,
/// Link local scope
Expand All @@ -47,7 +47,7 @@ pub(crate) enum Scope {
Unknown = 0xFF,
}

impl From<u8> for Scope {
impl From<u8> for MulticastScope {
fn from(value: u8) -> Self {
match value {
0x1 => Self::InterfaceLocal,
Expand Down Expand Up @@ -280,19 +280,19 @@ impl Address {
}

/// Return the scope of the address.
pub(crate) fn scope(&self) -> Scope {
pub(crate) fn multicast_scope(&self) -> MulticastScope {
if self.is_multicast() {
return Scope::from(self.as_bytes()[1] & 0b1111);
return MulticastScope::from(self.as_bytes()[1] & 0b1111);
}

if self.is_link_local() {
Scope::LinkLocal
MulticastScope::LinkLocal
} else if self.is_unique_local() || self.is_global_unicast() {
// ULA are considered global scope
// https://www.rfc-editor.org/rfc/rfc6724#section-3.1
Scope::Global
MulticastScope::Global
} else {
Scope::Unknown
MulticastScope::Unknown
}
}

Expand Down Expand Up @@ -1263,40 +1263,46 @@ pub(crate) mod test {
fn test_scope() {
use super::*;
assert_eq!(
Address::new(0xff01, 0, 0, 0, 0, 0, 0, 1).scope(),
Scope::InterfaceLocal
Address::new(0xff01, 0, 0, 0, 0, 0, 0, 1).multicast_scope(),
MulticastScope::InterfaceLocal
);
assert_eq!(
Address::new(0xff02, 0, 0, 0, 0, 0, 0, 1).scope(),
Scope::LinkLocal
Address::new(0xff02, 0, 0, 0, 0, 0, 0, 1).multicast_scope(),
MulticastScope::LinkLocal
);
assert_eq!(
Address::new(0xff03, 0, 0, 0, 0, 0, 0, 1).scope(),
Scope::Unknown
Address::new(0xff03, 0, 0, 0, 0, 0, 0, 1).multicast_scope(),
MulticastScope::Unknown
);
assert_eq!(
Address::new(0xff04, 0, 0, 0, 0, 0, 0, 1).scope(),
Scope::AdminLocal
Address::new(0xff04, 0, 0, 0, 0, 0, 0, 1).multicast_scope(),
MulticastScope::AdminLocal
);
assert_eq!(
Address::new(0xff05, 0, 0, 0, 0, 0, 0, 1).scope(),
Scope::SiteLocal
Address::new(0xff05, 0, 0, 0, 0, 0, 0, 1).multicast_scope(),
MulticastScope::SiteLocal
);
assert_eq!(
Address::new(0xff08, 0, 0, 0, 0, 0, 0, 1).scope(),
Scope::OrganizationLocal
Address::new(0xff08, 0, 0, 0, 0, 0, 0, 1).multicast_scope(),
MulticastScope::OrganizationLocal
);
assert_eq!(
Address::new(0xff0e, 0, 0, 0, 0, 0, 0, 1).scope(),
Scope::Global
Address::new(0xff0e, 0, 0, 0, 0, 0, 0, 1).multicast_scope(),
MulticastScope::Global
);

assert_eq!(Address::LINK_LOCAL_ALL_NODES.scope(), Scope::LinkLocal);
assert_eq!(
Address::LINK_LOCAL_ALL_NODES.multicast_scope(),
MulticastScope::LinkLocal
);

// For source address selection, unicast addresses also have a scope:
assert_eq!(LINK_LOCAL_ADDR.scope(), Scope::LinkLocal);
assert_eq!(GLOBAL_UNICAST_ADDR.scope(), Scope::Global);
assert_eq!(UNIQUE_LOCAL_ADDR.scope(), Scope::Global);
assert_eq!(LINK_LOCAL_ADDR.multicast_scope(), MulticastScope::LinkLocal);
assert_eq!(
GLOBAL_UNICAST_ADDR.multicast_scope(),
MulticastScope::Global
);
assert_eq!(UNIQUE_LOCAL_ADDR.multicast_scope(), MulticastScope::Global);
}

static REPR_PACKET_BYTES: [u8; 52] = [
Expand Down
2 changes: 1 addition & 1 deletion src/wire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub use self::ipv4::{
};

#[cfg(feature = "proto-ipv6")]
pub(crate) use self::ipv6::Scope as Ipv6AddressScope;
pub(crate) use self::ipv6::MulticastScope as Ipv6MulticastScope;
#[cfg(feature = "proto-ipv6")]
pub use self::ipv6::{
Address as Ipv6Address, Cidr as Ipv6Cidr, Packet as Ipv6Packet, Repr as Ipv6Repr,
Expand Down

0 comments on commit c7de00a

Please sign in to comment.