Bug 41897 - NotSupportedException thrown from IPInterfaceProperties.UnicastAddresses
Summary: NotSupportedException thrown from IPInterfaceProperties.UnicastAddresses
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: master
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-06-16 14:04 UTC by Mike Voorhees
Modified: 2016-06-21 14:04 UTC (History)
2 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

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

Note You need to log in before you can comment on or make changes to this bug.