Open alexkirsz opened 1 year ago
Hi @alexkirsz thanks for openning this feature request!
Are you planning to work on this?
Code borrowed from if_addrs
:
fn is_loopback_ipv6(ip: Ipv6Addr) -> bool {
ip.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
}
fn is_loopback_ipv4(ip: Ipv4Addr) -> bool {
ip.octets()[0] == 127
}
We use this:
let network_interfaces = NetworkInterface::show()?;
let network_interfaces = network_interfaces
.into_iter()
.filter(|itf| !itf.name.starts_with("lo"))
.collect::<Vec<NetworkInterface>>();
@nvandamme I think it would be nice to get a flag from the OS whether the IF is loopback or not and don't decide on its address.
@utkarshgupta137 Your app code seems to be platform specific (Linux/UNIX) and might not work on Windows. Also I think it's risky to just test for IF name to start with "lo".
By the way: Despite not having a solution (I guess @alexkirsz might go in the right direction?) either, here is my workaround I'm using in my app code:
let network_interfaces = NetworkInterface::show().unwrap();
for itf in network_interfaces.iter() {
if let Some(mac_addr) = &itf.mac_addr {
if mac_addr == "00:00:00:00:00:00" { // <- not strictly lo related, but I'd like to skip those anyway
continue;
}
for addr in &itf.addr {
let ip = addr.ip();
if ip.is_loopback() { // <- HERE; checks both IPv4 and IPv6
continue;
}
// ...
}
};
}
@podarcis Why ? Either way, local interfaces are following RFC's adresses allocations.... And if the OS allows otherwise, it is another level of problems awaiting network stack usage anyway...
@nvandamme My initial thinking was that the OS knows best about whether it's a loopback or not. And I thought about other interfaces besides AF_PACKET
, AF_INET
and AF_INET6
might have the concept of loopback interfaces, however, it's the only types that are supported with this library (at least for now and Linux). So there might be no point in going the extra mile.
So what about NetworkInterface
implements a function is_loopback()
that simply iterates over the containing IPv4/IPv6 address vector and returns true on the first addr.ip().is_loopback()
?
@nvandamme My initial thinking was that the OS knows best about whether it's a loopback or not. And I thought about other interfaces besides
AF_PACKET
,AF_INET
andAF_INET6
might have the concept of loopback interfaces, however, it's the only types that are supported with this library (at least for now and Linux). So there might be no point in going the extra mile.So what about
NetworkInterface
implements a functionis_loopback()
that simply iterates over the containing IPv4/IPv6 address vector and returns true on the firstaddr.ip().is_loopback()
?
@podarcis, ok, for example cases using lo aliases with routed IPs, like management IPs on router and switches (linux/unix sure can handle this, but windows, I'm not sure) ? And in this case, the allocated IPs are fully routed to all network routes, so not really acting as a pure loopback IP anymore.
The only other case would be others layer 2 ethernet protocoles that might use a form of loopback (profinet, 6lowpan...)
@EstebanBorai What do you think? Expose a function is_loopback()
on the interface checking on its adresses for loopback? Or just add a recipe to the README (see my example above)?
Hey!
I'm looking for the same functionality as the
is_loopback()
method of thepnet_datalink
crate.FWIW, here's what GPT-4 generated to do this: