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

Fix double tunsetif and invalid cstring handling on linux #9

Merged
merged 1 commit into from
Dec 12, 2024
Merged
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
26 changes: 4 additions & 22 deletions neptun/src/device/tun_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl TunSocket {

pub fn new_from_fd(fd: RawFd) -> Result<TunSocket, Error> {
#[cfg(target_os = "linux")]
let mut ifr = ifreq {
let ifr = ifreq {
ifr_name: [0; IFNAMSIZ],
ifr_ifru: IfrIfru { ifru_intval: 0 },
};
Expand All @@ -140,29 +140,11 @@ impl TunSocket {
if flags & IFF_TUN as c_short == 0 {
return Err(Error::InvalidTunnelName);
}
let name = std::str::from_utf8(&ifr.ifr_name[..])
let name = std::ffi::CStr::from_bytes_until_nul(&ifr.ifr_name)
.map_err(|_| Error::InvalidTunnelName)?
.to_str()
.map_err(|_| Error::InvalidTunnelName)?
.to_owned();

#[cfg(target_os = "linux")]
{
ifr.ifr_ifru = IfrIfru {
ifru_flags: (IFF_TUN | IFF_MULTI_QUEUE) as _,
tomasz-grz marked this conversation as resolved.
Show resolved Hide resolved
};
if unsafe { ioctl(fd, TUNSETIFF as _, &ifr) } < 0 {
let error = Error::IOCtl(io::Error::last_os_error());
let flags = unsafe { format!("{:x}", ifr.ifr_ifru.ifru_flags) };
error!(
?error,
op = "TUNSETIFF",
name,
flags,
"Failed to configure tunnel flags"
);
return Err(error);
}
}

Ok(TunSocket { fd, name })
}

Expand Down
Loading