Skip to content

Commit 7c4a8e4

Browse files
author
Julian Monticelli
committed
feat: Add TTL connection param to multicast
1 parent 6f7c089 commit 7c4a8e4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

io/zenoh-links/zenoh-link-udp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ impl LocatorInspector for UdpLocatorInspector {
107107
pub mod config {
108108
pub const UDP_MULTICAST_IFACE: &str = "iface";
109109
pub const UDP_MULTICAST_JOIN: &str = "join";
110+
pub const UDP_MULTICAST_TTL: &str = "ttl";
110111
}
111112

112113
pub async fn get_udp_addrs(address: Address<'_>) -> ZResult<impl Iterator<Item = SocketAddr>> {

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl LinkManagerMulticastUdp {
272272
.map_err(|e| zerror!("{}: {}", mcast_addr, e))?;
273273
}
274274

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

324+
// If TTL is specified, add set the socket's TTL
325+
if let Some(ttl_str) = config.get(UDP_MULTICAST_TTL) {
326+
let ttl = match ttl_str.parse::<u32>() {
327+
Ok(ttl) => ttl,
328+
Err(e) => bail!("Can not parse TTL '{}' to a u32: {}", ttl_str, e)
329+
};
330+
331+
match &local_addr {
332+
IpAddr::V4(_) => {
333+
ucast_sock
334+
.set_multicast_ttl_v4(ttl)
335+
.map_err(|e| zerror!("{}: {}", mcast_addr, e))?;
336+
}
337+
IpAddr::V6(_) => match zenoh_util::net::get_index_of_interface(local_addr) {
338+
Ok(_) => {
339+
tracing::warn!("set_multicast_ttl_v4 on v6 socket (may have no effect): {}", mcast_addr);
340+
ucast_sock
341+
.set_multicast_ttl_v4(ttl)
342+
.map_err(|e| zerror!("{}: {}", mcast_addr, e))?
343+
},
344+
Err(e) => bail!("{}: {}", mcast_addr, e),
345+
},
346+
}
347+
}
348+
324349
// Build the tokio multicast UdpSocket
325350
let mcast_sock = UdpSocket::from_std(mcast_sock.into())?;
326351

0 commit comments

Comments
 (0)