Skip to content

Commit

Permalink
Add x_ prefix to all IP methods that collide with unstable core's.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Oct 6, 2024
1 parent 803fa87 commit b65e1b6
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/iface/interface/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl InterfaceInner {

/// Checks if an ipv4 address is unicast, taking into account subnet broadcast addresses
fn is_unicast_v4(&self, address: Ipv4Address) -> bool {
address.is_unicast() && !self.is_broadcast_v4(address)
address.x_is_unicast() && !self.is_broadcast_v4(address)
}

/// Get the first IPv4 address of the interface.
Expand Down Expand Up @@ -182,7 +182,7 @@ impl InterfaceInner {
// Ignore IP packets not directed at us, or broadcast, or any of the multicast groups.
// If AnyIP is enabled, also check if the packet is routed locally.
if !self.any_ip
|| !ipv4_repr.dst_addr.is_unicast()
|| !ipv4_repr.dst_addr.x_is_unicast()
|| self
.routes
.lookup(&IpAddress::Ipv4(ipv4_repr.dst_addr), self.now)
Expand Down Expand Up @@ -260,7 +260,7 @@ impl InterfaceInner {
}

// Discard packets with non-unicast source addresses.
if !source_protocol_addr.is_unicast() || !source_hardware_addr.is_unicast() {
if !source_protocol_addr.x_is_unicast() || !source_hardware_addr.is_unicast() {
net_debug!("arp: non-unicast source address");
return None;
}
Expand Down
27 changes: 14 additions & 13 deletions src/iface/interface/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl InterfaceInner {
}

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

// Rule 2: prefer appropriate scope.
if (candidate.address().multicast_scope() as u8)
< (addr.address().multicast_scope() as u8)
if (candidate.address().x_multicast_scope() as u8)
< (addr.address().x_multicast_scope() as u8)
{
if (candidate.address().multicast_scope() as u8)
< (dst_addr.multicast_scope() as u8)
if (candidate.address().x_multicast_scope() as u8)
< (dst_addr.x_multicast_scope() as u8)
{
candidate = addr;
}
} else if (addr.address().multicast_scope() as u8) > (dst_addr.multicast_scope() as u8)
} else if (addr.address().x_multicast_scope() as u8)
> (dst_addr.x_multicast_scope() as u8)
{
candidate = addr;
}
Expand Down Expand Up @@ -192,7 +193,7 @@ impl InterfaceInner {
) -> Option<Packet<'frame>> {
let ipv6_repr = check!(Ipv6Repr::parse(ipv6_packet));

if !ipv6_repr.src_addr.is_unicast() {
if !ipv6_repr.src_addr.x_is_unicast() {
// Discard packets with non-unicast source addresses.
net_debug!("non-unicast source address");
return None;
Expand All @@ -213,7 +214,7 @@ impl InterfaceInner {
{
// If AnyIP is enabled, also check if the packet is routed locally.
if !self.any_ip
|| !ipv6_repr.dst_addr.is_unicast()
|| !ipv6_repr.dst_addr.x_is_unicast()
|| self
.routes
.lookup(&IpAddress::Ipv6(ipv6_repr.dst_addr), self.now)
Expand All @@ -230,7 +231,7 @@ impl InterfaceInner {
let handled_by_raw_socket = false;

#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
if ipv6_repr.dst_addr.is_unicast() {
if ipv6_repr.dst_addr.x_is_unicast() {
self.neighbor_cache.reset_expiry_if_existing(
IpAddress::Ipv6(ipv6_repr.src_addr),
source_hardware_addr,
Expand Down Expand Up @@ -436,7 +437,7 @@ impl InterfaceInner {
let ip_addr = ip_repr.src_addr.into();
if let Some(lladdr) = lladdr {
let lladdr = check!(lladdr.parse(self.caps.medium));
if !lladdr.is_unicast() || !target_addr.is_unicast() {
if !lladdr.is_unicast() || !target_addr.x_is_unicast() {
return None;
}
if flags.contains(NdiscNeighborFlags::OVERRIDE)
Expand All @@ -454,7 +455,7 @@ impl InterfaceInner {
} => {
if let Some(lladdr) = lladdr {
let lladdr = check!(lladdr.parse(self.caps.medium));
if !lladdr.is_unicast() || !target_addr.is_unicast() {
if !lladdr.is_unicast() || !target_addr.x_is_unicast() {
return None;
}
self.neighbor_cache
Expand Down Expand Up @@ -492,7 +493,7 @@ impl InterfaceInner {
let src_addr = ipv6_repr.dst_addr;
let dst_addr = ipv6_repr.src_addr;

let src_addr = if src_addr.is_unicast() {
let src_addr = if src_addr.x_is_unicast() {
src_addr
} else {
self.get_source_address_ipv6(&dst_addr)
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ compile_error!("at least one socket needs to be enabled"); */
#![allow(clippy::option_map_unit_fn)]
#![allow(clippy::unit_arg)]
#![allow(clippy::new_without_default)]
#![allow(unstable_name_collisions)]

#[cfg(feature = "alloc")]
extern crate alloc;
Expand Down
6 changes: 3 additions & 3 deletions src/socket/dhcpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl<'a> Socket<'a> {

match (&mut self.state, dhcp_repr.message_type) {
(ClientState::Discovering(_state), DhcpMessageType::Offer) => {
if !dhcp_repr.your_ip.is_unicast() {
if !dhcp_repr.your_ip.x_is_unicast() {
net_debug!("DHCP ignoring OFFER because your_ip is not unicast");
return;
}
Expand Down Expand Up @@ -462,7 +462,7 @@ impl<'a> Socket<'a> {
}
};

if !dhcp_repr.your_ip.is_unicast() {
if !dhcp_repr.your_ip.x_is_unicast() {
net_debug!("DHCP ignoring ACK because your_ip is not unicast");
return None;
}
Expand All @@ -483,7 +483,7 @@ impl<'a> Socket<'a> {
.dns_servers
.iter()
.flatten()
.filter(|s| s.is_unicast())
.filter(|s| s.x_is_unicast())
.for_each(|a| {
// This will never produce an error, as both the arrays and `dns_servers`
// have length DHCP_MAX_DNS_SERVER_COUNT
Expand Down
4 changes: 2 additions & 2 deletions src/wire/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ impl Address {
pub fn is_unicast(&self) -> bool {
match self {
#[cfg(feature = "proto-ipv4")]
Address::Ipv4(addr) => addr.is_unicast(),
Address::Ipv4(addr) => addr.x_is_unicast(),
#[cfg(feature = "proto-ipv6")]
Address::Ipv6(addr) => addr.is_unicast(),
Address::Ipv6(addr) => addr.x_is_unicast(),
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/wire/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ pub(crate) trait AddressExt {
fn from_bytes(data: &[u8]) -> Self;

/// Query whether the address is an unicast address.
fn is_unicast(&self) -> bool;
///
/// `x_` prefix is to avoid a collision with the still-unstable method in `core::ip`.
fn x_is_unicast(&self) -> bool;

/// If `self` is a CIDR-compatible subnet mask, return `Some(prefix_len)`,
/// where `prefix_len` is the number of leading zeroes. Return `None` otherwise.
Expand All @@ -66,7 +68,7 @@ impl AddressExt for Address {
}

/// Query whether the address is an unicast address.
fn is_unicast(&self) -> bool {
fn x_is_unicast(&self) -> bool {
!(self.is_broadcast() || self.is_multicast() || self.is_unspecified())
}

Expand Down
62 changes: 37 additions & 25 deletions src/wire/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ pub(crate) trait AddressExt {
/// Query whether the IPv6 address is an [unicast address].
///
/// [unicast address]: https://tools.ietf.org/html/rfc4291#section-2.5
fn is_unicast(&self) -> bool;
///
/// `x_` prefix is to avoid a collision with the still-unstable method in `core::ip`.
fn x_is_unicast(&self) -> bool;

/// Query whether the IPv6 address is a [global unicast address].
///
Expand All @@ -101,7 +103,9 @@ pub(crate) trait AddressExt {
/// Query whether the IPv6 address is a [Unique Local Address] (ULA).
///
/// [Unique Local Address]: https://tools.ietf.org/html/rfc4193
fn is_unique_local(&self) -> bool;
///
/// `x_` prefix is to avoid a collision with the still-unstable method in `core::ip`.
fn x_is_unique_local(&self) -> bool;

/// Helper function used to mask an address given a prefix.
///
Expand All @@ -117,7 +121,9 @@ pub(crate) trait AddressExt {
fn solicited_node(&self) -> Address;

/// Return the scope of the address.
fn multicast_scope(&self) -> MulticastScope;
///
/// `x_` prefix is to avoid a collision with the still-unstable method in `core::ip`.
fn x_multicast_scope(&self) -> MulticastScope;

/// If `self` is a CIDR-compatible subnet mask, return `Some(prefix_len)`,
/// where `prefix_len` is the number of leading zeroes. Return `None` otherwise.
Expand All @@ -131,7 +137,7 @@ impl AddressExt for Address {
Address::from(bytes)
}

fn is_unicast(&self) -> bool {
fn x_is_unicast(&self) -> bool {
!(self.is_multicast() || self.is_unspecified())
}

Expand All @@ -143,7 +149,7 @@ impl AddressExt for Address {
self.octets()[0..8] == [0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
}

fn is_unique_local(&self) -> bool {
fn x_is_unique_local(&self) -> bool {
(self.octets()[0] & 0b1111_1110) == 0xfc
}

Expand All @@ -163,22 +169,22 @@ impl AddressExt for Address {
}

fn solicited_node(&self) -> Address {
assert!(self.is_unicast());
assert!(self.x_is_unicast());
let o = self.octets();
Address::from([
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, o[13],
o[14], o[15],
])
}

fn multicast_scope(&self) -> MulticastScope {
fn x_multicast_scope(&self) -> MulticastScope {
if self.is_multicast() {
return MulticastScope::from(self.octets()[1] & 0b1111);
}

if self.is_link_local() {
MulticastScope::LinkLocal
} else if self.is_unique_local() || self.is_global_unicast() {
} else if self.x_is_unique_local() || self.is_global_unicast() {
// ULA are considered global scope
// https://www.rfc-editor.org/rfc/rfc6724#section-3.1
MulticastScope::Global
Expand Down Expand Up @@ -680,13 +686,13 @@ pub(crate) mod test {
assert!(LINK_LOCAL_ALL_ROUTERS.is_multicast());
assert!(!LINK_LOCAL_ALL_ROUTERS.is_link_local());
assert!(!LINK_LOCAL_ALL_ROUTERS.is_loopback());
assert!(!LINK_LOCAL_ALL_ROUTERS.is_unique_local());
assert!(!LINK_LOCAL_ALL_ROUTERS.x_is_unique_local());
assert!(!LINK_LOCAL_ALL_ROUTERS.is_global_unicast());
assert!(!LINK_LOCAL_ALL_NODES.is_unspecified());
assert!(LINK_LOCAL_ALL_NODES.is_multicast());
assert!(!LINK_LOCAL_ALL_NODES.is_link_local());
assert!(!LINK_LOCAL_ALL_NODES.is_loopback());
assert!(!LINK_LOCAL_ALL_NODES.is_unique_local());
assert!(!LINK_LOCAL_ALL_NODES.x_is_unique_local());
assert!(!LINK_LOCAL_ALL_NODES.is_global_unicast());
}

Expand All @@ -696,7 +702,7 @@ pub(crate) mod test {
assert!(!LINK_LOCAL_ADDR.is_multicast());
assert!(LINK_LOCAL_ADDR.is_link_local());
assert!(!LINK_LOCAL_ADDR.is_loopback());
assert!(!LINK_LOCAL_ADDR.is_unique_local());
assert!(!LINK_LOCAL_ADDR.x_is_unique_local());
assert!(!LINK_LOCAL_ADDR.is_global_unicast());
}

Expand All @@ -706,7 +712,7 @@ pub(crate) mod test {
assert!(!Address::LOCALHOST.is_multicast());
assert!(!Address::LOCALHOST.is_link_local());
assert!(Address::LOCALHOST.is_loopback());
assert!(!Address::LOCALHOST.is_unique_local());
assert!(!Address::LOCALHOST.x_is_unique_local());
assert!(!Address::LOCALHOST.is_global_unicast());
}

Expand All @@ -716,7 +722,7 @@ pub(crate) mod test {
assert!(!UNIQUE_LOCAL_ADDR.is_multicast());
assert!(!UNIQUE_LOCAL_ADDR.is_link_local());
assert!(!UNIQUE_LOCAL_ADDR.is_loopback());
assert!(UNIQUE_LOCAL_ADDR.is_unique_local());
assert!(UNIQUE_LOCAL_ADDR.x_is_unique_local());
assert!(!UNIQUE_LOCAL_ADDR.is_global_unicast());
}

Expand All @@ -726,7 +732,7 @@ pub(crate) mod test {
assert!(!GLOBAL_UNICAST_ADDR.is_multicast());
assert!(!GLOBAL_UNICAST_ADDR.is_link_local());
assert!(!GLOBAL_UNICAST_ADDR.is_loopback());
assert!(!GLOBAL_UNICAST_ADDR.is_unique_local());
assert!(!GLOBAL_UNICAST_ADDR.x_is_unique_local());
assert!(GLOBAL_UNICAST_ADDR.is_global_unicast());
}

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

assert_eq!(
LINK_LOCAL_ALL_NODES.multicast_scope(),
LINK_LOCAL_ALL_NODES.x_multicast_scope(),
MulticastScope::LinkLocal
);

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

static REPR_PACKET_BYTES: [u8; 52] = [
Expand Down

0 comments on commit b65e1b6

Please sign in to comment.