Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MTU value error #15

Open
vnt-dev opened this issue Jan 15, 2025 · 3 comments
Open

MTU value error #15

vnt-dev opened this issue Jan 15, 2025 · 3 comments

Comments

@vnt-dev
Copy link
Contributor

vnt-dev commented Jan 15, 2025

pub(crate) fn get_adapter_mtu(luid: &NET_LUID_LH) -> std::io::Result<usize> {

The MTU retrieved is for IPv6, but the MTU is set for IPv4 in the command line. Therefore, the retrieved MTU is always 65535.

@ssrlive
Copy link
Member

ssrlive commented Jan 15, 2025

It looks like Microsoft's API is a mess. It is impossible to get the MTU correctly.
The right side of the figure below shows the result of running the function GetAdaptersAddresses.

The current solution may only be through the command line and then parse the results.
After obtaining the MTU of ipv4 and ipv6 respectively, they are returned in the form of tuples.

(Some(1450), None)

image

Maybe this feature will be completed sometime in the future.

netsh interface ipv4 set interface "wintun" mtu=1450
netsh interface ipv6 set interface "wintun" mtu=2450

netsh interface ipv4 show subinterface
netsh interface ipv6 show subinterface

@vnt-dev
Copy link
Contributor Author

vnt-dev commented Jan 15, 2025

This method can retrieve the MTU as expected.

pub fn get_mtu_by_index(index: u32, is_v4: bool) -> io::Result<u32> {
    // https://learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-getipinterfacetable#examples
    let mut if_table: *mut MIB_IPINTERFACE_TABLE = ptr::null_mut();
    let mut mtu = None;
    unsafe {
        if GetIpInterfaceTable(if is_v4 { AF_INET } else { AF_INET6 }, &mut if_table as _)
            != NO_ERROR
        {
            return Err(io::Error::last_os_error());
        }
        let ifaces = unsafe {
            std::slice::from_raw_parts::<MIB_IPINTERFACE_ROW>(
                &(*if_table).Table[0],
                (*if_table).NumEntries as usize,
            )
        };
        for x in ifaces {
            if x.InterfaceIndex == index {
                mtu = Some(x.NlMtu);
                break;
            }
        }
        windows_sys::Win32::NetworkManagement::IpHelper::FreeMibTable(if_table as _);
    }
    if let Some(mtu) = mtu {
        Ok(mtu)
    } else {
        Err(io::Error::from(io::ErrorKind::NotFound))
    }
}

@xmh0511
Copy link

xmh0511 commented Jan 16, 2025

The correct MTU value can be obtained either by https://crates.io/crates/mtu or copy the code from this crates into wintun-bindings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants