Skip to content

Conversation

@MarkusPettersson98
Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 commented Dec 2, 2025

This PR aims to write talpid-net/src/unix.rs in a more idiomatic way by wrapping the call to ioctl in a struct with a vetted constructor (IfReq::new) and safe methods. I managed to consolidate some unsafe blocks as well, which reduced the number of unsafe blocks from 7 to 5.:)


This change is Reviewable

Copy link
Contributor

@hulthe hulthe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @MarkusPettersson98)


talpid-net/src/unix.rs line 59 at r1 (raw file):

        let interface_name = interface.as_bytes();
        if interface_name.len() > nix::libc::IF_NAMESIZE {
            return Err(invalid_input("Interface name too long"));

I think, based on this SO-thread i found, that you need to leave space for a null bytes at the end. So this is the correct comparison (note the >=):

// Check that the name (plus a null byte) fits into IF_NAMESIZE
if interface_name.len() >= nix::libc::IF_NAMESIZE {
    return Err(invalid_input("Interface name too long"));
}

Copy link
Contributor

@hulthe hulthe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 files reviewed, 2 unresolved discussions (waiting on @MarkusPettersson98)


talpid-net/src/unix.rs line 64 at r1 (raw file):

        let mut ifr: ifreq = unsafe { mem::zeroed() };
        // SAFETY: `interface_name.len()` does not exceed IF_NAMESIZE and `interface_name` only
        // contains ASCII.

Write something about the null terminator here also maybe

@hulthe hulthe self-requested a review December 2, 2025 15:20
Copy link
Contributor Author

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @hulthe)


talpid-net/src/unix.rs line 59 at r1 (raw file):

Previously, hulthe (Joakim Hulthe) wrote…

I think, based on this SO-thread i found, that you need to leave space for a null bytes at the end. So this is the correct comparison (note the >=):

// Check that the name (plus a null byte) fits into IF_NAMESIZE
if interface_name.len() >= nix::libc::IF_NAMESIZE {
    return Err(invalid_input("Interface name too long"));
}

Good catch! I interpreted the netdevice source code the same way: https://www.man7.org/linux/man-pages/man7/netdevice.7.html


talpid-net/src/unix.rs line 64 at r1 (raw file):

Previously, hulthe (Joakim Hulthe) wrote…

Write something about the null terminator here also maybe

Done

Copy link
Contributor

@hulthe hulthe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this could even be upstreamed to nix :lgtm:

@hulthe reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @MarkusPettersson98)


talpid-net/src/unix.rs line 74 at r2 (raw file):

Domain::IPV4

I know you didn't change this, but does it really work for ipv6?

@MarkusPettersson98 MarkusPettersson98 merged commit 2ca3ca6 into main Dec 5, 2025
49 of 50 checks passed
@MarkusPettersson98 MarkusPettersson98 deleted the cleanup-talpid-net-ifreq branch December 5, 2025 13:22
Copy link
Contributor Author

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3 I might look into it! It would be a very welcome addition:-)

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @hulthe)


talpid-net/src/unix.rs line 74 at r2 (raw file):

Previously, hulthe (Joakim Hulthe) wrote…

Domain::IPV4

I know you didn't change this, but does it really work for ipv6?

That's a great question. I'll run some checks and try to dig into it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants