예제 #1
0
        //private IP_ADAPTER_ADDRESSES ipaa;

        internal IPInterfaceProperties(IP_ADAPTER_ADDRESSES addresses)
        {
            //this.ipaa = addresses;
            dnsSuffix           = addresses.DnsSuffix;
            isDynamicDnsEnabled = addresses.Flags.HasFlag(IP_ADAPTER.IP_ADAPTER_DDNS_ENABLED);
        }
        /// <summary>
        /// Returns an object that describes the configuration of this network interface.
        /// </summary>
        /// <returns>An <see cref="IPInterfaceProperties"/> object that describes this network interface.</returns>
        /// <remarks>Note that the information in the object returned by this method reflects the interfaces as of the time the array is created.
        /// This information is not updated dynamically.
        /// <list type="table"><listheader><term>Platforms Supported</term><description></description></listheader>
        /// <item><term>Windows Mobile</term><description>Windows Mobile Version 5.0 and later</description></item>
        /// <item><term>Windows Embedded Compact</term><description>Windows CE .NET 4.1 and later</description></item>
        /// </list></remarks>
        public IPInterfaceProperties GetIPProperties()
        {
            IPInterfaceProperties ipp = null;

            IntPtr buffer    = IntPtr.Zero;
            int    bufferlen = 0;
            int    result    = NativeMethods.GetAdaptersAddresses(0, (GAA_FLAG)0, IntPtr.Zero, buffer, ref bufferlen);

            buffer = Marshal.AllocHGlobal(bufferlen);
            try
            {
                result = NativeMethods.GetAdaptersAddresses(0, (GAA_FLAG)0, IntPtr.Zero, buffer, ref bufferlen);
                if (result == 0)
                {
                    IP_ADAPTER_ADDRESSES ipaa = (IP_ADAPTER_ADDRESSES)Marshal.PtrToStructure(buffer, typeof(IP_ADAPTER_ADDRESSES));
                    while ((ipaa.IfIndex != this.row.dwIndex) && (ipaa.Next != IntPtr.Zero))
                    {
                        ipaa = (IP_ADAPTER_ADDRESSES)Marshal.PtrToStructure(ipaa.Next, typeof(IP_ADAPTER_ADDRESSES));
                    }
                    if (ipaa.IfIndex != this.row.dwIndex)
                    {
                        return(null);
                    }

                    ipp = new IPInterfaceProperties(ipaa);
                    if (ipaa.FirstAnycastAddress != IntPtr.Zero)
                    {
                        IP_ADAPTER_ANYCAST_ADDRESS aa = (IP_ADAPTER_ANYCAST_ADDRESS)Marshal.PtrToStructure(ipaa.FirstAnycastAddress, typeof(IP_ADAPTER_ANYCAST_ADDRESS));
                        ipp.AnycastAddresses.InternalAdd(new IPAddressInformation(aa));
                        while (aa.Next != IntPtr.Zero)
                        {
                            aa = (IP_ADAPTER_ANYCAST_ADDRESS)Marshal.PtrToStructure(aa.Next, typeof(IP_ADAPTER_ANYCAST_ADDRESS));
                            ipp.AnycastAddresses.InternalAdd(new IPAddressInformation(aa));
                        }
                    }
                    if (ipaa.FirstMulticastAddress != IntPtr.Zero)
                    {
                        IP_ADAPTER_MULTICAST_ADDRESS ma = (IP_ADAPTER_MULTICAST_ADDRESS)Marshal.PtrToStructure(ipaa.FirstMulticastAddress, typeof(IP_ADAPTER_MULTICAST_ADDRESS));
                        ipp.MulticastAddresses.InternalAdd(new MulticastIPAddressInformation(ma));

                        while (ma.Next != IntPtr.Zero)
                        {
                            ma = (IP_ADAPTER_MULTICAST_ADDRESS)Marshal.PtrToStructure(ma.Next, typeof(IP_ADAPTER_MULTICAST_ADDRESS));
                            ipp.MulticastAddresses.InternalAdd(new MulticastIPAddressInformation(ma));
                        }
                    }
                    if (ipaa.FirstUnicastAddress != IntPtr.Zero)
                    {
                        IP_ADAPTER_UNICAST_ADDRESS ua = (IP_ADAPTER_UNICAST_ADDRESS)Marshal.PtrToStructure(ipaa.FirstUnicastAddress, typeof(IP_ADAPTER_UNICAST_ADDRESS));
                        ipp.UnicastAddresses.InternalAdd(new UnicastIPAddressInformation(ua));

                        while (ua.Next != IntPtr.Zero)
                        {
                            ua = (IP_ADAPTER_UNICAST_ADDRESS)Marshal.PtrToStructure(ua.Next, typeof(IP_ADAPTER_UNICAST_ADDRESS));
                            ipp.UnicastAddresses.InternalAdd(new UnicastIPAddressInformation(ua));
                        }
                    }
                    if (ipaa.FirstDnsServerAddress != IntPtr.Zero)
                    {
                        IP_ADAPTER_DNS_SERVER_ADDRESS da = (IP_ADAPTER_DNS_SERVER_ADDRESS)Marshal.PtrToStructure(ipaa.FirstDnsServerAddress, typeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
                        ipp.DnsAddresses.InternalAdd(IPAddressInformation.GetAddressFromSocketAddress(da.Address.lpSockaddr));

                        while (da.Next != IntPtr.Zero)
                        {
                            da = (IP_ADAPTER_DNS_SERVER_ADDRESS)Marshal.PtrToStructure(ipaa.FirstDnsServerAddress, typeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
                            ipp.DnsAddresses.InternalAdd(IPAddressInformation.GetAddressFromSocketAddress(da.Address.lpSockaddr));
                        }
                    }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }

            return(ipp);
        }