Skip to content

feat: Add TTL connection param to multicast #1842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions io/zenoh-links/zenoh-link-udp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl LocatorInspector for UdpLocatorInspector {
pub mod config {
pub const UDP_MULTICAST_IFACE: &str = "iface";
pub const UDP_MULTICAST_JOIN: &str = "join";
pub const UDP_MULTICAST_TTL: &str = "ttl";
}

pub async fn get_udp_addrs(address: Address<'_>) -> ZResult<impl Iterator<Item = SocketAddr>> {
Expand Down
24 changes: 23 additions & 1 deletion io/zenoh-links/zenoh-link-udp/src/multicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl LinkManagerMulticastUdp {
.map_err(|e| zerror!("{}: {}", mcast_addr, e))?;
}

// Bind the socket: let's bing to the unspecified address so we can join and read
// Bind the socket: let's bind to the unspecified address so we can join and read
// from multiple multicast groups.
let bind_mcast_addr = match mcast_addr.ip() {
IpAddr::V4(_) => IpAddr::V4(Ipv4Addr::UNSPECIFIED),
Expand Down Expand Up @@ -321,6 +321,28 @@ impl LinkManagerMulticastUdp {
// https://docs.rs/tokio/latest/tokio/net/struct.UdpSocket.html#notes
mcast_sock.set_nonblocking(true)?;

// If TTL is specified, add set the socket's TTL
if let Some(ttl_str) = config.get(UDP_MULTICAST_TTL) {
match &local_addr {
IpAddr::V4(_) => {
let ttl = match ttl_str.parse::<u32>() {
Ok(ttl) => ttl,
Err(e) => bail!("Can not parse TTL '{}' to a u32: {}", ttl_str, e),
};

ucast_sock.set_multicast_ttl_v4(ttl).map_err(|e| {
zerror!("Can not set multicast TTL {} on {}: {}", ttl, mcast_addr, e)
})?;
}
IpAddr::V6(_) => {
tracing::warn!(
"UDP multicast hop limit not supported for v6 socket: {}. See https://github.com/rust-lang/rust/pull/138744.",
mcast_addr
);
}
}
}

// Build the tokio multicast UdpSocket
let mcast_sock = UdpSocket::from_std(mcast_sock.into())?;

Expand Down
Loading