Skip to content

Detect change on network interface to update locators and scouting #1824

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
84 changes: 84 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ regex = "1.10.6"
ron = "0.8.1"
ringbuffer-spsc = "0.1.9"
rsa = "0.9"
rtnetlink = "0.15"
rustc_version = "0.4.1"
rustls = { version = "0.23.13", default-features = false, features = [
"logging",
Expand Down
31 changes: 20 additions & 11 deletions commons/zenoh-util/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
use std::net::{IpAddr, Ipv6Addr};
use std::{
net::{IpAddr, Ipv6Addr},
sync::RwLock,
};

#[cfg(unix)]
use lazy_static::lazy_static;
#[cfg(unix)]
use pnet_datalink::NetworkInterface;
use tokio::net::{TcpSocket, UdpSocket};
use zenoh_core::zconfigurable;
use zenoh_core::{zconfigurable, zread, zwrite};
#[cfg(unix)]
use zenoh_result::zerror;
use zenoh_result::{bail, ZResult};
Expand All @@ -30,7 +33,7 @@ zconfigurable! {

#[cfg(unix)]
lazy_static! {
static ref IFACES: Vec<NetworkInterface> = pnet_datalink::interfaces();
static ref IFACES: RwLock<Vec<NetworkInterface>> = RwLock::new(pnet_datalink::interfaces());
}

#[cfg(windows)]
Expand Down Expand Up @@ -68,7 +71,7 @@ unsafe fn get_adapters_addresses(af_spec: i32) -> ZResult<Vec<u8>> {
pub fn get_interface(name: &str) -> ZResult<Option<IpAddr>> {
#[cfg(unix)]
{
for iface in IFACES.iter() {
for iface in zread!(IFACES).iter() {
if iface.name == name {
for ifaddr in &iface.ips {
if ifaddr.is_ipv4() {
Expand Down Expand Up @@ -131,7 +134,7 @@ pub fn get_interface(name: &str) -> ZResult<Option<IpAddr>> {
pub fn get_multicast_interfaces() -> Vec<IpAddr> {
#[cfg(unix)]
{
IFACES
zread!(IFACES)
.iter()
.filter_map(|iface| {
if iface.is_up() && iface.is_running() && iface.is_multicast() {
Expand All @@ -155,7 +158,7 @@ pub fn get_multicast_interfaces() -> Vec<IpAddr> {
pub fn get_local_addresses(interface: Option<&str>) -> ZResult<Vec<IpAddr>> {
#[cfg(unix)]
{
Ok(IFACES
Ok(zread!(IFACES)
.iter()
.filter(|iface| {
if let Some(interface) = interface.as_ref() {
Expand Down Expand Up @@ -205,7 +208,7 @@ pub fn get_local_addresses(interface: Option<&str>) -> ZResult<Vec<IpAddr>> {
pub fn get_unicast_addresses_of_multicast_interfaces() -> Vec<IpAddr> {
#[cfg(unix)]
{
IFACES
zread!(IFACES)
.iter()
.filter(|iface| iface.is_up() && iface.is_running() && iface.is_multicast())
.flat_map(|iface| {
Expand All @@ -228,7 +231,7 @@ pub fn get_unicast_addresses_of_multicast_interfaces() -> Vec<IpAddr> {
pub fn get_unicast_addresses_of_interface(name: &str) -> ZResult<Vec<IpAddr>> {
#[cfg(unix)]
{
match IFACES.iter().find(|iface| iface.name == name) {
match zread!(IFACES).iter().find(|iface| iface.name == name) {
Some(iface) => {
if !iface.is_up() {
bail!("Interface {name} is not up");
Expand Down Expand Up @@ -282,7 +285,7 @@ pub fn get_unicast_addresses_of_interface(name: &str) -> ZResult<Vec<IpAddr>> {
pub fn get_index_of_interface(addr: IpAddr) -> ZResult<u32> {
#[cfg(unix)]
{
IFACES
zread!(IFACES)
.iter()
.find(|iface| iface.ips.iter().any(|ipnet| ipnet.ip() == addr))
.map(|iface| iface.index)
Expand Down Expand Up @@ -319,12 +322,12 @@ pub fn get_interface_names_by_addr(addr: IpAddr) -> ZResult<Vec<String>> {
#[cfg(unix)]
{
if addr.is_unspecified() {
Ok(IFACES
Ok(zread!(IFACES)
.iter()
.map(|iface| iface.name.clone())
.collect::<Vec<String>>())
} else {
Ok(IFACES
Ok(zread!(IFACES)
.iter()
.filter(|iface| iface.ips.iter().any(|ipnet| ipnet.ip() == addr))
.map(|iface| iface.name.clone())
Expand Down Expand Up @@ -435,6 +438,12 @@ pub fn get_ipv6_ipaddrs(interface: Option<&str>) -> Vec<IpAddr> {
.collect()
}

#[cfg(unix)]
pub fn update_iface_cache() {
let mut interfaces = zwrite!(IFACES);
*interfaces = pnet_datalink::interfaces();
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn set_bind_to_device_tcp_socket(socket: &TcpSocket, iface: &str) -> ZResult<()> {
socket.bind_device(Some(iface.as_bytes()))?;
Expand Down
3 changes: 3 additions & 0 deletions zenoh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ zenoh-runtime = { workspace = true }
zenoh-task = { workspace = true }
once_cell = { workspace = true }

[target.'cfg(target_os = "linux")'.dependencies]
rtnetlink = { workspace = true }

[dev-dependencies]
tokio = { workspace = true }
libc = { workspace = true }
Expand Down
5 changes: 3 additions & 2 deletions zenoh/src/api/scouting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ use crate::{
builders::scouting::ScoutBuilder,
handlers::{Callback, DefaultHandler},
},
net::runtime::{orchestrator::Loop, Runtime},
net::runtime::{orchestrator::Loop, Runtime, Scouting},
Config,
};

/// A scout that returns [`Hello`] messages through a callback.
///
/// # Examples
Expand Down Expand Up @@ -172,7 +173,7 @@ pub(crate) fn _scout(
let task = TerminatableTask::spawn(
zenoh_runtime::ZRuntime::Acceptor,
async move {
let scout = Runtime::scout(&sockets, what, &addr, move |hello| {
let scout = Scouting::scout(&sockets, what, &addr, move |hello| {
let callback = callback.clone();
async move {
callback.call(hello.into());
Expand Down
Loading
Loading