From 549b2cf5d0684fb331236b5bed9d47059ee0bee5 Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Thu, 28 Nov 2019 09:52:59 +0100 Subject: Close socket fd after getmtu ioctl --- src/platform/linux/tun.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/platform/linux/tun.rs') diff --git a/src/platform/linux/tun.rs b/src/platform/linux/tun.rs index d9bda07..2bac49f 100644 --- a/src/platform/linux/tun.rs +++ b/src/platform/linux/tun.rs @@ -130,6 +130,12 @@ impl Writer for LinuxTunWriter { } 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 idx = unsafe { let ptr: *const libc::c_char = mem::transmute(&name); @@ -145,6 +151,12 @@ fn get_mtu(name: &[u8; libc::IFNAMSIZ]) -> Result { mtu: u32, } + debug_assert_eq!( + name[libc::IFNAMSIZ - 1], + 0, + "name buffer not null-terminated" + ); + // create socket let fd = unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM, 0) }; if fd < 0 { @@ -160,6 +172,11 @@ fn get_mtu(name: &[u8; libc::IFNAMSIZ]) -> Result { let ptr: &libc::c_void = mem::transmute(&buf); libc::ioctl(fd, libc::SIOCGIFMTU, ptr) }; + + // close socket + unsafe { libc::close(fd) }; + + // handle error from ioctl if err != 0 { return Err(LinuxTunError::GetMTUIoctlFailed); } -- cgit v1.2.3-59-g8ed1b