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 all 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 @@
.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 @@
// 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 {

Check warning on line 326 in io/zenoh-links/zenoh-link-udp/src/multicast.rs

View check run for this annotation

Codecov / codecov/patch

io/zenoh-links/zenoh-link-udp/src/multicast.rs#L326

Added line #L326 was not covered by tests
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),

Check warning on line 330 in io/zenoh-links/zenoh-link-udp/src/multicast.rs

View check run for this annotation

Codecov / codecov/patch

io/zenoh-links/zenoh-link-udp/src/multicast.rs#L328-L330

Added lines #L328 - L330 were not covered by tests
};

ucast_sock.set_multicast_ttl_v4(ttl).map_err(|e| {
zerror!("Can not set multicast TTL {} on {}: {}", ttl, mcast_addr, e)
})?;

Check warning on line 335 in io/zenoh-links/zenoh-link-udp/src/multicast.rs

View check run for this annotation

Codecov / codecov/patch

io/zenoh-links/zenoh-link-udp/src/multicast.rs#L333-L335

Added lines #L333 - L335 were not covered by tests
}
IpAddr::V6(_) => {
tracing::warn!(
"UDP multicast hop limit not supported for v6 socket: {}. See https://github.com/rust-lang/rust/pull/138744.",

Check warning on line 339 in io/zenoh-links/zenoh-link-udp/src/multicast.rs

View check run for this annotation

Codecov / codecov/patch

io/zenoh-links/zenoh-link-udp/src/multicast.rs#L338-L339

Added lines #L338 - L339 were not covered by tests
mcast_addr
);
}
}
}

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

Expand Down