Beispiel #1
0
        internal static IPAddressInformationCollection ToAddressInformationCollection(IntPtr ptr, IPVersion versionSupported)
        {
            IPAddressInformationCollection informations = new IPAddressInformationCollection();

            if (ptr != IntPtr.Zero)
            {
                IPEndPoint       point;
                IpAdapterAddress adapterAddress = (IpAdapterAddress)Marshal.PtrToStructure(ptr, typeof(IpAdapterAddress));
                AddressFamily    family         = (adapterAddress.address.addressLength > 0x10) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
                SocketAddress    socketAddress  = new SocketAddress(family, adapterAddress.address.addressLength);
                Marshal.Copy(adapterAddress.address.address, socketAddress.m_Buffer, 0, adapterAddress.address.addressLength);
                if (family == AddressFamily.InterNetwork)
                {
                    point = (IPEndPoint)IPEndPoint.Any.Create(socketAddress);
                }
                else
                {
                    point = (IPEndPoint)IPEndPoint.IPv6Any.Create(socketAddress);
                }
                informations.InternalAdd(new SystemIPAddressInformation(adapterAddress, point.Address));
                while (adapterAddress.next != IntPtr.Zero)
                {
                    adapterAddress = (IpAdapterAddress)Marshal.PtrToStructure(adapterAddress.next, typeof(IpAdapterAddress));
                    family         = (adapterAddress.address.addressLength > 0x10) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
                    if (((family == AddressFamily.InterNetwork) && ((versionSupported & IPVersion.IPv4) > IPVersion.None)) || ((family == AddressFamily.InterNetworkV6) && ((versionSupported & IPVersion.IPv6) > IPVersion.None)))
                    {
                        socketAddress = new SocketAddress(family, adapterAddress.address.addressLength);
                        Marshal.Copy(adapterAddress.address.address, socketAddress.m_Buffer, 0, adapterAddress.address.addressLength);
                        if (family == AddressFamily.InterNetwork)
                        {
                            point = (IPEndPoint)IPEndPoint.Any.Create(socketAddress);
                        }
                        else
                        {
                            point = (IPEndPoint)IPEndPoint.IPv6Any.Create(socketAddress);
                        }
                        informations.InternalAdd(new SystemIPAddressInformation(adapterAddress, point.Address));
                    }
                }
            }
            return(informations);
        }
Beispiel #2
0
        static IPAddressInformationCollection Win32FromAnycast(IntPtr ptr)
        {
            var c = new IPAddressInformationCollection();
            Win32_IP_ADAPTER_ANYCAST_ADDRESS a;

            for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next)
            {
                a = (Win32_IP_ADAPTER_ANYCAST_ADDRESS)Marshal.PtrToStructure(p, typeof(Win32_IP_ADAPTER_ANYCAST_ADDRESS));
                c.InternalAdd(new SystemIPAddressInformation(
                                  a.Address.GetIPAddress(),
                                  a.LengthFlags.IsDnsEligible,
                                  a.LengthFlags.IsTransient));
            }
            return(c);
        }
Beispiel #3
0
        internal static IPAddressInformationCollection MarshalIpAddressInformationCollection(IntPtr ptr)
        {
            IPAddressInformationCollection addressList = new IPAddressInformationCollection();

            while (ptr != IntPtr.Zero)
            {
                IpAdapterAddress addressStructure =
                    (IpAdapterAddress)Marshal.PtrToStructure(ptr, typeof(IpAdapterAddress));
                IPAddress address = addressStructure.address.MarshalIPAddress();
                addressList.InternalAdd(new SystemIPAddressInformation(address, addressStructure.flags));

                ptr = addressStructure.next;
            }

            return(addressList);
        }