views:

185

answers:

1

I'm working on pseudo-transport layer software that runs over UDP, providing reliable connection-oriented transmission, as an alternative to TCP. In order to maximize network efficiency, we query the MTU of the "best" network adapter upon initialization.

MIB_IFROW row = {0};
row.dwIndex = dwBestIfIndex;

dwRes = GetIfEntry(&row);

Searching online I found that you can use the following netsh commands to query for this same value, from a command prompt (not a C++ API)

netsh interface ipv4 show interfaces
netsh interface ipv4 show subinterfaces

The troubling issue is that while row.dwMtu may be set to 1500, snooping the network traffic on the sending laptop shows that our packets are fragmented into 1300 byte packets. netsh also reports that the MTU is 1300.

Clearly the value reported by netsh command is the actual used values. Anyone know what API I can call to get the same values as netsh?

+1  A: 

There are local MTU size and Path MTU (see RFC 1191). You can use commands like

ping -f -l 1464 www.stackoverflow.com
ping -f -l 1465 www.stackoverflow.com

to determine a Path MTU. Take in consideration the header size of ICMP packages and IP headers. See http://stackoverflow.com/questions/1605084/path-mtu-discovery for API solution.

UPDATED: Please no more such questions! It was very interesting for me to find the answer and I spend some hours for this. But... I found! To receive correct value of MTU size you should use GetIpInterfaceTable API which returns PMIB_IPINTERFACE_TABLE structure which has array of MIB_IPINTERFACE_ROW structures. MIB_IPINTERFACE_ROW hat InterfaceIndex which helps you to identify IP interface, it is the same as in other well known IP Helper functions. You can also use ConvertInterfaceLuidToNameW function to receive from another field InterfaceLuid the interface name.

But the most interesting thing is NlMtu field

typedef struct _MIB_IPINTERFACE_ROW {
    ADDRESS_FAMILY Family;
    NET_LUID InterfaceLuid;
    NET_IFINDEX InterfaceIndex;
    // ...
    ULONG NlMtu;
    // ...
};

In the documentation (see http://msdn.microsoft.com/en-us/library/aa814496(v=VS.85).aspx) it is described as "The network layer MTU size, in bytes.". The same text and no more you will find also in Windows Driver Kit documentation (see http://msdn.microsoft.com/en-us/library/ff559254(VS.85).aspx). This field NlMtu is what you are searching for. My computer for example is connected to the Internet through a DSL router and NlMtu is not 1500 like it would be without the router, but has the correct value 1492 instead. In your case it must be 1300.

If you have one of the last versions of Microsoft SDK installed, then you will find in C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\netds\iphelp\Netinfo directory an example which use GetIpInterfaceTable API. Just set a brakepoint on the line about 270 in the netinfo.c file and you will see in InterfaceTable->Table[i].NlMtu the correct value of IP MTU of the corresponding interface adapter. If you verify in registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces{34407201-997C-41FF-9EBF-1B7D6DF92B38} which value has MTU REG_DWORD for your Interfaces card ({34407201-997C-41FF-9EBF-1B7D6DF92B38} in my case) it must be the same value.

The function GetIpInterfaceTable exists starting with Vista, but how you can see from the name PMIB_IPINTERFACE_TABLE the values come from MIB information of TCP adapter see (Windows DDK). Some years ago it was no IP helper DLL at all and to receive the information which displays IpConfig.exe one has to use CreateFile and DeviceIoControl to give such informations from TCP TDI driver (see constants like L"\Device\Tcp" and IOCTL_TCP_QUERY_INFORMATION_EX in tcpioctl.h). So perhaps it is possible to give the same information on Windows XP, but it is not important.

Oleg
Thanks for the response Oleg. I'm not looking to determine the MTU, as it seems to me that there are at least two agents that have already determined the MTU, and each provides a way for querying the value:1\ GetIfEntry() and2\Whatever netsh uses.What I'm looking for is the API that netsh uses.
ChrisJ
@ChrisJ I wrote in my answer, that you don't should ask more such questions. I means this of cause not sireously :-). It's a very good question, but not a simple one.
Oleg
@Oleg - thanks for the solution. I haven't had a chance to test it, but I've passed it along to my colleague. We'll provide an update soon.
ChrisJ