-
Notifications
You must be signed in to change notification settings - Fork 27
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
Check if a network interface is a loopback interface #39
Comments
Hi @alexkirsz thanks for openning this feature request! Are you planning to work on this? |
Code borrowed from 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 So what about |
@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 |
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:
The text was updated successfully, but these errors were encountered: