3

Note: I am aware and of full knowledge what a NullReferenceException is and how to debug and find where it comes from - this is not the question here

My goal: For a project in Unity3D I am trying to find all my device's network interfaces and store their IpAddresses in a list for later usage. Later I will broadcast some message on all of them.

I followed How do I get the network interface and its right IPv4 address? and Broadcasting UDP message to all the available network cards (in particular this answer) among others and tried to come up with the following:

var addresses = new List<IPAddress>();

var allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

foreach (var networkInterface in allNetworkInterfaces)
{
    if (networkInterface.OperationalStatus != OperationalStatus.Up) continue;
    if (networkInterface.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Ethernet)continue;
    if (!networkInterface.SupportsMulticast) continue;
    if (!networkInterface.Supports(NetworkInterfaceComponent.IPv4)) continue;

    var ipProperties = networkInterface.GetIPProperties();
    if (ipProperties == null) continue;

    var ipv4Properties = ipProperties.GetIPv4Properties(); // !! EXCEPTION IS THROWN HERE !!
    if (ipv4Properties == null) continue;

    if (NetworkInterface.LoopbackInterfaceIndex == ipv4Properties.Index) continue;
            
    var unicastAddresses = ipProperties.UnicastAddresses;

    foreach (var unicastIpAddressInformation in unicastAddresses)
    {
        if (unicastIpAddressInformation.Address.AddressFamily != AddressFamily.InterNetwork) continue;

        addresses.Add(unicastIpAddressInformation.Address);
    }
}

// and yes I used Linq before but I converted it to the loop due to debug the exception ^^

The exception

System.NullReferenceException: Object reference not set to an instance of an object
at (wrapper managed-to-native) System.Net.NetworkInformation.Win32IPv4InterfaceProperties.GetPerAdapterInfo(int,System.Net.NetworkInformation.Win32_IP_PER_ADAPTER_INFO,int&)
at System.Net.NetworkInformation.Win32IPv4InterfaceProperties..ctor (System.Net.NetworkInformation.Win32_IP_ADAPTER_ADDRESSES addr, System.Net.NetworkInformation.Win32_MIB_IFROW mib) [0x00030] in <14e3453b740b4bd690e8d4e5a013a715>:0
at System.Net.NetworkInformation.Win32IPInterfaceProperties2.GetIPv4Properties () [0x00000] in <14e3453b740b4bd690e8d4e5a013a715>:0
at Broadcast.BroadCastTest (System.UInt16 broadcastPort, System.Net.Sockets.UdpClient client) [0x000ae] in C:\xxxxxx\Broadcast.cs:797

is thrown in

var ipv4Properties = ipProperties.GetIPv4Properties();

where ipProperties is not null so apparently it comes from somewhere inside of GetIPv4Properties. The only possibly thrown exception listed in the API would be a NetworkInformationException but not a NullReferenceException.

Also before I checked and made already sure that

networkInterface.Supports(NetworkInterfaceComponent.IPv4)

is true (it's my Ethernet interface so it clearly is an IPv4 interface).

So why is the exception thrown and how can I solve this?

Am I doing something stupid? ^^


Update

Following the principle, fix it by commenting it out for now I simply skip the loopBack check

// var ipv4Properties = ipProperties.GetIPv4Properties();
// if (ipv4Properties == null) continue;

// if (NetworkInterface.LoopbackInterfaceIndex == ipv4Properties.Index) continue;

Since afaik the loopback should already be covered since

networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Interesting problem, and a very frustrating one I'm sure.... Could this be the same as this unity bug? https://issuetracker.unity3d.com/issues/il2cpp-player-crashes-when-ipinterfaceproperties-dot-getipv4properties-is-called-in-an-il2cpp-build – Ruzihm Sep 25 '20 at 15:32
  • @Ruzihm hm interesting, might be indeed a bug .. especially since exactly this is used in so many examples .. would be strange if it just fails for me – derHugo Oct 05 '20 at 08:53

0 Answers0