Skip to content
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

Unexpected "\0" in the network interface name #110

Closed
bitcapybara opened this issue Mar 8, 2023 · 3 comments · Fixed by #111
Closed

Unexpected "\0" in the network interface name #110

bitcapybara opened this issue Mar 8, 2023 · 3 comments · Fixed by #111

Comments

@bitcapybara
Copy link
Contributor

Environment

  1. Linux version: windows11 wsl2
$ uname -a
Linux bitcapybara 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 GNU/Linux
  1. ip
$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:2b:e5:eb brd ff:ff:ff:ff:ff:ff
    inet 172.x.x.x/20 brd 172.x.x.x scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::x:x:x:x/64 scope link
       valid_lft forever preferred_lft forever

Code

use local_ip_address::list_afinet_netifas;

fn main() {
    let ifas = list_afinet_netifas().unwrap();
    println!("{:?}", ifas);
}

Expected output

$ cargo run
[("lo", 127.0.0.1), ("eth0", 172.x.x.x), ("lo", ::1), ("eth0", fe80::x:x:x:x)]

Actual output

$ cargo run
[("lo\0", 127.0.0.1), ("eth0\0", 172.x.x.x), ("lo\0", ::1), ("eth0\0", fe80::x:x:x:x)]

Conclusion

As shown above, the network interface name in all output has an extra "\0". Is this normal? And how can I solve it?

@LeoBorai
Copy link
Owner

LeoBorai commented Mar 9, 2023

Hi @bitcapybara! Thanks for raising this issue!

This looks related to how OsStr is parsed on windows 🤔 will give it a shot!
Please feel free to open a PR if you like to and count on me for all the help I can bring!

@bitcapybara
Copy link
Contributor Author

bitcapybara commented Mar 9, 2023

It seems to have this problem when using v0.5.2. v0.5.1 version is ok

@bitcapybara
Copy link
Contributor Author

bitcapybara commented Mar 9, 2023

I tried to use CStr :: from_bytes_with_nul to solve this problem, in file src/linux.rs, In line 238 and line 308:

let ifname = CStr::from_bytes_with_nul(rtattr.payload().as_ref())
    .map_err(|_| {
        Error::StrategyError(String::from(
            "An error occurred convert interface name to string",
        ))
    })?
    .to_string_lossy()
    .to_string();
if_indexes.insert(p.ifi_index, ifname)

But I'm not sure if the change here will affect other places

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants