Skip to content

Commit

Permalink
feat: replace deprecated ioctl with nix (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Feb 18, 2024
1 parent e762c41 commit 5e85ceb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tokio = { version = "1", features = ["net", "macros"], optional = true }
tokio-util = { version = "0.7", features = ["codec"], optional = true }

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
ioctl = { version = "0.8", package = "ioctl-sys" }
nix = { version = "0.27", features = ["ioctl"] }

[target.'cfg(target_os = "windows")'.dependencies]
wintun = { version = "0.3", features = ["panic_on_unsent_packets"] }
Expand Down
68 changes: 34 additions & 34 deletions src/platform/linux/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ impl Device {
let tun = Fd::new(libc::open(b"/dev/net/tun\0".as_ptr() as *const _, O_RDWR))
.map_err(|_| io::Error::last_os_error())?;

if tunsetiff(tun.0, &mut req as *mut _ as *mut _) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = tunsetiff(tun.0, &mut req as *mut _ as *mut _) {
return Err(io::Error::from(err).into());
}

queues.push(Queue {
Expand Down Expand Up @@ -128,8 +128,8 @@ impl Device {
/// Make the device persistent.
pub fn persist(&mut self) -> Result<()> {
unsafe {
if tunsetpersist(self.as_raw_fd(), &1) < 0 {
Err(io::Error::last_os_error().into())
if let Err(err) = tunsetpersist(self.as_raw_fd(), &1) {
Err(io::Error::from(err).into())
} else {
Ok(())
}
Expand All @@ -139,8 +139,8 @@ impl Device {
/// Set the owner of the device.
pub fn user(&mut self, value: i32) -> Result<()> {
unsafe {
if tunsetowner(self.as_raw_fd(), &value) < 0 {
Err(io::Error::last_os_error().into())
if let Err(err) = tunsetowner(self.as_raw_fd(), &value) {
Err(io::Error::from(err).into())
} else {
Ok(())
}
Expand All @@ -150,8 +150,8 @@ impl Device {
/// Set the group of the device.
pub fn group(&mut self, value: i32) -> Result<()> {
unsafe {
if tunsetgroup(self.as_raw_fd(), &value) < 0 {
Err(io::Error::last_os_error().into())
if let Err(err) = tunsetgroup(self.as_raw_fd(), &value) {
Err(io::Error::from(err).into())
} else {
Ok(())
}
Expand Down Expand Up @@ -221,8 +221,8 @@ impl D for Device {
value.len(),
);

if siocsifname(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifname(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

self.name = value.into();
Expand All @@ -235,8 +235,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifflags(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifflags(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

if value {
Expand All @@ -245,8 +245,8 @@ impl D for Device {
req.ifr_ifru.ifru_flags &= !(IFF_UP as c_short);
}

if siocsifflags(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifflags(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -257,8 +257,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifaddr(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifaddr(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

SockAddr::new(&req.ifr_ifru.ifru_addr).map(Into::into)
Expand All @@ -270,8 +270,8 @@ impl D for Device {
let mut req = self.request();
req.ifr_ifru.ifru_addr = SockAddr::from(value).into();

if siocsifaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifaddr(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -282,8 +282,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifdstaddr(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifdstaddr(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

SockAddr::new(&req.ifr_ifru.ifru_dstaddr).map(Into::into)
Expand All @@ -295,8 +295,8 @@ impl D for Device {
let mut req = self.request();
req.ifr_ifru.ifru_dstaddr = SockAddr::from(value).into();

if siocsifdstaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifdstaddr(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -307,8 +307,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifbrdaddr(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifbrdaddr(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

SockAddr::new(&req.ifr_ifru.ifru_broadaddr).map(Into::into)
Expand All @@ -320,8 +320,8 @@ impl D for Device {
let mut req = self.request();
req.ifr_ifru.ifru_broadaddr = SockAddr::from(value).into();

if siocsifbrdaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifbrdaddr(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -332,8 +332,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifnetmask(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifnetmask(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

SockAddr::new(&req.ifr_ifru.ifru_netmask).map(Into::into)
Expand All @@ -345,8 +345,8 @@ impl D for Device {
let mut req = self.request();
req.ifr_ifru.ifru_netmask = SockAddr::from(value).into();

if siocsifnetmask(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifnetmask(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -357,8 +357,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifmtu(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifmtu(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

Ok(req.ifr_ifru.ifru_mtu)
Expand All @@ -370,8 +370,8 @@ impl D for Device {
let mut req = self.request();
req.ifr_ifru.ifru_mtu = value;

if siocsifmtu(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifmtu(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand Down
36 changes: 18 additions & 18 deletions src/platform/linux/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@

//! Bindings to internal Linux stuff.

use ioctl::*;
use libc::{c_int, ifreq};
use nix::{ioctl_read_bad, ioctl_write_ptr, ioctl_write_ptr_bad};

ioctl!(bad read siocgifflags with 0x8913; ifreq);
ioctl!(bad write siocsifflags with 0x8914; ifreq);
ioctl!(bad read siocgifaddr with 0x8915; ifreq);
ioctl!(bad write siocsifaddr with 0x8916; ifreq);
ioctl!(bad read siocgifdstaddr with 0x8917; ifreq);
ioctl!(bad write siocsifdstaddr with 0x8918; ifreq);
ioctl!(bad read siocgifbrdaddr with 0x8919; ifreq);
ioctl!(bad write siocsifbrdaddr with 0x891a; ifreq);
ioctl!(bad read siocgifnetmask with 0x891b; ifreq);
ioctl!(bad write siocsifnetmask with 0x891c; ifreq);
ioctl!(bad read siocgifmtu with 0x8921; ifreq);
ioctl!(bad write siocsifmtu with 0x8922; ifreq);
ioctl!(bad write siocsifname with 0x8923; ifreq);
ioctl_read_bad!(siocgifflags, 0x8913, ifreq);
ioctl_write_ptr_bad!(siocsifflags, 0x8914, ifreq);
ioctl_read_bad!(siocgifaddr, 0x8915, ifreq);
ioctl_write_ptr_bad!(siocsifaddr, 0x8916, ifreq);
ioctl_read_bad!(siocgifdstaddr, 0x8917, ifreq);
ioctl_write_ptr_bad!(siocsifdstaddr, 0x8918, ifreq);
ioctl_read_bad!(siocgifbrdaddr, 0x8919, ifreq);
ioctl_write_ptr_bad!(siocsifbrdaddr, 0x891a, ifreq);
ioctl_read_bad!(siocgifnetmask, 0x891b, ifreq);
ioctl_write_ptr_bad!(siocsifnetmask, 0x891c, ifreq);
ioctl_read_bad!(siocgifmtu, 0x8921, ifreq);
ioctl_write_ptr_bad!(siocsifmtu, 0x8922, ifreq);
ioctl_write_ptr_bad!(siocsifname, 0x8923, ifreq);

ioctl!(write tunsetiff with b'T', 202; c_int);
ioctl!(write tunsetpersist with b'T', 203; c_int);
ioctl!(write tunsetowner with b'T', 204; c_int);
ioctl!(write tunsetgroup with b'T', 206; c_int);
ioctl_write_ptr!(tunsetiff, b'T', 202, c_int);
ioctl_write_ptr!(tunsetpersist, b'T', 203, c_int);
ioctl_write_ptr!(tunsetowner, b'T', 204, c_int);
ioctl_write_ptr!(tunsetgroup, b'T', 206, c_int);
56 changes: 28 additions & 28 deletions src/platform/macos/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl Device {
},
};

if ctliocginfo(tun.0, &mut info as *mut _ as *mut _) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = ctliocginfo(tun.0, &mut info as *mut _ as *mut _) {
return Err(io::Error::from(err).into());
}

let addr = sockaddr_ctl {
Expand Down Expand Up @@ -158,8 +158,8 @@ impl Device {
req.broadaddr = SockAddr::from(broadaddr).into();
req.mask = SockAddr::from(mask).into();

if siocaifaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocaifaddr(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand Down Expand Up @@ -223,8 +223,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifflags(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifflags(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

if value {
Expand All @@ -233,8 +233,8 @@ impl D for Device {
req.ifru.flags &= !(IFF_UP as c_short);
}

if siocsifflags(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifflags(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -245,8 +245,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifaddr(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifaddr(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

SockAddr::new(&req.ifru.addr).map(Into::into)
Expand All @@ -258,8 +258,8 @@ impl D for Device {
let mut req = self.request();
req.ifru.addr = SockAddr::from(value).into();

if siocsifaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifaddr(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -270,8 +270,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifdstaddr(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifdstaddr(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

SockAddr::new(&req.ifru.dstaddr).map(Into::into)
Expand All @@ -283,8 +283,8 @@ impl D for Device {
let mut req = self.request();
req.ifru.dstaddr = SockAddr::from(value).into();

if siocsifdstaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifdstaddr(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -295,8 +295,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifbrdaddr(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifbrdaddr(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

SockAddr::new(&req.ifru.broadaddr).map(Into::into)
Expand All @@ -308,8 +308,8 @@ impl D for Device {
let mut req = self.request();
req.ifru.broadaddr = SockAddr::from(value).into();

if siocsifbrdaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifbrdaddr(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -320,8 +320,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifnetmask(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifnetmask(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

SockAddr::unchecked(&req.ifru.addr).map(Into::into)
Expand All @@ -333,8 +333,8 @@ impl D for Device {
let mut req = self.request();
req.ifru.addr = SockAddr::from(value).into();

if siocsifnetmask(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifnetmask(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand All @@ -345,8 +345,8 @@ impl D for Device {
unsafe {
let mut req = self.request();

if siocgifmtu(self.ctl.as_raw_fd(), &mut req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocgifmtu(self.ctl.as_raw_fd(), &mut req) {
return Err(io::Error::from(err).into());
}

Ok(req.ifru.mtu)
Expand All @@ -358,8 +358,8 @@ impl D for Device {
let mut req = self.request();
req.ifru.mtu = value;

if siocsifmtu(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
if let Err(err) = siocsifmtu(self.ctl.as_raw_fd(), &req) {
return Err(io::Error::from(err).into());
}

Ok(())
Expand Down
Loading

0 comments on commit 5e85ceb

Please sign in to comment.