Bug 41897

Summary: NotSupportedException thrown from IPInterfaceProperties.UnicastAddresses
Product: [Mono] Class Libraries Reporter: Mike Voorhees <michaelv>
Component: SystemAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, mono-bugs+mono
Priority: ---    
Version: master   
Target Milestone: Untriaged   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:

Description Mike Voorhees 2016-06-16 14:04:55 UTC
Test Case :

foreach (var localAdapter in NetworkInterface.GetAllNetworkInterfaces())
{
	var ipProperties = localAdapter.GetIPProperties();
	foreach (var localAddress in ipProperties.UnicastAddresses)
	{
		Console.WriteLine(localAddress);
	}
}

Console.WriteLine("All Good!");


Results in exception :

Unhandled Exception:
System.NotSupportedException: The collection is read-only.
  at System.Net.NetworkInformation.UnicastIPAddressInformationCollection.Add (System.Net.NetworkInformation.UnicastIPAddressInformation address) <0x2aa5018 + 0x00037> in <filename unknown>:0
  at System.Net.NetworkInformation.Win32IPInterfaceProperties2.Win32FromUnicast (System.Int32 ifIndex, System.IntPtr ptr) <0x2aa4c20 + 0x0013e> in <filename unknown>:0
  at System.Net.NetworkInformation.Win32IPInterfaceProperties2.get_UnicastAddresses () <0x2aa4388 + 0x00073> in <filename unknown>:0
  at DateTimeProblem.Program.Main (System.String[] args) <0x2aa11a8 + 0x00050> in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: Nested exception trying to figure out what went wrong


Notes:

UnicastIPAddressInformationCollection is coming from reference source now, while Win32IPInterfaceProperties2 is a mono implementation.  Looks like the assumption made by the following code that it can call Add on UnicastIPAddressInformationCollection is no longer true.

private static UnicastIPAddressInformationCollection Win32FromUnicast(int ifIndex, IntPtr ptr)
{
	UnicastIPAddressInformationCollection unicastIPAddressInformationCollection = new UnicastIPAddressInformationCollection();
	IntPtr intPtr = ptr;
	while (intPtr != IntPtr.Zero)
	{
		Win32_IP_ADAPTER_UNICAST_ADDRESS info = (Win32_IP_ADAPTER_UNICAST_ADDRESS)Marshal.PtrToStructure(intPtr, typeof(Win32_IP_ADAPTER_UNICAST_ADDRESS));
		unicastIPAddressInformationCollection.Add(new Win32UnicastIPAddressInformation(ifIndex, info));
		intPtr = info.Next;
	}
	return unicastIPAddressInformationCollection;
}
Comment 1 Marek Safar 2016-06-21 14:04:07 UTC
Fixed in master and Mono 4.5.1