aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2019-11-28 09:52:59 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2019-11-28 09:52:59 +0100
commit549b2cf5d0684fb331236b5bed9d47059ee0bee5 (patch)
treeac51ca3c7bbfdd03e495062bd6a68c32f0f8b1db /src
parentFetch updated MTU on linux (diff)
downloadwireguard-rs-549b2cf5d0684fb331236b5bed9d47059ee0bee5.tar.xz
wireguard-rs-549b2cf5d0684fb331236b5bed9d47059ee0bee5.zip
Close socket fd after getmtu ioctl
Diffstat (limited to 'src')
-rw-r--r--src/platform/linux/tun.rs17
1 files changed, 17 insertions, 0 deletions
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<usize, LinuxTunError> {
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<usize, LinuxTunError> {
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);
}