Close socket fd after getmtu ioctl

This commit is contained in:
Mathias Hall-Andersen
2019-11-28 09:52:59 +01:00
parent 68b04e8074
commit 549b2cf5d0

View File

@@ -130,6 +130,12 @@ impl Writer for LinuxTunWriter {
} }
fn get_ifindex(name: &[u8; libc::IFNAMSIZ]) -> i32 { fn get_ifindex(name: &[u8; libc::IFNAMSIZ]) -> i32 {
debug_assert_eq!(
name[libc::IFNAMSIZ - 1],
0,
"name buffer not null-terminated"
);
let name = *name; let name = *name;
let idx = unsafe { let idx = unsafe {
let ptr: *const libc::c_char = mem::transmute(&name); let ptr: *const libc::c_char = mem::transmute(&name);
@@ -145,6 +151,12 @@ fn get_mtu(name: &[u8; libc::IFNAMSIZ]) -> Result<usize, LinuxTunError> {
mtu: u32, mtu: u32,
} }
debug_assert_eq!(
name[libc::IFNAMSIZ - 1],
0,
"name buffer not null-terminated"
);
// create socket // create socket
let fd = unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM, 0) }; let fd = unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM, 0) };
if fd < 0 { if fd < 0 {
@@ -160,6 +172,11 @@ fn get_mtu(name: &[u8; libc::IFNAMSIZ]) -> Result<usize, LinuxTunError> {
let ptr: &libc::c_void = mem::transmute(&buf); let ptr: &libc::c_void = mem::transmute(&buf);
libc::ioctl(fd, libc::SIOCGIFMTU, ptr) libc::ioctl(fd, libc::SIOCGIFMTU, ptr)
}; };
// close socket
unsafe { libc::close(fd) };
// handle error from ioctl
if err != 0 { if err != 0 {
return Err(LinuxTunError::GetMTUIoctlFailed); return Err(LinuxTunError::GetMTUIoctlFailed);
} }