views:

118

answers:

2

The following is a section of code which builds a list of IP addresses and their subnet masks from the local system, however the Warn function seems to get triggered regularly which should in theory be impossible - as it should not be possible to have an IPv4 address without the associated subnet mask[?].

    static NetworkUtil()
    {
        foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
        {
            foreach (UnicastIPAddressInformation address in ni.GetIPProperties().UnicastAddresses)
            {
                if (address.Address.AddressFamily == AddressFamily.InterNetwork)
                {
                    if (address.IPv4Mask != null)
                    {
                        m_subnets.Add(address.Address, address.IPv4Mask);
                    }
                    else
                    {
                        m_log.Warn("[NetworkUtil] Found IPv4 Address without Subnet Mask!?");
                    }
                }
            }
        }
    }
A: 

That is rather odd; obviously the semantics of this are not quite what one could reasonably expect.

Can you check to see if the subnet masks of the interfaces in question are classful? I.e., for those interface addresses where IPv4Mask is null, are they class A, B and C addresses with a network mask of /8, /16 and /24 respectively?

Perhaps this function is not really a member of the CIDR world, and they really do only "subnet" masks; an address without one is assumed to use the classful network mask.

Curt Sampson
A: 

It is because you have come across a loop back adapter. ie 127.0.0.1 These have the IPv4Mask set to null. Refer to ni.Name to see if I am right.

To check for a loopback adapter do...

if (ni.NetworkInterfaceType != NetworkInterfaceType.Loopback) ...

DJA