Skip to content

Commit

Permalink
issues of setting MTU
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Dec 12, 2024
1 parent 42f720b commit 61d39b0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use crate::{
util::get_active_network_interface_gateways,
};
#[doc(hidden)]
pub use util::{format_message, get_wintun_bin_pattern_path, run_command};
pub use util::{format_message, get_wintun_bin_pattern_path, run_command, set_adapter_mtu};

pub use windows_sys::Win32::{Foundation::HANDLE, NetworkManagement::Ndis::NET_LUID_LH};

Expand Down
30 changes: 26 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,20 +424,42 @@ pub(crate) fn get_os_error_from_id(id: i32) -> std::io::Result<()> {
}
}

pub(crate) fn set_adapter_mtu(name: &str, mtu: usize) -> std::io::Result<()> {
// command line: `netsh interface ipv4 set subinterface "MyAdapter" mtu=1500 store=persistent`
pub fn set_adapter_mtu(name: &str, mtu: usize) -> std::io::Result<()> {
// command line: `netsh interface ipv4 set subinterface "MyAdapter" mtu=1500`
let args = &[
"interface",
"ipv4",
"set",
"subinterface",
&format!("\"{}\"", name),
&format!("mtu={}", mtu),
"store=persistent",
];
if let Err(e) = run_command("netsh", args) {
log::error!("Failed to set MTU for adapter: {}", e);
return Err(e);
set_adapter_mtu2(name, mtu)?;
}
Ok(())
}

/// FIXME: This function perhapes is not working as expected, so don't use it for now.
pub fn set_adapter_mtu2(name: &str, mtu: usize) -> std::io::Result<()> {
use windows_sys::Win32::NetworkManagement::IpHelper::{GetIfEntry, SetIfEntry, MIB_IFROW};
let luid = crate::ffi::alias_to_luid(name)?;
let index = crate::ffi::luid_to_index(&luid)?;

let mut row: MIB_IFROW = unsafe { std::mem::zeroed() };
row.dwIndex = index;

let v0 = unsafe { GetIfEntry(&mut row) };
if v0 != NO_ERROR {
let info = format_message(v0)?;
return Err(std::io::Error::new(std::io::ErrorKind::Other, info));
}
row.dwMtu = mtu as u32;
let v2 = unsafe { SetIfEntry(&row) };
if v2 != NO_ERROR {
let info = format_message(v2)?;
return Err(std::io::Error::new(std::io::ErrorKind::Other, info));
}
Ok(())
}
Expand Down

0 comments on commit 61d39b0

Please sign in to comment.