aboutsummaryrefslogtreecommitdiffstats
path: root/src/platform/linux/tun.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-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);
}