use core::net::{IpAddr, Ipv4Addr};
use core::num::NonZeroU32;
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct UdpOptions {
pub recv_buffer_bytes: Option<NonZeroU32>,
pub reuse_address: bool,
pub reuse_port: bool,
pub join: Option<MulticastJoin>,
}
impl UdpOptions {
#[must_use]
pub const fn new() -> Self {
Self {
recv_buffer_bytes: None,
reuse_address: false,
reuse_port: false,
join: None,
}
}
#[must_use]
pub const fn with_recv_buffer_bytes(mut self, bytes: NonZeroU32) -> Self {
self.recv_buffer_bytes = Some(bytes);
self
}
#[must_use]
pub const fn with_reuse_address(mut self, on: bool) -> Self {
self.reuse_address = on;
self
}
#[must_use]
pub const fn with_reuse_port(mut self, on: bool) -> Self {
self.reuse_port = on;
self
}
#[must_use]
pub fn with_join(mut self, join: MulticastJoin) -> Self {
self.join = Some(join);
self
}
pub fn validate(&self) -> Result<(), JoinMismatch> {
let Some(join) = &self.join else {
return Ok(());
};
let group = join.group;
let family_ok = match (group, &join.interface) {
(IpAddr::V4(_), MulticastInterface::V4(_) | MulticastInterface::Any)
| (IpAddr::V4(_), MulticastInterface::Name(_)) => true,
(IpAddr::V6(_), MulticastInterface::V6Index(_) | MulticastInterface::Any)
| (IpAddr::V6(_), MulticastInterface::Name(_)) => true,
(IpAddr::V4(_), MulticastInterface::V6Index(_))
| (IpAddr::V6(_), MulticastInterface::V4(_)) => false,
};
if !family_ok {
return Err(JoinMismatch::Family);
}
if !group.is_multicast() && !group.is_unspecified() {
return Err(JoinMismatch::Group);
}
match join.source {
Some(source) if source.is_ipv4() != group.is_ipv4() => Err(JoinMismatch::SourceFamily),
Some(source) if source.is_multicast() => Err(JoinMismatch::SourceFamily),
_ => Ok(()),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MulticastJoin {
pub group: IpAddr,
pub interface: MulticastInterface,
pub source: Option<IpAddr>,
}
impl MulticastJoin {
#[must_use]
pub const fn new(group: IpAddr) -> Self {
Self {
group,
interface: MulticastInterface::Any,
source: None,
}
}
#[must_use]
pub fn with_interface(mut self, interface: MulticastInterface) -> Self {
self.interface = interface;
self
}
#[must_use]
pub fn with_group(mut self, group: IpAddr) -> Self {
self.group = group;
self
}
#[must_use]
pub const fn with_source(mut self, source: IpAddr) -> Self {
self.source = Some(source);
self
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub enum MulticastInterface {
#[default]
Any,
V4(Ipv4Addr),
V6Index(u32),
Name(alloc::string::String),
}
impl MulticastInterface {
pub fn parse(text: &str) -> Result<Self, InterfaceError> {
if let Ok(address) = text.parse::<Ipv4Addr>() {
return Ok(Self::V4(address));
}
let numeric = !text.is_empty() && text.bytes().all(|b| matches!(b, b'0'..=b'9' | b'.'));
if !numeric {
return Ok(Self::Name(alloc::string::String::from(text)));
}
match text.parse::<u32>() {
Ok(index) => Ok(Self::V6Index(index)),
Err(_) if text.bytes().all(|b| b.is_ascii_digit()) => {
Err(InterfaceError::Index(text.into()))
}
Err(_) => Err(InterfaceError::Address(text.into())),
}
}
#[must_use]
pub fn as_str(&self) -> alloc::borrow::Cow<'_, str> {
use alloc::string::ToString as _;
match self {
Self::Any => "any".into(),
Self::V4(address) => address.to_string().into(),
Self::V6Index(index) => index.to_string().into(),
Self::Name(name) => name.as_str().into(),
}
}
#[must_use]
pub const fn ipv4(&self) -> Ipv4Addr {
match self {
Self::V4(address) => *address,
Self::Any | Self::V6Index(_) | Self::Name(_) => Ipv4Addr::UNSPECIFIED,
}
}
#[must_use]
pub const fn ipv6_index(&self) -> u32 {
match self {
Self::V6Index(index) => *index,
Self::Any | Self::V4(_) | Self::Name(_) => 0,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InterfaceError {
Address(alloc::string::String),
Index(alloc::string::String),
}
impl InterfaceError {
#[must_use]
pub const fn name(&self) -> &'static str {
match self {
Self::Address(_) => "not an IPv4 address (and not an interface name or index)",
Self::Index(_) => "not an interface index (above the u32 range)",
}
}
}
impl core::fmt::Display for InterfaceError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.name())?;
f.write_str(": ")?;
match self {
Self::Address(text) | Self::Index(text) => f.write_str(text),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for InterfaceError {}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinMismatch {
Group,
Family,
SourceFamily,
}
impl JoinMismatch {
#[must_use]
pub const fn reason(self) -> &'static str {
match self {
Self::Group => "the joined address is not a multicast group",
Self::Family => "multicast interface kind does not match the group's address family",
Self::SourceFamily => {
"multicast source must be a unicast address of the group's family"
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use core::net::Ipv6Addr;
const GROUP4: IpAddr = IpAddr::V4(Ipv4Addr::new(239, 255, 42, 42));
const GROUP6: IpAddr = IpAddr::V6(Ipv6Addr::new(0xff02, 0, 0, 0, 0, 0, 0, 1));
#[test]
fn defaults_request_nothing() {
let options = UdpOptions::default();
assert_eq!(options, UdpOptions::new());
assert_eq!(options.recv_buffer_bytes, None);
assert!(!options.reuse_address);
assert!(!options.reuse_port);
assert_eq!(options.join, None);
}
#[test]
fn the_setters_carry_each_option() {
let join = MulticastJoin::new(GROUP4)
.with_interface(MulticastInterface::V4(Ipv4Addr::new(192, 168, 1, 10)))
.with_source(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 7)));
let options = UdpOptions::new()
.with_recv_buffer_bytes(NonZeroU32::new(4 << 20).unwrap())
.with_reuse_address(true)
.with_reuse_port(false)
.with_join(join.clone());
assert_eq!(options.recv_buffer_bytes, NonZeroU32::new(4 << 20));
assert!(options.reuse_address);
assert!(!options.reuse_port);
assert_eq!(options.join, Some(join.clone()));
assert_eq!(join.group, GROUP4);
assert_eq!(
join.interface.ipv4(),
Ipv4Addr::new(192, 168, 1, 10),
"an IPv4 address joins with itself"
);
}
#[test]
fn a_receive_buffer_request_is_never_zero() {
assert_eq!(NonZeroU32::new(0), None);
assert_eq!(
UdpOptions::new().with_recv_buffer_bytes(NonZeroU32::MIN),
UdpOptions {
recv_buffer_bytes: NonZeroU32::new(1),
..UdpOptions::new()
}
);
let options = UdpOptions::new().with_recv_buffer_bytes(NonZeroU32::MAX);
assert_eq!(options.recv_buffer_bytes, NonZeroU32::new(u32::MAX));
}
#[test]
fn one_interface_is_either_any_an_address_an_index_or_a_name() {
assert_eq!(MulticastInterface::Any.ipv4(), Ipv4Addr::UNSPECIFIED);
assert_eq!(MulticastInterface::Any.ipv6_index(), 0);
assert_eq!(MulticastInterface::V6Index(3).ipv4(), Ipv4Addr::UNSPECIFIED);
assert_eq!(MulticastInterface::V6Index(3).ipv6_index(), 3);
assert_eq!(MulticastInterface::V4(Ipv4Addr::LOCALHOST).ipv6_index(), 0);
assert_eq!(
MulticastInterface::Name("eth0".into()).ipv4(),
Ipv4Addr::UNSPECIFIED
);
assert_eq!(MulticastInterface::Name("eth0".into()).ipv6_index(), 0);
}
#[test]
fn an_interface_parses_from_text_as_address_then_index_then_name() {
for (text, want) in [
(
"192.168.1.10",
MulticastInterface::V4(Ipv4Addr::new(192, 168, 1, 10)),
),
("3", MulticastInterface::V6Index(3)),
("eth0", MulticastInterface::Name("eth0".into())),
("en0.100", MulticastInterface::Name("en0.100".into())),
] {
let interface = MulticastInterface::parse(text).unwrap();
assert_eq!(interface, want, "{text}");
assert_eq!(interface.as_str(), text, "{text} round-trips");
}
assert_eq!(MulticastInterface::Any.as_str(), "any");
for bad in ["300.1.1.1", "4294967296"] {
assert!(MulticastInterface::parse(bad).is_err(), "{bad}");
}
}
#[test]
fn a_matching_join_validates() {
for (group, interface) in [
(GROUP4, MulticastInterface::Any),
(GROUP6, MulticastInterface::Any),
(GROUP4, MulticastInterface::V4(Ipv4Addr::LOCALHOST)),
(GROUP6, MulticastInterface::V6Index(1)),
(GROUP4, MulticastInterface::Name("eth0".into())),
(GROUP6, MulticastInterface::Name("eth0".into())),
] {
let options = UdpOptions::new()
.with_join(MulticastJoin::new(group).with_interface(interface.clone()));
assert_eq!(options.validate(), Ok(()), "{group} on {interface:?}");
}
assert_eq!(UdpOptions::default().validate(), Ok(()));
}
#[test]
fn a_join_of_the_wrong_family_is_refused() {
let v6_index = UdpOptions::new()
.with_join(MulticastJoin::new(GROUP4).with_interface(MulticastInterface::V6Index(1)));
assert_eq!(v6_index.validate(), Err(JoinMismatch::Family));
let v4_address = UdpOptions::new().with_join(
MulticastJoin::new(GROUP6).with_interface(MulticastInterface::V4(Ipv4Addr::LOCALHOST)),
);
assert_eq!(v4_address.validate(), Err(JoinMismatch::Family));
}
#[test]
fn a_unicast_join_target_is_refused() {
let unicast = IpAddr::V4(Ipv4Addr::LOCALHOST);
let options = UdpOptions::new().with_join(MulticastJoin::new(unicast));
assert_eq!(options.validate(), Err(JoinMismatch::Group));
}
#[test]
fn an_unspecified_join_group_defers_to_the_bind_address() {
let options =
UdpOptions::new().with_join(MulticastJoin::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED)));
assert_eq!(options.validate(), Ok(()));
}
#[test]
fn a_source_specific_join_checks_the_source() {
let with_source = |source: IpAddr| {
UdpOptions::new().with_join(MulticastJoin::new(GROUP4).with_source(source))
};
assert_eq!(
with_source(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 7))).validate(),
Ok(())
);
assert_eq!(
with_source(IpAddr::V6(Ipv6Addr::LOCALHOST)).validate(),
Err(JoinMismatch::SourceFamily)
);
assert_eq!(
with_source(GROUP4).validate(),
Err(JoinMismatch::SourceFamily),
"a group is not a source"
);
}
}